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