KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > remoting > detection > multicast > MulticastUnitTestCase


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.test.remoting.detection.multicast;
8
9 import java.io.ByteArrayInputStream JavaDoc;
10 import javax.management.MBeanServer JavaDoc;
11 import javax.management.MBeanServerFactory JavaDoc;
12 import javax.management.ObjectName JavaDoc;
13 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
14 import org.jboss.remoting.InvokerLocator;
15 import org.jboss.remoting.detection.multicast.MulticastDetector;
16 import org.jboss.remoting.network.NetworkRegistry;
17 import org.jboss.remoting.transport.Connector;
18 import org.jboss.test.remoting.TestUtil;
19 import org.w3c.dom.Document JavaDoc;
20
21 import junit.framework.TestCase;
22
23 /**
24  * Just tests that detector A sees detector B when B comes online then off.
25  *
26  * @author <a HREF="mailto:telrod@e2technologies.net">Tom Elrod</a>
27  */

28 public class MulticastUnitTestCase extends TestCase
29 {
30    public MulticastUnitTestCase(String JavaDoc name)
31    {
32       super(name);
33    }
34
35    public void testDetectors() throws Exception JavaDoc
36    {
37       MulticastDetector detector1 = new MulticastDetector();
38       MulticastDetector detector2 = new MulticastDetector();
39
40       Connector connector1 = new Connector();
41       Connector connector2 = new Connector();
42
43       NetworkRegistry reg1 = setupServers(detector1, connector1);
44       NetworkRegistry reg2 = setupServers(detector2, connector2);
45
46       // Need to allow heartbeat so have detection
47
Thread.currentThread().sleep(2000);
48
49       //Should now have an entry for both of the registries
50
int reg1Count = reg1.getServers().length;
51       int reg2Count = reg2.getServers().length;
52
53       // Actual junit test
54
assertTrue(reg1Count == 1 && reg2Count == 1);
55
56       if(reg1Count == 1 && reg2Count == 1)
57       {
58          System.out.println("PASSED - both registries have found the detectors.");
59       }
60       else
61       {
62          System.out.println("FAILED - registries not populated with remote detectors.");
63       }
64
65       // stop the 2nd detector, so see if 1st one detects it is missing
66
connector1.stop();
67       connector1.destroy();
68       connector1 = null;
69       connector2.stop();
70       connector2.destroy();
71       connector2 = null;
72       detector1.stop();
73
74       // sleep for a few seconds so the 1st detector can discover 2nd one down
75
Thread.currentThread().sleep(60000);
76
77       // 1st one should be empty
78
reg1Count = reg2.getServers().length;
79
80       // Actual junit test
81
assertTrue(reg1Count == 0);
82
83       if(reg1Count == 0)
84       {
85          System.out.println("PASSED - 2nd detector stopped and no longer in registry.");
86       }
87       else
88       {
89          System.out.println("FAILED - 2nd detector stopped but still in registry.");
90       }
91
92       // cleanup
93
detector2.stop();
94       //connector2.stop();
95
//connector2.destroy();
96
}
97
98
99    private synchronized NetworkRegistry setupServers(MulticastDetector detector, Connector connector)
100    {
101       NetworkRegistry registry = null;
102       System.setProperty("jboss.identity", String.valueOf(System.currentTimeMillis()));
103       System.out.println("jboss.identity = " + System.getProperty("jboss.identity"));
104
105       try
106       {
107          MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
108
109          //registry = NetworkRegistry.getInstance();
110
registry = TestNetworkRegistry.createNetworkRegistry();
111          server.registerMBean(registry, new ObjectName JavaDoc("remoting:type=NetworkRegistry"));
112
113          //int port = Math.abs(new Random().nextInt(2000));
114
int port = TestUtil.getRandomPort();
115          System.out.println("port = " + port);
116
117
118          InvokerLocator locator = new InvokerLocator("socket://localhost:" + port);
119          StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
120          buf.append("<?xml version=\"1.0\"?>\n");
121          buf.append("<handlers>\n");
122          buf.append(" <handler subsystem=\"mock\">org.jboss.test.remoting.transport.mock.MockServerInvocationHandler</handler>\n");
123          buf.append("</handlers>\n");
124          Document JavaDoc xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream JavaDoc(buf.toString().getBytes()));
125          connector.setInvokerLocator(locator.getLocatorURI());
126          connector.setConfiguration(xml.getDocumentElement());
127          ObjectName JavaDoc obj = new ObjectName JavaDoc("jboss.remoting:type=Connector,transport=" + locator.getProtocol());
128          server.registerMBean(connector, obj);
129          //connector.create();
130
connector.start();
131
132          //Need to set new domain for identity
133
server.registerMBean(detector, new ObjectName JavaDoc("remoting:type=JNDIDetector"));
134
135          // set config info for detector and start it.
136
detector.start();
137       }
138       catch(Exception JavaDoc e)
139       {
140          e.printStackTrace();
141       }
142
143       return registry;
144    }
145
146    private static class TestNetworkRegistry extends NetworkRegistry
147    {
148       public static NetworkRegistry createNetworkRegistry()
149       {
150          return new TestNetworkRegistry();
151       }
152    }
153
154 }
155
Popular Tags