moving unit tests from .testsuite -> .aop
This commit is contained in:
@@ -1,61 +0,0 @@
|
||||
|
||||
/*
|
||||
* 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.support;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.aop.ClassFilter;
|
||||
import org.springframework.beans.ITestBean;
|
||||
import org.springframework.beans.TestBean;
|
||||
import org.springframework.core.NestedRuntimeException;
|
||||
|
||||
/**
|
||||
* @author Rod Johnson
|
||||
* @author Chris Beams
|
||||
*/
|
||||
public class ClassFiltersTests {
|
||||
|
||||
private ClassFilter exceptionFilter = new RootClassFilter(Exception.class);
|
||||
|
||||
private ClassFilter itbFilter = new RootClassFilter(ITestBean.class);
|
||||
|
||||
private ClassFilter hasRootCauseFilter = new RootClassFilter(NestedRuntimeException.class);
|
||||
|
||||
@Test
|
||||
public void testUnion() {
|
||||
assertTrue(exceptionFilter.matches(RuntimeException.class));
|
||||
assertFalse(exceptionFilter.matches(TestBean.class));
|
||||
assertFalse(itbFilter.matches(Exception.class));
|
||||
assertTrue(itbFilter.matches(TestBean.class));
|
||||
ClassFilter union = ClassFilters.union(exceptionFilter, itbFilter);
|
||||
assertTrue(union.matches(RuntimeException.class));
|
||||
assertTrue(union.matches(TestBean.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIntersection() {
|
||||
assertTrue(exceptionFilter.matches(RuntimeException.class));
|
||||
assertTrue(hasRootCauseFilter.matches(NestedRuntimeException.class));
|
||||
ClassFilter intersection = ClassFilters.intersection(exceptionFilter, hasRootCauseFilter);
|
||||
assertFalse(intersection.matches(RuntimeException.class));
|
||||
assertFalse(intersection.matches(TestBean.class));
|
||||
assertTrue(intersection.matches(NestedRuntimeException.class));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,318 +0,0 @@
|
||||
/*
|
||||
* 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.support;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.aopalliance.intercept.MethodInterceptor;
|
||||
import org.easymock.MockControl;
|
||||
|
||||
import org.springframework.aop.IntroductionAdvisor;
|
||||
import org.springframework.aop.IntroductionInterceptor;
|
||||
import org.springframework.aop.framework.ProxyFactory;
|
||||
import org.springframework.aop.framework.TimeStamped;
|
||||
import org.springframework.aop.interceptor.SerializableNopInterceptor;
|
||||
import org.springframework.beans.INestedTestBean;
|
||||
import org.springframework.beans.ITestBean;
|
||||
import org.springframework.beans.NestedTestBean;
|
||||
import org.springframework.beans.Person;
|
||||
import org.springframework.beans.SerializablePerson;
|
||||
import org.springframework.beans.TestBean;
|
||||
import org.springframework.util.SerializationTestUtils;
|
||||
|
||||
/**
|
||||
* @author Rod Johnson
|
||||
* @since 13.05.2003
|
||||
*/
|
||||
public class DelegatingIntroductionInterceptorTests extends TestCase {
|
||||
|
||||
public void testNullTarget() throws Exception {
|
||||
try {
|
||||
IntroductionInterceptor ii = new DelegatingIntroductionInterceptor(null);
|
||||
fail("Shouldn't accept null target");
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
// OK
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
long timestamp = 111L;
|
||||
tsControl.setReturnValue(timestamp, 1);
|
||||
tsControl.replay();
|
||||
|
||||
factory.addAdvisor(0, new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts)));
|
||||
|
||||
TimeStamped tsp = (TimeStamped) factory.getProxy();
|
||||
assertTrue(tsp.getTimeStamp() == timestamp);
|
||||
|
||||
tsControl.verify();
|
||||
}
|
||||
|
||||
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();
|
||||
long timestamp = 111L;
|
||||
tsControl.setReturnValue(timestamp, 1);
|
||||
tsControl.replay();
|
||||
|
||||
factory.addAdvisor(0, new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts), SubTimeStamped.class));
|
||||
|
||||
SubTimeStamped tsp = (SubTimeStamped) factory.getProxy();
|
||||
assertTrue(tsp.getTimeStamp() == timestamp);
|
||||
|
||||
tsControl.verify();
|
||||
}
|
||||
|
||||
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();
|
||||
long timestamp = 111L;
|
||||
tsControl.setReturnValue(timestamp, 1);
|
||||
tsControl.replay();
|
||||
|
||||
factory.addAdvisor(0, new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts), TimeStamped.class));
|
||||
|
||||
TimeStamped tsp = (TimeStamped) factory.getProxy();
|
||||
assertTrue(!(tsp instanceof SubTimeStamped));
|
||||
assertTrue(tsp.getTimeStamp() == timestamp);
|
||||
|
||||
tsControl.verify();
|
||||
}
|
||||
|
||||
public void testAutomaticInterfaceRecognitionInDelegate() throws Exception {
|
||||
final long t = 1001L;
|
||||
class Tester implements TimeStamped, ITester {
|
||||
public void foo() throws Exception {
|
||||
}
|
||||
public long getTimeStamp() {
|
||||
return t;
|
||||
}
|
||||
}
|
||||
|
||||
DelegatingIntroductionInterceptor ii = new DelegatingIntroductionInterceptor(new Tester());
|
||||
|
||||
TestBean target = new TestBean();
|
||||
|
||||
ProxyFactory pf = new ProxyFactory(target);
|
||||
pf.addAdvisor(0, new DefaultIntroductionAdvisor(ii));
|
||||
|
||||
//assertTrue(Arrays.binarySearch(pf.getProxiedInterfaces(), TimeStamped.class) != -1);
|
||||
TimeStamped ts = (TimeStamped) pf.getProxy();
|
||||
|
||||
assertTrue(ts.getTimeStamp() == t);
|
||||
((ITester) ts).foo();
|
||||
|
||||
((ITestBean) ts).getAge();
|
||||
}
|
||||
|
||||
|
||||
public void testAutomaticInterfaceRecognitionInSubclass() throws Exception {
|
||||
final long t = 1001L;
|
||||
class TestII extends DelegatingIntroductionInterceptor implements TimeStamped, ITester {
|
||||
public void foo() throws Exception {
|
||||
}
|
||||
public long getTimeStamp() {
|
||||
return t;
|
||||
}
|
||||
}
|
||||
|
||||
DelegatingIntroductionInterceptor ii = new TestII();
|
||||
|
||||
TestBean target = new TestBean();
|
||||
|
||||
ProxyFactory pf = new ProxyFactory(target);
|
||||
IntroductionAdvisor ia = new DefaultIntroductionAdvisor(ii);
|
||||
assertTrue(ia.isPerInstance());
|
||||
pf.addAdvisor(0, ia);
|
||||
|
||||
//assertTrue(Arrays.binarySearch(pf.getProxiedInterfaces(), TimeStamped.class) != -1);
|
||||
TimeStamped ts = (TimeStamped) pf.getProxy();
|
||||
|
||||
assertTrue(ts instanceof TimeStamped);
|
||||
// Shoulnd't proxy framework interfaces
|
||||
assertTrue(!(ts instanceof MethodInterceptor));
|
||||
assertTrue(!(ts instanceof IntroductionInterceptor));
|
||||
|
||||
assertTrue(ts.getTimeStamp() == t);
|
||||
((ITester) ts).foo();
|
||||
((ITestBean) ts).getAge();
|
||||
|
||||
// Test removal
|
||||
ii.suppressInterface(TimeStamped.class);
|
||||
// Note that we need to construct a new proxy factory,
|
||||
// or suppress the interface on the proxy factory
|
||||
pf = new ProxyFactory(target);
|
||||
pf.addAdvisor(0, new DefaultIntroductionAdvisor(ii));
|
||||
Object o = pf.getProxy();
|
||||
assertTrue(!(o instanceof TimeStamped));
|
||||
}
|
||||
|
||||
public void testIntroductionInterceptorDoesntReplaceToString() throws Exception {
|
||||
TestBean raw = new TestBean();
|
||||
assertTrue(! (raw instanceof TimeStamped));
|
||||
ProxyFactory factory = new ProxyFactory(raw);
|
||||
|
||||
TimeStamped ts = new SerializableTimeStamped(0);
|
||||
|
||||
factory.addAdvisor(0, new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts) {
|
||||
public String toString() {
|
||||
throw new UnsupportedOperationException("Shouldn't be invoked");
|
||||
}
|
||||
}));
|
||||
|
||||
TimeStamped tsp = (TimeStamped) factory.getProxy();
|
||||
assertEquals(0, tsp.getTimeStamp());
|
||||
|
||||
assertEquals(raw.toString(), tsp.toString());
|
||||
}
|
||||
|
||||
public void testDelegateReturnsThisIsMassagedToReturnProxy() {
|
||||
NestedTestBean target = new NestedTestBean();
|
||||
String company = "Interface21";
|
||||
target.setCompany(company);
|
||||
TestBean delegate = new TestBean() {
|
||||
public ITestBean getSpouse() {
|
||||
return this;
|
||||
}
|
||||
};
|
||||
ProxyFactory pf = new ProxyFactory(target);
|
||||
pf.addAdvice(new DelegatingIntroductionInterceptor(delegate));
|
||||
INestedTestBean proxy = (INestedTestBean) pf.getProxy();
|
||||
|
||||
assertEquals(company, proxy.getCompany());
|
||||
ITestBean introduction = (ITestBean) proxy;
|
||||
assertSame("Introduced method returning delegate returns proxy", introduction, introduction.getSpouse());
|
||||
assertTrue("Introduced method returning delegate returns proxy", AopUtils.isAopProxy(introduction.getSpouse()));
|
||||
}
|
||||
|
||||
public void testSerializableDelegatingIntroductionInterceptorSerializable() throws Exception {
|
||||
SerializablePerson serializableTarget = new SerializablePerson();
|
||||
String name = "Tony";
|
||||
serializableTarget.setName("Tony");
|
||||
|
||||
ProxyFactory factory = new ProxyFactory(serializableTarget);
|
||||
factory.addInterface(Person.class);
|
||||
long time = 1000;
|
||||
TimeStamped ts = new SerializableTimeStamped(time);
|
||||
|
||||
factory.addAdvisor(new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts)));
|
||||
factory.addAdvice(new SerializableNopInterceptor());
|
||||
|
||||
Person p = (Person) factory.getProxy();
|
||||
|
||||
assertEquals(name, p.getName());
|
||||
assertEquals(time, ((TimeStamped) p).getTimeStamp());
|
||||
|
||||
Person p1 = (Person) SerializationTestUtils.serializeAndDeserialize(p);
|
||||
assertEquals(name, p1.getName());
|
||||
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.
|
||||
public void testIntroductionMasksTargetImplementation() throws Exception {
|
||||
final long t = 1001L;
|
||||
class TestII extends DelegatingIntroductionInterceptor implements TimeStamped {
|
||||
public long getTimeStamp() {
|
||||
return t;
|
||||
}
|
||||
}
|
||||
|
||||
DelegatingIntroductionInterceptor ii = new TestII();
|
||||
|
||||
// != t
|
||||
TestBean target = new TargetClass(t + 1);
|
||||
|
||||
ProxyFactory pf = new ProxyFactory(target);
|
||||
pf.addAdvisor(0, new DefaultIntroductionAdvisor(ii));
|
||||
|
||||
TimeStamped ts = (TimeStamped) pf.getProxy();
|
||||
// From introduction interceptor, not target
|
||||
assertTrue(ts.getTimeStamp() == t);
|
||||
}
|
||||
|
||||
|
||||
private static class SerializableTimeStamped implements TimeStamped, Serializable {
|
||||
|
||||
private final long ts;
|
||||
|
||||
public SerializableTimeStamped(long ts) {
|
||||
this.ts = ts;
|
||||
}
|
||||
|
||||
public long getTimeStamp() {
|
||||
return ts;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class TargetClass extends TestBean implements TimeStamped {
|
||||
|
||||
long t;
|
||||
|
||||
public TargetClass(long t) {
|
||||
this.t = t;
|
||||
}
|
||||
|
||||
public long getTimeStamp() {
|
||||
return t;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public interface ITester {
|
||||
|
||||
void foo() throws Exception;
|
||||
}
|
||||
|
||||
|
||||
private static interface SubTimeStamped extends TimeStamped {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,120 +0,0 @@
|
||||
/*
|
||||
* 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.support;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.aop.MethodMatcher;
|
||||
import org.springframework.beans.IOther;
|
||||
import org.springframework.beans.ITestBean;
|
||||
import org.springframework.beans.TestBean;
|
||||
import org.springframework.util.SerializationTestUtils;
|
||||
|
||||
/**
|
||||
* $Id: MethodMatchersTests.java,v 1.7 2005/03/25 09:28:18 jhoeller Exp $
|
||||
*/
|
||||
public class MethodMatchersTests extends TestCase {
|
||||
|
||||
private final Method EXCEPTION_GETMESSAGE;
|
||||
|
||||
private final Method ITESTBEAN_SETAGE;
|
||||
|
||||
private final Method ITESTBEAN_GETAGE;
|
||||
|
||||
private final Method IOTHER_ABSQUATULATE;
|
||||
|
||||
public MethodMatchersTests() throws Exception {
|
||||
EXCEPTION_GETMESSAGE = Exception.class.getMethod("getMessage", (Class[]) null);
|
||||
ITESTBEAN_GETAGE = ITestBean.class.getMethod("getAge", (Class[]) null);
|
||||
ITESTBEAN_SETAGE = ITestBean.class.getMethod("setAge", new Class[] { int.class });
|
||||
IOTHER_ABSQUATULATE = IOther.class.getMethod("absquatulate", (Class[]) null);
|
||||
}
|
||||
|
||||
public void testDefaultMatchesAll() throws Exception {
|
||||
MethodMatcher defaultMm = MethodMatcher.TRUE;
|
||||
assertTrue(defaultMm.matches(EXCEPTION_GETMESSAGE, Exception.class));
|
||||
assertTrue(defaultMm.matches(ITESTBEAN_SETAGE, TestBean.class));
|
||||
}
|
||||
|
||||
public void testMethodMatcherTrueSerializable() throws Exception {
|
||||
assertSame(SerializationTestUtils.serializeAndDeserialize(MethodMatcher.TRUE), MethodMatcher.TRUE);
|
||||
}
|
||||
|
||||
public void testSingle() throws Exception {
|
||||
MethodMatcher defaultMm = MethodMatcher.TRUE;
|
||||
assertTrue(defaultMm.matches(EXCEPTION_GETMESSAGE, Exception.class));
|
||||
assertTrue(defaultMm.matches(ITESTBEAN_SETAGE, TestBean.class));
|
||||
defaultMm = MethodMatchers.intersection(defaultMm, new StartsWithMatcher("get"));
|
||||
|
||||
assertTrue(defaultMm.matches(EXCEPTION_GETMESSAGE, Exception.class));
|
||||
assertFalse(defaultMm.matches(ITESTBEAN_SETAGE, TestBean.class));
|
||||
}
|
||||
|
||||
|
||||
public void testDynamicAndStaticMethodMatcherIntersection() throws Exception {
|
||||
MethodMatcher mm1 = MethodMatcher.TRUE;
|
||||
MethodMatcher mm2 = new TestDynamicMethodMatcherWhichMatches();
|
||||
MethodMatcher intersection = MethodMatchers.intersection(mm1, mm2);
|
||||
assertTrue("Intersection is a dynamic matcher", intersection.isRuntime());
|
||||
assertTrue("2Matched setAge method", intersection.matches(ITESTBEAN_SETAGE, TestBean.class));
|
||||
assertTrue("3Matched setAge method", intersection.matches(ITESTBEAN_SETAGE, TestBean.class, new Object[] { new Integer(5) }));
|
||||
// Knock out dynamic part
|
||||
intersection = MethodMatchers.intersection(intersection, new TestDynamicMethodMatcherWhichDoesNotMatch());
|
||||
assertTrue("Intersection is a dynamic matcher", intersection.isRuntime());
|
||||
assertTrue("2Matched setAge method", intersection.matches(ITESTBEAN_SETAGE, TestBean.class));
|
||||
assertFalse("3 - not Matched setAge method", intersection.matches(ITESTBEAN_SETAGE, TestBean.class, new Object[] { new Integer(5) }));
|
||||
}
|
||||
|
||||
public void testStaticMethodMatcherUnion() throws Exception {
|
||||
MethodMatcher getterMatcher = new StartsWithMatcher("get");
|
||||
MethodMatcher setterMatcher = new StartsWithMatcher("set");
|
||||
MethodMatcher union = MethodMatchers.union(getterMatcher, setterMatcher);
|
||||
|
||||
assertFalse("Union is a static matcher", union.isRuntime());
|
||||
assertTrue("Matched setAge method", union.matches(ITESTBEAN_SETAGE, TestBean.class));
|
||||
assertTrue("Matched getAge method", union.matches(ITESTBEAN_GETAGE, TestBean.class));
|
||||
assertFalse("Didn't matched absquatulate method", union.matches(IOTHER_ABSQUATULATE, TestBean.class));
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static class StartsWithMatcher extends StaticMethodMatcher {
|
||||
private String prefix;
|
||||
public StartsWithMatcher(String s) {
|
||||
this.prefix = s;
|
||||
}
|
||||
public boolean matches(Method m, Class targetClass) {
|
||||
return m.getName().startsWith(prefix);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class TestDynamicMethodMatcherWhichMatches extends DynamicMethodMatcher {
|
||||
public boolean matches(Method m, Class targetClass, Object[] args) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
private static class TestDynamicMethodMatcherWhichDoesNotMatch extends DynamicMethodMatcher {
|
||||
public boolean matches(Method m, Class targetClass, Object[] args) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,132 +0,0 @@
|
||||
/*
|
||||
* 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.support;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.aop.framework.Advised;
|
||||
import org.springframework.aop.framework.ProxyFactory;
|
||||
import org.springframework.aop.interceptor.NopInterceptor;
|
||||
import org.springframework.aop.interceptor.SerializableNopInterceptor;
|
||||
import org.springframework.beans.Person;
|
||||
import org.springframework.beans.SerializablePerson;
|
||||
import org.springframework.util.SerializationTestUtils;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Rod Johnson
|
||||
*/
|
||||
public class NameMatchMethodPointcutTests extends TestCase {
|
||||
|
||||
protected NameMatchMethodPointcut pc;
|
||||
|
||||
protected Person proxied;
|
||||
|
||||
protected SerializableNopInterceptor nop;
|
||||
|
||||
public NameMatchMethodPointcutTests(String s) {
|
||||
super(s);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an empty pointcut, populating instance variables.
|
||||
* @see junit.framework.TestCase#setUp()
|
||||
*/
|
||||
protected void setUp() {
|
||||
ProxyFactory pf = new ProxyFactory(new SerializablePerson());
|
||||
nop = new SerializableNopInterceptor();
|
||||
pc = new NameMatchMethodPointcut();
|
||||
pf.addAdvisor(new DefaultPointcutAdvisor(pc, nop));
|
||||
proxied = (Person) pf.getProxy();
|
||||
}
|
||||
|
||||
public void testMatchingOnly() {
|
||||
// Can't do exact matching through isMatch
|
||||
assertTrue(pc.isMatch("echo", "ech*"));
|
||||
assertTrue(pc.isMatch("setName", "setN*"));
|
||||
assertTrue(pc.isMatch("setName", "set*"));
|
||||
assertFalse(pc.isMatch("getName", "set*"));
|
||||
assertFalse(pc.isMatch("setName", "set"));
|
||||
assertTrue(pc.isMatch("testing", "*ing"));
|
||||
}
|
||||
|
||||
public void testEmpty() throws Throwable {
|
||||
assertEquals(0, nop.getCount());
|
||||
proxied.getName();
|
||||
proxied.setName("");
|
||||
proxied.echo(null);
|
||||
assertEquals(0, nop.getCount());
|
||||
}
|
||||
|
||||
|
||||
public void testMatchOneMethod() throws Throwable {
|
||||
pc.addMethodName("echo");
|
||||
pc.addMethodName("set*");
|
||||
assertEquals(0, nop.getCount());
|
||||
proxied.getName();
|
||||
proxied.getName();
|
||||
assertEquals(0, nop.getCount());
|
||||
proxied.echo(null);
|
||||
assertEquals(1, nop.getCount());
|
||||
|
||||
proxied.setName("");
|
||||
assertEquals(2, nop.getCount());
|
||||
proxied.setAge(25);
|
||||
assertEquals(25, proxied.getAge());
|
||||
assertEquals(3, nop.getCount());
|
||||
}
|
||||
|
||||
public void testSets() throws Throwable {
|
||||
pc.setMappedNames(new String[] { "set*", "echo" });
|
||||
assertEquals(0, nop.getCount());
|
||||
proxied.getName();
|
||||
proxied.setName("");
|
||||
assertEquals(1, nop.getCount());
|
||||
proxied.echo(null);
|
||||
assertEquals(2, nop.getCount());
|
||||
}
|
||||
|
||||
public void testSerializable() throws Throwable {
|
||||
testSets();
|
||||
// Count is now 2
|
||||
Person p2 = (Person) SerializationTestUtils.serializeAndDeserialize(proxied);
|
||||
NopInterceptor nop2 = (NopInterceptor) ((Advised) p2).getAdvisors()[0].getAdvice();
|
||||
p2.getName();
|
||||
assertEquals(2, nop2.getCount());
|
||||
p2.echo(null);
|
||||
assertEquals(3, nop2.getCount());
|
||||
}
|
||||
|
||||
public void testEqualsAndHashCode() throws Exception {
|
||||
NameMatchMethodPointcut pc1 = new NameMatchMethodPointcut();
|
||||
NameMatchMethodPointcut pc2 = new NameMatchMethodPointcut();
|
||||
|
||||
String foo = "foo";
|
||||
|
||||
assertEquals(pc1, pc2);
|
||||
assertEquals(pc1.hashCode(), pc2.hashCode());
|
||||
|
||||
pc1.setMappedName(foo);
|
||||
assertFalse(pc1.equals(pc2));
|
||||
assertTrue(pc1.hashCode() != pc2.hashCode());
|
||||
|
||||
pc2.setMappedName(foo);
|
||||
assertEquals(pc1, pc2);
|
||||
assertEquals(pc1.hashCode(), pc2.hashCode());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,108 +0,0 @@
|
||||
/*
|
||||
* 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.support;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.aop.framework.Advised;
|
||||
import org.springframework.aop.interceptor.NopInterceptor;
|
||||
import org.springframework.aop.interceptor.SerializableNopInterceptor;
|
||||
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.util.SerializationTestUtils;
|
||||
|
||||
/**
|
||||
* @author Rod Johnson
|
||||
*/
|
||||
public class RegexpMethodPointcutAdvisorIntegrationTests extends TestCase {
|
||||
|
||||
public void testSinglePattern() throws Throwable {
|
||||
BeanFactory bf = new ClassPathXmlApplicationContext("org/springframework/aop/support/regexpSetterTests.xml");
|
||||
ITestBean advised = (ITestBean) bf.getBean("settersAdvised");
|
||||
// Interceptor behind regexp advisor
|
||||
NopInterceptor nop = (NopInterceptor) bf.getBean("nopInterceptor");
|
||||
assertEquals(0, nop.getCount());
|
||||
|
||||
int newAge = 12;
|
||||
// Not advised
|
||||
advised.exceptional(null);
|
||||
assertEquals(0, nop.getCount());
|
||||
advised.setAge(newAge);
|
||||
assertEquals(newAge, advised.getAge());
|
||||
// Only setter fired
|
||||
assertEquals(1, nop.getCount());
|
||||
}
|
||||
|
||||
public void testMultiplePatterns() throws Throwable {
|
||||
BeanFactory bf = new ClassPathXmlApplicationContext("org/springframework/aop/support/regexpSetterTests.xml");
|
||||
// This is a CGLIB proxy, so we can proxy it to the target class
|
||||
TestBean advised = (TestBean) bf.getBean("settersAndAbsquatulateAdvised");
|
||||
// Interceptor behind regexp advisor
|
||||
NopInterceptor nop = (NopInterceptor) bf.getBean("nopInterceptor");
|
||||
assertEquals(0, nop.getCount());
|
||||
|
||||
int newAge = 12;
|
||||
// Not advised
|
||||
advised.exceptional(null);
|
||||
assertEquals(0, nop.getCount());
|
||||
|
||||
// This is proxied
|
||||
advised.absquatulate();
|
||||
assertEquals(1, nop.getCount());
|
||||
advised.setAge(newAge);
|
||||
assertEquals(newAge, advised.getAge());
|
||||
// Only setter fired
|
||||
assertEquals(2, nop.getCount());
|
||||
}
|
||||
|
||||
public void testSerialization() throws Throwable {
|
||||
BeanFactory bf = new ClassPathXmlApplicationContext("org/springframework/aop/support/regexpSetterTests.xml");
|
||||
// This is a CGLIB proxy, so we can proxy it to the target class
|
||||
Person p = (Person) bf.getBean("serializableSettersAdvised");
|
||||
// Interceptor behind regexp advisor
|
||||
NopInterceptor nop = (NopInterceptor) bf.getBean("nopInterceptor");
|
||||
assertEquals(0, nop.getCount());
|
||||
|
||||
int newAge = 12;
|
||||
// Not advised
|
||||
assertEquals(0, p.getAge());
|
||||
assertEquals(0, nop.getCount());
|
||||
|
||||
// This is proxied
|
||||
p.setAge(newAge);
|
||||
assertEquals(1, nop.getCount());
|
||||
p.setAge(newAge);
|
||||
assertEquals(newAge, p.getAge());
|
||||
// Only setter fired
|
||||
assertEquals(2, nop.getCount());
|
||||
|
||||
// Serialize and continue...
|
||||
p = (Person) SerializationTestUtils.serializeAndDeserialize(p);
|
||||
assertEquals(newAge, p.getAge());
|
||||
// Remembers count, but we need to get a new reference to nop...
|
||||
nop = (SerializableNopInterceptor) ((Advised) p).getAdvisors()[0].getAdvice();
|
||||
assertEquals(2, nop.getCount());
|
||||
assertEquals("serializableSettersAdvised", p.getName());
|
||||
p.setAge(newAge + 1);
|
||||
assertEquals(3, nop.getCount());
|
||||
assertEquals(newAge + 1, p.getAge());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
|
||||
|
||||
<beans>
|
||||
|
||||
<!-- Simple target -->
|
||||
<bean id="test" class="org.springframework.beans.TestBean">
|
||||
<property name="name"><value>custom</value></property>
|
||||
<property name="age"><value>666</value></property>
|
||||
</bean>
|
||||
|
||||
<bean id="nopInterceptor" class="org.springframework.aop.interceptor.SerializableNopInterceptor"/>
|
||||
|
||||
<bean id="settersAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
|
||||
<property name="advice"><ref local="nopInterceptor"/></property>
|
||||
<property name="pattern">
|
||||
<value>
|
||||
.*set.*
|
||||
</value>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="settersAdvised" class="org.springframework.aop.framework.ProxyFactoryBean">
|
||||
<property name="proxyInterfaces"><value>org.springframework.beans.ITestBean</value></property>
|
||||
<property name="target"><ref local="test"/></property>
|
||||
<property name="interceptorNames"><value>settersAdvisor</value></property>
|
||||
</bean>
|
||||
|
||||
<bean id="serializableSettersAdvised" class="org.springframework.aop.framework.ProxyFactoryBean">
|
||||
<property name="proxyInterfaces"><value>org.springframework.beans.Person</value></property>
|
||||
<property name="target">
|
||||
<bean class="org.springframework.beans.SerializablePerson">
|
||||
<property name="name"><value>serializableSettersAdvised</value></property>
|
||||
</bean>
|
||||
</property>
|
||||
<property name="interceptorNames"><value>settersAdvisor</value></property>
|
||||
</bean>
|
||||
|
||||
<!-- Illustrates use of multiple patterns -->
|
||||
<bean id="settersAndAbsquatulateAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
|
||||
<property name="advice"><ref local="nopInterceptor"/></property>
|
||||
<property name="patterns">
|
||||
<list>
|
||||
<value>.*get.*</value>
|
||||
<value>.*absquatulate</value>
|
||||
</list>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="settersAndAbsquatulateAdvised" class="org.springframework.aop.framework.ProxyFactoryBean">
|
||||
<property name="proxyInterfaces"><value>org.springframework.beans.ITestBean</value></property>
|
||||
<!-- Force CGLIB so we can cast to TestBean -->
|
||||
<property name="proxyTargetClass"><value>true</value></property>
|
||||
<property name="target"><ref local="test"/></property>
|
||||
<property name="interceptorNames"><value>settersAndAbsquatulateAdvisor</value></property>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
Reference in New Issue
Block a user