From eabec18151e074d418e6bc85e28838b611fe0f8c Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Fri, 19 Dec 2008 02:29:04 +0000 Subject: [PATCH] refactoring .testsuite .aop.aspectj tests in preparation for migration to .context --- .../aspectj/AbstractAdviceBindingTests.java | 47 --- .../aop/aspectj/AdviceBindingTestAspect.java | 6 +- .../aop/aspectj/AfterAdviceBindingTests.java | 77 +++-- ...AfterReturningAdviceBindingTestAspect.java | 67 ---- .../AfterReturningAdviceBindingTests.java | 148 ++++++--- .../AfterThrowingAdviceBindingTestAspect.java | 60 ---- .../AfterThrowingAdviceBindingTests.java | 170 +++++----- .../AroundAdviceBindingTestAspect.java | 71 ----- .../aop/aspectj/AroundAdviceBindingTests.java | 113 +++++-- .../aspectj/AroundAdviceCircularTests.java | 11 +- .../AspectAndAdvicePrecedenceTests.java | 161 ++++++++-- ...AspectJExpressionPointcutAdvisorTests.java | 54 ++-- .../aop/aspectj/AuthenticationLogger.java | 37 --- .../aop/aspectj/BeanNameAwareMixin.java | 37 --- .../BeanNamePointcutAtAspectTests.java | 80 ++--- .../aop/aspectj/BeanNamePointcutTests.java | 65 ++-- .../aop/aspectj/BeforeAdviceBindingTests.java | 82 +++-- .../aop/aspectj/CallCountingInterceptor.java | 26 -- .../springframework/aop/aspectj/Counter.java | 2 +- .../DeclarationOrderIndependenceTests.java | 157 ++++++--- .../DeclareParentsDelegateRefTests.java | 67 ++-- .../aop/aspectj/DeclareParentsTests.java | 44 ++- .../springframework/aop/aspectj/ICounter.java | 2 +- ...licitJPArgumentMatchingAtAspectJTests.java | 20 +- .../ImplicitJPArgumentMatchingTests.java | 21 +- .../aop/aspectj/JoinPointMonitorAspect.java | 53 ---- .../JoinPointMonitorAtAspectJAspect.java | 58 ---- .../aop/aspectj/NonAnnotatedMakeLockable.java | 32 -- .../aspectj/OverloadedAdviceTestAspect.java | 32 -- .../aop/aspectj/OverloadedAdviceTests.java | 19 +- .../aop/aspectj/PrecedenceTestAspect.java | 103 ------ .../aop/aspectj/ProceedTests.java | 297 +++++++++--------- .../aspectj/PropertyDependentAspectTests.java | 71 ++++- .../aop/aspectj/SerializableMixin.java | 28 -- ... SharedPointcutWithArgsMismatchTests.java} | 61 ++-- .../aop/aspectj/SimpleSpringBeforeAdvice.java | 55 ---- .../aspectj/SubtypeMatchingTestClasses.java | 45 --- .../SubtypeSensitiveMatchingTests.java | 61 +++- .../aspectj/TargetPointcutSelectionTests.java | 44 +-- ...tSelectionOnlyPointcutsAtAspectJTests.java | 41 ++- ...sAndTargetSelectionOnlyPointcutsTests.java | 74 +++-- .../aop/aspectj/TopsyTurvyAspect.java | 53 ---- .../aop/aspectj/TopsyTurvyTarget.java | 30 -- .../aop/aspectj/TopsyTurvyTargetImpl.java | 42 --- .../aop/aspectj/args-mismatch.xml | 4 +- .../bean-name-pointcut-atAspect-tests.xml | 2 +- .../declare-parents-delegate-ref-tests.xml | 8 +- .../aop/aspectj/proceedTests.xml | 6 +- ...d-target-selectionOnly-pointcuts-tests.xml | 38 +-- .../aop/aspectj/topsy-turvy-aspect.xml | 4 +- 50 files changed, 1266 insertions(+), 1620 deletions(-) delete mode 100644 org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AbstractAdviceBindingTests.java delete mode 100644 org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AfterReturningAdviceBindingTestAspect.java delete mode 100644 org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AfterThrowingAdviceBindingTestAspect.java delete mode 100644 org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AroundAdviceBindingTestAspect.java delete mode 100644 org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AuthenticationLogger.java delete mode 100644 org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/BeanNameAwareMixin.java delete mode 100644 org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/CallCountingInterceptor.java delete mode 100644 org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/JoinPointMonitorAspect.java delete mode 100644 org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/JoinPointMonitorAtAspectJAspect.java delete mode 100644 org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/NonAnnotatedMakeLockable.java delete mode 100644 org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/OverloadedAdviceTestAspect.java delete mode 100644 org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/PrecedenceTestAspect.java delete mode 100644 org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/SerializableMixin.java rename org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/{SharedPointcutWithArgsMismatch.java => SharedPointcutWithArgsMismatchTests.java} (58%) delete mode 100644 org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/SimpleSpringBeforeAdvice.java delete mode 100644 org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/SubtypeMatchingTestClasses.java delete mode 100644 org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/TopsyTurvyAspect.java delete mode 100644 org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/TopsyTurvyTarget.java delete mode 100644 org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/TopsyTurvyTargetImpl.java diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AbstractAdviceBindingTests.java b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AbstractAdviceBindingTests.java deleted file mode 100644 index 443e736a52..0000000000 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AbstractAdviceBindingTests.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright 2002-2006 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.aspectj; - -import org.springframework.aop.framework.Advised; -import org.springframework.aop.support.AopUtils; -import org.springframework.beans.ITestBean; -import org.springframework.beans.TestBean; -import org.springframework.test.AbstractDependencyInjectionSpringContextTests; - -/** - * @author Rod Johnson - */ -public abstract class AbstractAdviceBindingTests extends AbstractDependencyInjectionSpringContextTests { - - protected ITestBean testBeanProxy; - - protected TestBean testBeanTarget; - - public final void setTestBean(ITestBean injectedTestBean) throws Exception { - assertTrue(AopUtils.isAopProxy(injectedTestBean)); - this.testBeanProxy = injectedTestBean; - // we need the real target too, not just the proxy... - this.testBeanTarget = (TestBean) ((Advised) testBeanProxy).getTargetSource().getTarget(); - } - - // Simple test to ensure all is well with the XML file. - // Note that this implicitly tests that the arg-names binding is working. - public final void testParse() { - // Do nothing - } - -} diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AdviceBindingTestAspect.java b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AdviceBindingTestAspect.java index 4c8476397e..393f7d755f 100644 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AdviceBindingTestAspect.java +++ b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AdviceBindingTestAspect.java @@ -19,11 +19,13 @@ package org.springframework.aop.aspectj; import org.aspectj.lang.JoinPoint; /** - * Aspect used as part of before advice binding tests. + * Aspect used as part of before before advice binding tests and + * serves as base class for a number of more specialized test aspects. * * @author Adrian Colyer + * @author Chris Beams */ -public class AdviceBindingTestAspect { +class AdviceBindingTestAspect { protected AdviceBindingCollaborator collaborator = null; diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AfterAdviceBindingTests.java b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AfterAdviceBindingTests.java index f248763a2d..efad82cd6a 100644 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AfterAdviceBindingTests.java +++ b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AfterAdviceBindingTests.java @@ -16,81 +16,94 @@ package org.springframework.aop.aspectj; -import org.easymock.MockControl; +import static org.easymock.EasyMock.*; +import static org.junit.Assert.assertTrue; +import org.junit.Before; +import org.junit.Test; import org.springframework.aop.aspectj.AdviceBindingTestAspect.AdviceBindingCollaborator; +import org.springframework.aop.framework.Advised; +import org.springframework.aop.support.AopUtils; +import org.springframework.beans.ITestBean; +import org.springframework.beans.TestBean; +import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Tests for various parameter binding scenarios with before advice. * * @author Adrian Colyer * @author Rod Johnson + * @author Chris Beams */ -public class AfterAdviceBindingTests extends AbstractAdviceBindingTests { - - private AdviceBindingTestAspect afterAdviceAspect; - - private MockControl mockControl; +public final class AfterAdviceBindingTests { private AdviceBindingCollaborator mockCollaborator; + + private ITestBean testBeanProxy; + + private TestBean testBeanTarget; - - public void setAfterAdviceAspect(AdviceBindingTestAspect anAspect) { - this.afterAdviceAspect = anAspect; - } - - protected String getConfigPath() { - return "after-advice-tests.xml"; - } - - protected void onSetUp() throws Exception { - super.onSetUp(); - mockControl = MockControl.createNiceControl(AdviceBindingCollaborator.class); - mockCollaborator = (AdviceBindingCollaborator) mockControl.getMock(); + @Before + public void setUp() throws Exception { + ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("after-advice-tests.xml", getClass()); + AdviceBindingTestAspect afterAdviceAspect = (AdviceBindingTestAspect) ctx.getBean("testAspect"); + + testBeanProxy = (ITestBean) ctx.getBean("testBean"); + assertTrue(AopUtils.isAopProxy(testBeanProxy)); + + // we need the real target too, not just the proxy... + testBeanTarget = (TestBean) ((Advised) testBeanProxy).getTargetSource().getTarget(); + + mockCollaborator = createNiceMock(AdviceBindingCollaborator.class); afterAdviceAspect.setCollaborator(mockCollaborator); } - + @Test public void testOneIntArg() { mockCollaborator.oneIntArg(5); - mockControl.replay(); + replay(mockCollaborator); testBeanProxy.setAge(5); - mockControl.verify(); + verify(mockCollaborator); } + @Test public void testOneObjectArgBindingProxyWithThis() { mockCollaborator.oneObjectArg(this.testBeanProxy); - mockControl.replay(); + replay(mockCollaborator); testBeanProxy.getAge(); - mockControl.verify(); + verify(mockCollaborator); } + @Test public void testOneObjectArgBindingTarget() { mockCollaborator.oneObjectArg(this.testBeanTarget); - mockControl.replay(); + replay(mockCollaborator); testBeanProxy.getDoctor(); - mockControl.verify(); + verify(mockCollaborator); } + @Test public void testOneIntAndOneObjectArgs() { mockCollaborator.oneIntAndOneObject(5,this.testBeanProxy); - mockControl.replay(); + replay(mockCollaborator); testBeanProxy.setAge(5); - mockControl.verify(); + verify(mockCollaborator); } + @Test public void testNeedsJoinPoint() { mockCollaborator.needsJoinPoint("getAge"); - mockControl.replay(); + replay(mockCollaborator); testBeanProxy.getAge(); - mockControl.verify(); + verify(mockCollaborator); } + @Test public void testNeedsJoinPointStaticPart() { mockCollaborator.needsJoinPointStaticPart("getAge"); - mockControl.replay(); + replay(mockCollaborator); testBeanProxy.getAge(); - mockControl.verify(); + verify(mockCollaborator); } } diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AfterReturningAdviceBindingTestAspect.java b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AfterReturningAdviceBindingTestAspect.java deleted file mode 100644 index 29eb3641f8..0000000000 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AfterReturningAdviceBindingTestAspect.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright 2002-2007 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.aop.aspectj; - -import org.springframework.beans.ITestBean; -import org.springframework.beans.TestBean; - -/** - * @author Adrian Colyer - * @author Juergen Hoeller - */ -public class AfterReturningAdviceBindingTestAspect extends AdviceBindingTestAspect { - - private AfterReturningAdviceBindingCollaborator getCollaborator() { - return (AfterReturningAdviceBindingCollaborator) this.collaborator; - } - - public void oneString(String name) { - getCollaborator().oneString(name); - } - - public void oneTestBeanArg(TestBean bean) { - getCollaborator().oneTestBeanArg(bean); - } - - public void testBeanArrayArg(ITestBean[] beans) { - getCollaborator().testBeanArrayArg(beans); - } - - public void objectMatchNoArgs() { - getCollaborator().objectMatchNoArgs(); - } - - public void stringMatchNoArgs() { - getCollaborator().stringMatchNoArgs(); - } - - public void oneInt(int result) { - getCollaborator().oneInt(result); - } - - - interface AfterReturningAdviceBindingCollaborator extends AdviceBindingCollaborator { - - void oneString(String s); - void oneTestBeanArg(TestBean b); - void testBeanArrayArg(ITestBean[] b); - void objectMatchNoArgs(); - void stringMatchNoArgs(); - void oneInt(int result); - } - -} diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AfterReturningAdviceBindingTests.java b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AfterReturningAdviceBindingTests.java index ddf8f622d6..92c670580a 100644 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AfterReturningAdviceBindingTests.java +++ b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AfterReturningAdviceBindingTests.java @@ -16,14 +16,18 @@ package org.springframework.aop.aspectj; -import org.easymock.MockControl; +import static org.easymock.EasyMock.*; +import static org.junit.Assert.assertTrue; +import org.junit.Before; +import org.junit.Test; import org.springframework.aop.aspectj.AfterReturningAdviceBindingTestAspect.AfterReturningAdviceBindingCollaborator; import org.springframework.aop.framework.Advised; import org.springframework.aop.support.AopUtils; import org.springframework.beans.ITestBean; import org.springframework.beans.TestBean; -import org.springframework.test.AbstractDependencyInjectionSpringContextTests; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Tests for various parameter binding scenarios with before advice. @@ -31,8 +35,9 @@ import org.springframework.test.AbstractDependencyInjectionSpringContextTests; * @author Adrian Colyer * @author Rod Johnson * @author Juergen Hoeller + * @author Chris Beams */ -public class AfterReturningAdviceBindingTests extends AbstractDependencyInjectionSpringContextTests { +public final class AfterReturningAdviceBindingTests { private AfterReturningAdviceBindingTestAspect afterAdviceAspect; @@ -40,8 +45,6 @@ public class AfterReturningAdviceBindingTests extends AbstractDependencyInjectio private TestBean testBeanTarget; - private MockControl mockControl; - private AfterReturningAdviceBindingCollaborator mockCollaborator; @@ -49,121 +52,172 @@ public class AfterReturningAdviceBindingTests extends AbstractDependencyInjectio this.afterAdviceAspect = anAspect; } - public void setTestBean(ITestBean aBean) throws Exception { - assertTrue(AopUtils.isAopProxy(aBean)); - this.testBeanProxy = aBean; - // we need the real target too, not just the proxy... - this.testBeanTarget = (TestBean) ((Advised)aBean).getTargetSource().getTarget(); - } - protected String getConfigPath() { return "afterReturning-advice-tests.xml"; } - protected void onSetUp() throws Exception { - super.onSetUp(); - mockControl = MockControl.createNiceControl(AfterReturningAdviceBindingCollaborator.class); - mockCollaborator = (AfterReturningAdviceBindingCollaborator) mockControl.getMock(); + @Before + public void setUp() throws Exception { + ApplicationContext ctx = new ClassPathXmlApplicationContext(getConfigPath(), getClass()); + + afterAdviceAspect = (AfterReturningAdviceBindingTestAspect) ctx.getBean("testAspect"); + + mockCollaborator = createNiceMock(AfterReturningAdviceBindingCollaborator.class); afterAdviceAspect.setCollaborator(mockCollaborator); + + testBeanProxy = (ITestBean) ctx.getBean("testBean"); + assertTrue(AopUtils.isAopProxy(testBeanProxy)); + + // we need the real target too, not just the proxy... + this.testBeanTarget = (TestBean) ((Advised)testBeanProxy).getTargetSource().getTarget(); } - // simple test to ensure all is well with the xml file - // note that this implicitly tests that the arg-names binding is working - public void testParse() { - } - + @Test public void testOneIntArg() { mockCollaborator.oneIntArg(5); - mockControl.replay(); + replay(mockCollaborator); testBeanProxy.setAge(5); - mockControl.verify(); + verify(mockCollaborator); } + @Test public void testOneObjectArg() { mockCollaborator.oneObjectArg(this.testBeanProxy); - mockControl.replay(); + replay(mockCollaborator); testBeanProxy.getAge(); - mockControl.verify(); + verify(mockCollaborator); } + @Test public void testOneIntAndOneObjectArgs() { mockCollaborator.oneIntAndOneObject(5,this.testBeanProxy); - mockControl.replay(); + replay(mockCollaborator); testBeanProxy.setAge(5); - mockControl.verify(); + verify(mockCollaborator); } + @Test public void testNeedsJoinPoint() { mockCollaborator.needsJoinPoint("getAge"); - mockControl.replay(); + replay(mockCollaborator); testBeanProxy.getAge(); - mockControl.verify(); + verify(mockCollaborator); } + @Test public void testNeedsJoinPointStaticPart() { mockCollaborator.needsJoinPointStaticPart("getAge"); - mockControl.replay(); + replay(mockCollaborator); testBeanProxy.getAge(); - mockControl.verify(); + verify(mockCollaborator); } + @Test public void testReturningString() { mockCollaborator.oneString("adrian"); - mockControl.replay(); + replay(mockCollaborator); testBeanProxy.setName("adrian"); testBeanProxy.getName(); - mockControl.verify(); + verify(mockCollaborator); } + @Test public void testReturningObject() { mockCollaborator.oneObjectArg(this.testBeanTarget); - mockControl.replay(); + replay(mockCollaborator); testBeanProxy.returnsThis(); - mockControl.verify(); + verify(mockCollaborator); } + @Test public void testReturningBean() { mockCollaborator.oneTestBeanArg(this.testBeanTarget); - mockControl.replay(); + replay(mockCollaborator); testBeanProxy.returnsThis(); - mockControl.verify(); + verify(mockCollaborator); } + @Test public void testReturningBeanArray() { this.testBeanTarget.setSpouse(new TestBean()); ITestBean[] spouses = (ITestBean[]) this.testBeanTarget.getSpouses(); mockCollaborator.testBeanArrayArg(spouses); - mockControl.replay(); + replay(mockCollaborator); testBeanProxy.getSpouses(); - mockControl.verify(); + verify(mockCollaborator); } + @Test public void testNoInvokeWhenReturningParameterTypeDoesNotMatch() { // we need a strict mock for this... - mockControl = MockControl.createControl(AfterReturningAdviceBindingCollaborator.class); - mockCollaborator = (AfterReturningAdviceBindingCollaborator) mockControl.getMock(); + mockCollaborator = createMock(AfterReturningAdviceBindingCollaborator.class); afterAdviceAspect.setCollaborator(mockCollaborator); - mockControl.replay(); + replay(mockCollaborator); testBeanProxy.setSpouse(this.testBeanProxy); testBeanProxy.getSpouse(); - mockControl.verify(); + verify(mockCollaborator); } + @Test public void testReturningByType() { mockCollaborator.objectMatchNoArgs(); - mockControl.replay(); + replay(mockCollaborator); testBeanProxy.returnsThis(); - mockControl.verify(); + verify(mockCollaborator); } + @Test public void testReturningPrimitive() { mockCollaborator.oneInt(20); - mockControl.replay(); + replay(mockCollaborator); testBeanProxy.setAge(20); testBeanProxy.haveBirthday(); - mockControl.verify(); + verify(mockCollaborator); + } + +} + + +final class AfterReturningAdviceBindingTestAspect extends AdviceBindingTestAspect { + + private AfterReturningAdviceBindingCollaborator getCollaborator() { + return (AfterReturningAdviceBindingCollaborator) this.collaborator; + } + + public void oneString(String name) { + getCollaborator().oneString(name); + } + + public void oneTestBeanArg(TestBean bean) { + getCollaborator().oneTestBeanArg(bean); + } + + public void testBeanArrayArg(ITestBean[] beans) { + getCollaborator().testBeanArrayArg(beans); + } + + public void objectMatchNoArgs() { + getCollaborator().objectMatchNoArgs(); + } + + public void stringMatchNoArgs() { + getCollaborator().stringMatchNoArgs(); + } + + public void oneInt(int result) { + getCollaborator().oneInt(result); + } + + interface AfterReturningAdviceBindingCollaborator extends AdviceBindingCollaborator { + + void oneString(String s); + void oneTestBeanArg(TestBean b); + void testBeanArrayArg(ITestBean[] b); + void objectMatchNoArgs(); + void stringMatchNoArgs(); + void oneInt(int result); } } diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AfterThrowingAdviceBindingTestAspect.java b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AfterThrowingAdviceBindingTestAspect.java deleted file mode 100644 index 6f20265410..0000000000 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AfterThrowingAdviceBindingTestAspect.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright 2002-2006 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.aspectj; - -/** - * Aspect used as part of before advice binding tests. - * @author Adrian Colyer - */ -public class AfterThrowingAdviceBindingTestAspect { - - // collaborator interface that makes it easy to test this aspect is - // working as expected through mocking. - public interface AfterThrowingAdviceBindingCollaborator { - void noArgs(); - void oneThrowable(Throwable t); - void oneRuntimeException(RuntimeException re); - void noArgsOnThrowableMatch(); - void noArgsOnRuntimeExceptionMatch(); - } - - protected AfterThrowingAdviceBindingCollaborator collaborator = null; - - public void setCollaborator(AfterThrowingAdviceBindingCollaborator aCollaborator) { - this.collaborator = aCollaborator; - } - - public void noArgs() { - this.collaborator.noArgs(); - } - - public void oneThrowable(Throwable t) { - this.collaborator.oneThrowable(t); - } - - public void oneRuntimeException(RuntimeException ex) { - this.collaborator.oneRuntimeException(ex); - } - - public void noArgsOnThrowableMatch() { - this.collaborator.noArgsOnThrowableMatch(); - } - - public void noArgsOnRuntimeExceptionMatch() { - this.collaborator.noArgsOnRuntimeExceptionMatch(); - } - -} diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AfterThrowingAdviceBindingTests.java b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AfterThrowingAdviceBindingTests.java index a4a9b24fda..8973add224 100644 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AfterThrowingAdviceBindingTests.java +++ b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AfterThrowingAdviceBindingTests.java @@ -16,132 +16,136 @@ package org.springframework.aop.aspectj; -import org.easymock.MockControl; +import static org.easymock.EasyMock.*; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; import org.springframework.aop.aspectj.AfterThrowingAdviceBindingTestAspect.AfterThrowingAdviceBindingCollaborator; import org.springframework.beans.ITestBean; -import org.springframework.test.AbstractDependencyInjectionSpringContextTests; +import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Tests for various parameter binding scenarios with before advice. * * @author Adrian Colyer + * @author Chris Beams */ -public class AfterThrowingAdviceBindingTests extends AbstractDependencyInjectionSpringContextTests { - - private AfterThrowingAdviceBindingTestAspect afterThrowingAdviceAspect; +public final class AfterThrowingAdviceBindingTests { private ITestBean testBean; - private MockControl mockControl; + private AfterThrowingAdviceBindingTestAspect afterThrowingAdviceAspect; private AfterThrowingAdviceBindingCollaborator mockCollaborator; - - public void setAfterAdviceAspect(AfterThrowingAdviceBindingTestAspect anAspect) { - this.afterThrowingAdviceAspect = anAspect; - } - - public void setTestBean(ITestBean aBean) throws Exception { - this.testBean = aBean; - } - - protected String getConfigPath() { - return "afterThrowing-advice-tests.xml"; - } - - protected void onSetUp() throws Exception { - mockControl = MockControl.createNiceControl(AfterThrowingAdviceBindingCollaborator.class); - mockCollaborator = (AfterThrowingAdviceBindingCollaborator) mockControl.getMock(); + @Before + public void setUp() { + ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("afterThrowing-advice-tests.xml", getClass()); + + testBean = (ITestBean) ctx.getBean("testBean"); + afterThrowingAdviceAspect = (AfterThrowingAdviceBindingTestAspect) ctx.getBean("testAspect"); + + mockCollaborator = createNiceMock(AfterThrowingAdviceBindingCollaborator.class); afterThrowingAdviceAspect.setCollaborator(mockCollaborator); } - - - // Simple test to ensure all is well with the XML file. - // Note that this implicitly tests that the arg-names binding is working. - public void testParse() { + + @After + public void tearDown() { + verify(mockCollaborator); } - public void testSimpleAfterThrowing() { + @Test(expected=Throwable.class) + public void testSimpleAfterThrowing() throws Throwable { mockCollaborator.noArgs(); - mockControl.replay(); - try { - this.testBean.exceptional(new Throwable()); - fail("should throw exception"); - } catch (Throwable t) { - // no-op - } - mockControl.verify(); + replay(mockCollaborator); + this.testBean.exceptional(new Throwable()); } - public void testAfterThrowingWithBinding() { + @Test(expected=Throwable.class) + public void testAfterThrowingWithBinding() throws Throwable { Throwable t = new Throwable(); mockCollaborator.oneThrowable(t); - mockControl.replay(); - try { - this.testBean.exceptional(t); - fail("should throw exception"); - } catch (Throwable x) { - // no-op - } - mockControl.verify(); + replay(mockCollaborator); + this.testBean.exceptional(t); } - public void testAfterThrowingWithNamedTypeRestriction() { + @Test(expected=Throwable.class) + public void testAfterThrowingWithNamedTypeRestriction() throws Throwable { Throwable t = new Throwable(); // need a strict mock for this test... - mockControl = MockControl.createControl(AfterThrowingAdviceBindingCollaborator.class); - mockCollaborator = (AfterThrowingAdviceBindingCollaborator) mockControl.getMock(); + mockCollaborator = createMock(AfterThrowingAdviceBindingCollaborator.class); afterThrowingAdviceAspect.setCollaborator(mockCollaborator); mockCollaborator.noArgs(); mockCollaborator.oneThrowable(t); mockCollaborator.noArgsOnThrowableMatch(); - mockControl.replay(); - try { - this.testBean.exceptional(t); - fail("should throw exception"); - } catch (Throwable x) { - // no-op - } - mockControl.verify(); + replay(mockCollaborator); + this.testBean.exceptional(t); } - public void testAfterThrowingWithRuntimeExceptionBinding() { + @Test(expected=Throwable.class) + public void testAfterThrowingWithRuntimeExceptionBinding() throws Throwable { RuntimeException ex = new RuntimeException(); mockCollaborator.oneRuntimeException(ex); - mockControl.replay(); - try { - this.testBean.exceptional(ex); - fail("should throw exception"); - } catch (Throwable x) { - // no-op - } - mockControl.verify(); + replay(mockCollaborator); + this.testBean.exceptional(ex); } - public void testAfterThrowingWithTypeSpecified() { + @Test(expected=Throwable.class) + public void testAfterThrowingWithTypeSpecified() throws Throwable { mockCollaborator.noArgsOnThrowableMatch(); - mockControl.replay(); - try { - this.testBean.exceptional(new Throwable()); - fail("should throw exception"); - } catch (Throwable t) { - // no-op - } - mockControl.verify(); + replay(mockCollaborator); + this.testBean.exceptional(new Throwable()); } - public void testAfterThrowingWithRuntimeTypeSpecified() { + @Test(expected=Throwable.class) + public void testAfterThrowingWithRuntimeTypeSpecified() throws Throwable { mockCollaborator.noArgsOnRuntimeExceptionMatch(); - mockControl.replay(); - try { - this.testBean.exceptional(new RuntimeException()); - fail("should throw exception"); - } catch (Throwable t) { - // no-op - } - mockControl.verify(); + replay(mockCollaborator); + this.testBean.exceptional(new RuntimeException()); + verify(mockCollaborator); + } + +} + + +final class AfterThrowingAdviceBindingTestAspect { + + // collaborator interface that makes it easy to test this aspect is + // working as expected through mocking. + public interface AfterThrowingAdviceBindingCollaborator { + void noArgs(); + void oneThrowable(Throwable t); + void oneRuntimeException(RuntimeException re); + void noArgsOnThrowableMatch(); + void noArgsOnRuntimeExceptionMatch(); + } + + protected AfterThrowingAdviceBindingCollaborator collaborator = null; + + public void setCollaborator(AfterThrowingAdviceBindingCollaborator aCollaborator) { + this.collaborator = aCollaborator; + } + + public void noArgs() { + this.collaborator.noArgs(); + } + + public void oneThrowable(Throwable t) { + this.collaborator.oneThrowable(t); + } + + public void oneRuntimeException(RuntimeException ex) { + this.collaborator.oneRuntimeException(ex); + } + + public void noArgsOnThrowableMatch() { + this.collaborator.noArgsOnThrowableMatch(); + } + + public void noArgsOnRuntimeExceptionMatch() { + this.collaborator.noArgsOnRuntimeExceptionMatch(); } } diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AroundAdviceBindingTestAspect.java b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AroundAdviceBindingTestAspect.java deleted file mode 100644 index 58ab885590..0000000000 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AroundAdviceBindingTestAspect.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright 2002-2006 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.aspectj; - -import org.aspectj.lang.ProceedingJoinPoint; - -/** - * Aspect used as part of before advice binding tests. - * - * @author Adrian Colyer - */ -public class AroundAdviceBindingTestAspect { - - private AroundAdviceBindingCollaborator collaborator = null; - - public void setCollaborator(AroundAdviceBindingCollaborator aCollaborator) { - this.collaborator = aCollaborator; - } - - // "advice" methods - public void oneIntArg(ProceedingJoinPoint pjp, int age) throws Throwable { - this.collaborator.oneIntArg(age); - pjp.proceed(); - } - - public int oneObjectArg(ProceedingJoinPoint pjp, Object bean) throws Throwable { - this.collaborator.oneObjectArg(bean); - return ((Integer) pjp.proceed()).intValue(); - } - - public void oneIntAndOneObject(ProceedingJoinPoint pjp, int x , Object o) throws Throwable { - this.collaborator.oneIntAndOneObject(x,o); - pjp.proceed(); - } - - public int justJoinPoint(ProceedingJoinPoint pjp) throws Throwable { - this.collaborator.justJoinPoint(pjp.getSignature().getName()); - return ((Integer) pjp.proceed()).intValue(); - } - - - /** - * Collaborator interface that makes it easy to test this aspect - * is working as expected through mocking. - */ - public interface AroundAdviceBindingCollaborator { - - void oneIntArg(int x); - - void oneObjectArg(Object o); - - void oneIntAndOneObject(int x, Object o); - - void justJoinPoint(String s); - } - -} diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AroundAdviceBindingTests.java b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AroundAdviceBindingTests.java index 5a4f840991..7512d2a9f6 100644 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AroundAdviceBindingTests.java +++ b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AroundAdviceBindingTests.java @@ -16,66 +16,135 @@ package org.springframework.aop.aspectj; -import org.easymock.MockControl; +import static org.easymock.EasyMock.*; +import static org.junit.Assert.assertTrue; +import org.aspectj.lang.ProceedingJoinPoint; +import org.junit.Before; +import org.junit.Test; import org.springframework.aop.aspectj.AroundAdviceBindingTestAspect.AroundAdviceBindingCollaborator; +import org.springframework.aop.framework.Advised; +import org.springframework.aop.support.AopUtils; +import org.springframework.beans.ITestBean; +import org.springframework.beans.TestBean; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Tests for various parameter binding scenarios with before advice. * * @author Adrian Colyer + * @author Chris Beams */ -public class AroundAdviceBindingTests extends AbstractAdviceBindingTests { - - private MockControl mockControl; +public class AroundAdviceBindingTests { private AroundAdviceBindingCollaborator mockCollaborator; - private AroundAdviceBindingTestAspect aroundAdviceAspect; + private ITestBean testBeanProxy; - - public void setAroundAdviceAspect(AroundAdviceBindingTestAspect anAspect) { - this.aroundAdviceAspect = anAspect; - } + private TestBean testBeanTarget; + + protected ApplicationContext ctx; protected String getConfigPath() { return "around-advice-tests.xml"; } - protected void onSetUp() throws Exception { - super.onSetUp(); - mockControl = MockControl.createNiceControl(AroundAdviceBindingCollaborator.class); - mockCollaborator = (AroundAdviceBindingCollaborator) mockControl.getMock(); + @Before + public void onSetUp() throws Exception { + ctx = new ClassPathXmlApplicationContext(getConfigPath(), getClass()); + + AroundAdviceBindingTestAspect aroundAdviceAspect = ((AroundAdviceBindingTestAspect) ctx.getBean("testAspect")); + + ITestBean injectedTestBean = (ITestBean) ctx.getBean("testBean"); + assertTrue(AopUtils.isAopProxy(injectedTestBean)); + + this.testBeanProxy = injectedTestBean; + // we need the real target too, not just the proxy... + + this.testBeanTarget = (TestBean) ((Advised) testBeanProxy).getTargetSource().getTarget(); + + mockCollaborator = createNiceMock(AroundAdviceBindingCollaborator.class); aroundAdviceAspect.setCollaborator(mockCollaborator); } - + @Test public void testOneIntArg() { mockCollaborator.oneIntArg(5); - mockControl.replay(); + replay(mockCollaborator); testBeanProxy.setAge(5); - mockControl.verify(); + verify(mockCollaborator); } + @Test public void testOneObjectArgBoundToTarget() { mockCollaborator.oneObjectArg(this.testBeanTarget); - mockControl.replay(); + replay(mockCollaborator); testBeanProxy.getAge(); - mockControl.verify(); + verify(mockCollaborator); } + @Test public void testOneIntAndOneObjectArgs() { mockCollaborator.oneIntAndOneObject(5, this.testBeanProxy); - mockControl.replay(); + replay(mockCollaborator); testBeanProxy.setAge(5); - mockControl.verify(); + verify(mockCollaborator); } + @Test public void testJustJoinPoint() { mockCollaborator.justJoinPoint("getAge"); - mockControl.replay(); + replay(mockCollaborator); testBeanProxy.getAge(); - mockControl.verify(); + verify(mockCollaborator); } } + + +class AroundAdviceBindingTestAspect { + + private AroundAdviceBindingCollaborator collaborator = null; + + public void setCollaborator(AroundAdviceBindingCollaborator aCollaborator) { + this.collaborator = aCollaborator; + } + + // "advice" methods + public void oneIntArg(ProceedingJoinPoint pjp, int age) throws Throwable { + this.collaborator.oneIntArg(age); + pjp.proceed(); + } + + public int oneObjectArg(ProceedingJoinPoint pjp, Object bean) throws Throwable { + this.collaborator.oneObjectArg(bean); + return ((Integer) pjp.proceed()).intValue(); + } + + public void oneIntAndOneObject(ProceedingJoinPoint pjp, int x , Object o) throws Throwable { + this.collaborator.oneIntAndOneObject(x,o); + pjp.proceed(); + } + + public int justJoinPoint(ProceedingJoinPoint pjp) throws Throwable { + this.collaborator.justJoinPoint(pjp.getSignature().getName()); + return ((Integer) pjp.proceed()).intValue(); + } + + /** + * Collaborator interface that makes it easy to test this aspect + * is working as expected through mocking. + */ + public interface AroundAdviceBindingCollaborator { + + void oneIntArg(int x); + + void oneObjectArg(Object o); + + void oneIntAndOneObject(int x, Object o); + + void justJoinPoint(String s); + } + +} diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AroundAdviceCircularTests.java b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AroundAdviceCircularTests.java index 7294ebb1be..ff558864fc 100644 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AroundAdviceCircularTests.java +++ b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AroundAdviceCircularTests.java @@ -16,21 +16,26 @@ package org.springframework.aop.aspectj; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; import org.springframework.aop.support.AopUtils; /** * @author Juergen Hoeller + * @author Chris Beams */ -public class AroundAdviceCircularTests extends AroundAdviceBindingTests { +public final class AroundAdviceCircularTests extends AroundAdviceBindingTests { protected String getConfigPath() { return "around-advice-circular-tests.xml"; } + @Test public void testBothBeansAreProxies() { - Object tb = getApplicationContext().getBean("testBean"); + Object tb = ctx.getBean("testBean"); assertTrue(AopUtils.isAopProxy(tb)); - Object tb2 = getApplicationContext().getBean("testBean2"); + Object tb2 = ctx.getBean("testBean2"); assertTrue(AopUtils.isAopProxy(tb2)); } diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AspectAndAdvicePrecedenceTests.java b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AspectAndAdvicePrecedenceTests.java index f670edfbc5..068b2fa7e8 100644 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AspectAndAdvicePrecedenceTests.java +++ b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AspectAndAdvicePrecedenceTests.java @@ -16,52 +16,49 @@ package org.springframework.aop.aspectj; +import static org.junit.Assert.fail; + +import java.lang.reflect.Method; + +import org.aspectj.lang.ProceedingJoinPoint; +import org.junit.Before; +import org.junit.Test; +import org.springframework.aop.MethodBeforeAdvice; import org.springframework.beans.ITestBean; -import org.springframework.test.AbstractDependencyInjectionSpringContextTests; +import org.springframework.beans.factory.BeanNameAware; +import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.core.Ordered; /** * @author Adrian Colyer + * @author Chris Beams */ -public class AspectAndAdvicePrecedenceTests extends AbstractDependencyInjectionSpringContextTests { +public final class AspectAndAdvicePrecedenceTests { private PrecedenceTestAspect highPrecedenceAspect; + private PrecedenceTestAspect lowPrecedenceAspect; - private SimpleSpringBeforeAdvice lowPrecedenceSpringAdvice; + private SimpleSpringBeforeAdvice highPrecedenceSpringAdvice; + + private SimpleSpringBeforeAdvice lowPrecedenceSpringAdvice; + private ITestBean testBean; - public AspectAndAdvicePrecedenceTests() { - setAutowireMode(AUTOWIRE_BY_NAME); + @Before + public void setUp() { + ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("advice-precedence-tests.xml", getClass()); + highPrecedenceAspect = (PrecedenceTestAspect) ctx.getBean("highPrecedenceAspect"); + lowPrecedenceAspect = (PrecedenceTestAspect) ctx.getBean("lowPrecedenceAspect"); + highPrecedenceSpringAdvice = (SimpleSpringBeforeAdvice) ctx.getBean("highPrecedenceSpringAdvice"); + lowPrecedenceSpringAdvice = (SimpleSpringBeforeAdvice) ctx.getBean("lowPrecedenceSpringAdvice"); + testBean = (ITestBean) ctx.getBean("testBean"); } - public void setHighPrecedenceAspect(PrecedenceTestAspect highPrecedenceAspect) { - this.highPrecedenceAspect = highPrecedenceAspect; - } - - public void setLowPrecedenceAspect(PrecedenceTestAspect lowPrecedenceAspect) { - this.lowPrecedenceAspect = lowPrecedenceAspect; - } - - public void setLowPrecedenceSpringAdvice(SimpleSpringBeforeAdvice lowPrecedenceSpringAdvice) { - this.lowPrecedenceSpringAdvice = lowPrecedenceSpringAdvice; - } - - public void setHighPrecedenceSpringAdvice(SimpleSpringBeforeAdvice highPrecedenceSpringAdvice) { - this.highPrecedenceSpringAdvice = highPrecedenceSpringAdvice; - } - - public void setTestBean(ITestBean testBean) { - this.testBean = testBean; - } - - protected String getConfigPath() { - return "advice-precedence-tests.xml"; - } - - // ========== end of test case set up, start of tests proper =================== + @Test public void testAdviceOrder() { PrecedenceTestAspect.Collaborator collaborator = new PrecedenceVerifyingCollaborator(); this.highPrecedenceAspect.setCollaborator(collaborator); @@ -138,3 +135,107 @@ public class AspectAndAdvicePrecedenceTests extends AbstractDependencyInjectionS } } + + +class PrecedenceTestAspect implements BeanNameAware, Ordered { + + private String name; + + private int order = Ordered.LOWEST_PRECEDENCE; + + private Collaborator collaborator; + + + public void setBeanName(String name) { + this.name = name; + } + + public void setOrder(int order) { + this.order = order; + } + + public int getOrder() { + return order; + } + + public void setCollaborator(Collaborator collaborator) { + this.collaborator = collaborator; + } + + public void beforeAdviceOne() { + this.collaborator.beforeAdviceOne(this.name); + } + + public void beforeAdviceTwo() { + this.collaborator.beforeAdviceTwo(this.name); + } + + public int aroundAdviceOne(ProceedingJoinPoint pjp) { + int ret = -1; + this.collaborator.aroundAdviceOne(this.name); + try { + ret = ((Integer)pjp.proceed()).intValue(); + } + catch(Throwable t) { throw new RuntimeException(t); } + this.collaborator.aroundAdviceOne(this.name); + return ret; + } + + public int aroundAdviceTwo(ProceedingJoinPoint pjp) { + int ret = -1; + this.collaborator.aroundAdviceTwo(this.name); + try { + ret = ((Integer)pjp.proceed()).intValue(); + } + catch(Throwable t) {throw new RuntimeException(t);} + this.collaborator.aroundAdviceTwo(this.name); + return ret; + } + + public void afterAdviceOne() { + this.collaborator.afterAdviceOne(this.name); + } + + public void afterAdviceTwo() { + this.collaborator.afterAdviceTwo(this.name); + } + + + public interface Collaborator { + + void beforeAdviceOne(String beanName); + void beforeAdviceTwo(String beanName); + void aroundAdviceOne(String beanName); + void aroundAdviceTwo(String beanName); + void afterAdviceOne(String beanName); + void afterAdviceTwo(String beanName); + } + +} + + +class SimpleSpringBeforeAdvice implements MethodBeforeAdvice, BeanNameAware { + + private PrecedenceTestAspect.Collaborator collaborator; + private String name; + + /* (non-Javadoc) + * @see org.springframework.aop.MethodBeforeAdvice#before(java.lang.reflect.Method, java.lang.Object[], java.lang.Object) + */ + public void before(Method method, Object[] args, Object target) + throws Throwable { + this.collaborator.beforeAdviceOne(this.name); + } + + public void setCollaborator(PrecedenceTestAspect.Collaborator collaborator) { + this.collaborator = collaborator; + } + + /* (non-Javadoc) + * @see org.springframework.beans.factory.BeanNameAware#setBeanName(java.lang.String) + */ + public void setBeanName(String name) { + this.name = name; + } + +} diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AspectJExpressionPointcutAdvisorTests.java b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AspectJExpressionPointcutAdvisorTests.java index cdfcf2684c..984131569d 100644 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AspectJExpressionPointcutAdvisorTests.java +++ b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AspectJExpressionPointcutAdvisorTests.java @@ -16,38 +16,35 @@ package org.springframework.aop.aspectj; +import static org.junit.Assert.assertEquals; + +import org.aopalliance.intercept.MethodInterceptor; +import org.aopalliance.intercept.MethodInvocation; +import org.junit.Before; +import org.junit.Test; import org.springframework.beans.ITestBean; -import org.springframework.test.AbstractDependencyInjectionSpringContextTests; +import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @author Rob Harrop * @author Juergen Hoeller + * @author Chris Beams */ -public class AspectJExpressionPointcutAdvisorTests extends AbstractDependencyInjectionSpringContextTests { +public final class AspectJExpressionPointcutAdvisorTests { private ITestBean testBean; private CallCountingInterceptor interceptor; - - public void setTestBean(ITestBean testBean) { - this.testBean = testBean; + @Before + public void setUp() { + ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("aspectj.xml", getClass()); + testBean = (ITestBean) ctx.getBean("testBean"); + interceptor = (CallCountingInterceptor) ctx.getBean("interceptor"); } - public void setInterceptor(CallCountingInterceptor interceptor) { - this.interceptor = interceptor; - } - - protected String getConfigPath() { - return "aspectj.xml"; - } - - protected void onSetUp() throws Exception { - interceptor.reset(); - } - - - public void testPointcutting() throws Exception { + @Test + public void testPointcutting() { assertEquals("Count should be 0", 0, interceptor.getCount()); testBean.getSpouses(); assertEquals("Count should be 1", 1, interceptor.getCount()); @@ -56,3 +53,22 @@ public class AspectJExpressionPointcutAdvisorTests extends AbstractDependencyInj } } + + +class CallCountingInterceptor implements MethodInterceptor { + + private int count; + + public Object invoke(MethodInvocation methodInvocation) throws Throwable { + count++; + return methodInvocation.proceed(); + } + + public int getCount() { + return count; + } + + public void reset() { + this.count = 0; + } +} \ No newline at end of file diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AuthenticationLogger.java b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AuthenticationLogger.java deleted file mode 100644 index 8d4ad88ad9..0000000000 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/AuthenticationLogger.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright 2002-2006 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. - * - * Created on 13-Feb-2006 by Adrian Colyer - */ -package org.springframework.aop.aspectj; - -/** - * Used by before-advice-tests.xml - * @author Adrian Colyer - * @since 2.0 - */ -public class AuthenticationLogger { - - public void logAuthenticationAttempt(String username) { - System.out.println("User [" + username + "] attempting to authenticate"); - } - -} - -class SecurityManager { - public boolean authenticate(String username, String password) { - return false; - } -} diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/BeanNameAwareMixin.java b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/BeanNameAwareMixin.java deleted file mode 100644 index 1bc6e9c22d..0000000000 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/BeanNameAwareMixin.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright 2002-2006 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. - * - * Created on 15 Nov 2006 by Adrian Colyer - */ -package org.springframework.aop.aspectj; - -import org.springframework.beans.factory.BeanNameAware; - -/** - * @author Adrian Colyer - * @since 2.0 - */ -public class BeanNameAwareMixin implements BeanNameAware { - - private String beanName; - - /* (non-Javadoc) - * @see org.springframework.beans.factory.BeanNameAware#setBeanName(java.lang.String) - */ - public void setBeanName(String name) { - this.beanName = name; - } - -} diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/BeanNamePointcutAtAspectTests.java b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/BeanNamePointcutAtAspectTests.java index be9b305759..31a709f47d 100644 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/BeanNamePointcutAtAspectTests.java +++ b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/BeanNamePointcutAtAspectTests.java @@ -16,46 +16,45 @@ package org.springframework.aop.aspectj; +import static org.junit.Assert.*; + import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; - +import org.junit.Test; import org.springframework.aop.aspectj.annotation.AspectJProxyFactory; import org.springframework.aop.framework.Advised; import org.springframework.beans.ITestBean; import org.springframework.beans.TestBean; -import org.springframework.test.AbstractDependencyInjectionSpringContextTests; +import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Test for correct application of the bean() PCD for @AspectJ-based aspects. * * @author Ramnivas Laddad * @author Juergen Hoeller + * @author Chris Beams */ -public class BeanNamePointcutAtAspectTests extends AbstractDependencyInjectionSpringContextTests { +public final class BeanNamePointcutAtAspectTests { - protected ITestBean testBean1; + private ITestBean testBean1; - protected ITestBean testBean2; + private ITestBean testBean2; - protected ITestBean testBean3; + private ITestBean testBean3; - protected CounterAspect counterAspect; + private CounterAspect counterAspect; - public BeanNamePointcutAtAspectTests() { - setPopulateProtectedVariables(true); + @org.junit.Before + public void setUp() { + ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("bean-name-pointcut-atAspect-tests.xml", getClass()); + counterAspect = (CounterAspect) ctx.getBean("counterAspect"); + testBean1 = (ITestBean) ctx.getBean("testBean1"); + testBean2 = (ITestBean) ctx.getBean("testBean2"); + testBean3 = (ITestBean) ctx.getBean("testBean3"); } - protected String getConfigPath() { - return "bean-name-pointcut-atAspect-tests.xml"; - } - - protected void onSetUp() throws Exception { - counterAspect.count = 0; - super.onSetUp(); - } - - + @Test public void testMatchingBeanName() { assertTrue("Expected a proxy", testBean1 instanceof Advised); // Call two methods to test for SPR-3953-like condition @@ -64,12 +63,14 @@ public class BeanNamePointcutAtAspectTests extends AbstractDependencyInjectionSp assertEquals(2 /*TODO: make this 3 when upgrading to AspectJ 1.6.0 and advice in CounterAspect are uncommented*/, counterAspect.count); } + @Test public void testNonMatchingBeanName() { assertFalse("Didn't expect a proxy", testBean3 instanceof Advised); testBean3.setAge(20); assertEquals(0, counterAspect.count); } + @Test public void testProgrammaticProxyCreation() { ITestBean testBean = new TestBean(); @@ -86,32 +87,17 @@ public class BeanNamePointcutAtAspectTests extends AbstractDependencyInjectionSp assertEquals("Programmatically created proxy shouldn't match bean()", 0, myCounterAspect.count); } - - @Aspect - public static class CounterAspect { - - int count; - - @Before("execution(* set*(..)) && bean(testBean1)") - public void increment1ForAnonymousPointcut() { - count++; - } - -// @Pointcut("execution(* setAge(..)) && bean(testBean1)") -// public void testBean1SetAge() {} -// -// @Pointcut("execution(* setAge(..)) && bean(testBean2)") -// public void testBean2SetAge() {} -// -// @Before("testBean1SetAge()") -// public void increment1() { -// count++; -// } -// -// @Before("testBean2SetAge()") -// public void increment2() { -// count++; -// } - } - +} + + +@Aspect +class CounterAspect { + + int count; + + @Before("execution(* set*(..)) && bean(testBean1)") + public void increment1ForAnonymousPointcut() { + count++; + } + } diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/BeanNamePointcutTests.java b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/BeanNamePointcutTests.java index 7935a1777b..76273948a9 100644 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/BeanNamePointcutTests.java +++ b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/BeanNamePointcutTests.java @@ -16,51 +16,61 @@ package org.springframework.aop.aspectj; +import static org.junit.Assert.*; + import java.lang.reflect.Method; import java.util.Map; +import org.junit.Before; +import org.junit.Test; import org.springframework.aop.MethodBeforeAdvice; import org.springframework.aop.framework.Advised; import org.springframework.beans.ITestBean; import org.springframework.beans.factory.FactoryBean; -import org.springframework.test.AbstractDependencyInjectionSpringContextTests; +import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Test for correct application of the bean() PCD for XML-based AspectJ aspects. * * @author Ramnivas Laddad * @author Juergen Hoeller + * @author Chris Beams */ -public class BeanNamePointcutTests extends AbstractDependencyInjectionSpringContextTests { +public final class BeanNamePointcutTests { - protected ITestBean testBean1; - protected ITestBean testBean2; - protected ITestBean testBeanContainingNestedBean; - protected Map testFactoryBean1; - protected Map testFactoryBean2; - protected Counter counterAspect; + private ITestBean testBean1; + private ITestBean testBean2; + private ITestBean testBeanContainingNestedBean; + private Map testFactoryBean1; + private Map testFactoryBean2; + private Counter counterAspect; - protected ITestBean interceptThis; - protected ITestBean dontInterceptThis; - protected TestInterceptor testInterceptor; + private ITestBean interceptThis; + private ITestBean dontInterceptThis; + private TestInterceptor testInterceptor; + + private ClassPathXmlApplicationContext ctx; - public BeanNamePointcutTests() { - setPopulateProtectedVariables(true); + @Before + public void setUp() { + ctx = new ClassPathXmlApplicationContext("bean-name-pointcut-tests.xml", getClass()); + testBean1 = (ITestBean) ctx.getBean("testBean1"); + testBean2 = (ITestBean) ctx.getBean("testBean2"); + testBeanContainingNestedBean = (ITestBean) ctx.getBean("testBeanContainingNestedBean"); + testFactoryBean1 = (Map) ctx.getBean("testFactoryBean1"); + testFactoryBean2 = (Map) ctx.getBean("testFactoryBean2"); + counterAspect = (Counter) ctx.getBean("counterAspect"); + interceptThis = (ITestBean) ctx.getBean("interceptThis"); + dontInterceptThis = (ITestBean) ctx.getBean("dontInterceptThis"); + testInterceptor = (TestInterceptor) ctx.getBean("testInterceptor"); + + counterAspect.reset(); } - protected String getConfigPath() { - return "bean-name-pointcut-tests.xml"; - } - - protected void onSetUp() throws Exception { - this.counterAspect.reset(); - super.onSetUp(); - } - - // We don't need to test all combination of pointcuts due to BeanNamePointcutMatchingTests + @Test public void testMatchingBeanName() { assertTrue("Matching bean must be advised (proxied)", this.testBean1 instanceof Advised); // Call two methods to test for SPR-3953-like condition @@ -69,34 +79,39 @@ public class BeanNamePointcutTests extends AbstractDependencyInjectionSpringCont assertEquals("Advice not executed: must have been", 2, this.counterAspect.getCount()); } + @Test public void testNonMatchingBeanName() { assertFalse("Non-matching bean must *not* be advised (proxied)", this.testBean2 instanceof Advised); this.testBean2.setAge(20); assertEquals("Advice must *not* have been executed", 0, this.counterAspect.getCount()); } + @Test public void testNonMatchingNestedBeanName() { assertFalse("Non-matching bean must *not* be advised (proxied)", this.testBeanContainingNestedBean.getDoctor() instanceof Advised); } + @Test public void testMatchingFactoryBeanObject() { assertTrue("Matching bean must be advised (proxied)", this.testFactoryBean1 instanceof Advised); assertEquals("myValue", this.testFactoryBean1.get("myKey")); assertEquals("myValue", this.testFactoryBean1.get("myKey")); assertEquals("Advice not executed: must have been", 2, this.counterAspect.getCount()); - FactoryBean fb = (FactoryBean) getApplicationContext().getBean("&testFactoryBean1"); + FactoryBean fb = (FactoryBean) ctx.getBean("&testFactoryBean1"); assertTrue("FactoryBean itself must *not* be advised", !(fb instanceof Advised)); } + @Test public void testMatchingFactoryBeanItself() { assertTrue("Matching bean must *not* be advised (proxied)", !(this.testFactoryBean2 instanceof Advised)); - FactoryBean fb = (FactoryBean) getApplicationContext().getBean("&testFactoryBean2"); + FactoryBean fb = (FactoryBean) ctx.getBean("&testFactoryBean2"); assertTrue("FactoryBean itself must be advised", fb instanceof Advised); assertTrue(Map.class.isAssignableFrom(fb.getObjectType())); assertTrue(Map.class.isAssignableFrom(fb.getObjectType())); assertEquals("Advice not executed: must have been", 2, this.counterAspect.getCount()); } + @Test public void testPointcutAdvisorCombination() { assertTrue("Matching bean must be advised (proxied)", this.interceptThis instanceof Advised); assertFalse("Non-matching bean must *not* be advised (proxied)", this.dontInterceptThis instanceof Advised); diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/BeforeAdviceBindingTests.java b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/BeforeAdviceBindingTests.java index 52792be622..d976422be9 100644 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/BeforeAdviceBindingTests.java +++ b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/BeforeAdviceBindingTests.java @@ -16,75 +16,109 @@ package org.springframework.aop.aspectj; -import org.easymock.MockControl; +import static org.easymock.EasyMock.*; +import static org.junit.Assert.assertTrue; +import org.junit.Before; +import org.junit.Test; import org.springframework.aop.aspectj.AdviceBindingTestAspect.AdviceBindingCollaborator; +import org.springframework.aop.framework.Advised; +import org.springframework.aop.support.AopUtils; +import org.springframework.beans.ITestBean; +import org.springframework.beans.TestBean; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Tests for various parameter binding scenarios with before advice. * * @author Adrian Colyer * @author Rod Johnson + * @author Chris Beams */ -public class BeforeAdviceBindingTests extends AbstractAdviceBindingTests { - - private AdviceBindingTestAspect beforeAdviceAspect; - - private MockControl mockControl; +public final class BeforeAdviceBindingTests { private AdviceBindingCollaborator mockCollaborator; - - public void setBeforeAdviceAspect(AdviceBindingTestAspect anAspect) { - this.beforeAdviceAspect = anAspect; - } + private ITestBean testBeanProxy; + + private TestBean testBeanTarget; protected String getConfigPath() { return "before-advice-tests.xml"; } - protected void onSetUp() throws Exception { - super.onSetUp(); - mockControl = MockControl.createNiceControl(AdviceBindingCollaborator.class); - mockCollaborator = (AdviceBindingCollaborator) mockControl.getMock(); + @Before + public void setUp() throws Exception { + ApplicationContext ctx = new ClassPathXmlApplicationContext(getConfigPath(), getClass()); + + testBeanProxy = (ITestBean) ctx.getBean("testBean"); + assertTrue(AopUtils.isAopProxy(testBeanProxy)); + + // we need the real target too, not just the proxy... + testBeanTarget = (TestBean) ((Advised) testBeanProxy).getTargetSource().getTarget(); + + AdviceBindingTestAspect beforeAdviceAspect = (AdviceBindingTestAspect) ctx.getBean("testAspect"); + + mockCollaborator = createNiceMock(AdviceBindingCollaborator.class); beforeAdviceAspect.setCollaborator(mockCollaborator); } + @Test public void testOneIntArg() { mockCollaborator.oneIntArg(5); - mockControl.replay(); + replay(mockCollaborator); testBeanProxy.setAge(5); - mockControl.verify(); + verify(mockCollaborator); } + @Test public void testOneObjectArgBoundToProxyUsingThis() { mockCollaborator.oneObjectArg(this.testBeanProxy); - mockControl.replay(); + replay(mockCollaborator); testBeanProxy.getAge(); - mockControl.verify(); + verify(mockCollaborator); } + @Test public void testOneIntAndOneObjectArgs() { mockCollaborator.oneIntAndOneObject(5,this.testBeanTarget); - mockControl.replay(); + replay(mockCollaborator); testBeanProxy.setAge(5); - mockControl.verify(); + verify(mockCollaborator); } + @Test public void testNeedsJoinPoint() { mockCollaborator.needsJoinPoint("getAge"); - mockControl.replay(); + replay(mockCollaborator); testBeanProxy.getAge(); - mockControl.verify(); + verify(mockCollaborator); } + @Test public void testNeedsJoinPointStaticPart() { mockCollaborator.needsJoinPointStaticPart("getAge"); - mockControl.replay(); + replay(mockCollaborator); testBeanProxy.getAge(); - mockControl.verify(); + verify(mockCollaborator); } } + + +class AuthenticationLogger { + + public void logAuthenticationAttempt(String username) { + System.out.println("User [" + username + "] attempting to authenticate"); + } + +} + +class SecurityManager { + public boolean authenticate(String username, String password) { + return false; + } +} diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/CallCountingInterceptor.java b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/CallCountingInterceptor.java deleted file mode 100644 index aa5eb61edd..0000000000 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/CallCountingInterceptor.java +++ /dev/null @@ -1,26 +0,0 @@ - -package org.springframework.aop.aspectj; - -import org.aopalliance.intercept.MethodInterceptor; -import org.aopalliance.intercept.MethodInvocation; - -/** - * @author robh - */ -class CallCountingInterceptor implements MethodInterceptor { - - private int count; - - public Object invoke(MethodInvocation methodInvocation) throws Throwable { - count++; - return methodInvocation.proceed(); - } - - public int getCount() { - return count; - } - - public void reset() { - this.count = 0; - } -} diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/Counter.java b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/Counter.java index c8ae64ce44..dc5b652fa4 100644 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/Counter.java +++ b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/Counter.java @@ -21,7 +21,7 @@ package org.springframework.aop.aspectj; * * @author Ramnivas Laddad */ -public class Counter implements ICounter { +final class Counter implements ICounter { private int count; diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/DeclarationOrderIndependenceTests.java b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/DeclarationOrderIndependenceTests.java index a962b3d04b..37a3de4617 100644 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/DeclarationOrderIndependenceTests.java +++ b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/DeclarationOrderIndependenceTests.java @@ -16,46 +16,45 @@ package org.springframework.aop.aspectj; +import static org.junit.Assert.assertTrue; + import java.io.Serializable; +import org.aspectj.lang.ProceedingJoinPoint; +import org.junit.Before; +import org.junit.Test; import org.springframework.beans.factory.BeanNameAware; -import org.springframework.test.AbstractDependencyInjectionSpringContextTests; +import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @author Adrian Colyer + * @author Chris Beams */ -public class DeclarationOrderIndependenceTests extends AbstractDependencyInjectionSpringContextTests { +public final class DeclarationOrderIndependenceTests { private TopsyTurvyAspect aspect; private TopsyTurvyTarget target; - public DeclarationOrderIndependenceTests() { - setAutowireMode(AUTOWIRE_BY_NAME); - } - - public void setTopsyTurvyAspect(TopsyTurvyAspect aspect) { - this.aspect = aspect; + @Before + public void setUp() { + ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("topsy-turvy-aspect.xml", getClass()); + aspect = (TopsyTurvyAspect) ctx.getBean("topsyTurvyAspect"); + target = (TopsyTurvyTarget) ctx.getBean("topsyTurvyTarget"); } - public void setTopsyTurvyTarget(TopsyTurvyTarget target) { - this.target = target; - } - - protected String getConfigPath() { - return "topsy-turvy-aspect.xml"; - } - - + @Test public void testTargetIsSerializable() { assertTrue("target bean is serializable",this.target instanceof Serializable); } + @Test public void testTargetIsBeanNameAware() { assertTrue("target bean is bean name aware",this.target instanceof BeanNameAware); } + @Test public void testBeforeAdviceFiringOk() { AspectCollaborator collab = new AspectCollaborator(); this.aspect.setCollaborator(collab); @@ -63,6 +62,7 @@ public class DeclarationOrderIndependenceTests extends AbstractDependencyInjecti assertTrue("before advice fired",collab.beforeFired); } + @Test public void testAroundAdviceFiringOk() { AspectCollaborator collab = new AspectCollaborator(); this.aspect.setCollaborator(collab); @@ -70,6 +70,7 @@ public class DeclarationOrderIndependenceTests extends AbstractDependencyInjecti assertTrue("around advice fired",collab.aroundFired); } + @Test public void testAfterReturningFiringOk() { AspectCollaborator collab = new AspectCollaborator(); this.aspect.setCollaborator(collab); @@ -77,34 +78,114 @@ public class DeclarationOrderIndependenceTests extends AbstractDependencyInjecti assertTrue("after returning advice fired",collab.afterReturningFired); } - private static class AspectCollaborator implements TopsyTurvyAspect.Collaborator { - - public boolean afterReturningFired = false; - public boolean aroundFired = false; - public boolean beforeFired = false; + + /** public visibility is required */ + public static class BeanNameAwareMixin implements BeanNameAware { + + private String beanName; /* (non-Javadoc) - * @see org.springframework.aop.aspectj.TopsyTurvyAspect.Collaborator#afterReturningAdviceFired() + * @see org.springframework.beans.factory.BeanNameAware#setBeanName(java.lang.String) */ - public void afterReturningAdviceFired() { - this.afterReturningFired = true; + public void setBeanName(String name) { + this.beanName = name; } + + } + + /** public visibility is required */ + @SuppressWarnings("serial") + public static class SerializableMixin implements Serializable { + } - /* (non-Javadoc) - * @see org.springframework.aop.aspectj.TopsyTurvyAspect.Collaborator#aroundAdviceFired() - */ - public void aroundAdviceFired() { - this.aroundFired = true; - } +} - /* (non-Javadoc) - * @see org.springframework.aop.aspectj.TopsyTurvyAspect.Collaborator#beforeAdviceFired() - */ - public void beforeAdviceFired() { - this.beforeFired = true; - } - + +class TopsyTurvyAspect { + + interface Collaborator { + void beforeAdviceFired(); + void afterReturningAdviceFired(); + void aroundAdviceFired(); + } + + private Collaborator collaborator; + + public void setCollaborator(Collaborator collaborator) { + this.collaborator = collaborator; + } + + public void before() { + this.collaborator.beforeAdviceFired(); + } + + public void afterReturning() { + this.collaborator.afterReturningAdviceFired(); + } + + public Object around(ProceedingJoinPoint pjp) throws Throwable { + Object ret = pjp.proceed(); + this.collaborator.aroundAdviceFired(); + return ret; } } +interface TopsyTurvyTarget { + + public abstract void doSomething(); + + public abstract int getX(); + +} + + +class TopsyTurvyTargetImpl implements TopsyTurvyTarget { + + private int x = 5; + + /* (non-Javadoc) + * @see org.springframework.aop.aspectj.TopsyTurvyTarget#doSomething() + */ + public void doSomething() { + this.x = 10; + } + + /* (non-Javadoc) + * @see org.springframework.aop.aspectj.TopsyTurvyTarget#getX() + */ + public int getX() { + return x; + } + +} + + +class AspectCollaborator implements TopsyTurvyAspect.Collaborator { + + public boolean afterReturningFired = false; + public boolean aroundFired = false; + public boolean beforeFired = false; + + /* (non-Javadoc) + * @see org.springframework.aop.aspectj.TopsyTurvyAspect.Collaborator#afterReturningAdviceFired() + */ + public void afterReturningAdviceFired() { + this.afterReturningFired = true; + } + + /* (non-Javadoc) + * @see org.springframework.aop.aspectj.TopsyTurvyAspect.Collaborator#aroundAdviceFired() + */ + public void aroundAdviceFired() { + this.aroundFired = true; + } + + /* (non-Javadoc) + * @see org.springframework.aop.aspectj.TopsyTurvyAspect.Collaborator#beforeAdviceFired() + */ + public void beforeAdviceFired() { + this.beforeFired = true; + } + +} diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/DeclareParentsDelegateRefTests.java b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/DeclareParentsDelegateRefTests.java index 8da46feed6..7f3e833382 100644 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/DeclareParentsDelegateRefTests.java +++ b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/DeclareParentsDelegateRefTests.java @@ -16,62 +16,49 @@ package org.springframework.aop.aspectj; -import org.springframework.test.AbstractDependencyInjectionSpringContextTests; +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; +import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @author Ramnivas Laddad + * @author Chris Beams */ -public class DeclareParentsDelegateRefTests extends AbstractDependencyInjectionSpringContextTests { +public class DeclareParentsDelegateRefTests { protected NoMethodsBean noMethodsBean; - protected CounterImpl counter; + protected Counter counter; - public DeclareParentsDelegateRefTests() { - setPopulateProtectedVariables(true); - } - - protected void onSetUp() throws Exception { + @Before + public void setUp() { + ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("declare-parents-delegate-ref-tests.xml", getClass()); + noMethodsBean = (NoMethodsBean) ctx.getBean("noMethodsBean"); + counter = (Counter) ctx.getBean("counter"); counter.reset(); } - protected String getConfigPath() { - return "declare-parents-delegate-ref-tests.xml"; - } - - + @Test public void testIntroductionWasMade() { - assertTrue("Introduction must have been made", noMethodsBean instanceof Counter); + assertTrue("Introduction must have been made", noMethodsBean instanceof ICounter); } + @Test public void testIntroductionDelegation() { - ((Counter)noMethodsBean).increment(); - assertEquals("Delegate's counter should be updated", 1, counter.count); - } - - public static interface NoMethodsBean { - } - - public static class NoMethodsBeanImpl implements NoMethodsBean { - } - - public static interface Counter { - public void increment(); - } - - - public static class CounterImpl implements Counter { - - int count; - - public void increment() { - count++; - } - - public void reset() { - count = 0; - } + ((ICounter)noMethodsBean).increment(); + assertEquals("Delegate's counter should be updated", 1, counter.getCount()); } } + + +interface NoMethodsBean { +} + + +class NoMethodsBeanImpl implements NoMethodsBean { +} + diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/DeclareParentsTests.java b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/DeclareParentsTests.java index 0eb09b31ec..98ed62d2ad 100644 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/DeclareParentsTests.java +++ b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/DeclareParentsTests.java @@ -16,18 +16,46 @@ package org.springframework.aop.aspectj; +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; +import org.springframework.aop.framework.Advised; import org.springframework.aop.framework.Lockable; import org.springframework.aop.support.AopUtils; +import org.springframework.beans.ITestBean; +import org.springframework.beans.TestBean; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @author Rod Johnson + * @author Chris Beams */ -public class DeclareParentsTests extends AbstractAdviceBindingTests { +public final class DeclareParentsTests { + + private ITestBean testBeanProxy; + + private TestBean testBeanTarget; + + private ApplicationContext ctx; protected String getConfigPath() { return "declare-parents-tests.xml"; } + @Before + public void setUp() throws Exception { + ctx = new ClassPathXmlApplicationContext(getConfigPath(), getClass()); + + testBeanProxy = (ITestBean) ctx.getBean("testBean"); + assertTrue(AopUtils.isAopProxy(testBeanProxy)); + + // we need the real target too, not just the proxy... + testBeanTarget = (TestBean) ((Advised) testBeanProxy).getTargetSource().getTarget(); + } + + @Test public void testIntroductionWasMade() { assertTrue("Introduction must have been made", testBeanProxy instanceof Lockable); } @@ -36,8 +64,9 @@ public class DeclareParentsTests extends AbstractAdviceBindingTests { // to org.springframework..* it also matches introduction. // Perhaps generated advisor bean definition could be made to depend // on the introduction, in which case this would not be a problem. + @Test public void testLockingWorks() { - Object introductionObject = applicationContext.getBean("introduction"); + Object introductionObject = ctx.getBean("introduction"); assertFalse("Introduction should not be proxied", AopUtils.isAopProxy(introductionObject)); Lockable lockable = (Lockable) testBeanProxy; @@ -58,3 +87,14 @@ public class DeclareParentsTests extends AbstractAdviceBindingTests { } } + + +class NonAnnotatedMakeLockable { + + public void checkNotLocked(Lockable mixin) { + if (mixin.locked()) { + throw new IllegalStateException("locked"); + } + } + +} \ No newline at end of file diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/ICounter.java b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/ICounter.java index 4c35daf9d8..e554acfe69 100644 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/ICounter.java +++ b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/ICounter.java @@ -19,7 +19,7 @@ package org.springframework.aop.aspectj; /** * @author Ramnivas Laddad */ -public interface ICounter { +interface ICounter { void increment(); diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/ImplicitJPArgumentMatchingAtAspectJTests.java b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/ImplicitJPArgumentMatchingAtAspectJTests.java index d030b294a5..f58dd722eb 100644 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/ImplicitJPArgumentMatchingAtAspectJTests.java +++ b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/ImplicitJPArgumentMatchingAtAspectJTests.java @@ -18,33 +18,27 @@ package org.springframework.aop.aspectj; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; +import org.junit.Test; import org.springframework.beans.TestBean; -import org.springframework.test.AbstractDependencyInjectionSpringContextTests; +import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Tests to check if the first implicit join point argument is correctly processed. * See SPR-3723 for more details. * * @author Ramnivas Laddad + * @author Chris Beams */ -public class ImplicitJPArgumentMatchingAtAspectJTests extends AbstractDependencyInjectionSpringContextTests { - protected TestBean testBean; +public final class ImplicitJPArgumentMatchingAtAspectJTests { - public ImplicitJPArgumentMatchingAtAspectJTests() { - setPopulateProtectedVariables(true); - } - - protected String getConfigPath() { - return "implicit-jp-argument-matching-atAspectJ-tests.xml"; - } - + @Test public void testAspect() { // nothing to really test; it is enough if we don't get error while creating app context - testBean.setCountry("US"); + new ClassPathXmlApplicationContext("implicit-jp-argument-matching-atAspectJ-tests.xml", getClass()); } @Aspect - public static class CounterAtAspectJAspect { + static class CounterAtAspectJAspect { @Around(value="execution(* org.springframework.beans.TestBean.*(..)) and this(bean) and args(argument)", argNames="bean,argument") public void increment(ProceedingJoinPoint pjp, TestBean bean, Object argument) throws Throwable { diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/ImplicitJPArgumentMatchingTests.java b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/ImplicitJPArgumentMatchingTests.java index 20662199d3..8e8ab3f93f 100644 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/ImplicitJPArgumentMatchingTests.java +++ b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/ImplicitJPArgumentMatchingTests.java @@ -16,32 +16,25 @@ package org.springframework.aop.aspectj; import org.aspectj.lang.ProceedingJoinPoint; -import org.springframework.beans.TestBean; -import org.springframework.test.AbstractDependencyInjectionSpringContextTests; +import org.junit.Test; +import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Tests to check if the first implicit join point argument is correctly processed. * See SPR-3723 for more details. * * @author Ramnivas Laddad + * @author Chris Beams */ -public class ImplicitJPArgumentMatchingTests extends AbstractDependencyInjectionSpringContextTests { - protected TestBean testBean; +public final class ImplicitJPArgumentMatchingTests { - public ImplicitJPArgumentMatchingTests() { - setPopulateProtectedVariables(true); - } - - protected String getConfigPath() { - return "implicit-jp-argument-matching-tests.xml"; - } - + @Test public void testAspect() { // nothing to really test; it is enough if we don't get error while creating app context - testBean.setCountry("US"); + new ClassPathXmlApplicationContext("implicit-jp-argument-matching-tests.xml", getClass()); } - public static class CounterAspect { + static class CounterAspect { public void increment(ProceedingJoinPoint pjp, Object bean, Object argument) throws Throwable { pjp.proceed(); } diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/JoinPointMonitorAspect.java b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/JoinPointMonitorAspect.java deleted file mode 100644 index 0b23dbd074..0000000000 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/JoinPointMonitorAspect.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 2002-2006 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.aspectj; - -import org.aspectj.lang.ProceedingJoinPoint; - -/** - * @author Ramnivas Laddad - */ -public class JoinPointMonitorAspect { - - /** - * The counter property is purposefully not used in the aspect to avoid distraction - * from the main bug -- merely needing a dependency on an advised bean - * is sufficient to reproduce the bug. - */ - private ICounter counter; - - int beforeExecutions; - int aroundExecutions; - - public void before() { - beforeExecutions++; - } - - public Object around(ProceedingJoinPoint pjp) throws Throwable { - aroundExecutions++; - return pjp.proceed(); - } - - public ICounter getCounter() { - return counter; - } - - public void setCounter(ICounter counter) { - this.counter = counter; - } - -} diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/JoinPointMonitorAtAspectJAspect.java b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/JoinPointMonitorAtAspectJAspect.java deleted file mode 100644 index bbb6b3338d..0000000000 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/JoinPointMonitorAtAspectJAspect.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright 2002-2006 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.aspectj; - -import org.aspectj.lang.ProceedingJoinPoint; -import org.aspectj.lang.annotation.Around; -import org.aspectj.lang.annotation.Aspect; -import org.aspectj.lang.annotation.Before; - -/** - * @author Ramnivas Laddad - * @since 2.0 - */ -@Aspect -public class JoinPointMonitorAtAspectJAspect { - /* The counter property is purposefully not used in the aspect to avoid distraction - * from the main bug -- merely needing a dependency on an advised bean - * is sufficient to reproduce the bug. - */ - private ICounter counter; - - int beforeExecutions; - int aroundExecutions; - - @Before("execution(* increment*())") - public void before() { - beforeExecutions++; - } - - @Around("execution(* increment*())") - public Object around(ProceedingJoinPoint pjp) throws Throwable { - aroundExecutions++; - return pjp.proceed(); - } - - public ICounter getCounter() { - return counter; - } - - public void setCounter(ICounter counter) { - this.counter = counter; - } - -} diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/NonAnnotatedMakeLockable.java b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/NonAnnotatedMakeLockable.java deleted file mode 100644 index e608d4e8d2..0000000000 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/NonAnnotatedMakeLockable.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 2002-2007 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.aop.aspectj; - -import org.springframework.aop.framework.Lockable; - -/** - * @author Rod Johnson - */ -public class NonAnnotatedMakeLockable { - - public void checkNotLocked(Lockable mixin) { - if (mixin.locked()) { - throw new IllegalStateException("locked"); - } - } - -} diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/OverloadedAdviceTestAspect.java b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/OverloadedAdviceTestAspect.java deleted file mode 100644 index 196a6bc3ae..0000000000 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/OverloadedAdviceTestAspect.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 2002-2006 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.aspectj; - - -/** - * Aspect used as part of overloaded advice tests. - * @author Adrian Colyer - */ -public class OverloadedAdviceTestAspect { - - public void myBeforeAdvice(String name) { - // no-op - } - - public void myBeforeAdvice(int age) { - // no-op - } -} diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/OverloadedAdviceTests.java b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/OverloadedAdviceTests.java index 3259cebae3..1bf4330397 100644 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/OverloadedAdviceTests.java +++ b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/OverloadedAdviceTests.java @@ -28,12 +28,12 @@ import org.springframework.context.support.ClassPathXmlApplicationContext; * @author Adrian Colyer * @author Chris Beams */ -public class OverloadedAdviceTests { +public final class OverloadedAdviceTests { @Test public void testExceptionOnConfigParsingWithMismatchedAdviceMethod() { try { - new ClassPathXmlApplicationContext("org/springframework/aop/aspectj/overloaded-advice-tests.xml"); + new ClassPathXmlApplicationContext("overloaded-advice-tests.xml", getClass()); } catch (BeanCreationException ex) { Throwable cause = ex.getRootCause(); @@ -46,7 +46,7 @@ public class OverloadedAdviceTests { @Test public void testExceptionOnConfigParsingWithAmbiguousAdviceMethod() { try { - new ClassPathXmlApplicationContext("org/springframework/aop/aspectj/ambiguous-advice-tests.xml"); + new ClassPathXmlApplicationContext("ambiguous-advice-tests.xml", getClass()); } catch (BeanCreationException ex) { Throwable cause = ex.getRootCause(); @@ -57,3 +57,16 @@ public class OverloadedAdviceTests { } } + + +class OverloadedAdviceTestAspect { + + public void myBeforeAdvice(String name) { + // no-op + } + + public void myBeforeAdvice(int age) { + // no-op + } +} + diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/PrecedenceTestAspect.java b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/PrecedenceTestAspect.java deleted file mode 100644 index bf0b092bc6..0000000000 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/PrecedenceTestAspect.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Copyright 2002-2006 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.aspectj; - -import org.aspectj.lang.ProceedingJoinPoint; - -import org.springframework.beans.factory.BeanNameAware; -import org.springframework.core.Ordered; - -/** - * Used in advice precedence tests (surprise!) - * - * @author Adrian Colyer - */ -public class PrecedenceTestAspect implements BeanNameAware, Ordered { - - private String name; - - private int order = Ordered.LOWEST_PRECEDENCE; - - private Collaborator collaborator; - - - public void setBeanName(String name) { - this.name = name; - } - - public void setOrder(int order) { - this.order = order; - } - - public int getOrder() { - return order; - } - - public void setCollaborator(Collaborator collaborator) { - this.collaborator = collaborator; - } - - public void beforeAdviceOne() { - this.collaborator.beforeAdviceOne(this.name); - } - - public void beforeAdviceTwo() { - this.collaborator.beforeAdviceTwo(this.name); - } - - public int aroundAdviceOne(ProceedingJoinPoint pjp) { - int ret = -1; - this.collaborator.aroundAdviceOne(this.name); - try { - ret = ((Integer)pjp.proceed()).intValue(); - } - catch(Throwable t) { throw new RuntimeException(t); } - this.collaborator.aroundAdviceOne(this.name); - return ret; - } - - public int aroundAdviceTwo(ProceedingJoinPoint pjp) { - int ret = -1; - this.collaborator.aroundAdviceTwo(this.name); - try { - ret = ((Integer)pjp.proceed()).intValue(); - } - catch(Throwable t) {throw new RuntimeException(t);} - this.collaborator.aroundAdviceTwo(this.name); - return ret; - } - - public void afterAdviceOne() { - this.collaborator.afterAdviceOne(this.name); - } - - public void afterAdviceTwo() { - this.collaborator.afterAdviceTwo(this.name); - } - - - public interface Collaborator { - - void beforeAdviceOne(String beanName); - void beforeAdviceTwo(String beanName); - void aroundAdviceOne(String beanName); - void aroundAdviceTwo(String beanName); - void afterAdviceOne(String beanName); - void afterAdviceTwo(String beanName); - } - -} diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/ProceedTests.java b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/ProceedTests.java index b882dcc83f..f95d6f75d1 100644 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/ProceedTests.java +++ b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/ProceedTests.java @@ -16,70 +16,59 @@ package org.springframework.aop.aspectj; +import static org.junit.Assert.*; + import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; - -import org.springframework.core.JdkVersion; +import org.junit.Before; +import org.junit.Test; +import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.core.Ordered; -import org.springframework.test.AbstractDependencyInjectionSpringContextTests; /** * Test for SPR-3522. Arguments changed on a call to proceed should be * visible to advice further down the invocation chain. * * @author Adrian Colyer + * @author Chris Beams */ -public class ProceedTests extends AbstractDependencyInjectionSpringContextTests { +public final class ProceedTests { private SimpleBean testBean; - private ProceedTestingAspect testAspect; + private ProceedTestingAspect firstTestAspect; private ProceedTestingAspect secondTestAspect; - public ProceedTests() { - setAutowireMode(AUTOWIRE_BY_NAME); + @Before + public void setUp() { + ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("proceedTests.xml", getClass()); + testBean = (SimpleBean) ctx.getBean("testBean"); + firstTestAspect = (ProceedTestingAspect) ctx.getBean("firstTestAspect"); + secondTestAspect = (ProceedTestingAspect) ctx.getBean("secondTestAspect"); } - protected String[] getConfigLocations() { - return new String[] {"org/springframework/aop/aspectj/proceedTests.xml"}; - } - - public void setFirstTestAspect(ProceedTestingAspect anAspect) { - this.testAspect = anAspect; - } - - public void setSecondTestAspect(ProceedTestingAspect anAspect) { - this.secondTestAspect = anAspect; - } - - public void setTestBean(SimpleBean aBean) { - this.testBean = aBean; - } - - + @Test public void testSimpleProceedWithChangedArgs() { this.testBean.setName("abc"); assertEquals("Name changed in around advice", "ABC", this.testBean.getName()); } + @Test public void testGetArgsIsDefensive() { this.testBean.setAge(5); assertEquals("getArgs is defensive", 5, this.testBean.getAge()); } + @Test public void testProceedWithArgsInSameAspect() { - if (!JdkVersion.isAtLeastJava15()) { - // Doesn't work on JDK 1.4 for some reason... - return; - } - this.testBean.setMyFloat(1.0F); assertTrue("value changed in around advice", this.testBean.getMyFloat() > 1.9F); - assertTrue("changed value visible to next advice in chain", this.testAspect.getLastBeforeFloatValue() > 1.9F); + assertTrue("changed value visible to next advice in chain", this.firstTestAspect.getLastBeforeFloatValue() > 1.9F); } + @Test public void testProceedWithArgsAcrossAspects() { this.testBean.setSex("male"); assertEquals("value changed in around advice","MALE", this.testBean.getSex()); @@ -88,127 +77,129 @@ public class ProceedTests extends AbstractDependencyInjectionSpringContextTests } - public interface SimpleBean { - - public void setName(String name); - public String getName(); - public void setAge(int age); - public int getAge(); - public void setMyFloat(float f); - public float getMyFloat(); - public void setSex(String sex); - public String getSex(); - } - - - public static class SimpleBeanImpl implements SimpleBean { - - private int age; - private float aFloat; - private String name; - private String sex; - - public int getAge() { - return age; - } - - public float getMyFloat() { - return aFloat; - } - - public String getName() { - return name; - } - - public String getSex() { - return sex; - } - - public void setAge(int age) { - this.age = age; - } - - public void setMyFloat(float f) { - this.aFloat = f; - } - - public void setName(String name) { - this.name = name; - } - - public void setSex(String sex) { - this.sex = sex; - } - } - - - public static class ProceedTestingAspect implements Ordered { - - private String lastBeforeStringValue; - private String lastAroundStringValue; - private float lastBeforeFloatValue; - private int order; - - public void setOrder(int order) { this.order = order; } - public int getOrder() { return this.order; } - - public Object capitalize(ProceedingJoinPoint pjp, String value) throws Throwable { - return pjp.proceed(new Object[] {value.toUpperCase()}); - } - - public Object doubleOrQuits(ProceedingJoinPoint pjp) throws Throwable { - int value = ((Integer) pjp.getArgs()[0]).intValue(); - pjp.getArgs()[0] = new Integer(value * 2); - return pjp.proceed(); - } - - public Object addOne(ProceedingJoinPoint pjp, Float value) throws Throwable { - float fv = value.floatValue(); - return pjp.proceed(new Object[] {new Float(fv + 1.0F)}); - } - - public void captureStringArgument(JoinPoint tjp, String arg) { - if (!tjp.getArgs()[0].equals(arg)) { - throw new IllegalStateException( - "argument is '" + arg + "', " + - "but args array has '" + tjp.getArgs()[0] + "'" - ); - } - this.lastBeforeStringValue = arg; - } - - public Object captureStringArgumentInAround(ProceedingJoinPoint pjp, String arg) throws Throwable { - if (!pjp.getArgs()[0].equals(arg)) { - throw new IllegalStateException( - "argument is '" + arg + "', " + - "but args array has '" + pjp.getArgs()[0] + "'"); - } - this.lastAroundStringValue = arg; - return pjp.proceed(); - } - - public void captureFloatArgument(JoinPoint tjp, float arg) { - float tjpArg = ((Float) tjp.getArgs()[0]).floatValue(); - if (Math.abs(tjpArg - arg) > 0.000001) { - throw new IllegalStateException( - "argument is '" + arg + "', " + - "but args array has '" + tjpArg + "'" - ); - } - this.lastBeforeFloatValue = arg; - } - - public String getLastBeforeStringValue() { - return this.lastBeforeStringValue; - } - - public String getLastAroundStringValue() { - return this.lastAroundStringValue; - } - - public float getLastBeforeFloatValue() { - return this.lastBeforeFloatValue; - } - } - } + + +interface SimpleBean { + + void setName(String name); + String getName(); + void setAge(int age); + int getAge(); + void setMyFloat(float f); + float getMyFloat(); + void setSex(String sex); + String getSex(); +} + + +class SimpleBeanImpl implements SimpleBean { + + private int age; + private float aFloat; + private String name; + private String sex; + + public int getAge() { + return age; + } + + public float getMyFloat() { + return aFloat; + } + + public String getName() { + return name; + } + + public String getSex() { + return sex; + } + + public void setAge(int age) { + this.age = age; + } + + public void setMyFloat(float f) { + this.aFloat = f; + } + + public void setName(String name) { + this.name = name; + } + + public void setSex(String sex) { + this.sex = sex; + } +} + + +class ProceedTestingAspect implements Ordered { + + private String lastBeforeStringValue; + private String lastAroundStringValue; + private float lastBeforeFloatValue; + private int order; + + public void setOrder(int order) { this.order = order; } + public int getOrder() { return this.order; } + + public Object capitalize(ProceedingJoinPoint pjp, String value) throws Throwable { + return pjp.proceed(new Object[] {value.toUpperCase()}); + } + + public Object doubleOrQuits(ProceedingJoinPoint pjp) throws Throwable { + int value = ((Integer) pjp.getArgs()[0]).intValue(); + pjp.getArgs()[0] = new Integer(value * 2); + return pjp.proceed(); + } + + public Object addOne(ProceedingJoinPoint pjp, Float value) throws Throwable { + float fv = value.floatValue(); + return pjp.proceed(new Object[] {new Float(fv + 1.0F)}); + } + + public void captureStringArgument(JoinPoint tjp, String arg) { + if (!tjp.getArgs()[0].equals(arg)) { + throw new IllegalStateException( + "argument is '" + arg + "', " + + "but args array has '" + tjp.getArgs()[0] + "'" + ); + } + this.lastBeforeStringValue = arg; + } + + public Object captureStringArgumentInAround(ProceedingJoinPoint pjp, String arg) throws Throwable { + if (!pjp.getArgs()[0].equals(arg)) { + throw new IllegalStateException( + "argument is '" + arg + "', " + + "but args array has '" + pjp.getArgs()[0] + "'"); + } + this.lastAroundStringValue = arg; + return pjp.proceed(); + } + + public void captureFloatArgument(JoinPoint tjp, float arg) { + float tjpArg = ((Float) tjp.getArgs()[0]).floatValue(); + if (Math.abs(tjpArg - arg) > 0.000001) { + throw new IllegalStateException( + "argument is '" + arg + "', " + + "but args array has '" + tjpArg + "'" + ); + } + this.lastBeforeFloatValue = arg; + } + + public String getLastBeforeStringValue() { + return this.lastBeforeStringValue; + } + + public String getLastAroundStringValue() { + return this.lastAroundStringValue; + } + + public float getLastBeforeFloatValue() { + return this.lastBeforeFloatValue; + } +} + diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/PropertyDependentAspectTests.java b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/PropertyDependentAspectTests.java index 35d2fa3213..eb47909dec 100644 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/PropertyDependentAspectTests.java +++ b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/PropertyDependentAspectTests.java @@ -18,6 +18,10 @@ package org.springframework.aop.aspectj; import static org.junit.Assert.*; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; import org.junit.Test; import org.springframework.aop.framework.Advised; import org.springframework.context.ApplicationContext; @@ -31,7 +35,7 @@ import org.springframework.context.support.ClassPathXmlApplicationContext; * @author Juergen Hoeller * @author Chris Beams */ -public class PropertyDependentAspectTests { +public final class PropertyDependentAspectTests { @Test public void testPropertyDependentAspectWithPropertyDeclaredBeforeAdvice() throws Exception { @@ -76,3 +80,68 @@ public class PropertyDependentAspectTests { } } + + +class JoinPointMonitorAspect { + + /** + * The counter property is purposefully not used in the aspect to avoid distraction + * from the main bug -- merely needing a dependency on an advised bean + * is sufficient to reproduce the bug. + */ + private ICounter counter; + + int beforeExecutions; + int aroundExecutions; + + public void before() { + beforeExecutions++; + } + + public Object around(ProceedingJoinPoint pjp) throws Throwable { + aroundExecutions++; + return pjp.proceed(); + } + + public ICounter getCounter() { + return counter; + } + + public void setCounter(ICounter counter) { + this.counter = counter; + } + +} + + +@Aspect +class JoinPointMonitorAtAspectJAspect { + /* The counter property is purposefully not used in the aspect to avoid distraction + * from the main bug -- merely needing a dependency on an advised bean + * is sufficient to reproduce the bug. + */ + private ICounter counter; + + int beforeExecutions; + int aroundExecutions; + + @Before("execution(* increment*())") + public void before() { + beforeExecutions++; + } + + @Around("execution(* increment*())") + public Object around(ProceedingJoinPoint pjp) throws Throwable { + aroundExecutions++; + return pjp.proceed(); + } + + public ICounter getCounter() { + return counter; + } + + public void setCounter(ICounter counter) { + this.counter = counter; + } + +} \ No newline at end of file diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/SerializableMixin.java b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/SerializableMixin.java deleted file mode 100644 index 2046942aa4..0000000000 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/SerializableMixin.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright 2002-2006 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. - * - * Created on 15 Nov 2006 by Adrian Colyer - */ -package org.springframework.aop.aspectj; - -import java.io.Serializable; - -/** - * @author Adrian Colyer - * @since 2.0 - */ -public class SerializableMixin implements Serializable { - -} diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/SharedPointcutWithArgsMismatch.java b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/SharedPointcutWithArgsMismatchTests.java similarity index 58% rename from org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/SharedPointcutWithArgsMismatch.java rename to org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/SharedPointcutWithArgsMismatchTests.java index 9083ebe1df..77f26b8332 100644 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/SharedPointcutWithArgsMismatch.java +++ b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/SharedPointcutWithArgsMismatchTests.java @@ -16,49 +16,52 @@ package org.springframework.aop.aspectj; -import org.springframework.test.AbstractDependencyInjectionSpringContextTests; +import org.junit.Before; +import org.junit.Test; +import org.springframework.context.support.ClassPathXmlApplicationContext; /** * See SPR-1682. * * @author Adrian Colyer + * @author Chris Beams */ -public class SharedPointcutWithArgsMismatch extends AbstractDependencyInjectionSpringContextTests { +public final class SharedPointcutWithArgsMismatchTests { private ToBeAdvised toBeAdvised; - - public void setToBeAdvised(ToBeAdvised tba) { - this.toBeAdvised = tba; + + @Before + public void setUp() { + ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("args-mismatch.xml", getClass()); + toBeAdvised = (ToBeAdvised) ctx.getBean("toBeAdvised"); } - protected String getConfigPath() { - return "args-mismatch.xml"; - } - - + @Test public void testMismatchedArgBinding() { this.toBeAdvised.foo("Hello"); } - public static class ToBeAdvised { - - public void foo(String s) { - System.out.println(s); - } - } - - - public static class MyAspect { - - public void doBefore(int x) { - System.out.println(x); - } - - public void doBefore(String x) { - System.out.println(x); - } - } - } + + +class ToBeAdvised { + + public void foo(String s) { + System.out.println(s); + } +} + + +class MyAspect { + + public void doBefore(int x) { + System.out.println(x); + } + + public void doBefore(String x) { + System.out.println(x); + } +} + diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/SimpleSpringBeforeAdvice.java b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/SimpleSpringBeforeAdvice.java deleted file mode 100644 index 77a3ca45ef..0000000000 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/SimpleSpringBeforeAdvice.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright 2002-2006 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. - * - * Created on 14-Feb-2006 by Adrian Colyer - */ -package org.springframework.aop.aspectj; - -import java.lang.reflect.Method; - -import org.springframework.aop.MethodBeforeAdvice; -import org.springframework.beans.factory.BeanNameAware; - -/** - * Used as part of aspect precedence tests - * - * @author Adrian Colyer - * @since 2.0 - */ -public class SimpleSpringBeforeAdvice implements MethodBeforeAdvice, BeanNameAware { - - private PrecedenceTestAspect.Collaborator collaborator; - private String name; - - /* (non-Javadoc) - * @see org.springframework.aop.MethodBeforeAdvice#before(java.lang.reflect.Method, java.lang.Object[], java.lang.Object) - */ - public void before(Method method, Object[] args, Object target) - throws Throwable { - this.collaborator.beforeAdviceOne(this.name); - } - - public void setCollaborator(PrecedenceTestAspect.Collaborator collaborator) { - this.collaborator = collaborator; - } - - /* (non-Javadoc) - * @see org.springframework.beans.factory.BeanNameAware#setBeanName(java.lang.String) - */ - public void setBeanName(String name) { - this.name = name; - } - -} diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/SubtypeMatchingTestClasses.java b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/SubtypeMatchingTestClasses.java deleted file mode 100644 index ce5a77189f..0000000000 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/SubtypeMatchingTestClasses.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright 2002-2006 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.aspectj; - -import java.io.Serializable; - - -// strange looking interfaces are just to set up certain test conditions... -interface NonSerializableFoo { void foo(); } -interface SerializableFoo extends Serializable { void foo(); } - -class SubtypeMatchingTestClassA implements NonSerializableFoo { - - public void foo() {} - -} - -class SubtypeMatchingTestClassB implements SerializableFoo { - - public void foo() {} - -} - -interface Bar { void bar(Object o); } - -class SubtypeMatchingTestClassC implements Bar { - - public void bar(Object o) {} - -} - diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/SubtypeSensitiveMatchingTests.java b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/SubtypeSensitiveMatchingTests.java index 13dff54d61..45a310dbac 100644 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/SubtypeSensitiveMatchingTests.java +++ b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/SubtypeSensitiveMatchingTests.java @@ -16,13 +16,20 @@ package org.springframework.aop.aspectj; +import static org.junit.Assert.*; + +import java.io.Serializable; + +import org.junit.Before; +import org.junit.Test; import org.springframework.aop.framework.Advised; -import org.springframework.test.AbstractDependencyInjectionSpringContextTests; +import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @author Adrian Colyer + * @author Chris Beams */ -public class SubtypeSensitiveMatchingTests extends AbstractDependencyInjectionSpringContextTests { +public final class SubtypeSensitiveMatchingTests { private NonSerializableFoo nonSerializableBean; @@ -31,36 +38,56 @@ public class SubtypeSensitiveMatchingTests extends AbstractDependencyInjectionSp private Bar bar; - public void setNonSerializableFoo(NonSerializableFoo aBean) { - this.nonSerializableBean = aBean; + @Before + public void setUp() { + ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("subtype-sensitive-matching.xml", getClass()); + nonSerializableBean = (NonSerializableFoo) ctx.getBean("testClassA"); + serializableBean = (SerializableFoo) ctx.getBean("testClassB"); + bar = (Bar) ctx.getBean("testClassC"); } - public void setSerializableFoo(SerializableFoo aBean) { - this.serializableBean = aBean; - } - - public void setBar(Bar aBean) { - this.bar = aBean; - } - - protected String getConfigPath() { - return "subtype-sensitive-matching.xml"; - } - - + @Test public void testBeansAreProxiedOnStaticMatch() { assertTrue("bean with serializable type should be proxied", this.serializableBean instanceof Advised); } + @Test public void testBeansThatDoNotMatchBasedSolelyOnRuntimeTypeAreNotProxied() { assertFalse("bean with non-serializable type should not be proxied", this.nonSerializableBean instanceof Advised); } + @Test public void testBeansThatDoNotMatchBasedOnOtherTestAreProxied() { assertTrue("bean with args check should be proxied", this.bar instanceof Advised); } } + +//strange looking interfaces are just to set up certain test conditions... +interface NonSerializableFoo { void foo(); } + +interface SerializableFoo extends Serializable { void foo(); } + +class SubtypeMatchingTestClassA implements NonSerializableFoo { + + public void foo() {} + +} + +@SuppressWarnings("serial") +class SubtypeMatchingTestClassB implements SerializableFoo { + + public void foo() {} + +} + +interface Bar { void bar(Object o); } + +class SubtypeMatchingTestClassC implements Bar { + + public void bar(Object o) {} + +} \ No newline at end of file diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/TargetPointcutSelectionTests.java b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/TargetPointcutSelectionTests.java index 1146b777f6..af6b711bd2 100644 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/TargetPointcutSelectionTests.java +++ b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/TargetPointcutSelectionTests.java @@ -16,42 +16,45 @@ package org.springframework.aop.aspectj; +import static org.junit.Assert.assertEquals; + import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; - -import org.springframework.test.AbstractDependencyInjectionSpringContextTests; +import org.junit.Before; +import org.junit.Test; +import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Tests for target selection matching (see SPR-3783). * Thanks to Tomasz Blachowicz for the bug report! * * @author Ramnivas Laddad + * @author Chris Beams */ -public class TargetPointcutSelectionTests extends AbstractDependencyInjectionSpringContextTests { +public final class TargetPointcutSelectionTests { - protected TestInterface testImpl1; - protected TestInterface testImpl2; - protected TestAspect testAspectForTestImpl1; - protected TestAspect testAspectForAbstractTestImpl; - protected TestInterceptor testInterceptor; + public TestInterface testImpl1; + public TestInterface testImpl2; + public TestAspect testAspectForTestImpl1; + public TestAspect testAspectForAbstractTestImpl; + public TestInterceptor testInterceptor; - public TargetPointcutSelectionTests() { - setPopulateProtectedVariables(true); - } - - protected String getConfigPath() { - return "targetPointcutSelectionTests.xml"; - } - - protected void onSetUp() throws Exception { + @Before + public void setUp() { + ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("targetPointcutSelectionTests.xml", getClass()); + testImpl1 = (TestInterface) ctx.getBean("testImpl1"); + testImpl2 = (TestInterface) ctx.getBean("testImpl2"); + testAspectForTestImpl1 = (TestAspect) ctx.getBean("testAspectForTestImpl1"); + testAspectForAbstractTestImpl = (TestAspect) ctx.getBean("testAspectForAbstractTestImpl"); + testInterceptor = (TestInterceptor) ctx.getBean("testInterceptor"); + testAspectForTestImpl1.count = 0; testAspectForAbstractTestImpl.count = 0; testInterceptor.count = 0; - super.onSetUp(); } - - + + @Test public void testTargetSelectionForMatchedType() { testImpl1.interfaceMethod(); assertEquals("Should have been advised by POJO advice for impl", 1, testAspectForTestImpl1.count); @@ -59,6 +62,7 @@ public class TargetPointcutSelectionTests extends AbstractDependencyInjectionSpr assertEquals("Should have been advised by advisor", 1, testInterceptor.count); } + @Test public void testTargetNonSelectionForMismatchedType() { testImpl2.interfaceMethod(); assertEquals("Shouldn't have been advised by POJO advice for impl", 0, testAspectForTestImpl1.count); diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/ThisAndTargetSelectionOnlyPointcutsAtAspectJTests.java b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/ThisAndTargetSelectionOnlyPointcutsAtAspectJTests.java index ad19554bac..90cc66e5c4 100644 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/ThisAndTargetSelectionOnlyPointcutsAtAspectJTests.java +++ b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/ThisAndTargetSelectionOnlyPointcutsAtAspectJTests.java @@ -16,78 +16,87 @@ package org.springframework.aop.aspectj; +import static org.junit.Assert.assertEquals; + import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; -import org.springframework.test.AbstractDependencyInjectionSpringContextTests; +import org.junit.Test; +import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @author Ramnivas Laddad + * @author Chris Beams */ -public class ThisAndTargetSelectionOnlyPointcutsAtAspectJTests extends AbstractDependencyInjectionSpringContextTests { - protected TestInterface testBean; - protected TestInterface testAnnotatedClassBean; - protected TestInterface testAnnotatedMethodBean; +public final class ThisAndTargetSelectionOnlyPointcutsAtAspectJTests { + public TestInterface testBean; + public TestInterface testAnnotatedClassBean; + public TestInterface testAnnotatedMethodBean; protected Counter counter; - public ThisAndTargetSelectionOnlyPointcutsAtAspectJTests() { - setPopulateProtectedVariables(true); - } - - @Override - protected void onSetUp() throws Exception { - super.onSetUp(); + @org.junit.Before + public void setUp() { + ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("this-and-target-selectionOnly-pointcuts-atAspectJ-tests.xml", getClass()); + testBean = (TestInterface) ctx.getBean("testBean"); + testAnnotatedClassBean = (TestInterface) ctx.getBean("testAnnotatedClassBean"); + testAnnotatedMethodBean = (TestInterface) ctx.getBean("testAnnotatedMethodBean"); + counter = (Counter) ctx.getBean("counter"); counter.reset(); } - protected String getConfigPath() { - return "this-and-target-selectionOnly-pointcuts-atAspectJ-tests.xml"; - } - + @Test public void testThisAsClassDoesNotMatch() { testBean.doIt(); assertEquals(0, counter.thisAsClassCounter); } + @Test public void testThisAsInterfaceMatch() { testBean.doIt(); assertEquals(1, counter.thisAsInterfaceCounter); } + @Test public void testTargetAsClassDoesMatch() { testBean.doIt(); assertEquals(1, counter.targetAsClassCounter); } + @Test public void testTargetAsInterfaceMatch() { testBean.doIt(); assertEquals(1, counter.targetAsInterfaceCounter); } + @Test public void testThisAsClassAndTargetAsClassCounterNotMatch() { testBean.doIt(); assertEquals(0, counter.thisAsClassAndTargetAsClassCounter); } + @Test public void testThisAsInterfaceAndTargetAsInterfaceCounterMatch() { testBean.doIt(); assertEquals(1, counter.thisAsInterfaceAndTargetAsInterfaceCounter); } + @Test public void testThisAsInterfaceAndTargetAsClassCounterMatch() { testBean.doIt(); assertEquals(1, counter.thisAsInterfaceAndTargetAsInterfaceCounter); } + @Test public void testAtTargetClassAnnotationMatch() { testAnnotatedClassBean.doIt(); assertEquals(1, counter.atTargetClassAnnotationCounter); } + @Test public void testAtAnnotationMethodAnnotationMatch() { testAnnotatedMethodBean.doIt(); assertEquals(1, counter.atAnnotationMethodAnnotationCounter); diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/ThisAndTargetSelectionOnlyPointcutsTests.java b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/ThisAndTargetSelectionOnlyPointcutsTests.java index f2b470e738..b0a3b12ce1 100644 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/ThisAndTargetSelectionOnlyPointcutsTests.java +++ b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/ThisAndTargetSelectionOnlyPointcutsTests.java @@ -16,28 +16,43 @@ package org.springframework.aop.aspectj; -import org.springframework.test.AbstractDependencyInjectionSpringContextTests; +import static org.junit.Assert.assertEquals; + +import org.junit.Before; +import org.junit.Test; +import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @author Ramnivas Laddad + * @author Chris Beams */ -public class ThisAndTargetSelectionOnlyPointcutsTests extends AbstractDependencyInjectionSpringContextTests { - protected TestInterface testBean; +public final class ThisAndTargetSelectionOnlyPointcutsTests { - protected Counter thisAsClassCounter; - protected Counter thisAsInterfaceCounter; - protected Counter targetAsClassCounter; - protected Counter targetAsInterfaceCounter; - protected Counter thisAsClassAndTargetAsClassCounter; - protected Counter thisAsInterfaceAndTargetAsInterfaceCounter; - protected Counter thisAsInterfaceAndTargetAsClassCounter; + private TestInterface testBean; - public ThisAndTargetSelectionOnlyPointcutsTests() { - setPopulateProtectedVariables(true); - } + private Counter thisAsClassCounter; + private Counter thisAsInterfaceCounter; + private Counter targetAsClassCounter; + private Counter targetAsInterfaceCounter; + private Counter thisAsClassAndTargetAsClassCounter; + private Counter thisAsInterfaceAndTargetAsInterfaceCounter; + private Counter thisAsInterfaceAndTargetAsClassCounter; - protected void onSetUp() throws Exception { - super.onSetUp(); + @Before + public void setUp() { + ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("this-and-target-selectionOnly-pointcuts-tests.xml", getClass()); + + testBean = (TestInterface) ctx.getBean("testBean"); + + thisAsClassCounter = (Counter) ctx.getBean("thisAsClassCounter"); + thisAsInterfaceCounter = (Counter) ctx.getBean("thisAsInterfaceCounter"); + targetAsClassCounter = (Counter) ctx.getBean("targetAsClassCounter"); + targetAsInterfaceCounter = (Counter) ctx.getBean("targetAsInterfaceCounter"); + + thisAsClassAndTargetAsClassCounter = (Counter) ctx.getBean("thisAsClassAndTargetAsClassCounter"); + thisAsInterfaceAndTargetAsInterfaceCounter = (Counter) ctx.getBean("thisAsInterfaceAndTargetAsInterfaceCounter"); + thisAsInterfaceAndTargetAsClassCounter = (Counter) ctx.getBean("thisAsInterfaceAndTargetAsClassCounter"); + thisAsClassCounter.reset(); thisAsInterfaceCounter.reset(); targetAsClassCounter.reset(); @@ -48,52 +63,57 @@ public class ThisAndTargetSelectionOnlyPointcutsTests extends AbstractDependency thisAsInterfaceAndTargetAsClassCounter.reset(); } - protected String getConfigPath() { - return "this-and-target-selectionOnly-pointcuts-tests.xml"; - } - + @Test public void testThisAsClassDoesNotMatch() { testBean.doIt(); assertEquals(0, thisAsClassCounter.getCount()); } + @Test public void testThisAsInterfaceMatch() { testBean.doIt(); assertEquals(1, thisAsInterfaceCounter.getCount()); } + @Test public void testTargetAsClassDoesMatch() { testBean.doIt(); assertEquals(1, targetAsClassCounter.getCount()); } + @Test public void testTargetAsInterfaceMatch() { testBean.doIt(); assertEquals(1, targetAsInterfaceCounter.getCount()); } + @Test public void testThisAsClassAndTargetAsClassCounterNotMatch() { testBean.doIt(); assertEquals(0, thisAsClassAndTargetAsClassCounter.getCount()); } + @Test public void testThisAsInterfaceAndTargetAsInterfaceCounterMatch() { testBean.doIt(); assertEquals(1, thisAsInterfaceAndTargetAsInterfaceCounter.getCount()); } + @Test public void testThisAsInterfaceAndTargetAsClassCounterMatch() { testBean.doIt(); assertEquals(1, thisAsInterfaceAndTargetAsInterfaceCounter.getCount()); } - public static interface TestInterface { - public void doIt(); - } - - public static class TestImpl implements TestInterface { - public void doIt() { - } - } } + +interface TestInterface { + public void doIt(); +} + + +class TestImpl implements TestInterface { + public void doIt() { + } +} \ No newline at end of file diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/TopsyTurvyAspect.java b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/TopsyTurvyAspect.java deleted file mode 100644 index 489f762132..0000000000 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/TopsyTurvyAspect.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 2002-2006 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. - * - * Created on 15 Nov 2006 by Adrian Colyer - */ -package org.springframework.aop.aspectj; - -import org.aspectj.lang.ProceedingJoinPoint; - -/** - * @author Adrian Colyer - * @since 2.0 - */ -public class TopsyTurvyAspect { - - interface Collaborator { - void beforeAdviceFired(); - void afterReturningAdviceFired(); - void aroundAdviceFired(); - } - - private Collaborator collaborator; - - public void setCollaborator(Collaborator collaborator) { - this.collaborator = collaborator; - } - - public void before() { - this.collaborator.beforeAdviceFired(); - } - - public void afterReturning() { - this.collaborator.afterReturningAdviceFired(); - } - - public Object around(ProceedingJoinPoint pjp) throws Throwable { - Object ret = pjp.proceed(); - this.collaborator.aroundAdviceFired(); - return ret; - } -} diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/TopsyTurvyTarget.java b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/TopsyTurvyTarget.java deleted file mode 100644 index 3cb679c0de..0000000000 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/TopsyTurvyTarget.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright 2002-2006 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. - * - * Created on 15 Nov 2006 by Adrian Colyer - */ -package org.springframework.aop.aspectj; - -/** - * @author Adrian Colyer - * @since 2.0 - */ -public interface TopsyTurvyTarget { - - public abstract void doSomething(); - - public abstract int getX(); - -} \ No newline at end of file diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/TopsyTurvyTargetImpl.java b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/TopsyTurvyTargetImpl.java deleted file mode 100644 index bfebb397bb..0000000000 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/TopsyTurvyTargetImpl.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 2002-2006 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. - * - * Created on 15 Nov 2006 by Adrian Colyer - */ -package org.springframework.aop.aspectj; - -/** - * @author Adrian Colyer - * @since 2.0 - */ -public class TopsyTurvyTargetImpl implements TopsyTurvyTarget { - - private int x = 5; - - /* (non-Javadoc) - * @see org.springframework.aop.aspectj.TopsyTurvyTarget#doSomething() - */ - public void doSomething() { - this.x = 10; - } - - /* (non-Javadoc) - * @see org.springframework.aop.aspectj.TopsyTurvyTarget#getX() - */ - public int getX() { - return x; - } - -} diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/args-mismatch.xml b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/args-mismatch.xml index d82ac51900..1100db6b5a 100644 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/args-mismatch.xml +++ b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/args-mismatch.xml @@ -14,7 +14,7 @@ - + - + \ No newline at end of file diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/bean-name-pointcut-atAspect-tests.xml b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/bean-name-pointcut-atAspect-tests.xml index c08dd03453..987a8b852c 100644 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/bean-name-pointcut-atAspect-tests.xml +++ b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/bean-name-pointcut-atAspect-tests.xml @@ -11,5 +11,5 @@ - + \ No newline at end of file diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/declare-parents-delegate-ref-tests.xml b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/declare-parents-delegate-ref-tests.xml index 0c6ee0117b..ee552093f9 100644 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/declare-parents-delegate-ref-tests.xml +++ b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/declare-parents-delegate-ref-tests.xml @@ -8,15 +8,15 @@ - + - + diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/proceedTests.xml b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/proceedTests.xml index 2b4ca898e2..8bb019245e 100644 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/proceedTests.xml +++ b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/proceedTests.xml @@ -19,13 +19,13 @@ - + - + - + diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/this-and-target-selectionOnly-pointcuts-tests.xml b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/this-and-target-selectionOnly-pointcuts-tests.xml index acf51f5ccd..4d2cb3c4bb 100644 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/this-and-target-selectionOnly-pointcuts-tests.xml +++ b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/this-and-target-selectionOnly-pointcuts-tests.xml @@ -9,56 +9,48 @@ + pointcut="this(org.springframework.aop.aspectj.TestImpl)" /> + pointcut="this(org.springframework.aop.aspectj.TestInterface)" /> + pointcut="target(org.springframework.aop.aspectj.TestImpl)" /> + pointcut="target(org.springframework.aop.aspectj.TestInterface)" /> + pointcut="this(org.springframework.aop.aspectj.TestImpl) and target(org.springframework.aop.aspectj.TestImpl)" /> + pointcut="this(org.springframework.aop.aspectj.TestInterface) and target(org.springframework.aop.aspectj.TestInterface)" /> + pointcut="this(org.springframework.aop.aspectj.TestInterface) and target(org.springframework.aop.aspectj.TestImpl)" /> - - - - - - - - - + + + + + + + + \ No newline at end of file diff --git a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/topsy-turvy-aspect.xml b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/topsy-turvy-aspect.xml index f0d90523d9..f1d0bdc21c 100644 --- a/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/topsy-turvy-aspect.xml +++ b/org.springframework.testsuite/src/test/java/org/springframework/aop/aspectj/topsy-turvy-aspect.xml @@ -16,7 +16,7 @@ + default-impl="org.springframework.aop.aspectj.DeclarationOrderIndependenceTests$SerializableMixin"/> + default-impl="org.springframework.aop.aspectj.DeclarationOrderIndependenceTests$BeanNameAwareMixin"/>