1 45 package org.exolab.jms.net.registry; 46 47 import java.rmi.AccessException ; 48 import java.rmi.AlreadyBoundException ; 49 import java.rmi.NotBoundException ; 50 import java.util.Map ; 51 52 import org.apache.commons.logging.Log; 53 import org.apache.commons.logging.LogFactory; 54 55 import org.exolab.jms.net.EchoService; 56 import org.exolab.jms.net.EchoServiceImpl; 57 import org.exolab.jms.net.connector.Authenticator; 58 import org.exolab.jms.net.connector.TestAuthenticator; 59 import org.exolab.jms.net.invoke.ORBTestCase; 60 import org.exolab.jms.net.orb.ORB; 61 import org.exolab.jms.net.proxy.Proxy; 62 import org.exolab.jms.common.security.BasicPrincipal; 63 64 65 71 public abstract class RegistryTestCase extends ORBTestCase { 72 73 76 private Proxy _service; 77 78 81 private static final Log _log = LogFactory.getLog(RegistryTestCase.class); 82 83 86 private static final String ECHO_SERVICE = "echo"; 87 88 89 95 public RegistryTestCase(String name, String uri) { 96 super(name, uri); 97 } 98 99 106 public RegistryTestCase(String name, String uri, Map properties) { 107 super(name, uri, properties); 108 } 109 110 117 public RegistryTestCase(String name, String uri, String routeURI) { 118 super(name, uri, routeURI); 119 } 120 121 129 public RegistryTestCase(String name, String uri, String routeURI, 130 Map properties) { 131 super(name, uri, routeURI, properties); 132 } 133 134 139 public void testGetRegistry() throws Exception { 140 Registry registry = getRegistry(); 141 assertNotNull(registry); 142 assertTrue(registry instanceof Proxy); 143 144 assertTrue(!(registry instanceof LocalRegistry)); 147 } 148 149 154 public void testGetRegistryWithAuth() throws Exception { 155 getORB().shutdown(); 157 BasicPrincipal principal = new BasicPrincipal("user", "password"); 159 Authenticator authenticator = new TestAuthenticator(principal); 160 ORB orb = createORB(authenticator); 161 162 EchoService service = new EchoServiceImpl(); 164 _service = orb.exportObject(service); 165 assertTrue(_service instanceof EchoService); 166 orb.getRegistry().bind(ECHO_SERVICE, _service); 167 168 Registry registry = getRegistry(principal); 170 assertNotNull(registry); 171 Proxy proxy = registry.lookup(ECHO_SERVICE); 172 assertTrue(proxy instanceof EchoService); 173 174 try { 176 registry = getRegistry(); 177 fail("Expected AccessException to be thrown"); 178 } catch (AccessException exception) { 179 } catch (Exception exception) { 181 fail("Expected AccessException to be thrown, but got " + exception); 182 } 183 184 185 } 186 187 192 public void testLookup() throws Exception { 193 Registry registry = getRegistry(); 194 checkLookup(registry); 195 } 196 197 203 public void testLocalLookup() throws Exception { 204 ORB orb = getORB(); 205 Registry registry = orb.getRegistry(); 206 checkLookup(registry); 207 } 208 209 214 public void testNotBound() throws Exception { 215 Registry registry = getRegistry(); 216 checkNotBound(registry); 217 } 218 219 225 public void testLocalNotBound() throws Exception { 226 ORB orb = getORB(); 227 Registry registry = orb.getRegistry(); 228 checkNotBound(registry); 229 } 230 231 237 public void testAlreadyBound() throws Exception { 238 Registry registry = getRegistry(); 239 checkAlreadyBound(registry); 240 } 241 242 248 public void testLocalAlreadyBound() throws Exception { 249 ORB orb = getORB(); 250 Registry registry = orb.getRegistry(); 251 checkAlreadyBound(registry); 252 } 253 254 259 public void testUnbind() throws Exception { 260 Registry registry = getRegistry(); 261 checkUnbind(registry); 262 } 263 264 269 public void testLocalUnbind() throws Exception { 270 ORB orb = getORB(); 271 Registry registry = orb.getRegistry(); 272 checkUnbind(registry); 273 } 274 275 281 public void testReadOnly() throws Exception { 282 ORB orb = getORB(); 283 LocalRegistry local = orb.getRegistry(); 284 Registry remote = getRegistry(); 285 286 assertFalse(local.getReadOnly()); 288 289 local.setReadOnly(true); 291 assertTrue(local.getReadOnly()); 292 293 try { 295 remote.bind(ECHO_SERVICE, _service); 296 fail("Expected bind to fail with exception " 297 + AccessException .class.getName()); 298 } catch (AccessException expected) { 299 } 301 302 local.bind(ECHO_SERVICE, _service); 305 Proxy proxy = remote.lookup(ECHO_SERVICE); 306 assertNotNull(proxy); 307 assertTrue(proxy instanceof EchoService); 308 309 try { 311 remote.unbind(ECHO_SERVICE); 312 fail("Expected unbind to fail with exception " 313 + AccessException .class.getName()); 314 } catch (AccessException expected) { 315 } 317 318 local.setReadOnly(false); 321 remote.unbind(ECHO_SERVICE); 322 remote.bind(ECHO_SERVICE, _service); 323 proxy = remote.lookup(ECHO_SERVICE); 324 assertNotNull(proxy); 325 assertTrue(proxy instanceof EchoService); 326 } 327 328 333 protected void setUp() throws Exception { 334 super.setUp(); 335 ORB orb = getORB(); 337 EchoService service = new EchoServiceImpl(); 338 _service = orb.exportObject(service); 339 assertTrue(_service instanceof EchoService); 340 341 orb.getRegistry(); 343 } 344 345 351 private void checkLookup(Registry registry) throws Exception { 352 registry.bind(ECHO_SERVICE, _service); 354 Proxy proxy = registry.lookup(ECHO_SERVICE); 355 assertNotNull(proxy); 356 assertTrue(proxy instanceof EchoService); 357 } 358 359 365 private void checkNotBound(Registry registry) throws Exception { 366 try { 367 _log.debug("Looking up unbound object"); 368 registry.lookup("Foo"); 369 fail("Expected lookup of unbound object to throw " 370 + NotBoundException .class.getName()); 371 } catch (NotBoundException expected) { 372 } catch (Exception exception) { 374 fail("Expected lookup of unbound object to throw " 375 + NotBoundException .class.getName() + ", but threw " 376 + exception.getClass().getName() + ": " + exception); 377 } 378 } 379 380 387 private void checkAlreadyBound(Registry registry) throws Exception { 388 registry.bind(ECHO_SERVICE, _service); 390 391 try { 392 registry.bind(ECHO_SERVICE, _service); 393 fail("Expected attempt to bind to existing name to throw " 394 + AlreadyBoundException .class.getName()); 395 } catch (AlreadyBoundException ignore) { 396 } catch (Exception exception) { 398 fail("Expected attempt to bind to existing name to throw " 399 + AlreadyBoundException .class.getName() + ", but threw " 400 + exception.getClass().getName() + ": " + exception); 401 } 402 } 403 404 410 private void checkUnbind(Registry registry) throws Exception { 411 registry.bind(ECHO_SERVICE, _service); 413 414 registry.unbind(ECHO_SERVICE); 416 try { 417 registry.lookup(ECHO_SERVICE); 418 fail("unbind() failed. Expected lookup of unbound object to throw " 419 + NotBoundException .class.getName()); 420 } catch (NotBoundException ignore) { 421 } catch (Exception exception) { 423 fail("unbind() failed. Expected lookup of unbound object to throw " 424 + NotBoundException .class.getName() + ", but threw " 425 + exception.getClass().getName() + ": " + exception); 426 } 427 } 428 429 } 430 | Popular Tags |