View Javadoc
1   /*
2    * Copyright 2002-2013 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;
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   * @author Rod Johnson
34   * @author Juergen Hoeller
35   * @author Chris Beams
36   * @since 21.05.2003
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  				// parameterize
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  		// Need lifecycle methods
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  				// parameterize
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  		// Need lifecycle methods
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 				// parameterize
118 				assertTrue(name.equals(jndiName));
119 				return home;
120 			}
121 		};
122 
123 		LocalStatelessSessionProxyFactoryBean fb = new LocalStatelessSessionProxyFactoryBean();
124 		fb.setJndiName(jndiName);
125 		fb.setResourceRef(false);	// no java:comp/env prefix
126 		fb.setBusinessInterface(MyBusinessMethods.class);
127 		assertEquals(fb.getBusinessInterface(), MyBusinessMethods.class);
128 		fb.setJndiTemplate(jt);
129 
130 		// Need lifecycle methods
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 		// Will do JNDI lookup to get home but won't call create
148 		// Could actually try to figure out interface from create?
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 				// parameterize
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 		// Don't set business interface
166 		fb.setJndiTemplate(jt);
167 
168 		// Check it's a singleton
169 		assertTrue(fb.isSingleton());
170 
171 		try {
172 			fb.afterPropertiesSet();
173 			fail("Should have failed to create EJB");
174 		}
175 		catch (IllegalArgumentException ex) {
176 			// TODO more appropriate exception?
177 			assertTrue(ex.getMessage().indexOf("businessInterface") != 1);
178 		}
179 
180 		// Expect no methods on home
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 }