moving .ejb.access unit tests from .testsuite -> .context

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@432 50f2f4bb-b051-0410-bef5-90022cba6387
This commit is contained in:
Chris Beams 2008-12-16 18:28:19 +00:00
parent dfce78549e
commit bdd36aa36a
4 changed files with 214 additions and 249 deletions

View File

@ -16,40 +16,43 @@
package org.springframework.ejb.access; package org.springframework.ejb.access;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;
import javax.ejb.CreateException; import javax.ejb.CreateException;
import javax.ejb.EJBLocalHome; import javax.ejb.EJBLocalHome;
import javax.ejb.EJBLocalObject; import javax.ejb.EJBLocalObject;
import javax.naming.Context; import javax.naming.Context;
import javax.naming.NamingException; import javax.naming.NamingException;
import junit.framework.TestCase; import org.junit.Test;
import org.easymock.MockControl;
import org.springframework.aop.framework.ProxyFactory; import org.springframework.aop.framework.ProxyFactory;
import org.springframework.jndi.JndiTemplate; import org.springframework.jndi.JndiTemplate;
/** /**
* @author Rod Johnson * @author Rod Johnson
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Chris Beams
*/ */
public class LocalSlsbInvokerInterceptorTests extends TestCase { public class LocalSlsbInvokerInterceptorTests {
/** /**
* Test that it performs the correct lookup. * Test that it performs the correct lookup.
*/ */
@Test
public void testPerformsLookup() throws Exception { public void testPerformsLookup() throws Exception {
MockControl ejbControl = MockControl.createControl(LocalInterfaceWithBusinessMethods.class); LocalInterfaceWithBusinessMethods ejb = createMock(LocalInterfaceWithBusinessMethods.class);
final LocalInterfaceWithBusinessMethods ejb = (LocalInterfaceWithBusinessMethods) ejbControl.getMock(); replay(ejb);
ejbControl.replay();
final String jndiName= "foobar"; String jndiName= "foobar";
MockControl contextControl = contextControl(jndiName, ejb); Context mockContext = mockContext(jndiName, ejb);
LocalSlsbInvokerInterceptor si = configuredInterceptor(contextControl, jndiName); configuredInterceptor(mockContext, jndiName);
contextControl.verify(); verify(mockContext);
} }
@Test
public void testLookupFailure() throws Exception { public void testLookupFailure() throws Exception {
final NamingException nex = new NamingException(); final NamingException nex = new NamingException();
final String jndiName= "foobar"; final String jndiName= "foobar";
@ -74,20 +77,18 @@ public class LocalSlsbInvokerInterceptorTests extends TestCase {
} }
} }
@Test
public void testInvokesMethodOnEjbInstance() throws Exception { public void testInvokesMethodOnEjbInstance() throws Exception {
Object retVal = new Object(); Object retVal = new Object();
MockControl ejbControl = MockControl.createControl(LocalInterfaceWithBusinessMethods.class); LocalInterfaceWithBusinessMethods ejb = createMock(LocalInterfaceWithBusinessMethods.class);
final LocalInterfaceWithBusinessMethods ejb = (LocalInterfaceWithBusinessMethods) ejbControl.getMock(); expect(ejb.targetMethod()).andReturn(retVal);
ejb.targetMethod();
ejbControl.setReturnValue(retVal, 1);
ejb.remove(); ejb.remove();
ejbControl.setVoidCallable(1); replay(ejb);
ejbControl.replay();
final String jndiName= "foobar"; String jndiName= "foobar";
MockControl contextControl = contextControl(jndiName, ejb); Context mockContext = mockContext(jndiName, ejb);
LocalSlsbInvokerInterceptor si = configuredInterceptor(contextControl, jndiName); LocalSlsbInvokerInterceptor si = configuredInterceptor(mockContext, jndiName);
ProxyFactory pf = new ProxyFactory(new Class[] { BusinessMethods.class } ); ProxyFactory pf = new ProxyFactory(new Class[] { BusinessMethods.class } );
pf.addAdvice(si); pf.addAdvice(si);
@ -95,24 +96,22 @@ public class LocalSlsbInvokerInterceptorTests extends TestCase {
assertTrue(target.targetMethod() == retVal); assertTrue(target.targetMethod() == retVal);
contextControl.verify(); verify(mockContext);
ejbControl.verify(); verify(ejb);
} }
@Test
public void testInvokesMethodOnEjbInstanceWithSeparateBusinessMethods() throws Exception { public void testInvokesMethodOnEjbInstanceWithSeparateBusinessMethods() throws Exception {
Object retVal = new Object(); Object retVal = new Object();
MockControl ejbControl = MockControl.createControl(LocalInterface.class); LocalInterface ejb = createMock(LocalInterface.class);
final LocalInterface ejb = (LocalInterface) ejbControl.getMock(); expect(ejb.targetMethod()).andReturn(retVal);
ejb.targetMethod();
ejbControl.setReturnValue(retVal, 1);
ejb.remove(); ejb.remove();
ejbControl.setVoidCallable(1); replay(ejb);
ejbControl.replay();
final String jndiName= "foobar"; String jndiName= "foobar";
MockControl contextControl = contextControl(jndiName, ejb); Context mockContext = mockContext(jndiName, ejb);
LocalSlsbInvokerInterceptor si = configuredInterceptor(contextControl, jndiName); LocalSlsbInvokerInterceptor si = configuredInterceptor(mockContext, jndiName);
ProxyFactory pf = new ProxyFactory(new Class[] { BusinessMethods.class } ); ProxyFactory pf = new ProxyFactory(new Class[] { BusinessMethods.class } );
pf.addAdvice(si); pf.addAdvice(si);
@ -120,21 +119,19 @@ public class LocalSlsbInvokerInterceptorTests extends TestCase {
assertTrue(target.targetMethod() == retVal); assertTrue(target.targetMethod() == retVal);
contextControl.verify(); verify(mockContext);
ejbControl.verify(); verify(ejb);
} }
private void testException(Exception expected) throws Exception { private void testException(Exception expected) throws Exception {
MockControl ejbControl = MockControl.createControl(LocalInterfaceWithBusinessMethods.class); LocalInterfaceWithBusinessMethods ejb = createMock(LocalInterfaceWithBusinessMethods.class);
final LocalInterfaceWithBusinessMethods ejb = (LocalInterfaceWithBusinessMethods) ejbControl.getMock(); expect(ejb.targetMethod()).andThrow(expected);
ejb.targetMethod(); replay(ejb);
ejbControl.setThrowable(expected);
ejbControl.replay();
final String jndiName= "foobar"; String jndiName= "foobar";
MockControl contextControl = contextControl(jndiName, ejb); Context mockContext = mockContext(jndiName, ejb);
LocalSlsbInvokerInterceptor si = configuredInterceptor(contextControl, jndiName); LocalSlsbInvokerInterceptor si = configuredInterceptor(mockContext, jndiName);
ProxyFactory pf = new ProxyFactory(new Class[] { LocalInterfaceWithBusinessMethods.class } ); ProxyFactory pf = new ProxyFactory(new Class[] { LocalInterfaceWithBusinessMethods.class } );
pf.addAdvice(si); pf.addAdvice(si);
@ -148,38 +145,33 @@ public class LocalSlsbInvokerInterceptorTests extends TestCase {
assertTrue(thrown == expected); assertTrue(thrown == expected);
} }
contextControl.verify(); verify(mockContext);
ejbControl.verify(); verify(ejb);
} }
@Test
public void testApplicationException() throws Exception { public void testApplicationException() throws Exception {
testException(new ApplicationException()); testException(new ApplicationException());
} }
protected MockControl contextControl(final String jndiName, final Object ejbInstance) protected Context mockContext(final String jndiName, final Object ejbInstance)
throws Exception { throws Exception {
MockControl homeControl = MockControl.createControl(SlsbHome.class); final SlsbHome mockHome = createMock(SlsbHome.class);
final SlsbHome mockHome = (SlsbHome) homeControl.getMock(); expect(mockHome.create()).andReturn((LocalInterface)ejbInstance);
mockHome.create(); replay(mockHome);
homeControl.setReturnValue(ejbInstance, 1);
homeControl.replay();
MockControl ctxControl = MockControl.createControl(Context.class); final Context mockCtx = createMock(Context.class);
final Context mockCtx = (Context) ctxControl.getMock();
mockCtx.lookup("java:comp/env/" + jndiName); expect(mockCtx.lookup("java:comp/env/" + jndiName)).andReturn(mockHome);
ctxControl.setReturnValue(mockHome);
mockCtx.close(); mockCtx.close();
ctxControl.setVoidCallable(); replay(mockCtx);
ctxControl.replay(); return mockCtx;
return ctxControl;
} }
protected LocalSlsbInvokerInterceptor configuredInterceptor(MockControl contextControl, final String jndiName) protected LocalSlsbInvokerInterceptor configuredInterceptor(final Context mockCtx, final String jndiName)
throws Exception { throws Exception {
final Context mockCtx = (Context) contextControl.getMock();
LocalSlsbInvokerInterceptor si = new LocalSlsbInvokerInterceptor(); LocalSlsbInvokerInterceptor si = new LocalSlsbInvokerInterceptor();
si.setJndiTemplate(new JndiTemplate() { si.setJndiTemplate(new JndiTemplate() {
protected Context createInitialContext() throws NamingException { protected Context createInitialContext() throws NamingException {
@ -219,7 +211,8 @@ public class LocalSlsbInvokerInterceptorTests extends TestCase {
} }
private class ApplicationException extends Exception { @SuppressWarnings("serial")
private class ApplicationException extends Exception {
public ApplicationException() { public ApplicationException() {
super("appException"); super("appException");

View File

@ -16,6 +16,9 @@
package org.springframework.ejb.access; package org.springframework.ejb.access;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;
import java.lang.reflect.Proxy; import java.lang.reflect.Proxy;
import javax.ejb.CreateException; import javax.ejb.CreateException;
@ -23,35 +26,30 @@ import javax.ejb.EJBLocalHome;
import javax.ejb.EJBLocalObject; import javax.ejb.EJBLocalObject;
import javax.naming.NamingException; import javax.naming.NamingException;
import junit.framework.TestCase; import org.junit.Test;
import org.easymock.MockControl;
import org.springframework.jndi.JndiTemplate; import org.springframework.jndi.JndiTemplate;
/** /**
* @author Rod Johnson * @author Rod Johnson
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Chris Beams
* @since 21.05.2003 * @since 21.05.2003
*/ */
public class LocalStatelessSessionProxyFactoryBeanTests extends TestCase { public class LocalStatelessSessionProxyFactoryBeanTests {
@Test
public void testInvokesMethod() throws Exception { public void testInvokesMethod() throws Exception {
final int value = 11; final int value = 11;
final String jndiName = "foo"; final String jndiName = "foo";
MockControl ec = MockControl.createControl(MyEjb.class); MyEjb myEjb = createMock(MyEjb.class);
MyEjb myEjb = (MyEjb) ec.getMock(); expect(myEjb.getValue()).andReturn(value);
myEjb.getValue();
ec.setReturnValue(value, 1);
myEjb.remove(); myEjb.remove();
ec.setVoidCallable(1); replay(myEjb);
ec.replay();
MockControl mc = MockControl.createControl(MyHome.class); final MyHome home = createMock(MyHome.class);
final MyHome home = (MyHome) mc.getMock(); expect(home.create()).andReturn(myEjb);
home.create(); replay(home);
mc.setReturnValue(myEjb, 1);
mc.replay();
JndiTemplate jt = new JndiTemplate() { JndiTemplate jt = new JndiTemplate() {
public Object lookup(String name) throws NamingException { public Object lookup(String name) throws NamingException {
@ -73,19 +71,18 @@ public class LocalStatelessSessionProxyFactoryBeanTests extends TestCase {
MyBusinessMethods mbm = (MyBusinessMethods) fb.getObject(); MyBusinessMethods mbm = (MyBusinessMethods) fb.getObject();
assertTrue(Proxy.isProxyClass(mbm.getClass())); assertTrue(Proxy.isProxyClass(mbm.getClass()));
assertTrue(mbm.getValue() == value); assertTrue(mbm.getValue() == value);
mc.verify(); verify(myEjb);
ec.verify(); verify(home);
} }
@Test
public void testInvokesMethodOnEjb3StyleBean() throws Exception { public void testInvokesMethodOnEjb3StyleBean() throws Exception {
final int value = 11; final int value = 11;
final String jndiName = "foo"; final String jndiName = "foo";
MockControl ec = MockControl.createControl(MyEjb.class); final MyEjb myEjb = createMock(MyEjb.class);
final MyEjb myEjb = (MyEjb) ec.getMock(); expect(myEjb.getValue()).andReturn(value);
myEjb.getValue(); replay(myEjb);
ec.setReturnValue(value, 1);
ec.replay();
JndiTemplate jt = new JndiTemplate() { JndiTemplate jt = new JndiTemplate() {
public Object lookup(String name) throws NamingException { public Object lookup(String name) throws NamingException {
@ -107,18 +104,17 @@ public class LocalStatelessSessionProxyFactoryBeanTests extends TestCase {
MyBusinessMethods mbm = (MyBusinessMethods) fb.getObject(); MyBusinessMethods mbm = (MyBusinessMethods) fb.getObject();
assertTrue(Proxy.isProxyClass(mbm.getClass())); assertTrue(Proxy.isProxyClass(mbm.getClass()));
assertTrue(mbm.getValue() == value); assertTrue(mbm.getValue() == value);
ec.verify(); verify(myEjb);
} }
@Test
public void testCreateException() throws Exception { public void testCreateException() throws Exception {
final String jndiName = "foo"; final String jndiName = "foo";
final CreateException cex = new CreateException(); final CreateException cex = new CreateException();
MockControl mc = MockControl.createControl(MyHome.class); final MyHome home = createMock(MyHome.class);
final MyHome home = (MyHome) mc.getMock(); expect(home.create()).andThrow(cex);
home.create(); replay(home);
mc.setThrowable(cex);
mc.replay();
JndiTemplate jt = new JndiTemplate() { JndiTemplate jt = new JndiTemplate() {
public Object lookup(String name) throws NamingException { public Object lookup(String name) throws NamingException {
@ -149,17 +145,17 @@ public class LocalStatelessSessionProxyFactoryBeanTests extends TestCase {
assertSame(cex, ex.getCause()); assertSame(cex, ex.getCause());
} }
mc.verify(); verify(home);
} }
@Test
public void testNoBusinessInterfaceSpecified() throws Exception { public void testNoBusinessInterfaceSpecified() throws Exception {
// Will do JNDI lookup to get home but won't call create // Will do JNDI lookup to get home but won't call create
// Could actually try to figure out interface from create? // Could actually try to figure out interface from create?
final String jndiName = "foo"; final String jndiName = "foo";
MockControl mc = MockControl.createControl(MyHome.class); final MyHome home = createMock(MyHome.class);
final MyHome home = (MyHome) mc.getMock(); replay(home);
mc.replay();
JndiTemplate jt = new JndiTemplate() { JndiTemplate jt = new JndiTemplate() {
public Object lookup(String name) throws NamingException { public Object lookup(String name) throws NamingException {
@ -188,7 +184,7 @@ public class LocalStatelessSessionProxyFactoryBeanTests extends TestCase {
} }
// Expect no methods on home // Expect no methods on home
mc.verify(); verify(home);
} }

View File

@ -16,6 +16,9 @@
package org.springframework.ejb.access; package org.springframework.ejb.access;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;
import java.rmi.ConnectException; import java.rmi.ConnectException;
import java.rmi.RemoteException; import java.rmi.RemoteException;
@ -25,9 +28,7 @@ import javax.ejb.EJBObject;
import javax.naming.Context; import javax.naming.Context;
import javax.naming.NamingException; import javax.naming.NamingException;
import junit.framework.TestCase; import org.junit.Test;
import org.easymock.MockControl;
import org.springframework.aop.framework.ProxyFactory; import org.springframework.aop.framework.ProxyFactory;
import org.springframework.jndi.JndiTemplate; import org.springframework.jndi.JndiTemplate;
import org.springframework.remoting.RemoteAccessException; import org.springframework.remoting.RemoteAccessException;
@ -35,35 +36,31 @@ import org.springframework.remoting.RemoteAccessException;
/** /**
* @author Rod Johnson * @author Rod Johnson
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Chris Beams
*/ */
public class SimpleRemoteSlsbInvokerInterceptorTests extends TestCase { public class SimpleRemoteSlsbInvokerInterceptorTests {
private MockControl contextControl( private Context mockContext(
String jndiName, RemoteInterface ejbInstance, int createCount, int lookupCount, int closeCount) String jndiName, RemoteInterface ejbInstance, int createCount, int lookupCount, int closeCount)
throws Exception { throws Exception {
MockControl homeControl = MockControl.createControl(SlsbHome.class); final SlsbHome mockHome = createMock(SlsbHome.class);
final SlsbHome mockHome = (SlsbHome) homeControl.getMock(); expect(mockHome.create()).andReturn(ejbInstance).times(createCount);
mockHome.create(); replay(mockHome);
homeControl.setReturnValue(ejbInstance, createCount);
homeControl.replay();
MockControl ctxControl = MockControl.createControl(Context.class); final Context mockCtx = createMock(Context.class);
final Context mockCtx = (Context) ctxControl.getMock();
mockCtx.lookup("java:comp/env/" + jndiName); expect(mockCtx.lookup("java:comp/env/" + jndiName)).andReturn(mockHome).times(lookupCount);
ctxControl.setReturnValue(mockHome, lookupCount);
mockCtx.close(); mockCtx.close();
ctxControl.setVoidCallable(closeCount); expectLastCall().times(closeCount);
ctxControl.replay(); replay(mockCtx);
return ctxControl; return mockCtx;
} }
private SimpleRemoteSlsbInvokerInterceptor configuredInterceptor( private SimpleRemoteSlsbInvokerInterceptor configuredInterceptor(
MockControl ctxControl, String jndiName) throws Exception { final Context mockCtx, String jndiName) throws Exception {
final Context mockCtx = (Context) ctxControl.getMock();
SimpleRemoteSlsbInvokerInterceptor si = createInterceptor(); SimpleRemoteSlsbInvokerInterceptor si = createInterceptor();
si.setJndiTemplate(new JndiTemplate() { si.setJndiTemplate(new JndiTemplate() {
protected Context createInitialContext() { protected Context createInitialContext() {
@ -80,7 +77,7 @@ public class SimpleRemoteSlsbInvokerInterceptorTests extends TestCase {
return new SimpleRemoteSlsbInvokerInterceptor(); return new SimpleRemoteSlsbInvokerInterceptor();
} }
protected Object configuredProxy(SimpleRemoteSlsbInvokerInterceptor si, Class ifc) throws NamingException { protected Object configuredProxy(SimpleRemoteSlsbInvokerInterceptor si, Class<?> ifc) throws NamingException {
si.afterPropertiesSet(); si.afterPropertiesSet();
ProxyFactory pf = new ProxyFactory(new Class[] {ifc}); ProxyFactory pf = new ProxyFactory(new Class[] {ifc});
pf.addAdvice(si); pf.addAdvice(si);
@ -88,38 +85,38 @@ public class SimpleRemoteSlsbInvokerInterceptorTests extends TestCase {
} }
@Test
public void testPerformsLookup() throws Exception { public void testPerformsLookup() throws Exception {
MockControl ejbControl = MockControl.createControl(RemoteInterface.class); RemoteInterface ejb = createMock(RemoteInterface.class);
RemoteInterface ejb = (RemoteInterface) ejbControl.getMock(); replay(ejb);
ejbControl.replay();
String jndiName= "foobar"; String jndiName= "foobar";
MockControl contextControl = contextControl(jndiName, ejb, 1, 1, 1); Context mockContext = mockContext(jndiName, ejb, 1, 1, 1);
SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(contextControl, jndiName); SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(mockContext, jndiName);
RemoteInterface target = (RemoteInterface) configuredProxy(si, RemoteInterface.class); configuredProxy(si, RemoteInterface.class);
contextControl.verify(); verify(mockContext);
} }
@Test
public void testPerformsLookupWithAccessContext() throws Exception { public void testPerformsLookupWithAccessContext() throws Exception {
MockControl ejbControl = MockControl.createControl(RemoteInterface.class); RemoteInterface ejb = createMock(RemoteInterface.class);
RemoteInterface ejb = (RemoteInterface) ejbControl.getMock(); expect(ejb.targetMethod()).andReturn(null);
ejb.targetMethod(); replay(ejb);
ejbControl.setReturnValue(null);
ejbControl.replay();
String jndiName= "foobar"; String jndiName= "foobar";
MockControl contextControl = contextControl(jndiName, ejb, 1, 1, 2); Context mockContext = mockContext(jndiName, ejb, 1, 1, 2);
SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(contextControl, jndiName); SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(mockContext, jndiName);
si.setExposeAccessContext(true); si.setExposeAccessContext(true);
RemoteInterface target = (RemoteInterface) configuredProxy(si, RemoteInterface.class); RemoteInterface target = (RemoteInterface) configuredProxy(si, RemoteInterface.class);
assertNull(target.targetMethod()); assertNull(target.targetMethod());
contextControl.verify(); verify(mockContext);
} }
@Test
public void testLookupFailure() throws Exception { public void testLookupFailure() throws Exception {
final NamingException nex = new NamingException(); final NamingException nex = new NamingException();
final String jndiName = "foobar"; final String jndiName = "foobar";
@ -144,31 +141,32 @@ public class SimpleRemoteSlsbInvokerInterceptorTests extends TestCase {
} }
} }
@Test
public void testInvokesMethodOnEjbInstance() throws Exception { public void testInvokesMethodOnEjbInstance() throws Exception {
doTestInvokesMethodOnEjbInstance(true, true); doTestInvokesMethodOnEjbInstance(true, true);
} }
@Test
public void testInvokesMethodOnEjbInstanceWithLazyLookup() throws Exception { public void testInvokesMethodOnEjbInstanceWithLazyLookup() throws Exception {
doTestInvokesMethodOnEjbInstance(false, true); doTestInvokesMethodOnEjbInstance(false, true);
} }
@Test
public void testInvokesMethodOnEjbInstanceWithLazyLookupAndNoCache() throws Exception { public void testInvokesMethodOnEjbInstanceWithLazyLookupAndNoCache() throws Exception {
doTestInvokesMethodOnEjbInstance(false, false); doTestInvokesMethodOnEjbInstance(false, false);
} }
@Test
public void testInvokesMethodOnEjbInstanceWithNoCache() throws Exception { public void testInvokesMethodOnEjbInstanceWithNoCache() throws Exception {
doTestInvokesMethodOnEjbInstance(true, false); doTestInvokesMethodOnEjbInstance(true, false);
} }
private void doTestInvokesMethodOnEjbInstance(boolean lookupHomeOnStartup, boolean cacheHome) throws Exception { private void doTestInvokesMethodOnEjbInstance(boolean lookupHomeOnStartup, boolean cacheHome) throws Exception {
Object retVal = new Object(); Object retVal = new Object();
MockControl ejbControl = MockControl.createControl(RemoteInterface.class); final RemoteInterface ejb = createMock(RemoteInterface.class);
final RemoteInterface ejb = (RemoteInterface) ejbControl.getMock(); expect(ejb.targetMethod()).andReturn(retVal).times(2);
ejb.targetMethod();
ejbControl.setReturnValue(retVal, 2);
ejb.remove(); ejb.remove();
ejbControl.setVoidCallable(2); replay(ejb);
ejbControl.replay();
int lookupCount = 1; int lookupCount = 1;
if (!cacheHome) { if (!cacheHome) {
@ -179,9 +177,9 @@ public class SimpleRemoteSlsbInvokerInterceptorTests extends TestCase {
} }
final String jndiName= "foobar"; final String jndiName= "foobar";
MockControl contextControl = contextControl(jndiName, ejb, 2, lookupCount, lookupCount); Context mockContext = mockContext(jndiName, ejb, 2, lookupCount, lookupCount);
SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(contextControl, jndiName); SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(mockContext, jndiName);
si.setLookupHomeOnStartup(lookupHomeOnStartup); si.setLookupHomeOnStartup(lookupHomeOnStartup);
si.setCacheHome(cacheHome); si.setCacheHome(cacheHome);
@ -189,46 +187,40 @@ public class SimpleRemoteSlsbInvokerInterceptorTests extends TestCase {
assertTrue(target.targetMethod() == retVal); assertTrue(target.targetMethod() == retVal);
assertTrue(target.targetMethod() == retVal); assertTrue(target.targetMethod() == retVal);
contextControl.verify(); verify(mockContext, ejb);
ejbControl.verify();
} }
@Test
public void testInvokesMethodOnEjbInstanceWithHomeInterface() throws Exception { public void testInvokesMethodOnEjbInstanceWithHomeInterface() throws Exception {
Object retVal = new Object(); Object retVal = new Object();
MockControl ejbControl = MockControl.createControl(RemoteInterface.class); final RemoteInterface ejb = createMock(RemoteInterface.class);
final RemoteInterface ejb = (RemoteInterface) ejbControl.getMock(); expect(ejb.targetMethod()).andReturn(retVal);
ejb.targetMethod();
ejbControl.setReturnValue(retVal, 1);
ejb.remove(); ejb.remove();
ejbControl.setVoidCallable(1); replay(ejb);
ejbControl.replay();
final String jndiName= "foobar"; final String jndiName= "foobar";
MockControl contextControl = contextControl(jndiName, ejb, 1, 1, 1); Context mockContext = mockContext(jndiName, ejb, 1, 1, 1);
SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(contextControl, jndiName); SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(mockContext, jndiName);
si.setHomeInterface(SlsbHome.class); si.setHomeInterface(SlsbHome.class);
RemoteInterface target = (RemoteInterface) configuredProxy(si, RemoteInterface.class); RemoteInterface target = (RemoteInterface) configuredProxy(si, RemoteInterface.class);
assertTrue(target.targetMethod() == retVal); assertTrue(target.targetMethod() == retVal);
contextControl.verify(); verify(mockContext, ejb);
ejbControl.verify();
} }
@Test
public void testInvokesMethodOnEjbInstanceWithRemoteException() throws Exception { public void testInvokesMethodOnEjbInstanceWithRemoteException() throws Exception {
MockControl ejbControl = MockControl.createControl(RemoteInterface.class); final RemoteInterface ejb = createMock(RemoteInterface.class);
final RemoteInterface ejb = (RemoteInterface) ejbControl.getMock(); expect(ejb.targetMethod()).andThrow(new RemoteException());
ejb.targetMethod();
ejbControl.setThrowable(new RemoteException(), 1);
ejb.remove(); ejb.remove();
ejbControl.setVoidCallable(1); replay(ejb);
ejbControl.replay();
final String jndiName= "foobar"; final String jndiName= "foobar";
MockControl contextControl = contextControl(jndiName, ejb, 1, 1, 1); Context mockContext = mockContext(jndiName, ejb, 1, 1, 1);
SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(contextControl, jndiName); SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(mockContext, jndiName);
RemoteInterface target = (RemoteInterface) configuredProxy(si, RemoteInterface.class); RemoteInterface target = (RemoteInterface) configuredProxy(si, RemoteInterface.class);
try { try {
@ -239,22 +231,25 @@ public class SimpleRemoteSlsbInvokerInterceptorTests extends TestCase {
// expected // expected
} }
contextControl.verify(); verify(mockContext, ejb);
ejbControl.verify();
} }
@Test
public void testInvokesMethodOnEjbInstanceWithConnectExceptionWithRefresh() throws Exception { public void testInvokesMethodOnEjbInstanceWithConnectExceptionWithRefresh() throws Exception {
doTestInvokesMethodOnEjbInstanceWithConnectExceptionWithRefresh(true, true); doTestInvokesMethodOnEjbInstanceWithConnectExceptionWithRefresh(true, true);
} }
@Test
public void testInvokesMethodOnEjbInstanceWithConnectExceptionWithRefreshAndLazyLookup() throws Exception { public void testInvokesMethodOnEjbInstanceWithConnectExceptionWithRefreshAndLazyLookup() throws Exception {
doTestInvokesMethodOnEjbInstanceWithConnectExceptionWithRefresh(false, true); doTestInvokesMethodOnEjbInstanceWithConnectExceptionWithRefresh(false, true);
} }
@Test
public void testInvokesMethodOnEjbInstanceWithConnectExceptionWithRefreshAndLazyLookupAndNoCache() throws Exception { public void testInvokesMethodOnEjbInstanceWithConnectExceptionWithRefreshAndLazyLookupAndNoCache() throws Exception {
doTestInvokesMethodOnEjbInstanceWithConnectExceptionWithRefresh(false, false); doTestInvokesMethodOnEjbInstanceWithConnectExceptionWithRefresh(false, false);
} }
@Test
public void testInvokesMethodOnEjbInstanceWithConnectExceptionWithRefreshAndNoCache() throws Exception { public void testInvokesMethodOnEjbInstanceWithConnectExceptionWithRefreshAndNoCache() throws Exception {
doTestInvokesMethodOnEjbInstanceWithConnectExceptionWithRefresh(true, false); doTestInvokesMethodOnEjbInstanceWithConnectExceptionWithRefresh(true, false);
} }
@ -262,13 +257,11 @@ public class SimpleRemoteSlsbInvokerInterceptorTests extends TestCase {
private void doTestInvokesMethodOnEjbInstanceWithConnectExceptionWithRefresh( private void doTestInvokesMethodOnEjbInstanceWithConnectExceptionWithRefresh(
boolean lookupHomeOnStartup, boolean cacheHome) throws Exception { boolean lookupHomeOnStartup, boolean cacheHome) throws Exception {
MockControl ejbControl = MockControl.createControl(RemoteInterface.class); final RemoteInterface ejb = createMock(RemoteInterface.class);
final RemoteInterface ejb = (RemoteInterface) ejbControl.getMock(); expect(ejb.targetMethod()).andThrow(new ConnectException("")).times(2);
ejb.targetMethod();
ejbControl.setThrowable(new ConnectException(""), 2);
ejb.remove(); ejb.remove();
ejbControl.setVoidCallable(2); expectLastCall().times(2);
ejbControl.replay(); replay(ejb);
int lookupCount = 2; int lookupCount = 2;
if (!cacheHome) { if (!cacheHome) {
@ -279,9 +272,9 @@ public class SimpleRemoteSlsbInvokerInterceptorTests extends TestCase {
} }
final String jndiName= "foobar"; final String jndiName= "foobar";
MockControl contextControl = contextControl(jndiName, ejb, 2, lookupCount, lookupCount); Context mockContext = mockContext(jndiName, ejb, 2, lookupCount, lookupCount);
SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(contextControl, jndiName); SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(mockContext, jndiName);
si.setRefreshHomeOnConnectFailure(true); si.setRefreshHomeOnConnectFailure(true);
si.setLookupHomeOnStartup(lookupHomeOnStartup); si.setLookupHomeOnStartup(lookupHomeOnStartup);
si.setCacheHome(cacheHome); si.setCacheHome(cacheHome);
@ -295,45 +288,39 @@ public class SimpleRemoteSlsbInvokerInterceptorTests extends TestCase {
// expected // expected
} }
contextControl.verify(); verify(mockContext, ejb);
ejbControl.verify();
} }
@Test
public void testInvokesMethodOnEjbInstanceWithBusinessInterface() throws Exception { public void testInvokesMethodOnEjbInstanceWithBusinessInterface() throws Exception {
Object retVal = new Object(); Object retVal = new Object();
MockControl ejbControl = MockControl.createControl(RemoteInterface.class); final RemoteInterface ejb = createMock(RemoteInterface.class);
final RemoteInterface ejb = (RemoteInterface) ejbControl.getMock(); expect(ejb.targetMethod()).andReturn(retVal);
ejb.targetMethod();
ejbControl.setReturnValue(retVal, 1);
ejb.remove(); ejb.remove();
ejbControl.setVoidCallable(1); replay(ejb);
ejbControl.replay();
final String jndiName= "foobar"; final String jndiName= "foobar";
MockControl contextControl = contextControl(jndiName, ejb, 1, 1, 1); Context mockContext = mockContext(jndiName, ejb, 1, 1, 1);
SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(contextControl, jndiName); SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(mockContext, jndiName);
BusinessInterface target = (BusinessInterface) configuredProxy(si, BusinessInterface.class); BusinessInterface target = (BusinessInterface) configuredProxy(si, BusinessInterface.class);
assertTrue(target.targetMethod() == retVal); assertTrue(target.targetMethod() == retVal);
contextControl.verify(); verify(mockContext, ejb);
ejbControl.verify();
} }
@Test
public void testInvokesMethodOnEjbInstanceWithBusinessInterfaceWithRemoteException() throws Exception { public void testInvokesMethodOnEjbInstanceWithBusinessInterfaceWithRemoteException() throws Exception {
MockControl ejbControl = MockControl.createControl(RemoteInterface.class); final RemoteInterface ejb = createMock(RemoteInterface.class);
final RemoteInterface ejb = (RemoteInterface) ejbControl.getMock(); expect(ejb.targetMethod()).andThrow(new RemoteException());
ejb.targetMethod();
ejbControl.setThrowable(new RemoteException(), 1);
ejb.remove(); ejb.remove();
ejbControl.setVoidCallable(1); replay(ejb);
ejbControl.replay();
final String jndiName= "foobar"; final String jndiName= "foobar";
MockControl contextControl = contextControl(jndiName, ejb, 1, 1, 1); Context mockContext = mockContext(jndiName, ejb, 1, 1, 1);
SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(contextControl, jndiName); SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(mockContext, jndiName);
BusinessInterface target = (BusinessInterface) configuredProxy(si, BusinessInterface.class); BusinessInterface target = (BusinessInterface) configuredProxy(si, BusinessInterface.class);
try { try {
@ -344,31 +331,29 @@ public class SimpleRemoteSlsbInvokerInterceptorTests extends TestCase {
// expected // expected
} }
contextControl.verify(); verify(mockContext, ejb);
ejbControl.verify();
} }
@Test
public void testApplicationException() throws Exception { public void testApplicationException() throws Exception {
doTestException(new ApplicationException()); doTestException(new ApplicationException());
} }
@Test
public void testRemoteException() throws Exception { public void testRemoteException() throws Exception {
doTestException(new RemoteException()); doTestException(new RemoteException());
} }
private void doTestException(Exception expected) throws Exception { private void doTestException(Exception expected) throws Exception {
MockControl ejbControl = MockControl.createControl(RemoteInterface.class); final RemoteInterface ejb = createMock(RemoteInterface.class);
final RemoteInterface ejb = (RemoteInterface) ejbControl.getMock(); expect(ejb.targetMethod()).andThrow(expected);
ejb.targetMethod();
ejbControl.setThrowable(expected);
ejb.remove(); ejb.remove();
ejbControl.setVoidCallable(1); replay(ejb);
ejbControl.replay();
final String jndiName= "foobar"; final String jndiName= "foobar";
MockControl contextControl = contextControl(jndiName, ejb, 1, 1, 1); Context mockContext = mockContext(jndiName, ejb, 1, 1, 1);
SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(contextControl, jndiName); SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(mockContext, jndiName);
RemoteInterface target = (RemoteInterface) configuredProxy(si, RemoteInterface.class); RemoteInterface target = (RemoteInterface) configuredProxy(si, RemoteInterface.class);
try { try {
@ -379,8 +364,7 @@ public class SimpleRemoteSlsbInvokerInterceptorTests extends TestCase {
assertTrue(thrown == expected); assertTrue(thrown == expected);
} }
contextControl.verify(); verify(mockContext, ejb);
ejbControl.verify();
} }
@ -406,7 +390,8 @@ public class SimpleRemoteSlsbInvokerInterceptorTests extends TestCase {
} }
protected class ApplicationException extends Exception { @SuppressWarnings("serial")
protected class ApplicationException extends Exception {
public ApplicationException() { public ApplicationException() {
super("appException"); super("appException");

View File

@ -16,6 +16,9 @@
package org.springframework.ejb.access; package org.springframework.ejb.access;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;
import java.lang.reflect.Proxy; import java.lang.reflect.Proxy;
import java.rmi.RemoteException; import java.rmi.RemoteException;
@ -24,8 +27,7 @@ import javax.ejb.EJBHome;
import javax.ejb.EJBObject; import javax.ejb.EJBObject;
import javax.naming.NamingException; import javax.naming.NamingException;
import org.easymock.MockControl; import org.junit.Test;
import org.springframework.jndi.JndiTemplate; import org.springframework.jndi.JndiTemplate;
import org.springframework.remoting.RemoteAccessException; import org.springframework.remoting.RemoteAccessException;
@ -40,30 +42,26 @@ public class SimpleRemoteStatelessSessionProxyFactoryBeanTests extends SimpleRem
return new SimpleRemoteStatelessSessionProxyFactoryBean(); return new SimpleRemoteStatelessSessionProxyFactoryBean();
} }
protected Object configuredProxy(SimpleRemoteSlsbInvokerInterceptor si, Class ifc) throws NamingException { protected Object configuredProxy(SimpleRemoteSlsbInvokerInterceptor si, Class<?> ifc) throws NamingException {
SimpleRemoteStatelessSessionProxyFactoryBean fb = (SimpleRemoteStatelessSessionProxyFactoryBean) si; SimpleRemoteStatelessSessionProxyFactoryBean fb = (SimpleRemoteStatelessSessionProxyFactoryBean) si;
fb.setBusinessInterface(ifc); fb.setBusinessInterface(ifc);
fb.afterPropertiesSet(); fb.afterPropertiesSet();
return fb.getObject(); return fb.getObject();
} }
@Test
public void testInvokesMethod() throws Exception { public void testInvokesMethod() throws Exception {
final int value = 11; final int value = 11;
final String jndiName = "foo"; final String jndiName = "foo";
MockControl ec = MockControl.createControl(MyEjb.class); MyEjb myEjb = createMock(MyEjb.class);
MyEjb myEjb = (MyEjb) ec.getMock(); expect(myEjb.getValue()).andReturn(value);
myEjb.getValue();
ec.setReturnValue(value, 1);
myEjb.remove(); myEjb.remove();
ec.setVoidCallable(1); replay(myEjb);
ec.replay();
MockControl mc = MockControl.createControl(MyHome.class); final MyHome home = createMock(MyHome.class);
final MyHome home = (MyHome) mc.getMock(); expect(home.create()).andReturn(myEjb);
home.create(); replay(home);
mc.setReturnValue(myEjb, 1);
mc.replay();
JndiTemplate jt = new JndiTemplate() { JndiTemplate jt = new JndiTemplate() {
public Object lookup(String name) { public Object lookup(String name) {
@ -85,19 +83,18 @@ public class SimpleRemoteStatelessSessionProxyFactoryBeanTests extends SimpleRem
MyBusinessMethods mbm = (MyBusinessMethods) fb.getObject(); MyBusinessMethods mbm = (MyBusinessMethods) fb.getObject();
assertTrue(Proxy.isProxyClass(mbm.getClass())); assertTrue(Proxy.isProxyClass(mbm.getClass()));
assertEquals("Returns expected value", value, mbm.getValue()); assertEquals("Returns expected value", value, mbm.getValue());
mc.verify(); verify(myEjb);
ec.verify(); verify(home);
} }
@Test
public void testInvokesMethodOnEjb3StyleBean() throws Exception { public void testInvokesMethodOnEjb3StyleBean() throws Exception {
final int value = 11; final int value = 11;
final String jndiName = "foo"; final String jndiName = "foo";
MockControl ec = MockControl.createControl(MyEjb.class); final MyEjb myEjb = createMock(MyEjb.class);
final MyEjb myEjb = (MyEjb) ec.getMock(); expect(myEjb.getValue()).andReturn(value);
myEjb.getValue(); replay(myEjb);
ec.setReturnValue(value, 1);
ec.replay();
JndiTemplate jt = new JndiTemplate() { JndiTemplate jt = new JndiTemplate() {
public Object lookup(String name) { public Object lookup(String name) {
@ -119,28 +116,24 @@ public class SimpleRemoteStatelessSessionProxyFactoryBeanTests extends SimpleRem
MyBusinessMethods mbm = (MyBusinessMethods) fb.getObject(); MyBusinessMethods mbm = (MyBusinessMethods) fb.getObject();
assertTrue(Proxy.isProxyClass(mbm.getClass())); assertTrue(Proxy.isProxyClass(mbm.getClass()));
assertEquals("Returns expected value", value, mbm.getValue()); assertEquals("Returns expected value", value, mbm.getValue());
ec.verify(); verify(myEjb);
} }
@Test
public void testRemoteException() throws Exception { public void testRemoteException() throws Exception {
final RemoteException rex = new RemoteException(); final RemoteException rex = new RemoteException();
final String jndiName = "foo"; final String jndiName = "foo";
MockControl ec = MockControl.createControl(MyEjb.class); MyEjb myEjb = createMock(MyEjb.class);
MyEjb myEjb = (MyEjb) ec.getMock(); expect(myEjb.getValue()).andThrow(rex);
myEjb.getValue();
ec.setThrowable(rex);
// TODO might want to control this behaviour... // TODO might want to control this behaviour...
// Do we really want to call remove after a remote exception? // Do we really want to call remove after a remote exception?
myEjb.remove(); myEjb.remove();
ec.setVoidCallable(1); replay(myEjb);
ec.replay();
MockControl mc = MockControl.createControl(MyHome.class); final MyHome home = createMock(MyHome.class);
final MyHome home = (MyHome) mc.getMock(); expect(home.create()).andReturn(myEjb);
home.create(); replay(home);
mc.setReturnValue(myEjb, 1);
mc.replay();
JndiTemplate jt = new JndiTemplate() { JndiTemplate jt = new JndiTemplate() {
public Object lookup(String name) { public Object lookup(String name) {
@ -168,19 +161,18 @@ public class SimpleRemoteStatelessSessionProxyFactoryBeanTests extends SimpleRem
catch (RemoteException ex) { catch (RemoteException ex) {
assertSame("Threw expected RemoteException", rex, ex); assertSame("Threw expected RemoteException", rex, ex);
} }
mc.verify(); verify(myEjb);
ec.verify(); verify(home);
} }
@Test
public void testCreateException() throws Exception { public void testCreateException() throws Exception {
final String jndiName = "foo"; final String jndiName = "foo";
final CreateException cex = new CreateException(); final CreateException cex = new CreateException();
MockControl mc = MockControl.createControl(MyHome.class); final MyHome home = createMock(MyHome.class);
final MyHome home = (MyHome) mc.getMock(); expect(home.create()).andThrow(cex);
home.create(); replay(home);
mc.setThrowable(cex);
mc.replay();
JndiTemplate jt = new JndiTemplate() { JndiTemplate jt = new JndiTemplate() {
public Object lookup(String name) { public Object lookup(String name) {
@ -211,18 +203,17 @@ public class SimpleRemoteStatelessSessionProxyFactoryBeanTests extends SimpleRem
// expected // expected
} }
mc.verify(); verify(home);
} }
@Test
public void testCreateExceptionWithLocalBusinessInterface() throws Exception { public void testCreateExceptionWithLocalBusinessInterface() throws Exception {
final String jndiName = "foo"; final String jndiName = "foo";
final CreateException cex = new CreateException(); final CreateException cex = new CreateException();
MockControl mc = MockControl.createControl(MyHome.class); final MyHome home = createMock(MyHome.class);
final MyHome home = (MyHome) mc.getMock(); expect(home.create()).andThrow(cex);
home.create(); replay(home);
mc.setThrowable(cex);
mc.replay();
JndiTemplate jt = new JndiTemplate() { JndiTemplate jt = new JndiTemplate() {
public Object lookup(String name) { public Object lookup(String name) {
@ -253,17 +244,17 @@ public class SimpleRemoteStatelessSessionProxyFactoryBeanTests extends SimpleRem
assertTrue(ex.getCause() == cex); assertTrue(ex.getCause() == cex);
} }
mc.verify(); verify(home);
} }
@Test
public void testNoBusinessInterfaceSpecified() throws Exception { public void testNoBusinessInterfaceSpecified() throws Exception {
// Will do JNDI lookup to get home but won't call create // Will do JNDI lookup to get home but won't call create
// Could actually try to figure out interface from create? // Could actually try to figure out interface from create?
final String jndiName = "foo"; final String jndiName = "foo";
MockControl mc = MockControl.createControl(MyHome.class); final MyHome home = createMock(MyHome.class);
final MyHome home = (MyHome) mc.getMock(); replay(home);
mc.replay();
JndiTemplate jt = new JndiTemplate() { JndiTemplate jt = new JndiTemplate() {
public Object lookup(String name) throws NamingException { public Object lookup(String name) throws NamingException {
@ -292,7 +283,7 @@ public class SimpleRemoteStatelessSessionProxyFactoryBeanTests extends SimpleRem
} }
// Expect no methods on home // Expect no methods on home
mc.verify(); verify(home);
} }