KickJava   Java API By Example, From Geeks To Geeks.

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


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.rmi.ConnectException 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.Context JavaDoc;
26 import javax.naming.NamingException JavaDoc;
27
28 import junit.framework.TestCase;
29 import org.easymock.MockControl;
30
31 import org.springframework.aop.framework.ProxyFactory;
32 import org.springframework.jndi.JndiTemplate;
33 import org.springframework.remoting.RemoteAccessException;
34
35 /**
36  * @author Rod Johnson
37  */

38 public class SimpleRemoteSlsbInvokerInterceptorTests extends TestCase {
39
40     private MockControl contextControl(
41             String JavaDoc jndiName, RemoteInterface ejbInstance, int createCount, int lookupCount) throws Exception JavaDoc {
42
43         MockControl homeControl = MockControl.createControl(SlsbHome.class);
44         final SlsbHome mockHome = (SlsbHome) homeControl.getMock();
45         mockHome.create();
46         homeControl.setReturnValue(ejbInstance, createCount);
47         homeControl.replay();
48
49         MockControl ctxControl = MockControl.createControl(Context JavaDoc.class);
50         final Context JavaDoc mockCtx = (Context JavaDoc) ctxControl.getMock();
51
52         mockCtx.lookup("java:comp/env/" + jndiName);
53         ctxControl.setReturnValue(mockHome, lookupCount);
54         mockCtx.close();
55         ctxControl.setVoidCallable(lookupCount);
56         ctxControl.replay();
57
58         return ctxControl;
59     }
60
61     private SimpleRemoteSlsbInvokerInterceptor configuredInterceptor(
62             MockControl contextControl, final String JavaDoc jndiName) throws Exception JavaDoc {
63
64         final Context JavaDoc mockCtx = (Context JavaDoc) contextControl.getMock();
65         SimpleRemoteSlsbInvokerInterceptor si = createInterceptor();
66         si.setJndiTemplate(new JndiTemplate() {
67             protected Context JavaDoc createInitialContext() {
68                 return mockCtx;
69             }
70         });
71         si.setResourceRef(true);
72         si.setJndiName(jndiName);
73
74         return si;
75     }
76
77     protected SimpleRemoteSlsbInvokerInterceptor createInterceptor() {
78         return new SimpleRemoteSlsbInvokerInterceptor();
79     }
80
81     protected Object JavaDoc configuredProxy(SimpleRemoteSlsbInvokerInterceptor si, Class JavaDoc ifc) throws NamingException JavaDoc {
82         si.afterPropertiesSet();
83         ProxyFactory pf = new ProxyFactory(new Class JavaDoc[] { ifc } );
84         pf.addAdvice(si);
85         return pf.getProxy();
86     }
87
88
89     /**
90      * Test that it performs the correct lookup.
91      */

92     public void testPerformsLookup() throws Exception JavaDoc {
93         MockControl ejbControl = MockControl.createControl(RemoteInterface.class);
94         final RemoteInterface ejb = (RemoteInterface) ejbControl.getMock();
95         ejbControl.replay();
96         
97         final String JavaDoc jndiName= "foobar";
98         MockControl contextControl = contextControl(jndiName, ejb, 1, 1);
99         
100         SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(contextControl, jndiName);
101         RemoteInterface target = (RemoteInterface) configuredProxy(si, RemoteInterface.class);
102
103         contextControl.verify();
104     }
105
106     public void testLookupFailure() throws Exception JavaDoc {
107         final NamingException JavaDoc nex = new NamingException JavaDoc();
108         final String JavaDoc jndiName= "foobar";
109         JndiTemplate jt = new JndiTemplate() {
110             public Object JavaDoc lookup(String JavaDoc name) throws NamingException JavaDoc {
111                 assertTrue(jndiName.equals(name));
112                 throw nex;
113             }
114         };
115
116         SimpleRemoteSlsbInvokerInterceptor si = new SimpleRemoteSlsbInvokerInterceptor();
117         si.setJndiName("foobar");
118         // default resourceRef=false should cause this to fail, as java:/comp/env will not
119
// automatically be added
120
si.setJndiTemplate(jt);
121         try {
122             si.afterPropertiesSet();
123             fail("Should have failed with naming exception");
124         }
125         catch (NamingException JavaDoc ex) {
126             assertTrue(ex == nex);
127         }
128     }
129     
130     public void testInvokesMethodOnEjbInstance() throws Exception JavaDoc {
131         doTestInvokesMethodOnEjbInstance(true, true);
132     }
133
134     public void testInvokesMethodOnEjbInstanceWithLazyLookup() throws Exception JavaDoc {
135         doTestInvokesMethodOnEjbInstance(false, true);
136     }
137
138     public void testInvokesMethodOnEjbInstanceWithLazyLookupAndNoCache() throws Exception JavaDoc {
139         doTestInvokesMethodOnEjbInstance(false, false);
140     }
141
142     public void testInvokesMethodOnEjbInstanceWithNoCache() throws Exception JavaDoc {
143         doTestInvokesMethodOnEjbInstance(true, false);
144     }
145
146     private void doTestInvokesMethodOnEjbInstance(boolean lookupHomeOnStartup, boolean cacheHome) throws Exception JavaDoc {
147         Object JavaDoc retVal = new Object JavaDoc();
148         MockControl ejbControl = MockControl.createControl(RemoteInterface.class);
149         final RemoteInterface ejb = (RemoteInterface) ejbControl.getMock();
150         ejb.targetMethod();
151         ejbControl.setReturnValue(retVal, 2);
152         ejb.remove();
153         ejbControl.setVoidCallable(2);
154         ejbControl.replay();
155
156         int lookupCount = 1;
157         if (!cacheHome) {
158             lookupCount++;
159             if (lookupHomeOnStartup) {
160                 lookupCount++;
161             }
162         }
163
164         final String JavaDoc jndiName= "foobar";
165         MockControl contextControl = contextControl(jndiName, ejb, 2, lookupCount);
166     
167         SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(contextControl, jndiName);
168         si.setLookupHomeOnStartup(lookupHomeOnStartup);
169         si.setCacheHome(cacheHome);
170
171         RemoteInterface target = (RemoteInterface) configuredProxy(si, RemoteInterface.class);
172         assertTrue(target.targetMethod() == retVal);
173         assertTrue(target.targetMethod() == retVal);
174
175         contextControl.verify();
176         ejbControl.verify();
177     }
178     
179     public void testInvokesMethodOnEjbInstanceWithHomeInterface() throws Exception JavaDoc {
180         Object JavaDoc retVal = new Object JavaDoc();
181         MockControl ejbControl = MockControl.createControl(RemoteInterface.class);
182         final RemoteInterface ejb = (RemoteInterface) ejbControl.getMock();
183         ejb.targetMethod();
184         ejbControl.setReturnValue(retVal, 1);
185         ejb.remove();
186         ejbControl.setVoidCallable(1);
187         ejbControl.replay();
188
189         final String JavaDoc jndiName= "foobar";
190         MockControl contextControl = contextControl(jndiName, ejb, 1, 1);
191
192         SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(contextControl, jndiName);
193         si.setHomeInterface(SlsbHome.class);
194
195         RemoteInterface target = (RemoteInterface) configuredProxy(si, RemoteInterface.class);
196         assertTrue(target.targetMethod() == retVal);
197
198         contextControl.verify();
199         ejbControl.verify();
200     }
201
202     public void testInvokesMethodOnEjbInstanceWithRemoteException() throws Exception JavaDoc {
203         MockControl ejbControl = MockControl.createControl(RemoteInterface.class);
204         final RemoteInterface ejb = (RemoteInterface) ejbControl.getMock();
205         ejb.targetMethod();
206         ejbControl.setThrowable(new RemoteException JavaDoc(), 1);
207         ejb.remove();
208         ejbControl.setVoidCallable(1);
209         ejbControl.replay();
210
211         final String JavaDoc jndiName= "foobar";
212         MockControl contextControl = contextControl(jndiName, ejb, 1, 1);
213
214         SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(contextControl, jndiName);
215
216         RemoteInterface target = (RemoteInterface) configuredProxy(si, RemoteInterface.class);
217         try {
218             target.targetMethod();
219             fail("Should have thrown RemoteException");
220         }
221         catch (RemoteException JavaDoc ex) {
222             // expected
223
}
224
225         contextControl.verify();
226         ejbControl.verify();
227     }
228
229     public void testInvokesMethodOnEjbInstanceWithConnectExceptionWithRefresh() throws Exception JavaDoc {
230         doTestInvokesMethodOnEjbInstanceWithConnectExceptionWithRefresh(true, true);
231     }
232
233     public void testInvokesMethodOnEjbInstanceWithConnectExceptionWithRefreshAndLazyLookup() throws Exception JavaDoc {
234         doTestInvokesMethodOnEjbInstanceWithConnectExceptionWithRefresh(false, true);
235     }
236
237     public void testInvokesMethodOnEjbInstanceWithConnectExceptionWithRefreshAndLazyLookupAndNoCache() throws Exception JavaDoc {
238         doTestInvokesMethodOnEjbInstanceWithConnectExceptionWithRefresh(false, false);
239     }
240
241     public void testInvokesMethodOnEjbInstanceWithConnectExceptionWithRefreshAndNoCache() throws Exception JavaDoc {
242         doTestInvokesMethodOnEjbInstanceWithConnectExceptionWithRefresh(true, false);
243     }
244
245     private void doTestInvokesMethodOnEjbInstanceWithConnectExceptionWithRefresh(
246             boolean lookupHomeOnStartup, boolean cacheHome) throws Exception JavaDoc {
247
248         MockControl ejbControl = MockControl.createControl(RemoteInterface.class);
249         final RemoteInterface ejb = (RemoteInterface) ejbControl.getMock();
250         ejb.targetMethod();
251         ejbControl.setThrowable(new ConnectException JavaDoc(""), 2);
252         ejb.remove();
253         ejbControl.setVoidCallable(2);
254         ejbControl.replay();
255
256         int lookupCount = 2;
257         if (!cacheHome) {
258             lookupCount++;
259             if (lookupHomeOnStartup) {
260                 lookupCount++;
261             }
262         }
263
264         final String JavaDoc jndiName= "foobar";
265         MockControl contextControl = contextControl(jndiName, ejb, 2, lookupCount);
266
267         SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(contextControl, jndiName);
268         si.setRefreshHomeOnConnectFailure(true);
269         si.setLookupHomeOnStartup(lookupHomeOnStartup);
270         si.setCacheHome(cacheHome);
271
272         RemoteInterface target = (RemoteInterface) configuredProxy(si, RemoteInterface.class);
273         try {
274             target.targetMethod();
275             fail("Should have thrown RemoteException");
276         }
277         catch (ConnectException JavaDoc ex) {
278             // expected
279
}
280
281         contextControl.verify();
282         ejbControl.verify();
283     }
284
285     public void testInvokesMethodOnEjbInstanceWithBusinessInterface() throws Exception JavaDoc {
286         Object JavaDoc retVal = new Object JavaDoc();
287         MockControl ejbControl = MockControl.createControl(RemoteInterface.class);
288         final RemoteInterface ejb = (RemoteInterface) ejbControl.getMock();
289         ejb.targetMethod();
290         ejbControl.setReturnValue(retVal, 1);
291         ejbControl.replay();
292
293         final String JavaDoc jndiName= "foobar";
294         MockControl contextControl = contextControl(jndiName, ejb, 1, 1);
295
296         SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(contextControl, jndiName);
297
298         BusinessInterface target = (BusinessInterface) configuredProxy(si, BusinessInterface.class);
299         assertTrue(target.targetMethod() == retVal);
300
301         contextControl.verify();
302         ejbControl.verify();
303     }
304
305     public void testInvokesMethodOnEjbInstanceWithBusinessInterfaceWithRemoteException() throws Exception JavaDoc {
306         MockControl ejbControl = MockControl.createControl(RemoteInterface.class);
307         final RemoteInterface ejb = (RemoteInterface) ejbControl.getMock();
308         ejb.targetMethod();
309         ejbControl.setThrowable(new RemoteException JavaDoc(), 1);
310         ejbControl.replay();
311
312         final String JavaDoc jndiName= "foobar";
313         MockControl contextControl = contextControl(jndiName, ejb, 1, 1);
314
315         SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(contextControl, jndiName);
316
317         BusinessInterface target = (BusinessInterface) configuredProxy(si, BusinessInterface.class);
318         try {
319             target.targetMethod();
320             fail("Should have thrown RemoteAccessException");
321         }
322         catch (RemoteAccessException ex) {
323             // expected
324
}
325
326         contextControl.verify();
327         ejbControl.verify();
328     }
329
330     public void testApplicationException() throws Exception JavaDoc {
331         doTestException(new ApplicationException());
332     }
333
334     public void testRemoteException() throws Exception JavaDoc {
335         doTestException(new RemoteException JavaDoc());
336     }
337
338     private void doTestException(Exception JavaDoc expected) throws Exception JavaDoc {
339         MockControl ejbControl = MockControl.createControl(RemoteInterface.class);
340         final RemoteInterface ejb = (RemoteInterface) ejbControl.getMock();
341         ejb.targetMethod();
342         ejbControl.setThrowable(expected);
343         ejbControl.replay();
344
345         final String JavaDoc jndiName= "foobar";
346         MockControl contextControl = contextControl(jndiName, ejb, 1, 1);
347
348         SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(contextControl, jndiName);
349
350         RemoteInterface target = (RemoteInterface) configuredProxy(si, RemoteInterface.class);
351         try {
352             target.targetMethod();
353             fail("Should have thrown remote exception");
354         }
355         catch (Exception JavaDoc thrown) {
356             assertTrue(thrown == expected);
357         }
358
359         contextControl.verify();
360         ejbControl.verify();
361     }
362
363
364     /**
365      * Needed so that we can mock create() method.
366      */

367     protected interface SlsbHome extends EJBHome JavaDoc {
368
369         EJBObject JavaDoc create() throws RemoteException JavaDoc, CreateException JavaDoc;
370     }
371
372
373     protected interface RemoteInterface extends EJBObject JavaDoc {
374
375         // Also business exception!?
376
Object JavaDoc targetMethod() throws RemoteException JavaDoc, ApplicationException;
377     }
378
379
380     protected interface BusinessInterface {
381
382         Object JavaDoc targetMethod() throws ApplicationException;
383     }
384
385
386     protected class ApplicationException extends Exception JavaDoc {
387
388         public ApplicationException() {
389             super("appException");
390         }
391     }
392
393 }
394
Popular Tags