KickJava   Java API By Example, From Geeks To Geeks.

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


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
21 import javax.ejb.CreateException JavaDoc;
22 import javax.ejb.EJBLocalHome JavaDoc;
23 import javax.ejb.EJBLocalObject JavaDoc;
24 import javax.naming.NamingException JavaDoc;
25
26 import junit.framework.TestCase;
27 import org.aopalliance.aop.AspectException;
28 import org.easymock.MockControl;
29
30 import org.springframework.jndi.JndiTemplate;
31
32 /**
33  * Tests Business Methods pattern
34  * @author Rod Johnson
35  * @since 21.05.2003
36  */

37 public class LocalStatelessSessionProxyFactoryBeanTests extends TestCase {
38
39     public void testInvokesMethod() throws Exception JavaDoc {
40         final int value = 11;
41         final String JavaDoc jndiName = "foo";
42         
43         MockControl ec = MockControl.createControl(MyEjb.class);
44         MyEjb myEjb = (MyEjb) ec.getMock();
45         myEjb.getValue();
46         ec.setReturnValue(value, 1);
47         ec.replay();
48         
49         MockControl mc = MockControl.createControl(MyHome.class);
50         final MyHome home = (MyHome) mc.getMock();
51         home.create();
52         mc.setReturnValue(myEjb, 1);
53         mc.replay();
54         
55         JndiTemplate jt = new JndiTemplate() {
56             public Object JavaDoc lookup(String JavaDoc name) throws NamingException JavaDoc {
57                 // parameterize
58
assertTrue(name.equals("java:comp/env/" + jndiName));
59                 return home;
60             }
61         };
62         
63         LocalStatelessSessionProxyFactoryBean fb = new LocalStatelessSessionProxyFactoryBean();
64         fb.setJndiName(jndiName);
65         fb.setResourceRef(true);
66         fb.setBusinessInterface(MyBusinessMethods.class);
67         fb.setJndiTemplate(jt);
68         
69         // Need lifecycle methods
70
fb.afterPropertiesSet();
71
72         MyBusinessMethods mbm = (MyBusinessMethods) fb.getObject();
73         assertTrue(Proxy.isProxyClass(mbm.getClass()));
74         assertTrue(mbm.getValue() == value);
75         mc.verify();
76         ec.verify();
77     }
78     
79     
80     public void testCreateException() throws Exception JavaDoc {
81         final String JavaDoc jndiName = "foo";
82     
83         final CreateException JavaDoc cex = new CreateException JavaDoc();
84         MockControl mc = MockControl.createControl(MyHome.class);
85         final MyHome home = (MyHome) mc.getMock();
86         home.create();
87         mc.setThrowable(cex);
88         mc.replay();
89     
90         JndiTemplate jt = new JndiTemplate() {
91             public Object JavaDoc lookup(String JavaDoc name) throws NamingException JavaDoc {
92                 // parameterize
93
assertTrue(name.equals(jndiName));
94                 return home;
95             }
96         };
97     
98         LocalStatelessSessionProxyFactoryBean fb = new LocalStatelessSessionProxyFactoryBean();
99         fb.setJndiName(jndiName);
100         fb.setResourceRef(false); // no java:comp/env prefix
101
fb.setBusinessInterface(MyBusinessMethods.class);
102         assertEquals(fb.getBusinessInterface(), MyBusinessMethods.class);
103         fb.setJndiTemplate(jt);
104     
105         // Need lifecycle methods
106
fb.afterPropertiesSet();
107
108         MyBusinessMethods mbm = (MyBusinessMethods) fb.getObject();
109         assertTrue(Proxy.isProxyClass(mbm.getClass()));
110         
111         try {
112             mbm.getValue();
113             fail("Should have failed to create EJB");
114         }
115         catch (AspectException ex) {
116             assertSame(cex, ex.getCause());
117         }
118         
119         mc.verify();
120     }
121     
122     public void testNoBusinessInterfaceSpecified() throws Exception JavaDoc {
123         // Will do JNDI lookup to get home but won't call create
124
// Could actually try to figure out interface from create?
125
final String JavaDoc jndiName = "foo";
126
127         MockControl mc = MockControl.createControl(MyHome.class);
128         final MyHome home = (MyHome) mc.getMock();
129         mc.replay();
130
131         JndiTemplate jt = new JndiTemplate() {
132             public Object JavaDoc lookup(String JavaDoc name) throws NamingException JavaDoc {
133                 // parameterize
134
assertTrue(name.equals("java:comp/env/" + jndiName));
135                 return home;
136             }
137         };
138
139         SimpleRemoteStatelessSessionProxyFactoryBean fb = new SimpleRemoteStatelessSessionProxyFactoryBean();
140         fb.setJndiName(jndiName);
141         fb.setResourceRef(true);
142         // Don't set business interface
143
fb.setJndiTemplate(jt);
144
145         // Check it's a singleton
146
assertTrue(fb.isSingleton());
147
148         try {
149             fb.afterPropertiesSet();
150             fail("Should have failed to create EJB");
151         }
152         catch (IllegalArgumentException JavaDoc ex) {
153             // TODO more appropriate exception?
154
assertTrue(ex.getMessage().indexOf("businessInterface") != 1);
155         }
156
157         // Expect no methods on home
158
mc.verify();
159     }
160     
161     
162     public static interface MyHome extends EJBLocalHome JavaDoc {
163
164         MyBusinessMethods create() throws CreateException JavaDoc;
165     }
166
167
168     public static interface MyBusinessMethods {
169
170         int getValue();
171     }
172
173
174     public static interface MyEjb extends EJBLocalObject JavaDoc, MyBusinessMethods {
175     }
176
177 }
178
Popular Tags