KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > rmi > test > integration > JndiTest


1 package org.sapia.ubik.rmi.test.integration;
2
3 import java.util.Properties JavaDoc;
4
5 import javax.naming.Context JavaDoc;
6 import javax.naming.InitialContext JavaDoc;
7 import javax.naming.NameNotFoundException JavaDoc;
8
9 import org.sapia.ubik.rmi.examples.Foo;
10 import org.sapia.ubik.rmi.examples.UbikFoo;
11 import org.sapia.ubik.rmi.naming.remote.EmbeddableJNDIServer;
12 import org.sapia.ubik.rmi.naming.remote.RemoteInitialContextFactory;
13 import org.sapia.ubik.rmi.naming.remote.discovery.DiscoveryHelper;
14 import org.sapia.ubik.rmi.naming.remote.discovery.JndiDiscoListener;
15 import org.sapia.ubik.rmi.naming.remote.discovery.ServiceDiscoListener;
16 import org.sapia.ubik.rmi.naming.remote.discovery.ServiceDiscoveryEvent;
17 import org.sapia.ubik.rmi.server.Hub;
18
19 import junit.framework.TestCase;
20 import junit.framework.TestResult;
21 import junit.framework.TestSuite;
22
23 /**
24  * @author Yanick Duchesne
25  *
26  * <dl>
27  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2004 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
28  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
29  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
30  * </dl>
31  */

32 public class JndiTest extends TestCase{
33     
34   static final int PORT = 5152;
35     static final int WRONG_PORT = 5153;
36   static final String JavaDoc DOMAIN = "JndiTest";
37     static final String JavaDoc WRONG_DOMAIN = "default";
38   
39   private EmbeddableJNDIServer _server;
40     
41     public JndiTest(String JavaDoc name){
42         super(name);
43     }
44     
45     /**
46    * @see junit.framework.TestCase#setUp()
47    */

48   private void doSetUp() throws Exception JavaDoc {
49         _server = new EmbeddableJNDIServer(DOMAIN, PORT);
50         _server.start(false);
51     }
52     
53     /**
54    * @see junit.framework.TestCase#tearDown()
55    */

56   private void doTearDown() throws Exception JavaDoc {
57     _server.stop();
58         Hub.shutdown(10000);
59   }
60   
61   public void testAll() throws Exception JavaDoc{
62     doSetUp();
63         TestSuite suite = new TestSuite();
64         suite.addTest(new JndiTest("doTestDiscovery"));
65     suite.addTest(new JndiTest("doTestBind"));
66         suite.addTest(new JndiTest("doTestLookup"));
67         suite.addTest(new JndiTest("doTestDiscoveryHelper"));
68         TestResult res = super.createResult();
69         TestListenerImpl listener = new TestListenerImpl();
70         res.addListener(listener);
71         suite.run(res);
72         doTearDown();
73         listener.throwExc();
74   }
75
76     public void doTestDiscovery() throws Exception JavaDoc{
77         InitialContext JavaDoc ctx = new InitialContext JavaDoc(props(WRONG_PORT, DOMAIN));
78         ctx.close();
79     }
80     
81     public void doTestBind() throws Exception JavaDoc{
82         InitialContext JavaDoc ctx = new InitialContext JavaDoc(props(PORT, DOMAIN));
83         Foo f = new UbikFoo();
84         ctx.bind("intg/test/foo", f);
85         ctx.close();
86     }
87     
88     public void doTestLookup() throws Exception JavaDoc{
89         InitialContext JavaDoc ctx = new InitialContext JavaDoc(props(PORT, DOMAIN));
90         Foo f = (Foo)ctx.lookup("intg/test/foo");
91         
92         try{
93             ctx.lookup("intg/test/none");
94             throw new Exception JavaDoc("Name should not have been found");
95         }catch(NameNotFoundException JavaDoc e){
96             //ok
97
}
98
99         ctx.close();
100     }
101     
102     public void doTestDiscoveryHelper() throws Exception JavaDoc{
103         DiscoveryHelper helper = new DiscoveryHelper(DOMAIN);
104         DiscoListenerImpl listener = new DiscoListenerImpl();
105         helper.addServiceDiscoListener(listener);
106         helper.addJndiDiscoListener(listener);
107         EmbeddableJNDIServer server = new EmbeddableJNDIServer(DOMAIN, WRONG_PORT);
108         server.start(true);
109         InitialContext JavaDoc ctx = new InitialContext JavaDoc(props(PORT, DOMAIN));
110         Foo f = new UbikFoo();
111         ctx.rebind("intg/test/foo", f);
112         server.stop();
113         ctx.close();
114         Thread.sleep(2000);
115         super.assertTrue("Service not discovered", listener.serviceDiscovered);
116         super.assertTrue("JNDI not discovered", listener.jndiDiscovered);
117
118     }
119
120     private static Properties JavaDoc props(int port, String JavaDoc domain){
121         Properties JavaDoc props = new Properties JavaDoc();
122         props.setProperty(Context.INITIAL_CONTEXT_FACTORY, RemoteInitialContextFactory.class.getName());
123         props.setProperty(Context.PROVIDER_URL, "ubik://localhost:" + port);
124         props.setProperty(RemoteInitialContextFactory.UBIK_DOMAIN_NAME, domain);
125         return props;
126     }
127     
128     public static class DiscoListenerImpl
129       implements ServiceDiscoListener, JndiDiscoListener{
130         
131         boolean serviceDiscovered, jndiDiscovered;
132         /**
133      * @see org.sapia.ubik.rmi.naming.remote.discovery.ServiceDiscoListener#onServiceDiscovered(org.sapia.ubik.rmi.naming.remote.discovery.ServiceDiscoveryEvent)
134      */

135     public void onServiceDiscovered(ServiceDiscoveryEvent evt) {
136             serviceDiscovered = true;
137     }
138     
139     /**
140      * @see org.sapia.ubik.rmi.naming.remote.discovery.JndiDiscoListener#onJndiDiscovered(javax.naming.Context)
141      */

142     public void onJndiDiscovered(Context JavaDoc ctx) {
143             jndiDiscovered = true;
144     }
145
146     
147     }
148 }
149
Popular Tags