forked from plusone/plusone-commons
test: 完善 MapModifier 的单元测试
This commit is contained in:
@@ -16,36 +16,42 @@
|
||||
|
||||
package xyz.zhouxy.plusone.commons.collection;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
// TODO 完善单元测试
|
||||
@Slf4j
|
||||
public class MapModifierTests {
|
||||
|
||||
private static final String APP_START_ID = UUID.randomUUID().toString();
|
||||
private static final String LOCKED = "LOCKED";
|
||||
|
||||
private static final Map<String, Object> commonProperties = ImmutableMap.<String, Object>builder()
|
||||
private static final Map<String, String> commonProperties = ImmutableMap.<String, String>builder()
|
||||
.put("channel", "MOBILE")
|
||||
.put("appStartId", APP_START_ID)
|
||||
.build();
|
||||
|
||||
@Test
|
||||
void initMap() {
|
||||
Map<String, Object> expected = new HashMap<String, Object>() {
|
||||
void demo() {
|
||||
Map<String, String> expected = new HashMap<String, String>() {
|
||||
{
|
||||
put("channel", "MOBILE");
|
||||
put("appStartId", APP_START_ID);
|
||||
@@ -55,31 +61,33 @@ public class MapModifierTests {
|
||||
};
|
||||
|
||||
// MapModifier
|
||||
MapModifier<String, Object> modifier = new MapModifier<String, Object>()
|
||||
MapModifier<String, String> modifier = new MapModifier<String, String>()
|
||||
.putAll(commonProperties)
|
||||
.put("username", "Ben")
|
||||
.put("accountStatus", LOCKED);
|
||||
|
||||
// 从 Supplier 中获取 Map,并修改数据
|
||||
HashMap<String, Object> hashMap1 = modifier.getAndModify(HashMap::new);
|
||||
HashMap<String, String> hashMap1 = modifier.getAndModify(HashMap::new);
|
||||
assertEquals(expected, hashMap1);
|
||||
|
||||
// 可以灵活使用不同 Map 类型的不同构造器
|
||||
HashMap<String, Object> hashMap2 = modifier.getAndModify(() -> new HashMap<>(8));
|
||||
HashMap<String, String> hashMap2 = modifier.getAndModify(() -> new HashMap<>(8));
|
||||
assertEquals(expected, hashMap2);
|
||||
|
||||
// HashMap<String, Object> hashMap3 = modifier.getAndModify(() -> new HashMap<>(anotherMap));
|
||||
TreeMap<String, Object> treeMap = modifier.getAndModify(TreeMap::new);
|
||||
// HashMap<String, String> hashMap3 = modifier.getAndModify(() -> new HashMap<>(anotherMap));
|
||||
TreeMap<String, String> treeMap = modifier.getAndModify(TreeMap::new);
|
||||
assertEquals(expected, treeMap);
|
||||
ConcurrentHashMap<String, Object> concurrentHashMap = modifier.getAndModify(ConcurrentHashMap::new);
|
||||
ConcurrentHashMap<String, String> concurrentHashMap = modifier.getAndModify(ConcurrentHashMap::new);
|
||||
assertEquals(expected, concurrentHashMap);
|
||||
|
||||
assertNull(modifier.getAndModify(() -> (Map<String, String>) null));
|
||||
|
||||
// 修改已有的 Map
|
||||
Map<String, Object> srcMap = new HashMap<>();
|
||||
Map<String, String> srcMap = new HashMap<>();
|
||||
srcMap.put("srcKey1", "srcValue1");
|
||||
srcMap.put("srcKey2", "srcValue2");
|
||||
modifier.modify(srcMap);
|
||||
assertEquals(new HashMap<String, Object>() {
|
||||
assertEquals(new HashMap<String, String>() {
|
||||
{
|
||||
putAll(commonProperties);
|
||||
put("username", "Ben");
|
||||
@@ -89,8 +97,10 @@ public class MapModifierTests {
|
||||
}
|
||||
}, srcMap);
|
||||
|
||||
assertDoesNotThrow(() -> modifier.modify((Map<String, String>) null));
|
||||
|
||||
// 创建一个有初始化数据的不可变的 {@code Map}
|
||||
Map<String, Object> unmodifiableMap = modifier.getUnmodifiableMap();
|
||||
Map<String, String> unmodifiableMap = modifier.getUnmodifiableMap();
|
||||
assertEquals(expected, unmodifiableMap);
|
||||
assertThrows(UnsupportedOperationException.class,
|
||||
() -> unmodifiableMap.put("key", "value"));
|
||||
@@ -99,13 +109,13 @@ public class MapModifierTests {
|
||||
@Test
|
||||
void createAndInitData() {
|
||||
// 链式调用创建并初始化数据
|
||||
HashMap<String, Object> map = new MapModifier<String, Object>()
|
||||
HashMap<String, String> map = new MapModifier<String, String>()
|
||||
.putAll(commonProperties)
|
||||
.put("username", "Ben")
|
||||
.put("accountStatus", LOCKED)
|
||||
.getAndModify(HashMap::new);
|
||||
|
||||
HashMap<String, Object> expected = new HashMap<String, Object>() {
|
||||
HashMap<String, String> expected = new HashMap<String, String>() {
|
||||
{
|
||||
put("channel", "MOBILE");
|
||||
put("appStartId", APP_START_ID);
|
||||
@@ -115,4 +125,192 @@ public class MapModifierTests {
|
||||
};
|
||||
assertEquals(expected, map);
|
||||
}
|
||||
|
||||
@Test
|
||||
void put() {
|
||||
Map<String, String> map = new MapModifier<String, String>()
|
||||
.put("key1", "value0")
|
||||
.put("key1", "value1")
|
||||
.getAndModify(HashMap::new);
|
||||
|
||||
assertEquals(new HashMap<String, String>() {
|
||||
{
|
||||
put("key1", "value0");
|
||||
put("key1", "value1");
|
||||
}
|
||||
}, map);
|
||||
|
||||
new MapModifier<String, String>()
|
||||
.put("key1", "newValue1")
|
||||
.put("key2", null)
|
||||
.modify(map);
|
||||
|
||||
assertEquals("newValue1", map.get("key1"));
|
||||
assertTrue(map.containsKey("key2"));
|
||||
assertNull(map.get("key2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void putIfAbsent() {
|
||||
Map<String, String> map = new MapModifier<String, String>()
|
||||
.putIfAbsent("key1", null)
|
||||
.putIfAbsent("key1", "value1")
|
||||
.putIfAbsent("key1", "value2")
|
||||
.getAndModify(HashMap::new);
|
||||
|
||||
assertEquals(new HashMap<String, String>() {
|
||||
{
|
||||
putIfAbsent("key1", null);
|
||||
putIfAbsent("key1", "value1");
|
||||
putIfAbsent("key1", "value2");
|
||||
}
|
||||
}, map);
|
||||
|
||||
new MapModifier<String, String>()
|
||||
.putIfAbsent("key1", "newValue1")
|
||||
.modify(map);
|
||||
|
||||
assertTrue(map.containsKey("key1"));
|
||||
assertEquals("value1", map.get("key1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void putAll_map() {
|
||||
Map<String, String> entries = new HashMap<String, String>() {
|
||||
{
|
||||
put("key1", "value1");
|
||||
put("key2", "value2");
|
||||
}
|
||||
};
|
||||
Map<String, String> map = new MapModifier<String, String>()
|
||||
.putAll((Map<String, String>) null)
|
||||
.putAll(Collections.emptyMap())
|
||||
.putAll(entries)
|
||||
.getAndModify(HashMap::new);
|
||||
assertEquals(entries, map);
|
||||
new MapModifier<String, String>()
|
||||
.putAll(new HashMap<String, String>() {
|
||||
{
|
||||
put("key2", "newValue2");
|
||||
put("key3", "value3");
|
||||
}
|
||||
})
|
||||
.modify(map);
|
||||
assertEquals(new HashMap<String, String>() {
|
||||
{
|
||||
put("key1", "value1");
|
||||
put("key2", "value2");
|
||||
put("key2", "newValue2");
|
||||
put("key3", "value3");
|
||||
}
|
||||
}, map);
|
||||
}
|
||||
|
||||
@Test
|
||||
void putAll_entries() {
|
||||
Map<String, String> entries = new HashMap<String, String>() {
|
||||
{
|
||||
put("key1", "value1");
|
||||
put("key2", "value2");
|
||||
}
|
||||
};
|
||||
Map<String, String> map = new MapModifier<String, String>()
|
||||
.putAll(new SimpleEntry<>("key1", "value1"),
|
||||
new SimpleEntry<>("key2", "value2"))
|
||||
.getAndModify(HashMap::new);
|
||||
assertEquals(entries, map);
|
||||
new MapModifier<String, String>()
|
||||
.putAll()
|
||||
.putAll(new SimpleEntry<>("key2", "newValue2"),
|
||||
new SimpleEntry<>("key3", "value3"))
|
||||
.modify(map);
|
||||
assertEquals(new HashMap<String, String>() {
|
||||
{
|
||||
put("key1", "value1");
|
||||
put("key2", "value2");
|
||||
put("key2", "newValue2");
|
||||
put("key3", "value3");
|
||||
}
|
||||
}, map);
|
||||
}
|
||||
|
||||
@Test
|
||||
void computeIfAbsent_keyAndFunction() {
|
||||
Map<String, String> map = new MapModifier<String, String>()
|
||||
.computeIfAbsent("key1", k -> null)
|
||||
.computeIfAbsent("key1", k -> "value1")
|
||||
.computeIfAbsent("key1", k -> "value2")
|
||||
.getAndModify(HashMap::new);
|
||||
|
||||
assertEquals(new HashMap<String, String>() {
|
||||
{
|
||||
computeIfAbsent("key1", k -> null);
|
||||
computeIfAbsent("key1", k -> "value1");
|
||||
computeIfAbsent("key1", k -> "value2");
|
||||
}
|
||||
}, map);
|
||||
|
||||
new MapModifier<String, String>()
|
||||
.computeIfAbsent("key1", k -> "newValue1")
|
||||
.modify(map);
|
||||
|
||||
assertTrue(map.containsKey("key1"));
|
||||
assertEquals("value1", map.get("key1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void computeIfPresent_keyAndBiFunction() {
|
||||
Map<String, String> map = new HashMap<String, String>() {{
|
||||
put("key1", "value1");
|
||||
}};
|
||||
new MapModifier<String, String>()
|
||||
.computeIfPresent("key1", (k, v) -> k + v)
|
||||
.computeIfPresent("key2", (k, v) -> k + v)
|
||||
.modify(map);
|
||||
assertEquals(new HashMap<String, String>() {{
|
||||
put("key1", "key1value1");
|
||||
}}, map);
|
||||
}
|
||||
|
||||
@Test
|
||||
void remove() {
|
||||
Map<String, String> map = new HashMap<String, String>() {{
|
||||
put("key1", "value1");
|
||||
put("key2", "value2");
|
||||
}};
|
||||
new MapModifier<String, String>()
|
||||
.remove("key2")
|
||||
.modify(map);
|
||||
assertEquals(new HashMap<String, String>() {{
|
||||
put("key1", "value1");
|
||||
}}, map);
|
||||
}
|
||||
|
||||
@Test
|
||||
void clear() {
|
||||
Map<String, String> map = new HashMap<String, String>() {{
|
||||
put("key1", "value1");
|
||||
put("key2", "value2");
|
||||
}};
|
||||
new MapModifier<String, String>()
|
||||
.clear()
|
||||
.modify(map);
|
||||
assertTrue(map.isEmpty());
|
||||
}
|
||||
|
||||
@Getter
|
||||
static class SimpleEntry<K, V> implements Map.Entry<K, V> {
|
||||
private final K key;
|
||||
private final V value;
|
||||
|
||||
public SimpleEntry(K key, V value) {
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public V setValue(@Nullable V value) {
|
||||
throw new UnsupportedOperationException("Unimplemented method 'setValue'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user