1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.ejb.access;
18
19 import java.lang.reflect.Proxy;
20 import javax.ejb.CreateException;
21 import javax.ejb.EJBLocalHome;
22 import javax.ejb.EJBLocalObject;
23 import javax.naming.NamingException;
24
25 import org.junit.Test;
26
27 import org.springframework.jndi.JndiTemplate;
28
29 import static org.junit.Assert.*;
30 import static org.mockito.BDDMockito.*;
31
32
33
34
35
36
37
38 public class LocalStatelessSessionProxyFactoryBeanTests {
39
40 @Test
41 public void testInvokesMethod() throws Exception {
42 final int value = 11;
43 final String jndiName = "foo";
44
45 MyEjb myEjb = mock(MyEjb.class);
46 given(myEjb.getValue()).willReturn(value);
47
48 final MyHome home = mock(MyHome.class);
49 given(home.create()).willReturn(myEjb);
50
51 JndiTemplate jt = new JndiTemplate() {
52 @Override
53 public Object lookup(String name) throws NamingException {
54
55 assertTrue(name.equals("java:comp/env/" + jndiName));
56 return home;
57 }
58 };
59
60 LocalStatelessSessionProxyFactoryBean fb = new LocalStatelessSessionProxyFactoryBean();
61 fb.setJndiName(jndiName);
62 fb.setResourceRef(true);
63 fb.setBusinessInterface(MyBusinessMethods.class);
64 fb.setJndiTemplate(jt);
65
66
67 fb.afterPropertiesSet();
68
69 MyBusinessMethods mbm = (MyBusinessMethods) fb.getObject();
70 assertTrue(Proxy.isProxyClass(mbm.getClass()));
71 assertTrue(mbm.getValue() == value);
72 verify(myEjb).remove();
73 }
74
75 @Test
76 public void testInvokesMethodOnEjb3StyleBean() throws Exception {
77 final int value = 11;
78 final String jndiName = "foo";
79
80 final MyEjb myEjb = mock(MyEjb.class);
81 given(myEjb.getValue()).willReturn(value);
82
83 JndiTemplate jt = new JndiTemplate() {
84 @Override
85 public Object lookup(String name) throws NamingException {
86
87 assertTrue(name.equals("java:comp/env/" + jndiName));
88 return myEjb;
89 }
90 };
91
92 LocalStatelessSessionProxyFactoryBean fb = new LocalStatelessSessionProxyFactoryBean();
93 fb.setJndiName(jndiName);
94 fb.setResourceRef(true);
95 fb.setBusinessInterface(MyBusinessMethods.class);
96 fb.setJndiTemplate(jt);
97
98
99 fb.afterPropertiesSet();
100
101 MyBusinessMethods mbm = (MyBusinessMethods) fb.getObject();
102 assertTrue(Proxy.isProxyClass(mbm.getClass()));
103 assertTrue(mbm.getValue() == value);
104 }
105
106 @Test
107 public void testCreateException() throws Exception {
108 final String jndiName = "foo";
109
110 final CreateException cex = new CreateException();
111 final MyHome home = mock(MyHome.class);
112 given(home.create()).willThrow(cex);
113
114 JndiTemplate jt = new JndiTemplate() {
115 @Override
116 public Object lookup(String name) throws NamingException {
117
118 assertTrue(name.equals(jndiName));
119 return home;
120 }
121 };
122
123 LocalStatelessSessionProxyFactoryBean fb = new LocalStatelessSessionProxyFactoryBean();
124 fb.setJndiName(jndiName);
125 fb.setResourceRef(false);
126 fb.setBusinessInterface(MyBusinessMethods.class);
127 assertEquals(fb.getBusinessInterface(), MyBusinessMethods.class);
128 fb.setJndiTemplate(jt);
129
130
131 fb.afterPropertiesSet();
132
133 MyBusinessMethods mbm = (MyBusinessMethods) fb.getObject();
134 assertTrue(Proxy.isProxyClass(mbm.getClass()));
135
136 try {
137 mbm.getValue();
138 fail("Should have failed to create EJB");
139 }
140 catch (EjbAccessException ex) {
141 assertSame(cex, ex.getCause());
142 }
143 }
144
145 @Test
146 public void testNoBusinessInterfaceSpecified() throws Exception {
147
148
149 final String jndiName = "foo";
150
151 final MyHome home = mock(MyHome.class);
152
153 JndiTemplate jt = new JndiTemplate() {
154 @Override
155 public Object lookup(String name) throws NamingException {
156
157 assertTrue(name.equals("java:comp/env/" + jndiName));
158 return home;
159 }
160 };
161
162 LocalStatelessSessionProxyFactoryBean fb = new LocalStatelessSessionProxyFactoryBean();
163 fb.setJndiName(jndiName);
164 fb.setResourceRef(true);
165
166 fb.setJndiTemplate(jt);
167
168
169 assertTrue(fb.isSingleton());
170
171 try {
172 fb.afterPropertiesSet();
173 fail("Should have failed to create EJB");
174 }
175 catch (IllegalArgumentException ex) {
176
177 assertTrue(ex.getMessage().indexOf("businessInterface") != 1);
178 }
179
180
181 verifyZeroInteractions(home);
182 }
183
184
185 public static interface MyHome extends EJBLocalHome {
186
187 MyBusinessMethods create() throws CreateException;
188 }
189
190
191 public static interface MyBusinessMethods {
192
193 int getValue();
194 }
195
196
197 public static interface MyEjb extends EJBLocalObject, MyBusinessMethods {
198 }
199
200 }