SPR-5749: Add defensive matching using target class loader

* Changes to ASpectJExpressionPointcut plus some tests in Spring AOP
* plus some tests in groovy support
This commit is contained in:
David Syer
2011-06-24 17:23:43 +00:00
parent 1e07af8827
commit bd0f68d095
14 changed files with 685 additions and 25 deletions

View File

@@ -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>

View File

@@ -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>

View File

@@ -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>

View File

@@ -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>

View File

@@ -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());
}
}

View File

@@ -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());
}
}

View File

@@ -0,0 +1,11 @@
package org.springframework.scripting.groovy;
@Log
public class GroovyServiceImpl implements TestService {
public String sayHello() {
throw new TestException("GroovyServiceImpl");
}
}

View File

@@ -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 {
}

View File

@@ -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;
}
}

View File

@@ -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);
}
}

View File

@@ -0,0 +1,5 @@
package org.springframework.scripting.groovy;
public interface TestService {
public String sayHello();
}

View File

@@ -0,0 +1,8 @@
package org.springframework.scripting.groovy;
@Log
public class TestServiceImpl implements TestService{
public String sayHello() {
throw new TestException("TestServiceImpl");
}
}