| 1 package org.sapia.ubik.rmi.test.integration; 2 3 import java.util.Properties ; 4 5 import javax.naming.Context ; 6 import javax.naming.InitialContext ; 7 import javax.naming.NameNotFoundException ; 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 32 public class JndiTest extends TestCase{ 33 34 static final int PORT = 5152; 35 static final int WRONG_PORT = 5153; 36 static final String DOMAIN = "JndiTest"; 37 static final String WRONG_DOMAIN = "default"; 38 39 private EmbeddableJNDIServer _server; 40 41 public JndiTest(String name){ 42 super(name); 43 } 44 45 48 private void doSetUp() throws Exception { 49 _server = new EmbeddableJNDIServer(DOMAIN, PORT); 50 _server.start(false); 51 } 52 53 56 private void doTearDown() throws Exception { 57 _server.stop(); 58 Hub.shutdown(10000); 59 } 60 61 public void testAll() throws Exception { 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 { 77 InitialContext ctx = new InitialContext (props(WRONG_PORT, DOMAIN)); 78 ctx.close(); 79 } 80 81 public void doTestBind() throws Exception { 82 InitialContext ctx = new InitialContext (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 { 89 InitialContext ctx = new InitialContext (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 ("Name should not have been found"); 95 }catch(NameNotFoundException e){ 96 } 98 99 ctx.close(); 100 } 101 102 public void doTestDiscoveryHelper() throws Exception { 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 ctx = new InitialContext (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 props(int port, String domain){ 121 Properties props = new Properties (); 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 135 public void onServiceDiscovered(ServiceDiscoveryEvent evt) { 136 serviceDiscovered = true; 137 } 138 139 142 public void onJndiDiscovered(Context ctx) { 143 jndiDiscovered = true; 144 } 145 146 147 } 148 } 149 | Popular Tags |