moving unit tests from .testsuite -> .aop

This commit is contained in:
Chris Beams 2008-12-13 01:07:30 +00:00
parent 40016fc902
commit e3ec177aab
13 changed files with 105 additions and 66 deletions

View File

@ -34,7 +34,7 @@ public interface ClassFilter {
* @param clazz the candidate target class
* @return whether the advice should apply to the given target class
*/
boolean matches(Class clazz);
boolean matches(Class<?> clazz);
/**

View File

@ -16,54 +16,49 @@
package org.springframework.aop.scope;
import junit.framework.TestCase;
import org.easymock.MockControl;
import static org.easymock.EasyMock.*;
import org.junit.Test;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.test.AssertThrows;
/**
* Unit tests for the {@link DefaultScopedObject} class.
*
* @author Rick Evans
*/
public final class DefaultScopedObjectTests extends TestCase {
public final class DefaultScopedObjectTests {
private static final String GOOD_BEAN_NAME = "foo";
@Test(expected=IllegalArgumentException.class)
public void testCtorWithNullBeanFactory() throws Exception {
new AssertThrows(IllegalArgumentException.class) {
public void test() throws Exception {
new DefaultScopedObject(null, GOOD_BEAN_NAME);
}
}.runTest();
}
@Test(expected=IllegalArgumentException.class)
public void testCtorWithNullTargetBeanName() throws Exception {
testBadTargetBeanName(null);
}
@Test(expected=IllegalArgumentException.class)
public void testCtorWithEmptyTargetBeanName() throws Exception {
testBadTargetBeanName("");
}
@Test(expected=IllegalArgumentException.class)
public void testCtorWithJustWhitespacedTargetBeanName() throws Exception {
testBadTargetBeanName(" ");
}
private static void testBadTargetBeanName(final String badTargetBeanName) {
MockControl mock = MockControl.createControl(ConfigurableBeanFactory.class);
final ConfigurableBeanFactory factory = (ConfigurableBeanFactory) mock.getMock();
mock.replay();
ConfigurableBeanFactory factory = createMock(ConfigurableBeanFactory.class);
replay(factory);
new AssertThrows(IllegalArgumentException.class) {
public void test() throws Exception {
new DefaultScopedObject(factory, badTargetBeanName);
}
}.runTest();
mock.verify();
verify(factory);
}
}

View File

@ -16,16 +16,19 @@
package org.springframework.aop.scope;
import junit.framework.TestCase;
import static org.junit.Assert.assertSame;
import org.junit.Test;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
/**
* @author Mark Fisher
* @author Chris Beams
*/
public class ScopedProxyAutowireTests extends TestCase {
public class ScopedProxyAutowireTests {
@Test
public void testScopedProxyInheritsAutowireCandidateFalse() {
XmlBeanFactory bf = new XmlBeanFactory(new ClassPathResource("scopedAutowireFalse.xml", getClass()));
TestBean autowired = (TestBean) bf.getBean("autowired");
@ -33,6 +36,7 @@ public class ScopedProxyAutowireTests extends TestCase {
assertSame(unscoped, autowired.getChild());
}
@Test
public void testScopedProxyReplacesAutowireCandidateTrue() {
XmlBeanFactory bf = new XmlBeanFactory(new ClassPathResource("scopedAutowireTrue.xml", getClass()));
TestBean autowired = (TestBean) bf.getBean("autowired");
@ -41,7 +45,7 @@ public class ScopedProxyAutowireTests extends TestCase {
}
public static class TestBean {
static class TestBean {
private TestBean child;

View File

@ -16,31 +16,37 @@
package org.springframework.aop.support;
import javax.servlet.ServletException;
import static org.junit.Assert.*;
import junit.framework.TestCase;
import java.io.IOException;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.TestBean;
import org.springframework.util.SerializationTestUtils;
/**
* @author Rod Johnson
* @author Dmitriy Kopylenko
* @author Chris Beams
*/
public abstract class AbstractRegexpMethodPointcutTests extends TestCase {
public abstract class AbstractRegexpMethodPointcutTests {
private AbstractRegexpMethodPointcut rpc;
protected void setUp() {
@Before
public void setUp() {
rpc = getRegexpMethodPointcut();
}
protected abstract AbstractRegexpMethodPointcut getRegexpMethodPointcut();
@Test
public void testNoPatternSupplied() throws Exception {
noPatternSuppliedTests(rpc);
}
@Test
public void testSerializationWithNoPatternSupplied() throws Exception {
rpc = (AbstractRegexpMethodPointcut) SerializationTestUtils.serializeAndDeserialize(rpc);
noPatternSuppliedTests(rpc);
@ -52,6 +58,7 @@ public abstract class AbstractRegexpMethodPointcutTests extends TestCase {
assertEquals(0, rpc.getPatterns().length);
}
@Test
public void testExactMatch() throws Exception {
rpc.setPattern("java.lang.Object.hashCode");
exactMatchTests(rpc);
@ -66,36 +73,41 @@ public abstract class AbstractRegexpMethodPointcutTests extends TestCase {
assertFalse(rpc.matches(Object.class.getMethod("wait", (Class[]) null), Object.class));
}
@Test
public void testSpecificMatch() throws Exception {
rpc.setPattern("java.lang.String.hashCode");
assertTrue(rpc.matches(Object.class.getMethod("hashCode", (Class[]) null), String.class));
assertFalse(rpc.matches(Object.class.getMethod("hashCode", (Class[]) null), Object.class));
}
@Test
public void testWildcard() throws Exception {
rpc.setPattern(".*Object.hashCode");
assertTrue(rpc.matches(Object.class.getMethod("hashCode", (Class[]) null), Object.class));
assertFalse(rpc.matches(Object.class.getMethod("wait", (Class[]) null), Object.class));
}
@Test
public void testWildcardForOneClass() throws Exception {
rpc.setPattern("java.lang.Object.*");
assertTrue(rpc.matches(Object.class.getMethod("hashCode", (Class[]) null), String.class));
assertTrue(rpc.matches(Object.class.getMethod("wait", (Class[]) null), String.class));
}
@Test
public void testMatchesObjectClass() throws Exception {
rpc.setPattern("java.lang.Object.*");
assertTrue(rpc.matches(Exception.class.getMethod("hashCode", (Class[]) null), ServletException.class));
assertTrue(rpc.matches(Exception.class.getMethod("hashCode", (Class[]) null), IOException.class));
// Doesn't match a method from Throwable
assertFalse(rpc.matches(Exception.class.getMethod("getMessage", (Class[]) null), Exception.class));
}
@Test
public void testWithExclusion() throws Exception {
this.rpc.setPattern(".*get.*");
this.rpc.setExcludedPattern(".*Age.*");
assertTrue(this.rpc.matches(TestBean.class.getMethod("getName", null), TestBean.class));
assertFalse(this.rpc.matches(TestBean.class.getMethod("getAge", null), TestBean.class));
assertTrue(this.rpc.matches(TestBean.class.getMethod("getName"), TestBean.class));
assertFalse(this.rpc.matches(TestBean.class.getMethod("getAge"), TestBean.class));
}
}

View File

@ -16,10 +16,11 @@
package org.springframework.aop.support;
import static org.junit.Assert.*;
import java.lang.reflect.Method;
import junit.framework.TestCase;
import org.junit.Test;
import org.springframework.aop.ClassFilter;
import org.springframework.aop.MethodMatcher;
import org.springframework.aop.Pointcut;
@ -31,12 +32,14 @@ import org.springframework.util.SerializationTestUtils;
/**
* @author Rod Johnson
* @author Chris Beams
*/
public class AopUtilsTests extends TestCase {
public class AopUtilsTests {
@Test
public void testPointcutCanNeverApply() {
class TestPointcut extends StaticMethodMatcherPointcut {
public boolean matches(Method method, Class clazzy) {
public boolean matches(Method method, Class<?> clazzy) {
return false;
}
}
@ -45,14 +48,16 @@ public class AopUtilsTests extends TestCase {
assertFalse(AopUtils.canApply(no, Object.class));
}
@Test
public void testPointcutAlwaysApplies() {
assertTrue(AopUtils.canApply(new DefaultPointcutAdvisor(new NopInterceptor()), Object.class));
assertTrue(AopUtils.canApply(new DefaultPointcutAdvisor(new NopInterceptor()), TestBean.class));
}
@Test
public void testPointcutAppliesToOneMethodOnObject() {
class TestPointcut extends StaticMethodMatcherPointcut {
public boolean matches(Method method, Class clazz) {
public boolean matches(Method method, Class<?> clazz) {
return method.getName().equals("hashCode");
}
}
@ -68,6 +73,7 @@ public class AopUtilsTests extends TestCase {
* of AOP classes, they return the same instance, not a new instance
* that's subverted the singleton construction limitation.
*/
@Test
public void testCanonicalFrameworkClassesStillCanonicalOnDeserialization() throws Exception {
assertSame(MethodMatcher.TRUE, SerializationTestUtils.serializeAndDeserialize(MethodMatcher.TRUE));
assertSame(ClassFilter.TRUE, SerializationTestUtils.serializeAndDeserialize(ClassFilter.TRUE));

View File

@ -17,10 +17,11 @@
package org.springframework.aop.support;
import static org.junit.Assert.*;
import java.lang.reflect.Method;
import junit.framework.TestCase;
import org.junit.Test;
import org.springframework.aop.ClassFilter;
import org.springframework.aop.MethodMatcher;
import org.springframework.aop.Pointcut;
@ -29,39 +30,42 @@ import org.springframework.core.NestedRuntimeException;
/**
* @author Rod Johnson
* @author Chris Beams
*/
public class ComposablePointcutTests extends TestCase {
public class ComposablePointcutTests {
public static MethodMatcher GETTER_METHOD_MATCHER = new StaticMethodMatcher() {
public boolean matches(Method m, Class targetClass) {
public boolean matches(Method m, Class<?> targetClass) {
return m.getName().startsWith("get");
}
};
public static MethodMatcher GET_AGE_METHOD_MATCHER = new StaticMethodMatcher() {
public boolean matches(Method m, Class targetClass) {
public boolean matches(Method m, Class<?> targetClass) {
return m.getName().equals("getAge");
}
};
public static MethodMatcher ABSQUATULATE_METHOD_MATCHER = new StaticMethodMatcher() {
public boolean matches(Method m, Class targetClass) {
public boolean matches(Method m, Class<?> targetClass) {
return m.getName().equals("absquatulate");
}
};
public static MethodMatcher SETTER_METHOD_MATCHER = new StaticMethodMatcher() {
public boolean matches(Method m, Class targetClass) {
public boolean matches(Method m, Class<?> targetClass) {
return m.getName().startsWith("set");
}
};
@Test
public void testMatchAll() throws NoSuchMethodException {
Pointcut pc = new ComposablePointcut();
assertTrue(pc.getClassFilter().matches(Object.class));
assertTrue(pc.getMethodMatcher().matches(Object.class.getMethod("hashCode", (Class[]) null), Exception.class));
}
@Test
public void testFilterByClass() throws NoSuchMethodException {
ComposablePointcut pc = new ComposablePointcut();
@ -81,6 +85,7 @@ public class ComposablePointcutTests extends TestCase {
assertTrue(pc.getClassFilter().matches(NestedRuntimeException.class));
}
@Test
public void testUnionMethodMatcher() {
// Matches the getAge() method in any class
ComposablePointcut pc = new ComposablePointcut(ClassFilter.TRUE, GET_AGE_METHOD_MATCHER);
@ -103,6 +108,7 @@ public class ComposablePointcutTests extends TestCase {
assertFalse(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_SET_AGE, TestBean.class, null));
}
@Test
public void testIntersectionMethodMatcher() {
ComposablePointcut pc = new ComposablePointcut();
assertTrue(pc.getMethodMatcher().matches(PointcutsTests.TEST_BEAN_ABSQUATULATE, TestBean.class));
@ -119,6 +125,7 @@ public class ComposablePointcutTests extends TestCase {
assertFalse(Pointcuts.matches(pc, PointcutsTests.TEST_BEAN_GET_NAME, TestBean.class, null));
}
@Test
public void testEqualsAndHashCode() throws Exception {
ComposablePointcut pc1 = new ComposablePointcut();
ComposablePointcut pc2 = new ComposablePointcut();

View File

@ -16,8 +16,9 @@
package org.springframework.aop.support;
import junit.framework.TestCase;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.aop.Pointcut;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.interceptor.NopInterceptor;
@ -25,15 +26,12 @@ import org.springframework.beans.ITestBean;
import org.springframework.beans.TestBean;
/**
*
* @author Rod Johnson
* @author Chris Beams
*/
public class ControlFlowPointcutTests extends TestCase {
public ControlFlowPointcutTests(String s) {
super(s);
}
public class ControlFlowPointcutTests {
@Test
public void testMatches() {
TestBean target = new TestBean();
target.setAge(27);
@ -64,6 +62,7 @@ public class ControlFlowPointcutTests extends TestCase {
* to the cflow pointcut, meaning that it's not so prohibitively
* expensive.
*/
@Test
public void testSelectiveApplication() {
TestBean target = new TestBean();
target.setAge(27);
@ -90,6 +89,7 @@ public class ControlFlowPointcutTests extends TestCase {
assertEquals(1, cflow.getEvaluations());
}
@Test
public void testEqualsAndHashCode() throws Exception {
assertEquals(new ControlFlowPointcut(One.class), new ControlFlowPointcut(One.class));
assertEquals(new ControlFlowPointcut(One.class, "getAge"), new ControlFlowPointcut(One.class, "getAge"));

View File

@ -16,19 +16,20 @@
package org.springframework.aop.support;
import static org.junit.Assert.*;
import java.lang.reflect.Method;
import org.junit.Test;
import org.springframework.aop.ClassFilter;
import org.springframework.aop.Pointcut;
import org.springframework.beans.TestBean;
import junit.framework.TestCase;
/**
*
* @author Rod Johnson
* @author Chris Beams
*/
public class PointcutsTests extends TestCase {
public class PointcutsTests {
public static Method TEST_BEAN_SET_AGE;
public static Method TEST_BEAN_GET_AGE;
@ -53,13 +54,13 @@ public class PointcutsTests extends TestCase {
public static Pointcut allTestBeanMethodsPointcut = new StaticMethodMatcherPointcut() {
public ClassFilter getClassFilter() {
return new ClassFilter() {
public boolean matches(Class clazz) {
public boolean matches(Class<?> clazz) {
return clazz.equals(TestBean.class);
}
};
}
public boolean matches(Method m, Class targetClass) {
public boolean matches(Method m, Class<?> targetClass) {
return true;
}
};
@ -75,7 +76,7 @@ public class PointcutsTests extends TestCase {
return new RootClassFilter(MyTestBean.class);
}
public boolean matches(Method m, Class targetClass) {
public boolean matches(Method m, Class<?> targetClass) {
return m.getName().startsWith("set");
}
};
@ -86,7 +87,7 @@ public class PointcutsTests extends TestCase {
return new RootClassFilter(MyTestBean.class);
}
public boolean matches(Method m, Class targetClass) {
public boolean matches(Method m, Class<?> targetClass) {
return m.getName().startsWith("get");
}
};
@ -100,7 +101,7 @@ public class PointcutsTests extends TestCase {
return new RootClassFilter(MyTestBeanSubclass.class);
}
public boolean matches(Method m, Class targetClass) {
public boolean matches(Method m, Class<?> targetClass) {
return m.getName().startsWith("get");
}
};
@ -112,10 +113,7 @@ public class PointcutsTests extends TestCase {
public static Pointcut allClassGetNamePointcut = new NameMatchMethodPointcut().addMethodName("getName");
public PointcutsTests(String s) {
super(s);
}
@Test
public void testTrue() {
assertTrue(Pointcuts.matches(Pointcut.TRUE, TEST_BEAN_SET_AGE, TestBean.class, new Object[] { new Integer(6)}));
assertTrue(Pointcuts.matches(Pointcut.TRUE, TEST_BEAN_GET_AGE, TestBean.class, null));
@ -125,6 +123,7 @@ public class PointcutsTests extends TestCase {
assertTrue(Pointcuts.matches(Pointcut.TRUE, TEST_BEAN_ABSQUATULATE, TestBean.class, null));
}
@Test
public void testMatches() {
assertTrue(Pointcuts.matches(allClassSetterPointcut, TEST_BEAN_SET_AGE, TestBean.class, new Object[] { new Integer(6)}));
assertFalse(Pointcuts.matches(allClassSetterPointcut, TEST_BEAN_GET_AGE, TestBean.class, null));
@ -137,6 +136,7 @@ public class PointcutsTests extends TestCase {
/**
* Should match all setters and getters on any class
*/
@Test
public void testUnionOfSettersAndGetters() {
Pointcut union = Pointcuts.union(allClassGetterPointcut, allClassSetterPointcut);
assertTrue(Pointcuts.matches(union, TEST_BEAN_SET_AGE, TestBean.class, new Object[] { new Integer(6)}));
@ -144,6 +144,7 @@ public class PointcutsTests extends TestCase {
assertFalse(Pointcuts.matches(union, TEST_BEAN_ABSQUATULATE, TestBean.class, null));
}
@Test
public void testUnionOfSpecificGetters() {
Pointcut union = Pointcuts.union(allClassGetAgePointcut, allClassGetNamePointcut);
assertFalse(Pointcuts.matches(union, TEST_BEAN_SET_AGE, TestBean.class, new Object[] { new Integer(6)}));
@ -167,6 +168,7 @@ public class PointcutsTests extends TestCase {
* Tests vertical composition. First pointcut matches all setters.
* Second one matches all getters in the MyTestBean class. TestBean getters shouldn't pass.
*/
@Test
public void testUnionOfAllSettersAndSubclassSetters() {
assertFalse(Pointcuts.matches(myTestBeanSetterPointcut, TEST_BEAN_SET_AGE, TestBean.class, new Object[] { new Integer(6)}));
assertTrue(Pointcuts.matches(myTestBeanSetterPointcut, TEST_BEAN_SET_AGE, MyTestBean.class, new Object[] { new Integer(6)}));
@ -184,6 +186,7 @@ public class PointcutsTests extends TestCase {
* Intersection should be MyTestBean getAge() only:
* it's the union of allClassGetAge and subclass getters
*/
@Test
public void testIntersectionOfSpecificGettersAndSubclassGetters() {
assertTrue(Pointcuts.matches(allClassGetAgePointcut, TEST_BEAN_GET_AGE, TestBean.class, null));
assertTrue(Pointcuts.matches(allClassGetAgePointcut, TEST_BEAN_GET_AGE, MyTestBean.class, null));
@ -229,6 +232,7 @@ public class PointcutsTests extends TestCase {
/**
* The intersection of these two pointcuts leaves nothing.
*/
@Test
public void testSimpleIntersection() {
Pointcut intersection = Pointcuts.intersection(allClassGetterPointcut, allClassSetterPointcut);
assertFalse(Pointcuts.matches(intersection, TEST_BEAN_SET_AGE, TestBean.class, new Object[] { new Integer(6)}));

View File

@ -16,12 +16,13 @@
package org.springframework.aop.scope;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import junit.framework.TestCase;
import org.junit.Test;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.ITestBean;
import org.springframework.beans.TestBean;
@ -34,16 +35,19 @@ import org.springframework.core.io.ClassPathResource;
/**
* @author Rob Harrop
* @author Juergen Hoeller
* @author Chris Beams
*/
public class ScopedProxyTests extends TestCase {
public class ScopedProxyTests {
/* SPR-2108 */
@Test
public void testProxyAssignable() throws Exception {
XmlBeanFactory bf = new XmlBeanFactory(new ClassPathResource("scopedMap.xml", getClass()));
Object baseMap = bf.getBean("singletonMap");
assertTrue(baseMap instanceof Map);
}
@Test
public void testSimpleProxy() throws Exception {
XmlBeanFactory bf = new XmlBeanFactory(new ClassPathResource("scopedMap.xml", getClass()));
Object simpleMap = bf.getBean("simpleMap");
@ -51,6 +55,7 @@ public class ScopedProxyTests extends TestCase {
assertTrue(simpleMap instanceof HashMap);
}
@Test
public void testScopedOverride() throws Exception {
GenericApplicationContext ctx = new GenericApplicationContext();
new XmlBeanDefinitionReader(ctx).loadBeanDefinitions(new ClassPathResource("scopedOverride.xml", getClass()));
@ -66,6 +71,7 @@ public class ScopedProxyTests extends TestCase {
assertEquals(TestBean.class, scope.getMap().get("scopedTarget.testBean").getClass());
}
@Test
public void testJdkScopedProxy() throws Exception {
XmlBeanFactory bf = new XmlBeanFactory(new ClassPathResource("scopedTestBean.xml", getClass()));
SimpleMapScope scope = new SimpleMapScope();
@ -82,6 +88,7 @@ public class ScopedProxyTests extends TestCase {
assertEquals(TestBean.class, scope.getMap().get("testBeanTarget").getClass());
}
@Test
public void testCglibScopedProxy() {
XmlBeanFactory bf = new XmlBeanFactory(new ClassPathResource("scopedList.xml", getClass()));
SimpleMapScope scope = new SimpleMapScope();

View File

@ -17,8 +17,9 @@
package org.springframework.aop.support;
import junit.framework.TestCase;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.aop.ClassFilter;
import org.springframework.beans.ITestBean;
import org.springframework.beans.TestBean;
@ -26,8 +27,9 @@ import org.springframework.core.NestedRuntimeException;
/**
* @author Rod Johnson
* @author Chris Beams
*/
public class ClassFiltersTests extends TestCase {
public class ClassFiltersTests {
private ClassFilter exceptionFilter = new RootClassFilter(Exception.class);
@ -35,6 +37,7 @@ public class ClassFiltersTests extends TestCase {
private ClassFilter hasRootCauseFilter = new RootClassFilter(NestedRuntimeException.class);
@Test
public void testUnion() {
assertTrue(exceptionFilter.matches(RuntimeException.class));
assertFalse(exceptionFilter.matches(TestBean.class));
@ -45,6 +48,7 @@ public class ClassFiltersTests extends TestCase {
assertTrue(union.matches(TestBean.class));
}
@Test
public void testIntersection() {
assertTrue(exceptionFilter.matches(RuntimeException.class));
assertTrue(hasRootCauseFilter.matches(NestedRuntimeException.class));