1 16 17 package org.springframework.remoting.rmi; 18 19 import java.lang.reflect.Constructor ; 20 import java.rmi.ConnectException ; 21 import java.rmi.ConnectIOException ; 22 import java.rmi.MarshalException ; 23 import java.rmi.NoSuchObjectException ; 24 import java.rmi.Remote ; 25 import java.rmi.RemoteException ; 26 import java.rmi.StubNotFoundException ; 27 import java.rmi.UnknownHostException ; 28 import java.rmi.UnmarshalException ; 29 import java.util.ArrayList ; 30 31 import junit.framework.TestCase; 32 import org.aopalliance.intercept.MethodInvocation; 33 34 import org.springframework.aop.framework.ReflectiveMethodInvocation; 35 import org.springframework.remoting.RemoteAccessException; 36 import org.springframework.remoting.RemoteConnectFailureException; 37 import org.springframework.remoting.support.RemoteInvocation; 38 39 43 public class RmiSupportTests extends TestCase { 44 45 public void testRmiProxyFactoryBean() throws Exception { 46 CountingRmiProxyFactoryBean factory = new CountingRmiProxyFactoryBean(); 47 factory.setServiceInterface(IRemoteBean.class); 48 factory.setServiceUrl("rmi://localhost:1090/test"); 49 factory.afterPropertiesSet(); 50 assertTrue("Correct singleton value", factory.isSingleton()); 51 assertTrue(factory.getObject() instanceof IRemoteBean); 52 IRemoteBean proxy = (IRemoteBean) factory.getObject(); 53 proxy.setName("myName"); 54 assertEquals(RemoteBean.name, "myName"); 55 assertEquals(1, factory.counter); 56 } 57 58 public void testRmiProxyFactoryBeanWithRemoteException() throws Exception { 59 doTestRmiProxyFactoryBeanWithException(RemoteException .class); 60 } 61 62 public void testRmiProxyFactoryBeanWithConnectException() throws Exception { 63 doTestRmiProxyFactoryBeanWithException(ConnectException .class); 64 } 65 66 public void testRmiProxyFactoryBeanWithConnectIOException() throws Exception { 67 doTestRmiProxyFactoryBeanWithException(ConnectIOException .class); 68 } 69 70 public void testRmiProxyFactoryBeanWithUnknownHostException() throws Exception { 71 doTestRmiProxyFactoryBeanWithException(UnknownHostException .class); 72 } 73 74 public void testRmiProxyFactoryBeanWithNoSuchObjectException() throws Exception { 75 doTestRmiProxyFactoryBeanWithException(NoSuchObjectException .class); 76 } 77 78 public void testRmiProxyFactoryBeanWithStubNotFoundException() throws Exception { 79 doTestRmiProxyFactoryBeanWithException(StubNotFoundException .class); 80 } 81 82 public void testRmiProxyFactoryBeanWithMarshalException() throws Exception { 83 doTestRmiProxyFactoryBeanWithException(MarshalException .class); 84 } 85 86 public void testRmiProxyFactoryBeanWithUnmarshalException() throws Exception { 87 doTestRmiProxyFactoryBeanWithException(UnmarshalException .class); 88 } 89 90 private void doTestRmiProxyFactoryBeanWithException(Class exceptionClass) throws Exception { 91 CountingRmiProxyFactoryBean factory = new CountingRmiProxyFactoryBean(); 92 factory.setServiceInterface(IRemoteBean.class); 93 factory.setServiceUrl("rmi://localhost:1090/test"); 94 factory.afterPropertiesSet(); 95 assertTrue(factory.getObject() instanceof IRemoteBean); 96 IRemoteBean proxy = (IRemoteBean) factory.getObject(); 97 try { 98 proxy.setName(exceptionClass.getName()); 99 fail("Should have thrown " + exceptionClass.getName()); 100 } 101 catch (Exception ex) { 102 if (exceptionClass.isInstance(ex)) { 103 } 105 else { 106 throw ex; 107 } 108 } 109 assertEquals(1, factory.counter); 110 } 111 112 public void testRmiProxyFactoryBeanWithConnectExceptionAndRefresh() throws Exception { 113 doTestRmiProxyFactoryBeanWithExceptionAndRefresh(ConnectException .class); 114 } 115 116 public void testRmiProxyFactoryBeanWithConnectIOExceptionAndRefresh() throws Exception { 117 doTestRmiProxyFactoryBeanWithExceptionAndRefresh(ConnectIOException .class); 118 } 119 120 public void testRmiProxyFactoryBeanWithUnknownHostExceptionAndRefresh() throws Exception { 121 doTestRmiProxyFactoryBeanWithExceptionAndRefresh(UnknownHostException .class); 122 } 123 124 public void testRmiProxyFactoryBeanWithNoSuchObjectExceptionAndRefresh() throws Exception { 125 doTestRmiProxyFactoryBeanWithExceptionAndRefresh(NoSuchObjectException .class); 126 } 127 128 public void testRmiProxyFactoryBeanWithStubNotFoundExceptionAndRefresh() throws Exception { 129 doTestRmiProxyFactoryBeanWithExceptionAndRefresh(StubNotFoundException .class); 130 } 131 132 public void testRmiProxyFactoryBeanWithMarshalExceptionAndRefresh() throws Exception { 133 doTestRmiProxyFactoryBeanWithExceptionAndRefresh(MarshalException .class); 134 } 135 136 public void testRmiProxyFactoryBeanWithUnmarshalExceptionAndRefresh() throws Exception { 137 doTestRmiProxyFactoryBeanWithExceptionAndRefresh(UnmarshalException .class); 138 } 139 140 private void doTestRmiProxyFactoryBeanWithExceptionAndRefresh(Class exceptionClass) throws Exception { 141 CountingRmiProxyFactoryBean factory = new CountingRmiProxyFactoryBean(); 142 factory.setServiceInterface(IRemoteBean.class); 143 factory.setServiceUrl("rmi://localhost:1090/test"); 144 factory.setRefreshStubOnConnectFailure(true); 145 factory.afterPropertiesSet(); 146 assertTrue(factory.getObject() instanceof IRemoteBean); 147 IRemoteBean proxy = (IRemoteBean) factory.getObject(); 148 try { 149 proxy.setName(exceptionClass.getName()); 150 fail("Should have thrown " + exceptionClass.getName()); 151 } 152 catch (Exception ex) { 153 if (exceptionClass.isInstance(ex)) { 154 } 156 else { 157 throw ex; 158 } 159 } 160 assertEquals(2, factory.counter); 161 } 162 163 public void testRmiProxyFactoryBeanWithBusinessInterface() throws Exception { 164 CountingRmiProxyFactoryBean factory = new CountingRmiProxyFactoryBean(); 165 factory.setServiceInterface(IBusinessBean.class); 166 factory.setServiceUrl("rmi://localhost:1090/test"); 167 factory.afterPropertiesSet(); 168 assertTrue(factory.getObject() instanceof IBusinessBean); 169 IBusinessBean proxy = (IBusinessBean) factory.getObject(); 170 assertFalse(proxy instanceof IRemoteBean); 171 proxy.setName("myName"); 172 assertEquals(RemoteBean.name, "myName"); 173 assertEquals(1, factory.counter); 174 } 175 176 public void testRmiProxyFactoryBeanWithBusinessInterfaceAndRemoteException() throws Exception { 177 doTestRmiProxyFactoryBeanWithBusinessInterfaceAndException( 178 RemoteException .class, RemoteAccessException.class); 179 } 180 181 public void testRmiProxyFactoryBeanWithBusinessInterfaceAndConnectException() throws Exception { 182 doTestRmiProxyFactoryBeanWithBusinessInterfaceAndException( 183 ConnectException .class, RemoteConnectFailureException.class); 184 } 185 186 public void testRmiProxyFactoryBeanWithBusinessInterfaceAndConnectIOException() throws Exception { 187 doTestRmiProxyFactoryBeanWithBusinessInterfaceAndException( 188 ConnectIOException .class, RemoteConnectFailureException.class); 189 } 190 191 public void testRmiProxyFactoryBeanWithBusinessInterfaceAndUnknownHostException() throws Exception { 192 doTestRmiProxyFactoryBeanWithBusinessInterfaceAndException( 193 UnknownHostException .class, RemoteConnectFailureException.class); 194 } 195 196 public void testRmiProxyFactoryBeanWithBusinessInterfaceAndNoSuchObjectExceptionException() throws Exception { 197 doTestRmiProxyFactoryBeanWithBusinessInterfaceAndException( 198 NoSuchObjectException .class, RemoteConnectFailureException.class); 199 } 200 201 public void testRmiProxyFactoryBeanWithBusinessInterfaceAndStubNotFoundException() throws Exception { 202 doTestRmiProxyFactoryBeanWithBusinessInterfaceAndException( 203 StubNotFoundException .class, RemoteConnectFailureException.class); 204 } 205 206 public void testRmiProxyFactoryBeanWithBusinessInterfaceAndMarshalException() throws Exception { 207 doTestRmiProxyFactoryBeanWithBusinessInterfaceAndException( 208 MarshalException .class, RemoteConnectFailureException.class); 209 } 210 211 public void testRmiProxyFactoryBeanWithBusinessInterfaceAndUnmarshalException() throws Exception { 212 doTestRmiProxyFactoryBeanWithBusinessInterfaceAndException( 213 UnmarshalException .class, RemoteConnectFailureException.class); 214 } 215 216 private void doTestRmiProxyFactoryBeanWithBusinessInterfaceAndException( 217 Class rmiExceptionClass, Class springExceptionClass) throws Exception { 218 CountingRmiProxyFactoryBean factory = new CountingRmiProxyFactoryBean(); 219 factory.setServiceInterface(IBusinessBean.class); 220 factory.setServiceUrl("rmi://localhost:1090/test"); 221 factory.afterPropertiesSet(); 222 assertTrue(factory.getObject() instanceof IBusinessBean); 223 IBusinessBean proxy = (IBusinessBean) factory.getObject(); 224 assertFalse(proxy instanceof IRemoteBean); 225 try { 226 proxy.setName(rmiExceptionClass.getName()); 227 fail("Should have thrown " + rmiExceptionClass.getName()); 228 } 229 catch (Exception ex) { 230 if (springExceptionClass.isInstance(ex)) { 231 } 233 else { 234 throw ex; 235 } 236 } 237 assertEquals(1, factory.counter); 238 } 239 240 public void testRmiProxyFactoryBeanWithBusinessInterfaceAndRemoteExceptionAndRefresh() throws Exception { 241 doTestRmiProxyFactoryBeanWithBusinessInterfaceAndExceptionAndRefresh( 242 RemoteException .class, RemoteAccessException.class); 243 } 244 245 public void testRmiProxyFactoryBeanWithBusinessInterfaceAndConnectExceptionAndRefresh() throws Exception { 246 doTestRmiProxyFactoryBeanWithBusinessInterfaceAndExceptionAndRefresh( 247 ConnectException .class, RemoteConnectFailureException.class); 248 } 249 250 public void testRmiProxyFactoryBeanWithBusinessInterfaceAndConnectIOExceptionAndRefresh() throws Exception { 251 doTestRmiProxyFactoryBeanWithBusinessInterfaceAndExceptionAndRefresh( 252 ConnectIOException .class, RemoteConnectFailureException.class); 253 } 254 255 public void testRmiProxyFactoryBeanWithBusinessInterfaceAndUnknownHostExceptionAndRefresh() throws Exception { 256 doTestRmiProxyFactoryBeanWithBusinessInterfaceAndExceptionAndRefresh( 257 UnknownHostException .class, RemoteConnectFailureException.class); 258 } 259 260 public void testRmiProxyFactoryBeanWithBusinessInterfaceAndNoSuchObjectExceptionAndRefresh() throws Exception { 261 doTestRmiProxyFactoryBeanWithBusinessInterfaceAndExceptionAndRefresh( 262 NoSuchObjectException .class, RemoteConnectFailureException.class); 263 } 264 265 public void testRmiProxyFactoryBeanWithBusinessInterfaceAndStubNotFoundExceptionAndRefresh() throws Exception { 266 doTestRmiProxyFactoryBeanWithBusinessInterfaceAndExceptionAndRefresh( 267 StubNotFoundException .class, RemoteConnectFailureException.class); 268 } 269 270 public void testRmiProxyFactoryBeanWithBusinessInterfaceAndMarshalExceptionAndRefresh() throws Exception { 271 doTestRmiProxyFactoryBeanWithBusinessInterfaceAndExceptionAndRefresh( 272 MarshalException .class, RemoteConnectFailureException.class); 273 } 274 275 public void testRmiProxyFactoryBeanWithBusinessInterfaceAndUnmarshalExceptionAndRefresh() throws Exception { 276 doTestRmiProxyFactoryBeanWithBusinessInterfaceAndExceptionAndRefresh( 277 UnmarshalException .class, RemoteConnectFailureException.class); 278 } 279 280 private void doTestRmiProxyFactoryBeanWithBusinessInterfaceAndExceptionAndRefresh( 281 Class rmiExceptionClass, Class springExceptionClass) throws Exception { 282 283 CountingRmiProxyFactoryBean factory = new CountingRmiProxyFactoryBean(); 284 factory.setServiceInterface(IBusinessBean.class); 285 factory.setServiceUrl("rmi://localhost:1090/test"); 286 factory.setRefreshStubOnConnectFailure(true); 287 factory.afterPropertiesSet(); 288 assertTrue(factory.getObject() instanceof IBusinessBean); 289 IBusinessBean proxy = (IBusinessBean) factory.getObject(); 290 assertFalse(proxy instanceof IRemoteBean); 291 try { 292 proxy.setName(rmiExceptionClass.getName()); 293 fail("Should have thrown " + rmiExceptionClass.getName()); 294 } 295 catch (Exception ex) { 296 if (springExceptionClass.isInstance(ex)) { 297 } 299 else { 300 throw ex; 301 } 302 } 303 if (RemoteConnectFailureException.class.isAssignableFrom(springExceptionClass)) { 304 assertEquals(2, factory.counter); 305 } 306 else { 307 assertEquals(1, factory.counter); 308 } 309 } 310 311 public void testRmiClientInterceptorRequiresUrl() throws Exception { 312 RmiClientInterceptor client = new RmiClientInterceptor(); 313 client.setServiceInterface(IRemoteBean.class); 314 315 try { 316 client.afterPropertiesSet(); 317 fail("url isn't set, expected IllegalArgumentException"); 318 } 319 catch(IllegalArgumentException e){ 320 } 322 } 323 324 public void testRemoteInvocation() throws NoSuchMethodException { 325 327 RemoteBean rb = new RemoteBean(); 328 329 MethodInvocation mi = new ReflectiveMethodInvocation( 330 rb, rb, rb.getClass().getDeclaredMethod("setName", new Class [] {String .class}), 331 new Object [] { "bla" }, RemoteBean.class, new ArrayList ()); 332 333 RemoteInvocation inv = new RemoteInvocation(mi); 334 335 assertEquals("setName", inv.getMethodName()); 336 assertEquals("bla", inv.getArguments()[0]); 337 assertEquals(String .class, inv.getParameterTypes()[0]); 338 339 inv = new RemoteInvocation(); 341 inv.setArguments(new Object [] { "bla" }); 342 assertEquals("bla", inv.getArguments()[0]); 343 inv.setMethodName("setName"); 344 assertEquals("setName", inv.getMethodName()); 345 inv.setParameterTypes(new Class [] {String .class}); 346 assertEquals(String .class, inv.getParameterTypes()[0]); 347 348 inv = new RemoteInvocation("setName", new Class [] {String .class}, new Object [] {"bla"}); 349 assertEquals("bla", inv.getArguments()[0]); 350 assertEquals("setName", inv.getMethodName()); 351 assertEquals(String .class, inv.getParameterTypes()[0]); 352 } 353 354 public void testRmiInvokerWithSpecialLocalMethods() throws Exception { 355 String serviceUrl = "rmi://localhost:1090/test"; 356 RmiProxyFactoryBean factory = new RmiProxyFactoryBean() { 357 protected Remote lookupStub() throws Exception { 358 return new RmiInvocationHandler() { 359 public Object invoke(RemoteInvocation invocation) throws RemoteException { 360 throw new RemoteException (); 361 } 362 }; 363 } 364 }; 365 factory.setServiceInterface(IBusinessBean.class); 366 factory.setServiceUrl(serviceUrl); 367 factory.afterPropertiesSet(); 368 IBusinessBean proxy = (IBusinessBean) factory.getObject(); 369 370 assertTrue(proxy.toString().indexOf("RMI invoker") != -1); 372 assertTrue(proxy.toString().indexOf(serviceUrl) != -1); 373 assertEquals(proxy.hashCode(), proxy.hashCode()); 374 assertTrue(proxy.equals(proxy)); 375 376 try { 378 proxy.setName("test"); 379 fail("Should have thrown RemoteAccessException"); 380 } 381 catch (RemoteAccessException ex) { 382 } 384 } 385 386 387 private static class CountingRmiProxyFactoryBean extends RmiProxyFactoryBean { 388 389 private int counter = 0; 390 391 protected Remote lookupStub() { 392 counter++; 393 return new RemoteBean(); 394 } 395 } 396 397 398 public static interface IBusinessBean { 399 400 public void setName(String name); 401 402 } 403 404 405 public static interface IRemoteBean extends Remote { 406 407 public void setName(String name) throws RemoteException ; 408 409 } 410 411 412 public static class RemoteBean implements IRemoteBean { 413 414 private static String name; 415 416 public void setName(String nam) throws RemoteException { 417 if (nam != null && nam.endsWith("Exception")) { 418 RemoteException rex = null; 419 try { 420 Class exClass = Class.forName(nam); 421 Constructor ctor = exClass.getConstructor(new Class [] {String .class}); 422 rex = (RemoteException ) ctor.newInstance(new Object [] {"myMessage"}); 423 } 424 catch (Exception ex) { 425 throw new RemoteException ("Illegal exception class name: " + nam, ex); 426 } 427 throw rex; 428 } 429 name = nam; 430 } 431 } 432 433 } 434 | Popular Tags |