+ refactoring .testsuite .aop.aspectj tests in preparation for migration to .context

+ updated all ivy.xml files to depend on AspectJ 1.6.2
This commit is contained in:
Chris Beams 2008-12-19 14:56:09 +00:00
parent 5b9da399c6
commit d7a9d7c8e6
154 changed files with 1348 additions and 2407 deletions

View File

@ -22,7 +22,7 @@
<dependencies> <dependencies>
<dependency org="org.apache.commons" name="com.springsource.org.apache.commons.logging" rev="1.1.1" conf="compile->compile"/> <dependency org="org.apache.commons" name="com.springsource.org.apache.commons.logging" rev="1.1.1" conf="compile->compile"/>
<dependency org="org.aspectj" name="com.springsource.org.aspectj.weaver" rev="1.5.4" conf="optional, aspectj->compile"/> <dependency org="org.aspectj" name="com.springsource.org.aspectj.weaver" rev="1.6.2.RELEASE" conf="optional, aspectj->compile"/>
<dependency org="org.springframework" name="org.springframework.beans" rev="latest.integration" conf="compile->compile"/> <dependency org="org.springframework" name="org.springframework.beans" rev="latest.integration" conf="compile->compile"/>
<dependency org="org.springframework" name="org.springframework.transaction" rev="latest.integration" conf="optional, tx->compile"/> <dependency org="org.springframework" name="org.springframework.transaction" rev="latest.integration" conf="optional, tx->compile"/>
</dependencies> </dependencies>

View File

@ -1,4 +1,3 @@
package org.springframework.aop.aspectj;
/* /*
* Copyright 2002-2006 the original author or authors. * Copyright 2002-2006 the original author or authors.
* *
@ -15,6 +14,8 @@ package org.springframework.aop.aspectj;
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.aop.aspectj;
import org.aspectj.lang.JoinPoint; import org.aspectj.lang.JoinPoint;
/** /**

View File

@ -25,7 +25,7 @@
<dependency org="org.apache.commons" name="com.springsource.org.apache.commons.collections" rev="3.2.0" conf="optional, commons-collections->compile"/> <dependency org="org.apache.commons" name="com.springsource.org.apache.commons.collections" rev="3.2.0" conf="optional, commons-collections->compile"/>
<dependency org="org.apache.commons" name="com.springsource.org.apache.commons.logging" rev="1.1.1" conf="compile->compile"/> <dependency org="org.apache.commons" name="com.springsource.org.apache.commons.logging" rev="1.1.1" conf="compile->compile"/>
<dependency org="org.apache.log4j" name="com.springsource.org.apache.log4j" rev="1.2.15" conf="optional, log4j->compile"/> <dependency org="org.apache.log4j" name="com.springsource.org.apache.log4j" rev="1.2.15" conf="optional, log4j->compile"/>
<dependency org="org.aspectj" name="com.springsource.org.aspectj.weaver" rev="1.5.4" conf="optional, aspectj->compile"/> <dependency org="org.aspectj" name="com.springsource.org.aspectj.weaver" rev="1.6.2.RELEASE" conf="optional, aspectj->compile"/>
<!-- These should be replaced with the internal repackaging of ASM --> <!-- These should be replaced with the internal repackaging of ASM -->
<dependency org="org.objectweb.asm" name="com.springsource.org.objectweb.asm" rev="2.2.3" conf="optional->compile"/> <dependency org="org.objectweb.asm" name="com.springsource.org.objectweb.asm" rev="2.2.3" conf="optional->compile"/>
<dependency org="org.objectweb.asm" name="com.springsource.org.objectweb.asm.commons" rev="2.2.3" conf="optional->compile"/> <dependency org="org.objectweb.asm" name="com.springsource.org.objectweb.asm.commons" rev="2.2.3" conf="optional->compile"/>

View File

@ -25,7 +25,7 @@
<dependency org="org.apache.commons" name="com.springsource.org.apache.commons.dbcp" rev="1.2.2.osgi" conf="compile->runtime"/> <dependency org="org.apache.commons" name="com.springsource.org.apache.commons.dbcp" rev="1.2.2.osgi" conf="compile->runtime"/>
<dependency org="org.apache.log4j" name="com.springsource.org.apache.log4j" rev="1.2.15" conf="compile->runtime"/> <dependency org="org.apache.log4j" name="com.springsource.org.apache.log4j" rev="1.2.15" conf="compile->runtime"/>
<dependency org="org.apache.taglibs" name="com.springsource.org.apache.taglibs.standard" rev="1.1.2" conf="compile->runtime"/> <dependency org="org.apache.taglibs" name="com.springsource.org.apache.taglibs.standard" rev="1.1.2" conf="compile->runtime"/>
<dependency org="org.aspectj" name="com.springsource.org.aspectj.weaver" rev="1.5.4" conf="compile->compile"/> <dependency org="org.aspectj" name="com.springsource.org.aspectj.weaver" rev="1.6.2.RELEASE" conf="compile->compile"/>
<dependency org="org.objectweb.asm" name="com.springsource.org.objectweb.asm" rev="2.2.3" conf="compile->compile" /> <dependency org="org.objectweb.asm" name="com.springsource.org.objectweb.asm" rev="2.2.3" conf="compile->compile" />
<dependency org="org.objectweb.asm" name="com.springsource.org.objectweb.asm.commons" rev="2.2.3" conf="compile->compile" /> <dependency org="org.objectweb.asm" name="com.springsource.org.objectweb.asm.commons" rev="2.2.3" conf="compile->compile" />
<dependency org="org.springframework" name="org.springframework.orm" rev="latest.integration" conf="compile->compile"/> <dependency org="org.springframework" name="org.springframework.orm" rev="latest.integration" conf="compile->compile"/>

View File

@ -29,7 +29,7 @@
<dependency org="org.junit" name="com.springsource.org.junit" rev="4.5.0" conf="optional->compile"/> <dependency org="org.junit" name="com.springsource.org.junit" rev="4.5.0" conf="optional->compile"/>
<dependency org="org.apache.commons" name="com.springsource.org.apache.commons.logging" rev="1.1.1" conf="compile->compile"/> <dependency org="org.apache.commons" name="com.springsource.org.apache.commons.logging" rev="1.1.1" conf="compile->compile"/>
<dependency org="org.apache.taglibs" name="com.springsource.org.apache.taglibs.standard" rev="1.1.2" conf="optional->compile"/> <dependency org="org.apache.taglibs" name="com.springsource.org.apache.taglibs.standard" rev="1.1.2" conf="optional->compile"/>
<dependency org="org.aspectj" name="com.springsource.org.aspectj.weaver" rev="1.5.4" conf="optional->compile"/> <dependency org="org.aspectj" name="com.springsource.org.aspectj.weaver" rev="1.6.2.RELEASE" conf="optional->compile"/>
<dependency org="org.testng" name="com.springsource.org.testng" rev="5.8.0" conf="optional->compile"/> <dependency org="org.testng" name="com.springsource.org.testng" rev="5.8.0" conf="optional->compile"/>
<dependency org="org.springframework" name="org.springframework.beans" rev="latest.integration" conf="optional->compile"/> <dependency org="org.springframework" name="org.springframework.beans" rev="latest.integration" conf="optional->compile"/>
<dependency org="org.springframework" name="org.springframework.context" rev="latest.integration" conf="optional->compile"/> <dependency org="org.springframework" name="org.springframework.context" rev="latest.integration" conf="optional->compile"/>

View File

@ -62,7 +62,7 @@
<dependency org="org.apache.tiles" name="com.springsource.org.apache.tiles.jsp" rev="2.0.5" conf="test->compile"/> <dependency org="org.apache.tiles" name="com.springsource.org.apache.tiles.jsp" rev="2.0.5" conf="test->compile"/>
<dependency org="org.apache.velocity" name="com.springsource.org.apache.velocity" rev="1.5.0" conf="test->compile"/> <dependency org="org.apache.velocity" name="com.springsource.org.apache.velocity" rev="1.5.0" conf="test->compile"/>
<dependency org="org.apache.velocity" name="com.springsource.org.apache.velocity.tools.view" rev="1.4.0" conf="test->compile"/> <dependency org="org.apache.velocity" name="com.springsource.org.apache.velocity.tools.view" rev="1.4.0" conf="test->compile"/>
<dependency org="org.aspectj" name="com.springsource.org.aspectj.weaver" rev="1.5.4" conf="test->compile"/> <dependency org="org.aspectj" name="com.springsource.org.aspectj.weaver" rev="1.6.2.RELEASE" conf="test->compile"/>
<dependency org="org.codehaus.groovy" name="com.springsource.org.codehaus.groovy" rev="1.5.1" conf="test->compile"/> <dependency org="org.codehaus.groovy" name="com.springsource.org.codehaus.groovy" rev="1.5.1" conf="test->compile"/>
<dependency org="org.dom4j" name="com.springsource.org.dom4j" rev="1.6.1" conf="test->compile"/> <dependency org="org.dom4j" name="com.springsource.org.dom4j" rev="1.6.1" conf="test->compile"/>
<dependency org="org.easymock" name="com.springsource.org.easymock" rev="2.3.0" conf="test->compile"/> <dependency org="org.easymock" name="com.springsource.org.easymock" rev="2.3.0" conf="test->compile"/>

View File

@ -1,41 +0,0 @@
/*
* Copyright 2002-2005 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.aop.aspectj.autoproxy;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class AdviceUsingThisJoinPoint {
private String lastEntry = "";
public String getLastMethodEntered() {
return this.lastEntry;
}
@Pointcut("execution(* *(..))")
public void methodExecution() {}
@Before("methodExecution()")
public void entryTrace(JoinPoint jp) {
this.lastEntry = jp.toString();
}
}

View File

@ -1,33 +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.autoproxy;
/**
* @author Adrian Colyer
* @since 2.0
*/
public interface AnnotatedTestBean {
String doThis();
String doThat();
String doTheOther();
String[] doArray();
}

View File

@ -1,48 +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.autoproxy;
import org.springframework.transaction.annotation.Transactional;
/**
* @author Adrian Colyer
* @since 2.0
*/
@Transactional
public class AnnotatedTestBeanImpl implements AnnotatedTestBean {
@TestAnnotation("this value")
public String doThis() {
return "doThis";
}
@TestAnnotation("that value")
public String doThat() {
return "doThat";
}
@TestAnnotation("array value")
public String[] doArray() {
return new String[] {"doThis", "doThat"};
}
// not annotated
public String doTheOther() {
return "doTheOther";
}
}

View File

@ -1,30 +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.autoproxy;
import org.aspectj.lang.ProceedingJoinPoint;
/**
* @author Adrian Colyer
*/
public class AnnotationBindingTestAspect {
public String doWithAnnotation(ProceedingJoinPoint pjp, TestAnnotation testAnnotation) throws Throwable {
return testAnnotation.value();
}
}

View File

@ -16,29 +16,35 @@
package org.springframework.aop.aspectj.autoproxy; package org.springframework.aop.aspectj.autoproxy;
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 Adrian Colyer * @author Adrian Colyer
* @author Chris Beams
*/ */
public class AnnotationBindingTests extends AbstractDependencyInjectionSpringContextTests { public final class AnnotationBindingTests {
private AnnotatedTestBean testBean; private AnnotatedTestBean testBean;
@Override @Before
protected String getConfigPath() { public void setUp() {
return "around-advice-tests.xml"; ClassPathXmlApplicationContext ctx =
} new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-context.xml", getClass());
public void setTestBean(AnnotatedTestBean testBean) { testBean = (AnnotatedTestBean) ctx.getBean("testBean");
this.testBean = testBean;
} }
@Test
public void testAnnotationBindingInAroundAdvice() { public void testAnnotationBindingInAroundAdvice() {
assertEquals("this value", testBean.doThis()); assertEquals("this value", testBean.doThis());
assertEquals("that value", testBean.doThat()); assertEquals("that value", testBean.doThat());
} }
@Test
public void testNoMatchingWithoutAnnotationPresent() { public void testNoMatchingWithoutAnnotationPresent() {
assertEquals("doTheOther", testBean.doTheOther()); assertEquals("doTheOther", testBean.doTheOther());
} }

View File

@ -9,7 +9,7 @@
<aop:advisor advice-ref="testInterceptor" pointcut-ref="myPointcut"/> <aop:advisor advice-ref="testInterceptor" pointcut-ref="myPointcut"/>
</aop:config> </aop:config>
<bean id="testInterceptor" class="org.springframework.aop.aspectj.autoproxy.AnnotationPointcutTests$TestMethodInterceptor"/> <bean id="testInterceptor" class="org.springframework.aop.aspectj.autoproxy.TestMethodInterceptor"/>
<bean id="myPointcut" class="org.springframework.aop.support.annotation.AnnotationMatchingPointcut" factory-method="forMethodAnnotation"> <bean id="myPointcut" class="org.springframework.aop.support.annotation.AnnotationMatchingPointcut" factory-method="forMethodAnnotation">
<constructor-arg value="org.springframework.aop.aspectj.autoproxy.TestAnnotation"/> <constructor-arg value="org.springframework.aop.aspectj.autoproxy.TestAnnotation"/>

View File

@ -16,41 +16,47 @@
package org.springframework.aop.aspectj.autoproxy; package org.springframework.aop.aspectj.autoproxy;
import static org.junit.Assert.assertEquals;
import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation; import org.aopalliance.intercept.MethodInvocation;
import org.junit.Before;
import org.springframework.test.AbstractDependencyInjectionSpringContextTests; import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/** /**
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Chris Beams
*/ */
public class AnnotationPointcutTests extends AbstractDependencyInjectionSpringContextTests { public final class AnnotationPointcutTests {
private AnnotatedTestBean testBean; private AnnotatedTestBean testBean;
@Override @Before
protected String getConfigPath() { public void setUp() {
return "annotationPointcut.xml"; ClassPathXmlApplicationContext ctx =
} new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-context.xml", getClass());
public void setTestBean(AnnotatedTestBean testBean) { testBean = (AnnotatedTestBean) ctx.getBean("testBean");
this.testBean = testBean;
} }
@Test
public void testAnnotationBindingInAroundAdvice() { public void testAnnotationBindingInAroundAdvice() {
assertEquals("this value", testBean.doThis()); assertEquals("this value", testBean.doThis());
} }
@Test
public void testNoMatchingWithoutAnnotationPresent() { public void testNoMatchingWithoutAnnotationPresent() {
assertEquals("doTheOther", testBean.doTheOther()); assertEquals("doTheOther", testBean.doTheOther());
} }
}
public static class TestMethodInterceptor implements MethodInterceptor {
class TestMethodInterceptor implements MethodInterceptor {
public Object invoke(MethodInvocation methodInvocation) throws Throwable { public Object invoke(MethodInvocation methodInvocation) throws Throwable {
return "this value"; return "this value";
} }
}
} }

View File

@ -16,5 +16,5 @@
<bean id="testBean" class="org.springframework.beans.TestBean" /> <bean id="testBean" class="org.springframework.beans.TestBean" />
<bean id="interfaceExtendingAspect" <bean id="interfaceExtendingAspect"
class="org.springframework.aop.aspectj.autoproxy.AspectImplementingInterfaceTests$InterfaceExtendingAspect"/> class="org.springframework.aop.aspectj.autoproxy.InterfaceExtendingAspect"/>
</beans> </beans>

View File

@ -16,47 +16,47 @@
package org.springframework.aop.aspectj.autoproxy; package org.springframework.aop.aspectj.autoproxy;
import static org.junit.Assert.*;
import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.ProceedingJoinPoint;
import org.junit.Test;
import org.springframework.aop.framework.Advised; import org.springframework.aop.framework.Advised;
import org.springframework.beans.ITestBean; import org.springframework.beans.ITestBean;
import org.springframework.test.AbstractDependencyInjectionSpringContextTests; import org.springframework.context.support.ClassPathXmlApplicationContext;
/** /**
* Test for ensuring the aspects aren't advised. See SPR-3893 for more details. * Test for ensuring the aspects aren't advised. See SPR-3893 for more details.
* *
* @author Ramnivas Laddad * @author Ramnivas Laddad
* @author Chris Beams
*/ */
public class AspectImplementingInterfaceTests extends AbstractDependencyInjectionSpringContextTests { public final class AspectImplementingInterfaceTests {
protected ITestBean testBean;
protected AnInterface interfaceExtendingAspect;
public AspectImplementingInterfaceTests() {
setPopulateProtectedVariables(true);
}
protected String getConfigPath() {
return "aspect-implementing-interface-tests.xml";
}
protected void onSetUp() throws Exception {
super.onSetUp();
}
@Test
public void testProxyCreation() { public void testProxyCreation() {
ClassPathXmlApplicationContext ctx =
new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-context.xml", getClass());
ITestBean testBean = (ITestBean) ctx.getBean("testBean");
AnInterface interfaceExtendingAspect = (AnInterface) ctx.getBean("interfaceExtendingAspect");
assertTrue(testBean instanceof Advised); assertTrue(testBean instanceof Advised);
assertFalse(interfaceExtendingAspect instanceof Advised); assertFalse(interfaceExtendingAspect instanceof Advised);
} }
public static interface AnInterface { }
public void interfaceMethod();
}
public static class InterfaceExtendingAspect implements AnInterface {
interface AnInterface {
public void interfaceMethod();
}
class InterfaceExtendingAspect implements AnInterface {
public void increment(ProceedingJoinPoint pjp) throws Throwable { public void increment(ProceedingJoinPoint pjp) throws Throwable {
pjp.proceed(); pjp.proceed();
} }
public void interfaceMethod() { public void interfaceMethod() {
} }
}
} }

View File

@ -16,26 +16,26 @@
package org.springframework.aop.aspectj.autoproxy; package org.springframework.aop.aspectj.autoproxy;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.beans.ITestBean; import org.springframework.beans.ITestBean;
import org.springframework.test.AbstractDependencyInjectionSpringContextTests; import org.springframework.beans.TestBean;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/** /**
* @author Rod Johnson * @author Rod Johnson
* @author Rob Harrop * @author Rob Harrop
* @author Chris Beams
*/ */
public class AspectJAutoProxyCreatorAndLazyInitTargetSourceTests extends AbstractDependencyInjectionSpringContextTests { public final class AspectJAutoProxyCreatorAndLazyInitTargetSourceTests {
public AspectJAutoProxyCreatorAndLazyInitTargetSourceTests() {
setAutowireMode(AUTOWIRE_BY_NAME);
}
@Override
protected String getConfigPath() {
return "lazy.xml";
}
@Test
public void testAdrian() { public void testAdrian() {
ITestBean adrian = (ITestBean) applicationContext.getBean("adrian"); ClassPathXmlApplicationContext ctx =
new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-context.xml", getClass());
ITestBean adrian = (ITestBean) ctx.getBean("adrian");
assertEquals(0, LazyTestBean.instantiations); assertEquals(0, LazyTestBean.instantiations);
assertNotNull(adrian); assertNotNull(adrian);
adrian.getAge(); adrian.getAge();
@ -44,3 +44,14 @@ public class AspectJAutoProxyCreatorAndLazyInitTargetSourceTests extends Abstrac
} }
} }
class LazyTestBean extends TestBean {
public static int instantiations;
public LazyTestBean() {
++instantiations;
}
}

View File

@ -15,27 +15,36 @@
*/ */
package org.springframework.aop.aspectj.autoproxy; package org.springframework.aop.aspectj.autoproxy;
import static java.lang.String.format;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import java.lang.reflect.Method;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.junit.Test; import org.junit.Test;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator; import org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator;
import org.springframework.aop.aspectj.annotation.AspectMetadata; import org.springframework.aop.aspectj.annotation.AspectMetadata;
import org.springframework.aop.config.AopConfigUtils; import org.springframework.aop.config.AopConfigUtils;
import org.springframework.aop.framework.ProxyConfig; import org.springframework.aop.framework.ProxyConfig;
import org.springframework.aop.support.AopUtils; import org.springframework.aop.support.AopUtils;
import org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor;
import org.springframework.beans.INestedTestBean; import org.springframework.beans.INestedTestBean;
import org.springframework.beans.ITestBean; import org.springframework.beans.ITestBean;
import org.springframework.beans.NestedTestBean; import org.springframework.beans.NestedTestBean;
import org.springframework.beans.PropertyValue; import org.springframework.beans.PropertyValue;
import org.springframework.beans.TestBean; import org.springframework.beans.TestBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.config.MethodInvokingFactoryBean; import org.springframework.beans.factory.config.MethodInvokingFactoryBean;
import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.beans.factory.support.RootBeanDefinition;
@ -43,7 +52,10 @@ import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.GenericApplicationContext; import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.NestedRuntimeException;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.StopWatch; import org.springframework.util.StopWatch;
/** /**
@ -54,14 +66,13 @@ import org.springframework.util.StopWatch;
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Chris Beams * @author Chris Beams
*/ */
public class AspectJAutoProxyCreatorTests { public final class AspectJAutoProxyCreatorTests {
private static final Log factoryLog = LogFactory.getLog(DefaultListableBeanFactory.class); private static final Log factoryLog = LogFactory.getLog(DefaultListableBeanFactory.class);
@Test @Test
public void testAspectsAreApplied() { public void testAspectsAreApplied() {
ClassPathXmlApplicationContext bf = new ClassPathXmlApplicationContext( ClassPathXmlApplicationContext bf = newContext("aspects.xml");
"/org/springframework/aop/aspectj/autoproxy/aspects.xml");
ITestBean tb = (ITestBean) bf.getBean("adrian"); ITestBean tb = (ITestBean) bf.getBean("adrian");
assertEquals(68, tb.getAge()); assertEquals(68, tb.getAge());
MethodInvokingFactoryBean factoryBean = (MethodInvokingFactoryBean) bf.getBean("&factoryBean"); MethodInvokingFactoryBean factoryBean = (MethodInvokingFactoryBean) bf.getBean("&factoryBean");
@ -71,8 +82,7 @@ public class AspectJAutoProxyCreatorTests {
@Test @Test
public void testMultipleAspectsWithParameterApplied() { public void testMultipleAspectsWithParameterApplied() {
ClassPathXmlApplicationContext bf = new ClassPathXmlApplicationContext( ClassPathXmlApplicationContext bf = newContext("aspects.xml");
"/org/springframework/aop/aspectj/autoproxy/aspects.xml");
ITestBean tb = (ITestBean) bf.getBean("adrian"); ITestBean tb = (ITestBean) bf.getBean("adrian");
tb.setAge(10); tb.setAge(10);
assertEquals(20, tb.getAge()); assertEquals(20, tb.getAge());
@ -80,18 +90,16 @@ public class AspectJAutoProxyCreatorTests {
@Test @Test
public void testAspectsAreAppliedInDefinedOrder() { public void testAspectsAreAppliedInDefinedOrder() {
ClassPathXmlApplicationContext bf = new ClassPathXmlApplicationContext( ClassPathXmlApplicationContext bf = newContext("aspectsWithOrdering.xml");
"/org/springframework/aop/aspectj/autoproxy/aspectsWithOrdering.xml");
ITestBean tb = (ITestBean) bf.getBean("adrian"); ITestBean tb = (ITestBean) bf.getBean("adrian");
assertEquals(71, tb.getAge()); assertEquals(71, tb.getAge());
} }
@Test @Test
public void testAspectsAndAdvisorAreApplied() { public void testAspectsAndAdvisorAreApplied() {
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext( ClassPathXmlApplicationContext ac = newContext("aspectsPlusAdvisor.xml");
"/org/springframework/aop/aspectj/autoproxy/aspectsPlusAdvisor.xml");
ITestBean shouldBeWeaved = (ITestBean) ac.getBean("adrian"); ITestBean shouldBeWeaved = (ITestBean) ac.getBean("adrian");
testAspectsAndAdvisorAreApplied(ac, shouldBeWeaved); doTestAspectsAndAdvisorAreApplied(ac, shouldBeWeaved);
} }
@Test @Test
@ -100,14 +108,13 @@ public class AspectJAutoProxyCreatorTests {
// Skip this test: Trace logging blows the time limit. // Skip this test: Trace logging blows the time limit.
return; return;
} }
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext( ClassPathXmlApplicationContext ac = newContext("aspectsPlusAdvisor.xml");
"/org/springframework/aop/aspectj/autoproxy/aspectsPlusAdvisor.xml");
StopWatch sw = new StopWatch(); StopWatch sw = new StopWatch();
sw.start("prototype"); sw.start("prototype");
for (int i = 0; i < 10000; i++) { for (int i = 0; i < 10000; i++) {
ITestBean shouldBeWeaved = (ITestBean) ac.getBean("adrian2"); ITestBean shouldBeWeaved = (ITestBean) ac.getBean("adrian2");
if (i < 10) { if (i < 10) {
testAspectsAndAdvisorAreApplied(ac, shouldBeWeaved); doTestAspectsAndAdvisorAreApplied(ac, shouldBeWeaved);
} }
} }
sw.stop(); sw.stop();
@ -121,8 +128,7 @@ public class AspectJAutoProxyCreatorTests {
// Skip this test: Trace logging blows the time limit. // Skip this test: Trace logging blows the time limit.
return; return;
} }
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext( ClassPathXmlApplicationContext ac = newContext("aspectsPlusAdvisor.xml");
"/org/springframework/aop/aspectj/autoproxy/aspectsPlusAdvisor.xml");
StopWatch sw = new StopWatch(); StopWatch sw = new StopWatch();
sw.start("prototype"); sw.start("prototype");
for (int i = 0; i < 100000; i++) { for (int i = 0; i < 100000; i++) {
@ -144,7 +150,7 @@ public class AspectJAutoProxyCreatorTests {
} }
GenericApplicationContext ac = new GenericApplicationContext(); GenericApplicationContext ac = new GenericApplicationContext();
new XmlBeanDefinitionReader(ac).loadBeanDefinitions( new XmlBeanDefinitionReader(ac).loadBeanDefinitions(
"/org/springframework/aop/aspectj/autoproxy/aspectsPlusAdvisor.xml"); new ClassPathResource(qName("aspectsPlusAdvisor.xml"), getClass()));
for (int i = 0; i < 10000; i++) { for (int i = 0; i < 10000; i++) {
ac.registerBeanDefinition("singleton" + i, new RootBeanDefinition(NestedTestBean.class)); ac.registerBeanDefinition("singleton" + i, new RootBeanDefinition(NestedTestBean.class));
} }
@ -158,8 +164,7 @@ public class AspectJAutoProxyCreatorTests {
@Test @Test
public void testAspectsAndAdvisorAreAppliedEvenIfComingFromParentFactory() { public void testAspectsAndAdvisorAreAppliedEvenIfComingFromParentFactory() {
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext( ClassPathXmlApplicationContext ac = newContext("aspectsPlusAdvisor.xml");
"/org/springframework/aop/aspectj/autoproxy/aspectsPlusAdvisor.xml");
GenericApplicationContext childAc = new GenericApplicationContext(ac); GenericApplicationContext childAc = new GenericApplicationContext(ac);
// Create a child factory with a bean that should be weaved // Create a child factory with a bean that should be weaved
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
@ -173,10 +178,10 @@ public class AspectJAutoProxyCreatorTests {
ITestBean beanFromChildContextThatShouldBeWeaved = (ITestBean) childAc.getBean("adrian2"); ITestBean beanFromChildContextThatShouldBeWeaved = (ITestBean) childAc.getBean("adrian2");
//testAspectsAndAdvisorAreApplied(childAc, (ITestBean) ac.getBean("adrian")); //testAspectsAndAdvisorAreApplied(childAc, (ITestBean) ac.getBean("adrian"));
testAspectsAndAdvisorAreApplied(childAc, beanFromChildContextThatShouldBeWeaved); doTestAspectsAndAdvisorAreApplied(childAc, beanFromChildContextThatShouldBeWeaved);
} }
protected void testAspectsAndAdvisorAreApplied(ApplicationContext ac, ITestBean shouldBeWeaved) { protected void doTestAspectsAndAdvisorAreApplied(ApplicationContext ac, ITestBean shouldBeWeaved) {
TestBeanAdvisor tba = (TestBeanAdvisor) ac.getBean("advisor"); TestBeanAdvisor tba = (TestBeanAdvisor) ac.getBean("advisor");
MultiplyReturnValue mrv = (MultiplyReturnValue) ac.getBean("aspect"); MultiplyReturnValue mrv = (MultiplyReturnValue) ac.getBean("aspect");
@ -195,8 +200,7 @@ public class AspectJAutoProxyCreatorTests {
@Test @Test
public void testPerThisAspect() { public void testPerThisAspect() {
ClassPathXmlApplicationContext bf = new ClassPathXmlApplicationContext( ClassPathXmlApplicationContext bf = newContext("perthis.xml");
"/org/springframework/aop/aspectj/autoproxy/perthis.xml");
ITestBean adrian1 = (ITestBean) bf.getBean("adrian"); ITestBean adrian1 = (ITestBean) bf.getBean("adrian");
assertTrue(AopUtils.isAopProxy(adrian1)); assertTrue(AopUtils.isAopProxy(adrian1));
@ -216,8 +220,7 @@ public class AspectJAutoProxyCreatorTests {
@Test @Test
public void testPerTargetAspect() throws SecurityException, NoSuchMethodException { public void testPerTargetAspect() throws SecurityException, NoSuchMethodException {
ClassPathXmlApplicationContext bf = new ClassPathXmlApplicationContext( ClassPathXmlApplicationContext bf = newContext("pertarget.xml");
"/org/springframework/aop/aspectj/autoproxy/pertarget.xml");
ITestBean adrian1 = (ITestBean) bf.getBean("adrian"); ITestBean adrian1 = (ITestBean) bf.getBean("adrian");
assertTrue(AopUtils.isAopProxy(adrian1)); assertTrue(AopUtils.isAopProxy(adrian1));
@ -262,8 +265,7 @@ public class AspectJAutoProxyCreatorTests {
} }
private void doTestTwoAdviceAspectWith(String location) { private void doTestTwoAdviceAspectWith(String location) {
ClassPathXmlApplicationContext bf = new ClassPathXmlApplicationContext( ClassPathXmlApplicationContext bf = newContext(location);
"/org/springframework/aop/aspectj/autoproxy/" + location);
boolean aspectSingleton = bf.isSingleton("aspect"); boolean aspectSingleton = bf.isSingleton("aspect");
ITestBean adrian1 = (ITestBean) bf.getBean("adrian"); ITestBean adrian1 = (ITestBean) bf.getBean("adrian");
@ -275,8 +277,7 @@ public class AspectJAutoProxyCreatorTests {
@Test @Test
public void testAdviceUsingJoinPoint() { public void testAdviceUsingJoinPoint() {
ClassPathXmlApplicationContext bf = new ClassPathXmlApplicationContext( ClassPathXmlApplicationContext bf = newContext("usesJoinPointAspect.xml");
"/org/springframework/aop/aspectj/autoproxy/usesJoinPointAspect.xml");
ITestBean adrian1 = (ITestBean) bf.getBean("adrian"); ITestBean adrian1 = (ITestBean) bf.getBean("adrian");
adrian1.getAge(); adrian1.getAge();
@ -288,8 +289,7 @@ public class AspectJAutoProxyCreatorTests {
@Test @Test
public void testIncludeMechanism() { public void testIncludeMechanism() {
ClassPathXmlApplicationContext bf = new ClassPathXmlApplicationContext( ClassPathXmlApplicationContext bf = newContext("usesInclude.xml");
"/org/springframework/aop/aspectj/autoproxy/usesInclude.xml");
ITestBean adrian = (ITestBean) bf.getBean("adrian"); ITestBean adrian = (ITestBean) bf.getBean("adrian");
assertTrue(AopUtils.isAopProxy(adrian)); assertTrue(AopUtils.isAopProxy(adrian));
@ -310,8 +310,7 @@ public class AspectJAutoProxyCreatorTests {
@Test @Test
public void testForceProxyTargetClass() { public void testForceProxyTargetClass() {
ClassPathXmlApplicationContext bf = new ClassPathXmlApplicationContext( ClassPathXmlApplicationContext bf = newContext("aspectsWithCGLIB.xml");
"/org/springframework/aop/aspectj/autoproxy/aspectsWithCGLIB.xml");
ProxyConfig pc = (ProxyConfig) bf.getBean(AopConfigUtils.AUTO_PROXY_CREATOR_BEAN_NAME); ProxyConfig pc = (ProxyConfig) bf.getBean(AopConfigUtils.AUTO_PROXY_CREATOR_BEAN_NAME);
assertTrue("should be proxying classes", pc.isProxyTargetClass()); assertTrue("should be proxying classes", pc.isProxyTargetClass());
@ -319,8 +318,7 @@ public class AspectJAutoProxyCreatorTests {
@Test @Test
public void testWithAbstractFactoryBeanAreApplied() { public void testWithAbstractFactoryBeanAreApplied() {
ClassPathXmlApplicationContext bf = new ClassPathXmlApplicationContext( ClassPathXmlApplicationContext bf = newContext("aspectsWithAbstractBean.xml");
"/org/springframework/aop/aspectj/autoproxy/aspectsWithAbstractBean.xml");
ITestBean adrian = (ITestBean) bf.getBean("adrian"); ITestBean adrian = (ITestBean) bf.getBean("adrian");
assertTrue(AopUtils.isAopProxy(adrian)); assertTrue(AopUtils.isAopProxy(adrian));
@ -329,8 +327,7 @@ public class AspectJAutoProxyCreatorTests {
@Test @Test
public void testRetryAspect() throws Exception { public void testRetryAspect() throws Exception {
ClassPathXmlApplicationContext bf = new ClassPathXmlApplicationContext( ClassPathXmlApplicationContext bf = newContext("retryAspect.xml");
"/org/springframework/aop/aspectj/autoproxy/retryAspect.xml");
UnreliableBean bean = (UnreliableBean) bf.getBean("unreliableBean"); UnreliableBean bean = (UnreliableBean) bf.getBean("unreliableBean");
RetryAspect aspect = (RetryAspect) bf.getBean("retryAspect"); RetryAspect aspect = (RetryAspect) bf.getBean("retryAspect");
int attempts = bean.unreliable(); int attempts = bean.unreliable();
@ -340,6 +337,22 @@ public class AspectJAutoProxyCreatorTests {
assertEquals(1, aspect.getCommitCalls()); assertEquals(1, aspect.getCommitCalls());
} }
/**
* Returns a new {@link ClassPathXmlApplicationContext} for the file ending in <var>fileSuffix</var>.
*/
private ClassPathXmlApplicationContext newContext(String fileSuffix) {
return new ClassPathXmlApplicationContext(qName(fileSuffix), getClass());
}
/**
* Returns the relatively qualified name for <var>fileSuffix</var>.
* e.g. for a fileSuffix='foo.xml', this method will return
* 'AspectJAutoProxyCreatorTests-foo.xml'
*/
private String qName(String fileSuffix) {
return format("%s-%s", getClass().getSimpleName(), fileSuffix);
}
} }
@ -369,3 +382,203 @@ class PerTargetAspect implements Ordered {
} }
} }
@Aspect
class AdviceUsingThisJoinPoint {
private String lastEntry = "";
public String getLastMethodEntered() {
return this.lastEntry;
}
@Pointcut("execution(* *(..))")
public void methodExecution() {}
@Before("methodExecution()")
public void entryTrace(JoinPoint jp) {
this.lastEntry = jp.toString();
}
}
@Aspect
class DummyAspect {
@Around("execution(* setAge(int))")
public Object test(ProceedingJoinPoint pjp) throws Throwable {
return pjp.proceed();
}
}
@Aspect
class DummyAspectWithParameter {
@Around("execution(* setAge(int)) && args(age)")
public Object test(ProceedingJoinPoint pjp, int age) throws Throwable {
return pjp.proceed();
}
}
class DummyFactoryBean implements FactoryBean<Object> {
public Object getObject() throws Exception {
throw new UnsupportedOperationException();
}
public Class<?> getObjectType() {
throw new UnsupportedOperationException();
}
public boolean isSingleton() {
throw new UnsupportedOperationException();
}
}
@Aspect
@Order(10)
class IncreaseReturnValue {
@Around("execution(int *.getAge())")
public Object doubleReturnValue(ProceedingJoinPoint pjp) throws Throwable {
int result = (Integer) pjp.proceed();
return result + 3;
}
}
@Aspect
class MultiplyReturnValue {
private int multiple = 2;
public int invocations;
public void setMultiple(int multiple) {
this.multiple = multiple;
}
public int getMultiple() {
return this.multiple;
}
@Around("execution(int *.getAge())")
public Object doubleReturnValue(ProceedingJoinPoint pjp) throws Throwable {
++this.invocations;
int result = (Integer) pjp.proceed();
return result * this.multiple;
}
}
@Aspect
class RetryAspect {
private int beginCalls;
private int commitCalls;
private int rollbackCalls;
@Pointcut("execution(public * UnreliableBean.*(..))")
public void execOfPublicMethod() {
}
/**
* Retry Advice
*/
@Around("execOfPublicMethod()")
public Object retry(ProceedingJoinPoint jp) throws Throwable {
boolean retry = true;
Object o = null;
while (retry) {
try {
retry = false;
this.beginCalls++;
try {
o = jp.proceed();
this.commitCalls++;
}
catch (RetryableException e) {
this.rollbackCalls++;
throw e;
}
}
catch (RetryableException re) {
retry = true;
}
}
return o;
}
public int getBeginCalls() {
return this.beginCalls;
}
public int getCommitCalls() {
return this.commitCalls;
}
public int getRollbackCalls() {
return this.rollbackCalls;
}
}
@SuppressWarnings("serial")
class RetryableException extends NestedRuntimeException {
public RetryableException(String msg) {
super(msg);
}
public RetryableException(String msg, Throwable cause) {
super(msg, cause);
}
}
class UnreliableBean {
private int calls;
public int unreliable() {
this.calls++;
if (this.calls % 2 != 0) {
throw new RetryableException("foo");
}
return this.calls;
}
}
@SuppressWarnings("serial")
class TestBeanAdvisor extends StaticMethodMatcherPointcutAdvisor {
public int count;
public TestBeanAdvisor() {
setAdvice(new MethodBeforeAdvice() {
public void before(Method method, Object[] args, Object target) throws Throwable {
++count;
}
});
}
public boolean matches(Method method, Class<?> targetClass) {
return ITestBean.class.isAssignableFrom(targetClass);
}
}

View File

@ -18,6 +18,8 @@ package org.springframework.aop.aspectj.autoproxy;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.ITestBean; import org.springframework.beans.ITestBean;
import org.springframework.aop.support.AopUtils; import org.springframework.aop.support.AopUtils;
@ -27,16 +29,18 @@ import java.io.IOException;
/** /**
* @author Rob Harrop * @author Rob Harrop
* @author Chris Beams
* @since 2.0 * @since 2.0
*/ */
public class AtAspectJAfterThrowingTests { public final class AtAspectJAfterThrowingTests {
@Test @Test
public void testAccessThrowable() throws Exception { public void testAccessThrowable() throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("afterThrowingAdviceTests.xml", getClass()); ClassPathXmlApplicationContext ctx =
new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-context.xml", getClass());
ITestBean bean = (ITestBean) context.getBean("testBean"); ITestBean bean = (ITestBean) ctx.getBean("testBean");
ExceptionHandlingAspect aspect = (ExceptionHandlingAspect) context.getBean("aspect"); ExceptionHandlingAspect aspect = (ExceptionHandlingAspect) ctx.getBean("aspect");
assertTrue(AopUtils.isAopProxy(bean)); assertTrue(AopUtils.isAopProxy(bean));
try { try {
@ -50,3 +54,19 @@ public class AtAspectJAfterThrowingTests {
assertNotNull(aspect.lastException); assertNotNull(aspect.lastException);
} }
} }
@Aspect
class ExceptionHandlingAspect {
public int handled;
public IOException lastException;
@AfterThrowing(pointcut = "within(org.springframework.beans.ITestBean+)", throwing = "ex")
public void handleIOException(IOException ex) {
handled++;
lastException = ex;
}
}

View File

@ -1,38 +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.autoproxy;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
/**
* @author Adrian Colyer
* @author Juergen Hoeller
*/
@Aspect
public class AtAspectJAnnotationBindingTestAspect {
@Around("execution(* *(..)) && @annotation(testAnn)")
public Object doWithAnnotation(ProceedingJoinPoint pjp, TestAnnotation testAnn)
throws Throwable {
String annValue = testAnn.value();
Object result = pjp.proceed();
return (result instanceof String ? annValue + " " + result : result);
}
}

View File

@ -16,39 +16,84 @@
package org.springframework.aop.aspectj.autoproxy; package org.springframework.aop.aspectj.autoproxy;
import org.springframework.test.AbstractDependencyInjectionSpringContextTests; import static org.junit.Assert.assertEquals;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.Resource;
/** /**
* @author Adrian Colyer * @author Adrian Colyer
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Chris Beams
*/ */
public class AtAspectJAnnotationBindingTests extends AbstractDependencyInjectionSpringContextTests { public final class AtAspectJAnnotationBindingTests {
private AnnotatedTestBean testBean; private AnnotatedTestBean testBean;
private ClassPathXmlApplicationContext ctx;
public void setTestBean(AnnotatedTestBean testBean) { @Before
this.testBean = testBean; public void setUp() {
} ctx = new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-context.xml", getClass());
testBean = (AnnotatedTestBean) ctx.getBean("testBean");
@Override
protected String getConfigPath() {
return "ataspectj-around-advice-tests.xml";
} }
@Test
public void testAnnotationBindingInAroundAdvice() { public void testAnnotationBindingInAroundAdvice() {
assertEquals("this value doThis", testBean.doThis()); assertEquals("this value doThis", testBean.doThis());
assertEquals("that value doThat", testBean.doThat()); assertEquals("that value doThat", testBean.doThat());
assertEquals(2, testBean.doArray().length); assertEquals(2, testBean.doArray().length);
} }
@Test
public void testNoMatchingWithoutAnnotationPresent() { public void testNoMatchingWithoutAnnotationPresent() {
assertEquals("doTheOther", testBean.doTheOther()); assertEquals("doTheOther", testBean.doTheOther());
} }
@Test
public void testPointcutEvaulatedAgainstArray() { public void testPointcutEvaulatedAgainstArray() {
applicationContext.getBean("arrayFactoryBean"); ctx.getBean("arrayFactoryBean");
}
}
@Aspect
class AtAspectJAnnotationBindingTestAspect {
@Around("execution(* *(..)) && @annotation(testAnn)")
public Object doWithAnnotation(ProceedingJoinPoint pjp, TestAnnotation testAnn)
throws Throwable {
String annValue = testAnn.value();
Object result = pjp.proceed();
return (result instanceof String ? annValue + " " + result : result);
}
}
class ResourceArrayFactoryBean implements FactoryBean<Object> {
@TestAnnotation("some value")
public Object getObject() throws Exception {
return new Resource[0];
}
@TestAnnotation("some value")
public Class<?> getObjectType() {
return Resource[].class;
}
public boolean isSingleton() {
return true;
} }
} }

View File

@ -1,34 +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.autoproxy;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
/**
* @author Juergen Hoeller
*/
@Aspect
public class DummyAspect {
@Around("execution(* setAge(int))")
public Object test(ProceedingJoinPoint pjp) throws Throwable {
return pjp.proceed();
}
}

View File

@ -1,34 +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.autoproxy;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
/**
* @author Juergen Hoeller
*/
@Aspect
public class DummyAspectWithParameter {
@Around("execution(* setAge(int)) && args(age)")
public Object test(ProceedingJoinPoint pjp, int age) throws Throwable {
return pjp.proceed();
}
}

View File

@ -1,38 +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.autoproxy;
import org.springframework.beans.factory.FactoryBean;
/**
* @author Rob Harrop
*/
public class DummyFactoryBean implements FactoryBean {
public Object getObject() throws Exception {
throw new UnsupportedOperationException();
}
public Class getObjectType() {
throw new UnsupportedOperationException();
}
public boolean isSingleton() {
throw new UnsupportedOperationException();
}
}

View File

@ -1,41 +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.autoproxy;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import java.io.IOException;
/**
* @author Rob Harrop
* @since 2.0
*/
@Aspect
public class ExceptionHandlingAspect {
public int handled;
public IOException lastException;
@AfterThrowing(pointcut = "within(org.springframework.beans.ITestBean+)", throwing = "ex")
public void handleIOException(IOException ex) {
handled++;
lastException = ex;
}
}

View File

@ -1,38 +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.autoproxy;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.annotation.Order;
/**
* @author Juergen Hoeller
*/
@Aspect
@Order(10)
public class IncreaseReturnValue {
@Around("execution(int *.getAge())")
public Object doubleReturnValue(ProceedingJoinPoint pjp) throws Throwable {
int result = (Integer) pjp.proceed();
return result + 3;
}
}

View File

@ -1,36 +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.autoproxy;
import org.springframework.beans.TestBean;
/**
* @author Rod Johnson
* @since 2.0
*/
public class LazyTestBean extends TestBean {
public static int instantiations;
/**
*
*/
public LazyTestBean() {
++instantiations;
}
}

View File

@ -1,48 +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.autoproxy;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
/**
* @author Rod Johnson
*/
@Aspect
public class MultiplyReturnValue {
private int multiple = 2;
public int invocations;
public void setMultiple(int multiple) {
this.multiple = multiple;
}
public int getMultiple() {
return this.multiple;
}
@Around("execution(int *.getAge())")
public Object doubleReturnValue(ProceedingJoinPoint pjp) throws Throwable {
++this.invocations;
int result = (Integer) pjp.proceed();
return result * this.multiple;
}
}

View File

@ -1,41 +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.autoproxy;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.core.io.Resource;
/**
* @author Juergen Hoeller
*/
public class ResourceArrayFactoryBean implements FactoryBean {
@TestAnnotation("some value")
public Object getObject() throws Exception {
return new Resource[0];
}
@TestAnnotation("some value")
public Class getObjectType() {
return Resource[].class;
}
public boolean isSingleton() {
return true;
}
}

View File

@ -1,83 +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.autoproxy;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
/**
* @author Rob Harrop
* @author Juergen Hoeller
*/
@Aspect
public class RetryAspect {
private int beginCalls;
private int commitCalls;
private int rollbackCalls;
@Pointcut("execution(public * UnreliableBean.*(..))")
public void execOfPublicMethod() {
}
/**
* Retry Advice
*/
@Around("execOfPublicMethod()")
public Object retry(ProceedingJoinPoint jp) throws Throwable {
boolean retry = true;
Object o = null;
while (retry) {
try {
retry = false;
this.beginCalls++;
try {
o = jp.proceed();
this.commitCalls++;
}
catch (RetryableException e) {
this.rollbackCalls++;
throw e;
}
}
catch (RetryableException re) {
retry = true;
}
}
return o;
}
public int getBeginCalls() {
return this.beginCalls;
}
public int getCommitCalls() {
return this.commitCalls;
}
public int getRollbackCalls() {
return this.rollbackCalls;
}
}

View File

@ -1,34 +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.autoproxy;
import org.springframework.core.NestedRuntimeException;
/**
* @author Rob Harrop
* @since 2.0
*/
public class RetryableException extends NestedRuntimeException {
public RetryableException(String msg) {
super(msg);
}
public RetryableException(String msg, Throwable cause) {
super(msg, cause);
}
}

View File

@ -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 6 Oct 2006 by Adrian Colyer
*/
package org.springframework.aop.aspectj.autoproxy;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* @author Adrian Colyer
* @since 2.0
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface TestAnnotation {
String value() ;
}

View File

@ -1,41 +0,0 @@
/*
* Copyright 2002-2005 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.aop.aspectj.autoproxy;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor;
import org.springframework.beans.ITestBean;
public class TestBeanAdvisor extends StaticMethodMatcherPointcutAdvisor {
public int count;
public TestBeanAdvisor() {
setAdvice(new MethodBeforeAdvice() {
public void before(Method method, Object[] args, Object target) throws Throwable {
++count;
}
});
}
public boolean matches(Method method, Class targetClass) {
return ITestBean.class.isAssignableFrom(targetClass);
}
}

View File

@ -1,34 +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.autoproxy;
/**
* @author Rob Harrop
*/
public class UnreliableBean {
private int calls;
public int unreliable() {
this.calls++;
if (this.calls % 2 != 0) {
throw new RetryableException("foo");
}
return this.calls;
}
}

View File

@ -18,9 +18,21 @@ package org.springframework.aop.aspectj.autoproxy.benchmark;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import java.lang.reflect.Method;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.aop.Advisor;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.framework.Advised; import org.springframework.aop.framework.Advised;
import org.springframework.aop.support.AopUtils; import org.springframework.aop.support.AopUtils;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.StaticMethodMatcherPointcut;
import org.springframework.beans.ITestBean; import org.springframework.beans.ITestBean;
import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.StopWatch; import org.springframework.util.StopWatch;
@ -32,18 +44,13 @@ import org.springframework.util.StopWatch;
* @author Rod Johnson * @author Rod Johnson
* @author Chris Beams * @author Chris Beams
*/ */
public class BenchmarkTests { public final class BenchmarkTests {
private static final String ASPECTJ_CONTEXT = "/org/springframework/aop/aspectj/autoproxy/benchmark/aspectj.xml"; private static final Class<?> CLASS = BenchmarkTests.class;
private static final String SPRING_AOP_CONTEXT = "/org/springframework/aop/aspectj/autoproxy/benchmark/springAop.xml"; private static final String ASPECTJ_CONTEXT = CLASS.getSimpleName() + "-aspectj.xml";
/** private static final String SPRING_AOP_CONTEXT = CLASS.getSimpleName() + "-springAop.xml";
* Change the return number to a higher number to make this test useful.
*/
protected int getCount() {
return 10;
}
@Test @Test
public void testRepeatedAroundAdviceInvocationsWithAspectJ() { public void testRepeatedAroundAdviceInvocationsWithAspectJ() {
@ -85,8 +92,15 @@ public class BenchmarkTests {
testMix(SPRING_AOP_CONTEXT, getCount(), "Spring AOP"); testMix(SPRING_AOP_CONTEXT, getCount(), "Spring AOP");
} }
/**
* Change the return number to a higher number to make this test useful.
*/
protected int getCount() {
return 10;
}
private long testRepeatedAroundAdviceInvocations(String file, int howmany, String technology) { private long testRepeatedAroundAdviceInvocations(String file, int howmany, String technology) {
ClassPathXmlApplicationContext bf = new ClassPathXmlApplicationContext(file); ClassPathXmlApplicationContext bf = new ClassPathXmlApplicationContext(file, CLASS);
StopWatch sw = new StopWatch(); StopWatch sw = new StopWatch();
sw.start(howmany + " repeated around advice invocations with " + technology); sw.start(howmany + " repeated around advice invocations with " + technology);
@ -105,7 +119,7 @@ public class BenchmarkTests {
} }
private long testBeforeAdviceWithoutJoinPoint(String file, int howmany, String technology) { private long testBeforeAdviceWithoutJoinPoint(String file, int howmany, String technology) {
ClassPathXmlApplicationContext bf = new ClassPathXmlApplicationContext(file); ClassPathXmlApplicationContext bf = new ClassPathXmlApplicationContext(file, CLASS);
StopWatch sw = new StopWatch(); StopWatch sw = new StopWatch();
sw.start(howmany + " repeated before advice invocations with " + technology); sw.start(howmany + " repeated before advice invocations with " + technology);
@ -126,7 +140,7 @@ public class BenchmarkTests {
} }
private long testAfterReturningAdviceWithoutJoinPoint(String file, int howmany, String technology) { private long testAfterReturningAdviceWithoutJoinPoint(String file, int howmany, String technology) {
ClassPathXmlApplicationContext bf = new ClassPathXmlApplicationContext(file); ClassPathXmlApplicationContext bf = new ClassPathXmlApplicationContext(file, CLASS);
StopWatch sw = new StopWatch(); StopWatch sw = new StopWatch();
sw.start(howmany + " repeated after returning advice invocations with " + technology); sw.start(howmany + " repeated after returning advice invocations with " + technology);
@ -148,7 +162,7 @@ public class BenchmarkTests {
} }
private long testMix(String file, int howmany, String technology) { private long testMix(String file, int howmany, String technology) {
ClassPathXmlApplicationContext bf = new ClassPathXmlApplicationContext(file); ClassPathXmlApplicationContext bf = new ClassPathXmlApplicationContext(file, CLASS);
StopWatch sw = new StopWatch(); StopWatch sw = new StopWatch();
sw.start(howmany + " repeated mixed invocations with " + technology); sw.start(howmany + " repeated mixed invocations with " + technology);
@ -176,3 +190,89 @@ public class BenchmarkTests {
} }
} }
class MultiplyReturnValueInterceptor implements MethodInterceptor {
private int multiple = 2;
public int invocations;
public void setMultiple(int multiple) {
this.multiple = multiple;
}
public int getMultiple() {
return this.multiple;
}
public Object invoke(MethodInvocation mi) throws Throwable {
++invocations;
int result = (Integer) mi.proceed();
return result * this.multiple;
}
}
class TraceAfterReturningAdvice implements AfterReturningAdvice {
public int afterTakesInt;
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
++afterTakesInt;
}
public static Advisor advisor() {
return new DefaultPointcutAdvisor(
new StaticMethodMatcherPointcut() {
public boolean matches(Method method, Class<?> targetClass) {
return method.getParameterTypes().length == 1 &&
method.getParameterTypes()[0].equals(Integer.class);
}
},
new TraceAfterReturningAdvice());
}
}
@Aspect
class TraceAspect {
public int beforeStringReturn;
public int afterTakesInt;
@Before("execution(String *.*(..))")
public void traceWithoutJoinPoint() {
++beforeStringReturn;
}
@AfterReturning("execution(void *.*(int))")
public void traceWithoutJoinPoint2() {
++afterTakesInt;
}
}
class TraceBeforeAdvice implements MethodBeforeAdvice {
public int beforeStringReturn;
public void before(Method method, Object[] args, Object target) throws Throwable {
++beforeStringReturn;
}
public static Advisor advisor() {
return new DefaultPointcutAdvisor(
new StaticMethodMatcherPointcut() {
public boolean matches(Method method, Class<?> targetClass) {
return method.getReturnType().equals(String.class);
}
},
new TraceBeforeAdvice());
}
}

View File

@ -1,42 +0,0 @@
/*
* Copyright 2002-2005 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.aop.aspectj.autoproxy.benchmark;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class MultiplyReturnValueInterceptor implements MethodInterceptor {
private int multiple = 2;
public int invocations;
public void setMultiple(int multiple) {
this.multiple = multiple;
}
public int getMultiple() {
return this.multiple;
}
public Object invoke(MethodInvocation mi) throws Throwable {
++invocations;
int result = (Integer) mi.proceed();
return result * this.multiple;
}
}

View File

@ -1,51 +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.autoproxy.benchmark;
import java.lang.reflect.Method;
import org.springframework.aop.Advisor;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.StaticMethodMatcherPointcut;
/**
*
*
* @author Rod Johnson
*
*/
public class TraceAfterReturningAdvice implements AfterReturningAdvice {
public int afterTakesInt;
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
++afterTakesInt;
}
public static Advisor advisor() {
return new DefaultPointcutAdvisor(
new StaticMethodMatcherPointcut() {
public boolean matches(Method method, Class targetClass) {
return method.getParameterTypes().length == 1 &&
method.getParameterTypes()[0].equals(Integer.class);
}
},
new TraceAfterReturningAdvice());
}
}

View File

@ -1,40 +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.autoproxy.benchmark;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class TraceAspect {
public int beforeStringReturn;
public int afterTakesInt;
@Before("execution(String *.*(..))")
public void traceWithoutJoinPoint() {
++beforeStringReturn;
}
@AfterReturning("execution(void *.*(int))")
public void traceWithoutJoinPoint2() {
++afterTakesInt;
}
}

View File

@ -1,44 +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.autoproxy.benchmark;
import java.lang.reflect.Method;
import org.springframework.aop.Advisor;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.StaticMethodMatcherPointcut;
public class TraceBeforeAdvice implements MethodBeforeAdvice {
public int beforeStringReturn;
public void before(Method method, Object[] args, Object target) throws Throwable {
++beforeStringReturn;
}
public static Advisor advisor() {
return new DefaultPointcutAdvisor(
new StaticMethodMatcherPointcut() {
public boolean matches(Method method, Class targetClass) {
return method.getReturnType().equals(String.class);
}
},
new TraceBeforeAdvice());
}
}

View File

@ -15,26 +15,33 @@
*/ */
package org.springframework.aop.aspectj.autoproxy.spr3064; package org.springframework.aop.aspectj.autoproxy.spr3064;
import org.springframework.test.AbstractDependencyInjectionSpringContextTests; import static org.junit.Assert.*;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/** /**
* @author acolyer * @author Adrian Colyer
* * @author Chris Beams
*/ */
public class SPR3064Tests extends AbstractDependencyInjectionSpringContextTests { public final class SPR3064Tests {
private Service service; private Service service;
@Override @Test
protected String[] getConfigLocations() {
return new String[] {"org/springframework/aop/aspectj/autoproxy/spr3064/annotationbinding-spr3064.xml"};
}
public void setService(Service service) {
this.service = service;
}
public void testServiceIsAdvised() { public void testServiceIsAdvised() {
ClassPathXmlApplicationContext ctx =
new ClassPathXmlApplicationContext(getClass().getSimpleName() + ".xml", getClass());
service = (Service) ctx.getBean("service");
try { try {
this.service.serveMe(); this.service.serveMe();
fail("service operation has not been advised by transaction interceptor"); fail("service operation has not been advised by transaction interceptor");
@ -45,3 +52,37 @@ public class SPR3064Tests extends AbstractDependencyInjectionSpringContextTests
} }
} }
@Retention(RetentionPolicy.RUNTIME)
@interface Transaction {
}
@Aspect
class TransactionInterceptor {
@Around(value="execution(* *..Service.*(..)) && @annotation(transaction)")
public Object around(ProceedingJoinPoint pjp, Transaction transaction) throws Throwable {
throw new RuntimeException("advice invoked");
//return pjp.proceed();
}
}
interface Service {
void serveMe();
}
class ServiceImpl implements Service {
@Transaction
public void serveMe() {
}
}

View File

@ -7,7 +7,7 @@
<aop:aspectj-autoproxy/> <aop:aspectj-autoproxy/>
<bean id="annotatedBean" class="org.springframework.aop.aspectj.autoproxy.spr3064.ServiceImpl"/> <bean id="service" class="org.springframework.aop.aspectj.autoproxy.spr3064.ServiceImpl"/>
<bean id="transactionInterceptor" class="org.springframework.aop.aspectj.autoproxy.spr3064.TransactionInterceptor"/> <bean id="transactionInterceptor" class="org.springframework.aop.aspectj.autoproxy.spr3064.TransactionInterceptor"/>

View File

@ -1,26 +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.autoproxy.spr3064;
/**
* @author acolyer
*
*/
public interface Service {
void serveMe();
}

View File

@ -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.autoproxy.spr3064;
/**
* @author acolyer
*
*/
public class ServiceImpl implements Service {
/* (non-Javadoc)
* @see org.springframework.aop.aspectj.autoproxy.spr3064.Service#serveMe()
*/
@Transaction
public void serveMe() {
}
}

View File

@ -1,28 +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.autoproxy.spr3064;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* @author acolyer
*
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface Transaction {
}

View File

@ -1,35 +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.autoproxy.spr3064;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
/**
* @author acolyer
*
*/
@Aspect
public class TransactionInterceptor {
@Around(value="execution(* *..Service.*(..)) && @annotation(transaction)")
public Object around(ProceedingJoinPoint pjp, Transaction transaction) throws Throwable {
throw new RuntimeException("advice invoked");
//return pjp.proceed();
}
}

View File

@ -7,8 +7,8 @@
<aop:aspectj-autoproxy/> <aop:aspectj-autoproxy/>
<bean id="counterAspect" class="org.springframework.aop.aspectj.generic.AfterReturningGenericTypeMatchingTests$CounterAspect"/> <bean id="counterAspect" class="org.springframework.aop.aspectj.generic.CounterAspect"/>
<bean id="testBean" class="org.springframework.aop.aspectj.generic.AfterReturningGenericTypeMatchingTests$GenericReturnTypeVariationClass"/> <bean id="testBean" class="org.springframework.aop.aspectj.generic.GenericReturnTypeVariationClass"/>
</beans> </beans>

View File

@ -16,16 +16,19 @@
package org.springframework.aop.aspectj.generic; package org.springframework.aop.aspectj.generic;
import static org.junit.Assert.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.annotation.Pointcut;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.Employee; import org.springframework.beans.Employee;
import org.springframework.beans.TestBean; import org.springframework.beans.TestBean;
import org.springframework.test.AbstractDependencyInjectionSpringContextTests; import org.springframework.context.support.ClassPathXmlApplicationContext;
/** /**
* Tests ensuring that after-returning advice for generic parameters bound to * Tests ensuring that after-returning advice for generic parameters bound to
@ -34,29 +37,27 @@ import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
* <p>See SPR-3628 for more details. * <p>See SPR-3628 for more details.
* *
* @author Ramnivas Laddad * @author Ramnivas Laddad
* @author Chris Beams
*/ */
public class AfterReturningGenericTypeMatchingTests extends AbstractDependencyInjectionSpringContextTests { public final class AfterReturningGenericTypeMatchingTests {
protected GenericReturnTypeVariationClass testBean; private GenericReturnTypeVariationClass testBean;
protected CounterAspect counterAspect; private CounterAspect counterAspect;
public AfterReturningGenericTypeMatchingTests() { @Before
setPopulateProtectedVariables(true); public void setUp() {
} ClassPathXmlApplicationContext ctx =
new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-context.xml", getClass());
@Override counterAspect = (CounterAspect) ctx.getBean("counterAspect");
protected String getConfigPath() {
return "afterReturningGenericTypeMatchingTests-context.xml";
}
@Override
protected void onSetUp() throws Exception {
counterAspect.reset(); counterAspect.reset();
super.onSetUp();
testBean = (GenericReturnTypeVariationClass) ctx.getBean("testBean");
} }
@Test
public void testReturnTypeExactMatching() { public void testReturnTypeExactMatching() {
testBean.getStrings(); testBean.getStrings();
assertEquals(1, counterAspect.getStringsInvocationsCount); assertEquals(1, counterAspect.getStringsInvocationsCount);
@ -69,6 +70,7 @@ public class AfterReturningGenericTypeMatchingTests extends AbstractDependencyIn
assertEquals(1, counterAspect.getIntegersInvocationsCount); assertEquals(1, counterAspect.getIntegersInvocationsCount);
} }
@Test
public void testReturnTypeRawMatching() { public void testReturnTypeRawMatching() {
testBean.getStrings(); testBean.getStrings();
assertEquals(1, counterAspect.getRawsInvocationsCount); assertEquals(1, counterAspect.getRawsInvocationsCount);
@ -79,11 +81,13 @@ public class AfterReturningGenericTypeMatchingTests extends AbstractDependencyIn
assertEquals(1, counterAspect.getRawsInvocationsCount); assertEquals(1, counterAspect.getRawsInvocationsCount);
} }
@Test
public void testReturnTypeUpperBoundMatching() { public void testReturnTypeUpperBoundMatching() {
testBean.getIntegers(); testBean.getIntegers();
assertEquals(1, counterAspect.getNumbersInvocationsCount); assertEquals(1, counterAspect.getNumbersInvocationsCount);
} }
@Test
public void testReturnTypeLowerBoundMatching() { public void testReturnTypeLowerBoundMatching() {
testBean.getTestBeans(); testBean.getTestBeans();
assertEquals(1, counterAspect.getTestBeanInvocationsCount); assertEquals(1, counterAspect.getTestBeanInvocationsCount);
@ -94,8 +98,10 @@ public class AfterReturningGenericTypeMatchingTests extends AbstractDependencyIn
assertEquals(0, counterAspect.getTestBeanInvocationsCount); assertEquals(0, counterAspect.getTestBeanInvocationsCount);
} }
}
public static class GenericReturnTypeVariationClass {
class GenericReturnTypeVariationClass {
public Collection<String> getStrings() { public Collection<String> getStrings() {
return new ArrayList<String>(); return new ArrayList<String>();
@ -112,28 +118,28 @@ public class AfterReturningGenericTypeMatchingTests extends AbstractDependencyIn
public Collection<Employee> getEmployees() { public Collection<Employee> getEmployees() {
return new ArrayList<Employee>(); return new ArrayList<Employee>();
} }
} }
@Aspect @Aspect
public static class CounterAspect { class CounterAspect {
private int getRawsInvocationsCount; int getRawsInvocationsCount;
private int getStringsInvocationsCount; int getStringsInvocationsCount;
private int getIntegersInvocationsCount; int getIntegersInvocationsCount;
private int getNumbersInvocationsCount; int getNumbersInvocationsCount;
private int getTestBeanInvocationsCount; int getTestBeanInvocationsCount;
@Pointcut("execution(* org.springframework.aop.aspectj.generic.AfterReturningGenericTypeMatchingTests.GenericReturnTypeVariationClass.*(..))") @Pointcut("execution(* org.springframework.aop.aspectj.generic.GenericReturnTypeVariationClass.*(..))")
public void anyTestMethod() { public void anyTestMethod() {
} }
@AfterReturning(pointcut = "anyTestMethod()", returning = "ret") @AfterReturning(pointcut = "anyTestMethod()", returning = "ret")
public void incrementGetRawsInvocationsCount(Collection ret) { public void incrementGetRawsInvocationsCount(Collection<?> ret) {
getRawsInvocationsCount++; getRawsInvocationsCount++;
} }
@ -164,6 +170,5 @@ public class AfterReturningGenericTypeMatchingTests extends AbstractDependencyIn
getNumbersInvocationsCount = 0; getNumbersInvocationsCount = 0;
getTestBeanInvocationsCount = 0; getTestBeanInvocationsCount = 0;
} }
}
} }

View File

@ -7,8 +7,8 @@
<aop:aspectj-autoproxy proxy-target-class="true"/> <aop:aspectj-autoproxy proxy-target-class="true"/>
<bean id="counterAspect" class="org.springframework.aop.aspectj.generic.GenericBridgeMethodMatchingTests$CounterAspect"/> <bean id="counterAspect" class="org.springframework.aop.aspectj.generic.GenericCounterAspect"/>
<bean id="testBean" class="org.springframework.aop.aspectj.generic.GenericBridgeMethodMatchingTests$DerivedStringParameterizedClass"/> <bean id="testBean" class="org.springframework.aop.aspectj.generic.DerivedStringParameterizedClass"/>
</beans> </beans>

View File

@ -16,6 +16,10 @@
package org.springframework.aop.aspectj.generic; package org.springframework.aop.aspectj.generic;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
/** /**
* Tests for AspectJ pointcut expression matching when working with bridge methods. * Tests for AspectJ pointcut expression matching when working with bridge methods.
* *
@ -24,19 +28,17 @@ package org.springframework.aop.aspectj.generic;
* <p>See GenericBridgeMethodMatchingTests for more details. * <p>See GenericBridgeMethodMatchingTests for more details.
* *
* @author Ramnivas Laddad * @author Ramnivas Laddad
* @author Chris Beams
*/ */
public class GenericBridgeMethodMatchingClassProxyTests extends GenericBridgeMethodMatchingTests { public final class GenericBridgeMethodMatchingClassProxyTests extends GenericBridgeMethodMatchingTests {
@Override
protected String getConfigPath() {
return "genericBridgeMethodMatchingTests-classProxy-context.xml";
}
@Test
public void testGenericDerivedInterfaceMethodThroughClass() { public void testGenericDerivedInterfaceMethodThroughClass() {
((DerivedStringParameterizedClass) testBean).genericDerivedInterfaceMethod(""); ((DerivedStringParameterizedClass) testBean).genericDerivedInterfaceMethod("");
assertEquals(1, counterAspect.count); assertEquals(1, counterAspect.count);
} }
@Test
public void testGenericBaseInterfaceMethodThroughClass() { public void testGenericBaseInterfaceMethodThroughClass() {
((DerivedStringParameterizedClass) testBean).genericBaseInterfaceMethod(""); ((DerivedStringParameterizedClass) testBean).genericBaseInterfaceMethod("");
assertEquals(1, counterAspect.count); assertEquals(1, counterAspect.count);

View File

@ -7,8 +7,8 @@
<aop:aspectj-autoproxy proxy-target-class="false"/> <aop:aspectj-autoproxy proxy-target-class="false"/>
<bean id="counterAspect" class="org.springframework.aop.aspectj.generic.GenericBridgeMethodMatchingTests$CounterAspect"/> <bean id="counterAspect" class="org.springframework.aop.aspectj.generic.GenericCounterAspect"/>
<bean id="testBean" class="org.springframework.aop.aspectj.generic.GenericBridgeMethodMatchingTests$DerivedStringParameterizedClass"/> <bean id="testBean" class="org.springframework.aop.aspectj.generic.DerivedStringParameterizedClass"/>
</beans> </beans>

View File

@ -16,9 +16,12 @@
package org.springframework.aop.aspectj.generic; package org.springframework.aop.aspectj.generic;
import static org.junit.Assert.*;
import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Before;
import org.springframework.test.AbstractDependencyInjectionSpringContextTests; import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/** /**
* Tests for AspectJ pointcut expression matching when working with bridge methods. * Tests for AspectJ pointcut expression matching when working with bridge methods.
@ -33,63 +36,66 @@ import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
* See SPR-3556 for more details. * See SPR-3556 for more details.
* *
* @author Ramnivas Laddad * @author Ramnivas Laddad
* @author Chris Beams
*/ */
public class GenericBridgeMethodMatchingTests extends AbstractDependencyInjectionSpringContextTests { public class GenericBridgeMethodMatchingTests {
protected DerivedInterface<String> testBean; protected DerivedInterface<String> testBean;
protected CounterAspect counterAspect; protected GenericCounterAspect counterAspect;
public GenericBridgeMethodMatchingTests() { @SuppressWarnings("unchecked")
setPopulateProtectedVariables(true); @org.junit.Before
} public void setUp() {
ClassPathXmlApplicationContext ctx =
new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-context.xml", getClass());
@Override counterAspect = (GenericCounterAspect) ctx.getBean("counterAspect");
protected String getConfigPath() {
return "genericBridgeMethodMatchingTests-context.xml";
}
@Override
protected void onSetUp() throws Exception {
counterAspect.count = 0; counterAspect.count = 0;
super.onSetUp();
testBean = (DerivedInterface<String>) ctx.getBean("testBean");
} }
@Test
public void testGenericDerivedInterfaceMethodThroughInterface() { public void testGenericDerivedInterfaceMethodThroughInterface() {
testBean.genericDerivedInterfaceMethod(""); testBean.genericDerivedInterfaceMethod("");
assertEquals(1, counterAspect.count); assertEquals(1, counterAspect.count);
} }
@Test
public void testGenericBaseInterfaceMethodThroughInterface() { public void testGenericBaseInterfaceMethodThroughInterface() {
testBean.genericBaseInterfaceMethod(""); testBean.genericBaseInterfaceMethod("");
assertEquals(1, counterAspect.count); assertEquals(1, counterAspect.count);
} }
}
public interface BaseInterface<T> {
interface BaseInterface<T> {
void genericBaseInterfaceMethod(T t); void genericBaseInterfaceMethod(T t);
} }
public interface DerivedInterface<T> extends BaseInterface<T> { interface DerivedInterface<T> extends BaseInterface<T> {
public void genericDerivedInterfaceMethod(T t); public void genericDerivedInterfaceMethod(T t);
} }
public static class DerivedStringParameterizedClass implements DerivedInterface<String> { class DerivedStringParameterizedClass implements DerivedInterface<String> {
public void genericDerivedInterfaceMethod(String t) { public void genericDerivedInterfaceMethod(String t) {
} }
public void genericBaseInterfaceMethod(String t) { public void genericBaseInterfaceMethod(String t) {
} }
} }
@Aspect @Aspect
public static class CounterAspect { class GenericCounterAspect {
public int count; public int count;
@ -98,6 +104,5 @@ public class GenericBridgeMethodMatchingTests extends AbstractDependencyInjectio
count++; count++;
} }
}
} }

View File

@ -16,54 +16,56 @@
package org.springframework.aop.aspectj.generic; package org.springframework.aop.aspectj.generic;
import static org.junit.Assert.assertEquals;
import java.util.Collection; import java.util.Collection;
import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.annotation.Pointcut;
import org.junit.Test;
import org.springframework.test.AbstractDependencyInjectionSpringContextTests; import org.springframework.context.support.ClassPathXmlApplicationContext;
/** /**
* Tests that poitncut matching is correct with generic method parameter. * Tests that poitncut matching is correct with generic method parameter.
* See SPR-3904 for more details. * See SPR-3904 for more details.
* *
* @author Ramnivas Laddad * @author Ramnivas Laddad
* @author Chris Beams
*/ */
public class GenericParameterMatchingTests extends AbstractDependencyInjectionSpringContextTests { public final class GenericParameterMatchingTests {
protected CounterAspect counterAspect; private CounterAspect counterAspect;
protected GenericInterface<String> testBean; private GenericInterface<String> testBean;
public GenericParameterMatchingTests() { @SuppressWarnings("unchecked")
setPopulateProtectedVariables(true); @org.junit.Before
} public void setUp() {
ClassPathXmlApplicationContext ctx =
new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-context.xml", getClass());
@Override counterAspect = (CounterAspect) ctx.getBean("counterAspect");
protected String getConfigPath() {
return "genericParameterMatchingTests-context.xml";
}
@Override
protected void onSetUp() throws Exception {
counterAspect.reset(); counterAspect.reset();
super.onSetUp();
testBean = (GenericInterface<String>) ctx.getBean("testBean");
} }
@Test
public void testGenericInterfaceGenericArgExecution() { public void testGenericInterfaceGenericArgExecution() {
testBean.save(""); testBean.save("");
assertEquals(1, counterAspect.genericInterfaceGenericArgExecutionCount); assertEquals(1, counterAspect.genericInterfaceGenericArgExecutionCount);
} }
@Test
public void testGenericInterfaceGenericCollectionArgExecution() { public void testGenericInterfaceGenericCollectionArgExecution() {
testBean.saveAll(null); testBean.saveAll(null);
// TODO: uncomment once we officially update to AspectJ 1.6.0 assertEquals(1, counterAspect.genericInterfaceGenericCollectionArgExecutionCount);
//assertEquals(1, counterAspect.genericInterfaceGenericCollectionArgExecutionCount);
} }
@Test
public void testGenericInterfaceSubtypeGenericCollectionArgExecution() { public void testGenericInterfaceSubtypeGenericCollectionArgExecution() {
testBean.saveAll(null); testBean.saveAll(null);
assertEquals(1, counterAspect.genericInterfaceSubtypeGenericCollectionArgExecutionCount); assertEquals(1, counterAspect.genericInterfaceSubtypeGenericCollectionArgExecutionCount);
@ -89,7 +91,7 @@ public class GenericParameterMatchingTests extends AbstractDependencyInjectionSp
@Aspect @Aspect
public static class CounterAspect { static class CounterAspect {
int genericInterfaceGenericArgExecutionCount; int genericInterfaceGenericArgExecutionCount;
int genericInterfaceGenericCollectionArgExecutionCount; int genericInterfaceGenericCollectionArgExecutionCount;

View File

@ -15,9 +15,9 @@
</aop:config> </aop:config>
<bean id="getNameCounter" class="org.springframework.aop.framework.CountingBeforeAdvice"/> <bean id="getNameCounter" class="test.advice.CountingBeforeAdvice"/>
<bean id="getAgeCounter" class="org.springframework.aop.framework.CountingBeforeAdvice"/> <bean id="getAgeCounter" class="test.advice.CountingBeforeAdvice"/>
<bean id="testBean" class="org.springframework.beans.TestBean"/> <bean id="testBean" class="org.springframework.beans.TestBean"/>

View File

@ -16,9 +16,9 @@
</aop:aspect> </aop:aspect>
</aop:config> </aop:config>
<bean id="getNameCounter" class="org.springframework.aop.framework.CountingBeforeAdvice"/> <bean id="getNameCounter" class="test.advice.CountingBeforeAdvice"/>
<bean id="getAgeCounter" class="org.springframework.aop.framework.CountingBeforeAdvice"/> <bean id="getAgeCounter" class="test.advice.CountingBeforeAdvice"/>
<bean id="testBean" class="org.springframework.beans.TestBean"/> <bean id="testBean" class="org.springframework.beans.TestBean"/>

View File

@ -19,36 +19,25 @@ package org.springframework.aop.config;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.ITestBean;
import org.springframework.beans.factory.BeanDefinitionStoreException; import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.xml.sax.SAXParseException; import org.xml.sax.SAXParseException;
/** /**
* @author Adrian Colyer * @author Adrian Colyer
* @author Chris Beams
*/ */
public class AopNamespaceAdviceTypeTests { public final class AopNamespaceHandlerAdviceTypeTests {
private ApplicationContext context;
protected String getOKConfigLocation() {
return "org/springframework/aop/config/aopNamespaceHandlerAdviceTypeOKTests.xml";
}
protected String getErrorConfigLocation() {
return "org/springframework/aop/config/aopNamespaceHandlerAdviceTypeErrorTests.xml";
}
@Test @Test
public void testParsingOfAdviceTypes() { public void testParsingOfAdviceTypes() {
this.context = new ClassPathXmlApplicationContext(getOKConfigLocation()); new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-ok.xml", getClass());
} }
@Test @Test
public void testParsingOfAdviceTypesWithError() { public void testParsingOfAdviceTypesWithError() {
try { try {
this.context = new ClassPathXmlApplicationContext(getErrorConfigLocation()); new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-error.xml", getClass());
fail("Expected BeanDefinitionStoreException"); fail("Expected BeanDefinitionStoreException");
} }
catch (BeanDefinitionStoreException ex) { catch (BeanDefinitionStoreException ex) {
@ -56,8 +45,4 @@ public class AopNamespaceAdviceTypeTests {
} }
} }
protected ITestBean getTestBean() {
return (ITestBean) this.context.getBean("testBean");
}
} }

View File

@ -12,9 +12,9 @@
</aop:aspect> </aop:aspect>
</aop:config> </aop:config>
<bean id="getNameCounter" class="org.springframework.aop.framework.CountingBeforeAdvice"/> <bean id="getNameCounter" class="test.advice.CountingBeforeAdvice"/>
<bean id="getAgeCounter" class="org.springframework.aop.framework.CountingBeforeAdvice"/> <bean id="getAgeCounter" class="test.advice.CountingBeforeAdvice"/>
<bean id="testBean" class="org.springframework.beans.TestBean"/> <bean id="testBean" class="org.springframework.beans.TestBean"/>

View File

@ -12,9 +12,9 @@
</aop:aspect> </aop:aspect>
</aop:config> </aop:config>
<bean id="getNameCounter" class="org.springframework.aop.framework.CountingBeforeAdvice"/> <bean id="getNameCounter" class="test.advice.CountingBeforeAdvice"/>
<bean id="getAgeCounter" class="org.springframework.aop.framework.CountingBeforeAdvice"/> <bean id="getAgeCounter" class="test.advice.CountingBeforeAdvice"/>
<bean id="testBean" class="org.springframework.beans.TestBean"/> <bean id="testBean" class="org.springframework.beans.TestBean"/>

View File

@ -19,35 +19,24 @@ package org.springframework.aop.config;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.ITestBean;
import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.BeanCreationException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
/** /**
* @author Adrian Colyer * @author Adrian Colyer
* @author Chris Beams
*/ */
public class AopNamespaceHandlerArgNamesTests { public final class AopNamespaceHandlerArgNamesTests {
private ApplicationContext context;
protected String getOKConfigLocation() {
return "org/springframework/aop/config/aopNamespaceHandlerArgNamesOKTests.xml";
}
protected String getErrorConfigLocation() {
return "org/springframework/aop/config/aopNamespaceHandlerArgNamesErrorTests.xml";
}
@Test @Test
public void testArgNamesOK() { public void testArgNamesOK() {
this.context = new ClassPathXmlApplicationContext(getOKConfigLocation()); new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-ok.xml", getClass());
} }
@Test @Test
public void testArgNamesError() { public void testArgNamesError() {
try { try {
this.context = new ClassPathXmlApplicationContext(getErrorConfigLocation()); new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-error.xml", getClass());
fail("Expected BeanCreationException"); fail("Expected BeanCreationException");
} }
catch (BeanCreationException ex) { catch (BeanCreationException ex) {
@ -55,8 +44,4 @@ public class AopNamespaceHandlerArgNamesTests {
} }
} }
protected ITestBean getTestBean() {
return (ITestBean) this.context.getBean("testBean");
}
} }

View File

@ -19,9 +19,9 @@
</aop:config> </aop:config>
<bean id="getNameCounter" class="org.springframework.aop.framework.CountingBeforeAdvice"/> <bean id="getNameCounter" class="test.advice.CountingBeforeAdvice"/>
<bean id="getAgeCounter" class="org.springframework.aop.framework.CountingBeforeAdvice"/> <bean id="getAgeCounter" class="test.advice.CountingBeforeAdvice"/>
<bean id="testBean" class="org.springframework.beans.TestBean"/> <bean id="testBean" class="org.springframework.beans.TestBean"/>

View File

@ -24,8 +24,9 @@ import org.springframework.beans.ITestBean;
/** /**
* @author Rob Harrop * @author Rob Harrop
* @author Chris Beams
*/ */
public class AopNamespaceHandlerProxyTargetClassTests extends AopNamespaceHandlerTests { public final class AopNamespaceHandlerProxyTargetClassTests extends AopNamespaceHandlerTests {
@Test @Test
public void testIsClassProxy() { public void testIsClassProxy() {
@ -33,8 +34,4 @@ public class AopNamespaceHandlerProxyTargetClassTests extends AopNamespaceHandle
assertTrue("Should be a CGLIB proxy", AopUtils.isCglibProxy(bean)); assertTrue("Should be a CGLIB proxy", AopUtils.isCglibProxy(bean));
} }
protected String getConfigLocation() {
return "org/springframework/aop/config/aopNamespaceHandlerProxyTargetClassTests.xml";
}
} }

View File

@ -11,9 +11,9 @@
</aop:aspect> </aop:aspect>
</aop:config> </aop:config>
<bean id="getNameCounter" class="org.springframework.aop.framework.CountingBeforeAdvice"/> <bean id="getNameCounter" class="test.advice.CountingBeforeAdvice"/>
<bean id="getAgeCounter" class="org.springframework.aop.framework.CountingBeforeAdvice"/> <bean id="getAgeCounter" class="test.advice.CountingBeforeAdvice"/>
<bean id="testBean" class="org.springframework.beans.TestBean"/> <bean id="testBean" class="org.springframework.beans.TestBean"/>

View File

@ -11,9 +11,9 @@
</aop:aspect> </aop:aspect>
</aop:config> </aop:config>
<bean id="getNameCounter" class="org.springframework.aop.framework.CountingBeforeAdvice"/> <bean id="getNameCounter" class="test.advice.CountingBeforeAdvice"/>
<bean id="getAgeCounter" class="org.springframework.aop.framework.CountingBeforeAdvice"/> <bean id="getAgeCounter" class="test.advice.CountingBeforeAdvice"/>
<bean id="testBean" class="org.springframework.beans.TestBean"/> <bean id="testBean" class="org.springframework.beans.TestBean"/>

View File

@ -19,36 +19,25 @@ package org.springframework.aop.config;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.ITestBean;
import org.springframework.beans.factory.BeanDefinitionStoreException; import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.xml.sax.SAXParseException; import org.xml.sax.SAXParseException;
/** /**
* @author Adrian Colyer * @author Adrian Colyer
* @author Chris Beams
*/ */
public class AopNamespaceHandlerReturningTests { public final class AopNamespaceHandlerReturningTests {
private ApplicationContext context;
protected String getOKConfigLocation() {
return "org/springframework/aop/config/aopNamespaceHandlerReturningOKTests.xml";
}
protected String getErrorConfigLocation() {
return "org/springframework/aop/config/aopNamespaceHandlerReturningErrorTests.xml";
}
@Test @Test
public void testReturningOnReturningAdvice() { public void testReturningOnReturningAdvice() {
this.context = new ClassPathXmlApplicationContext(getOKConfigLocation()); new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-ok.xml", getClass());
} }
@Test @Test
public void testParseReturningOnOtherAdviceType() { public void testParseReturningOnOtherAdviceType() {
try { try {
this.context = new ClassPathXmlApplicationContext(getErrorConfigLocation()); new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-error.xml", getClass());
fail("Expected BeanDefinitionStoreException"); fail("Expected BeanDefinitionStoreException");
} }
catch (BeanDefinitionStoreException ex) { catch (BeanDefinitionStoreException ex) {
@ -56,8 +45,4 @@ public class AopNamespaceHandlerReturningTests {
} }
} }
protected ITestBean getTestBean() {
return (ITestBean) this.context.getBean("testBean");
}
} }

View File

@ -34,15 +34,16 @@ import org.springframework.web.context.support.XmlWebApplicationContext;
/** /**
* @author Rob Harrop * @author Rob Harrop
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Chris Beams
*/ */
public class AopNamespaceHandlerScopeTests { public final class AopNamespaceHandlerScopeTests {
private ApplicationContext context; private ApplicationContext context;
@Before @Before
public void setUp() { public void setUp() {
XmlWebApplicationContext wac = new XmlWebApplicationContext(); XmlWebApplicationContext wac = new XmlWebApplicationContext();
wac.setConfigLocations(new String[] {"classpath:org/springframework/aop/config/aopNamespaceHandlerScopeTests.xml"}); wac.setConfigLocations(new String[] {"classpath:org/springframework/aop/config/AopNamespaceHandlerScopeTests-context.xml"});
wac.refresh(); wac.refresh();
this.context = wac; this.context = wac;
} }

View File

@ -19,9 +19,9 @@
</aop:config> </aop:config>
<bean id="getNameCounter" class="org.springframework.aop.framework.CountingBeforeAdvice"/> <bean id="getNameCounter" class="test.advice.CountingBeforeAdvice"/>
<bean id="getAgeCounter" class="org.springframework.aop.framework.CountingBeforeAdvice"/> <bean id="getAgeCounter" class="test.advice.CountingBeforeAdvice"/>
<bean id="testBean" class="org.springframework.beans.TestBean"/> <bean id="testBean" class="org.springframework.beans.TestBean"/>

View File

@ -18,16 +18,18 @@ package org.springframework.aop.config;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import org.aspectj.lang.ProceedingJoinPoint;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.aop.Advisor; import org.springframework.aop.Advisor;
import org.springframework.aop.framework.Advised; import org.springframework.aop.framework.Advised;
import org.springframework.aop.framework.CountingBeforeAdvice;
import org.springframework.aop.support.AopUtils; import org.springframework.aop.support.AopUtils;
import org.springframework.beans.ITestBean; import org.springframework.beans.ITestBean;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
import test.advice.CountingBeforeAdvice;
/** /**
* @author Rob Harrop * @author Rob Harrop
*/ */
@ -35,13 +37,11 @@ public class AopNamespaceHandlerTests {
private ApplicationContext context; private ApplicationContext context;
@Before @Before
public void setUp() { public void setUp() {
this.context = new ClassPathXmlApplicationContext(getConfigLocation()); this.context =
} new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-context.xml", getClass());
protected String getConfigLocation() {
return "org/springframework/aop/config/aopNamespaceHandlerTests.xml";
} }
@Test @Test
@ -103,3 +103,51 @@ public class AopNamespaceHandlerTests {
} }
} }
class CountingAspectJAdvice {
private int beforeCount;
private int afterCount;
private int aroundCount;
public void myBeforeAdvice() throws Throwable {
this.beforeCount++;
}
public void myAfterAdvice() throws Throwable {
this.afterCount++;
}
public void myAroundAdvice(ProceedingJoinPoint pjp) throws Throwable {
this.aroundCount++;
pjp.proceed();
}
public void myAfterReturningAdvice(int age) {
this.afterCount++;
}
public void myAfterThrowingAdvice(RuntimeException ex) {
this.afterCount++;
}
public void mySetAgeAdvice(int newAge, ITestBean bean) {
// no-op
}
public int getBeforeCount() {
return this.beforeCount;
}
public int getAfterCount() {
return this.afterCount;
}
public int getAroundCount() {
return this.aroundCount;
}
}

View File

@ -11,9 +11,9 @@
</aop:aspect> </aop:aspect>
</aop:config> </aop:config>
<bean id="getNameCounter" class="org.springframework.aop.framework.CountingBeforeAdvice"/> <bean id="getNameCounter" class="test.advice.CountingBeforeAdvice"/>
<bean id="getAgeCounter" class="org.springframework.aop.framework.CountingBeforeAdvice"/> <bean id="getAgeCounter" class="test.advice.CountingBeforeAdvice"/>
<bean id="testBean" class="org.springframework.beans.TestBean"/> <bean id="testBean" class="org.springframework.beans.TestBean"/>

View File

@ -11,9 +11,9 @@
</aop:aspect> </aop:aspect>
</aop:config> </aop:config>
<bean id="getNameCounter" class="org.springframework.aop.framework.CountingBeforeAdvice"/> <bean id="getNameCounter" class="test.advice.CountingBeforeAdvice"/>
<bean id="getAgeCounter" class="org.springframework.aop.framework.CountingBeforeAdvice"/> <bean id="getAgeCounter" class="test.advice.CountingBeforeAdvice"/>
<bean id="testBean" class="org.springframework.beans.TestBean"/> <bean id="testBean" class="org.springframework.beans.TestBean"/>

View File

@ -19,36 +19,25 @@ package org.springframework.aop.config;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.ITestBean;
import org.springframework.beans.factory.BeanDefinitionStoreException; import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.xml.sax.SAXParseException; import org.xml.sax.SAXParseException;
/** /**
* @author Adrian Colyer * @author Adrian Colyer
* @author Chris Beams
*/ */
public class AopNamespaceHandlerThrowingTests { public final class AopNamespaceHandlerThrowingTests {
private ApplicationContext context;
protected String getOKConfigLocation() {
return "org/springframework/aop/config/aopNamespaceHandlerThrowingOKTests.xml";
}
protected String getErrorConfigLocation() {
return "org/springframework/aop/config/aopNamespaceHandlerThrowingErrorTests.xml";
}
@Test @Test
public void testThrowingOnThrowingAdvice() { public void testThrowingOnThrowingAdvice() {
this.context = new ClassPathXmlApplicationContext(getOKConfigLocation()); new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-ok.xml", getClass());
} }
@Test @Test
public void testParseThrowingOnOtherAdviceType() { public void testParseThrowingOnOtherAdviceType() {
try { try {
this.context = new ClassPathXmlApplicationContext(getErrorConfigLocation()); new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-error.xml", getClass());
fail("Expected BeanDefinitionStoreException"); fail("Expected BeanDefinitionStoreException");
} }
catch (BeanDefinitionStoreException ex) { catch (BeanDefinitionStoreException ex) {
@ -56,8 +45,4 @@ public class AopNamespaceHandlerThrowingTests {
} }
} }
protected ITestBean getTestBean() {
return (ITestBean) this.context.getBean("testBean");
}
} }

View File

@ -1,69 +0,0 @@
/*
* Copyright 2002-2005 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.aop.config;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.beans.ITestBean;
/**
* @author Rob Harrop
*/
public class CountingAspectJAdvice {
private int beforeCount;
private int afterCount;
private int aroundCount;
public void myBeforeAdvice() throws Throwable {
this.beforeCount++;
}
public void myAfterAdvice() throws Throwable {
this.afterCount++;
}
public void myAroundAdvice(ProceedingJoinPoint pjp) throws Throwable {
this.aroundCount++;
pjp.proceed();
}
public void myAfterReturningAdvice(int age) {
this.afterCount++;
}
public void myAfterThrowingAdvice(RuntimeException ex) {
this.afterCount++;
}
public void mySetAgeAdvice(int newAge, ITestBean bean) {
// no-op
}
public int getBeforeCount() {
return this.beforeCount;
}
public int getAfterCount() {
return this.afterCount;
}
public int getAroundCount() {
return this.aroundCount;
}
}

View File

@ -16,128 +16,100 @@
package org.springframework.aop.config; package org.springframework.aop.config;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import org.easymock.MockControl; import org.junit.After;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactory;
import org.springframework.mock.easymock.AbstractScalarMockTemplate;
import org.springframework.test.AssertThrows;
/** /**
* @author Rick Evans * @author Rick Evans
* @author Chris Beams
*/ */
public final class MethodLocatingFactoryBeanTests { public final class MethodLocatingFactoryBeanTests {
private static final String BEAN_NAME = "string"; private static final String BEAN_NAME = "string";
private MethodLocatingFactoryBean factory;
private BeanFactory beanFactory;
@Before
public void setUp() {
factory = new MethodLocatingFactoryBean();
// methods must set up expectations and call replay() manually for this mock
beanFactory = createMock(BeanFactory.class);
}
@After
public void tearDown() {
verify(beanFactory);
}
@Test @Test
public void testIsSingleton() throws Exception { public void testIsSingleton() {
MethodLocatingFactoryBean factory = new MethodLocatingFactoryBean(); replay(beanFactory);
assertTrue(factory.isSingleton()); assertTrue(factory.isSingleton());
} }
@Test @Test
public void testGetObjectType() throws Exception { public void testGetObjectType() {
MethodLocatingFactoryBean factory = new MethodLocatingFactoryBean(); replay(beanFactory);
assertEquals(Method.class, factory.getObjectType()); assertEquals(Method.class, factory.getObjectType());
} }
@Test @Test(expected=IllegalArgumentException.class)
public void testWithNullTargetBeanName() throws Exception { public void testWithNullTargetBeanName() {
new BeanFactoryScalarMockTemplate() { replay(beanFactory);
public void doTestInternal(final BeanFactory beanFactory) throws Exception {
new AssertThrows(IllegalArgumentException.class) {
public void test() throws Exception {
MethodLocatingFactoryBean factory = new MethodLocatingFactoryBean();
factory.setMethodName("toString()"); factory.setMethodName("toString()");
factory.setBeanFactory(beanFactory); factory.setBeanFactory(beanFactory);
} }
}.runTest();
}
}.test();
}
@Test @Test(expected=IllegalArgumentException.class)
public void testWithEmptyTargetBeanName() throws Exception { public void testWithEmptyTargetBeanName() {
new BeanFactoryScalarMockTemplate() { replay(beanFactory);
public void doTestInternal(final BeanFactory beanFactory) throws Exception {
new AssertThrows(IllegalArgumentException.class) {
public void test() throws Exception {
MethodLocatingFactoryBean factory = new MethodLocatingFactoryBean();
factory.setTargetBeanName(""); factory.setTargetBeanName("");
factory.setMethodName("toString()"); factory.setMethodName("toString()");
factory.setBeanFactory(beanFactory); factory.setBeanFactory(beanFactory);
} }
}.runTest();
}
}.test();
}
@Test @Test(expected=IllegalArgumentException.class)
public void testWithNullTargetMethodName() throws Exception { public void testWithNullTargetMethodName() {
new BeanFactoryScalarMockTemplate() { replay(beanFactory);
public void doTestInternal(final BeanFactory beanFactory) throws Exception {
new AssertThrows(IllegalArgumentException.class) {
public void test() throws Exception {
MethodLocatingFactoryBean factory = new MethodLocatingFactoryBean();
factory.setTargetBeanName(BEAN_NAME); factory.setTargetBeanName(BEAN_NAME);
factory.setBeanFactory(beanFactory); factory.setBeanFactory(beanFactory);
} }
}.runTest();
}
}.test();
}
@Test @Test(expected=IllegalArgumentException.class)
public void testWithEmptyTargetMethodName() throws Exception { public void testWithEmptyTargetMethodName() {
new BeanFactoryScalarMockTemplate() { replay(beanFactory);
public void doTestInternal(final BeanFactory beanFactory) throws Exception {
new AssertThrows(IllegalArgumentException.class) {
public void test() throws Exception {
MethodLocatingFactoryBean factory = new MethodLocatingFactoryBean();
factory.setTargetBeanName(BEAN_NAME); factory.setTargetBeanName(BEAN_NAME);
factory.setMethodName(""); factory.setMethodName("");
factory.setBeanFactory(beanFactory); factory.setBeanFactory(beanFactory);
} }
}.runTest();
}
}.test();
}
@Test @Test(expected=IllegalArgumentException.class)
public void testWhenTargetBeanClassCannotBeResolved() throws Exception { public void testWhenTargetBeanClassCannotBeResolved() {
new BeanFactoryScalarMockTemplate() { expect(beanFactory.getType(BEAN_NAME)).andReturn(null);
protected void setupBeanFactoryExpectations(MockControl mockControl, BeanFactory beanFactory) throws Exception { replay(beanFactory);
beanFactory.getType(BEAN_NAME);
mockControl.setReturnValue(null);
}
protected void doTestInternal(final BeanFactory beanFactory) throws Exception {
new AssertThrows(IllegalArgumentException.class) {
public void test() throws Exception {
MethodLocatingFactoryBean factory = new MethodLocatingFactoryBean();
factory.setTargetBeanName(BEAN_NAME); factory.setTargetBeanName(BEAN_NAME);
factory.setMethodName("toString()"); factory.setMethodName("toString()");
factory.setBeanFactory(beanFactory); factory.setBeanFactory(beanFactory);
} }
}.runTest();
}
}.test();
}
@Test @Test
public void testSunnyDayPath() throws Exception { public void testSunnyDayPath() throws Exception {
new BeanFactoryScalarMockTemplate() { expect(beanFactory.getType(BEAN_NAME)).andReturn(String.class);
protected void setupBeanFactoryExpectations(MockControl mockControl, BeanFactory beanFactory) throws Exception { replay(beanFactory);
beanFactory.getType(BEAN_NAME);
mockControl.setReturnValue(String.class);
}
protected void doTestInternal(final BeanFactory beanFactory) throws Exception {
MethodLocatingFactoryBean factory = new MethodLocatingFactoryBean();
factory.setTargetBeanName(BEAN_NAME); factory.setTargetBeanName(BEAN_NAME);
factory.setMethodName("toString()"); factory.setMethodName("toString()");
factory.setBeanFactory(beanFactory); factory.setBeanFactory(beanFactory);
@ -147,48 +119,15 @@ public final class MethodLocatingFactoryBeanTests {
Method method = (Method) result; Method method = (Method) result;
assertEquals("Bingo", method.invoke("Bingo", new Object[]{})); assertEquals("Bingo", method.invoke("Bingo", new Object[]{}));
} }
}.test();
}
@Test @Test(expected=IllegalArgumentException.class)
public void testWhereMethodCannotBeResolved() throws Exception { public void testWhereMethodCannotBeResolved() {
new BeanFactoryScalarMockTemplate() { expect(beanFactory.getType(BEAN_NAME)).andReturn(String.class);
protected void setupBeanFactoryExpectations(MockControl mockControl, BeanFactory beanFactory) throws Exception { replay(beanFactory);
beanFactory.getType(BEAN_NAME);
mockControl.setReturnValue(String.class);
}
protected void doTestInternal(final BeanFactory beanFactory) throws Exception {
new AssertThrows(IllegalArgumentException.class) {
public void test() throws Exception {
MethodLocatingFactoryBean factory = new MethodLocatingFactoryBean();
factory.setTargetBeanName(BEAN_NAME); factory.setTargetBeanName(BEAN_NAME);
factory.setMethodName("loadOfOld()"); factory.setMethodName("loadOfOld()");
factory.setBeanFactory(beanFactory); factory.setBeanFactory(beanFactory);
} }
}.runTest();
}
}.test();
}
private static abstract class BeanFactoryScalarMockTemplate extends AbstractScalarMockTemplate {
public BeanFactoryScalarMockTemplate() {
super(BeanFactory.class);
}
public void setupExpectations(MockControl mockControl, Object mockObject) throws Exception {
setupBeanFactoryExpectations(mockControl, (BeanFactory) mockObject);
}
public void doTest(Object mockObject) throws Exception {
doTestInternal((BeanFactory) mockObject);
}
protected void setupBeanFactoryExpectations(MockControl mockControl, BeanFactory beanFactory) throws Exception {
}
protected abstract void doTestInternal(final BeanFactory beanFactory) throws Exception;
}
} }

View File

@ -21,12 +21,13 @@ import org.springframework.context.support.ClassPathXmlApplicationContext;
/** /**
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Chris Beams
*/ */
public class PrototypeProxyTests { public final class PrototypeProxyTests {
@Test @Test
public void testInjectionBeforeWrappingCheckDoesNotKickInForPrototypeProxy() { public void testInjectionBeforeWrappingCheckDoesNotKickInForPrototypeProxy() {
new ClassPathXmlApplicationContext("prototypeProxy.xml", getClass()); new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-context.xml", getClass());
} }
} }

View File

@ -31,6 +31,8 @@ import java.util.Map;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.transaction.TransactionRequiredException; import javax.transaction.TransactionRequiredException;
import junit.framework.TestCase;
import org.aopalliance.aop.Advice; import org.aopalliance.aop.Advice;
import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation; import org.aopalliance.intercept.MethodInvocation;
@ -42,10 +44,9 @@ import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.DynamicIntroductionAdvice; import org.springframework.aop.DynamicIntroductionAdvice;
import org.springframework.aop.MethodBeforeAdvice; import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.TargetSource; import org.springframework.aop.TargetSource;
import org.springframework.aop.ThrowsAdvice;
import org.springframework.aop.interceptor.DebugInterceptor; import org.springframework.aop.interceptor.DebugInterceptor;
import org.springframework.aop.interceptor.ExposeInvocationInterceptor; import org.springframework.aop.interceptor.ExposeInvocationInterceptor;
import org.springframework.aop.interceptor.NopInterceptor;
import org.springframework.aop.interceptor.SerializableNopInterceptor;
import org.springframework.aop.support.AopUtils; import org.springframework.aop.support.AopUtils;
import org.springframework.aop.support.DefaultIntroductionAdvisor; import org.springframework.aop.support.DefaultIntroductionAdvisor;
import org.springframework.aop.support.DefaultPointcutAdvisor; import org.springframework.aop.support.DefaultPointcutAdvisor;
@ -61,10 +62,24 @@ import org.springframework.beans.ITestBean;
import org.springframework.beans.Person; import org.springframework.beans.Person;
import org.springframework.beans.SerializablePerson; import org.springframework.beans.SerializablePerson;
import org.springframework.beans.TestBean; import org.springframework.beans.TestBean;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.CannotGetJdbcConnectionException; import org.springframework.jdbc.CannotGetJdbcConnectionException;
import org.springframework.util.SerializationTestUtils; import org.springframework.util.SerializationTestUtils;
import org.springframework.util.StopWatch; import org.springframework.util.StopWatch;
import test.advice.CountingAfterReturningAdvice;
import test.advice.CountingBeforeAdvice;
import test.advice.MethodCounter;
import test.advice.MyThrowsHandler;
import test.interceptor.NopInterceptor;
import test.interceptor.SerializableNopInterceptor;
import test.interceptor.TimestampIntroductionInterceptor;
import test.mixin.LockMixin;
import test.mixin.LockMixinAdvisor;
import test.mixin.Lockable;
import test.mixin.LockedException;
import test.util.TimeStamped;
/** /**
* @author Rod Johnson * @author Rod Johnson
* @author Juergen Hoeller * @author Juergen Hoeller
@ -804,7 +819,7 @@ public abstract class AbstractAopProxyTests {
@SuppressWarnings("serial") @SuppressWarnings("serial")
class MyDi extends DelegatingIntroductionInterceptor implements TimeStamped { class MyDi extends DelegatingIntroductionInterceptor implements TimeStamped {
/** /**
* @see org.springframework.aop.framework.TimeStamped#getTimeStamp() * @see test.util.TimeStamped#getTimeStamp()
*/ */
public long getTimeStamp() { public long getTimeStamp() {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
@ -1904,4 +1919,130 @@ public abstract class AbstractAopProxyTests {
} }
} }
@SuppressWarnings("serial")
public static class CountingMultiAdvice extends MethodCounter implements MethodBeforeAdvice,
AfterReturningAdvice, ThrowsAdvice {
public void before(Method m, Object[] args, Object target) throws Throwable {
count(m);
}
public void afterReturning(Object o, Method m, Object[] args, Object target)
throws Throwable {
count(m);
}
public void afterThrowing(ServletException sex) throws Throwable {
count(ServletException.class.getName());
}
public void afterThrowing(DataAccessException ex) throws Throwable {
count(DataAccessException.class.getName());
}
}
@SuppressWarnings("serial")
public static class CountingThrowsAdvice extends MethodCounter implements ThrowsAdvice {
public void afterThrowing(ServletException sex) throws Throwable {
count(ServletException.class.getName());
}
public void afterThrowing(DataAccessException ex) throws Throwable {
count(DataAccessException.class.getName());
}
}
static class MockTargetSource implements TargetSource {
private Object target;
public int gets;
public int releases;
public void reset() {
this.target = null;
gets = releases = 0;
}
public void setTarget(Object target) {
this.target = target;
}
/**
* @see org.springframework.aop.TargetSource#getTargetClass()
*/
public Class<?> getTargetClass() {
return target.getClass();
}
/**
* @see org.springframework.aop.TargetSource#getTarget()
*/
public Object getTarget() throws Exception {
++gets;
return target;
}
/**
* @see org.springframework.aop.TargetSource#releaseTarget(java.lang.Object)
*/
public void releaseTarget(Object pTarget) throws Exception {
if (pTarget != this.target)
throw new RuntimeException("Released wrong target");
++releases;
}
/**
* Check that gets and releases match
*
*/
public void verify() {
if (gets != releases)
throw new RuntimeException("Expectation failed: " + gets + " gets and " + releases + " releases");
}
/**
* @see org.springframework.aop.TargetSource#isStatic()
*/
public boolean isStatic() {
return false;
}
}
static abstract class ExposedInvocationTestBean extends TestBean {
public String getName() {
MethodInvocation invocation = ExposeInvocationInterceptor.currentInvocation();
assertions(invocation);
return super.getName();
}
public void absquatulate() {
MethodInvocation invocation = ExposeInvocationInterceptor.currentInvocation();
assertions(invocation);
super.absquatulate();
}
protected abstract void assertions(MethodInvocation invocation);
}
static class InvocationCheckExposedInvocationTestBean extends ExposedInvocationTestBean {
protected void assertions(MethodInvocation invocation) {
TestCase.assertTrue(invocation.getThis() == this);
TestCase.assertTrue("Invocation should be on ITestBean: " + invocation.getMethod(),
ITestBean.class.isAssignableFrom(invocation.getMethod().getDeclaringClass()));
}
}
} }

View File

@ -23,12 +23,12 @@ import java.io.Serializable;
import net.sf.cglib.core.CodeGenerationException; import net.sf.cglib.core.CodeGenerationException;
import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.junit.Test; import org.junit.Test;
import org.springframework.aop.ClassFilter; import org.springframework.aop.ClassFilter;
import org.springframework.aop.MethodMatcher; import org.springframework.aop.MethodMatcher;
import org.springframework.aop.Pointcut; import org.springframework.aop.Pointcut;
import org.springframework.aop.interceptor.NopInterceptor;
import org.springframework.aop.support.AopUtils; import org.springframework.aop.support.AopUtils;
import org.springframework.aop.support.DefaultPointcutAdvisor; import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.beans.ITestBean; import org.springframework.beans.ITestBean;
@ -37,6 +37,10 @@ import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextException; import org.springframework.context.ApplicationContextException;
import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
import test.advice.CountingBeforeAdvice;
import test.interceptor.NopInterceptor;
import test.mixin.LockMixinAdvisor;
/** /**
* Additional and overridden tests for the CGLIB proxy. * Additional and overridden tests for the CGLIB proxy.
* *
@ -46,7 +50,11 @@ import org.springframework.context.support.ClassPathXmlApplicationContext;
* @author Ramnivas Laddad * @author Ramnivas Laddad
* @author Chris Beams * @author Chris Beams
*/ */
public class CglibProxyTests extends AbstractAopProxyTests { @SuppressWarnings("serial")
public final class CglibProxyTests extends AbstractAopProxyTests implements Serializable {
private static final String DEPENDENCY_CHECK_CONTEXT = CglibProxyTests.class.getSimpleName() + "-with-dependency-checking.xml";
protected Object createProxy(ProxyCreatorSupport as) { protected Object createProxy(ProxyCreatorSupport as) {
as.setProxyTargetClass(true); as.setProxyTargetClass(true);
@ -330,7 +338,7 @@ public class CglibProxyTests extends AbstractAopProxyTests {
@Test @Test
public void testWithDependencyChecking() { public void testWithDependencyChecking() {
ApplicationContext ctx = ApplicationContext ctx =
new ClassPathXmlApplicationContext("org/springframework/aop/framework/withDependencyChecking.xml"); new ClassPathXmlApplicationContext(DEPENDENCY_CHECK_CONTEXT, getClass());
ctx.getBean("testBean"); ctx.getBean("testBean");
} }
@ -429,3 +437,61 @@ public class CglibProxyTests extends AbstractAopProxyTests {
} }
} }
class CglibTestBean {
private String name;
public CglibTestBean() {
setName("Some Default");
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}
class NoArgCtorTestBean {
private boolean called = false;
public NoArgCtorTestBean(String x, int y) {
called = true;
}
public boolean wasCalled() {
return called;
}
public void reset() {
called = false;
}
}
class ProtectedMethodTestBean {
protected String getString() {
return "foo";
}
}
class UnsupportedInterceptor implements MethodInterceptor {
/**
* @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation)
*/
public Object invoke(MethodInvocation mi) throws Throwable {
throw new UnsupportedOperationException(mi.getMethod().getName());
}
}

View File

@ -1,37 +0,0 @@
/*
* Copyright 2002-2005 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.aop.framework;
/**
* @author Rob Harrop
*/
public class CglibTestBean {
private String name;
public CglibTestBean() {
setName("Some Default");
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}

View File

@ -1,53 +0,0 @@
/*
* Copyright 2002-2005 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.aop.framework;
import java.lang.reflect.Method;
import javax.servlet.ServletException;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.ThrowsAdvice;
import org.springframework.dao.DataAccessException;
/**
* Advice object that implements <i>multiple</i> Advice interfaces.
*
* @author Juergen Hoeller
* @since 19.05.2005
*/
public class CountingMultiAdvice extends MethodCounter
implements MethodBeforeAdvice, AfterReturningAdvice, ThrowsAdvice {
public void before(Method m, Object[] args, Object target) throws Throwable {
count(m);
}
public void afterReturning(Object o, Method m, Object[] args, Object target) throws Throwable {
count(m);
}
public void afterThrowing(ServletException sex) throws Throwable {
count(ServletException.class.getName());
}
public void afterThrowing(DataAccessException ex) throws Throwable {
count(DataAccessException.class.getName());
}
}

View File

@ -1,37 +0,0 @@
/*
* Copyright 2002-2005 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.aop.framework;
import javax.servlet.ServletException;
import org.springframework.aop.ThrowsAdvice;
import org.springframework.dao.DataAccessException;
/**
* @author Rod Johnson
*/
public class CountingThrowsAdvice extends MethodCounter implements ThrowsAdvice {
public void afterThrowing(ServletException sex) throws Throwable {
count(ServletException.class.getName());
}
public void afterThrowing(DataAccessException ex) throws Throwable {
count(DataAccessException.class.getName());
}
}

View File

@ -1,20 +0,0 @@
/**
*
*/
package org.springframework.aop.framework;
class Echo implements IEcho {
private int a;
public int echoException(int i, Throwable t) throws Throwable {
if (t != null)
throw t;
return i;
}
public void setA(int a) {
this.a = a;
}
public int getA() {
return a;
}
}

View File

@ -1,43 +0,0 @@
/*
* Copyright 2002-2005 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.aop.framework;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.interceptor.ExposeInvocationInterceptor;
import org.springframework.beans.TestBean;
/**
* Used to test ExposeInvocationInterceptor.
* @author Rod Johnson
*/
public abstract class ExposedInvocationTestBean extends TestBean {
public String getName() {
MethodInvocation invocation = ExposeInvocationInterceptor.currentInvocation();
assertions(invocation);
return super.getName();
}
public void absquatulate() {
MethodInvocation invocation = ExposeInvocationInterceptor.currentInvocation();
assertions(invocation);
super.absquatulate();
}
protected abstract void assertions(MethodInvocation invocation);
}

Some files were not shown because too many files have changed in this diff Show More