LocalSlsbInvokerInterceptorTests.java
Upload User: jiancairen
Upload Date: 2007-08-27
Package Size: 26458k
Code Size: 6k
Category:

Java Develop

Development Platform:

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 javax.ejb.CreateException;
  18. import javax.ejb.EJBLocalHome;
  19. import javax.ejb.EJBLocalObject;
  20. import javax.naming.Context;
  21. import javax.naming.NamingException;
  22. import junit.framework.TestCase;
  23. import org.easymock.MockControl;
  24. import org.springframework.aop.framework.ProxyFactory;
  25. import org.springframework.jndi.JndiTemplate;
  26. /**
  27.  * @author Rod Johnson
  28. */
  29. public class LocalSlsbInvokerInterceptorTests extends TestCase {
  30. /**
  31.  * Test that it performs the correct lookup.
  32.  */
  33. public void testPerformsLookup() throws Exception {
  34. MockControl ejbControl = MockControl.createControl(LocalInterface.class);
  35. final LocalInterface ejb = (LocalInterface) ejbControl.getMock();
  36. ejbControl.replay();
  37. final String jndiName= "foobar";
  38. MockControl contextControl = contextControl(jndiName, ejb);
  39. LocalSlsbInvokerInterceptor si = configuredInterceptor(contextControl, jndiName);
  40. contextControl.verify();
  41. }
  42. public void testLookupFailure() throws Exception {
  43. final NamingException nex = new NamingException();
  44. final String jndiName= "foobar";
  45. JndiTemplate jt = new JndiTemplate() {
  46. public Object lookup(String name) throws NamingException {
  47. assertTrue(jndiName.equals(name));
  48. throw nex;
  49. }
  50. };
  51. LocalSlsbInvokerInterceptor si = new LocalSlsbInvokerInterceptor();
  52. si.setJndiName("foobar");
  53. // default resourceRef=false should cause this to fail, as java:/comp/env will not
  54. // automatically be added
  55. si.setJndiTemplate(jt);
  56. try {
  57. si.afterPropertiesSet();
  58. fail("Should have failed with naming exception");
  59. }
  60. catch (NamingException ex) {
  61. assertTrue(ex == nex);
  62. }
  63. }
  64. public void testInvokesMethodOnEjbInstance() throws Exception {
  65. Object retVal = new Object();
  66. MockControl ejbControl = MockControl.createControl(LocalInterface.class);
  67. final LocalInterface ejb = (LocalInterface) ejbControl.getMock();
  68. ejb.targetMethod();
  69. ejbControl.setReturnValue(retVal, 1);
  70. ejb.remove();
  71. ejbControl.setVoidCallable(1);
  72. ejbControl.replay();
  73. final String jndiName= "foobar";
  74. MockControl contextControl = contextControl(jndiName, ejb);
  75. LocalSlsbInvokerInterceptor si = configuredInterceptor(contextControl, jndiName);
  76. ProxyFactory pf = new ProxyFactory(new Class[] { LocalInterface.class } );
  77. pf.addAdvice(si);
  78. LocalInterface target = (LocalInterface) pf.getProxy();
  79. assertTrue(target.targetMethod() == retVal);
  80. contextControl.verify();
  81. ejbControl.verify();
  82. }
  83. private void testException(Exception expected) throws Exception {
  84. MockControl ejbControl = MockControl.createControl(LocalInterface.class);
  85. final LocalInterface ejb = (LocalInterface) ejbControl.getMock();
  86. ejb.targetMethod();
  87. ejbControl.setThrowable(expected);
  88. ejbControl.replay();
  89. final String jndiName= "foobar";
  90. MockControl contextControl = contextControl(jndiName, ejb);
  91. LocalSlsbInvokerInterceptor si = configuredInterceptor(contextControl, jndiName);
  92. ProxyFactory pf = new ProxyFactory(new Class[] { LocalInterface.class } );
  93. pf.addAdvice(si);
  94. LocalInterface target = (LocalInterface) pf.getProxy();
  95. try {
  96. target.targetMethod();
  97. fail("Should have thrown exception");
  98. }
  99. catch (Exception thrown) {
  100. assertTrue(thrown == expected);
  101. }
  102. contextControl.verify();
  103. ejbControl.verify();
  104. }
  105. public void testApplicationException() throws Exception {
  106. testException(new ApplicationException());
  107. }
  108. protected MockControl contextControl(final String jndiName, final LocalInterface ejbInstance) throws Exception {
  109. MockControl homeControl = MockControl.createControl(SlsbHome.class);
  110. final SlsbHome mockHome = (SlsbHome) homeControl.getMock();
  111. mockHome.create();
  112. homeControl.setReturnValue(ejbInstance, 1);
  113. homeControl.replay();
  114. MockControl ctxControl = MockControl.createControl(Context.class);
  115. final Context mockCtx = (Context) ctxControl.getMock();
  116. mockCtx.lookup("java:comp/env/" + jndiName);
  117. ctxControl.setReturnValue(mockHome);
  118. mockCtx.close();
  119. ctxControl.setVoidCallable();
  120. ctxControl.replay();
  121. return ctxControl;
  122. }
  123. protected LocalSlsbInvokerInterceptor configuredInterceptor(MockControl contextControl, final String jndiName) throws Exception {
  124. final Context mockCtx = (Context) contextControl.getMock();
  125. LocalSlsbInvokerInterceptor si = new LocalSlsbInvokerInterceptor();
  126. si.setJndiTemplate(new JndiTemplate() {
  127. protected Context createInitialContext() throws NamingException {
  128. return mockCtx;
  129. }
  130. });
  131. si.setJndiName(jndiName);
  132. si.setResourceRef(true);
  133. si.afterPropertiesSet();
  134. return si;
  135. }
  136. /** 
  137.  * Needed so that we can mock create() method.
  138.  */
  139. protected interface SlsbHome extends EJBLocalHome {
  140. LocalInterface create() throws CreateException;
  141. }
  142. protected interface BusinessMethods {
  143. Object targetMethod() throws ApplicationException;
  144. }
  145. protected interface LocalInterface extends EJBLocalObject, BusinessMethods {
  146. }
  147. protected class ApplicationException extends Exception {
  148. public ApplicationException() {
  149. super("appException");
  150. }
  151. }
  152.  
  153. }