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.assertThrows;
22 import static org.junit.jupiter.api.Assertions.assertTrue;
23
24 import java.util.Collection;
25 import java.util.HashMap;
26 import java.util.Map;
27
28 import org.junit.jupiter.api.BeforeEach;
29 import org.junit.jupiter.api.Test;
30
31
32
33
34
35 public class CompositeMapTest<K, V> extends AbstractIterableMapTest<K, V> {
36
37
38 private boolean pass;
39
40 public CompositeMapTest() {
41 super(CompositeMapTest.class.getSimpleName());
42 }
43
44 @SuppressWarnings("unchecked")
45 private Map<K, V> buildOne() {
46 final HashMap<K, V> map = new HashMap<>();
47 map.put((K) "1", (V) "one");
48 map.put((K) "2", (V) "two");
49 return map;
50 }
51
52 @SuppressWarnings("unchecked")
53 public Map<K, V> buildTwo() {
54 final HashMap<K, V> map = new HashMap<>();
55 map.put((K) "3", (V) "three");
56 map.put((K) "4", (V) "four");
57 return map;
58 }
59
60 @Override
61 public String getCompatibilityVersion() {
62 return "4";
63 }
64
65 @Override
66 public CompositeMap<K, V> makeObject() {
67 final CompositeMap<K, V> map = new CompositeMap<>();
68 map.addComposited(new HashMap<>());
69 map.setMutator( new EmptyMapMutator<>() );
70 return map;
71 }
72
73 @BeforeEach
74 public void setUp() throws Exception {
75 this.pass = false;
76 }
77
78 @Test
79 @SuppressWarnings("unchecked")
80 public void testAddComposited() {
81 final CompositeMap<K, V> map = new CompositeMap<>(buildOne(), buildTwo());
82 final HashMap<K, V> three = new HashMap<>();
83 three.put((K) "5", (V) "five");
84 map.addComposited(null);
85 map.addComposited(three);
86 assertTrue(map.containsKey("5"));
87
88 assertThrows(IllegalArgumentException.class, () -> map.addComposited(three));
89 }
90
91 @Test
92 public void testGet() {
93 final CompositeMap<K, V> map = new CompositeMap<>(buildOne(), buildTwo());
94 assertEquals("one", map.get("1"));
95 assertEquals("four", map.get("4"));
96 }
97
98 @Test
99 @SuppressWarnings("unchecked")
100 public void testPut() {
101 final CompositeMap<K, V> map = new CompositeMap<>(buildOne(), buildTwo(),
102 new CompositeMap.MapMutator<K, V>() {
103 private static final long serialVersionUID = 1L;
104 @Override
105 public V put(final CompositeMap<K, V> map, final Map<K, V>[] composited, final K key,
106 final V value) {
107 pass = true;
108 return (V) "foo";
109 }
110
111 @Override
112 public void putAll(final CompositeMap<K, V> map, final Map<K, V>[] composited, final Map<? extends K, ? extends V> t) {
113 throw new UnsupportedOperationException();
114 }
115
116 @Override
117 public void resolveCollision(final CompositeMap<K, V> composite,
118 final Map<K, V> existing,
119 final Map<K, V> added,
120 final Collection<K> intersect) {
121 throw new UnsupportedOperationException();
122 }
123 });
124
125 map.put((K) "willy", (V) "wonka");
126 assertTrue(pass);
127 }
128
129 @Test
130 public void testPutAll() {
131 final CompositeMap<K, V> map = new CompositeMap<>(buildOne(), buildTwo(),
132 new CompositeMap.MapMutator<K, V>() {
133 private static final long serialVersionUID = 1L;
134 @Override
135 public V put(final CompositeMap<K, V> map, final Map<K, V>[] composited, final K key,
136 final V value) {
137 throw new UnsupportedOperationException();
138 }
139
140 @Override
141 public void putAll(final CompositeMap<K, V> map, final Map<K, V>[] composited, final Map<? extends K, ? extends V> t) {
142 pass = true;
143 }
144
145 @Override
146 public void resolveCollision(final CompositeMap<K, V> composite,
147 final Map<K, V> existing,
148 final Map<K, V> added,
149 final Collection<K> intersect) {
150 throw new UnsupportedOperationException();
151 }
152 });
153
154 map.putAll(null);
155 assertTrue(pass);
156 }
157
158 @Test
159 @SuppressWarnings("unchecked")
160 public void testRemoveComposited() {
161 final CompositeMap<K, V> map = new CompositeMap<>(buildOne(), buildTwo());
162 final HashMap<K, V> three = new HashMap<>();
163 three.put((K) "5", (V) "five");
164 map.addComposited(null);
165 map.addComposited(three);
166 assertTrue(map.containsKey("5"));
167
168 map.removeComposited(three);
169 assertFalse(map.containsKey("5"));
170
171 map.removeComposited(buildOne());
172 assertFalse(map.containsKey("2"));
173
174 }
175
176 @Test
177 @SuppressWarnings("unchecked")
178 public void testRemoveFromComposited() {
179 final CompositeMap<K, V> map = new CompositeMap<>(buildOne(), buildTwo());
180 final HashMap<K, V> three = new HashMap<>();
181 three.put((K) "5", (V) "five");
182 map.addComposited(null);
183 map.addComposited(three);
184 assertTrue(map.containsKey("5"));
185
186
187 map.remove("5");
188 assertFalse(three.containsKey("5"));
189 }
190
191 @Test
192 @SuppressWarnings("unchecked")
193 public void testRemoveFromUnderlying() {
194 final CompositeMap<K, V> map = new CompositeMap<>(buildOne(), buildTwo());
195 final HashMap<K, V> three = new HashMap<>();
196 three.put((K) "5", (V) "five");
197 map.addComposited(null);
198 map.addComposited(three);
199 assertTrue(map.containsKey("5"));
200
201
202 three.remove("5");
203 assertFalse(map.containsKey("5"));
204 }
205
206 @Test
207 public void testResolveCollision() {
208 final CompositeMap<K, V> map = new CompositeMap<>(buildOne(), buildTwo(),
209 new CompositeMap.MapMutator<K, V>() {
210 private static final long serialVersionUID = 1L;
211
212 @Override
213 public V put(final CompositeMap<K, V> map, final Map<K, V>[] composited, final K key,
214 final V value) {
215 throw new UnsupportedOperationException();
216 }
217
218 @Override
219 public void putAll(final CompositeMap<K, V> map, final Map<K, V>[] composited, final Map<? extends K, ? extends V> t) {
220 throw new UnsupportedOperationException();
221 }
222
223 @Override
224 public void resolveCollision(final CompositeMap<K, V> composite,
225 final Map<K, V> existing,
226 final Map<K, V> added,
227 final Collection<K> intersect) {
228 pass = true;
229 }
230 });
231
232 map.addComposited(buildOne());
233 assertTrue(pass);
234 }
235
236
237
238
239
240
241
242
243 }