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.assertNull;
23 import static org.junit.jupiter.api.Assertions.assertSame;
24 import static org.junit.jupiter.api.Assertions.assertThrows;
25
26 import java.util.Comparator;
27 import java.util.HashMap;
28 import java.util.Iterator;
29 import java.util.Map;
30 import java.util.SortedMap;
31 import java.util.TreeMap;
32 import java.util.concurrent.ConcurrentSkipListMap;
33
34 import org.apache.commons.collections4.Predicate;
35 import org.apache.commons.collections4.functors.TruePredicate;
36 import org.junit.jupiter.api.Test;
37
38
39
40
41
42 public class PredicatedSortedMapTest<K, V> extends AbstractSortedMapTest<K, V> {
43
44 private final class ReverseStringComparator implements Comparator<K> {
45 @Override
46 public int compare(final K arg0, final K arg1) {
47 return ((String) arg1).compareTo((String) arg0);
48 }
49 }
50
51 protected static final Predicate<Object> truePredicate = TruePredicate.truePredicate();
52
53 protected static final Predicate<Object> testPredicate = String.class::isInstance;
54
55 protected final Comparator<K> reverseStringComparator = new ReverseStringComparator();
56
57 public PredicatedSortedMapTest() {
58 super(PredicatedSortedMapTest.class.getSimpleName());
59 }
60
61 protected SortedMap<K, V> decorateMap(final SortedMap<K, V> map, final Predicate<? super K> keyPredicate,
62 final Predicate<? super V> valuePredicate) {
63 return PredicatedSortedMap.predicatedSortedMap(map, keyPredicate, valuePredicate);
64 }
65
66 @Override
67 public String getCompatibilityVersion() {
68 return "4";
69 }
70
71 @Override
72 public boolean isAllowNullKey() {
73 return false;
74 }
75
76 @Override
77 public boolean isSubMapViewsSerializable() {
78
79 return false;
80 }
81
82 @Override
83 public SortedMap<K, V> makeObject() {
84 return decorateMap(new TreeMap<>(), truePredicate, truePredicate);
85 }
86
87 public SortedMap<K, V> makeTestMap() {
88 return decorateMap(new TreeMap<>(), testPredicate, testPredicate);
89 }
90
91 public SortedMap<K, V> makeTestMapWithComparator() {
92 return decorateMap(new ConcurrentSkipListMap<>(reverseStringComparator), testPredicate, testPredicate);
93 }
94
95
96 @Test
97 @SuppressWarnings("unchecked")
98 public void testEntrySet() {
99 SortedMap<K, V> map = makeTestMap();
100 assertNotNull(map.entrySet(), "returned entryset should not be null");
101 map = decorateMap(new TreeMap<>(), null, null);
102 map.put((K) "oneKey", (V) "oneValue");
103 assertEquals(1, map.entrySet().size(), "returned entryset should contain one entry");
104 map = decorateMap(map, null, null);
105 }
106
107 @Test
108 @SuppressWarnings("unchecked")
109 public void testPut() {
110 final Map<K, V> map = makeTestMap();
111 assertThrows(IllegalArgumentException.class, () -> map.put((K) "Hi", (V) Integer.valueOf(3)),
112 "Illegal value should raise IllegalArgument");
113
114 assertThrows(IllegalArgumentException.class, () -> map.put((K) Integer.valueOf(3), (V) "Hi"),
115 "Illegal key should raise IllegalArgument");
116
117 assertFalse(map.containsKey(Integer.valueOf(3)));
118 assertFalse(map.containsValue(Integer.valueOf(3)));
119
120 final Map<K, V> map2 = new HashMap<>();
121 map2.put((K) "A", (V) "a");
122 map2.put((K) "B", (V) "b");
123 map2.put((K) "C", (V) "c");
124 map2.put((K) "c", (V) Integer.valueOf(3));
125
126 assertThrows(IllegalArgumentException.class, () -> map.putAll(map2),
127 "Illegal value should raise IllegalArgument");
128
129 map.put((K) "E", (V) "e");
130 Iterator<Map.Entry<K, V>> iterator = map.entrySet().iterator();
131 Map.Entry<K, V> entry = iterator.next();
132 final Map.Entry<K, V> finalEntry = entry;
133 assertThrows(IllegalArgumentException.class, () -> finalEntry.setValue((V) Integer.valueOf(3)),
134 "Illegal value should raise IllegalArgument");
135
136 map.put((K) "F", (V) "f");
137 iterator = map.entrySet().iterator();
138 entry = iterator.next();
139 entry.setValue((V) "x");
140
141 }
142
143 @Test
144 @SuppressWarnings("unchecked")
145 public void testReverseSortOrder() {
146 final SortedMap<K, V> map = makeTestMapWithComparator();
147 map.put((K) "A", (V) "a");
148 map.put((K) "B", (V) "b");
149 assertThrows(IllegalArgumentException.class, () -> map.put(null, (V) "c"),
150 "Null key should raise IllegalArgument");
151 map.put((K) "C", (V) "c");
152 assertThrows(IllegalArgumentException.class, () -> map.put((K) "D", null),
153 "Null value should raise IllegalArgument");
154 assertEquals("A", map.lastKey(), "Last key should be A");
155 assertEquals("C", map.firstKey(), "First key should be C");
156 assertEquals("B", map.tailMap((K) "B").firstKey(),
157 "First key in tail map should be B");
158 assertEquals("B", map.headMap((K) "A").lastKey(),
159 "Last key in head map should be B");
160 assertEquals("B", map.subMap((K) "C", (K) "A").lastKey(),
161 "Last key in submap should be B");
162
163 final Comparator<? super K> c = map.comparator();
164 assertSame(c, reverseStringComparator, "reverse order, so comparator should be reverseStringComparator");
165 }
166
167 @Test
168 @SuppressWarnings("unchecked")
169 public void testSortOrder() {
170 final SortedMap<K, V> map = makeTestMap();
171 map.put((K) "A", (V) "a");
172 map.put((K) "B", (V) "b");
173 assertThrows(IllegalArgumentException.class, () -> map.put(null, (V) "c"),
174 "Null key should raise IllegalArgument");
175 map.put((K) "C", (V) "c");
176 assertThrows(IllegalArgumentException.class, () -> map.put((K) "D", null),
177 "Null value should raise IllegalArgument");
178 assertEquals("A", map.firstKey(), "First key should be A");
179 assertEquals("C", map.lastKey(), "Last key should be C");
180 assertEquals("B", map.tailMap((K) "B").firstKey(),
181 "First key in tail map should be B");
182 assertEquals("B", map.headMap((K) "C").lastKey(),
183 "Last key in head map should be B");
184 assertEquals("B", map.subMap((K) "A", (K) "C").lastKey(),
185 "Last key in submap should be B");
186
187 final Comparator<? super K> c = map.comparator();
188 assertNull(c, "natural order, so comparator should be null");
189 }
190
191
192
193
194
195
196
197
198
199
200
201
202 }