KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > remoting > detection > jndi > JNDIDetectorTest2


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.jndi;
8
9 import java.net.InetAddress JavaDoc;
10 import javax.management.MBeanServer JavaDoc;
11 import javax.management.MBeanServerFactory JavaDoc;
12 import javax.management.ObjectName JavaDoc;
13 import org.apache.log4j.Level;
14 import org.jboss.jrunit.extensions.ServerTestCase;
15 import org.jboss.remoting.InvokerLocator;
16 import org.jboss.remoting.detection.jndi.JNDIDetector;
17 import org.jboss.remoting.network.NetworkInstance;
18 import org.jboss.remoting.network.NetworkRegistry;
19 import org.jboss.remoting.transport.Connector;
20 import org.jboss.test.remoting.TestUtil;
21 import org.jboss.test.remoting.transport.mock.MockServerInvocationHandler;
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 JNDIDetectorTest2 extends ServerTestCase
29 {
30    private int detectorPort = 1099;
31    private String JavaDoc contextFactory = "org.jnp.interfaces.NamingContextFactory";
32    private String JavaDoc urlPackage = "org.jboss.naming:org.jnp.interfaces";
33
34    private JNDIDetector detector;
35    private Connector connector;
36    private NetworkRegistry registry;
37
38    public void setUp() throws Exception JavaDoc
39    {
40       String JavaDoc detectorHost = InetAddress.getLocalHost().getHostName();
41
42       detector = new JNDIDetector();
43       detector.setCleanDetectionNumber(2);
44
45       System.setProperty("jboss.identity", String.valueOf(System.currentTimeMillis()));
46       System.out.println("jboss.identity = " + System.getProperty("jboss.identity"));
47
48       MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
49
50       //registry = NetworkRegistry.getInstance();
51
registry = JNDIDetectorTest2.TestNetworkRegistry.createNetworkRegistry();
52       server.registerMBean(registry, new ObjectName JavaDoc("remoting:type=NetworkRegistry"));
53
54       int port = TestUtil.getRandomPort();
55       System.out.println("port = " + port);
56
57       InvokerLocator locator = new InvokerLocator("socket://localhost:" + port);
58
59       System.out.println("Starting remoting server with locator uri of: " + locator.getLocatorURI());
60       connector = new Connector();
61       connector.setInvokerLocator(locator.getLocatorURI());
62       connector.create();
63
64       MockServerInvocationHandler handler = new MockServerInvocationHandler();
65       connector.addInvocationHandler("mock", handler);
66
67       ObjectName JavaDoc obj = new ObjectName JavaDoc("jboss.remoting:type=Connector,transport=" + locator.getProtocol());
68       server.registerMBean(connector, obj);
69       //connector.create();
70
connector.start();
71
72       //Need to set new domain for identity
73
server.registerMBean(detector, new ObjectName JavaDoc("remoting:type=JNDIDetector"));
74
75       // set config info for detector and start it.
76
detector.setPort(detectorPort);
77       detector.setHost(detectorHost);
78       detector.setContextFactory(contextFactory);
79       detector.setURLPackage(urlPackage);
80       detector.start();
81
82    }
83
84    public void testDetector2() throws Exception JavaDoc
85    {
86       Thread.currentThread().sleep(5000);
87
88       NetworkInstance[] server = registry.getServers();
89       if(server != null && server.length > 0)
90       {
91          System.out.println("PASS - got more than zero servers detected.");
92          assertTrue("Detected server.", true);
93       }
94       else
95       {
96          System.out.println("FAIL - did not detect any other server.");
97          assertTrue("Did not detect any servers.", false);
98       }
99
100       if(connector != null)
101       {
102          connector.stop();
103          connector.destroy();
104          connector = null;
105       }
106       if(detector != null)
107       {
108          detector.stop();
109          detector = null;
110       }
111    }
112
113    public void tearDown() throws Exception JavaDoc
114    {
115       if(connector != null)
116       {
117          connector.stop();
118          connector.destroy();
119          connector = null;
120       }
121       if(detector != null)
122       {
123          detector.stop();
124          detector = null;
125       }
126
127    }
128
129    public static void main(String JavaDoc[] args)
130    {
131       org.apache.log4j.BasicConfigurator.configure();
132       org.apache.log4j.Category.getRoot().setLevel(Level.DEBUG);
133
134       try
135       {
136          JNDIDetectorTest2 test = new JNDIDetectorTest2();
137          test.setUp();
138          test.testDetector2();
139          test.tearDown();
140       }
141       catch(Exception JavaDoc e)
142       {
143          e.printStackTrace();
144       }
145
146    }
147
148    private static class TestNetworkRegistry extends NetworkRegistry
149    {
150       public static NetworkRegistry createNetworkRegistry()
151       {
152          return new JNDIDetectorTest2.TestNetworkRegistry();
153       }
154    }
155
156 }
Popular Tags