Browse Source
* Changes to ASpectJExpressionPointcut plus some tests in Spring AOP * plus some tests in groovy support git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@4622 50f2f4bb-b051-0410-bef5-90022cba6387pull/1/merge
14 changed files with 685 additions and 25 deletions
@ -0,0 +1,174 @@ |
|||||||
|
package org.springframework.aop.aspectj; |
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals; |
||||||
|
import static org.junit.Assert.fail; |
||||||
|
|
||||||
|
import java.lang.annotation.Documented; |
||||||
|
import java.lang.annotation.ElementType; |
||||||
|
import java.lang.annotation.Inherited; |
||||||
|
import java.lang.annotation.Retention; |
||||||
|
import java.lang.annotation.RetentionPolicy; |
||||||
|
import java.lang.annotation.Target; |
||||||
|
import java.lang.reflect.Method; |
||||||
|
|
||||||
|
import org.junit.Test; |
||||||
|
import org.springframework.aop.Advisor; |
||||||
|
import org.springframework.aop.MethodBeforeAdvice; |
||||||
|
import org.springframework.aop.ThrowsAdvice; |
||||||
|
import org.springframework.aop.framework.ProxyFactory; |
||||||
|
import org.springframework.aop.support.DefaultPointcutAdvisor; |
||||||
|
import org.springframework.core.OverridingClassLoader; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author Dave Syer |
||||||
|
*/ |
||||||
|
public class TrickyAspectJPointcutExpressionTests { |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testManualProxyJavaWithUnconditionalPointcut() throws Exception { |
||||||
|
TestService target = new TestServiceImpl(); |
||||||
|
LogUserAdvice logAdvice = new LogUserAdvice(); |
||||||
|
testAdvice(new DefaultPointcutAdvisor(logAdvice), logAdvice, target, "TestServiceImpl"); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testManualProxyJavaWithStaticPointcut() throws Exception { |
||||||
|
TestService target = new TestServiceImpl(); |
||||||
|
LogUserAdvice logAdvice = new LogUserAdvice(); |
||||||
|
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut(); |
||||||
|
pointcut.setExpression(String.format("execution(* %s.TestService.*(..))", getClass().getName())); |
||||||
|
testAdvice(new DefaultPointcutAdvisor(pointcut, logAdvice), logAdvice, target, "TestServiceImpl"); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testManualProxyJavaWithDynamicPointcut() throws Exception { |
||||||
|
TestService target = new TestServiceImpl(); |
||||||
|
LogUserAdvice logAdvice = new LogUserAdvice(); |
||||||
|
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut(); |
||||||
|
pointcut.setExpression(String.format("@within(%s.Log)", getClass().getName())); |
||||||
|
testAdvice(new DefaultPointcutAdvisor(pointcut, logAdvice), logAdvice, target, "TestServiceImpl"); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testManualProxyJavaWithDynamicPointcutAndProxyTargetClass() throws Exception { |
||||||
|
TestService target = new TestServiceImpl(); |
||||||
|
LogUserAdvice logAdvice = new LogUserAdvice(); |
||||||
|
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut(); |
||||||
|
pointcut.setExpression(String.format("@within(%s.Log)", getClass().getName())); |
||||||
|
testAdvice(new DefaultPointcutAdvisor(pointcut, logAdvice), logAdvice, target, "TestServiceImpl", true); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testManualProxyJavaWithStaticPointcutAndTwoClassLoaders() throws Exception { |
||||||
|
|
||||||
|
LogUserAdvice logAdvice = new LogUserAdvice(); |
||||||
|
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut(); |
||||||
|
pointcut.setExpression(String.format("execution(* %s.TestService.*(..))", getClass().getName())); |
||||||
|
|
||||||
|
// Test with default class loader first...
|
||||||
|
testAdvice(new DefaultPointcutAdvisor(pointcut, logAdvice), logAdvice, new TestServiceImpl(), "TestServiceImpl"); |
||||||
|
|
||||||
|
// Then try again with a different class loader on the target...
|
||||||
|
SimpleThrowawayClassLoader loader = new SimpleThrowawayClassLoader(new TestServiceImpl().getClass().getClassLoader()); |
||||||
|
// Make sure the interface is loaded from the parent class loader
|
||||||
|
loader.excludeClass(TestService.class.getName()); |
||||||
|
loader.excludeClass(TestException.class.getName()); |
||||||
|
TestService other = (TestService) loader.loadClass(TestServiceImpl.class.getName()).newInstance(); |
||||||
|
testAdvice(new DefaultPointcutAdvisor(pointcut, logAdvice), logAdvice, other, "TestServiceImpl"); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
private void testAdvice(Advisor advisor, LogUserAdvice logAdvice, TestService target, String message) |
||||||
|
throws Exception { |
||||||
|
testAdvice(advisor, logAdvice, target, message, false); |
||||||
|
} |
||||||
|
|
||||||
|
private void testAdvice(Advisor advisor, LogUserAdvice logAdvice, TestService target, String message, |
||||||
|
boolean proxyTargetClass) throws Exception { |
||||||
|
|
||||||
|
logAdvice.reset(); |
||||||
|
|
||||||
|
ProxyFactory factory = new ProxyFactory(target); |
||||||
|
factory.setProxyTargetClass(proxyTargetClass); |
||||||
|
factory.addAdvisor(advisor); |
||||||
|
TestService bean = (TestService) factory.getProxy(); |
||||||
|
|
||||||
|
assertEquals(0, logAdvice.getCountThrows()); |
||||||
|
try { |
||||||
|
bean.sayHello(); |
||||||
|
fail("Expected exception"); |
||||||
|
} catch (TestException e) { |
||||||
|
assertEquals(message, e.getMessage()); |
||||||
|
} |
||||||
|
assertEquals(1, logAdvice.getCountThrows()); |
||||||
|
} |
||||||
|
|
||||||
|
public static class SimpleThrowawayClassLoader extends OverridingClassLoader { |
||||||
|
|
||||||
|
/** |
||||||
|
* Create a new SimpleThrowawayClassLoader for the given class loader. |
||||||
|
* @param parent the ClassLoader to build a throwaway ClassLoader for |
||||||
|
*/ |
||||||
|
public SimpleThrowawayClassLoader(ClassLoader parent) { |
||||||
|
super(parent); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public static class TestException extends RuntimeException { |
||||||
|
|
||||||
|
public TestException(String string) { |
||||||
|
super(string); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Target({ ElementType.METHOD, ElementType.TYPE }) |
||||||
|
@Retention(RetentionPolicy.RUNTIME) |
||||||
|
@Documented |
||||||
|
@Inherited |
||||||
|
public static @interface Log { |
||||||
|
} |
||||||
|
|
||||||
|
public static interface TestService { |
||||||
|
public String sayHello(); |
||||||
|
} |
||||||
|
|
||||||
|
@Log |
||||||
|
public static class TestServiceImpl implements TestService{ |
||||||
|
public String sayHello() { |
||||||
|
throw new TestException("TestServiceImpl"); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public class LogUserAdvice implements MethodBeforeAdvice, ThrowsAdvice { |
||||||
|
|
||||||
|
private int countBefore = 0; |
||||||
|
|
||||||
|
private int countThrows = 0; |
||||||
|
|
||||||
|
public void before(Method method, Object[] objects, Object o) throws Throwable { |
||||||
|
countBefore++; |
||||||
|
} |
||||||
|
|
||||||
|
public void afterThrowing(Exception e) throws Throwable { |
||||||
|
countThrows++; |
||||||
|
throw e; |
||||||
|
} |
||||||
|
|
||||||
|
public int getCountBefore() { |
||||||
|
return countBefore; |
||||||
|
} |
||||||
|
|
||||||
|
public int getCountThrows() { |
||||||
|
return countThrows; |
||||||
|
} |
||||||
|
|
||||||
|
public void reset() { |
||||||
|
countThrows = 0; |
||||||
|
countBefore = 0; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,21 @@ |
|||||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||||
|
<beans xmlns="http://www.springframework.org/schema/beans" |
||||||
|
xmlns:lang="http://www.springframework.org/schema/lang" |
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||||
|
xmlns:aop="http://www.springframework.org/schema/aop" |
||||||
|
xmlns:p="http://www.springframework.org/schema/p" |
||||||
|
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" |
||||||
|
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd |
||||||
|
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd |
||||||
|
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> |
||||||
|
|
||||||
|
<aop:config > |
||||||
|
<aop:advisor id="logUserAdvisor" pointcut="@within(org.springframework.scripting.groovy.Log)" advice-ref="logUserAdvice"/> |
||||||
|
</aop:config> |
||||||
|
|
||||||
|
<bean id="logUserAdvice" class="org.springframework.scripting.groovy.LogUserAdvice" /> |
||||||
|
|
||||||
|
<!-- N.B. the pointcut never matches if refresh-delay is set (because proxy-target-class is always false) --> |
||||||
|
<lang:groovy id="groovyBean" script-source="classpath:/org/springframework/scripting/groovy/GroovyServiceImpl.grv" refresh-check-delay="1000"/> |
||||||
|
|
||||||
|
</beans> |
||||||
@ -0,0 +1,20 @@ |
|||||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||||
|
<beans xmlns="http://www.springframework.org/schema/beans" |
||||||
|
xmlns:lang="http://www.springframework.org/schema/lang" |
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||||
|
xmlns:aop="http://www.springframework.org/schema/aop" |
||||||
|
xmlns:p="http://www.springframework.org/schema/p" |
||||||
|
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" |
||||||
|
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd |
||||||
|
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd |
||||||
|
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> |
||||||
|
|
||||||
|
<aop:config > |
||||||
|
<aop:advisor id="logUserAdvisor" pointcut="@within(org.springframework.scripting.groovy.Log)" advice-ref="logUserAdvice"/> |
||||||
|
</aop:config> |
||||||
|
|
||||||
|
<bean id="logUserAdvice" class="org.springframework.scripting.groovy.LogUserAdvice" /> |
||||||
|
|
||||||
|
<lang:groovy id="groovyBean" script-source="classpath:/org/springframework/scripting/groovy/GroovyServiceImpl.grv"/> |
||||||
|
|
||||||
|
</beans> |
||||||
@ -0,0 +1,20 @@ |
|||||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||||
|
<beans xmlns="http://www.springframework.org/schema/beans" |
||||||
|
xmlns:lang="http://www.springframework.org/schema/lang" |
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||||
|
xmlns:aop="http://www.springframework.org/schema/aop" |
||||||
|
xmlns:p="http://www.springframework.org/schema/p" |
||||||
|
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" |
||||||
|
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd |
||||||
|
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd |
||||||
|
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> |
||||||
|
|
||||||
|
<aop:config > |
||||||
|
<aop:advisor id="logUserAdvisor" pointcut="@within(org.springframework.scripting.groovy.Log)" advice-ref="logUserAdvice"/> |
||||||
|
</aop:config> |
||||||
|
|
||||||
|
<bean id="logUserAdvice" class="org.springframework.scripting.groovy.LogUserAdvice" /> |
||||||
|
|
||||||
|
<lang:groovy id="groovyBean" script-source="classpath:/org/springframework/scripting/groovy/GroovyServiceImpl.grv" refresh-check-delay="1000" proxy-target-class="true"/> |
||||||
|
|
||||||
|
</beans> |
||||||
@ -0,0 +1,20 @@ |
|||||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||||
|
<beans xmlns="http://www.springframework.org/schema/beans" |
||||||
|
xmlns:lang="http://www.springframework.org/schema/lang" |
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||||
|
xmlns:aop="http://www.springframework.org/schema/aop" |
||||||
|
xmlns:p="http://www.springframework.org/schema/p" |
||||||
|
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" |
||||||
|
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd |
||||||
|
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd |
||||||
|
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> |
||||||
|
|
||||||
|
<aop:config > |
||||||
|
<aop:advisor id="logUserAdvisor" pointcut="@within(org.springframework.scripting.groovy.Log)" advice-ref="logUserAdvice"/> |
||||||
|
</aop:config> |
||||||
|
|
||||||
|
<bean id="logUserAdvice" class="org.springframework.scripting.groovy.LogUserAdvice" /> |
||||||
|
|
||||||
|
<bean id="javaBean" class="org.springframework.scripting.groovy.TestServiceImpl"/> |
||||||
|
|
||||||
|
</beans> |
||||||
@ -0,0 +1,100 @@ |
|||||||
|
package org.springframework.scripting.groovy; |
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals; |
||||||
|
import static org.junit.Assert.fail; |
||||||
|
|
||||||
|
import org.junit.After; |
||||||
|
import org.junit.Test; |
||||||
|
import org.springframework.context.support.GenericXmlApplicationContext; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author Dave Syer |
||||||
|
*/ |
||||||
|
public class GroovyAspectIntegrationTests { |
||||||
|
|
||||||
|
private GenericXmlApplicationContext context; |
||||||
|
|
||||||
|
@After |
||||||
|
public void close() { |
||||||
|
if (context != null) { |
||||||
|
context.close(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testJavaBean() { |
||||||
|
|
||||||
|
context = new GenericXmlApplicationContext(getClass(), getClass().getSimpleName()+"-java-context.xml"); |
||||||
|
|
||||||
|
TestService bean = context.getBean("javaBean", TestService.class); |
||||||
|
LogUserAdvice logAdvice = context.getBean(LogUserAdvice.class); |
||||||
|
|
||||||
|
assertEquals(0, logAdvice.getCountThrows()); |
||||||
|
try { |
||||||
|
bean.sayHello(); |
||||||
|
fail("Expected exception"); |
||||||
|
} catch (RuntimeException e) { |
||||||
|
assertEquals("TestServiceImpl", e.getMessage()); |
||||||
|
} |
||||||
|
assertEquals(1, logAdvice.getCountThrows()); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testGroovyBeanInterface() { |
||||||
|
context = new GenericXmlApplicationContext(getClass(), getClass().getSimpleName()+"-groovy-interface-context.xml"); |
||||||
|
|
||||||
|
TestService bean = context.getBean("groovyBean", TestService.class); |
||||||
|
LogUserAdvice logAdvice = context.getBean(LogUserAdvice.class); |
||||||
|
|
||||||
|
assertEquals(0, logAdvice.getCountThrows()); |
||||||
|
try { |
||||||
|
bean.sayHello(); |
||||||
|
fail("Expected exception"); |
||||||
|
} catch (RuntimeException e) { |
||||||
|
assertEquals("GroovyServiceImpl", e.getMessage()); |
||||||
|
} |
||||||
|
assertEquals(1, logAdvice.getCountThrows()); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
@Test |
||||||
|
public void testGroovyBeanDynamic() { |
||||||
|
|
||||||
|
context = new GenericXmlApplicationContext(getClass(), getClass().getSimpleName()+"-groovy-dynamic-context.xml"); |
||||||
|
|
||||||
|
TestService bean = context.getBean("groovyBean", TestService.class); |
||||||
|
LogUserAdvice logAdvice = context.getBean(LogUserAdvice.class); |
||||||
|
|
||||||
|
assertEquals(0, logAdvice.getCountThrows()); |
||||||
|
try { |
||||||
|
bean.sayHello(); |
||||||
|
fail("Expected exception"); |
||||||
|
} catch (RuntimeException e) { |
||||||
|
assertEquals("GroovyServiceImpl", e.getMessage()); |
||||||
|
} |
||||||
|
// No proxy here because the pointcut only applies to the concrete class, not the interface
|
||||||
|
assertEquals(0, logAdvice.getCountThrows()); |
||||||
|
assertEquals(0, logAdvice.getCountBefore()); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testGroovyBeanProxyTargetClass() { |
||||||
|
|
||||||
|
context = new GenericXmlApplicationContext(getClass(), getClass().getSimpleName()+"-groovy-proxy-target-class-context.xml"); |
||||||
|
|
||||||
|
TestService bean = context.getBean("groovyBean", TestService.class); |
||||||
|
LogUserAdvice logAdvice = context.getBean(LogUserAdvice.class); |
||||||
|
|
||||||
|
assertEquals(0, logAdvice.getCountThrows()); |
||||||
|
try { |
||||||
|
bean.sayHello(); |
||||||
|
fail("Expected exception"); |
||||||
|
} catch (TestException e) { |
||||||
|
assertEquals("GroovyServiceImpl", e.getMessage()); |
||||||
|
} |
||||||
|
assertEquals(1, logAdvice.getCountBefore()); |
||||||
|
assertEquals(1, logAdvice.getCountThrows()); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,101 @@ |
|||||||
|
package org.springframework.scripting.groovy; |
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals; |
||||||
|
import static org.junit.Assert.fail; |
||||||
|
|
||||||
|
import org.junit.Test; |
||||||
|
import org.springframework.aop.Advisor; |
||||||
|
import org.springframework.aop.aspectj.AspectJExpressionPointcut; |
||||||
|
import org.springframework.aop.framework.ProxyFactory; |
||||||
|
import org.springframework.aop.support.DefaultPointcutAdvisor; |
||||||
|
import org.springframework.core.io.ClassPathResource; |
||||||
|
import org.springframework.scripting.groovy.GroovyScriptFactory; |
||||||
|
import org.springframework.scripting.support.ResourceScriptSource; |
||||||
|
import org.springframework.util.ClassUtils; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author Dave Syer |
||||||
|
*/ |
||||||
|
public class GroovyAspectTests { |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testManualGroovyBeanWithUnconditionalPointcut() throws Exception { |
||||||
|
|
||||||
|
LogUserAdvice logAdvice = new LogUserAdvice(); |
||||||
|
|
||||||
|
GroovyScriptFactory scriptFactory = new GroovyScriptFactory("GroovyServiceImpl.grv"); |
||||||
|
TestService target = (TestService) scriptFactory.getScriptedObject(new ResourceScriptSource( |
||||||
|
new ClassPathResource("GroovyServiceImpl.grv", getClass())), null); |
||||||
|
|
||||||
|
testAdvice(new DefaultPointcutAdvisor(logAdvice), logAdvice, target, "GroovyServiceImpl"); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testManualGroovyBeanWithStaticPointcut() throws Exception { |
||||||
|
LogUserAdvice logAdvice = new LogUserAdvice(); |
||||||
|
|
||||||
|
GroovyScriptFactory scriptFactory = new GroovyScriptFactory("GroovyServiceImpl.grv"); |
||||||
|
TestService target = (TestService) scriptFactory.getScriptedObject(new ResourceScriptSource( |
||||||
|
new ClassPathResource("GroovyServiceImpl.grv", getClass())), null); |
||||||
|
|
||||||
|
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut(); |
||||||
|
pointcut.setExpression(String.format("execution(* %s.TestService+.*(..))", ClassUtils.getPackageName(getClass()))); |
||||||
|
testAdvice(new DefaultPointcutAdvisor(pointcut, logAdvice), logAdvice, target, "GroovyServiceImpl", true); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testManualGroovyBeanWithDynamicPointcut() throws Exception { |
||||||
|
|
||||||
|
LogUserAdvice logAdvice = new LogUserAdvice(); |
||||||
|
|
||||||
|
GroovyScriptFactory scriptFactory = new GroovyScriptFactory("GroovyServiceImpl.grv"); |
||||||
|
TestService target = (TestService) scriptFactory.getScriptedObject(new ResourceScriptSource( |
||||||
|
new ClassPathResource("GroovyServiceImpl.grv", getClass())), null); |
||||||
|
|
||||||
|
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut(); |
||||||
|
pointcut.setExpression(String.format("@within(%s.Log)", ClassUtils.getPackageName(getClass()))); |
||||||
|
testAdvice(new DefaultPointcutAdvisor(pointcut, logAdvice), logAdvice, target, "GroovyServiceImpl", false); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testManualGroovyBeanWithDynamicPointcutProxyTargetClass() throws Exception { |
||||||
|
|
||||||
|
LogUserAdvice logAdvice = new LogUserAdvice(); |
||||||
|
|
||||||
|
GroovyScriptFactory scriptFactory = new GroovyScriptFactory("GroovyServiceImpl.grv"); |
||||||
|
TestService target = (TestService) scriptFactory.getScriptedObject(new ResourceScriptSource( |
||||||
|
new ClassPathResource("GroovyServiceImpl.grv", getClass())), null); |
||||||
|
|
||||||
|
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut(); |
||||||
|
pointcut.setExpression(String.format("@within(%s.Log)", ClassUtils.getPackageName(getClass()))); |
||||||
|
testAdvice(new DefaultPointcutAdvisor(pointcut, logAdvice), logAdvice, target, "GroovyServiceImpl", true); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
private void testAdvice(Advisor advisor, LogUserAdvice logAdvice, TestService target, String message) |
||||||
|
throws Exception { |
||||||
|
testAdvice(advisor, logAdvice, target, message, false); |
||||||
|
} |
||||||
|
|
||||||
|
private void testAdvice(Advisor advisor, LogUserAdvice logAdvice, TestService target, String message, |
||||||
|
boolean proxyTargetClass) throws Exception { |
||||||
|
|
||||||
|
logAdvice.reset(); |
||||||
|
|
||||||
|
ProxyFactory factory = new ProxyFactory(target); |
||||||
|
factory.setProxyTargetClass(proxyTargetClass); |
||||||
|
factory.addAdvisor(advisor); |
||||||
|
TestService bean = (TestService) factory.getProxy(); |
||||||
|
|
||||||
|
assertEquals(0, logAdvice.getCountThrows()); |
||||||
|
try { |
||||||
|
bean.sayHello(); |
||||||
|
fail("Expected exception"); |
||||||
|
} catch (TestException e) { |
||||||
|
assertEquals(message, e.getMessage()); |
||||||
|
} |
||||||
|
assertEquals(1, logAdvice.getCountThrows()); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,11 @@ |
|||||||
|
package org.springframework.scripting.groovy; |
||||||
|
|
||||||
|
@Log |
||||||
|
public class GroovyServiceImpl implements TestService { |
||||||
|
|
||||||
|
public String sayHello() { |
||||||
|
throw new TestException("GroovyServiceImpl"); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,15 @@ |
|||||||
|
package org.springframework.scripting.groovy; |
||||||
|
|
||||||
|
import java.lang.annotation.Inherited; |
||||||
|
import java.lang.annotation.Target; |
||||||
|
import java.lang.annotation.ElementType; |
||||||
|
import java.lang.annotation.Retention; |
||||||
|
import java.lang.annotation.RetentionPolicy; |
||||||
|
import java.lang.annotation.Documented; |
||||||
|
|
||||||
|
@Target({ ElementType.METHOD, ElementType.TYPE }) |
||||||
|
@Retention(RetentionPolicy.RUNTIME) |
||||||
|
@Documented |
||||||
|
@Inherited |
||||||
|
public @interface Log { |
||||||
|
} |
||||||
@ -0,0 +1,41 @@ |
|||||||
|
package org.springframework.scripting.groovy; |
||||||
|
|
||||||
|
import java.lang.reflect.Method; |
||||||
|
|
||||||
|
import org.springframework.aop.MethodBeforeAdvice; |
||||||
|
import org.springframework.aop.ThrowsAdvice; |
||||||
|
|
||||||
|
public class LogUserAdvice implements MethodBeforeAdvice, ThrowsAdvice { |
||||||
|
|
||||||
|
private int countBefore = 0; |
||||||
|
|
||||||
|
private int countThrows = 0; |
||||||
|
|
||||||
|
public void before(Method method, Object[] objects, Object o) throws Throwable { |
||||||
|
countBefore++; |
||||||
|
System.out.println("Method:"+method.getName()); |
||||||
|
} |
||||||
|
|
||||||
|
public void afterThrowing(Exception e) throws Throwable { |
||||||
|
countThrows++; |
||||||
|
System.out.println("***********************************************************************************"); |
||||||
|
System.out.println("Exception caught:"); |
||||||
|
System.out.println("***********************************************************************************"); |
||||||
|
e.printStackTrace(); |
||||||
|
throw e; |
||||||
|
} |
||||||
|
|
||||||
|
public int getCountBefore() { |
||||||
|
return countBefore; |
||||||
|
} |
||||||
|
|
||||||
|
public int getCountThrows() { |
||||||
|
return countThrows; |
||||||
|
} |
||||||
|
|
||||||
|
public void reset() { |
||||||
|
countThrows = 0; |
||||||
|
countBefore = 0; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,29 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2002-2011 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.scripting.groovy; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author Dave Syer |
||||||
|
* |
||||||
|
*/ |
||||||
|
public class TestException extends RuntimeException { |
||||||
|
|
||||||
|
public TestException(String string) { |
||||||
|
super(string); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,5 @@ |
|||||||
|
package org.springframework.scripting.groovy; |
||||||
|
|
||||||
|
public interface TestService { |
||||||
|
public String sayHello(); |
||||||
|
} |
||||||
Loading…
Reference in new issue