1 45 package org.exolab.jms.net.invoke; 46 47 import java.rmi.RemoteException ; 48 import java.util.Map ; 49 import java.util.HashMap ; 50 import java.io.IOException ; 51 52 import org.apache.commons.logging.Log; 53 import org.apache.commons.logging.LogFactory; 54 import junit.framework.AssertionFailedError; 55 import junit.framework.Protectable; 56 import junit.framework.TestCase; 57 58 import org.exolab.jms.net.ExceptionService; 59 import org.exolab.jms.net.ExceptionServiceImpl; 60 import org.exolab.jms.net.orb.ORB; 61 import org.exolab.jms.net.orb.ORBFactory; 62 import org.exolab.jms.net.proxy.Proxy; 63 import org.exolab.jms.net.proxy.RemoteInvocationException; 64 import org.exolab.jms.net.registry.Registry; 65 import org.exolab.jms.net.util.SSLUtil; 66 67 68 74 public abstract class ExceptionTestCase extends TestCase { 75 76 79 private final String _uri; 80 81 84 private final String _routeURI; 85 86 90 private final Map _connectionProps; 91 92 96 private final Map _acceptorProps; 97 98 101 private ORB _orb; 102 103 106 private ExceptionService _service; 107 108 111 private static final Log _log = LogFactory.getLog(ExceptionTestCase.class); 112 113 116 private static final String EXCEPTION_SERVICE = "exception"; 117 118 119 125 public ExceptionTestCase(String name, String uri) { 126 this(name, uri, null, null); 127 } 128 129 136 public ExceptionTestCase(String name, String uri, String routeURI) { 137 this(name, uri, routeURI, null, null); 138 } 139 140 147 public ExceptionTestCase(String name, String uri, Map properties) { 148 this(name, uri, null, properties, properties); 149 } 150 151 159 public ExceptionTestCase(String name, String uri, String routeURI, 160 Map properties) { 161 this(name, uri, routeURI, properties, properties); 162 } 163 164 173 public ExceptionTestCase(String name, String uri, String routeURI, 174 Map connectionProps, Map acceptorProps) { 175 super(name); 176 _uri = uri; 177 _routeURI = routeURI; 178 _connectionProps = connectionProps; 179 _acceptorProps = acceptorProps; 180 } 181 182 188 public void testDeclaredThrowable() throws Exception { 189 Protectable protectable = new Protectable() { 190 public void protect() throws Throwable { 191 _service.throwThrowable(); 192 } 193 }; 194 checkException(protectable, Throwable .class, null); 195 } 196 197 203 public void testDeclaredException() throws Exception { 204 Protectable protectable = new Protectable() { 205 public void protect() throws Throwable { 206 _service.throwException(); 207 } 208 }; 209 checkException(protectable, Exception .class, null); 210 } 211 212 217 public void testDeclaredError() throws Exception { 218 Protectable protectable = new Protectable() { 219 public void protect() throws Throwable { 220 _service.throwError(); 221 } 222 }; 223 checkException(protectable, Error .class, null); 224 } 225 226 232 public void testUndeclaredError() throws Exception { 233 Protectable protectable = new Protectable() { 234 public void protect() throws Throwable { 235 _service.throwUndeclaredError(); 236 } 237 }; 238 checkException(protectable, RemoteInvocationException.class, 239 Error .class); 240 } 241 242 248 public void testDeclaredRuntimeException() throws Exception { 249 Protectable protectable = new Protectable() { 250 public void protect() throws Throwable { 251 _service.throwRuntimeException(); 252 } 253 }; 254 checkException(protectable, RuntimeException .class, null); 255 } 256 257 263 public void testUndeclaredRuntimeException() throws Exception { 264 Protectable protectable = new Protectable() { 265 public void protect() throws Throwable { 266 _service.throwUndeclaredRuntimeException(); 267 } 268 }; 269 checkException(protectable, RemoteInvocationException.class, 270 RuntimeException .class); 271 } 272 273 279 public void testDeclaredRemoteException() throws Exception { 280 Protectable protectable = new Protectable() { 281 public void protect() throws Throwable { 282 _service.throwRemoteException(); 283 } 284 }; 285 checkException(protectable, RemoteException .class, null); 286 } 287 288 295 public void testUndeclaredError2() throws Exception { 296 Protectable protectable = new Protectable() { 297 public void protect() throws Throwable { 298 _service.throwUndeclaredError2(); 299 } 300 }; 301 checkException(protectable, RemoteException .class, Error .class); 302 } 303 304 311 public void testUndeclaredRuntimeException2() throws Exception { 312 Protectable protectable = new Protectable() { 313 public void protect() throws Throwable { 314 _service.throwUndeclaredRuntimeException2(); 315 } 316 }; 317 checkException(protectable, RemoteException .class, 318 RuntimeException .class); 319 } 320 321 327 public void testUndeclaredRemoteInvocationException() throws Exception { 328 Protectable protectable = new Protectable() { 329 public void protect() throws Throwable { 330 _service.throwUndeclaredRemoteInvocationException(); 331 } 332 }; 333 checkException(protectable, RemoteInvocationException.class, null); 334 } 335 336 344 protected Map getConnectionProperties() throws IOException { 345 Map properties = new HashMap (); 346 properties.put(ORB.PROVIDER_URI, getServerURI()); 347 if (_connectionProps != null) { 348 properties.putAll(_connectionProps); 349 } 350 return properties; 351 } 352 353 360 protected Map getAcceptorProperties() throws Exception { 361 Map properties = new HashMap (); 362 properties.put(ORB.PROVIDER_URI, _uri); 363 if (_acceptorProps != null) { 364 properties.putAll(_acceptorProps); 365 } 366 return properties; 367 } 368 369 374 protected String getServerURI() { 375 return (_routeURI != null) ? _routeURI : _uri; 376 } 377 378 383 protected void setUp() throws Exception { 384 _log.debug("setUp() [test=" + getName() + ", uri=" + _uri + "]"); 385 _orb = ORBFactory.createORB(getAcceptorProperties()); 386 if (_routeURI != null) { 387 _orb.addRoute(_uri, _routeURI); 388 } 389 Registry serverRegistry = _orb.getRegistry(); 390 391 Proxy proxy = _orb.exportObject(new ExceptionServiceImpl()); 392 serverRegistry.bind(EXCEPTION_SERVICE, proxy); 393 394 Registry clientRegistry = _orb.getRegistry(getConnectionProperties()); 396 _service = (ExceptionService) clientRegistry.lookup(EXCEPTION_SERVICE); 397 } 398 399 404 protected void tearDown() throws Exception { 405 _log.debug("tearDown() [test=" + getName() + ", uri=" + _uri + "]"); 406 _orb.shutdown(); 407 408 SSLUtil.clearProperties(); 410 } 411 412 420 private void checkException(Protectable protectable, Class expected, 421 Class nested) { 422 try { 423 protectable.protect(); 424 fail("Expected exception of type=" + expected.getName() 425 + " to be thrown"); 426 } catch (RemoteInvocationException exception) { 427 checkExceptionType(exception, expected); 428 if (nested != null) { 429 checkNestedExceptionType(exception.getTargetException(), 430 nested); 431 } 432 } catch (RemoteException exception) { 433 checkExceptionType(exception, expected); 434 if (nested != null) { 435 checkNestedExceptionType(exception.detail, nested); 436 } 437 } catch (AssertionFailedError error) { 438 throw error; 439 } catch (Throwable throwable) { 440 checkExceptionType(throwable, expected); 441 } 442 } 443 444 450 private void checkExceptionType(Throwable exception, Class expected) { 451 Class actual = exception.getClass(); 452 if (!actual.equals(expected)) { 453 fail("Expected exception of type=" + expected.getName() 454 + " to be thrown, but got type=" + actual.getName() 455 + ", message=" + exception.getMessage()); 456 } 457 } 458 459 465 private void checkNestedExceptionType(Throwable exception, 466 Class expected) { 467 Class actual = exception.getClass(); 468 if (!actual.equals(expected)) { 469 fail("Expected nested exception of type=" + expected.getName() 470 + " but got type=" + actual.getClass().getName() 471 + ", message=" + exception.getMessage()); 472 } 473 } 474 475 } 476 | Popular Tags |