01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.util;
05:
06: import org.apache.commons.lang.ClassUtils;
07:
08: import java.util.HashMap;
09: import java.util.Map;
10:
11: import junit.framework.TestCase;
12:
13: public class CompositeIdentifierTest extends TestCase {
14:
15: public void testOrderMatters() {
16: AbstractIdentifier id1 = new IDType1(1);
17: AbstractIdentifier id2 = new IDType2(1);
18:
19: CompositeIdentifier cid1 = new CompositeIdentifier(
20: new AbstractIdentifier[] { id1, id2 });
21: CompositeIdentifier cid2 = new CompositeIdentifier(
22: new AbstractIdentifier[] { id2, id1 });
23:
24: assertFalse(cid1.equals(cid2));
25: assertFalse(cid2.equals(cid1));
26: }
27:
28: public void testContains() {
29: AbstractIdentifier id1 = new IDType1(1);
30: AbstractIdentifier id2 = new IDType2(1);
31:
32: CompositeIdentifier cid1 = new CompositeIdentifier(
33: new AbstractIdentifier[] { id1, id2 });
34: assertTrue(cid1.contains(id1));
35: assertTrue(cid1.contains(id2));
36:
37: assertFalse(cid1.contains(new IDType1(2)));
38: }
39:
40: public void testBasic() {
41: Map map = new HashMap();
42:
43: AbstractIdentifier id1 = new IDType1(1);
44: AbstractIdentifier id2 = new IDType2(1);
45:
46: CompositeIdentifier cid1 = new CompositeIdentifier(
47: new AbstractIdentifier[] { id1, id2 });
48: CompositeIdentifier cid2 = new CompositeIdentifier(
49: new AbstractIdentifier[] { id1, id1 });
50:
51: map.put(cid1, "yo");
52:
53: assertEquals("yo", map.get(cid1));
54: assertFalse(map.containsKey(cid2));
55: }
56:
57: public void testToString() {
58: AbstractIdentifier id1 = new IDType1(1);
59: AbstractIdentifier id2 = new IDType2(1);
60:
61: CompositeIdentifier cid1 = new CompositeIdentifier(
62: new AbstractIdentifier[] { id1, id2 });
63:
64: System.out.println(cid1);
65: }
66:
67: public static class IDType1 extends AbstractIdentifier {
68: public IDType1(long id) {
69: super (id);
70: }
71:
72: public String getIdentifierType() {
73: return ClassUtils.getShortClassName(getClass());
74: }
75: }
76:
77: public static class IDType2 extends AbstractIdentifier {
78: public IDType2(long id) {
79: super (id);
80: }
81:
82: public String getIdentifierType() {
83: return ClassUtils.getShortClassName(getClass());
84: }
85: }
86:
87: }
|