This repository has been archived by the owner on Dec 10, 2022. It is now read-only.
/
TestMap.java
1166 lines (999 loc) · 37.9 KB
/
TestMap.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2016 Carlos Ballesteros Velasco
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package javatest.utils;
import java.util.Arrays;
import java.util.Collection;
import java.util.ConcurrentModificationException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
* Tests base {@link java.util.Map} methods and contracts.
* <p>
* The forces at work here are similar to those in {@link TestCollection}.
* If your class implements the full Map interface, including optional
* operations, simply extend this class, and implement the {@link
* #makeEmptyMap()} method.
* <p>
* On the other hand, if your map implemenation is weird, you may have to
* override one or more of the other protected methods. They're described
* below.<P>
*
* <B>Entry Population Methods</B><P>
*
* Override these methods if your map requires special entries:
*
* <UL>
* <LI>{@link #getSampleKeys()}
* <LI>{@link #getSampleValues()}
* <LI>{@link #getNewSampleValues()}
* <LI>{@link #getOtherKeys()}
* <LI>{@link #getOtherValues()}
* </UL>
*
* <B>Supported Operation Methods</B><P>
*
* Override these methods if your map doesn't support certain operations:
*
* <UL>
* <LI> {@link #useDuplicateValues()}
* <LI> {@link #useNullKey()}
* <LI> {@link #useNullValue()}
* <LI> {@link #isAddRemoveModifiable()}
* <LI> {@link #isChangeable()}
* </UL>
*
* <B>Fixture Methods</B><P>
*
* For tests on modification operations (puts and removes), fixtures are used
* to verify that that operation results in correct state for the map and its
* collection views. Basically, the modification is performed against your
* map implementation, and an identical modification is performed against
* a <I>confirmed</I> map implementation. A confirmed map implementation is
* something like <Code>java.util.HashMap</Code>, which is known to conform
* exactly to the {@link Map} contract. After the modification takes place
* on both your map implementation and the confirmed map implementation, the
* two maps are compared to see if their state is identical. The comparison
* also compares the collection views to make sure they're still the same.<P>
*
* The upshot of all that is that <I>any</I> test that modifies the map in
* <I>any</I> way will verify that <I>all</I> of the map's state is still
* correct, including the state of its collection views. So for instance
* if a key is removed by the map's key set's iterator, then the entry set
* is checked to make sure the key/value pair no longer appears.<P>
*
* The {@link #map} field holds an instance of your collection implementation.
* The {@link #entrySet}, {@link #keySet} and {@link #collectionValues} fields hold
* that map's collection views. And the {@link #confirmed} field holds
* an instance of the confirmed collection implementation. The
* {@link #resetEmpty()} and {@link #resetFull()} methods set these fields to
* empty or full maps, so that tests can proceed from a known state.<P>
*
* After a modification operation to both {@link #map} and {@link #confirmed},
* the {@link #verify()} method is invoked to compare the results. The {@link
* verify()} method calls separate methods to verify the map and its three
* collection views ({@link verifyMap(), {@link verifyEntrySet()}, {@link
* verifyKeySet()}, and {@link verifyValues()}). You may want to override one
* of the verification methodsto perform additional verifications. For
* instance, {@link TestDoubleOrderedMap} would want override its {@link
* #verifyValues()} method to verify that the values are unique and in
* ascending order.<P>
*
* <B>Other Notes</B><P>
*
* If your {@link Map} fails one of these tests by design, you may still use
* this base set of cases. Simply override the test case (method) your {@link
* Map} fails and/or the methods that define the assumptions used by the test
* cases. For example, if your map does not allow duplicate values, override
* {@link #useDuplicateValues()} and have it return <code>false</code>
*
* @author Michael Smith
* @author Rodney Waldhoff
* @author Paul Jack
* @version $Id: TestMap.java,v 1.20.2.1 2004/05/22 12:14:05 scolebourne Exp $
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public class TestMap extends TestObject{
// These instance variables are initialized with the reset method.
// Tests for map methods that alter the map (put, putAll, remove)
// first call reset() to create the map and its views; then perform
// the modification on the map; perform the same modification on the
// confirmed; and then call verify() to ensure that the map is equal
// to the confirmed, that the already-constructed collection views
// are still equal to the confirmed's collection views.
/** Map created by reset(). */
protected Map map;
/** Entry set of map created by reset(). */
protected Set entrySet;
/** Key set of map created by reset(). */
protected Set keySet;
/** Values collection of map created by reset(). */
protected Collection collectionValues;
/** HashMap created by reset(). */
protected Map confirmed;
public void test() {
testFailFastEntrySet();
testFailFastKeySet();
testFailFastValues();
testMakeMap();
testMapClear();
testMapContainsKey();
testMapContainsValue();
testMapEquals();
testMapGet();
testMapHashCode();
testMapIsEmpty();
testMapPut();
testMapPutAll();
testMapRemove();
testMapSize();
testMapToString();
testSampleMappings();
}
/**
* Override if your map does not allow a <code>null</code> key. The
* default implementation returns <code>true</code>
**/
protected boolean useNullKey() {
return true;
}
/**
* Override if your map does not allow <code>null</code> values. The
* default implementation returns <code>true</code>.
**/
protected boolean useNullValue() {
return true;
}
/**
* Override if your map does not allow duplicate values. The default
* implementation returns <code>true</code>.
**/
protected boolean useDuplicateValues() {
return true;
}
/**
* Override if your map allows its mappings to be changed to new values.
* The default implementation returns <code>true</code>.
**/
protected boolean isChangeable() {
return true;
}
/**
* Override if your map does not allow add/remove modifications. The
* default implementation returns <code>true</code>.
**/
protected boolean isAddRemoveModifiable() {
return true;
}
/**
* Override if your map allows concurrent modifications. The default
* implementation returns <code>true</code>.
**/
protected boolean isFailFastExpected() {
return true;
}
/**
* Returns the set of keys in the mappings used to test the map. This
* method must return an array with the same length as {@link
* #getSampleValues()} and all array elements must be different. The
* default implementation constructs a set of String keys, and includes a
* single null key if {@link #useNullKey()} returns <code>true</code>.
**/
protected Object[] getSampleKeys() {
Object[] result = new Object[] {
"blah", "foo", "bar", "baz", "tmp", "gosh", "golly", "gee",
"hello", "goodbye", "we'll", "see", "you", "all", "again",
"key",
"key2",
(useNullKey()) ? null : "nonnullkey"
};
return result;
}
protected Object[] getOtherKeys() {
return TestCollection.getOtherNonNullStringElements();
}
protected Object[] getOtherValues() {
return TestCollection.getOtherNonNullStringElements();
}
/**
* Returns the set of values in the mappings used to test the map. This
* method must return an array with the same length as {@link
* #getSampleKeys()}. The default implementation contructs a set of
* String values and includes a single null value if {@link
* #useNullValue()} returns <code>true</code>, and includes two values
* that are the same if {@link #useDuplicateValues()} returns
* <code>true</code>.
**/
protected Object[] getSampleValues() {
Object[] result = new Object[] {
"blahv", "foov", "barv", "bazv", "tmpv", "goshv", "gollyv", "geev",
"hellov", "goodbyev", "we'llv", "seev", "youv", "allv", "againv",
(useNullValue()) ? null : "nonnullvalue",
"value",
(useDuplicateValues()) ? "value" : "value2",
};
return result;
}
/**
* Returns a the set of values that can be used to replace the values
* returned from {@link #getSampleValues()}. This method must return an
* array with the same length as {@link #getSampleValues()}. The values
* returned from this method should not be the same as those returned from
* {@link #getSampleValues()}. The default implementation constructs a
* set of String values and includes a single null value if {@link
* #useNullValue()} returns <code>true</code>, and includes two values
* that are the same if {@link #useDuplicateValues()} returns
* <code>true</code>.
**/
protected Object[] getNewSampleValues() {
Object[] result = new Object[] {
(useNullValue()) ? null : "newnonnullvalue",
"newvalue",
(useDuplicateValues()) ? "newvalue" : "newvalue2",
"newblahv", "newfoov", "newbarv", "newbazv", "newtmpv", "newgoshv",
"newgollyv", "newgeev", "newhellov", "newgoodbyev", "newwe'llv",
"newseev", "newyouv", "newallv", "newagainv",
};
return result;
}
/**
* Helper method to add all the mappings described by {@link
* #getSampleKeys()} and {@link #getSampleValues()}.
**/
protected void addSampleMappings(Map m) {
Object[] keys = getSampleKeys();
Object[] values = getSampleValues();
for(int i = 0; i < keys.length; i++) {
try {
m.put(keys[i], values[i]);
} catch (NullPointerException exception) {
assertTrue("NullPointerException only allowed to be thrown " +
"if either the key or value is null.",
keys[i] == null || values[i] == null);
if (keys[i] == null) {
if (useNullKey()) {
throw new Error("NullPointerException on null key, but " +
"useNullKey is not overridden to return false.", exception);
}
} else if (values[i] == null) {
if (useNullValue()) {
throw new Error("NullPointerException on null value, but " +
"useNullValue is not overridden to return false.", exception);
}
} else {
// Unknown reason for NullPointer.
throw exception;
}
}
}
assertEquals("size must reflect number of mappings added.",
keys.length, m.size());
}
/**
* Return a new, empty {@link Map} to be used for testing.
*/
protected Map makeEmptyMap() {
return new HashMap();
}
protected Map makeConfirmedMap() {
return new HashMap();
}
/**
* Return a new, populated map. The mappings in the map should match the
* keys and values returned from {@link #getSampleKeys()} and {@link
* #getSampleValues()}. The default implementation uses makeEmptyMap()
* and calls to add all the mappings to the
* map.
**/
protected Map makeFullMap() {
Map m = makeEmptyMap();
addSampleMappings(m);
return m;
}
@Override
public Object makeObject() {
return makeEmptyMap();
}
private void assertMap(Map map, String[] keys, Object[] values) {
assertEmptyMap(map, keys, values);
// Fill the map with special keys/values.
for (int i = 0; i < keys.length; i++) {
map.put(keys[i], values[i]);
}
// Assert the map with filled in keys/values
for (int i = 0; i < keys.length; i++) {
assertTrue(keys[i], map.containsKey(keys[i]));
assertTrue(keys[i], map.containsValue(values[i]));
assertSame(keys[i], values[i], map.get(keys[i]));
}
assertEquals(map.toString(), keys.length, map.size());
// Remove the keys and assert the results
for (int i = 0; i < keys.length; i++) {
assertSame(keys[i], values[i], map.remove(keys[i]));
}
assertEmptyMap(map, keys, values);
}
private static void assertEmptyMap(Map map, final String[] keys, final Object[] values) {
for (int i = 0; i < keys.length; i++) {
assertFalse(keys[i], map.containsKey(keys[i]));
assertFalse(keys[i], map.containsValue(values[i]));
assertNull(keys[i], map.get(keys[i]));
}
}
/**
* Test to ensure the test setup is working properly. This method checks
* to ensure that the getSampleKeys and getSampleValues methods are
* returning results that look appropriate. That is, they both return a
* non-null array of equal length. The keys array must not have any
* duplicate values, and may only contain a (single) null key if
* useNullKey() returns true. The values array must only have a null
* value if useNullValue() is true and may only have duplicate values if
* useDuplicateValues() returns true.
**/
public void testSampleMappings() {
Object[] keys = getSampleKeys();
Object[] values = getSampleValues();
Object[] newValues = getNewSampleValues();
assertTrue("failure in test: Must have keys returned from " +
"getSampleKeys.", keys != null);
assertTrue("failure in test: Must have values returned from " +
"getSampleValues.", values != null);
// verify keys and values have equivalent lengths (in case getSampleX are
// overridden)
assertEquals("failure in test: not the same number of sample " +
"keys and values.", keys.length, values.length);
assertEquals("failure in test: not the same number of values and new values.",
values.length, newValues.length);
// verify there aren't duplicate keys, and check values
for(int i = 0; i < keys.length - 1; i++) {
for(int j = i + 1; j < keys.length; j++) {
assertTrue("failure in test: duplicate null keys.",
(keys[i] != null || keys[j] != null));
assertTrue("failure in test: duplicate non-null key.",
(keys[i] == null || keys[j] == null ||
(!keys[i].equals(keys[j]) &&
!keys[j].equals(keys[i]))));
}
assertTrue("failure in test: found null key, but useNullKey " +
"is false.", keys[i] != null || useNullKey());
assertTrue("failure in test: found null value, but useNullValue " +
"is false.", values[i] != null || useNullValue());
assertTrue("failure in test: found null new value, but useNullValue " +
"is false.", newValues[i] != null || useNullValue());
assertTrue("failure in test: values should not be the same as new value",
values[i] != newValues[i] &&
(values[i] == null || !values[i].equals(newValues[i])));
}
}
// tests begin here. Each test adds a little bit of tested functionality.
// Many methods assume previous methods passed. That is, they do not
// exhaustively recheck things that have already been checked in a previous
// test methods.
/**
* Test to ensure that makeEmptyMap and makeFull returns a new non-null
* map with each invocation.
**/
public void testMakeMap() {
Map em = makeEmptyMap();
assertTrue("failure in test: makeEmptyMap must return a non-null map.",
em != null);
Map em2 = makeEmptyMap();
assertTrue("failure in test: makeEmptyMap must return a non-null map.",
em != null);
assertTrue("failure in test: makeEmptyMap must return a new map " +
"with each invocation.", em != em2);
Map fm = makeFullMap();
assertTrue("failure in test: makeFullMap must return a non-null map.",
fm != null);
Map fm2 = makeFullMap();
assertTrue("failure in test: makeFullMap must return a non-null map.",
fm != null);
assertTrue("failure in test: makeFullMap must return a new map " +
"with each invocation.", fm != fm2);
}
/**
* Tests Map.isEmpty()
**/
public void testMapIsEmpty() {
resetEmpty();
assertEquals("Map.isEmpty() should return true with an empty map",
true, map.isEmpty());
verify();
resetFull();
assertEquals("Map.isEmpty() should return false with a non-empty map",
false, map.isEmpty());
verify();
}
/**
* Tests Map.size()
**/
public void testMapSize() {
resetEmpty();
assertEquals("Map.size() should be 0 with an empty map",
0, map.size());
verify();
resetFull();
assertEquals("Map.size() should equal the number of entries " +
"in the map", getSampleKeys().length, map.size());
verify();
}
/**
* Tests {@link Map#clear()}. If the map {@link #isAddRemoveModifiable()
* can add and remove elements}, then {@link Map#size()} and {@link
* Map#isEmpty()} are used to ensure that map has no elements after a call
* to clear. If the map does not support adding and removing elements,
* this method checks to ensure clear throws an
* UnsupportedOperationException.
**/
public void testMapClear() {
if (!isAddRemoveModifiable()) return;
resetEmpty();
map.clear();
confirmed.clear();
verify();
resetFull();
map.clear();
confirmed.clear();
verify();
}
/**
* Tests Map.containsKey(Object) by verifying it returns false for all
* sample keys on a map created using an empty map and returns true for
* all sample keys returned on a full map.
**/
public void testMapContainsKey() {
Object[] keys = getSampleKeys();
resetEmpty();
for(int i = 0; i < keys.length; i++) {
assertTrue("Map must not contain key when map is empty",
!map.containsKey(keys[i]));
}
verify();
resetFull();
for(int i = 0; i < keys.length; i++) {
assertTrue("Map must contain key for a mapping in the map. " +
"Missing: " + keys[i], map.containsKey(keys[i]));
}
verify();
}
/**
* Tests Map.containsValue(Object) by verifying it returns false for all
* sample values on an empty map and returns true for all sample values on
* a full map.
**/
public void testMapContainsValue() {
Object[] values = getSampleValues();
resetEmpty();
for(int i = 0; i < values.length; i++) {
assertTrue("Empty map must not contain value",
!map.containsValue(values[i]));
}
verify();
resetFull();
for(int i = 0; i < values.length; i++) {
assertTrue("Map must contain value for a mapping in the map.",
map.containsValue(values[i]));
}
verify();
}
/**
* Tests Map.equals(Object)
**/
public void testMapEquals() {
resetEmpty();
assertTrue("Empty maps unequal.", map.equals(confirmed));
verify();
resetFull();
assertTrue("Full maps unequal.", map.equals(confirmed));
verify();
resetFull();
// modify the HashMap created from the full map and make sure this
// change results in map.equals() to return false.
Iterator iter = confirmed.keySet().iterator();
iter.next();
iter.remove();
assertTrue("Different maps equal.", !map.equals(confirmed));
resetFull();
assertTrue("equals(null) returned true.", !map.equals(null));
assertTrue("equals(new Object()) returned true.",
!map.equals(new Object()));
verify();
}
/**
* Tests Map.get(Object)
**/
public void testMapGet() {
resetEmpty();
Object[] keys = getSampleKeys();
Object[] values = getSampleValues();
for (int i = 0; i < keys.length; i++) {
assertTrue("Empty map.get() should return null.",
map.get(keys[i]) == null);
}
verify();
resetFull();
for (int i = 0; i < keys.length; i++) {
assertEquals("Full map.get() should return value from mapping.",
values[i], map.get(keys[i]));
}
}
/**
* Tests Map.hashCode()
**/
public void testMapHashCode() {
resetEmpty();
assertTrue("Empty maps have different hashCodes.",
map.hashCode() == confirmed.hashCode());
resetFull();
assertTrue("Equal maps have different hashCodes.",
map.hashCode() == confirmed.hashCode());
}
/**
* Tests Map.toString(). Since the format of the string returned by the
* toString() method is not defined in the Map interface, there is no
* common way to test the results of the toString() method. Thereforce,
* it is encouraged that Map implementations override this test with one
* that checks the format matches any format defined in its API. This
* default implementation just verifies that the toString() method does
* not return null.
**/
public void testMapToString() {
resetEmpty();
assertTrue("Empty map toString() should not return null",
map.toString() != null);
verify();
resetFull();
assertTrue("Empty map toString() should not return null",
map.toString() != null);
verify();
}
/**
* Tests Map.put(Object, Object)
**/
public void testMapPut() {
if (!isAddRemoveModifiable()) return;
resetEmpty();
Object[] keys = getSampleKeys();
Object[] values = getSampleValues();
Object[] newValues = getNewSampleValues();
for(int i = 0; i < keys.length; i++) {
Object o = map.put(keys[i], values[i]);
confirmed.put(keys[i], values[i]);
verify();
assertTrue("First map.put should return null", o == null);
assertTrue("Map should contain key after put",
map.containsKey(keys[i]));
assertTrue("Map should contain value after put",
map.containsValue(values[i]));
}
for(int i = 0; i < keys.length; i++) {
Object o = map.put(keys[i], newValues[i]);
confirmed.put(keys[i], newValues[i]);
verify();
assertEquals("Second map.put should return previous value",
values[i], o);
assertTrue("Map should still contain key after put",
map.containsKey(keys[i]));
assertTrue("Map should contain new value after put",
map.containsValue(newValues[i]));
// if duplicates are allowed, we're not guarunteed that the value
// no longer exists, so don't try checking that.
if(!useDuplicateValues()) {
assertTrue("Map should not contain old value after second put",
!map.containsValue(values[i]));
}
}
}
/**
* Tests Map.putAll(Collection)
**/
public void testMapPutAll() {
if (!isAddRemoveModifiable()) return;
resetEmpty();
Map m2 = makeFullMap();
map.putAll(m2);
confirmed.putAll(m2);
verify();
resetEmpty();
m2 = new HashMap();
Object[] keys = getSampleKeys();
Object[] values = getSampleValues();
for(int i = 0; i < keys.length; i++) {
m2.put(keys[i], values[i]);
}
map.putAll(m2);
confirmed.putAll(m2);
verify();
}
/**
* Tests Map.remove(Object)
**/
public void testMapRemove() {
if (!isAddRemoveModifiable()) return;
resetEmpty();
Object[] keys = getSampleKeys();
Object[] values = getSampleValues();
for(int i = 0; i < keys.length; i++) {
Object o = map.remove(keys[i]);
assertTrue("First map.remove should return null", o == null);
}
verify();
resetFull();
for(int i = 0; i < keys.length; i++) {
Object o = map.remove(keys[i]);
confirmed.remove(keys[i]);
verify();
assertEquals("map.remove with valid key should return value",
values[i], o);
}
Object[] other = getOtherKeys();
resetFull();
int size = map.size();
for (int i = 0; i < other.length; i++) {
Object o = map.remove(other[i]);
assertEquals("map.remove for nonexistent key should return null",
o, null);
assertEquals("map.remove for nonexistent key should not " +
"shrink map", size, map.size());
}
verify();
}
public void testFailFastEntrySet() {
if (!isAddRemoveModifiable()) {
return;
}
if (!isFailFastExpected()) {
return;
}
resetFull();
Iterator<Map.Entry> it = map.entrySet().iterator();
final Map.Entry val = it.next();
map.remove(val.getKey());
try {
it.next();
fail();
} catch (ConcurrentModificationException expected) {}
resetFull();
it = map.entrySet().iterator();
it.next();
map.clear();
try {
it.next();
fail();
} catch (ConcurrentModificationException expected) {}
}
public void testFailFastKeySet() {
if (!isAddRemoveModifiable()) {
return;
}
if (!isFailFastExpected()) {
return;
}
resetFull();
Iterator it = map.keySet().iterator();
final Object val = it.next();
map.remove(val);
try {
it.next();
fail();
} catch (ConcurrentModificationException expected) {}
resetFull();
it = map.keySet().iterator();
it.next();
map.clear();
try {
it.next();
fail();
} catch (ConcurrentModificationException expected) {}
}
public void testFailFastValues() {
if (!isAddRemoveModifiable()) {
return;
}
if (!isFailFastExpected()) {
return;
}
resetFull();
Iterator it = map.values().iterator();
it.next();
map.remove(map.keySet().iterator().next());
try {
it.next();
fail();
} catch (ConcurrentModificationException expected) {}
resetFull();
it = map.values().iterator();
it.next();
map.clear();
try {
it.next();
fail();
} catch (ConcurrentModificationException expected) {}
}
/**
* Utility methods to create an array of Map.Entry objects
* out of the given key and value arrays.<P>
*
* @param keys the array of keys
* @param values the array of values
* @return an array of Map.Entry of those keys to those values
*/
private Map.Entry[] makeEntryArray(Object[] keys, Object[] values) {
Map.Entry[] result = new Map.Entry[keys.length];
for (int i = 0; i < keys.length; i++) {
result[i] = new DefaultMapEntry(keys[i], values[i]);
}
return result;
}
class TestMapEntrySet extends TestSet {
public TestMapEntrySet() {
super("");
}
// Have to implement manually; entrySet doesn't support addAll
@Override
protected Object[] getFullElements() {
Object[] k = getSampleKeys();
Object[] v = getSampleValues();
return makeEntryArray(k, v);
}
// Have to implement manually; entrySet doesn't support addAll
@Override
protected Object[] getOtherElements() {
Object[] k = getOtherKeys();
Object[] v = getOtherValues();
return makeEntryArray(k, v);
}
@Override
protected Set makeEmptySet() {
return makeEmptyMap().entrySet();
}
@Override
protected Set makeFullSet() {
return makeFullMap().entrySet();
}
@Override
protected boolean isAddSupported() {
// Collection views don't support add operations.
return false;
}
@Override
protected boolean isRemoveSupported() {
// Entry set should only support remove if map does
return isAddRemoveModifiable();
}
@Override
protected void resetFull() {
TestMap.this.resetFull();
collection = map.entrySet();
TestMapEntrySet.this.confirmed = TestMap.this.confirmed.entrySet();
}
@Override
protected void resetEmpty() {
TestMap.this.resetEmpty();
collection = map.entrySet();
TestMapEntrySet.this.confirmed = TestMap.this.confirmed.entrySet();
}
@Override
protected void verify() {
super.verify();
TestMap.this.verify();
}
}
class TestMapKeySet extends TestSet {
public TestMapKeySet() {
super("");
}
@Override
protected Object[] getFullElements() {
return getSampleKeys();
}
@Override
protected Object[] getOtherElements() {
return getOtherKeys();
}
@Override
protected Set makeEmptySet() {
return makeEmptyMap().keySet();
}
@Override
protected Set makeFullSet() {
return makeFullMap().keySet();
}
@Override
protected boolean isAddSupported() {
return false;
}
@Override
protected boolean isRemoveSupported() {
return isAddRemoveModifiable();
}
@Override
protected void resetEmpty() {
TestMap.this.resetEmpty();
collection = map.keySet();
TestMapKeySet.this.confirmed = TestMap.this.confirmed.keySet();
}
@Override
protected void resetFull() {
TestMap.this.resetFull();
collection = map.keySet();
TestMapKeySet.this.confirmed = TestMap.this.confirmed.keySet();
}
@Override
protected void verify() {
super.verify();
TestMap.this.verify();
}
}
class TestMapValues extends TestCollection {
public TestMapValues() {
}
@Override
protected Object[] getFullElements() {
return getSampleValues();
}
@Override
protected Object[] getOtherElements() {
return getOtherValues();
}
@Override
protected Collection makeCollection() {
return makeEmptyMap().values();
}