KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > arooa > registry > ComponentRegistryTest


1 /*
2  * (c) Rob Gordon 2005
3  */

4 package org.oddjob.arooa.registry;
5
6 import junit.framework.TestCase;
7
8 /**
9  *
10  */

11 public class ComponentRegistryTest extends TestCase {
12
13     class Component {
14         final String JavaDoc name;
15         Component(String JavaDoc name) {
16             this.name = name;
17         }
18         public String JavaDoc toString() {
19             return name;
20         }
21     }
22     
23     /** Test a single local registry with no children. */
24     public void testSingle() {
25         Object JavaDoc comp = new Object JavaDoc();
26         ComponentRegistry cr = new ComponentRegistry();
27         cr.register("foo", comp);
28
29         // check we can get the path.
30
assertEquals("foo", cr.pathForObject(comp).toString());
31         
32         // check we can look it up again by path.
33
assertEquals(
34                 comp, cr.objectForPath(
35                         new Path("foo")));
36         
37         // check it has an address
38
Address address = cr.addressForObject(comp);
39         assertNotNull(address);
40         
41         // and we can look it up by that address.
42
assertEquals(
43                 comp, cr.objectForAddress(
44                         address));
45         
46         
47     }
48     
49     /** Test a hierarchy for the same server */
50     public void testSameServer() {
51         Object JavaDoc comp1 = new Component("comp1");
52
53         ComponentRegistry cr1 = new ComponentRegistry();
54         cr1.register("a", comp1);
55
56         ComponentRegistry cr2 = new ComponentRegistry();
57
58         Object JavaDoc comp2 = new Component("comp2");
59         
60         cr1.addChild(cr2, comp1);
61         
62         cr2.register("b", comp2);
63         
64         // check we can get the path.
65
assertEquals("a/b", cr1.pathForObject(comp2).toString());
66         
67         
68         // check we can get object for path
69
assertEquals(
70                 comp2, cr1.objectForPath(
71                         new Path("a/b")));
72         
73         // check there is and address
74
Address address = cr2.addressForObject(comp2);
75         assertNotNull(address);
76
77         // check the address
78
assertEquals("local:a/b", address.toString());
79         
80         // check we can get the object back for addresses.
81
assertEquals(
82                 comp2, cr1.objectForAddress(
83                         address));
84         
85     }
86
87     /** Test a hierarchy with a different server */
88     public void testDifferentServer() {
89         Object JavaDoc comp1 = new Component("comp1");
90
91         ComponentRegistry cr1 = new ComponentRegistry(new ServerId("server1"));
92         cr1.register("a", comp1);
93
94         ComponentRegistry cr2 = new ComponentRegistry(new ServerId("server2"));
95
96         Object JavaDoc comp2 = new Component("comp2");
97         
98         cr1.addChild(cr2, comp1);
99         
100         cr2.register("b", comp2);
101
102         // check we can get the path.
103
assertEquals("a/b", cr1.pathForObject(comp2).toString());
104
105         // check we can get the object by path
106
assertEquals(
107                 comp2, cr1.objectForPath(
108                         new Path("a/b")));
109         
110         // check there are now two addresses
111
Address address = cr2.addressForObject(comp2);
112         assertNotNull(address);
113
114         assertEquals("server2:b", address.toString());
115         
116         // check finding the second server
117
ComponentRegistry checkCR = cr1.registryForServer(new ServerId("server2"));
118         assertNotNull(checkCR);
119         
120         // check we get the component back for addresses
121
assertEquals(
122                 comp2, cr1.objectForAddress(
123                         address));
124         
125     }
126     
127     /** Test a double sided owner object - has an id in both registries such
128      * ass Oddjob */

129     public void testTwoFaced() {
130         Object JavaDoc comp1 = new Object JavaDoc();
131
132         ComponentRegistry cr1 = new ComponentRegistry();
133         cr1.register("a", comp1);
134
135         ComponentRegistry cr2 = new ComponentRegistry();
136
137         cr1.addChild(cr2, comp1);
138         
139         cr2.register("b", comp1);
140         
141         // check we the shortest path.
142
assertEquals("a", cr1.pathForObject(comp1).toString());
143         
144         
145         assertEquals(
146                 comp1, cr1.objectForPath(
147                         new Path("a/b")));
148         
149         Address address = cr2.addressForObject(comp1);
150         assertNotNull(address);
151
152         assertEquals("local:a/b", address.toString());
153         
154         assertEquals(
155                 comp1, cr1.objectForAddress(
156                         address));
157         
158     }
159 }
160
Popular Tags