moving unit tests from .testsuite -> .aop

This commit is contained in:
Chris Beams 2008-12-12 17:17:32 +00:00
parent 003866835e
commit 1546c15187
10 changed files with 198 additions and 83 deletions

View File

@ -7,6 +7,7 @@
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="var" path="IVY_CACHE/org.aopalliance/com.springsource.org.aopalliance/1.0.0/com.springsource.org.aopalliance-1.0.0.jar" sourcepath="/IVY_CACHE/org.aopalliance/com.springsource.org.aopalliance/1.0.0/com.springsource.org.aopalliance-sources-1.0.0.jar"/>
<classpathentry kind="var" path="IVY_CACHE/org.apache.commons/com.springsource.org.apache.commons.logging/1.1.1/com.springsource.org.apache.commons.logging-1.1.1.jar" sourcepath="/IVY_CACHE/org.apache.commons/com.springsource.org.apache.commons.logging/1.1.1/com.springsource.org.apache.commons.logging-sources-1.1.1.jar"/>
<classpathentry kind="var" path="IVY_CACHE/org.easymock/com.springsource.org.easymock/2.3.0/com.springsource.org.easymock-2.3.0.jar" sourcepath="/IVY_CACHE/org.easymock/com.springsource.org.easymock/2.3.0/com.springsource.org.easymock-sources-2.3.0.jar"/>
<classpathentry kind="var" path="IVY_CACHE/com.jamonapi/com.springsource.com.jamonapi/2.4.0/com.springsource.com.jamonapi-2.4.0.jar" sourcepath="/IVY_CACHE/com.jamonapi/com.springsource.com.jamonapi/2.4.0/com.springsource.com.jamonapi-sources-2.4.0.jar"/>
<classpathentry kind="var" path="IVY_CACHE/net.sourceforge.cglib/com.springsource.net.sf.cglib/2.1.3/com.springsource.net.sf.cglib-2.1.3.jar" sourcepath="/IVY_CACHE/net.sourceforge.cglib/com.springsource.net.sf.cglib/2.1.3/com.springsource.net.sf.cglib-sources-2.1.3.jar"/>
<classpathentry kind="var" path="IVY_CACHE/org.apache.log4j/com.springsource.org.apache.log4j/1.2.15/com.springsource.org.apache.log4j-1.2.15.jar" sourcepath="/IVY_CACHE/org.apache.log4j/com.springsource.org.apache.log4j/1.2.15/com.springsource.org.apache.log4j-sources-1.2.15.jar"/>

View File

@ -99,6 +99,17 @@
</SOURCES>
</library>
</orderEntry>
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="jar://$IVY_CACHE$/org.easymock/com.springsource.org.easymock/2.3.0/com.springsource.org.easymock-2.3.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="jar://$IVY_CACHE$/org.easymock/com.springsource.org.easymock/2.3.0/com.springsource.org.easymock-sources-2.3.0.jar!/" />
</SOURCES>
</library>
</orderEntry>
<orderEntry type="module" module-name="beans" />
<orderEntry type="module" module-name="core" />
</component>

View File

@ -33,6 +33,7 @@
<dependency org="org.springframework" name="org.springframework.core" rev="latest.integration" conf="compile->compile"/>
<!-- test dependencies -->
<dependency org="org.apache.log4j" name="com.springsource.org.apache.log4j" rev="1.2.15" conf="test->runtime"/>
<dependency org="org.easymock" name="com.springsource.org.easymock" rev="2.3.0" conf="test->compile"/>
<dependency org="org.junit" name="com.springsource.org.junit" rev="4.5.0" conf="test->runtime"/>
<dependency org="org.objectweb.asm" name="com.springsource.org.objectweb.asm" rev="2.2.3" conf="test->runtime"/>
<dependency org="org.objectweb.asm" name="com.springsource.org.objectweb.asm.commons" rev="2.2.3" conf="test->runtime"/>

View File

@ -0,0 +1,68 @@
/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.aop.framework;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.HashMap;
/**
* Useful abstract superclass for counting advices etc.
*
* @author Rod Johnson
*/
public class MethodCounter implements Serializable {
/** Method name --> count, does not understand overloading */
private HashMap map = new HashMap();
private int allCount;
protected void count(Method m) {
count(m.getName());
}
protected void count(String methodName) {
Integer i = (Integer) map.get(methodName);
i = (i != null) ? new Integer(i.intValue() + 1) : new Integer(1);
map.put(methodName, i);
++allCount;
}
public int getCalls(String methodName) {
Integer i = (Integer) map.get(methodName);
return (i != null ? i.intValue() : 0);
}
public int getCalls() {
return allCount;
}
/**
* A bit simplistic: just wants the same class.
* Doesn't worry about counts.
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals(Object other) {
return (other != null && other.getClass() == this.getClass());
}
public int hashCode() {
return getClass().hashCode();
}
}

View File

@ -16,24 +16,32 @@
package org.springframework.aop.framework.adapter;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Method;
import java.rmi.RemoteException;
import javax.servlet.ServletException;
import javax.transaction.TransactionRolledbackException;
import junit.framework.TestCase;
import org.aopalliance.intercept.MethodInvocation;
import org.easymock.MockControl;
import org.junit.Test;
import org.springframework.aop.ThrowsAdvice;
import org.springframework.aop.framework.MethodCounter;
/**
* @author Rod Johnson
* @author Chris Beams
*/
public class ThrowsAdviceInterceptorTests extends TestCase {
public class ThrowsAdviceInterceptorTests {
@Test
public void testNoHandlerMethods() {
Object o = new Object();
try {
@ -45,30 +53,28 @@ public class ThrowsAdviceInterceptorTests extends TestCase {
}
}
@Test
public void testNotInvoked() throws Throwable {
MyThrowsHandler th = new MyThrowsHandler();
ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th);
Object ret = new Object();
MockControl mc = MockControl.createControl(MethodInvocation.class);
MethodInvocation mi = (MethodInvocation) mc.getMock();
mi.proceed();
mc.setReturnValue(ret, 1);
mc.replay();
MethodInvocation mi = createMock(MethodInvocation.class);
expect(mi.proceed()).andReturn(ret);
replay(mi);
assertEquals(ret, ti.invoke(mi));
assertEquals(0, th.getCalls());
mc.verify();
verify(mi);
}
@Test
public void testNoHandlerMethodForThrowable() throws Throwable {
MyThrowsHandler th = new MyThrowsHandler();
ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th);
assertEquals(2, ti.getHandlerMethodCount());
Exception ex = new Exception();
MockControl mc = MockControl.createControl(MethodInvocation.class);
MethodInvocation mi = (MethodInvocation) mc.getMock();
mi.proceed();
mc.setThrowable(ex);
mc.replay();
MethodInvocation mi = createMock(MethodInvocation.class);
expect(mi.proceed()).andThrow(ex);
replay(mi);
try {
ti.invoke(mi);
fail();
@ -77,24 +83,20 @@ public class ThrowsAdviceInterceptorTests extends TestCase {
assertEquals(ex, caught);
}
assertEquals(0, th.getCalls());
mc.verify();
verify(mi);
}
@Test
public void testCorrectHandlerUsed() throws Throwable {
MyThrowsHandler th = new MyThrowsHandler();
ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th);
ServletException ex = new ServletException();
MockControl mc = MockControl.createControl(MethodInvocation.class);
MethodInvocation mi = (MethodInvocation) mc.getMock();
mi.getMethod();
mc.setReturnValue(Object.class.getMethod("hashCode", (Class[]) null), 1);
mi.getArguments();
mc.setReturnValue(null);
mi.getThis();
mc.setReturnValue(new Object());
mi.proceed();
mc.setThrowable(ex);
mc.replay();
FileNotFoundException ex = new FileNotFoundException();
MethodInvocation mi = createMock(MethodInvocation.class);
expect(mi.getMethod()).andReturn(Object.class.getMethod("hashCode", (Class[]) null));
expect(mi.getArguments()).andReturn(null);
expect(mi.getThis()).andReturn(new Object());
expect(mi.proceed()).andThrow(ex);
replay(mi);
try {
ti.invoke(mi);
fail();
@ -103,20 +105,19 @@ public class ThrowsAdviceInterceptorTests extends TestCase {
assertEquals(ex, caught);
}
assertEquals(1, th.getCalls());
assertEquals(1, th.getCalls("servletException"));
mc.verify();
assertEquals(1, th.getCalls("ioException"));
verify(mi);
}
@Test
public void testCorrectHandlerUsedForSubclass() throws Throwable {
MyThrowsHandler th = new MyThrowsHandler();
ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th);
// Extends RemoteException
TransactionRolledbackException ex = new TransactionRolledbackException();
MockControl mc = MockControl.createControl(MethodInvocation.class);
MethodInvocation mi = (MethodInvocation) mc.getMock();
mi.proceed();
mc.setThrowable(ex);
mc.replay();
MethodInvocation mi = createMock(MethodInvocation.class);
expect(mi.proceed()).andThrow(ex);
replay(mi);
try {
ti.invoke(mi);
fail();
@ -126,25 +127,27 @@ public class ThrowsAdviceInterceptorTests extends TestCase {
}
assertEquals(1, th.getCalls());
assertEquals(1, th.getCalls("remoteException"));
mc.verify();
verify(mi);
}
@Test
public void testHandlerMethodThrowsException() throws Throwable {
final Throwable t = new Throwable();
@SuppressWarnings("serial")
MyThrowsHandler th = new MyThrowsHandler() {
public void afterThrowing(RemoteException ex) throws Throwable {
super.afterThrowing(ex);
throw t;
}
};
ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th);
// Extends RemoteException
TransactionRolledbackException ex = new TransactionRolledbackException();
MockControl mc = MockControl.createControl(MethodInvocation.class);
MethodInvocation mi = (MethodInvocation) mc.getMock();
mi.proceed();
mc.setThrowable(ex);
mc.replay();
MethodInvocation mi = createMock(MethodInvocation.class);
expect(mi.proceed()).andThrow(ex);
replay(mi);
try {
ti.invoke(mi);
fail();
@ -154,44 +157,23 @@ public class ThrowsAdviceInterceptorTests extends TestCase {
}
assertEquals(1, th.getCalls());
assertEquals(1, th.getCalls("remoteException"));
mc.verify();
verify(mi);
}
public static class MyThrowsHandler extends MethodCounter implements ThrowsAdvice {
@SuppressWarnings("serial")
private static class MyThrowsHandler extends MethodCounter implements ThrowsAdvice {
// Full method signature
public void afterThrowing(Method m, Object[] args, Object target, ServletException ex) {
count("servletException");
}
public void afterThrowing(Method m, Object[] args, Object target, IOException ex) {
count("ioException");
}
public void afterThrowing(RemoteException ex) throws Throwable {
count("remoteException");
}
}
/** Not valid, wrong number of arguments */
public void afterThrowing(Method m, Exception ex) throws Throwable {
throw new UnsupportedOperationException("Shouldn't be called");
}
}
}
public interface IEcho {
int echoException(int i, Throwable t) throws Throwable;
int getA();
void setA(int a);
}
public static class Echo implements IEcho {
private int a;
public int echoException(int i, Throwable t) throws Throwable {
if (t != null)
throw t;
return i;
}
public void setA(int a) {
this.a = a;
}
public int getA() {
return a;
}
}
}

View File

@ -29,16 +29,15 @@ import javax.servlet.ServletException;
import javax.transaction.TransactionRequiredException;
import junit.framework.TestCase;
import org.aopalliance.aop.Advice;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.Advisor;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.DynamicIntroductionAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.TargetSource;
import org.springframework.aop.framework.adapter.ThrowsAdviceInterceptorTests;
import org.springframework.aop.interceptor.DebugInterceptor;
import org.springframework.aop.interceptor.ExposeInvocationInterceptor;
import org.springframework.aop.interceptor.NopInterceptor;
@ -1463,20 +1462,20 @@ public abstract class AbstractAopProxyTests extends TestCase {
public void testThrowsAdvisorIsInvoked() throws Throwable {
// Reacts to ServletException and RemoteException
ThrowsAdviceInterceptorTests.MyThrowsHandler th = new ThrowsAdviceInterceptorTests.MyThrowsHandler();
MyThrowsHandler th = new MyThrowsHandler();
Advisor matchesEchoInvocations = new StaticMethodMatcherPointcutAdvisor(th) {
public boolean matches(Method m, Class targetClass) {
return m.getName().startsWith("echo");
}
};
ThrowsAdviceInterceptorTests.Echo target = new ThrowsAdviceInterceptorTests.Echo();
Echo target = new Echo();
target.setA(16);
ProxyFactory pf = new ProxyFactory(target);
pf.addAdvice(new NopInterceptor());
pf.addAdvisor(matchesEchoInvocations);
assertEquals("Advisor was added", matchesEchoInvocations, pf.getAdvisors()[1]);
ThrowsAdviceInterceptorTests.IEcho proxied = (ThrowsAdviceInterceptorTests.IEcho) createProxy(pf);
IEcho proxied = (IEcho) createProxy(pf);
assertEquals(0, th.getCalls());
assertEquals(target.getA(), proxied.getA());
assertEquals(0, th.getCalls());
@ -1503,14 +1502,14 @@ public abstract class AbstractAopProxyTests extends TestCase {
public void testAddThrowsAdviceWithoutAdvisor() throws Throwable {
// Reacts to ServletException and RemoteException
ThrowsAdviceInterceptorTests.MyThrowsHandler th = new ThrowsAdviceInterceptorTests.MyThrowsHandler();
MyThrowsHandler th = new MyThrowsHandler();
ThrowsAdviceInterceptorTests.Echo target = new ThrowsAdviceInterceptorTests.Echo();
Echo target = new Echo();
target.setA(16);
ProxyFactory pf = new ProxyFactory(target);
pf.addAdvice(new NopInterceptor());
pf.addAdvice(th);
ThrowsAdviceInterceptorTests.IEcho proxied = (ThrowsAdviceInterceptorTests.IEcho) createProxy(pf);
IEcho proxied = (IEcho) createProxy(pf);
assertEquals(0, th.getCalls());
assertEquals(target.getA(), proxied.getA());
assertEquals(0, th.getCalls());
@ -1536,7 +1535,6 @@ public abstract class AbstractAopProxyTests extends TestCase {
assertEquals(1, th.getCalls("remoteException"));
}
private static class CheckMethodInvocationIsSameInAndOutInterceptor implements MethodInterceptor {
public Object invoke(MethodInvocation mi) throws Throwable {

View File

@ -0,0 +1,20 @@
/**
*
*/
package org.springframework.aop.framework;
class Echo implements IEcho {
private int a;
public int echoException(int i, Throwable t) throws Throwable {
if (t != null)
throw t;
return i;
}
public void setA(int a) {
this.a = a;
}
public int getA() {
return a;
}
}

View File

@ -0,0 +1,10 @@
/**
*
*/
package org.springframework.aop.framework;
interface IEcho {
int echoException(int i, Throwable t) throws Throwable;
int getA();
void setA(int a);
}

View File

@ -0,0 +1,25 @@
/**
*
*/
package org.springframework.aop.framework;
import java.io.IOException;
import java.lang.reflect.Method;
import java.rmi.RemoteException;
import org.springframework.aop.ThrowsAdvice;
@SuppressWarnings("serial") class MyThrowsHandler extends MethodCounter implements ThrowsAdvice {
// Full method signature
public void afterThrowing(Method m, Object[] args, Object target, IOException ex) {
count("ioException");
}
public void afterThrowing(RemoteException ex) throws Throwable {
count("remoteException");
}
/** Not valid, wrong number of arguments */
public void afterThrowing(Method m, Exception ex) throws Throwable {
throw new UnsupportedOperationException("Shouldn't be called");
}
}

View File

@ -31,7 +31,6 @@ import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.ClassFilter;
import org.springframework.aop.IntroductionAdvisor;
import org.springframework.aop.IntroductionInterceptor;
import org.springframework.aop.framework.adapter.ThrowsAdviceInterceptorTests;
import org.springframework.aop.interceptor.DebugInterceptor;
import org.springframework.aop.interceptor.NopInterceptor;
import org.springframework.aop.interceptor.SideEffectBean;
@ -404,11 +403,11 @@ public class ProxyFactoryBeanTests extends TestCase {
public void testCanAddThrowsAdviceWithoutAdvisor() throws Throwable {
BeanFactory f = new XmlBeanFactory(new ClassPathResource("throwsAdvice.xml", getClass()));
ThrowsAdviceInterceptorTests.MyThrowsHandler th = (ThrowsAdviceInterceptorTests.MyThrowsHandler) f.getBean("throwsAdvice");
MyThrowsHandler th = (MyThrowsHandler) f.getBean("throwsAdvice");
CountingBeforeAdvice cba = (CountingBeforeAdvice) f.getBean("countingBeforeAdvice");
assertEquals(0, cba.getCalls());
assertEquals(0, th.getCalls());
ThrowsAdviceInterceptorTests.IEcho echo = (ThrowsAdviceInterceptorTests.IEcho) f.getBean("throwsAdvised");
IEcho echo = (IEcho) f.getBean("throwsAdvised");
int i = 12;
echo.setA(i);
assertEquals(i, echo.getA());