1 16 17 package org.springframework.ejb.access; 18 19 import java.rmi.ConnectException ; 20 import java.rmi.RemoteException ; 21 22 import javax.ejb.CreateException ; 23 import javax.ejb.EJBHome ; 24 import javax.ejb.EJBObject ; 25 import javax.naming.Context ; 26 import javax.naming.NamingException ; 27 28 import junit.framework.TestCase; 29 import org.easymock.MockControl; 30 31 import org.springframework.aop.framework.ProxyFactory; 32 import org.springframework.jndi.JndiTemplate; 33 import org.springframework.remoting.RemoteAccessException; 34 35 38 public class SimpleRemoteSlsbInvokerInterceptorTests extends TestCase { 39 40 private MockControl contextControl( 41 String jndiName, RemoteInterface ejbInstance, int createCount, int lookupCount) throws Exception { 42 43 MockControl homeControl = MockControl.createControl(SlsbHome.class); 44 final SlsbHome mockHome = (SlsbHome) homeControl.getMock(); 45 mockHome.create(); 46 homeControl.setReturnValue(ejbInstance, createCount); 47 homeControl.replay(); 48 49 MockControl ctxControl = MockControl.createControl(Context .class); 50 final Context mockCtx = (Context ) ctxControl.getMock(); 51 52 mockCtx.lookup("java:comp/env/" + jndiName); 53 ctxControl.setReturnValue(mockHome, lookupCount); 54 mockCtx.close(); 55 ctxControl.setVoidCallable(lookupCount); 56 ctxControl.replay(); 57 58 return ctxControl; 59 } 60 61 private SimpleRemoteSlsbInvokerInterceptor configuredInterceptor( 62 MockControl contextControl, final String jndiName) throws Exception { 63 64 final Context mockCtx = (Context ) contextControl.getMock(); 65 SimpleRemoteSlsbInvokerInterceptor si = createInterceptor(); 66 si.setJndiTemplate(new JndiTemplate() { 67 protected Context createInitialContext() { 68 return mockCtx; 69 } 70 }); 71 si.setResourceRef(true); 72 si.setJndiName(jndiName); 73 74 return si; 75 } 76 77 protected SimpleRemoteSlsbInvokerInterceptor createInterceptor() { 78 return new SimpleRemoteSlsbInvokerInterceptor(); 79 } 80 81 protected Object configuredProxy(SimpleRemoteSlsbInvokerInterceptor si, Class ifc) throws NamingException { 82 si.afterPropertiesSet(); 83 ProxyFactory pf = new ProxyFactory(new Class [] { ifc } ); 84 pf.addAdvice(si); 85 return pf.getProxy(); 86 } 87 88 89 92 public void testPerformsLookup() throws Exception { 93 MockControl ejbControl = MockControl.createControl(RemoteInterface.class); 94 final RemoteInterface ejb = (RemoteInterface) ejbControl.getMock(); 95 ejbControl.replay(); 96 97 final String jndiName= "foobar"; 98 MockControl contextControl = contextControl(jndiName, ejb, 1, 1); 99 100 SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(contextControl, jndiName); 101 RemoteInterface target = (RemoteInterface) configuredProxy(si, RemoteInterface.class); 102 103 contextControl.verify(); 104 } 105 106 public void testLookupFailure() throws Exception { 107 final NamingException nex = new NamingException (); 108 final String jndiName= "foobar"; 109 JndiTemplate jt = new JndiTemplate() { 110 public Object lookup(String name) throws NamingException { 111 assertTrue(jndiName.equals(name)); 112 throw nex; 113 } 114 }; 115 116 SimpleRemoteSlsbInvokerInterceptor si = new SimpleRemoteSlsbInvokerInterceptor(); 117 si.setJndiName("foobar"); 118 si.setJndiTemplate(jt); 121 try { 122 si.afterPropertiesSet(); 123 fail("Should have failed with naming exception"); 124 } 125 catch (NamingException ex) { 126 assertTrue(ex == nex); 127 } 128 } 129 130 public void testInvokesMethodOnEjbInstance() throws Exception { 131 doTestInvokesMethodOnEjbInstance(true, true); 132 } 133 134 public void testInvokesMethodOnEjbInstanceWithLazyLookup() throws Exception { 135 doTestInvokesMethodOnEjbInstance(false, true); 136 } 137 138 public void testInvokesMethodOnEjbInstanceWithLazyLookupAndNoCache() throws Exception { 139 doTestInvokesMethodOnEjbInstance(false, false); 140 } 141 142 public void testInvokesMethodOnEjbInstanceWithNoCache() throws Exception { 143 doTestInvokesMethodOnEjbInstance(true, false); 144 } 145 146 private void doTestInvokesMethodOnEjbInstance(boolean lookupHomeOnStartup, boolean cacheHome) throws Exception { 147 Object retVal = new Object (); 148 MockControl ejbControl = MockControl.createControl(RemoteInterface.class); 149 final RemoteInterface ejb = (RemoteInterface) ejbControl.getMock(); 150 ejb.targetMethod(); 151 ejbControl.setReturnValue(retVal, 2); 152 ejb.remove(); 153 ejbControl.setVoidCallable(2); 154 ejbControl.replay(); 155 156 int lookupCount = 1; 157 if (!cacheHome) { 158 lookupCount++; 159 if (lookupHomeOnStartup) { 160 lookupCount++; 161 } 162 } 163 164 final String jndiName= "foobar"; 165 MockControl contextControl = contextControl(jndiName, ejb, 2, lookupCount); 166 167 SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(contextControl, jndiName); 168 si.setLookupHomeOnStartup(lookupHomeOnStartup); 169 si.setCacheHome(cacheHome); 170 171 RemoteInterface target = (RemoteInterface) configuredProxy(si, RemoteInterface.class); 172 assertTrue(target.targetMethod() == retVal); 173 assertTrue(target.targetMethod() == retVal); 174 175 contextControl.verify(); 176 ejbControl.verify(); 177 } 178 179 public void testInvokesMethodOnEjbInstanceWithHomeInterface() throws Exception { 180 Object retVal = new Object (); 181 MockControl ejbControl = MockControl.createControl(RemoteInterface.class); 182 final RemoteInterface ejb = (RemoteInterface) ejbControl.getMock(); 183 ejb.targetMethod(); 184 ejbControl.setReturnValue(retVal, 1); 185 ejb.remove(); 186 ejbControl.setVoidCallable(1); 187 ejbControl.replay(); 188 189 final String jndiName= "foobar"; 190 MockControl contextControl = contextControl(jndiName, ejb, 1, 1); 191 192 SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(contextControl, jndiName); 193 si.setHomeInterface(SlsbHome.class); 194 195 RemoteInterface target = (RemoteInterface) configuredProxy(si, RemoteInterface.class); 196 assertTrue(target.targetMethod() == retVal); 197 198 contextControl.verify(); 199 ejbControl.verify(); 200 } 201 202 public void testInvokesMethodOnEjbInstanceWithRemoteException() throws Exception { 203 MockControl ejbControl = MockControl.createControl(RemoteInterface.class); 204 final RemoteInterface ejb = (RemoteInterface) ejbControl.getMock(); 205 ejb.targetMethod(); 206 ejbControl.setThrowable(new RemoteException (), 1); 207 ejb.remove(); 208 ejbControl.setVoidCallable(1); 209 ejbControl.replay(); 210 211 final String jndiName= "foobar"; 212 MockControl contextControl = contextControl(jndiName, ejb, 1, 1); 213 214 SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(contextControl, jndiName); 215 216 RemoteInterface target = (RemoteInterface) configuredProxy(si, RemoteInterface.class); 217 try { 218 target.targetMethod(); 219 fail("Should have thrown RemoteException"); 220 } 221 catch (RemoteException ex) { 222 } 224 225 contextControl.verify(); 226 ejbControl.verify(); 227 } 228 229 public void testInvokesMethodOnEjbInstanceWithConnectExceptionWithRefresh() throws Exception { 230 doTestInvokesMethodOnEjbInstanceWithConnectExceptionWithRefresh(true, true); 231 } 232 233 public void testInvokesMethodOnEjbInstanceWithConnectExceptionWithRefreshAndLazyLookup() throws Exception { 234 doTestInvokesMethodOnEjbInstanceWithConnectExceptionWithRefresh(false, true); 235 } 236 237 public void testInvokesMethodOnEjbInstanceWithConnectExceptionWithRefreshAndLazyLookupAndNoCache() throws Exception { 238 doTestInvokesMethodOnEjbInstanceWithConnectExceptionWithRefresh(false, false); 239 } 240 241 public void testInvokesMethodOnEjbInstanceWithConnectExceptionWithRefreshAndNoCache() throws Exception { 242 doTestInvokesMethodOnEjbInstanceWithConnectExceptionWithRefresh(true, false); 243 } 244 245 private void doTestInvokesMethodOnEjbInstanceWithConnectExceptionWithRefresh( 246 boolean lookupHomeOnStartup, boolean cacheHome) throws Exception { 247 248 MockControl ejbControl = MockControl.createControl(RemoteInterface.class); 249 final RemoteInterface ejb = (RemoteInterface) ejbControl.getMock(); 250 ejb.targetMethod(); 251 ejbControl.setThrowable(new ConnectException (""), 2); 252 ejb.remove(); 253 ejbControl.setVoidCallable(2); 254 ejbControl.replay(); 255 256 int lookupCount = 2; 257 if (!cacheHome) { 258 lookupCount++; 259 if (lookupHomeOnStartup) { 260 lookupCount++; 261 } 262 } 263 264 final String jndiName= "foobar"; 265 MockControl contextControl = contextControl(jndiName, ejb, 2, lookupCount); 266 267 SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(contextControl, jndiName); 268 si.setRefreshHomeOnConnectFailure(true); 269 si.setLookupHomeOnStartup(lookupHomeOnStartup); 270 si.setCacheHome(cacheHome); 271 272 RemoteInterface target = (RemoteInterface) configuredProxy(si, RemoteInterface.class); 273 try { 274 target.targetMethod(); 275 fail("Should have thrown RemoteException"); 276 } 277 catch (ConnectException ex) { 278 } 280 281 contextControl.verify(); 282 ejbControl.verify(); 283 } 284 285 public void testInvokesMethodOnEjbInstanceWithBusinessInterface() throws Exception { 286 Object retVal = new Object (); 287 MockControl ejbControl = MockControl.createControl(RemoteInterface.class); 288 final RemoteInterface ejb = (RemoteInterface) ejbControl.getMock(); 289 ejb.targetMethod(); 290 ejbControl.setReturnValue(retVal, 1); 291 ejbControl.replay(); 292 293 final String jndiName= "foobar"; 294 MockControl contextControl = contextControl(jndiName, ejb, 1, 1); 295 296 SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(contextControl, jndiName); 297 298 BusinessInterface target = (BusinessInterface) configuredProxy(si, BusinessInterface.class); 299 assertTrue(target.targetMethod() == retVal); 300 301 contextControl.verify(); 302 ejbControl.verify(); 303 } 304 305 public void testInvokesMethodOnEjbInstanceWithBusinessInterfaceWithRemoteException() throws Exception { 306 MockControl ejbControl = MockControl.createControl(RemoteInterface.class); 307 final RemoteInterface ejb = (RemoteInterface) ejbControl.getMock(); 308 ejb.targetMethod(); 309 ejbControl.setThrowable(new RemoteException (), 1); 310 ejbControl.replay(); 311 312 final String jndiName= "foobar"; 313 MockControl contextControl = contextControl(jndiName, ejb, 1, 1); 314 315 SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(contextControl, jndiName); 316 317 BusinessInterface target = (BusinessInterface) configuredProxy(si, BusinessInterface.class); 318 try { 319 target.targetMethod(); 320 fail("Should have thrown RemoteAccessException"); 321 } 322 catch (RemoteAccessException ex) { 323 } 325 326 contextControl.verify(); 327 ejbControl.verify(); 328 } 329 330 public void testApplicationException() throws Exception { 331 doTestException(new ApplicationException()); 332 } 333 334 public void testRemoteException() throws Exception { 335 doTestException(new RemoteException ()); 336 } 337 338 private void doTestException(Exception expected) throws Exception { 339 MockControl ejbControl = MockControl.createControl(RemoteInterface.class); 340 final RemoteInterface ejb = (RemoteInterface) ejbControl.getMock(); 341 ejb.targetMethod(); 342 ejbControl.setThrowable(expected); 343 ejbControl.replay(); 344 345 final String jndiName= "foobar"; 346 MockControl contextControl = contextControl(jndiName, ejb, 1, 1); 347 348 SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(contextControl, jndiName); 349 350 RemoteInterface target = (RemoteInterface) configuredProxy(si, RemoteInterface.class); 351 try { 352 target.targetMethod(); 353 fail("Should have thrown remote exception"); 354 } 355 catch (Exception thrown) { 356 assertTrue(thrown == expected); 357 } 358 359 contextControl.verify(); 360 ejbControl.verify(); 361 } 362 363 364 367 protected interface SlsbHome extends EJBHome { 368 369 EJBObject create() throws RemoteException , CreateException ; 370 } 371 372 373 protected interface RemoteInterface extends EJBObject { 374 375 Object targetMethod() throws RemoteException , ApplicationException; 377 } 378 379 380 protected interface BusinessInterface { 381 382 Object targetMethod() throws ApplicationException; 383 } 384 385 386 protected class ApplicationException extends Exception { 387 388 public ApplicationException() { 389 super("appException"); 390 } 391 } 392 393 } 394 | Popular Tags |