1 16 17 package org.springframework.ejb.access; 18 19 import java.lang.reflect.Proxy ; 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.NamingException ; 26 27 import org.easymock.MockControl; 28 29 import org.springframework.jndi.JndiTemplate; 30 import org.springframework.remoting.RemoteAccessException; 31 32 38 public class SimpleRemoteStatelessSessionProxyFactoryBeanTests extends SimpleRemoteSlsbInvokerInterceptorTests { 39 40 protected SimpleRemoteSlsbInvokerInterceptor createInterceptor() { 41 return new SimpleRemoteStatelessSessionProxyFactoryBean(); 42 } 43 44 protected Object configuredProxy(SimpleRemoteSlsbInvokerInterceptor si, Class ifc) throws NamingException { 45 SimpleRemoteStatelessSessionProxyFactoryBean fb = (SimpleRemoteStatelessSessionProxyFactoryBean) si; 46 fb.setBusinessInterface(ifc); 47 fb.afterPropertiesSet(); 48 return fb.getObject(); 49 } 50 51 public void testInvokesMethod() throws Exception { 52 final int value = 11; 53 final String jndiName = "foo"; 54 55 MockControl ec = MockControl.createControl(MyEjb.class); 56 MyEjb myEjb = (MyEjb) ec.getMock(); 57 myEjb.getValue(); 58 ec.setReturnValue(value, 1); 59 myEjb.remove(); 60 ec.setVoidCallable(1); 61 ec.replay(); 62 63 MockControl mc = MockControl.createControl(MyHome.class); 64 final MyHome home = (MyHome) mc.getMock(); 65 home.create(); 66 mc.setReturnValue(myEjb, 1); 67 mc.replay(); 68 69 JndiTemplate jt = new JndiTemplate() { 70 public Object lookup(String name) { 71 assertTrue(name.equals("java:comp/env/" + jndiName)); 73 return home; 74 } 75 }; 76 77 SimpleRemoteStatelessSessionProxyFactoryBean fb = new SimpleRemoteStatelessSessionProxyFactoryBean(); 78 fb.setJndiName(jndiName); 79 fb.setResourceRef(true); 80 fb.setBusinessInterface(MyBusinessMethods.class); 81 fb.setJndiTemplate(jt); 82 83 fb.afterPropertiesSet(); 85 86 MyBusinessMethods mbm = (MyBusinessMethods) fb.getObject(); 87 assertTrue(Proxy.isProxyClass(mbm.getClass())); 88 assertEquals("Returns expected value", value, mbm.getValue()); 89 mc.verify(); 90 ec.verify(); 91 } 92 93 public void testRemoteException() throws Exception { 94 final RemoteException rex = new RemoteException (); 95 final String jndiName = "foo"; 96 97 MockControl ec = MockControl.createControl(MyEjb.class); 98 MyEjb myEjb = (MyEjb) ec.getMock(); 99 myEjb.getValue(); 100 ec.setThrowable(rex); 101 myEjb.remove(); 104 ec.setVoidCallable(1); 105 ec.replay(); 106 107 MockControl mc = MockControl.createControl(MyHome.class); 108 final MyHome home = (MyHome) mc.getMock(); 109 home.create(); 110 mc.setReturnValue(myEjb, 1); 111 mc.replay(); 112 113 JndiTemplate jt = new JndiTemplate() { 114 public Object lookup(String name) { 115 assertTrue(name.equals("java:comp/env/" + jndiName)); 117 return home; 118 } 119 }; 120 121 SimpleRemoteStatelessSessionProxyFactoryBean fb = new SimpleRemoteStatelessSessionProxyFactoryBean(); 122 fb.setJndiName(jndiName); 123 fb.setResourceRef(true); 124 fb.setBusinessInterface(MyBusinessMethods.class); 125 fb.setJndiTemplate(jt); 126 127 fb.afterPropertiesSet(); 129 130 MyBusinessMethods mbm = (MyBusinessMethods) fb.getObject(); 131 assertTrue(Proxy.isProxyClass(mbm.getClass())); 132 try { 133 mbm.getValue(); 134 fail("Should've thrown remote exception"); 135 } 136 catch (RemoteException ex) { 137 assertSame("Threw expected RemoteException", rex, ex); 138 } 139 mc.verify(); 140 ec.verify(); 141 } 142 143 public void testCreateException() throws Exception { 144 final String jndiName = "foo"; 145 146 final CreateException cex = new CreateException (); 147 MockControl mc = MockControl.createControl(MyHome.class); 148 final MyHome home = (MyHome) mc.getMock(); 149 home.create(); 150 mc.setThrowable(cex); 151 mc.replay(); 152 153 JndiTemplate jt = new JndiTemplate() { 154 public Object lookup(String name) { 155 assertTrue(name.equals(jndiName)); 157 return home; 158 } 159 }; 160 161 SimpleRemoteStatelessSessionProxyFactoryBean fb = new SimpleRemoteStatelessSessionProxyFactoryBean(); 162 fb.setJndiName(jndiName); 163 fb.setBusinessInterface(MyBusinessMethods.class); 165 assertEquals(fb.getBusinessInterface(), MyBusinessMethods.class); 166 fb.setJndiTemplate(jt); 167 168 fb.afterPropertiesSet(); 170 171 MyBusinessMethods mbm = (MyBusinessMethods) fb.getObject(); 172 assertTrue(Proxy.isProxyClass(mbm.getClass())); 173 174 try { 175 mbm.getValue(); 176 fail("Should have failed to create EJB"); 177 } 178 catch (RemoteException ex) { 179 } 181 182 mc.verify(); 183 } 184 185 public void testCreateExceptionWithLocalBusinessInterface() throws Exception { 186 final String jndiName = "foo"; 187 188 final CreateException cex = new CreateException (); 189 MockControl mc = MockControl.createControl(MyHome.class); 190 final MyHome home = (MyHome) mc.getMock(); 191 home.create(); 192 mc.setThrowable(cex); 193 mc.replay(); 194 195 JndiTemplate jt = new JndiTemplate() { 196 public Object lookup(String name) { 197 assertTrue(name.equals(jndiName)); 199 return home; 200 } 201 }; 202 203 SimpleRemoteStatelessSessionProxyFactoryBean fb = new SimpleRemoteStatelessSessionProxyFactoryBean(); 204 fb.setJndiName(jndiName); 205 fb.setBusinessInterface(MyLocalBusinessMethods.class); 207 assertEquals(fb.getBusinessInterface(), MyLocalBusinessMethods.class); 208 fb.setJndiTemplate(jt); 209 210 fb.afterPropertiesSet(); 212 213 MyLocalBusinessMethods mbm = (MyLocalBusinessMethods) fb.getObject(); 214 assertTrue(Proxy.isProxyClass(mbm.getClass())); 215 216 try { 217 mbm.getValue(); 218 fail("Should have failed to create EJB"); 219 } 220 catch (RemoteAccessException ex) { 221 assertTrue(ex.getCause() == cex); 222 } 223 224 mc.verify(); 225 } 226 227 public void testNoBusinessInterfaceSpecified() throws Exception { 228 final String jndiName = "foo"; 231 232 MockControl mc = MockControl.createControl(MyHome.class); 233 final MyHome home = (MyHome) mc.getMock(); 234 mc.replay(); 235 236 JndiTemplate jt = new JndiTemplate() { 237 public Object lookup(String name) throws NamingException { 238 assertTrue(name.equals(jndiName)); 240 return home; 241 } 242 }; 243 244 SimpleRemoteStatelessSessionProxyFactoryBean fb = new SimpleRemoteStatelessSessionProxyFactoryBean(); 245 fb.setJndiName(jndiName); 246 fb.setJndiTemplate(jt); 249 250 assertTrue(fb.isSingleton()); 252 253 try { 254 fb.afterPropertiesSet(); 255 fail("Should have failed to create EJB"); 256 } 257 catch (IllegalArgumentException ex) { 258 assertTrue(ex.getMessage().indexOf("businessInterface") != 1); 260 } 261 262 mc.verify(); 264 } 265 266 267 protected static interface MyHome extends EJBHome { 268 269 MyBusinessMethods create() throws CreateException , RemoteException ; 270 } 271 272 273 protected static interface MyBusinessMethods { 274 275 int getValue() throws RemoteException ; 276 } 277 278 279 protected static interface MyLocalBusinessMethods { 280 281 int getValue(); 282 } 283 284 285 protected static interface MyEjb extends EJBObject , MyBusinessMethods { 286 287 } 288 289 } 290 | Popular Tags |