KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > remoting > detection > metadata > MetadataTestCase


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.metadata;
8
9 import java.io.ByteArrayInputStream JavaDoc;
10 import java.util.List JavaDoc;
11 import javax.management.MBeanServer JavaDoc;
12 import javax.management.MBeanServerFactory JavaDoc;
13 import javax.management.Notification JavaDoc;
14 import javax.management.NotificationListener JavaDoc;
15 import javax.management.ObjectName JavaDoc;
16 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
17 import org.jboss.remoting.InvokerLocator;
18 import org.jboss.remoting.detection.ServerInvokerMetadata;
19 import org.jboss.remoting.detection.multicast.MulticastDetector;
20 import org.jboss.remoting.network.NetworkNotification;
21 import org.jboss.remoting.network.NetworkRegistry;
22 import org.jboss.remoting.transport.Connector;
23 import org.jboss.test.remoting.TestUtil;
24 import org.w3c.dom.Document JavaDoc;
25
26 import junit.framework.TestCase;
27
28 /**
29  * Just tests that detector A sees detector B when B comes online then off.
30  * Also checks to make sure the detection message contains the proper data.
31  *
32  * @author <a HREF="mailto:telrod@e2technologies.net">Tom Elrod</a>
33  */

34 public class MetadataTestCase extends TestCase implements NotificationListener JavaDoc
35 {
36    private String JavaDoc subSystem = null;
37
38    public MetadataTestCase(String JavaDoc name)
39    {
40       super(name);
41    }
42
43    public void testDetectors() throws Exception JavaDoc
44    {
45       MulticastDetector detector1 = new MulticastDetector();
46       MulticastDetector detector2 = new MulticastDetector();
47
48       Connector connector1 = new Connector();
49       Connector connector2 = new Connector();
50
51       NetworkRegistry reg1 = setupServers(detector1, connector1);
52       // need to register with the mbean server for notifications
53
List JavaDoc mbeanServers = MBeanServerFactory.findMBeanServer(null);
54       MBeanServer JavaDoc mbeanSvr = (MBeanServer JavaDoc) mbeanServers.get(0);
55       mbeanSvr.addNotificationListener(new ObjectName JavaDoc("remoting:type=NetworkRegistry"),
56                                        this, null, null);
57
58       NetworkRegistry reg2 = setupServers(detector2, connector2);
59
60       // Need to allow heartbeat so have detection
61
Thread.currentThread().sleep(2000);
62
63       //Should now have an entry for both of the registries
64
int reg1Count = reg1.getServers().length;
65       int reg2Count = reg2.getServers().length;
66
67       // Actual junit test
68
assertTrue(reg1Count == 1 && reg2Count == 1);
69
70       if(reg1Count == 1 && reg2Count == 1)
71       {
72          System.out.println("PASSED - both registries have found the detectors.");
73       }
74       else
75       {
76          System.out.println("FAILED - registries not populated with remote detectors.");
77       }
78
79       // now check to make sure got the subsystem as expected
80
assertEquals("MOCK", subSystem);
81
82       // stop the 2nd detector, so see if 1st one detects it is missing
83
connector1.stop();
84       connector1.destroy();
85       connector1 = null;
86       connector2.stop();
87       connector2.destroy();
88       connector2 = null;
89       detector1.stop();
90       detector2.stop();
91       //connector2.stop();
92
//connector2.destroy();
93
}
94
95
96    private synchronized NetworkRegistry setupServers(MulticastDetector detector, Connector connector)
97    {
98       NetworkRegistry registry = null;
99       System.setProperty("jboss.identity", String.valueOf(System.currentTimeMillis()));
100       System.out.println("jboss.identity = " + System.getProperty("jboss.identity"));
101
102       try
103       {
104          MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
105
106          //registry = NetworkRegistry.getInstance();
107
registry = TestNetworkRegistry.createNetworkRegistry();
108          server.registerMBean(registry, new ObjectName JavaDoc("remoting:type=NetworkRegistry"));
109
110          //int port = Math.abs(new Random().nextInt(2000));
111
int port = TestUtil.getRandomPort();
112          System.out.println("port = " + port);
113
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.test.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    public void handleNotification(Notification JavaDoc notification, Object JavaDoc o)
144    {
145       System.out.println("Received notification: " + notification);
146       if(notification instanceof NetworkNotification)
147       {
148          NetworkNotification netNot = (NetworkNotification) notification;
149          ServerInvokerMetadata[] serverMetadata = netNot.getServerInvokers();
150          subSystem = serverMetadata[0].getSubSystems()[0];
151       }
152    }
153
154    private static class TestNetworkRegistry extends NetworkRegistry
155    {
156       public static NetworkRegistry createNetworkRegistry()
157       {
158          return new TestNetworkRegistry();
159       }
160    }
161
162 }
163
Popular Tags