KickJava   Java API By Example, From Geeks To Geeks.

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


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

17
18 package org.springframework.ejb.access;
19
20 import javax.ejb.CreateException JavaDoc;
21 import javax.ejb.EJBLocalHome JavaDoc;
22 import javax.ejb.EJBLocalObject JavaDoc;
23 import javax.naming.Context JavaDoc;
24 import javax.naming.NamingException JavaDoc;
25
26 import junit.framework.TestCase;
27 import org.easymock.MockControl;
28
29 import org.springframework.aop.framework.ProxyFactory;
30 import org.springframework.jndi.JndiTemplate;
31
32 /**
33  * @author Rod Johnson
34 */

35 public class LocalSlsbInvokerInterceptorTests extends TestCase {
36
37     /**
38      * Test that it performs the correct lookup.
39      */

40     public void testPerformsLookup() throws Exception JavaDoc {
41         MockControl ejbControl = MockControl.createControl(LocalInterfaceWithBusinessMethods.class);
42         final LocalInterfaceWithBusinessMethods ejb = (LocalInterfaceWithBusinessMethods) ejbControl.getMock();
43         ejbControl.replay();
44         
45         final String JavaDoc jndiName= "foobar";
46         MockControl contextControl = contextControl(jndiName, ejb);
47         
48         LocalSlsbInvokerInterceptor si = configuredInterceptor(contextControl, jndiName);
49         
50         contextControl.verify();
51     }
52     
53     public void testLookupFailure() throws Exception JavaDoc {
54         final NamingException JavaDoc nex = new NamingException JavaDoc();
55         final String JavaDoc jndiName= "foobar";
56         JndiTemplate jt = new JndiTemplate() {
57             public Object JavaDoc lookup(String JavaDoc name) throws NamingException JavaDoc {
58                 assertTrue(jndiName.equals(name));
59                 throw nex;
60             }
61         };
62     
63         LocalSlsbInvokerInterceptor si = new LocalSlsbInvokerInterceptor();
64         si.setJndiName("foobar");
65         // default resourceRef=false should cause this to fail, as java:/comp/env will not
66
// automatically be added
67
si.setJndiTemplate(jt);
68         try {
69             si.afterPropertiesSet();
70             fail("Should have failed with naming exception");
71         }
72         catch (NamingException JavaDoc ex) {
73             assertTrue(ex == nex);
74         }
75     }
76     
77     public void testInvokesMethodOnEjbInstance() throws Exception JavaDoc {
78         Object JavaDoc retVal = new Object JavaDoc();
79         MockControl ejbControl = MockControl.createControl(LocalInterfaceWithBusinessMethods.class);
80         final LocalInterfaceWithBusinessMethods ejb = (LocalInterfaceWithBusinessMethods) ejbControl.getMock();
81         ejb.targetMethod();
82         ejbControl.setReturnValue(retVal, 1);
83         ejb.remove();
84         ejbControl.setVoidCallable(1);
85         ejbControl.replay();
86     
87         final String JavaDoc jndiName= "foobar";
88         MockControl contextControl = contextControl(jndiName, ejb);
89     
90         LocalSlsbInvokerInterceptor si = configuredInterceptor(contextControl, jndiName);
91     
92         ProxyFactory pf = new ProxyFactory(new Class JavaDoc[] { BusinessMethods.class } );
93         pf.addAdvice(si);
94         BusinessMethods target = (BusinessMethods) pf.getProxy();
95     
96         assertTrue(target.targetMethod() == retVal);
97     
98         contextControl.verify();
99         ejbControl.verify();
100     }
101     
102     public void testInvokesMethodOnEjbInstanceWithSeparateBusinessMethods() throws Exception JavaDoc {
103         Object JavaDoc retVal = new Object JavaDoc();
104         MockControl ejbControl = MockControl.createControl(LocalInterface.class);
105         final LocalInterface ejb = (LocalInterface) ejbControl.getMock();
106         ejb.targetMethod();
107         ejbControl.setReturnValue(retVal, 1);
108         ejb.remove();
109         ejbControl.setVoidCallable(1);
110         ejbControl.replay();
111
112         final String JavaDoc jndiName= "foobar";
113         MockControl contextControl = contextControl(jndiName, ejb);
114
115         LocalSlsbInvokerInterceptor si = configuredInterceptor(contextControl, jndiName);
116
117         ProxyFactory pf = new ProxyFactory(new Class JavaDoc[] { BusinessMethods.class } );
118         pf.addAdvice(si);
119         BusinessMethods target = (BusinessMethods) pf.getProxy();
120
121         assertTrue(target.targetMethod() == retVal);
122
123         contextControl.verify();
124         ejbControl.verify();
125     }
126
127     private void testException(Exception JavaDoc expected) throws Exception JavaDoc {
128         MockControl ejbControl = MockControl.createControl(LocalInterfaceWithBusinessMethods.class);
129         final LocalInterfaceWithBusinessMethods ejb = (LocalInterfaceWithBusinessMethods) ejbControl.getMock();
130         ejb.targetMethod();
131         ejbControl.setThrowable(expected);
132         ejbControl.replay();
133
134         final String JavaDoc jndiName= "foobar";
135         MockControl contextControl = contextControl(jndiName, ejb);
136
137         LocalSlsbInvokerInterceptor si = configuredInterceptor(contextControl, jndiName);
138
139         ProxyFactory pf = new ProxyFactory(new Class JavaDoc[] { LocalInterfaceWithBusinessMethods.class } );
140         pf.addAdvice(si);
141         LocalInterfaceWithBusinessMethods target = (LocalInterfaceWithBusinessMethods) pf.getProxy();
142
143         try {
144             target.targetMethod();
145             fail("Should have thrown exception");
146         }
147         catch (Exception JavaDoc thrown) {
148             assertTrue(thrown == expected);
149         }
150
151         contextControl.verify();
152         ejbControl.verify();
153     }
154     
155     public void testApplicationException() throws Exception JavaDoc {
156         testException(new ApplicationException());
157     }
158
159     protected MockControl contextControl(final String JavaDoc jndiName, final EJBLocalObject JavaDoc ejbInstance)
160             throws Exception JavaDoc {
161
162         MockControl homeControl = MockControl.createControl(SlsbHome.class);
163         final SlsbHome mockHome = (SlsbHome) homeControl.getMock();
164         mockHome.create();
165         homeControl.setReturnValue(ejbInstance, 1);
166         homeControl.replay();
167         
168         MockControl ctxControl = MockControl.createControl(Context JavaDoc.class);
169         final Context JavaDoc mockCtx = (Context JavaDoc) ctxControl.getMock();
170         
171         mockCtx.lookup("java:comp/env/" + jndiName);
172         ctxControl.setReturnValue(mockHome);
173         mockCtx.close();
174         ctxControl.setVoidCallable();
175         ctxControl.replay();
176         return ctxControl;
177     }
178         
179     protected LocalSlsbInvokerInterceptor configuredInterceptor(MockControl contextControl, final String JavaDoc jndiName)
180             throws Exception JavaDoc {
181
182         final Context JavaDoc mockCtx = (Context JavaDoc) contextControl.getMock();
183         LocalSlsbInvokerInterceptor si = new LocalSlsbInvokerInterceptor();
184         si.setJndiTemplate(new JndiTemplate() {
185             protected Context JavaDoc createInitialContext() throws NamingException JavaDoc {
186                 return mockCtx;
187             }
188         });
189         si.setJndiName(jndiName);
190         si.setResourceRef(true);
191         si.afterPropertiesSet();
192         
193         return si;
194     }
195     
196     
197     /**
198      * Needed so that we can mock the create() method.
199      */

200     private interface SlsbHome extends EJBLocalHome JavaDoc {
201
202         LocalInterface create() throws CreateException JavaDoc;
203     }
204
205
206     private interface BusinessMethods {
207
208         Object JavaDoc targetMethod() throws ApplicationException;
209     }
210
211
212     private interface LocalInterface extends EJBLocalObject JavaDoc {
213
214         Object JavaDoc targetMethod() throws ApplicationException;
215     }
216
217
218     private interface LocalInterfaceWithBusinessMethods extends LocalInterface, BusinessMethods {
219     }
220
221
222     private class ApplicationException extends Exception JavaDoc {
223
224         public ApplicationException() {
225             super("appException");
226         }
227     }
228  
229 }
230
Popular Tags