SimpleRemoteStatelessSessionProxyFactoryBeanTests.java
上传用户:jiancairen
上传日期:2007-08-27
资源大小:26458k
文件大小:8k
源码类别:

Java编程

开发平台:

Java

  1. /*
  2.  * Copyright 2002-2004 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. package org.springframework.ejb.access;
  17. import java.lang.reflect.Proxy;
  18. import java.rmi.RemoteException;
  19. import javax.ejb.CreateException;
  20. import javax.ejb.EJBHome;
  21. import javax.ejb.EJBObject;
  22. import javax.naming.NamingException;
  23. import junit.framework.TestCase;
  24. import org.easymock.MockControl;
  25. import org.springframework.jndi.JndiTemplate;
  26. import org.springframework.remoting.RemoteAccessException;
  27. /**
  28.  * Tests Business Methods pattern
  29.  * @author Rod Johnson
  30.  * @since 21-May-2003
  31.  */
  32. public class SimpleRemoteStatelessSessionProxyFactoryBeanTests extends TestCase {
  33. public void testInvokesMethod() throws Exception {
  34. final int value = 11;
  35. final String jndiName = "foo";
  36. MockControl ec = MockControl.createControl(MyEjb.class);
  37. MyEjb myEjb = (MyEjb) ec.getMock();
  38. myEjb.getValue();
  39. ec.setReturnValue(value, 1);
  40. ec.replay();
  41. MockControl mc = MockControl.createControl(MyHome.class);
  42. final MyHome home = (MyHome) mc.getMock();
  43. home.create();
  44. mc.setReturnValue(myEjb, 1);
  45. mc.replay();
  46. JndiTemplate jt = new JndiTemplate() {
  47. public Object lookup(String name) throws NamingException {
  48. // parameterize
  49. assertTrue(name.equals("java:comp/env/" + jndiName));
  50. return home;
  51. }
  52. };
  53. SimpleRemoteStatelessSessionProxyFactoryBean fb = new SimpleRemoteStatelessSessionProxyFactoryBean();
  54. fb.setJndiName(jndiName);
  55. fb.setResourceRef(true);
  56. fb.setBusinessInterface(MyBusinessMethods.class);
  57. fb.setJndiTemplate(jt);
  58. // Need lifecycle methods
  59. fb.afterPropertiesSet();
  60. MyBusinessMethods mbm = (MyBusinessMethods) fb.getObject();
  61. assertTrue(Proxy.isProxyClass(mbm.getClass()));
  62. assertTrue(mbm.getValue() == value);
  63. mc.verify();
  64. ec.verify();
  65. }
  66. public void testRemoteException() throws Exception {
  67. final RemoteException rex = new RemoteException();
  68. final String jndiName = "foo";
  69. MockControl ec = MockControl.createControl(MyEjb.class);
  70. MyEjb myEjb = (MyEjb) ec.getMock();
  71. myEjb.getValue();
  72. ec.setThrowable(rex);
  73. ec.replay();
  74. MockControl mc = MockControl.createControl(MyHome.class);
  75. final MyHome home = (MyHome) mc.getMock();
  76. home.create();
  77. mc.setReturnValue(myEjb, 1);
  78. mc.replay();
  79. JndiTemplate jt = new JndiTemplate() {
  80. public Object lookup(String name) throws NamingException {
  81. // parameterize
  82. assertTrue(name.equals("java:comp/env/" + jndiName));
  83. return home;
  84. }
  85. };
  86. SimpleRemoteStatelessSessionProxyFactoryBean fb = new SimpleRemoteStatelessSessionProxyFactoryBean();
  87. fb.setJndiName(jndiName);
  88. fb.setResourceRef(true);
  89. fb.setBusinessInterface(MyBusinessMethods.class);
  90. fb.setJndiTemplate(jt);
  91. // Need lifecycle methods
  92. fb.afterPropertiesSet();
  93. MyBusinessMethods mbm = (MyBusinessMethods) fb.getObject();
  94. assertTrue(Proxy.isProxyClass(mbm.getClass()));
  95. try {
  96. mbm.getValue();
  97. fail("Should've thrown remote exception");
  98. }
  99. catch (RemoteException ex) {
  100. assertTrue(ex == rex);
  101. }
  102. mc.verify();
  103. ec.verify();
  104. }
  105. public void testCreateException() throws Exception {
  106. final String jndiName = "foo";
  107. final CreateException cex = new CreateException();
  108. MockControl mc = MockControl.createControl(MyHome.class);
  109. final MyHome home = (MyHome) mc.getMock();
  110. home.create();
  111. mc.setThrowable(cex);
  112. mc.replay();
  113. JndiTemplate jt = new JndiTemplate() {
  114. public Object lookup(String name) throws NamingException {
  115. // parameterize
  116. assertTrue(name.equals(jndiName));
  117. return home;
  118. }
  119. };
  120. SimpleRemoteStatelessSessionProxyFactoryBean fb = new SimpleRemoteStatelessSessionProxyFactoryBean();
  121. fb.setJndiName(jndiName);
  122. // rely on default setting of resourceRef=false, no auto addition of java:/comp/env prefix
  123. fb.setBusinessInterface(MyBusinessMethods.class);
  124. assertEquals(fb.getBusinessInterface(), MyBusinessMethods.class);
  125. fb.setJndiTemplate(jt);
  126. // Need lifecycle methods
  127. fb.afterPropertiesSet();
  128. MyBusinessMethods mbm = (MyBusinessMethods) fb.getObject();
  129. assertTrue(Proxy.isProxyClass(mbm.getClass()));
  130. try {
  131. mbm.getValue();
  132. fail("Should have failed to create EJB");
  133. }
  134. catch (RemoteException ex) {
  135. assertTrue(ex.getCause() == cex);
  136. }
  137. mc.verify();
  138. }
  139. public void testCreateExceptionWithLocalBusinessInterface() throws Exception {
  140. final String jndiName = "foo";
  141. final CreateException cex = new CreateException();
  142. MockControl mc = MockControl.createControl(MyHome.class);
  143. final MyHome home = (MyHome) mc.getMock();
  144. home.create();
  145. mc.setThrowable(cex);
  146. mc.replay();
  147. JndiTemplate jt = new JndiTemplate() {
  148. public Object lookup(String name) throws NamingException {
  149. // parameterize
  150. assertTrue(name.equals(jndiName));
  151. return home;
  152. }
  153. };
  154. SimpleRemoteStatelessSessionProxyFactoryBean fb = new SimpleRemoteStatelessSessionProxyFactoryBean();
  155. fb.setJndiName(jndiName);
  156. // rely on default setting of resourceRef=false, no auto addition of java:/comp/env prefix
  157. fb.setBusinessInterface(MyLocalBusinessMethods.class);
  158. assertEquals(fb.getBusinessInterface(), MyLocalBusinessMethods.class);
  159. fb.setJndiTemplate(jt);
  160. // Need lifecycle methods
  161. fb.afterPropertiesSet();
  162. MyLocalBusinessMethods mbm = (MyLocalBusinessMethods) fb.getObject();
  163. assertTrue(Proxy.isProxyClass(mbm.getClass()));
  164. try {
  165. mbm.getValue();
  166. fail("Should have failed to create EJB");
  167. }
  168. catch (RemoteAccessException ex) {
  169. assertTrue(ex.getCause() == cex);
  170. }
  171. mc.verify();
  172. }
  173. public void testNoBusinessInterfaceSpecified() throws Exception {
  174. // Will do JNDI lookup to get home but won't call create
  175. // Could actually try to figure out interface from create?
  176. final String jndiName = "foo";
  177. MockControl mc = MockControl.createControl(MyHome.class);
  178. final MyHome home = (MyHome) mc.getMock();
  179. mc.replay();
  180. JndiTemplate jt = new JndiTemplate() {
  181. public Object lookup(String name) throws NamingException {
  182. // parameterize
  183. assertTrue(name.equals(jndiName));
  184. return home;
  185. }
  186. };
  187. SimpleRemoteStatelessSessionProxyFactoryBean fb = new SimpleRemoteStatelessSessionProxyFactoryBean();
  188. fb.setJndiName(jndiName);
  189. // rely on default setting of resourceRef=false, no auto addition of java:/comp/env prefix
  190. // Don't set business interface
  191. fb.setJndiTemplate(jt);
  192. // Check it's a singleton
  193. assertTrue(fb.isSingleton());
  194. try {
  195. fb.afterPropertiesSet();
  196. fail("Should have failed to create EJB");
  197. }
  198. catch (IllegalArgumentException ex) {
  199. // TODO more appropriate exception?
  200. assertTrue(ex.getMessage().indexOf("businessInterface") != 1);
  201. }
  202. // Expect no methods on home
  203. mc.verify();
  204. }
  205. protected static interface MyHome extends EJBHome {
  206. MyBusinessMethods create() throws CreateException, RemoteException;
  207. }
  208. protected static interface MyBusinessMethods  {
  209. int getValue() throws RemoteException;
  210. }
  211. protected static interface MyLocalBusinessMethods  {
  212. int getValue();
  213. }
  214. protected static interface MyEjb extends EJBObject, MyBusinessMethods {
  215. }
  216. }