diff --git a/org.springframework.aop/src/test/java/org/springframework/aop/interceptor/SerializableNopInterceptor.java b/org.springframework.aop/src/test/java/org/springframework/aop/interceptor/SerializableNopInterceptor.java new file mode 100644 index 00000000000..328f6cf9b2f --- /dev/null +++ b/org.springframework.aop/src/test/java/org/springframework/aop/interceptor/SerializableNopInterceptor.java @@ -0,0 +1,45 @@ +/* + * 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.interceptor; + +import java.io.Serializable; + + +/** + * Subclass of NopInterceptor that is serializable and + * can be used to test proxy serialization. + * + * @author Rod Johnson + */ +public class SerializableNopInterceptor extends NopInterceptor implements Serializable { + + /** + * We must override this field and the related methods as + * otherwise count won't be serialized from the non-serializable + * NopInterceptor superclass. + */ + private int count; + + public int getCount() { + return this.count; + } + + protected void increment() { + ++count; + } + +} \ No newline at end of file diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/support/ClassFiltersTests.java b/org.springframework.aop/src/test/java/org/springframework/aop/support/ClassFiltersTests.java similarity index 100% rename from org.springframework.testsuite/src/test/java/org/springframework/aop/support/ClassFiltersTests.java rename to org.springframework.aop/src/test/java/org/springframework/aop/support/ClassFiltersTests.java diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/support/DelegatingIntroductionInterceptorTests.java b/org.springframework.aop/src/test/java/org/springframework/aop/support/DelegatingIntroductionInterceptorTests.java similarity index 84% rename from org.springframework.testsuite/src/test/java/org/springframework/aop/support/DelegatingIntroductionInterceptorTests.java rename to org.springframework.aop/src/test/java/org/springframework/aop/support/DelegatingIntroductionInterceptorTests.java index 0c6af1f069a..a3d4f35953a 100644 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/support/DelegatingIntroductionInterceptorTests.java +++ b/org.springframework.aop/src/test/java/org/springframework/aop/support/DelegatingIntroductionInterceptorTests.java @@ -16,12 +16,13 @@ package org.springframework.aop.support; +import static org.easymock.EasyMock.*; +import static org.junit.Assert.*; + import java.io.Serializable; -import junit.framework.TestCase; import org.aopalliance.intercept.MethodInterceptor; -import org.easymock.MockControl; - +import org.junit.Test; import org.springframework.aop.IntroductionAdvisor; import org.springframework.aop.IntroductionInterceptor; import org.springframework.aop.framework.ProxyFactory; @@ -37,71 +38,65 @@ import org.springframework.util.SerializationTestUtils; /** * @author Rod Johnson + * @author Chris Beams * @since 13.05.2003 */ -public class DelegatingIntroductionInterceptorTests extends TestCase { +public class DelegatingIntroductionInterceptorTests { + @Test(expected=IllegalArgumentException.class) public void testNullTarget() throws Exception { - try { - IntroductionInterceptor ii = new DelegatingIntroductionInterceptor(null); - fail("Shouldn't accept null target"); - } - catch (IllegalArgumentException ex) { - // OK - } + // Shouldn't accept null target + new DelegatingIntroductionInterceptor(null); } + @Test public void testIntroductionInterceptorWithDelegation() throws Exception { TestBean raw = new TestBean(); assertTrue(! (raw instanceof TimeStamped)); ProxyFactory factory = new ProxyFactory(raw); - MockControl tsControl = MockControl.createControl(TimeStamped.class); - TimeStamped ts = (TimeStamped) tsControl.getMock(); - ts.getTimeStamp(); + TimeStamped ts = createMock(TimeStamped.class); long timestamp = 111L; - tsControl.setReturnValue(timestamp, 1); - tsControl.replay(); + expect(ts.getTimeStamp()).andReturn(timestamp); + replay(ts); factory.addAdvisor(0, new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts))); TimeStamped tsp = (TimeStamped) factory.getProxy(); assertTrue(tsp.getTimeStamp() == timestamp); - tsControl.verify(); + verify(ts); } + @Test public void testIntroductionInterceptorWithInterfaceHierarchy() throws Exception { TestBean raw = new TestBean(); assertTrue(! (raw instanceof SubTimeStamped)); ProxyFactory factory = new ProxyFactory(raw); - MockControl tsControl = MockControl.createControl(SubTimeStamped.class); - SubTimeStamped ts = (SubTimeStamped) tsControl.getMock(); - ts.getTimeStamp(); + TimeStamped ts = createMock(SubTimeStamped.class); long timestamp = 111L; - tsControl.setReturnValue(timestamp, 1); - tsControl.replay(); - + expect(ts.getTimeStamp()).andReturn(timestamp); + replay(ts); + factory.addAdvisor(0, new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts), SubTimeStamped.class)); SubTimeStamped tsp = (SubTimeStamped) factory.getProxy(); assertTrue(tsp.getTimeStamp() == timestamp); - tsControl.verify(); + verify(ts); } + @Test public void testIntroductionInterceptorWithSuperInterface() throws Exception { TestBean raw = new TestBean(); assertTrue(! (raw instanceof TimeStamped)); ProxyFactory factory = new ProxyFactory(raw); - MockControl tsControl = MockControl.createControl(SubTimeStamped.class); - SubTimeStamped ts = (SubTimeStamped) tsControl.getMock(); - ts.getTimeStamp(); + TimeStamped ts = createMock(SubTimeStamped.class); long timestamp = 111L; - tsControl.setReturnValue(timestamp, 1); - tsControl.replay(); + expect(ts.getTimeStamp()).andReturn(timestamp); + replay(ts); factory.addAdvisor(0, new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts), TimeStamped.class)); @@ -109,9 +104,10 @@ public class DelegatingIntroductionInterceptorTests extends TestCase { assertTrue(!(tsp instanceof SubTimeStamped)); assertTrue(tsp.getTimeStamp() == timestamp); - tsControl.verify(); + verify(ts); } + @Test public void testAutomaticInterfaceRecognitionInDelegate() throws Exception { final long t = 1001L; class Tester implements TimeStamped, ITester { @@ -139,8 +135,10 @@ public class DelegatingIntroductionInterceptorTests extends TestCase { } + @Test public void testAutomaticInterfaceRecognitionInSubclass() throws Exception { final long t = 1001L; + @SuppressWarnings("serial") class TestII extends DelegatingIntroductionInterceptor implements TimeStamped, ITester { public void foo() throws Exception { } @@ -180,6 +178,8 @@ public class DelegatingIntroductionInterceptorTests extends TestCase { assertTrue(!(o instanceof TimeStamped)); } + @SuppressWarnings("serial") + @Test public void testIntroductionInterceptorDoesntReplaceToString() throws Exception { TestBean raw = new TestBean(); assertTrue(! (raw instanceof TimeStamped)); @@ -199,6 +199,7 @@ public class DelegatingIntroductionInterceptorTests extends TestCase { assertEquals(raw.toString(), tsp.toString()); } + @Test public void testDelegateReturnsThisIsMassagedToReturnProxy() { NestedTestBean target = new NestedTestBean(); String company = "Interface21"; @@ -218,6 +219,7 @@ public class DelegatingIntroductionInterceptorTests extends TestCase { assertTrue("Introduced method returning delegate returns proxy", AopUtils.isAopProxy(introduction.getSpouse())); } + @Test public void testSerializableDelegatingIntroductionInterceptorSerializable() throws Exception { SerializablePerson serializableTarget = new SerializablePerson(); String name = "Tony"; @@ -241,23 +243,11 @@ public class DelegatingIntroductionInterceptorTests extends TestCase { assertEquals(time, ((TimeStamped) p1).getTimeStamp()); } -// public void testDelegatingIntroductionInterceptorDoesntMakeNonserializableSerializable() throws Exception { -// // Target is NOT serialiable -// TestBean raw = new TestBean(); -// ProxyFactory factory = new ProxyFactory(raw); -// factory.addInterface(Person.class); -// long time = 1000; -// TimeStamped ts = new SerializableTimeStamped(time); -// -// factory.addAdvisor(new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts))); -// Object proxy = factory.getProxy(); -// -// assertFalse(proxy instanceof Serializable); -// } - // Test when target implements the interface: should get interceptor by preference. + @Test public void testIntroductionMasksTargetImplementation() throws Exception { final long t = 1001L; + @SuppressWarnings("serial") class TestII extends DelegatingIntroductionInterceptor implements TimeStamped { public long getTimeStamp() { return t; @@ -278,6 +268,7 @@ public class DelegatingIntroductionInterceptorTests extends TestCase { } + @SuppressWarnings("serial") private static class SerializableTimeStamped implements TimeStamped, Serializable { private final long ts; diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/support/MethodMatchersTests.java b/org.springframework.aop/src/test/java/org/springframework/aop/support/MethodMatchersTests.java similarity index 96% rename from org.springframework.testsuite/src/test/java/org/springframework/aop/support/MethodMatchersTests.java rename to org.springframework.aop/src/test/java/org/springframework/aop/support/MethodMatchersTests.java index 8693653cad1..b19fca1fc94 100644 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/support/MethodMatchersTests.java +++ b/org.springframework.aop/src/test/java/org/springframework/aop/support/MethodMatchersTests.java @@ -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.MethodMatcher; import org.springframework.beans.IOther; import org.springframework.beans.ITestBean; @@ -27,9 +28,10 @@ import org.springframework.beans.TestBean; import org.springframework.util.SerializationTestUtils; /** - * $Id: MethodMatchersTests.java,v 1.7 2005/03/25 09:28:18 jhoeller Exp $ + * @author Juergen Hoeller + * @author Chris Beams */ -public class MethodMatchersTests extends TestCase { +public class MethodMatchersTests { private final Method EXCEPTION_GETMESSAGE; @@ -46,16 +48,19 @@ public class MethodMatchersTests extends TestCase { IOTHER_ABSQUATULATE = IOther.class.getMethod("absquatulate", (Class[]) null); } + @Test public void testDefaultMatchesAll() throws Exception { MethodMatcher defaultMm = MethodMatcher.TRUE; assertTrue(defaultMm.matches(EXCEPTION_GETMESSAGE, Exception.class)); assertTrue(defaultMm.matches(ITESTBEAN_SETAGE, TestBean.class)); } + @Test public void testMethodMatcherTrueSerializable() throws Exception { assertSame(SerializationTestUtils.serializeAndDeserialize(MethodMatcher.TRUE), MethodMatcher.TRUE); } + @Test public void testSingle() throws Exception { MethodMatcher defaultMm = MethodMatcher.TRUE; assertTrue(defaultMm.matches(EXCEPTION_GETMESSAGE, Exception.class)); @@ -67,6 +72,7 @@ public class MethodMatchersTests extends TestCase { } + @Test public void testDynamicAndStaticMethodMatcherIntersection() throws Exception { MethodMatcher mm1 = MethodMatcher.TRUE; MethodMatcher mm2 = new TestDynamicMethodMatcherWhichMatches(); @@ -81,6 +87,7 @@ public class MethodMatchersTests extends TestCase { assertFalse("3 - not Matched setAge method", intersection.matches(ITESTBEAN_SETAGE, TestBean.class, new Object[] { new Integer(5) })); } + @Test public void testStaticMethodMatcherUnion() throws Exception { MethodMatcher getterMatcher = new StartsWithMatcher("get"); MethodMatcher setterMatcher = new StartsWithMatcher("set"); diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/support/NameMatchMethodPointcutTests.java b/org.springframework.aop/src/test/java/org/springframework/aop/support/NameMatchMethodPointcutTests.java similarity index 94% rename from org.springframework.testsuite/src/test/java/org/springframework/aop/support/NameMatchMethodPointcutTests.java rename to org.springframework.aop/src/test/java/org/springframework/aop/support/NameMatchMethodPointcutTests.java index 409fda76759..f594304c6af 100644 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/support/NameMatchMethodPointcutTests.java +++ b/org.springframework.aop/src/test/java/org/springframework/aop/support/NameMatchMethodPointcutTests.java @@ -16,8 +16,10 @@ package org.springframework.aop.support; -import junit.framework.TestCase; +import static org.junit.Assert.*; +import org.junit.Before; +import org.junit.Test; import org.springframework.aop.framework.Advised; import org.springframework.aop.framework.ProxyFactory; import org.springframework.aop.interceptor.NopInterceptor; @@ -27,10 +29,10 @@ import org.springframework.beans.SerializablePerson; import org.springframework.util.SerializationTestUtils; /** - * * @author Rod Johnson + * @author Chris Beams */ -public class NameMatchMethodPointcutTests extends TestCase { +public class NameMatchMethodPointcutTests { protected NameMatchMethodPointcut pc; @@ -38,15 +40,12 @@ public class NameMatchMethodPointcutTests extends TestCase { protected SerializableNopInterceptor nop; - public NameMatchMethodPointcutTests(String s) { - super(s); - } - /** * Create an empty pointcut, populating instance variables. * @see junit.framework.TestCase#setUp() */ - protected void setUp() { + @Before + public void setUp() { ProxyFactory pf = new ProxyFactory(new SerializablePerson()); nop = new SerializableNopInterceptor(); pc = new NameMatchMethodPointcut(); @@ -54,6 +53,7 @@ public class NameMatchMethodPointcutTests extends TestCase { proxied = (Person) pf.getProxy(); } + @Test public void testMatchingOnly() { // Can't do exact matching through isMatch assertTrue(pc.isMatch("echo", "ech*")); @@ -64,6 +64,7 @@ public class NameMatchMethodPointcutTests extends TestCase { assertTrue(pc.isMatch("testing", "*ing")); } + @Test public void testEmpty() throws Throwable { assertEquals(0, nop.getCount()); proxied.getName(); @@ -73,6 +74,7 @@ public class NameMatchMethodPointcutTests extends TestCase { } + @Test public void testMatchOneMethod() throws Throwable { pc.addMethodName("echo"); pc.addMethodName("set*"); @@ -90,6 +92,7 @@ public class NameMatchMethodPointcutTests extends TestCase { assertEquals(3, nop.getCount()); } + @Test public void testSets() throws Throwable { pc.setMappedNames(new String[] { "set*", "echo" }); assertEquals(0, nop.getCount()); @@ -100,6 +103,7 @@ public class NameMatchMethodPointcutTests extends TestCase { assertEquals(2, nop.getCount()); } + @Test public void testSerializable() throws Throwable { testSets(); // Count is now 2 @@ -111,6 +115,7 @@ public class NameMatchMethodPointcutTests extends TestCase { assertEquals(3, nop2.getCount()); } + @Test public void testEqualsAndHashCode() throws Exception { NameMatchMethodPointcut pc1 = new NameMatchMethodPointcut(); NameMatchMethodPointcut pc2 = new NameMatchMethodPointcut(); diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/support/RegexpMethodPointcutAdvisorIntegrationTests.java b/org.springframework.aop/src/test/java/org/springframework/aop/support/RegexpMethodPointcutAdvisorIntegrationTests.java similarity index 85% rename from org.springframework.testsuite/src/test/java/org/springframework/aop/support/RegexpMethodPointcutAdvisorIntegrationTests.java rename to org.springframework.aop/src/test/java/org/springframework/aop/support/RegexpMethodPointcutAdvisorIntegrationTests.java index 08b86911375..9499ea4d1d1 100644 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/support/RegexpMethodPointcutAdvisorIntegrationTests.java +++ b/org.springframework.aop/src/test/java/org/springframework/aop/support/RegexpMethodPointcutAdvisorIntegrationTests.java @@ -16,8 +16,9 @@ package org.springframework.aop.support; -import junit.framework.TestCase; +import static org.junit.Assert.assertEquals; +import org.junit.Test; import org.springframework.aop.framework.Advised; import org.springframework.aop.interceptor.NopInterceptor; import org.springframework.aop.interceptor.SerializableNopInterceptor; @@ -25,16 +26,19 @@ import org.springframework.beans.ITestBean; import org.springframework.beans.Person; import org.springframework.beans.TestBean; import org.springframework.beans.factory.BeanFactory; -import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.beans.factory.xml.XmlBeanFactory; +import org.springframework.core.io.ClassPathResource; import org.springframework.util.SerializationTestUtils; /** * @author Rod Johnson + * @author Chris Beams */ -public class RegexpMethodPointcutAdvisorIntegrationTests extends TestCase { +public class RegexpMethodPointcutAdvisorIntegrationTests { + @Test public void testSinglePattern() throws Throwable { - BeanFactory bf = new ClassPathXmlApplicationContext("org/springframework/aop/support/regexpSetterTests.xml"); + BeanFactory bf = new XmlBeanFactory(new ClassPathResource("regexpSetterTests.xml", getClass())); ITestBean advised = (ITestBean) bf.getBean("settersAdvised"); // Interceptor behind regexp advisor NopInterceptor nop = (NopInterceptor) bf.getBean("nopInterceptor"); @@ -50,8 +54,9 @@ public class RegexpMethodPointcutAdvisorIntegrationTests extends TestCase { assertEquals(1, nop.getCount()); } + @Test public void testMultiplePatterns() throws Throwable { - BeanFactory bf = new ClassPathXmlApplicationContext("org/springframework/aop/support/regexpSetterTests.xml"); + BeanFactory bf = new XmlBeanFactory(new ClassPathResource("regexpSetterTests.xml", getClass())); // This is a CGLIB proxy, so we can proxy it to the target class TestBean advised = (TestBean) bf.getBean("settersAndAbsquatulateAdvised"); // Interceptor behind regexp advisor @@ -72,8 +77,9 @@ public class RegexpMethodPointcutAdvisorIntegrationTests extends TestCase { assertEquals(2, nop.getCount()); } + @Test public void testSerialization() throws Throwable { - BeanFactory bf = new ClassPathXmlApplicationContext("org/springframework/aop/support/regexpSetterTests.xml"); + BeanFactory bf = new XmlBeanFactory(new ClassPathResource("regexpSetterTests.xml", getClass())); // This is a CGLIB proxy, so we can proxy it to the target class Person p = (Person) bf.getBean("serializableSettersAdvised"); // Interceptor behind regexp advisor diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/support/regexpSetterTests.xml b/org.springframework.aop/src/test/java/org/springframework/aop/support/regexpSetterTests.xml similarity index 100% rename from org.springframework.testsuite/src/test/java/org/springframework/aop/support/regexpSetterTests.xml rename to org.springframework.aop/src/test/java/org/springframework/aop/support/regexpSetterTests.xml diff --git a/org.springframework.aop/src/test/java/org/springframework/beans/Person.java b/org.springframework.aop/src/test/java/org/springframework/beans/Person.java new file mode 100644 index 00000000000..af3bb924f7e --- /dev/null +++ b/org.springframework.aop/src/test/java/org/springframework/beans/Person.java @@ -0,0 +1,36 @@ +/* + * 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.beans; + +/** + * + * @author Rod Johnson + */ +public interface Person { + + String getName(); + void setName(String name); + int getAge(); + void setAge(int i); + + /** + * Test for non-property method matching. + * If the parameter is a Throwable, it will be thrown rather than + * returned. + */ + Object echo(Object o) throws Throwable; +} \ No newline at end of file diff --git a/org.springframework.aop/src/test/java/org/springframework/beans/SerializablePerson.java b/org.springframework.aop/src/test/java/org/springframework/beans/SerializablePerson.java new file mode 100644 index 00000000000..66a41f51131 --- /dev/null +++ b/org.springframework.aop/src/test/java/org/springframework/beans/SerializablePerson.java @@ -0,0 +1,64 @@ +/* + * 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.beans; + +import java.io.Serializable; + +import org.springframework.util.ObjectUtils; + +/** + * Serializable implementation of the Person interface. + * + * @author Rod Johnson + */ +public class SerializablePerson implements Person, Serializable { + + private String name; + private int age; + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Object echo(Object o) throws Throwable { + if (o instanceof Throwable) { + throw (Throwable) o; + } + return o; + } + + public boolean equals(Object other) { + if (!(other instanceof SerializablePerson)) { + return false; + } + SerializablePerson p = (SerializablePerson) other; + return p.age == age && ObjectUtils.nullSafeEquals(name, p.name); + } + +}