From a700d287fe138ea45a793b8d83de96adc8344ef9 Mon Sep 17 00:00:00 2001 From: monsterxxp <37952446+monsterxxp@users.noreply.github.com> Date: Wed, 14 Sep 2022 21:35:54 +0800 Subject: [PATCH] add MapUtilTest test case (#699) --- .../hippo4j/config/toolkit/MapUtilTest.java | 111 ++++++++++++++++++ 1 file changed, 111 insertions(+) diff --git a/hippo4j-config/src/test/java/cn/hippo4j/config/toolkit/MapUtilTest.java b/hippo4j-config/src/test/java/cn/hippo4j/config/toolkit/MapUtilTest.java index a5574c98..e67f3910 100644 --- a/hippo4j-config/src/test/java/cn/hippo4j/config/toolkit/MapUtilTest.java +++ b/hippo4j-config/src/test/java/cn/hippo4j/config/toolkit/MapUtilTest.java @@ -17,8 +17,119 @@ package cn.hippo4j.config.toolkit; +import cn.hippo4j.common.toolkit.Assert; +import cn.hutool.core.collection.CollectionUtil; +import com.google.common.collect.ImmutableMap; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.function.BiFunction; + /** * MapUtil Test */ public class MapUtilTest { + + @Test + public void parseMapForFilterRetIsEmptyTest() { + Map map = ImmutableMap.of("abc", "123", "bcd", "456", "cde", "789"); + List ret = MapUtil.parseMapForFilter(map, "x"); + Assert.isTrue(CollectionUtil.isEmpty(ret)); + } + + @Test + public void parseMapForFilterRetIsNotEmptyTest() { + Map map = ImmutableMap.of("abc", "123", "bcd", "456", "cde", "789"); + List ret = MapUtil.parseMapForFilter(map, "b"); + Assert.isTrue(CollectionUtil.isNotEmpty(ret)); + } + + @Test + public void computeIfAbsentNotExistTargetTest() { + BiFunction mappingFunction = (a, b) -> a + b; + try { + MapUtil.computeIfAbsent(null, "key", mappingFunction, "param1", "param2"); + } catch (Exception e) { + if (e instanceof NullPointerException) { + Assert.isTrue(Objects.equals("target", e.getMessage())); + } + } + } + + @Test + public void computeIfAbsentNotExistKeyTest() { + Map map = new HashMap<>(); + map.put("abc", "123"); + BiFunction mappingFunction = (a, b) -> a + b; + try { + MapUtil.computeIfAbsent(map, null, mappingFunction, "param1", "param2"); + } catch (Exception e) { + if (e instanceof NullPointerException) { + Assert.isTrue(Objects.equals("key", e.getMessage())); + } + } + } + + @Test + public void computeIfAbsentNotExistMappingFunctionTest() { + Map map = new HashMap<>(); + map.put("abc", "123"); + try { + MapUtil.computeIfAbsent(map, "abc", null, "param1", "param2"); + } catch (Exception e) { + if (e instanceof NullPointerException) { + Assert.isTrue(Objects.equals("mappingFunction", e.getMessage())); + } + } + } + + @Test + public void computeIfAbsentNotExistParam1Test() { + Map map = new HashMap<>(); + map.put("abc", "123"); + BiFunction mappingFunction = (a, b) -> a + b; + try { + MapUtil.computeIfAbsent(map, "abc", mappingFunction, null, "param2"); + } catch (Exception e) { + if (e instanceof NullPointerException) { + Assert.isTrue(Objects.equals("param1", e.getMessage())); + } + } + } + + @Test + public void computeIfAbsentNotExistParam2Test() { + Map map = new HashMap<>(); + map.put("abc", "123"); + BiFunction mappingFunction = (a, b) -> a + b; + try { + MapUtil.computeIfAbsent(map, "abc", mappingFunction, "param1", null); + } catch (Exception e) { + if (e instanceof NullPointerException) { + Assert.isTrue(Objects.equals("param2", e.getMessage())); + } + } + } + + @Test + public void computeIfAbsentValNotNullTest() { + Map map = new HashMap<>(); + map.put("abc", "123"); + BiFunction mappingFunction = (a, b) -> a + b; + Object ret = MapUtil.computeIfAbsent(map, "abc", mappingFunction, "param1", "param2"); + Assert.isTrue(Objects.equals("123", String.valueOf(ret))); + } + + @Test + public void computeIfAbsentValIsNullTest() { + Map map = new HashMap<>(); + map.put("abc", "123"); + BiFunction mappingFunction = (a, b) -> a + b; + Object ret = MapUtil.computeIfAbsent(map, "xyz", mappingFunction, "param1", "param2"); + Assert.isTrue(Objects.equals("param1param2", String.valueOf(ret))); + } + }