01: /*
02: * Copyright (C) 2004 Joe Walnes.
03: * Copyright (C) 2006, 2007 XStream Committers.
04: * All rights reserved.
05: *
06: * The software in this package is published under the terms of the BSD
07: * style license a copy of which has been included with this distribution in
08: * the LICENSE.txt file.
09: *
10: * Created on 30. May 2004 by Joe Walnes
11: */
12: package com.thoughtworks.xstream.core.util;
13:
14: import junit.framework.TestCase;
15:
16: public class ObjectIdDictionaryTest extends TestCase {
17:
18: public void testMapsIdsToObjectReferences() {
19: ObjectIdDictionary dict = new ObjectIdDictionary();
20: Object a = new Object();
21: Object b = new Object();
22: Object c = new Object();
23: dict.associateId(a, "id a");
24: dict.associateId(b, "id b");
25: dict.associateId(c, "id c");
26: assertEquals("id a", dict.lookupId(a));
27: assertEquals("id b", dict.lookupId(b));
28: assertEquals("id c", dict.lookupId(c));
29: }
30:
31: public void testTreatsObjectsThatAreEqualButNotSameInstanceAsDifferentReference() {
32: ObjectIdDictionary dict = new ObjectIdDictionary();
33: Integer a = new Integer(3);
34: Integer b = new Integer(3);
35: dict.associateId(a, "id a");
36: dict.associateId(b, "id b");
37: assertEquals("id a", dict.lookupId(a));
38: assertEquals("id b", dict.lookupId(b));
39: }
40:
41: public void testEnforceSameSystemHashCodeForGCedObjects() {
42: // create 100000 Strings and call GC after creation of 10000
43: final int loop = 10;
44: final int elements = 10000;
45: final int[] dictSizes = new int[loop * elements];
46: ObjectIdDictionary dict = new ObjectIdDictionary();
47: for (int i = 0; i < loop; ++i) {
48: System.gc();
49: for (int j = 0; j < elements; ++j) {
50: final String s = new String("JUnit ") + j; // enforce new object
51: dictSizes[i * elements + j] = dict.size();
52: assertFalse("Failed in (" + i + "/" + j + ")", dict
53: .containsId(s));
54: dict.associateId(s, "X");
55: }
56: }
57: assertFalse("Algorithm did not reach last element",
58: 0 == dictSizes[loop * elements - 1]);
59: assertFalse("Dictionary did not shrink",
60: loop * elements - 1 == dictSizes[loop * elements - 1]);
61: }
62: }
|