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

@@ -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

@@ -0,0 +1,179 @@
/*
* Copyright 2002-2005 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.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.transaction.TransactionRolledbackException;
import org.aopalliance.intercept.MethodInvocation;
import org.junit.Test;
import org.springframework.aop.ThrowsAdvice;
import org.springframework.aop.framework.MethodCounter;
/**
* @author Rod Johnson
* @author Chris Beams
*/
public class ThrowsAdviceInterceptorTests {
@Test
public void testNoHandlerMethods() {
Object o = new Object();
try {
new ThrowsAdviceInterceptor(o);
fail("Should require one handler method at least");
}
catch (IllegalArgumentException ex) {
// Ok
}
}
@Test
public void testNotInvoked() throws Throwable {
MyThrowsHandler th = new MyThrowsHandler();
ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th);
Object ret = new Object();
MethodInvocation mi = createMock(MethodInvocation.class);
expect(mi.proceed()).andReturn(ret);
replay(mi);
assertEquals(ret, ti.invoke(mi));
assertEquals(0, th.getCalls());
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();
MethodInvocation mi = createMock(MethodInvocation.class);
expect(mi.proceed()).andThrow(ex);
replay(mi);
try {
ti.invoke(mi);
fail();
}
catch (Exception caught) {
assertEquals(ex, caught);
}
assertEquals(0, th.getCalls());
verify(mi);
}
@Test
public void testCorrectHandlerUsed() throws Throwable {
MyThrowsHandler th = new MyThrowsHandler();
ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th);
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();
}
catch (Exception caught) {
assertEquals(ex, caught);
}
assertEquals(1, th.getCalls());
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();
MethodInvocation mi = createMock(MethodInvocation.class);
expect(mi.proceed()).andThrow(ex);
replay(mi);
try {
ti.invoke(mi);
fail();
}
catch (Exception caught) {
assertEquals(ex, caught);
}
assertEquals(1, th.getCalls());
assertEquals(1, th.getCalls("remoteException"));
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();
MethodInvocation mi = createMock(MethodInvocation.class);
expect(mi.proceed()).andThrow(ex);
replay(mi);
try {
ti.invoke(mi);
fail();
}
catch (Throwable caught) {
assertEquals(t, caught);
}
assertEquals(1, th.getCalls());
assertEquals(1, th.getCalls("remoteException"));
verify(mi);
}
@SuppressWarnings("serial")
private static 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");
}
}
}