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 public class PredicatedMapTest<K, V> extends AbstractIterableMapTest<K, V> {
38
39 protected static final Predicate<Object> truePredicate = TruePredicate.<Object>truePredicate();
40
41 protected static final Predicate<Object> testPredicate = String.class::isInstance;
42
43 public PredicatedMapTest() {
44 super(PredicatedMapTest.class.getSimpleName());
45 }
46
47 protected IterableMap<K, V> decorateMap(final Map<K, V> map, final Predicate<? super K> keyPredicate,
48 final Predicate<? super V> valuePredicate) {
49 return PredicatedMap.predicatedMap(map, keyPredicate, valuePredicate);
50 }
51
52 @Override
53 public String getCompatibilityVersion() {
54 return "4";
55 }
56
57 @Override
58 public IterableMap<K, V> makeObject() {
59 return decorateMap(new HashMap<>(), truePredicate, truePredicate);
60 }
61
62 public IterableMap<K, V> makeTestMap() {
63 return decorateMap(new HashMap<>(), testPredicate, testPredicate);
64 }
65
66 @Test
67 @SuppressWarnings("unchecked")
68 public void testEntrySet() {
69 Map<K, V> map = makeTestMap();
70 assertNotNull(map.entrySet(), "returned entryset should not be null");
71 map = decorateMap(new HashMap<>(), null, null);
72 map.put((K) "oneKey", (V) "oneValue");
73 assertEquals(1, map.entrySet().size(), "returned entryset should contain one entry");
74 map = decorateMap(map, null, null);
75 }
76
77 @Test
78 @SuppressWarnings("unchecked")
79 public void testPut() {
80 final Map<K, V> map = makeTestMap();
81 assertThrows(IllegalArgumentException.class, () -> map.put((K) "Hi", (V) Integer.valueOf(3)),
82 "Illegal value should raise IllegalArgument");
83
84 assertThrows(IllegalArgumentException.class, () -> map.put((K) Integer.valueOf(3), (V) "Hi"),
85 "Illegal key should raise IllegalArgument");
86
87 assertFalse(map.containsKey(Integer.valueOf(3)));
88 assertFalse(map.containsValue(Integer.valueOf(3)));
89
90 final Map<K, V> map2 = new HashMap<>();
91 map2.put((K) "A", (V) "a");
92 map2.put((K) "B", (V) "b");
93 map2.put((K) "C", (V) "c");
94 map2.put((K) "c", (V) Integer.valueOf(3));
95
96 assertThrows(IllegalArgumentException.class, () -> map.putAll(map2),
97 "Illegal value should raise IllegalArgument");
98
99 map.put((K) "E", (V) "e");
100 Iterator<Map.Entry<K, V>> iterator = map.entrySet().iterator();
101 Map.Entry<K, V> entry = iterator.next();
102 final Map.Entry<K, V> finalEntry = entry;
103 assertThrows(IllegalArgumentException.class, () -> finalEntry.setValue((V) Integer.valueOf(3)),
104 "Illegal value should raise IllegalArgument");
105
106 map.put((K) "F", (V) "f");
107 iterator = map.entrySet().iterator();
108 entry = iterator.next();
109 entry.setValue((V) "x");
110 }
111
112
113
114
115
116
117
118
119
120
121
122
123 }