1 45 package org.exolab.jms.net.invoke; 46 47 import java.rmi.server.ExportException ; 48 import java.rmi.NoSuchObjectException ; 49 import java.util.Map ; 50 51 import org.exolab.jms.common.security.BasicPrincipal; 52 import org.exolab.jms.net.EchoService; 53 import org.exolab.jms.net.EchoServiceImpl; 54 import org.exolab.jms.net.Callback; 55 import org.exolab.jms.net.CallbackService; 56 import org.exolab.jms.net.CallbackServiceImpl; 57 import org.exolab.jms.net.connector.Authenticator; 58 import org.exolab.jms.net.connector.TestAuthenticator; 59 import org.exolab.jms.net.orb.ORB; 60 import org.exolab.jms.net.proxy.Proxy; 61 import org.exolab.jms.net.proxy.RemoteInvocationException; 62 import org.exolab.jms.net.registry.Registry; 63 64 65 71 public abstract class ExportTestCase extends ORBTestCase { 72 73 79 public ExportTestCase(String name, String uri) { 80 super(name, uri); 81 } 82 83 90 public ExportTestCase(String name, String uri, Map properties) { 91 super(name, uri, properties); 92 } 93 94 101 public ExportTestCase(String name, String uri, String routeURI) { 102 super(name, uri, routeURI); 103 } 104 105 114 public ExportTestCase(String name, String uri, String routeURI, 115 Map connectionProps, Map acceptorProps) { 116 super(name, uri, routeURI, connectionProps, acceptorProps); 117 } 118 119 125 public void testExportObject() throws Exception { 126 checkExportObject(null); 127 } 128 129 135 public void testExportObjectWithAuth() throws Exception { 136 BasicPrincipal principal = new BasicPrincipal("first", "secret"); 137 checkExportObject(principal); 138 } 139 140 146 public void testExportObjectURI() throws Exception { 147 checkExportObjectURI(null); 148 } 149 150 156 public void testExportObjectURIWithAuth() throws Exception { 157 BasicPrincipal principal = new BasicPrincipal("second", "secret"); 158 checkExportObjectURI(principal); 159 } 160 161 167 public void testExportObjectToNoCaller() throws Exception { 168 ORB orb = getORB(); 169 EchoServiceImpl impl = new EchoServiceImpl(); 170 try { 171 orb.exportObjectTo(impl); 172 fail("Expected exportObjectTo() to fail with ExportException"); 173 } catch (ExportException expected) { 174 } 176 } 177 178 184 public void testExportObjectTo() throws Exception { 185 checkExportObjectTo(null); 186 } 187 188 194 public void testExportObjectToWithAuth() throws Exception { 195 BasicPrincipal principal = new BasicPrincipal("third", "secret"); 196 checkExportObjectTo(principal); 197 } 198 199 205 public void testExportObjectToURI() throws Exception { 206 checkExportObjectToURI(null); 207 } 208 209 215 public void testExportObjectToURIWithAuth() throws Exception { 216 BasicPrincipal principal = new BasicPrincipal("fourth", "secret"); 217 checkExportObjectToURI(principal); 218 } 219 220 228 private void checkExportObject(BasicPrincipal principal) 229 throws Exception { 230 Authenticator authenticator = new TestAuthenticator(principal); 231 ORB orb = createORB(authenticator); 232 EchoServiceImpl impl = new EchoServiceImpl(); 233 Proxy proxy = orb.exportObject(impl); 234 orb.getRegistry().bind("service", proxy); 235 236 Registry registry = getRegistry(principal); 237 EchoService service = (EchoService) registry.lookup("service"); 238 239 assertTrue(service.echoBoolean(true)); 240 241 checkUnexportObject(orb, impl, service); 242 } 243 244 252 private void checkExportObjectURI(BasicPrincipal principal) 253 throws Exception { 254 Authenticator authenticator = new TestAuthenticator(principal); 255 ORB orb = createORB(authenticator); 256 EchoServiceImpl impl = new EchoServiceImpl(); 257 Proxy proxy = orb.exportObject(impl, getExportURI()); 258 orb.getRegistry().bind("service", proxy); 259 260 Registry registry = getRegistry(principal); 261 EchoService service = (EchoService) registry.lookup("service"); 262 263 assertTrue(service.echoBoolean(true)); 264 265 checkUnexportObject(orb, impl, service); 266 } 267 268 276 private void checkExportObjectTo(BasicPrincipal principal) 277 throws Exception { 278 Authenticator authenticator = new TestAuthenticator(principal); 279 ORB orb = createORB(authenticator); 280 EchoServiceImpl echoImpl = new EchoServiceImpl(); 281 282 ExportServiceImpl exporterImpl = new ExportServiceImpl(echoImpl, orb); 283 Proxy proxy = orb.exportObject(exporterImpl); 284 orb.getRegistry().bind("service", proxy); 285 286 Registry registry = getRegistry(principal); 287 ExportService exporter = (ExportService) registry.lookup("service"); 288 EchoService echoer = (EchoService) exporter.exportObjectTo(); 289 290 assertTrue(echoer.echoBoolean(true)); 291 292 checkUnexportObject(orb, echoImpl, echoer); 293 } 294 295 303 private void checkExportObjectToURI(BasicPrincipal principal) 304 throws Exception { 305 final int count = 10; 306 String user = null; 307 String password = null; 308 309 if (principal != null) { 310 user = principal.getName(); 311 password = principal.getPassword(); 312 } 313 314 Authenticator authenticator = new TestAuthenticator(principal); 315 ORB orb = createORB(authenticator); 316 317 CallbackService serviceImpl = new CallbackServiceImpl(); 318 Proxy proxy = orb.exportObject(serviceImpl); 319 orb.getRegistry().bind("service", proxy); 320 321 ORB client = getClientORB(); 322 Registry registry = getRegistry(principal); 323 CallbackService service = (CallbackService) registry.lookup("service"); 324 325 LoggingCallback callback = new LoggingCallback(); 326 Callback callbackProxy = (Callback) client.exportObjectTo( 327 callback, getServerURI(), user, password); 328 service.addCallback(callbackProxy); 329 330 for (int i = 0; i < count; ++i) { 331 service.invoke(new Integer (i)); 332 } 333 334 Integer [] objects = (Integer []) callback.getObjects().toArray( 335 new Integer [0]); 336 assertEquals(count, objects.length); 337 for (int i = 0; i < count; ++i) { 338 assertEquals(i, objects[i].intValue()); 339 } 340 341 client.unexportObject(callback); 342 try { 343 service.invoke(new Integer (0)); 344 } catch (RemoteInvocationException expected) { 345 assertTrue(expected.getTargetException() 347 instanceof NoSuchObjectException ); 348 } 349 350 try { 351 orb.unexportObject(client); 352 fail("Expected NoSuchObjectException to be thrown"); 353 } catch (NoSuchObjectException expected) { 354 } 356 } 357 358 368 private void checkUnexportObject(ORB orb, EchoServiceImpl service, 369 EchoService proxy) throws Exception { 370 orb.unexportObject(service); 371 372 try { 373 proxy.echoBoolean(true); 374 fail("Managed to invoke method on unexported object"); 375 } catch (RemoteInvocationException expected) { 376 assertTrue(expected.getTargetException() 378 instanceof NoSuchObjectException ); 379 } 380 381 try { 382 orb.unexportObject(service); 383 fail("Expected NoSuchObjectException to be thrown"); 384 } catch (NoSuchObjectException expected) { 385 } 387 } 388 389 } 390 | Popular Tags |