1 16 17 package org.springframework.ejb.access; 18 19 import java.lang.reflect.Proxy ; 20 21 import javax.ejb.CreateException ; 22 import javax.ejb.EJBLocalHome ; 23 import javax.ejb.EJBLocalObject ; 24 import javax.naming.NamingException ; 25 26 import junit.framework.TestCase; 27 import org.aopalliance.aop.AspectException; 28 import org.easymock.MockControl; 29 30 import org.springframework.jndi.JndiTemplate; 31 32 37 public class LocalStatelessSessionProxyFactoryBeanTests extends TestCase { 38 39 public void testInvokesMethod() throws Exception { 40 final int value = 11; 41 final String jndiName = "foo"; 42 43 MockControl ec = MockControl.createControl(MyEjb.class); 44 MyEjb myEjb = (MyEjb) ec.getMock(); 45 myEjb.getValue(); 46 ec.setReturnValue(value, 1); 47 ec.replay(); 48 49 MockControl mc = MockControl.createControl(MyHome.class); 50 final MyHome home = (MyHome) mc.getMock(); 51 home.create(); 52 mc.setReturnValue(myEjb, 1); 53 mc.replay(); 54 55 JndiTemplate jt = new JndiTemplate() { 56 public Object lookup(String name) throws NamingException { 57 assertTrue(name.equals("java:comp/env/" + jndiName)); 59 return home; 60 } 61 }; 62 63 LocalStatelessSessionProxyFactoryBean fb = new LocalStatelessSessionProxyFactoryBean(); 64 fb.setJndiName(jndiName); 65 fb.setResourceRef(true); 66 fb.setBusinessInterface(MyBusinessMethods.class); 67 fb.setJndiTemplate(jt); 68 69 fb.afterPropertiesSet(); 71 72 MyBusinessMethods mbm = (MyBusinessMethods) fb.getObject(); 73 assertTrue(Proxy.isProxyClass(mbm.getClass())); 74 assertTrue(mbm.getValue() == value); 75 mc.verify(); 76 ec.verify(); 77 } 78 79 80 public void testCreateException() throws Exception { 81 final String jndiName = "foo"; 82 83 final CreateException cex = new CreateException (); 84 MockControl mc = MockControl.createControl(MyHome.class); 85 final MyHome home = (MyHome) mc.getMock(); 86 home.create(); 87 mc.setThrowable(cex); 88 mc.replay(); 89 90 JndiTemplate jt = new JndiTemplate() { 91 public Object lookup(String name) throws NamingException { 92 assertTrue(name.equals(jndiName)); 94 return home; 95 } 96 }; 97 98 LocalStatelessSessionProxyFactoryBean fb = new LocalStatelessSessionProxyFactoryBean(); 99 fb.setJndiName(jndiName); 100 fb.setResourceRef(false); fb.setBusinessInterface(MyBusinessMethods.class); 102 assertEquals(fb.getBusinessInterface(), MyBusinessMethods.class); 103 fb.setJndiTemplate(jt); 104 105 fb.afterPropertiesSet(); 107 108 MyBusinessMethods mbm = (MyBusinessMethods) fb.getObject(); 109 assertTrue(Proxy.isProxyClass(mbm.getClass())); 110 111 try { 112 mbm.getValue(); 113 fail("Should have failed to create EJB"); 114 } 115 catch (AspectException ex) { 116 assertSame(cex, ex.getCause()); 117 } 118 119 mc.verify(); 120 } 121 122 public void testNoBusinessInterfaceSpecified() throws Exception { 123 final String jndiName = "foo"; 126 127 MockControl mc = MockControl.createControl(MyHome.class); 128 final MyHome home = (MyHome) mc.getMock(); 129 mc.replay(); 130 131 JndiTemplate jt = new JndiTemplate() { 132 public Object lookup(String name) throws NamingException { 133 assertTrue(name.equals("java:comp/env/" + jndiName)); 135 return home; 136 } 137 }; 138 139 SimpleRemoteStatelessSessionProxyFactoryBean fb = new SimpleRemoteStatelessSessionProxyFactoryBean(); 140 fb.setJndiName(jndiName); 141 fb.setResourceRef(true); 142 fb.setJndiTemplate(jt); 144 145 assertTrue(fb.isSingleton()); 147 148 try { 149 fb.afterPropertiesSet(); 150 fail("Should have failed to create EJB"); 151 } 152 catch (IllegalArgumentException ex) { 153 assertTrue(ex.getMessage().indexOf("businessInterface") != 1); 155 } 156 157 mc.verify(); 159 } 160 161 162 public static interface MyHome extends EJBLocalHome { 163 164 MyBusinessMethods create() throws CreateException ; 165 } 166 167 168 public static interface MyBusinessMethods { 169 170 int getValue(); 171 } 172 173 174 public static interface MyEjb extends EJBLocalObject , MyBusinessMethods { 175 } 176 177 } 178 | Popular Tags |