1 16 17 package org.springframework.ejb.support; 18 19 import java.rmi.RemoteException ; 20 21 import javax.ejb.CreateException ; 22 import javax.ejb.EJBException ; 23 import javax.ejb.MessageDrivenContext ; 24 import javax.ejb.SessionContext ; 25 import javax.jms.Message ; 26 import javax.naming.NamingException ; 27 28 import junit.framework.TestCase; 29 import org.easymock.MockControl; 30 31 import org.springframework.beans.BeansException; 32 import org.springframework.beans.FatalBeanException; 33 import org.springframework.beans.factory.BeanFactory; 34 import org.springframework.beans.factory.access.BeanFactoryLocator; 35 import org.springframework.beans.factory.access.BeanFactoryReference; 36 import org.springframework.beans.factory.access.BootstrapException; 37 import org.springframework.beans.factory.support.StaticListableBeanFactory; 38 import org.springframework.mock.jndi.SimpleNamingContextBuilder; 39 40 47 public class EjbSupportTests extends TestCase { 48 49 public void testSfsb() throws CreateException { 50 MockControl mc = MockControl.createControl(SessionContext .class); 51 SessionContext sc = (SessionContext ) mc.getMock(); 52 mc.replay(); 53 54 final BeanFactory bf = new StaticListableBeanFactory(); 55 BeanFactoryLocator bfl = new BeanFactoryLocator() { 56 public BeanFactoryReference useBeanFactory(String factoryKey) 57 throws FatalBeanException { 58 return new BeanFactoryReference() { 59 public BeanFactory getFactory() { 60 return bf; 61 } 62 public void release() throws FatalBeanException { 63 } 65 }; 66 } 67 }; 68 69 class MySfsb extends AbstractStatefulSessionBean { 71 public void ejbCreate() throws CreateException { 72 loadBeanFactory(); 73 assertTrue(getBeanFactory() == bf); 74 } 75 public void ejbActivate() throws EJBException , RemoteException { 76 throw new UnsupportedOperationException ("ejbActivate"); 77 } 78 public void ejbPassivate() throws EJBException , RemoteException { 79 throw new UnsupportedOperationException ("ejbPassivate"); 80 } 81 82 } 83 84 MySfsb sfsb = new MySfsb(); 85 sfsb.setBeanFactoryLocator(bfl); 86 sfsb.setSessionContext(sc); 87 sfsb.ejbCreate(); 88 assertTrue(sc == sfsb.getSessionContext()); 89 } 90 91 94 public void testHelpfulNamingLookupMessage() throws NamingException , CreateException { 95 SimpleNamingContextBuilder.emptyActivatedContextBuilder(); 96 97 MockControl mc = MockControl.createControl(SessionContext .class); 98 SessionContext sc = (SessionContext ) mc.getMock(); 99 mc.replay(); 100 101 103 AbstractStatelessSessionBean slsb = new AbstractStatelessSessionBean() { 105 public void onEjbCreate() { 106 } 107 }; 108 109 slsb.setSessionContext(sc); 110 try { 111 slsb.ejbCreate(); 112 fail(); 113 } 114 catch (BeansException ex) { 115 assertTrue(ex.getMessage().indexOf("environment") != -1); 116 assertTrue(ex.getMessage().indexOf("ejb/BeanFactoryPath") != -1); 117 } 118 } 119 120 public void testSlsb() throws Exception { 121 MockControl mc = MockControl.createControl(SessionContext .class); 122 SessionContext sc = (SessionContext ) mc.getMock(); 123 mc.replay(); 124 125 final BeanFactory bf = new StaticListableBeanFactory(); 126 BeanFactoryLocator bfl = new BeanFactoryLocator() { 127 public BeanFactoryReference useBeanFactory(String factoryKey) throws FatalBeanException { 128 return new BeanFactoryReference() { 129 public BeanFactory getFactory() { 130 return bf; 131 } 132 public void release() throws FatalBeanException { 133 } 135 }; 136 } 137 }; 138 139 AbstractStatelessSessionBean slsb = new AbstractStatelessSessionBean() { 140 protected void onEjbCreate() throws CreateException { 141 assertTrue(getBeanFactory() == bf); 142 } 143 }; 144 slsb.setBeanFactoryLocator(bfl); 146 slsb.setSessionContext(sc); 147 assertTrue(sc == slsb.getSessionContext()); 148 slsb.ejbCreate(); 149 try { 150 slsb.ejbActivate(); 151 fail("Shouldn't allow activation of SLSB"); 152 } 153 catch (IllegalStateException ex) { 154 } 156 try { 157 slsb.ejbPassivate(); 158 fail("Shouldn't allow passivation of SLSB"); 159 } 160 catch (IllegalStateException ex) { 161 } 163 } 164 165 166 public void testJmsMdb() throws Exception { 167 MockControl mc = MockControl.createControl(MessageDrivenContext .class); 168 MessageDrivenContext sc = (MessageDrivenContext ) mc.getMock(); 169 mc.replay(); 170 171 final BeanFactory bf = new StaticListableBeanFactory(); 172 BeanFactoryLocator bfl = new BeanFactoryLocator() { 173 public BeanFactoryReference useBeanFactory(String factoryKey) throws FatalBeanException { 174 return new BeanFactoryReference() { 175 public BeanFactory getFactory() { 176 return bf; 177 } 178 public void release() throws FatalBeanException { 179 } 181 }; 182 } 183 }; 184 185 AbstractJmsMessageDrivenBean mdb = new AbstractJmsMessageDrivenBean() { 186 protected void onEjbCreate() { 187 assertTrue(getBeanFactory() == bf); 188 } 189 public void onMessage(Message msg) { 190 throw new UnsupportedOperationException ("onMessage"); 191 } 192 }; 193 mdb.setBeanFactoryLocator(bfl); 195 mdb.setMessageDrivenContext(sc); 196 assertTrue(sc == mdb.getMessageDrivenContext()); 197 mdb.ejbCreate(); 198 } 199 200 public void testCannotLoadBeanFactory() throws Exception { 201 MockControl mc = MockControl.createControl(SessionContext .class); 202 SessionContext sc = (SessionContext ) mc.getMock(); 203 mc.replay(); 204 205 BeanFactoryLocator bfl = new BeanFactoryLocator() { 206 public BeanFactoryReference useBeanFactory(String factoryKey) throws FatalBeanException { 207 throw new BootstrapException("", null); 208 }}; 209 210 AbstractStatelessSessionBean slsb = new AbstractStatelessSessionBean() { 211 protected void onEjbCreate() throws CreateException { 212 } 213 }; 214 slsb.setBeanFactoryLocator(bfl); 216 slsb.setSessionContext(sc); 217 218 try { 219 slsb.ejbCreate(); 220 fail(); 221 } 222 catch (BeansException ex) { 223 } 225 } 226 227 } 228 | Popular Tags |