KickJava   Java API By Example, From Geeks To Geeks.

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

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