KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > jmx > JMXClientJobTest


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

4 package org.oddjob.jmx;
5
6
7 import junit.framework.TestCase;
8
9 import org.apache.commons.beanutils.DynaBean;
10 import org.apache.commons.beanutils.PropertyUtils;
11 import org.apache.log4j.Logger;
12 import org.oddjob.Oddjob;
13 import org.oddjob.Resetable;
14 import org.oddjob.Stoppable;
15 import org.oddjob.Structural;
16 import org.oddjob.arooa.ArooaConstants;
17 import org.oddjob.arooa.ArooaContext;
18 import org.oddjob.arooa.ArooaRuntime;
19 import org.oddjob.arooa.registry.Address;
20 import org.oddjob.arooa.registry.ComponentRegistry;
21 import org.oddjob.arooa.registry.Path;
22 import org.oddjob.arooa.registry.ServerId;
23 import org.oddjob.jmx.client.ComponentTransportable;
24 import org.oddjob.jobs.structural.FolderJob;
25 import org.oddjob.state.JobState;
26 import org.oddjob.structural.ChildHelper;
27 import org.oddjob.structural.StructuralListener;
28
29 /**
30  *
31  */

32 public class JMXClientJobTest extends TestCase {
33     static final Logger logger = Logger.getLogger(JMXClientJobTest.class);
34     
35     public class ServerChild implements Structural {
36         ChildHelper childHelper = new ChildHelper(this);
37         String JavaDoc name;
38         ServerChild(String JavaDoc name) {
39             this.name = name;
40         }
41         public String JavaDoc toString() {
42             return name;
43         }
44         public void addStructuralListener(StructuralListener listener) {
45             childHelper.addStructuralListener(listener);
46         }
47         public void removeStructuralListener(StructuralListener listener) {
48             childHelper.removeStructuralListener(listener);
49         }
50     }
51     
52         
53     public void setUp() {
54         logger.debug("================== Running " + getName() + "================");
55 // System.setProperty("mx4j.log.priority", "trace");
56
}
57     
58     JMXServerJob createServer() {
59         ServerChild c1 = new ServerChild("test");
60         c1.childHelper.addChild(new ServerChild("test1"));
61         c1.childHelper.addChild(new ServerChild("test2"));
62         c1.childHelper.addChild(new ServerChild("test3"));
63         
64         ArooaContext context = new ArooaContext();
65         context.set(ArooaConstants.COMPONENT_REGISTRY, new ComponentRegistry());
66         context.set(ArooaConstants.CURRENTLY_CONFIGURING,
67                 new ArooaRuntime(new Object JavaDoc(), "dummy", context));
68         
69         JMXServerJob j = new JMXServerJob();
70         j.setRoot(c1);
71         j.setContext(context);
72                 
73         j.setUrl("service:jmx:rmi://");
74         return j;
75     }
76     
77     public void testRun() throws Exception JavaDoc {
78         
79         JMXServerJob server = createServer();
80         server.start();
81         
82         JMXClientJob client = new JMXClientJob();
83         client.setUrl(server.getAddress());
84         client.run();
85         
86         WaitForChildren w1 = new WaitForChildren(client);
87         // wait for the notifications
88
w1.waitFor(1);
89         assertEquals("child", "test", w1.children()[0].toString());
90         
91         WaitForChildren w2 = new WaitForChildren(w1.children()[0]);
92         w2.waitFor(3);
93         
94         // got this far - must have worked.
95
client.stop();
96         server.stop();
97         
98         assertEquals(JobState.COMPLETE, client.lastJobStateEvent().getJobState());
99     }
100     
101     /**
102      * This test creates a server and then attempts to
103      * connect and disconnect multiple times
104      *
105      * - it doesn't work and I don't know why!!!
106      */

107 // public void testRunLotsOfClients() throws Exception {
108
// final JMXClientJob c = new JMXClientJob();
109
//
110
// MyListener l = new MyListener();
111
// c.addStructuralListener(l);
112
//
113
//
114
// Thread t = new Thread(j);
115
// t.start();
116
//
117
// while (j.getAddress() == null) {
118
// Thread.yield();
119
// }
120
//
121
// c.setUrl(j.getAddress());
122
// for (int i = 0; i < 10; i++) {
123
// logger.debug("************ " + i + " *************");
124
// Thread t2 = new Thread(c);
125
// t2.start();
126
// Thread.sleep(1000);
127
// assertEquals("child", "test", l.child.toString());
128
// MyListener l2 = new MyListener();
129
// l.child.addStructuralListener(l2);
130
// assertEquals("children", 3, l2.count);
131
// c.stop();
132
// t2.join();
133
// }
134
//
135
// logger.debug("stopping server");
136
// j.stop();
137
// t.join();
138
//
139
// }
140

141     // test looking up a property on a server using a path.
142
public void testLookup() throws Exception JavaDoc {
143         Oddjob server = new Oddjob();
144         server.setInput(this.getClass().getResourceAsStream("server1.xml"));
145         server.run();
146         
147         Object JavaDoc sj = server.lookup("server");
148                 
149         Oddjob client = new Oddjob();
150         client.setInput(this.getClass().getResourceAsStream("client1.xml"));
151         String JavaDoc address = (String JavaDoc) PropertyUtils.getProperty(sj, "address");
152         assertNotNull(address);
153         
154         client.setArgs(new String JavaDoc[] { address });
155
156         client.run();
157         client.stop();
158         
159         server.stop();
160         
161         assertEquals(JobState.COMPLETE, client.lastJobStateEvent().getJobState());
162         assertEquals(JobState.COMPLETE, server.lastJobStateEvent().getJobState());
163     }
164
165     public static class Echo {
166         Object JavaDoc echo;
167         public Object JavaDoc getEchoWrapped() {
168             logger.debug("returning [" + echo + "]");
169             return new ComponentTransportable(
170                     new Address(
171                             new ServerId(""), new Path("echo")));
172         }
173         public void setEcho(Object JavaDoc echo) {
174             logger.debug("setting [" + echo + "]");
175             this.echo = echo;
176         }
177         
178     }
179
180     public void testHostRelative() throws Exception JavaDoc {
181         ComponentRegistry cr = new ComponentRegistry();
182         Echo e = new Echo();
183         cr.register("echo", e);
184         
185         ArooaContext context = new ArooaContext();
186         context.set(ArooaConstants.COMPONENT_REGISTRY, cr);
187         
188         final JMXServerJob server = new JMXServerJob();
189         server.setRoot(e);
190         server.setContext(context);
191                 
192         server.setUrl("service:jmx:rmi://");
193
194         ComponentRegistry localCR = new ComponentRegistry();
195         ArooaContext localAC = new ArooaContext();
196         localAC.set(ArooaConstants.COMPONENT_REGISTRY, localCR);
197         JMXClientJob client = new JMXClientJob();
198         localCR.register("client", client);
199         client.setContext(localAC);
200
201         server.start();
202         
203         client.setUrl(server.getAddress());
204         client.run();
205         
206         DynaBean bean = (DynaBean) localCR.objectForPath(
207                         new Path("client/echo"));
208         assertNotNull(bean);
209                 
210         bean.set("echo", bean);
211                 
212         Object JavaDoc echo = bean.get("echoWrapped");
213         assertEquals(bean, echo);
214     
215         client.stop();
216         server.stop();
217
218         assertEquals(JobState.COMPLETE, client.lastJobStateEvent().getJobState());
219     }
220
221     // test a remote nested registry.
222
public void testRemoteNestedRegistry() throws Exception JavaDoc {
223         FolderJob comp1 = new FolderJob();
224         Object JavaDoc comp2 = new Object JavaDoc() {
225             public String JavaDoc toString() {return "comp1";}
226         };
227         comp1.addComponent(comp2);
228         
229         FolderJob folder = new FolderJob();
230         folder.addComponent(comp1);
231         
232         ComponentRegistry cr1 = new ComponentRegistry();
233         cr1.register("folder", folder);
234         ComponentRegistry cr2 = new ComponentRegistry();
235         cr1.addChild(cr2, comp1);
236         cr2.register("comp2", comp2);
237         
238         ArooaContext context = new ArooaContext();
239         context.set(ArooaConstants.COMPONENT_REGISTRY, cr1);
240         
241         JMXServerJob server = new JMXServerJob();
242         server.setRoot(folder);
243         server.setContext(context);
244                 
245         server.setUrl("service:jmx:rmi://");
246         server.start();
247
248         ComponentRegistry localCR = new ComponentRegistry();
249         ArooaContext localAC = new ArooaContext();
250         localAC.set(ArooaConstants.COMPONENT_REGISTRY, localCR);
251         JMXClientJob client = new JMXClientJob();
252         client.setContext(localAC);
253         
254         client.setUrl(server.getAddress());
255         client.run();
256         
257         
258         final ResultHolder holder = new ResultHolder();
259         // test we can look up a component in a nested registry.
260
ComponentRegistry mirrorCR1 = localCR.registryOwnedBy(client);
261         Structural folderProxy = (Structural) mirrorCR1.objectForPath(new Path("folder"));
262         assertNotNull(folderProxy);
263
264         // takes a while for the notifications...
265
WaitForChildren w1 = new WaitForChildren(folderProxy);
266         w1.waitFor(1);
267         
268         Object JavaDoc[] folderChildren = w1.children();
269         
270         // this just tests our test!
271
assertEquals(1, folderChildren.length);
272         
273         Object JavaDoc proxy1 = folderChildren[0];
274         WaitForChildren w2 = new WaitForChildren(proxy1);
275         // wait for comp1
276
w2.waitFor(1);
277         
278         ComponentRegistry mirrorCR2 = mirrorCR1.registryOwnedBy(proxy1);
279         assertNotNull(mirrorCR2);
280         holder.result = mirrorCR2.objectForPath(new Path("comp2"));
281
282         client.stop();
283         server.stop();
284         
285         assertNotNull(holder.result);
286     }
287
288     class ResultHolder {
289         Object JavaDoc result;
290     }
291
292     /**
293      * When a client node such as Oddjob resets, check that the client
294      * removes it's registry.
295      */

296     public void testRegistryManagement() throws Exception JavaDoc {
297         
298         Oddjob oj = new Oddjob();
299         oj.setInput(JMXClientJobTest.class.getResourceAsStream("JMXClientJobTest1.xml"));
300         oj.setArgs(1, JMXClientJobTest.class.getResourceAsStream("JMXClientJobTest3.xml"));
301         oj.setArgs(0, JMXClientJobTest.class.getResourceAsStream("JMXClientJobTest2.xml"));
302         oj.run();
303                         
304         JMXClientJob client = new JMXClientJob();
305         
306         Object JavaDoc server = oj.lookup("server");
307         client.setUrl((String JavaDoc) PropertyUtils.getProperty(
308                 server, "address"));
309         
310         client.run();
311         
312         final ResultHolder holder = new ResultHolder();
313         
314         Object JavaDoc firstoj = client.lookup("oj");
315         assertNotNull(firstoj);
316         
317         WaitForChildren w1 = new WaitForChildren(firstoj);
318         w1.waitFor(1);
319         
320         Object JavaDoc seq = client.lookup("oj/seq");
321         assertNotNull(seq);
322         
323         WaitForChildren w2 = new WaitForChildren(seq);
324         w2.waitFor(2);
325         
326         Resetable nested = (Resetable) client.lookup("oj/oj");
327         assertNotNull(nested);
328
329         // takes a while for the notifications...
330
WaitForChildren w3 = new WaitForChildren(nested);
331         WaitForState w4 = new WaitForState(nested);
332
333         while (client.lookup("oj/oj/fruit") == null) {
334             Thread.yield();
335         }
336         
337         w4.waitFor(JobState.COMPLETE);
338         
339         nested.hardReset();
340
341         w4.waitFor(JobState.READY);
342         
343         ((Runnable JavaDoc) nested).run();
344
345         while (client.lookup("oj/oj/fruit") == null) {
346             Thread.yield();
347         }
348                             
349         w4.waitFor(JobState.COMPLETE);
350         
351         nested.hardReset();
352
353         w4.waitFor(JobState.READY);
354         
355         ((Runnable JavaDoc) nested).run();
356
357         while (client.lookup("oj/oj/fruit") == null) {
358             Thread.yield();
359         }
360         
361         w4.waitFor(JobState.COMPLETE);
362         
363         holder.result = "Done";
364
365         client.stop();
366         ((Stoppable) server).stop();
367         
368         assertNotNull(holder.result);
369
370         
371     }
372 }
373
Popular Tags