1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.collections4.map;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertFalse;
21 import static org.junit.jupiter.api.Assertions.assertNotNull;
22 import static org.junit.jupiter.api.Assertions.assertThrows;
23
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.Map;
27
28 import org.apache.commons.collections4.IterableMap;
29 import org.apache.commons.collections4.Predicate;
30 import org.apache.commons.collections4.functors.TruePredicate;
31 import org.junit.jupiter.api.Test;
32
33
34
35
36
37
38
39
40 public class PredicatedMapTest<K, V> extends AbstractIterableMapTest<K, V> {
41
42 protected static final Predicate<Object> truePredicate = TruePredicate.<Object>truePredicate();
43
44 protected static final Predicate<Object> testPredicate = String.class::isInstance;
45
46 protected IterableMap<K, V> decorateMap(final Map<K, V> map, final Predicate<? super K> keyPredicate,
47 final Predicate<? super V> valuePredicate) {
48 return PredicatedMap.predicatedMap(map, keyPredicate, valuePredicate);
49 }
50
51 @Override
52 public String getCompatibilityVersion() {
53 return "4";
54 }
55
56 @Override
57 public IterableMap<K, V> makeObject() {
58 return decorateMap(new HashMap<>(), truePredicate, truePredicate);
59 }
60
61 public IterableMap<K, V> makeTestMap() {
62 return decorateMap(new HashMap<>(), testPredicate, testPredicate);
63 }
64
65 @Test
66 @SuppressWarnings("unchecked")
67 public void testEntrySet() {
68 Map<K, V> map = makeTestMap();
69 assertNotNull(map.entrySet(), "returned entryset should not be null");
70 map = decorateMap(new HashMap<>(), null, null);
71 map.put((K) "oneKey", (V) "oneValue");
72 assertEquals(1, map.entrySet().size(), "returned entryset should contain one entry");
73 map = decorateMap(map, null, null);
74 }
75
76 @Test
77 @SuppressWarnings("unchecked")
78 public void testPut() {
79 final Map<K, V> map = makeTestMap();
80 assertThrows(IllegalArgumentException.class, () -> map.put((K) "Hi", (V) Integer.valueOf(3)),
81 "Illegal value should raise IllegalArgument");
82
83 assertThrows(IllegalArgumentException.class, () -> map.put((K) Integer.valueOf(3), (V) "Hi"),
84 "Illegal key should raise IllegalArgument");
85
86 assertFalse(map.containsKey(Integer.valueOf(3)));
87 assertFalse(map.containsValue(Integer.valueOf(3)));
88
89 final Map<K, V> map2 = new HashMap<>();
90 map2.put((K) "A", (V) "a");
91 map2.put((K) "B", (V) "b");
92 map2.put((K) "C", (V) "c");
93 map2.put((K) "c", (V) Integer.valueOf(3));
94
95 assertThrows(IllegalArgumentException.class, () -> map.putAll(map2),
96 "Illegal value should raise IllegalArgument");
97
98 map.put((K) "E", (V) "e");
99 Iterator<Map.Entry<K, V>> iterator = map.entrySet().iterator();
100 Map.Entry<K, V> entry = iterator.next();
101 final Map.Entry<K, V> finalEntry = entry;
102 assertThrows(IllegalArgumentException.class, () -> finalEntry.setValue((V) Integer.valueOf(3)),
103 "Illegal value should raise IllegalArgument");
104
105 map.put((K) "F", (V) "f");
106 iterator = map.entrySet().iterator();
107 entry = iterator.next();
108 entry.setValue((V) "x");
109 }
110
111
112
113
114
115
116
117
118
119
120
121
122 }