提交 32bea88e 编写于 作者: C Chris Beams

moving unit tests from .testsuite -> .aop

上级 e3ec177a
/*
* 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
......@@ -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;
......
......@@ -16,10 +16,11 @@
package org.springframework.aop.support;
import java.lang.reflect.Method;
import static org.junit.Assert.*;
import junit.framework.TestCase;
import java.lang.reflect.Method;
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");
......
......@@ -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();
......
......@@ -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
......
/*
* 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
/*
* 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);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册