14
14
* See the License for the specific language governing permissions and
15
15
* limitations under the License.
16
16
*/
17
- package org .apache .logging .log4j .spi ;
17
+ package org .apache .logging .log4j .internal . map ;
18
18
19
- import static org .junit .Assert .assertEquals ;
20
- import static org .junit .Assert .assertFalse ;
21
- import static org .junit .Assert .assertTrue ;
22
- import static org .junit .Assert .fail ;
19
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
20
+ import static org .junit .jupiter .api .Assertions .assertFalse ;
23
21
import static org .junit .jupiter .api .Assertions .assertNotEquals ;
22
+ import static org .junit .jupiter .api .Assertions .assertNull ;
23
+ import static org .junit .jupiter .api .Assertions .assertTrue ;
24
+ import static org .junit .jupiter .api .Assertions .fail ;
24
25
25
26
import java .util .ArrayList ;
26
27
import java .util .Collections ;
30
31
import java .util .LinkedHashSet ;
31
32
import java .util .Map ;
32
33
import java .util .Set ;
34
+ import org .apache .logging .log4j .util .TriConsumer ;
33
35
import org .junit .jupiter .api .Test ;
34
36
35
37
public class UnmodifiableArrayBackedMapTest {
@@ -49,20 +51,52 @@ private HashMap<String, String> getTestParameters(int numParams) {
49
51
}
50
52
51
53
@ Test
52
- public void testReads () {
53
- assertEquals (UnmodifiableArrayBackedMap .EMPTY_MAP .get ("test" ), null );
54
+ public void testCopyAndPut () {
55
+ UnmodifiableArrayBackedMap testMap = UnmodifiableArrayBackedMap .EMPTY_MAP ;
56
+ testMap = testMap .copyAndPut ("1" , "value1" );
57
+ assertTrue (testMap .containsKey ("1" ));
58
+ assertEquals (testMap .get ("1" ), "value1" );
59
+
60
+ testMap = testMap .copyAndPut ("1" , "another value" );
61
+ assertTrue (testMap .containsKey ("1" ));
62
+ assertEquals (testMap .get ("1" ), "another value" );
63
+
64
+ HashMap <String , String > newValues = getTestParameters ();
65
+ testMap = testMap .copyAndPutAll (newValues );
66
+ assertEquals (testMap .get ("1" ), "value1" );
67
+ assertEquals (testMap .get ("4" ), "value4" );
68
+ }
69
+
70
+ @ Test
71
+ public void testCopyAndRemove () {
72
+ // general removing from well-populated set
54
73
HashMap <String , String > params = getTestParameters ();
55
74
UnmodifiableArrayBackedMap testMap = UnmodifiableArrayBackedMap .EMPTY_MAP .copyAndPutAll (params );
56
- for (Map .Entry <String , String > entry : params .entrySet ()) {
57
- String key = entry .getKey ();
58
- String value = entry .getValue ();
59
- assertTrue (testMap .containsKey (key ));
60
- assertTrue (testMap .containsValue (value ));
61
- assertEquals (testMap .get (key ), params .get (key ));
62
- }
63
- assertFalse (testMap .containsKey ("not_present" ));
64
- assertFalse (testMap .containsValue ("not_present" ));
65
- assertEquals (null , testMap .get ("not_present" ));
75
+ testMap = testMap .copyAndRemove ("2" );
76
+ testMap = testMap .copyAndRemove ("not_present" );
77
+ assertEquals (4 , testMap .size ());
78
+ assertFalse (testMap .containsKey ("2" ));
79
+ assertTrue (testMap .containsKey ("1" ));
80
+ assertFalse (testMap .containsValue ("value2" ));
81
+
82
+ // test removing from empty set
83
+ testMap = UnmodifiableArrayBackedMap .EMPTY_MAP .copyAndPut ("test" , "test" );
84
+ testMap = testMap .copyAndRemove ("test" );
85
+ assertTrue (testMap .isEmpty ());
86
+
87
+ // test removing first of two elements
88
+ testMap = UnmodifiableArrayBackedMap .EMPTY_MAP .copyAndPut ("test1" , "test1" );
89
+ testMap = testMap .copyAndPut ("test2" , "test2" );
90
+ testMap = testMap .copyAndRemove ("test1" );
91
+ assertFalse (testMap .containsKey ("test1" ));
92
+ assertTrue (testMap .containsKey ("test2" ));
93
+
94
+ // test removing second of two elements
95
+ testMap = UnmodifiableArrayBackedMap .EMPTY_MAP .copyAndPut ("test1" , "test1" );
96
+ testMap = testMap .copyAndPut ("test2" , "test2" );
97
+ testMap = testMap .copyAndRemove ("test2" );
98
+ assertTrue (testMap .containsKey ("test1" ));
99
+ assertFalse (testMap .containsKey ("test2" ));
66
100
}
67
101
68
102
@ Test
@@ -111,29 +145,8 @@ public void testCopyAndRemoveAll() {
111
145
}
112
146
113
147
@ Test
114
- public void testCopyAndPut () {
115
- UnmodifiableArrayBackedMap testMap = UnmodifiableArrayBackedMap .EMPTY_MAP ;
116
- testMap = testMap .copyAndPut ("1" , "value1" );
117
- assertTrue (testMap .containsKey ("1" ));
118
- assertEquals (testMap .get ("1" ), "value1" );
119
-
120
- testMap = testMap .copyAndPut ("1" , "another value" );
121
- assertTrue (testMap .containsKey ("1" ));
122
- assertEquals (testMap .get ("1" ), "another value" );
123
-
124
- HashMap <String , String > newValues = getTestParameters ();
125
- testMap = testMap .copyAndPutAll (newValues );
126
- assertEquals (testMap .get ("1" ), "value1" );
127
- assertEquals (testMap .get ("4" ), "value4" );
128
- }
129
-
130
- @ Test
131
- public void testInstanceCopy () {
132
- HashMap <String , String > params = getTestParameters ();
133
- UnmodifiableArrayBackedMap testMap = UnmodifiableArrayBackedMap .EMPTY_MAP .copyAndPutAll (params );
134
-
135
- UnmodifiableArrayBackedMap testMap2 = new UnmodifiableArrayBackedMap (testMap );
136
- assertEquals (testMap , testMap2 );
148
+ public void testEmptyMap () {
149
+ assertNull (UnmodifiableArrayBackedMap .EMPTY_MAP .get ("test" ));
137
150
}
138
151
139
152
@ Test
@@ -176,76 +189,6 @@ public void testEntrySetMutatorsBlocked() {
176
189
}
177
190
}
178
191
179
- @ Test
180
- public void testNullValue () {
181
- UnmodifiableArrayBackedMap testMap = UnmodifiableArrayBackedMap .EMPTY_MAP ;
182
- testMap = testMap .copyAndPut ("key" , null );
183
- assertTrue (testMap .containsKey ("key" ));
184
- assertTrue (testMap .containsValue (null ));
185
- assertTrue (testMap .size () == 1 );
186
- assertEquals (testMap .get ("key" ), null );
187
- }
188
-
189
- @ Test
190
- public void testMutatorsBlocked () {
191
- UnmodifiableArrayBackedMap testMap = UnmodifiableArrayBackedMap .EMPTY_MAP .copyAndPutAll (getTestParameters ());
192
- try {
193
- testMap .put ("a" , "a" );
194
- fail ("put() wasn't blocked" );
195
- } catch (UnsupportedOperationException e ) {
196
- }
197
-
198
- try {
199
- testMap .putAll (new HashMap <>());
200
- fail ("putAll() wasn't blocked" );
201
- } catch (UnsupportedOperationException e ) {
202
- }
203
-
204
- try {
205
- testMap .remove ("1" );
206
- fail ("remove() wasn't blocked" );
207
- } catch (UnsupportedOperationException e ) {
208
- }
209
-
210
- try {
211
- testMap .clear ();
212
- fail ("clear() wasn't blocked" );
213
- } catch (UnsupportedOperationException e ) {
214
- }
215
- }
216
-
217
- @ Test
218
- public void testCopyAndRemove () {
219
- // general removing from well-populated set
220
- HashMap <String , String > params = getTestParameters ();
221
- UnmodifiableArrayBackedMap testMap = UnmodifiableArrayBackedMap .EMPTY_MAP .copyAndPutAll (params );
222
- testMap = testMap .copyAndRemove ("2" );
223
- testMap = testMap .copyAndRemove ("not_present" );
224
- assertEquals (4 , testMap .size ());
225
- assertFalse (testMap .containsKey ("2" ));
226
- assertTrue (testMap .containsKey ("1" ));
227
- assertFalse (testMap .containsValue ("value2" ));
228
-
229
- // test removing from empty set
230
- testMap = UnmodifiableArrayBackedMap .EMPTY_MAP .copyAndPut ("test" , "test" );
231
- testMap = testMap .copyAndRemove ("test" );
232
- assertTrue (testMap .isEmpty ());
233
-
234
- // test removing first of two elements
235
- testMap = UnmodifiableArrayBackedMap .EMPTY_MAP .copyAndPut ("test1" , "test1" );
236
- testMap = testMap .copyAndPut ("test2" , "test2" );
237
- testMap = testMap .copyAndRemove ("test1" );
238
- assertFalse (testMap .containsKey ("test1" ));
239
- assertTrue (testMap .containsKey ("test2" ));
240
-
241
- // test removing second of two elements
242
- testMap = UnmodifiableArrayBackedMap .EMPTY_MAP .copyAndPut ("test1" , "test1" );
243
- testMap = testMap .copyAndPut ("test2" , "test2" );
244
- testMap = testMap .copyAndRemove ("test2" );
245
- assertTrue (testMap .containsKey ("test1" ));
246
- assertFalse (testMap .containsKey ("test2" ));
247
- }
248
-
249
192
/**
250
193
* Tests various situations with .equals(). Test tries comparisons in both
251
194
* directions, to make sure that HashMap.equals(UnmodifiableArrayBackedMap) work
@@ -261,11 +204,14 @@ public void testEqualsHashCodeWithIdenticalContent() {
261
204
}
262
205
263
206
@ Test
264
- public void testEqualsWhenOneValueDiffers () {
207
+ public void testEqualsHashCodeWithOneEmptyMap () {
265
208
HashMap <String , String > params = getTestParameters ();
266
209
UnmodifiableArrayBackedMap testMap = UnmodifiableArrayBackedMap .EMPTY_MAP .copyAndPutAll (params );
267
- assertNotEquals (params , testMap .copyAndPut ("1" , "different value" ));
268
- assertNotEquals (testMap .copyAndPut ("1" , "different value" ), params );
210
+ // verify empty maps are not equal to non-empty maps
211
+ assertNotEquals (params , UnmodifiableArrayBackedMap .EMPTY_MAP );
212
+ assertNotEquals (new HashMap <>(), testMap );
213
+ assertNotEquals (UnmodifiableArrayBackedMap .EMPTY_MAP , params );
214
+ assertNotEquals (testMap , new HashMap <>());
269
215
}
270
216
271
217
@ Test
@@ -283,14 +229,56 @@ public void testEqualsHashCodeWithOneKeyRemoved() {
283
229
}
284
230
285
231
@ Test
286
- public void testEqualsHashCodeWithOneEmptyMap () {
232
+ public void testEqualsWhenOneValueDiffers () {
287
233
HashMap <String , String > params = getTestParameters ();
288
234
UnmodifiableArrayBackedMap testMap = UnmodifiableArrayBackedMap .EMPTY_MAP .copyAndPutAll (params );
289
- // verify empty maps are not equal to non-empty maps
290
- assertNotEquals (params , UnmodifiableArrayBackedMap .EMPTY_MAP );
291
- assertNotEquals (new HashMap <>(), testMap );
292
- assertNotEquals (UnmodifiableArrayBackedMap .EMPTY_MAP , params );
293
- assertNotEquals (testMap , new HashMap <>());
235
+ assertNotEquals (params , testMap .copyAndPut ("1" , "different value" ));
236
+ assertNotEquals (testMap .copyAndPut ("1" , "different value" ), params );
237
+ }
238
+
239
+ @ Test
240
+ public void testForEachBiConsumer_JavaUtil () {
241
+ UnmodifiableArrayBackedMap map = UnmodifiableArrayBackedMap .EMPTY_MAP .copyAndPutAll (getTestParameters ());
242
+ Set <String > keys = new HashSet <>();
243
+ java .util .function .BiConsumer <String , String > java_util_action =
244
+ new java .util .function .BiConsumer <String , String >() {
245
+ @ Override
246
+ public void accept (String key , String value ) {
247
+ keys .add (key );
248
+ }
249
+ };
250
+ map .forEach (java_util_action );
251
+ assertEquals (map .keySet (), keys );
252
+ }
253
+
254
+ @ Test
255
+ public void testForEachBiConsumer_Log4jUtil () {
256
+ UnmodifiableArrayBackedMap map = UnmodifiableArrayBackedMap .EMPTY_MAP .copyAndPutAll (getTestParameters ());
257
+ Set <String > keys = new HashSet <>();
258
+ org .apache .logging .log4j .util .BiConsumer <String , String > log4j_util_action =
259
+ new org .apache .logging .log4j .util .BiConsumer <String , String >() {
260
+ @ Override
261
+ public void accept (String key , String value ) {
262
+ keys .add (key );
263
+ }
264
+ };
265
+ map .forEach (log4j_util_action );
266
+ assertEquals (map .keySet (), keys );
267
+ }
268
+
269
+ @ Test
270
+ public void testForEachTriConsumer () {
271
+ UnmodifiableArrayBackedMap map = UnmodifiableArrayBackedMap .EMPTY_MAP .copyAndPutAll (getTestParameters ());
272
+ HashMap <String , String > iterationResultMap = new HashMap <>();
273
+ TriConsumer <String , String , Map <String , String >> triConsumer =
274
+ new TriConsumer <String , String , Map <String , String >>() {
275
+ @ Override
276
+ public void accept (String k , String v , Map <String , String > s ) {
277
+ s .put (k , v );
278
+ }
279
+ };
280
+ map .forEach (triConsumer , iterationResultMap );
281
+ assertEquals (map , iterationResultMap );
294
282
}
295
283
296
284
@ Test
@@ -306,6 +294,70 @@ public void testImmutability() {
306
294
assertEquals (originalMap , params );
307
295
}
308
296
297
+ @ Test
298
+ public void testInstanceCopy () {
299
+ HashMap <String , String > params = getTestParameters ();
300
+ UnmodifiableArrayBackedMap testMap = UnmodifiableArrayBackedMap .EMPTY_MAP .copyAndPutAll (params );
301
+
302
+ UnmodifiableArrayBackedMap testMap2 = new UnmodifiableArrayBackedMap (testMap );
303
+ assertEquals (testMap , testMap2 );
304
+ }
305
+
306
+ @ Test
307
+ public void testMutatorsBlocked () {
308
+ UnmodifiableArrayBackedMap testMap = UnmodifiableArrayBackedMap .EMPTY_MAP .copyAndPutAll (getTestParameters ());
309
+ try {
310
+ testMap .put ("a" , "a" );
311
+ fail ("put() wasn't blocked" );
312
+ } catch (UnsupportedOperationException e ) {
313
+ }
314
+
315
+ try {
316
+ testMap .putAll (new HashMap <>());
317
+ fail ("putAll() wasn't blocked" );
318
+ } catch (UnsupportedOperationException e ) {
319
+ }
320
+
321
+ try {
322
+ testMap .remove ("1" );
323
+ fail ("remove() wasn't blocked" );
324
+ } catch (UnsupportedOperationException e ) {
325
+ }
326
+
327
+ try {
328
+ testMap .clear ();
329
+ fail ("clear() wasn't blocked" );
330
+ } catch (UnsupportedOperationException e ) {
331
+ }
332
+ }
333
+
334
+ @ Test
335
+ public void testNullValue () {
336
+ UnmodifiableArrayBackedMap testMap = UnmodifiableArrayBackedMap .EMPTY_MAP ;
337
+ testMap = testMap .copyAndPut ("key" , null );
338
+ assertTrue (testMap .containsKey ("key" ));
339
+ assertTrue (testMap .containsValue (null ));
340
+ assertTrue (testMap .size () == 1 );
341
+ assertEquals (testMap .get ("key" ), null );
342
+ }
343
+
344
+ @ Test
345
+ public void testReads () {
346
+ assertEquals (UnmodifiableArrayBackedMap .EMPTY_MAP .get ("test" ), null );
347
+ HashMap <String , String > params = getTestParameters ();
348
+ UnmodifiableArrayBackedMap testMap = UnmodifiableArrayBackedMap .EMPTY_MAP .copyAndPutAll (params );
349
+ for (Map .Entry <String , String > entry : params .entrySet ()) {
350
+ String key = entry .getKey ();
351
+ String value = entry .getValue ();
352
+ assertTrue (testMap .containsKey (key ));
353
+ assertTrue (testMap .containsValue (value ));
354
+ assertEquals (testMap .get (key ), params .get (key ));
355
+ }
356
+ assertFalse (testMap .containsKey ("not_present" ));
357
+ assertFalse (testMap .containsValue ("not_present" ));
358
+ assertEquals (null , testMap .get ("not_present" ));
359
+ }
360
+
309
361
@ Test
310
362
public void testState () {
311
363
UnmodifiableArrayBackedMap originalMap ;
@@ -325,4 +377,11 @@ public void testState() {
325
377
newMap = UnmodifiableArrayBackedMap .getInstance (originalMap .getBackingArray ());
326
378
assertEquals (originalMap , newMap );
327
379
}
380
+
381
+ @ Test
382
+ public void testToMap () {
383
+ UnmodifiableArrayBackedMap map = UnmodifiableArrayBackedMap .EMPTY_MAP .copyAndPut ("test" , "test" );
384
+ // verify same instance, not just equals()
385
+ assertTrue (map == map .toMap ());
386
+ }
328
387
}
0 commit comments