1 2 17 18 package org.springframework.ejb.access; 19 20 import javax.ejb.CreateException ; 21 import javax.ejb.EJBLocalHome ; 22 import javax.ejb.EJBLocalObject ; 23 import javax.naming.Context ; 24 import javax.naming.NamingException ; 25 26 import junit.framework.TestCase; 27 import org.easymock.MockControl; 28 29 import org.springframework.aop.framework.ProxyFactory; 30 import org.springframework.jndi.JndiTemplate; 31 32 35 public class LocalSlsbInvokerInterceptorTests extends TestCase { 36 37 40 public void testPerformsLookup() throws Exception { 41 MockControl ejbControl = MockControl.createControl(LocalInterfaceWithBusinessMethods.class); 42 final LocalInterfaceWithBusinessMethods ejb = (LocalInterfaceWithBusinessMethods) ejbControl.getMock(); 43 ejbControl.replay(); 44 45 final String jndiName= "foobar"; 46 MockControl contextControl = contextControl(jndiName, ejb); 47 48 LocalSlsbInvokerInterceptor si = configuredInterceptor(contextControl, jndiName); 49 50 contextControl.verify(); 51 } 52 53 public void testLookupFailure() throws Exception { 54 final NamingException nex = new NamingException (); 55 final String jndiName= "foobar"; 56 JndiTemplate jt = new JndiTemplate() { 57 public Object lookup(String name) throws NamingException { 58 assertTrue(jndiName.equals(name)); 59 throw nex; 60 } 61 }; 62 63 LocalSlsbInvokerInterceptor si = new LocalSlsbInvokerInterceptor(); 64 si.setJndiName("foobar"); 65 si.setJndiTemplate(jt); 68 try { 69 si.afterPropertiesSet(); 70 fail("Should have failed with naming exception"); 71 } 72 catch (NamingException ex) { 73 assertTrue(ex == nex); 74 } 75 } 76 77 public void testInvokesMethodOnEjbInstance() throws Exception { 78 Object retVal = new Object (); 79 MockControl ejbControl = MockControl.createControl(LocalInterfaceWithBusinessMethods.class); 80 final LocalInterfaceWithBusinessMethods ejb = (LocalInterfaceWithBusinessMethods) ejbControl.getMock(); 81 ejb.targetMethod(); 82 ejbControl.setReturnValue(retVal, 1); 83 ejb.remove(); 84 ejbControl.setVoidCallable(1); 85 ejbControl.replay(); 86 87 final String jndiName= "foobar"; 88 MockControl contextControl = contextControl(jndiName, ejb); 89 90 LocalSlsbInvokerInterceptor si = configuredInterceptor(contextControl, jndiName); 91 92 ProxyFactory pf = new ProxyFactory(new Class [] { BusinessMethods.class } ); 93 pf.addAdvice(si); 94 BusinessMethods target = (BusinessMethods) pf.getProxy(); 95 96 assertTrue(target.targetMethod() == retVal); 97 98 contextControl.verify(); 99 ejbControl.verify(); 100 } 101 102 public void testInvokesMethodOnEjbInstanceWithSeparateBusinessMethods() throws Exception { 103 Object retVal = new Object (); 104 MockControl ejbControl = MockControl.createControl(LocalInterface.class); 105 final LocalInterface ejb = (LocalInterface) ejbControl.getMock(); 106 ejb.targetMethod(); 107 ejbControl.setReturnValue(retVal, 1); 108 ejb.remove(); 109 ejbControl.setVoidCallable(1); 110 ejbControl.replay(); 111 112 final String jndiName= "foobar"; 113 MockControl contextControl = contextControl(jndiName, ejb); 114 115 LocalSlsbInvokerInterceptor si = configuredInterceptor(contextControl, jndiName); 116 117 ProxyFactory pf = new ProxyFactory(new Class [] { BusinessMethods.class } ); 118 pf.addAdvice(si); 119 BusinessMethods target = (BusinessMethods) pf.getProxy(); 120 121 assertTrue(target.targetMethod() == retVal); 122 123 contextControl.verify(); 124 ejbControl.verify(); 125 } 126 127 private void testException(Exception expected) throws Exception { 128 MockControl ejbControl = MockControl.createControl(LocalInterfaceWithBusinessMethods.class); 129 final LocalInterfaceWithBusinessMethods ejb = (LocalInterfaceWithBusinessMethods) ejbControl.getMock(); 130 ejb.targetMethod(); 131 ejbControl.setThrowable(expected); 132 ejbControl.replay(); 133 134 final String jndiName= "foobar"; 135 MockControl contextControl = contextControl(jndiName, ejb); 136 137 LocalSlsbInvokerInterceptor si = configuredInterceptor(contextControl, jndiName); 138 139 ProxyFactory pf = new ProxyFactory(new Class [] { LocalInterfaceWithBusinessMethods.class } ); 140 pf.addAdvice(si); 141 LocalInterfaceWithBusinessMethods target = (LocalInterfaceWithBusinessMethods) pf.getProxy(); 142 143 try { 144 target.targetMethod(); 145 fail("Should have thrown exception"); 146 } 147 catch (Exception thrown) { 148 assertTrue(thrown == expected); 149 } 150 151 contextControl.verify(); 152 ejbControl.verify(); 153 } 154 155 public void testApplicationException() throws Exception { 156 testException(new ApplicationException()); 157 } 158 159 protected MockControl contextControl(final String jndiName, final EJBLocalObject ejbInstance) 160 throws Exception { 161 162 MockControl homeControl = MockControl.createControl(SlsbHome.class); 163 final SlsbHome mockHome = (SlsbHome) homeControl.getMock(); 164 mockHome.create(); 165 homeControl.setReturnValue(ejbInstance, 1); 166 homeControl.replay(); 167 168 MockControl ctxControl = MockControl.createControl(Context .class); 169 final Context mockCtx = (Context ) ctxControl.getMock(); 170 171 mockCtx.lookup("java:comp/env/" + jndiName); 172 ctxControl.setReturnValue(mockHome); 173 mockCtx.close(); 174 ctxControl.setVoidCallable(); 175 ctxControl.replay(); 176 return ctxControl; 177 } 178 179 protected LocalSlsbInvokerInterceptor configuredInterceptor(MockControl contextControl, final String jndiName) 180 throws Exception { 181 182 final Context mockCtx = (Context ) contextControl.getMock(); 183 LocalSlsbInvokerInterceptor si = new LocalSlsbInvokerInterceptor(); 184 si.setJndiTemplate(new JndiTemplate() { 185 protected Context createInitialContext() throws NamingException { 186 return mockCtx; 187 } 188 }); 189 si.setJndiName(jndiName); 190 si.setResourceRef(true); 191 si.afterPropertiesSet(); 192 193 return si; 194 } 195 196 197 200 private interface SlsbHome extends EJBLocalHome { 201 202 LocalInterface create() throws CreateException ; 203 } 204 205 206 private interface BusinessMethods { 207 208 Object targetMethod() throws ApplicationException; 209 } 210 211 212 private interface LocalInterface extends EJBLocalObject { 213 214 Object targetMethod() throws ApplicationException; 215 } 216 217 218 private interface LocalInterfaceWithBusinessMethods extends LocalInterface, BusinessMethods { 219 } 220 221 222 private class ApplicationException extends Exception { 223 224 public ApplicationException() { 225 super("appException"); 226 } 227 } 228 229 } 230 | Popular Tags |