001: /*
002: * Copyright 2006 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.emultest.java.util;
017:
018: import com.google.gwt.core.client.GWT;
019:
020: import org.apache.commons.collections.TestMap;
021:
022: import java.util.Collection;
023: import java.util.HashMap;
024: import java.util.Iterator;
025: import java.util.Map;
026: import java.util.Set;
027: import java.util.Map.Entry;
028:
029: /**
030: * Tests <code>HashMap</code>.
031: */
032: public class HashMapTest extends TestMap {
033: private static final int CAPACITY_16 = 16;
034: private static final int CAPACITY_NEG_ONE_HALF = -1;
035: private static final int CAPACITY_ZERO = 0;
036: private static final Integer INTEGER_1 = new Integer(1);
037: private static final Integer INTEGER_11 = new Integer(11);
038: private static final Integer INTEGER_2 = new Integer(2);
039: private static final Integer INTEGER_22 = new Integer(22);
040: private static final Integer INTEGER_3 = new Integer(3);
041: private static final Integer INTEGER_33 = new Integer(33);
042: private static final Integer INTEGER_ZERO_KEY = new Integer(0);
043: private static final String INTEGER_ZERO_VALUE = "integer zero";
044: private static final String KEY_1 = "key1";
045: private static final String KEY_2 = "key2";
046: private static final String KEY_3 = "key3";
047: private static final String KEY_4 = "key4";
048: private static final String KEY_KEY = "key";
049: private static final String KEY_TEST_CONTAINS_KEY = "testContainsKey";
050: private static final String KEY_TEST_CONTAINS_VALUE = "testContainsValue";
051: private static final String KEY_TEST_ENTRY_SET = "testEntrySet";
052: private static final String KEY_TEST_GET = "testGet";
053: private static final String KEY_TEST_KEY_SET = "testKeySet";
054: private static final String KEY_TEST_PUT = "testPut";
055: private static final String KEY_TEST_REMOVE = "testRemove";
056: private static final float LOAD_FACTOR_NEG_ONE = -1.0F;
057: private static final float LOAD_FACTOR_ONE_HALF = 0.5F;
058: private static final float LOAD_FACTOR_ONE_TENTH = 0.1F;
059: private static final float LOAD_FACTOR_ZERO = 0.0F;
060: private static final Object ODD_ZERO_KEY = new Object() {
061: public int hashCode() {
062: return 0;
063: }
064: };
065: private static final String ODD_ZERO_VALUE = "odd zero";
066: private static final int SIZE_ONE = 1;
067: private static final int SIZE_THREE = 3;
068: private static final int SIZE_TWO = 2;
069: private static final int SIZE_ZERO = 0;
070: private static final String STRING_ZERO_KEY = "0";
071: private static final String STRING_ZERO_VALUE = "string zero";
072: private static final String VALUE_1 = "val1";
073: private static final String VALUE_2 = "val2";
074: private static final String VALUE_3 = "val3";
075: private static final String VALUE_4 = "val4";
076: private static final String VALUE_TEST_CONTAINS_DOES_NOT_EXIST = "does not exist";
077: private static final Integer VALUE_TEST_CONTAINS_KEY = new Integer(
078: 5);
079: private static final String VALUE_TEST_ENTRY_SET_1 = KEY_TEST_ENTRY_SET
080: + " - value1";
081: private static final String VALUE_TEST_ENTRY_SET_2 = KEY_TEST_ENTRY_SET
082: + " - value2";
083: private static final String VALUE_TEST_GET = KEY_TEST_GET
084: + " - Value";
085: private static final String VALUE_TEST_KEY_SET = KEY_TEST_KEY_SET
086: + " - value";
087: private static final String VALUE_TEST_PUT_1 = KEY_TEST_PUT
088: + " - value 1";
089: private static final String VALUE_TEST_PUT_2 = KEY_TEST_PUT
090: + " - value 2";
091: private static final String VALUE_TEST_REMOVE = KEY_TEST_REMOVE
092: + " - value";
093: private static final String VALUE_VAL = "value";
094:
095: /**
096: * Check the state of a newly constructed, empty HashMap.
097: *
098: * @param hashMap
099: */
100: private static void checkEmptyHashMapAssumptions(HashMap hashMap) {
101: assertNotNull(hashMap);
102: assertTrue(hashMap.isEmpty());
103:
104: assertNotNull(hashMap.values());
105: assertTrue(hashMap.values().isEmpty());
106: assertTrue(hashMap.values().size() == 0);
107:
108: assertNotNull(hashMap.keySet());
109: assertTrue(hashMap.keySet().isEmpty());
110: assertTrue(hashMap.keySet().size() == 0);
111:
112: assertNotNull(hashMap.entrySet());
113: assertTrue(hashMap.entrySet().isEmpty());
114: assertTrue(hashMap.entrySet().size() == 0);
115:
116: assertNotNull(hashMap.entrySet().iterator());
117: assertFalse(hashMap.entrySet().iterator().hasNext());
118: }
119:
120: public String getModuleName() {
121: return "com.google.gwt.emultest.EmulSuite";
122: }
123:
124: public void testAddWatch() {
125: HashMap m = new HashMap();
126: m.put("watch", "watch");
127: assertEquals(m.get("watch"), "watch");
128: }
129:
130: public void testAddEqualKeys() {
131: final HashMap expected = new HashMap();
132: assertEquals(expected.size(), 0);
133: iterateThrough(expected);
134: expected.put(new Long(45), new Object());
135: assertEquals(expected.size(), 1);
136: iterateThrough(expected);
137: expected.put(new Integer(45), new Object());
138: assertNotSame(new Integer(45), new Long(45));
139: assertEquals(expected.size(), 2);
140: iterateThrough(expected);
141: }
142:
143: /*
144: * Test method for 'java.util.HashMap.clear()'
145: */
146: public void testClear() {
147: HashMap hashMap = new HashMap();
148: checkEmptyHashMapAssumptions(hashMap);
149:
150: hashMap.put("Hello", "Bye");
151: assertFalse(hashMap.isEmpty());
152: assertTrue(hashMap.size() == SIZE_ONE);
153:
154: hashMap.clear();
155: assertTrue(hashMap.isEmpty());
156: assertTrue(hashMap.size() == 0);
157: }
158:
159: /*
160: * Test method for 'java.util.HashMap.clone()'
161: */
162: public void testClone() {
163: HashMap srcMap = new HashMap();
164: checkEmptyHashMapAssumptions(srcMap);
165:
166: // Check empty clone behavior
167: HashMap dstMap = (HashMap) srcMap.clone();
168: assertNotNull(dstMap);
169: assertEquals(dstMap.size(), srcMap.size());
170: // assertTrue(dstMap.values().toArray().equals(srcMap.values().toArray()));
171: assertTrue(dstMap.keySet().equals(srcMap.keySet()));
172: assertTrue(dstMap.entrySet().equals(srcMap.entrySet()));
173:
174: // Check non-empty clone behavior
175: srcMap.put(KEY_1, VALUE_1);
176: srcMap.put(KEY_2, VALUE_2);
177: srcMap.put(KEY_3, VALUE_3);
178: dstMap = (HashMap) srcMap.clone();
179: assertNotNull(dstMap);
180: assertEquals(dstMap.size(), srcMap.size());
181:
182: assertTrue(dstMap.keySet().equals(srcMap.keySet()));
183:
184: assertTrue(dstMap.entrySet().equals(srcMap.entrySet()));
185: }
186:
187: /*
188: * Test method for 'java.util.HashMap.containsKey(Object)'
189: */
190: public void testContainsKey() {
191: HashMap hashMap = new HashMap();
192: checkEmptyHashMapAssumptions(hashMap);
193:
194: assertFalse(hashMap.containsKey(KEY_TEST_CONTAINS_KEY));
195: hashMap.put(KEY_TEST_CONTAINS_KEY, VALUE_TEST_CONTAINS_KEY);
196: assertTrue(hashMap.containsKey(KEY_TEST_CONTAINS_KEY));
197: assertFalse(hashMap
198: .containsKey(VALUE_TEST_CONTAINS_DOES_NOT_EXIST));
199:
200: assertFalse(hashMap.containsKey(null));
201: hashMap.put(null, VALUE_TEST_CONTAINS_KEY);
202: assertTrue(hashMap.containsKey(null));
203: }
204:
205: /*
206: * Test method for 'java.util.HashMap.containsValue(Object)'
207: */
208: public void testContainsValue() {
209: HashMap hashMap = new HashMap();
210: checkEmptyHashMapAssumptions(hashMap);
211:
212: assertFalse("check contains of empty map", hashMap
213: .containsValue(VALUE_TEST_CONTAINS_KEY));
214: hashMap.put(KEY_TEST_CONTAINS_VALUE, VALUE_TEST_CONTAINS_KEY);
215: assertTrue("check contains of map with element", hashMap
216: .containsValue(VALUE_TEST_CONTAINS_KEY));
217: assertFalse("check contains of map other element", hashMap
218: .containsValue(VALUE_TEST_CONTAINS_DOES_NOT_EXIST));
219:
220: assertFalse(hashMap.containsValue(null));
221: hashMap.put(KEY_TEST_CONTAINS_VALUE, null);
222: assertTrue(hashMap.containsValue(null));
223: }
224:
225: /*
226: * Test method for 'java.util.HashMap.entrySet()'
227: */
228: public void testEntrySet() {
229: HashMap hashMap = new HashMap();
230: checkEmptyHashMapAssumptions(hashMap);
231:
232: Set entrySet = hashMap.entrySet();
233: assertNotNull(entrySet);
234:
235: // Check that the entry set looks right
236: hashMap.put(KEY_TEST_ENTRY_SET, VALUE_TEST_ENTRY_SET_1);
237: entrySet = hashMap.entrySet();
238: assertEquals(entrySet.size(), SIZE_ONE);
239: Iterator itSet = entrySet.iterator();
240: Map.Entry entry = (Map.Entry) itSet.next();
241: assertEquals(entry.getKey(), KEY_TEST_ENTRY_SET);
242: assertEquals(entry.getValue(), VALUE_TEST_ENTRY_SET_1);
243:
244: // Check that entries in the entrySet are update correctly on overwrites
245: hashMap.put(KEY_TEST_ENTRY_SET, VALUE_TEST_ENTRY_SET_2);
246: entrySet = hashMap.entrySet();
247: assertEquals(entrySet.size(), SIZE_ONE);
248: itSet = entrySet.iterator();
249: entry = (Map.Entry) itSet.next();
250: assertEquals(entry.getKey(), KEY_TEST_ENTRY_SET);
251: assertEquals(entry.getValue(), VALUE_TEST_ENTRY_SET_2);
252:
253: // Check that entries are updated on removes
254: hashMap.remove(KEY_TEST_ENTRY_SET);
255: checkEmptyHashMapAssumptions(hashMap);
256: }
257:
258: /*
259: * Used to test the entrySet remove method.
260: */
261: public void testEntrySetRemove() {
262: HashMap hashMap = new HashMap();
263: hashMap.put("A", "B");
264: HashMap dummy = new HashMap();
265: dummy.put("A", "b");
266: Entry bogus = (Entry) dummy.entrySet().iterator().next();
267: Set entrySet = hashMap.entrySet();
268: boolean removed = entrySet.remove(bogus);
269: assertEquals(removed, false);
270: assertEquals(hashMap.get("A"), "B");
271: }
272:
273: /*
274: * Test method for 'java.util.AbstractMap.equals(Object)'
275: */
276: public void testEquals() {
277: HashMap hashMap = new HashMap();
278: checkEmptyHashMapAssumptions(hashMap);
279:
280: hashMap.put(KEY_KEY, VALUE_VAL);
281:
282: HashMap copyMap = (HashMap) hashMap.clone();
283:
284: assertTrue(hashMap.equals(copyMap));
285: hashMap.put(VALUE_VAL, KEY_KEY);
286: assertFalse(hashMap.equals(copyMap));
287: }
288:
289: /*
290: * Test method for 'java.lang.Object.finalize()'.
291: */
292: public void testFinalize() {
293: // no tests for finalize
294: }
295:
296: /*
297: * Test method for 'java.util.HashMap.get(Object)'.
298: */
299: public void testGet() {
300: HashMap hashMap = new HashMap();
301: checkEmptyHashMapAssumptions(hashMap);
302:
303: assertNull(hashMap.get(KEY_TEST_GET));
304: hashMap.put(KEY_TEST_GET, VALUE_TEST_GET);
305: assertNotNull(hashMap.get(KEY_TEST_GET));
306:
307: assertNull(hashMap.get(null));
308: hashMap.put(null, VALUE_TEST_GET);
309: assertNotNull(hashMap.get(null));
310:
311: hashMap.put(null, null);
312: assertNull(hashMap.get(null));
313: }
314:
315: /*
316: * Test method for 'java.util.AbstractMap.hashCode()'.
317: */
318: public void testHashCode() {
319: HashMap hashMap = new HashMap();
320: checkEmptyHashMapAssumptions(hashMap);
321:
322: // Check that hashCode changes
323: int hashCode1 = hashMap.hashCode();
324: hashMap.put(KEY_KEY, VALUE_VAL);
325: int hashCode2 = hashMap.hashCode();
326:
327: assertTrue(hashCode1 != hashCode2);
328: }
329:
330: /*
331: * Test method for 'java.util.HashMap.HashMap()'.
332: */
333: public void testHashMap() {
334: HashMap hashMap = new HashMap();
335: checkEmptyHashMapAssumptions(hashMap);
336: }
337:
338: /*
339: * Test method for 'java.util.HashMap.HashMap(int)'
340: */
341: public void testHashMapInt() {
342: HashMap hashMap = new HashMap(CAPACITY_16);
343: checkEmptyHashMapAssumptions(hashMap);
344:
345: // TODO(mmendez): how do we verify capacity?
346: boolean failed = true;
347: try {
348: new HashMap(-SIZE_ONE);
349: } catch (Throwable ex) {
350: if (ex instanceof IllegalArgumentException) {
351: failed = false;
352: }
353: }
354:
355: if (failed) {
356: fail("Failure testing new HashMap(-1)");
357: }
358:
359: HashMap zeroSizedHashMap = new HashMap(0);
360: assertNotNull(zeroSizedHashMap);
361: }
362:
363: /*
364: * Test method for 'java.util.HashMap.HashMap(int, float)'
365: */
366: public void testHashMapIntFloat() {
367:
368: HashMap hashMap = new HashMap(CAPACITY_16, LOAD_FACTOR_ONE_HALF);
369: checkEmptyHashMapAssumptions(hashMap);
370:
371: // TODO(mmendez): how do we verify capacity and load factor?
372:
373: // Test new HashMap(-1, 0.0F)
374: boolean failed = true;
375: try {
376: new HashMap(CAPACITY_NEG_ONE_HALF, LOAD_FACTOR_ZERO);
377: } catch (Throwable ex) {
378: if (ex instanceof IllegalArgumentException) {
379: failed = false;
380: }
381: }
382:
383: if (failed) {
384: fail("Failure testing new HashMap(-1, 0.0F)");
385: }
386:
387: // Test new HashMap(0, -1.0F)
388: failed = true;
389: try {
390: new HashMap(CAPACITY_ZERO, LOAD_FACTOR_NEG_ONE);
391: } catch (Throwable ex) {
392: if (ex instanceof IllegalArgumentException) {
393: failed = false;
394: }
395: }
396:
397: if (failed) {
398: fail("Failure testing new HashMap(0, -1.0F)");
399: }
400:
401: // Test new HashMap(0,0F);
402: hashMap = new HashMap(CAPACITY_ZERO, LOAD_FACTOR_ONE_TENTH);
403: assertNotNull(hashMap);
404: }
405:
406: /*
407: * Test method for 'java.util.HashMap.HashMap(Map)'
408: */
409: public void testHashMapMap() {
410: HashMap srcMap = new HashMap();
411: assertNotNull(srcMap);
412: checkEmptyHashMapAssumptions(srcMap);
413:
414: srcMap.put(INTEGER_1, INTEGER_11);
415: srcMap.put(INTEGER_2, INTEGER_22);
416: srcMap.put(INTEGER_3, INTEGER_33);
417:
418: HashMap hashMap = cloneHashMap(srcMap);
419: assertFalse(hashMap.isEmpty());
420: assertTrue(hashMap.size() == SIZE_THREE);
421:
422: Collection valColl = hashMap.values();
423: assertTrue(valColl.contains(INTEGER_11));
424: assertTrue(valColl.contains(INTEGER_22));
425: assertTrue(valColl.contains(INTEGER_33));
426:
427: Collection keyColl = hashMap.keySet();
428: assertTrue(keyColl.contains(INTEGER_1));
429: assertTrue(keyColl.contains(INTEGER_2));
430: assertTrue(keyColl.contains(INTEGER_3));
431: }
432:
433: /*
434: * Test method for 'java.util.AbstractMap.isEmpty()'
435: */
436: public void testIsEmpty() {
437: HashMap srcMap = new HashMap();
438: checkEmptyHashMapAssumptions(srcMap);
439:
440: HashMap dstMap = new HashMap();
441: checkEmptyHashMapAssumptions(dstMap);
442:
443: dstMap.putAll(srcMap);
444: assertTrue(dstMap.isEmpty());
445:
446: dstMap.put(KEY_KEY, VALUE_VAL);
447: assertFalse(dstMap.isEmpty());
448:
449: dstMap.remove(KEY_KEY);
450: assertTrue(dstMap.isEmpty());
451: assertEquals(dstMap.size(), 0);
452: }
453:
454: public void testKeysConflict() {
455: HashMap hashMap = new HashMap();
456:
457: hashMap.put(STRING_ZERO_KEY, STRING_ZERO_VALUE);
458: hashMap.put(INTEGER_ZERO_KEY, INTEGER_ZERO_VALUE);
459: hashMap.put(ODD_ZERO_KEY, ODD_ZERO_VALUE);
460: assertEquals(hashMap.get(INTEGER_ZERO_KEY), INTEGER_ZERO_VALUE);
461: assertEquals(hashMap.get(ODD_ZERO_KEY), ODD_ZERO_VALUE);
462: assertEquals(hashMap.get(STRING_ZERO_KEY), STRING_ZERO_VALUE);
463: hashMap.remove(INTEGER_ZERO_KEY);
464: assertEquals(hashMap.get(ODD_ZERO_KEY), ODD_ZERO_VALUE);
465: assertEquals(hashMap.get(STRING_ZERO_KEY), STRING_ZERO_VALUE);
466: assertEquals(hashMap.get(INTEGER_ZERO_KEY), null);
467: hashMap.remove(ODD_ZERO_KEY);
468: assertEquals(hashMap.get(INTEGER_ZERO_KEY), null);
469: assertEquals(hashMap.get(ODD_ZERO_KEY), null);
470: assertEquals(hashMap.get(STRING_ZERO_KEY), STRING_ZERO_VALUE);
471: hashMap.remove(STRING_ZERO_KEY);
472: assertEquals(hashMap.get(INTEGER_ZERO_KEY), null);
473: assertEquals(hashMap.get(ODD_ZERO_KEY), null);
474: assertEquals(hashMap.get(STRING_ZERO_KEY), null);
475: assertEquals(hashMap.size(), 0);
476: }
477:
478: /*
479: * Test method for 'java.util.HashMap.keySet()'
480: */
481: public void testKeySet() {
482: HashMap hashMap = new HashMap();
483: checkEmptyHashMapAssumptions(hashMap);
484:
485: Set keySet = hashMap.keySet();
486: assertNotNull(keySet);
487: assertTrue(keySet.isEmpty());
488: assertTrue(keySet.size() == 0);
489:
490: hashMap.put(KEY_TEST_KEY_SET, VALUE_TEST_KEY_SET);
491:
492: assertTrue(keySet.size() == SIZE_ONE);
493: assertTrue(keySet.contains(KEY_TEST_KEY_SET));
494: assertFalse(keySet.contains(VALUE_TEST_KEY_SET));
495: assertFalse(keySet.contains(KEY_TEST_KEY_SET.toUpperCase()));
496: }
497:
498: /*
499: * Test method for 'java.util.HashMap.put(Object, Object)'
500: */
501: public void testPut() {
502: HashMap hashMap = new HashMap();
503: checkEmptyHashMapAssumptions(hashMap);
504:
505: assertNull(hashMap.put(KEY_TEST_PUT, VALUE_TEST_PUT_1));
506: assertEquals(hashMap.put(KEY_TEST_PUT, VALUE_TEST_PUT_2),
507: VALUE_TEST_PUT_1);
508: assertNull(hashMap.put(null, VALUE_TEST_PUT_1));
509: assertEquals(hashMap.put(null, VALUE_TEST_PUT_2),
510: VALUE_TEST_PUT_1);
511: }
512:
513: /**
514: * Test method for 'java.util.HashMap.putAll(Map)'.
515: */
516: public void testPutAll() {
517: HashMap srcMap = new HashMap();
518: checkEmptyHashMapAssumptions(srcMap);
519:
520: srcMap.put(KEY_1, VALUE_1);
521: srcMap.put(KEY_2, VALUE_2);
522: srcMap.put(KEY_3, VALUE_3);
523:
524: // Make sure that the data is copied correctly
525: HashMap dstMap = new HashMap();
526: checkEmptyHashMapAssumptions(dstMap);
527:
528: dstMap.putAll(srcMap);
529: assertEquals(srcMap.size(), dstMap.size());
530: assertTrue(dstMap.containsKey(KEY_1));
531: assertTrue(dstMap.containsValue(VALUE_1));
532: assertFalse(dstMap.containsKey(KEY_1.toUpperCase()));
533: assertFalse(dstMap.containsValue(VALUE_1.toUpperCase()));
534:
535: assertTrue(dstMap.containsKey(KEY_2));
536: assertTrue(dstMap.containsValue(VALUE_2));
537: assertFalse(dstMap.containsKey(KEY_2.toUpperCase()));
538: assertFalse(dstMap.containsValue(VALUE_2.toUpperCase()));
539:
540: assertTrue(dstMap.containsKey(KEY_3));
541: assertTrue(dstMap.containsValue(VALUE_3));
542: assertFalse(dstMap.containsKey(KEY_3.toUpperCase()));
543: assertFalse(dstMap.containsValue(VALUE_3.toUpperCase()));
544:
545: // Check that an empty map does not blow away the contents of the destination map
546: HashMap emptyMap = new HashMap();
547: checkEmptyHashMapAssumptions(emptyMap);
548: dstMap.putAll(emptyMap);
549: assertTrue(dstMap.size() == srcMap.size());
550:
551: // Check that put all overwrite any existing mapping in the destination map
552: srcMap.put(KEY_1, VALUE_2);
553: srcMap.put(KEY_2, VALUE_3);
554: srcMap.put(KEY_3, VALUE_1);
555:
556: dstMap.putAll(srcMap);
557: assertEquals(dstMap.size(), srcMap.size());
558: assertEquals(dstMap.get(KEY_1), VALUE_2);
559: assertEquals(dstMap.get(KEY_2), VALUE_3);
560: assertEquals(dstMap.get(KEY_3), VALUE_1);
561:
562: // Check that a putAll does adds data but does not remove it
563:
564: srcMap.put(KEY_4, VALUE_4);
565: dstMap.putAll(srcMap);
566: assertEquals(dstMap.size(), srcMap.size());
567: assertTrue(dstMap.containsKey(KEY_4));
568: assertTrue(dstMap.containsValue(VALUE_4));
569: assertEquals(dstMap.get(KEY_1), VALUE_2);
570: assertEquals(dstMap.get(KEY_2), VALUE_3);
571: assertEquals(dstMap.get(KEY_3), VALUE_1);
572: assertEquals(dstMap.get(KEY_4), VALUE_4);
573:
574: dstMap.putAll(dstMap);
575: }
576:
577: /**
578: * Test method for 'java.util.HashMap.remove(Object)'.
579: */
580: public void testRemove() {
581: HashMap hashMap = new HashMap();
582: checkEmptyHashMapAssumptions(hashMap);
583:
584: assertNull(hashMap.remove(null));
585: hashMap.put(null, VALUE_TEST_REMOVE);
586: assertNotNull(hashMap.remove(null));
587:
588: hashMap.put(KEY_TEST_REMOVE, VALUE_TEST_REMOVE);
589: assertEquals(hashMap.remove(KEY_TEST_REMOVE), VALUE_TEST_REMOVE);
590: assertNull(hashMap.remove(KEY_TEST_REMOVE));
591: }
592:
593: /**
594: * Test method for 'java.util.HashMap.size()'.
595: */
596: public void testSize() {
597: HashMap hashMap = new HashMap();
598: checkEmptyHashMapAssumptions(hashMap);
599:
600: // Test size behavior on put
601: assertEquals(hashMap.size(), SIZE_ZERO);
602: hashMap.put(KEY_1, VALUE_1);
603: assertEquals(hashMap.size(), SIZE_ONE);
604: hashMap.put(KEY_2, VALUE_2);
605: assertEquals(hashMap.size(), SIZE_TWO);
606: hashMap.put(KEY_3, VALUE_3);
607: assertEquals(hashMap.size(), SIZE_THREE);
608:
609: // Test size behavior on remove
610: hashMap.remove(KEY_1);
611: assertEquals(hashMap.size(), SIZE_TWO);
612: hashMap.remove(KEY_2);
613: assertEquals(hashMap.size(), SIZE_ONE);
614: hashMap.remove(KEY_3);
615: assertEquals(hashMap.size(), SIZE_ZERO);
616:
617: // Test size behavior on putAll
618: hashMap.put(KEY_1, VALUE_1);
619: hashMap.put(KEY_2, VALUE_2);
620: hashMap.put(KEY_3, VALUE_3);
621: HashMap srcMap = cloneHashMap(hashMap);
622: hashMap.putAll(srcMap);
623: assertEquals(hashMap.size(), SIZE_THREE);
624:
625: // Test size behavior on clear
626: hashMap.clear();
627: assertEquals(hashMap.size(), SIZE_ZERO);
628: }
629:
630: /**
631: * Test method for 'java.util.AbstractMap.toString()'.
632: */
633: public void testToString() {
634: HashMap hashMap = new HashMap();
635: checkEmptyHashMapAssumptions(hashMap);
636: hashMap.put(KEY_KEY, VALUE_VAL);
637: String entryString = makeEntryString(KEY_KEY, VALUE_VAL);
638: assertTrue(entryString.equals(hashMap.toString()));
639: }
640:
641: /**
642: * Test method for 'java.util.AbstractMap.values()'.
643: */
644: public void testValues() {
645: HashMap hashMap = new HashMap();
646: checkEmptyHashMapAssumptions(hashMap);
647:
648: assertNotNull(hashMap.values());
649:
650: hashMap.put(KEY_KEY, VALUE_VAL);
651:
652: Collection valColl = hashMap.values();
653: assertNotNull(valColl);
654: assertEquals(valColl.size(), SIZE_ONE);
655:
656: Iterator itVal = valColl.iterator();
657: String val = (String) itVal.next();
658: assertEquals(val, VALUE_VAL);
659: }
660:
661: protected Map makeEmptyMap() {
662: return new HashMap();
663: }
664:
665: /**
666: * This method exists because java 1.5 no longer has HashMap(HashMap), replacing
667: * it with HashMap(Map<? extends K, ? extends V> m). Nevertheless, we want
668: * to use it in web mode to test that web mode function.
669: *
670: * @param hashMap the HashMap to be copied
671: * @return the copy
672: */
673: private HashMap cloneHashMap(HashMap hashMap) {
674: if (GWT.isScript()) {
675: return new HashMap(hashMap);
676: } else {
677: HashMap m = new HashMap();
678: m.putAll(hashMap);
679: return m;
680: }
681: }
682:
683: private Iterator iterateThrough(final HashMap expected) {
684: Iterator iter = expected.entrySet().iterator();
685: for (int i = 0; i < expected.size(); i++) {
686: iter.next();
687: }
688: return iter;
689: }
690:
691: private String makeEntryString(final String key, final String value) {
692: return "{" + key + "=" + value + "}";
693: }
694: }
|