KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > util > CompositeIdentifierTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.util;
5
6 import org.apache.commons.lang.ClassUtils;
7
8 import java.util.HashMap JavaDoc;
9 import java.util.Map JavaDoc;
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(new AbstractIdentifier[] { id1, id2 });
20     CompositeIdentifier cid2 = new CompositeIdentifier(new AbstractIdentifier[] { id2, id1 });
21
22     assertFalse(cid1.equals(cid2));
23     assertFalse(cid2.equals(cid1));
24   }
25
26   public void testContains() {
27     AbstractIdentifier id1 = new IDType1(1);
28     AbstractIdentifier id2 = new IDType2(1);
29
30     CompositeIdentifier cid1 = new CompositeIdentifier(new AbstractIdentifier[] { id1, id2 });
31     assertTrue(cid1.contains(id1));
32     assertTrue(cid1.contains(id2));
33
34     assertFalse(cid1.contains(new IDType1(2)));
35   }
36
37   public void testBasic() {
38     Map JavaDoc map = new HashMap JavaDoc();
39
40     AbstractIdentifier id1 = new IDType1(1);
41     AbstractIdentifier id2 = new IDType2(1);
42
43     CompositeIdentifier cid1 = new CompositeIdentifier(new AbstractIdentifier[] { id1, id2 });
44     CompositeIdentifier cid2 = new CompositeIdentifier(new AbstractIdentifier[] { id1, id1 });
45
46     map.put(cid1, "yo");
47
48     assertEquals("yo", map.get(cid1));
49     assertFalse(map.containsKey(cid2));
50   }
51
52   public void testToString() {
53     AbstractIdentifier id1 = new IDType1(1);
54     AbstractIdentifier id2 = new IDType2(1);
55
56     CompositeIdentifier cid1 = new CompositeIdentifier(new AbstractIdentifier[] { id1, id2 });
57
58     System.out.println(cid1);
59   }
60
61   public static class IDType1 extends AbstractIdentifier {
62     public IDType1(long id) {
63       super(id);
64     }
65
66     public String JavaDoc getIdentifierType() {
67       return ClassUtils.getShortClassName(getClass());
68     }
69   }
70
71   public static class IDType2 extends AbstractIdentifier {
72     public IDType2(long id) {
73       super(id);
74     }
75
76     public String JavaDoc getIdentifierType() {
77       return ClassUtils.getShortClassName(getClass());
78     }
79   }
80
81 }
82
Popular Tags