KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > ejb > support > EjbSupportTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.ejb.support;
18
19 import java.rmi.RemoteException JavaDoc;
20
21 import javax.ejb.CreateException JavaDoc;
22 import javax.ejb.EJBException JavaDoc;
23 import javax.ejb.MessageDrivenContext JavaDoc;
24 import javax.ejb.SessionContext JavaDoc;
25 import javax.jms.Message JavaDoc;
26 import javax.naming.NamingException JavaDoc;
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 /**
41  * Most of the value of the tests here is in being forced
42  * to implement ejbCreate() methods.
43  *
44  * @author Rod Johnson
45  * @since 21.05.2003
46  */

47 public class EjbSupportTests extends TestCase {
48
49     public void testSfsb() throws CreateException JavaDoc {
50         MockControl mc = MockControl.createControl(SessionContext JavaDoc.class);
51         SessionContext JavaDoc sc = (SessionContext JavaDoc) mc.getMock();
52         mc.replay();
53         
54         final BeanFactory bf = new StaticListableBeanFactory();
55         BeanFactoryLocator bfl = new BeanFactoryLocator() {
56             public BeanFactoryReference useBeanFactory(String JavaDoc factoryKey)
57                     throws FatalBeanException {
58                 return new BeanFactoryReference() {
59                     public BeanFactory getFactory() {
60                         return bf;
61                     }
62                     public void release() throws FatalBeanException {
63                         // nothing to do in default implementation
64
}
65                 };
66             }
67         };
68         
69         // Basically the test is what needed to be implemented here!
70
class MySfsb extends AbstractStatefulSessionBean {
71             public void ejbCreate() throws CreateException JavaDoc {
72                 loadBeanFactory();
73                 assertTrue(getBeanFactory() == bf);
74             }
75             public void ejbActivate() throws EJBException JavaDoc, RemoteException JavaDoc {
76                 throw new UnsupportedOperationException JavaDoc("ejbActivate");
77             }
78             public void ejbPassivate() throws EJBException JavaDoc, RemoteException JavaDoc {
79                 throw new UnsupportedOperationException JavaDoc("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     /**
92      * Check there's a helpful message if no JNDI key is present.
93      */

94     public void testHelpfulNamingLookupMessage() throws NamingException JavaDoc, CreateException JavaDoc {
95         SimpleNamingContextBuilder.emptyActivatedContextBuilder();
96         
97         MockControl mc = MockControl.createControl(SessionContext JavaDoc.class);
98         SessionContext JavaDoc sc = (SessionContext JavaDoc) mc.getMock();
99         mc.replay();
100     
101         // Leave with default XmlBeanFactoryLoader
102

103         // Basically the test is what needed to be implemented here!
104
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 JavaDoc {
121         MockControl mc = MockControl.createControl(SessionContext JavaDoc.class);
122         SessionContext JavaDoc sc = (SessionContext JavaDoc) mc.getMock();
123         mc.replay();
124         
125         final BeanFactory bf = new StaticListableBeanFactory();
126         BeanFactoryLocator bfl = new BeanFactoryLocator() {
127             public BeanFactoryReference useBeanFactory(String JavaDoc factoryKey) throws FatalBeanException {
128                 return new BeanFactoryReference() {
129                     public BeanFactory getFactory() {
130                         return bf;
131                     }
132                     public void release() throws FatalBeanException {
133                         // nothing to do in default implementation
134
}
135                 };
136             }
137         };
138     
139         AbstractStatelessSessionBean slsb = new AbstractStatelessSessionBean() {
140             protected void onEjbCreate() throws CreateException JavaDoc {
141                 assertTrue(getBeanFactory() == bf);
142             }
143         };
144         // Must call this method before ejbCreate()
145
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 JavaDoc ex) {
154             // Ok
155
}
156         try {
157             slsb.ejbPassivate();
158             fail("Shouldn't allow passivation of SLSB");
159         }
160         catch (IllegalStateException JavaDoc ex) {
161             // Ok
162
}
163     }
164
165
166     public void testJmsMdb() throws Exception JavaDoc {
167         MockControl mc = MockControl.createControl(MessageDrivenContext JavaDoc.class);
168         MessageDrivenContext JavaDoc sc = (MessageDrivenContext JavaDoc) mc.getMock();
169         mc.replay();
170     
171         final BeanFactory bf = new StaticListableBeanFactory();
172         BeanFactoryLocator bfl = new BeanFactoryLocator() {
173             public BeanFactoryReference useBeanFactory(String JavaDoc factoryKey) throws FatalBeanException {
174                 return new BeanFactoryReference() {
175                     public BeanFactory getFactory() {
176                         return bf;
177                     }
178                     public void release() throws FatalBeanException {
179                         // nothing to do in default implementation
180
}
181                 };
182             }
183         };
184
185         AbstractJmsMessageDrivenBean mdb = new AbstractJmsMessageDrivenBean() {
186             protected void onEjbCreate() {
187                 assertTrue(getBeanFactory() == bf);
188             }
189             public void onMessage(Message JavaDoc msg) {
190                 throw new UnsupportedOperationException JavaDoc("onMessage");
191             }
192         };
193         // Must call this method before ejbCreate()
194
mdb.setBeanFactoryLocator(bfl);
195         mdb.setMessageDrivenContext(sc);
196         assertTrue(sc == mdb.getMessageDrivenContext());
197         mdb.ejbCreate();
198     }
199     
200     public void testCannotLoadBeanFactory() throws Exception JavaDoc {
201         MockControl mc = MockControl.createControl(SessionContext JavaDoc.class);
202         SessionContext JavaDoc sc = (SessionContext JavaDoc) mc.getMock();
203         mc.replay();
204     
205         BeanFactoryLocator bfl = new BeanFactoryLocator() {
206             public BeanFactoryReference useBeanFactory(String JavaDoc factoryKey) throws FatalBeanException {
207                 throw new BootstrapException("", null);
208         }};
209
210         AbstractStatelessSessionBean slsb = new AbstractStatelessSessionBean() {
211             protected void onEjbCreate() throws CreateException JavaDoc {
212             }
213         };
214         // Must call this method before ejbCreate()
215
slsb.setBeanFactoryLocator(bfl);
216         slsb.setSessionContext(sc);
217         
218         try {
219             slsb.ejbCreate();
220             fail();
221         }
222         catch (BeansException ex) {
223             // Ok
224
}
225     }
226
227 }
228
Popular Tags