KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > ejb > access > SimpleRemoteStatelessSessionProxyFactoryBeanTests


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.access;
18
19 import java.lang.reflect.Proxy JavaDoc;
20 import java.rmi.RemoteException JavaDoc;
21
22 import javax.ejb.CreateException JavaDoc;
23 import javax.ejb.EJBHome JavaDoc;
24 import javax.ejb.EJBObject JavaDoc;
25 import javax.naming.NamingException JavaDoc;
26
27 import org.easymock.MockControl;
28
29 import org.springframework.jndi.JndiTemplate;
30 import org.springframework.remoting.RemoteAccessException;
31
32 /**
33  * Tests Business Methods pattern.
34  *
35  * @author Rod Johnson
36  * @since 21.05.2003
37  */

38 public class SimpleRemoteStatelessSessionProxyFactoryBeanTests extends SimpleRemoteSlsbInvokerInterceptorTests {
39
40     protected SimpleRemoteSlsbInvokerInterceptor createInterceptor() {
41         return new SimpleRemoteStatelessSessionProxyFactoryBean();
42     }
43
44     protected Object JavaDoc configuredProxy(SimpleRemoteSlsbInvokerInterceptor si, Class JavaDoc ifc) throws NamingException JavaDoc {
45         SimpleRemoteStatelessSessionProxyFactoryBean fb = (SimpleRemoteStatelessSessionProxyFactoryBean) si;
46         fb.setBusinessInterface(ifc);
47         fb.afterPropertiesSet();
48         return fb.getObject();
49     }
50
51     public void testInvokesMethod() throws Exception JavaDoc {
52         final int value = 11;
53         final String JavaDoc 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 JavaDoc lookup(String JavaDoc name) {
71                 // parameterize
72
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         // Need lifecycle methods
84
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 JavaDoc {
94         final RemoteException JavaDoc rex = new RemoteException JavaDoc();
95         final String JavaDoc jndiName = "foo";
96     
97         MockControl ec = MockControl.createControl(MyEjb.class);
98         MyEjb myEjb = (MyEjb) ec.getMock();
99         myEjb.getValue();
100         ec.setThrowable(rex);
101         // TODO might want to control this behaviour...
102
// Do we really want to call remove after a remote exception?
103
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 JavaDoc lookup(String JavaDoc name) {
115                 // parameterize
116
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         // Need lifecycle methods
128
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 JavaDoc ex) {
137             assertSame("Threw expected RemoteException", rex, ex);
138         }
139         mc.verify();
140         ec.verify();
141     }
142     
143     public void testCreateException() throws Exception JavaDoc {
144         final String JavaDoc jndiName = "foo";
145     
146         final CreateException JavaDoc cex = new CreateException JavaDoc();
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 JavaDoc lookup(String JavaDoc name) {
155                 // parameterize
156
assertTrue(name.equals(jndiName));
157                 return home;
158             }
159         };
160     
161         SimpleRemoteStatelessSessionProxyFactoryBean fb = new SimpleRemoteStatelessSessionProxyFactoryBean();
162         fb.setJndiName(jndiName);
163         // rely on default setting of resourceRef=false, no auto addition of java:/comp/env prefix
164
fb.setBusinessInterface(MyBusinessMethods.class);
165         assertEquals(fb.getBusinessInterface(), MyBusinessMethods.class);
166         fb.setJndiTemplate(jt);
167     
168         // Need lifecycle methods
169
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 JavaDoc ex) {
179             // expected
180
}
181         
182         mc.verify();
183     }
184     
185     public void testCreateExceptionWithLocalBusinessInterface() throws Exception JavaDoc {
186         final String JavaDoc jndiName = "foo";
187
188         final CreateException JavaDoc cex = new CreateException JavaDoc();
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 JavaDoc lookup(String JavaDoc name) {
197                 // parameterize
198
assertTrue(name.equals(jndiName));
199                 return home;
200             }
201         };
202
203         SimpleRemoteStatelessSessionProxyFactoryBean fb = new SimpleRemoteStatelessSessionProxyFactoryBean();
204         fb.setJndiName(jndiName);
205         // rely on default setting of resourceRef=false, no auto addition of java:/comp/env prefix
206
fb.setBusinessInterface(MyLocalBusinessMethods.class);
207         assertEquals(fb.getBusinessInterface(), MyLocalBusinessMethods.class);
208         fb.setJndiTemplate(jt);
209
210         // Need lifecycle methods
211
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 JavaDoc {
228         // Will do JNDI lookup to get home but won't call create
229
// Could actually try to figure out interface from create?
230
final String JavaDoc 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 JavaDoc lookup(String JavaDoc name) throws NamingException JavaDoc {
238                 // parameterize
239
assertTrue(name.equals(jndiName));
240                 return home;
241             }
242         };
243
244         SimpleRemoteStatelessSessionProxyFactoryBean fb = new SimpleRemoteStatelessSessionProxyFactoryBean();
245         fb.setJndiName(jndiName);
246         // rely on default setting of resourceRef=false, no auto addition of java:/comp/env prefix
247
// Don't set business interface
248
fb.setJndiTemplate(jt);
249         
250         // Check it's a singleton
251
assertTrue(fb.isSingleton());
252
253         try {
254             fb.afterPropertiesSet();
255             fail("Should have failed to create EJB");
256         }
257         catch (IllegalArgumentException JavaDoc ex) {
258             // TODO more appropriate exception?
259
assertTrue(ex.getMessage().indexOf("businessInterface") != 1);
260         }
261     
262         // Expect no methods on home
263
mc.verify();
264     }
265     
266     
267     protected static interface MyHome extends EJBHome JavaDoc {
268
269         MyBusinessMethods create() throws CreateException JavaDoc, RemoteException JavaDoc;
270     }
271
272
273     protected static interface MyBusinessMethods {
274
275         int getValue() throws RemoteException JavaDoc;
276     }
277
278
279     protected static interface MyLocalBusinessMethods {
280
281         int getValue();
282     }
283
284
285     protected static interface MyEjb extends EJBObject JavaDoc, MyBusinessMethods {
286         
287     }
288
289 }
290
Popular Tags