+ moving .aop.aspectj.* unit tests from .testsuite -> .context
+ @SuppressWarnings and generic parameters to production .aop sources
This commit is contained in:
parent
4a77699d38
commit
2d37eb722b
|
|
@ -38,6 +38,6 @@ public interface TargetSourceCreator {
|
|||
* @return a special TargetSource or <code>null</code> if this TargetSourceCreator isn't
|
||||
* interested in the particular bean
|
||||
*/
|
||||
TargetSource getTargetSource(Class beanClass, String beanName);
|
||||
TargetSource getTargetSource(Class<?> beanClass, String beanName);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -86,7 +86,7 @@ public abstract class AbstractBeanFactoryBasedTargetSourceCreator
|
|||
// Implementation of the TargetSourceCreator interface
|
||||
//---------------------------------------------------------------------
|
||||
|
||||
public final TargetSource getTargetSource(Class beanClass, String beanName) {
|
||||
public final TargetSource getTargetSource(Class<?> beanClass, String beanName) {
|
||||
AbstractBeanFactoryBasedTargetSource targetSource =
|
||||
createBeanFactoryBasedTargetSource(beanClass, beanName);
|
||||
if (targetSource == null) {
|
||||
|
|
@ -194,6 +194,6 @@ public abstract class AbstractBeanFactoryBasedTargetSourceCreator
|
|||
* @return the AbstractPrototypeBasedTargetSource, or <code>null</code> if we don't match this
|
||||
*/
|
||||
protected abstract AbstractBeanFactoryBasedTargetSource createBeanFactoryBasedTargetSource(
|
||||
Class beanClass, String beanName);
|
||||
Class<?> beanClass, String beanName);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ public abstract class StaticMethodMatcher implements MethodMatcher {
|
|||
return false;
|
||||
}
|
||||
|
||||
public final boolean matches(Method method, Class targetClass, Object[] args) {
|
||||
public final boolean matches(Method method, Class<?> targetClass, Object[] args) {
|
||||
// should never be invoked because isRuntime() returns false
|
||||
throw new UnsupportedOperationException("Illegal MethodMatcher usage");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,6 +32,7 @@ import org.springframework.util.Assert;
|
|||
* @author Rod Johnson
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public abstract class StaticMethodMatcherPointcutAdvisor extends StaticMethodMatcherPointcut
|
||||
implements PointcutAdvisor, Ordered, Serializable {
|
||||
|
||||
|
|
|
|||
|
|
@ -16,25 +16,33 @@
|
|||
|
||||
package org.springframework.aop.framework.autoproxy;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.aop.MethodBeforeAdvice;
|
||||
import org.springframework.aop.framework.Advised;
|
||||
import org.springframework.aop.framework.autoproxy.target.AbstractBeanFactoryBasedTargetSourceCreator;
|
||||
import org.springframework.aop.support.AopUtils;
|
||||
import org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor;
|
||||
import org.springframework.aop.target.AbstractBeanFactoryBasedTargetSource;
|
||||
import org.springframework.aop.target.CommonsPoolTargetSource;
|
||||
import org.springframework.aop.target.LazyInitTargetSource;
|
||||
import org.springframework.aop.target.PrototypeTargetSource;
|
||||
import org.springframework.aop.target.ThreadLocalTargetSource;
|
||||
import org.springframework.beans.ITestBean;
|
||||
import org.springframework.beans.TestBean;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.transaction.CallCountingTransactionManager;
|
||||
import org.springframework.transaction.NoTransactionException;
|
||||
import org.springframework.transaction.interceptor.TransactionInterceptor;
|
||||
|
||||
import test.advice.CountingBeforeAdvice;
|
||||
import test.advice.MethodCounter;
|
||||
|
|
@ -45,19 +53,28 @@ import test.mixin.Lockable;
|
|||
* Tests for auto proxy creation by advisor recognition.
|
||||
*
|
||||
* @author Rod Johnson
|
||||
* @author Dave Syer
|
||||
* @author Chris Beams
|
||||
*/
|
||||
public class AdvisorAutoProxyCreatorTests {
|
||||
public final class AdvisorAutoProxyCreatorTests {
|
||||
|
||||
private static final Class<?> CLASS = AdvisorAutoProxyCreatorTests.class;
|
||||
private static final String CLASSNAME = CLASS.getSimpleName();
|
||||
|
||||
private static final String DEFAULT_CONTEXT = CLASSNAME + "-context.xml";
|
||||
private static final String COMMON_INTERCEPTORS_CONTEXT = CLASSNAME + "-common-interceptors.xml";
|
||||
private static final String CUSTOM_TARGETSOURCE_CONTEXT = CLASSNAME + "-custom-targetsource.xml";
|
||||
private static final String QUICK_TARGETSOURCE_CONTEXT = CLASSNAME + "-quick-targetsource.xml";
|
||||
private static final String OPTIMIZED_CONTEXT = CLASSNAME + "-optimized.xml";
|
||||
|
||||
private static final String ADVISOR_APC_BEAN_NAME = "aapc";
|
||||
|
||||
private static final String TXMANAGER_BEAN_NAME = "txManager";
|
||||
|
||||
/**
|
||||
* Return a bean factory with attributes and EnterpriseServices configured.
|
||||
*/
|
||||
protected BeanFactory getBeanFactory() throws IOException {
|
||||
return new ClassPathXmlApplicationContext("/org/springframework/aop/framework/autoproxy/advisorAutoProxyCreator.xml");
|
||||
return new ClassPathXmlApplicationContext(DEFAULT_CONTEXT, CLASS);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -101,7 +118,7 @@ public class AdvisorAutoProxyCreatorTests {
|
|||
*/
|
||||
@Test
|
||||
public void testCommonInterceptorAndAdvisor() throws Exception {
|
||||
BeanFactory bf = new ClassPathXmlApplicationContext("/org/springframework/aop/framework/autoproxy/advisorAutoProxyCreatorWithCommonInterceptors.xml");
|
||||
BeanFactory bf = new ClassPathXmlApplicationContext(COMMON_INTERCEPTORS_CONTEXT, CLASS);
|
||||
ITestBean test1 = (ITestBean) bf.getBean("test1");
|
||||
assertTrue(AopUtils.isAopProxy(test1));
|
||||
|
||||
|
|
@ -131,7 +148,7 @@ public class AdvisorAutoProxyCreatorTests {
|
|||
*/
|
||||
@Test
|
||||
public void testCustomTargetSourceNoMatch() throws Exception {
|
||||
BeanFactory bf = new ClassPathXmlApplicationContext("/org/springframework/aop/framework/autoproxy/customTargetSource.xml");
|
||||
BeanFactory bf = new ClassPathXmlApplicationContext(CUSTOM_TARGETSOURCE_CONTEXT, CLASS);
|
||||
ITestBean test = (ITestBean) bf.getBean("test");
|
||||
assertFalse(AopUtils.isAopProxy(test));
|
||||
assertEquals("Rod", test.getName());
|
||||
|
|
@ -141,7 +158,7 @@ public class AdvisorAutoProxyCreatorTests {
|
|||
@Test
|
||||
public void testCustomPrototypeTargetSource() throws Exception {
|
||||
CountingTestBean.count = 0;
|
||||
BeanFactory bf = new ClassPathXmlApplicationContext("/org/springframework/aop/framework/autoproxy/customTargetSource.xml");
|
||||
BeanFactory bf = new ClassPathXmlApplicationContext(CUSTOM_TARGETSOURCE_CONTEXT, CLASS);
|
||||
ITestBean test = (ITestBean) bf.getBean("prototypeTest");
|
||||
assertTrue(AopUtils.isAopProxy(test));
|
||||
Advised advised = (Advised) test;
|
||||
|
|
@ -156,7 +173,7 @@ public class AdvisorAutoProxyCreatorTests {
|
|||
@Test
|
||||
public void testLazyInitTargetSource() throws Exception {
|
||||
CountingTestBean.count = 0;
|
||||
BeanFactory bf = new ClassPathXmlApplicationContext("/org/springframework/aop/framework/autoproxy/customTargetSource.xml");
|
||||
BeanFactory bf = new ClassPathXmlApplicationContext(CUSTOM_TARGETSOURCE_CONTEXT, CLASS);
|
||||
ITestBean test = (ITestBean) bf.getBean("lazyInitTest");
|
||||
assertTrue(AopUtils.isAopProxy(test));
|
||||
Advised advised = (Advised) test;
|
||||
|
|
@ -171,7 +188,7 @@ public class AdvisorAutoProxyCreatorTests {
|
|||
@Test
|
||||
public void testQuickTargetSourceCreator() throws Exception {
|
||||
ClassPathXmlApplicationContext bf =
|
||||
new ClassPathXmlApplicationContext("/org/springframework/aop/framework/autoproxy/quickTargetSource.xml");
|
||||
new ClassPathXmlApplicationContext(QUICK_TARGETSOURCE_CONTEXT, CLASS);
|
||||
ITestBean test = (ITestBean) bf.getBean("test");
|
||||
assertFalse(AopUtils.isAopProxy(test));
|
||||
assertEquals("Rod", test.getName());
|
||||
|
|
@ -213,17 +230,6 @@ public class AdvisorAutoProxyCreatorTests {
|
|||
bf.close();
|
||||
}
|
||||
|
||||
/*
|
||||
@Test
|
||||
public void testIntroductionIsProxied() throws Exception {
|
||||
BeanFactory bf = getBeanFactory();
|
||||
Object modifiable = bf.getBean("modifiable1");
|
||||
// We can tell it's a CGLIB proxy by looking at the class name
|
||||
System.out.println(modifiable.getClass().getName());
|
||||
assertFalse(modifiable.getClass().getName().equals(ModifiableTestBean.class.getName()));
|
||||
}
|
||||
*/
|
||||
|
||||
@Test
|
||||
public void testTransactionAttributeOnMethod() throws Exception {
|
||||
BeanFactory bf = getBeanFactory();
|
||||
|
|
@ -310,7 +316,7 @@ public class AdvisorAutoProxyCreatorTests {
|
|||
|
||||
@Test
|
||||
public void testWithOptimizedProxy() throws Exception {
|
||||
BeanFactory beanFactory = new ClassPathXmlApplicationContext("org/springframework/aop/framework/autoproxy/optimizedAutoProxyCreator.xml");
|
||||
BeanFactory beanFactory = new ClassPathXmlApplicationContext(OPTIMIZED_CONTEXT, CLASS);
|
||||
|
||||
ITestBean testBean = (ITestBean) beanFactory.getBean("optimizedTestBean");
|
||||
assertTrue(AopUtils.isAopProxy(testBean));
|
||||
|
|
@ -323,42 +329,171 @@ public class AdvisorAutoProxyCreatorTests {
|
|||
assertEquals("Incorrect number of calls to proxy", 2, beforeAdvice.getCalls());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests an introduction pointcut. This is a prototype, so that it can add
|
||||
* a Modifiable mixin. Tests that the autoproxy infrastructure can create
|
||||
* advised objects with independent interceptor instances.
|
||||
* The Modifiable behaviour of each instance of TestBean should be distinct.
|
||||
*/
|
||||
/*
|
||||
@Test
|
||||
public void testIntroductionViaPrototype() throws Exception {
|
||||
BeanFactory bf = getBeanFactory();
|
||||
|
||||
Object o = bf.getBean("modifiable1");
|
||||
ITestBean modifiable1 = (ITestBean) bf.getBean("modifiable1");
|
||||
ITestBean modifiable2 = (ITestBean) bf.getBean("modifiable2");
|
||||
class CountingTestBean extends TestBean {
|
||||
|
||||
Advised pc = (Advised) modifiable1;
|
||||
System.err.println(pc.toProxyConfigString());
|
||||
public static int count = 0;
|
||||
|
||||
// For convenience only
|
||||
Modifiable mod1 = (Modifiable) modifiable1;
|
||||
Modifiable mod2 = (Modifiable) modifiable2;
|
||||
|
||||
assertFalse(mod1.isModified());
|
||||
assertFalse(mod2.isModified());
|
||||
|
||||
int newAge = 33;
|
||||
modifiable1.setAge(newAge);
|
||||
assertTrue(mod1.isModified());
|
||||
// Changes to one shouldn't have affected the other
|
||||
assertFalse("Instances of prototype introduction pointcut don't seem distinct", mod2.isModified());
|
||||
mod1.acceptChanges();
|
||||
assertFalse(mod1.isModified());
|
||||
assertEquals(modifiable1.getAge(), newAge);
|
||||
assertFalse(mod1.isModified());
|
||||
public CountingTestBean() {
|
||||
count++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
class NeverMatchAdvisor extends StaticMethodMatcherPointcutAdvisor {
|
||||
|
||||
public NeverMatchAdvisor() {
|
||||
super(new NopInterceptor());
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is solely to allow us to create a mixture of dependencies in
|
||||
* the bean definitions. The dependencies don't have any meaning, and don't
|
||||
* <b>do</b> anything.
|
||||
*/
|
||||
public void setDependencies(List<?> l) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.springframework.aop.MethodMatcher#matches(java.lang.reflect.Method, java.lang.Class)
|
||||
*/
|
||||
public boolean matches(Method m, Class<?> targetClass) {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
class NoSetters {
|
||||
|
||||
public void A() {
|
||||
|
||||
}
|
||||
|
||||
public int getB() {
|
||||
return -1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
class OrderedTxCheckAdvisor extends StaticMethodMatcherPointcutAdvisor implements InitializingBean {
|
||||
|
||||
/**
|
||||
* Should we insist on the presence of a transaction attribute or refuse to accept one?
|
||||
*/
|
||||
private boolean requireTransactionContext = false;
|
||||
|
||||
|
||||
public void setRequireTransactionContext(boolean requireTransactionContext) {
|
||||
this.requireTransactionContext = requireTransactionContext;
|
||||
}
|
||||
|
||||
public boolean isRequireTransactionContext() {
|
||||
return requireTransactionContext;
|
||||
}
|
||||
|
||||
|
||||
public CountingBeforeAdvice getCountingBeforeAdvice() {
|
||||
return (CountingBeforeAdvice) getAdvice();
|
||||
}
|
||||
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
setAdvice(new TxCountingBeforeAdvice());
|
||||
}
|
||||
|
||||
public boolean matches(Method method, Class<?> targetClass) {
|
||||
return method.getName().startsWith("setAge");
|
||||
}
|
||||
|
||||
|
||||
private class TxCountingBeforeAdvice extends CountingBeforeAdvice {
|
||||
|
||||
public void before(Method method, Object[] args, Object target) throws Throwable {
|
||||
// do transaction checks
|
||||
if (requireTransactionContext) {
|
||||
TransactionInterceptor.currentTransactionStatus();
|
||||
}
|
||||
else {
|
||||
try {
|
||||
TransactionInterceptor.currentTransactionStatus();
|
||||
throw new RuntimeException("Shouldn't have a transaction");
|
||||
}
|
||||
catch (NoTransactionException ex) {
|
||||
// this is Ok
|
||||
}
|
||||
}
|
||||
super.before(method, args, target);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
class Rollback {
|
||||
|
||||
/**
|
||||
* Inherits transaction attribute.
|
||||
* Illustrates programmatic rollback.
|
||||
* @param rollbackOnly
|
||||
*/
|
||||
public void rollbackOnly(boolean rollbackOnly) {
|
||||
if (rollbackOnly) {
|
||||
setRollbackOnly();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracted in a protected method to facilitate testing
|
||||
*/
|
||||
protected void setRollbackOnly() {
|
||||
TransactionInterceptor.currentTransactionStatus().setRollbackOnly();
|
||||
}
|
||||
|
||||
/**
|
||||
* @org.springframework.transaction.interceptor.RuleBasedTransaction ( timeout=-1 )
|
||||
* @org.springframework.transaction.interceptor.RollbackRule ( "java.lang.Exception" )
|
||||
* @org.springframework.transaction.interceptor.NoRollbackRule ( "ServletException" )
|
||||
*/
|
||||
public void echoException(Exception ex) throws Exception {
|
||||
if (ex != null)
|
||||
throw ex;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
class SelectivePrototypeTargetSourceCreator extends AbstractBeanFactoryBasedTargetSourceCreator {
|
||||
|
||||
protected AbstractBeanFactoryBasedTargetSource createBeanFactoryBasedTargetSource(
|
||||
Class<?> beanClass, String beanName) {
|
||||
if (!beanName.startsWith("prototype")) {
|
||||
return null;
|
||||
}
|
||||
return new PrototypeTargetSource();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
class NullChecker implements MethodBeforeAdvice {
|
||||
|
||||
public void before(Method method, Object[] args, Object target) throws Throwable {
|
||||
check(args);
|
||||
}
|
||||
|
||||
private void check(Object[] args) {
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
if (args[i] == null) {
|
||||
throw new IllegalArgumentException("Null argument at position " + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -39,11 +39,11 @@ import org.springframework.context.support.StaticApplicationContext;
|
|||
import org.springframework.context.support.StaticMessageSource;
|
||||
|
||||
/**
|
||||
* @since 09.12.2003
|
||||
* @author Juergen Hoeller
|
||||
* @author Chris Beams
|
||||
* @since 09.12.2003
|
||||
*/
|
||||
public class AutoProxyCreatorTests {
|
||||
public final class AutoProxyCreatorTests {
|
||||
|
||||
@Test
|
||||
public void testBeanNameAutoProxyCreator() {
|
||||
|
|
|
|||
|
|
@ -27,12 +27,12 @@ import org.springframework.context.support.ClassPathXmlApplicationContext;
|
|||
* @author Dave Syer
|
||||
* @author Chris Beams
|
||||
*/
|
||||
public class BeanNameAutoProxyCreatorInitTests {
|
||||
public final class BeanNameAutoProxyCreatorInitTests {
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testIgnoreAdvisorThatIsCurrentlyCreation() {
|
||||
ClassPathXmlApplicationContext ctx =
|
||||
new ClassPathXmlApplicationContext("beanNameAutoProxyCreatorInitTests.xml", getClass());
|
||||
new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-context.xml", getClass());
|
||||
TestBean bean = (TestBean) ctx.getBean("bean");
|
||||
bean.setName("foo");
|
||||
assertEquals("foo", bean.getName());
|
||||
|
|
|
|||
|
|
@ -27,6 +27,7 @@ import org.springframework.aop.support.AopUtils;
|
|||
import org.springframework.beans.ITestBean;
|
||||
import org.springframework.beans.TestBean;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
import test.advice.CountingBeforeAdvice;
|
||||
|
|
@ -48,7 +49,8 @@ public class BeanNameAutoProxyCreatorTests {
|
|||
public void setUp() throws IOException {
|
||||
// Note that we need an ApplicationContext, not just a BeanFactory,
|
||||
// for post-processing and hence auto-proxying to work.
|
||||
this.beanFactory = new ClassPathXmlApplicationContext("beanNameAutoProxyCreatorTests.xml", getClass());
|
||||
beanFactory =
|
||||
new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-context.xml", getClass());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -196,3 +198,29 @@ public class BeanNameAutoProxyCreatorTests {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
class CreatesTestBean implements FactoryBean<Object> {
|
||||
|
||||
/**
|
||||
* @see org.springframework.beans.factory.FactoryBean#getObject()
|
||||
*/
|
||||
public Object getObject() throws Exception {
|
||||
return new TestBean();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.springframework.beans.factory.FactoryBean#getObjectType()
|
||||
*/
|
||||
public Class<?> getObjectType() {
|
||||
return TestBean.class;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.springframework.beans.factory.FactoryBean#isSingleton()
|
||||
*/
|
||||
public boolean isSingleton() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,37 +0,0 @@
|
|||
/*
|
||||
* $Id: CountingTestBean.java,v 1.2 2005/03/25 09:28:17 jhoeller Exp $
|
||||
*/
|
||||
|
||||
/*
|
||||
* 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.autoproxy;
|
||||
|
||||
import org.springframework.beans.TestBean;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @since 15.03.2005
|
||||
*/
|
||||
public class CountingTestBean extends TestBean {
|
||||
|
||||
public static int count = 0;
|
||||
|
||||
public CountingTestBean() {
|
||||
count++;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,49 +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.autoproxy;
|
||||
|
||||
import org.springframework.beans.TestBean;
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Rod Johnson
|
||||
*/
|
||||
public class CreatesTestBean implements FactoryBean {
|
||||
|
||||
/**
|
||||
* @see org.springframework.beans.factory.FactoryBean#getObject()
|
||||
*/
|
||||
public Object getObject() throws Exception {
|
||||
return new TestBean();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.springframework.beans.factory.FactoryBean#getObjectType()
|
||||
*/
|
||||
public Class getObjectType() {
|
||||
return TestBean.class;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.springframework.beans.factory.FactoryBean#isSingleton()
|
||||
*/
|
||||
public boolean isSingleton() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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.autoproxy;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor;
|
||||
|
||||
import test.interceptor.NopInterceptor;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Rod Johnson
|
||||
*/
|
||||
public class NeverMatchAdvisor extends StaticMethodMatcherPointcutAdvisor {
|
||||
|
||||
public NeverMatchAdvisor() {
|
||||
super(new NopInterceptor());
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is solely to allow us to create a mixture of dependencies in
|
||||
* the bean definitions. The dependencies don't have any meaning, and don't
|
||||
* <b>do</b> anything.
|
||||
*/
|
||||
public void setDependencies(List l) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.springframework.aop.MethodMatcher#matches(java.lang.reflect.Method, java.lang.Class)
|
||||
*/
|
||||
public boolean matches(Method m, Class targetClass) {
|
||||
//System.err.println("NeverMAtch test");
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,33 +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.autoproxy;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Rod Johnson
|
||||
*/
|
||||
public class NoSetters {
|
||||
|
||||
public void A() {
|
||||
|
||||
}
|
||||
|
||||
public int getB() {
|
||||
return -1;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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.framework.autoproxy;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.springframework.aop.MethodBeforeAdvice;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class NullChecker implements MethodBeforeAdvice {
|
||||
|
||||
public void before(Method method, Object[] args, Object target) throws Throwable {
|
||||
check(args);
|
||||
}
|
||||
|
||||
private void check(Object[] args) {
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
if (args[i] == null) {
|
||||
throw new IllegalArgumentException("Null argument at position " + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,88 +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.framework.autoproxy;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.transaction.NoTransactionException;
|
||||
import org.springframework.transaction.interceptor.TransactionInterceptor;
|
||||
|
||||
import test.advice.CountingBeforeAdvice;
|
||||
|
||||
/**
|
||||
* Before advisor that allow us to manipulate ordering to check
|
||||
* that superclass sorting works correctly.
|
||||
*
|
||||
* <p>It doesn't actually <i>do</i> anything except count
|
||||
* method invocations and check for presence of transaction context.
|
||||
* <br>Matches setters.
|
||||
*
|
||||
* @author Rod Johnson
|
||||
*/
|
||||
public class OrderedTxCheckAdvisor extends StaticMethodMatcherPointcutAdvisor implements InitializingBean {
|
||||
|
||||
/**
|
||||
* Should we insist on the presence of a transaction attribute or refuse to accept one?
|
||||
*/
|
||||
private boolean requireTransactionContext = false;
|
||||
|
||||
|
||||
public void setRequireTransactionContext(boolean requireTransactionContext) {
|
||||
this.requireTransactionContext = requireTransactionContext;
|
||||
}
|
||||
|
||||
public boolean isRequireTransactionContext() {
|
||||
return requireTransactionContext;
|
||||
}
|
||||
|
||||
|
||||
public CountingBeforeAdvice getCountingBeforeAdvice() {
|
||||
return (CountingBeforeAdvice) getAdvice();
|
||||
}
|
||||
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
setAdvice(new TxCountingBeforeAdvice());
|
||||
}
|
||||
|
||||
public boolean matches(Method method, Class targetClass) {
|
||||
return method.getName().startsWith("setAge");
|
||||
}
|
||||
|
||||
|
||||
private class TxCountingBeforeAdvice extends CountingBeforeAdvice {
|
||||
|
||||
public void before(Method method, Object[] args, Object target) throws Throwable {
|
||||
// do transaction checks
|
||||
if (requireTransactionContext) {
|
||||
TransactionInterceptor.currentTransactionStatus();
|
||||
}
|
||||
else {
|
||||
try {
|
||||
TransactionInterceptor.currentTransactionStatus();
|
||||
throw new RuntimeException("Shouldn't have a transaction");
|
||||
}
|
||||
catch (NoTransactionException ex) {
|
||||
// this is Ok
|
||||
}
|
||||
}
|
||||
super.before(method, args, target);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,57 +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.autoproxy;
|
||||
|
||||
import org.springframework.transaction.interceptor.TransactionInterceptor;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @org.springframework.enterpriseservices.Pooling (size=10)
|
||||
* @org.springframework.transaction.interceptor.DefaultTransaction ( timeout=-1 )
|
||||
*/
|
||||
public class Rollback {
|
||||
|
||||
/**
|
||||
* Inherits transaction attribute.
|
||||
* Illustrates programmatic rollback.
|
||||
* @param rollbackOnly
|
||||
*/
|
||||
public void rollbackOnly(boolean rollbackOnly) {
|
||||
if (rollbackOnly) {
|
||||
setRollbackOnly();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracted in a protected method to facilitate testing
|
||||
*/
|
||||
protected void setRollbackOnly() {
|
||||
TransactionInterceptor.currentTransactionStatus().setRollbackOnly();
|
||||
}
|
||||
|
||||
/**
|
||||
* @org.springframework.transaction.interceptor.RuleBasedTransaction ( timeout=-1 )
|
||||
* @org.springframework.transaction.interceptor.RollbackRule ( "java.lang.Exception" )
|
||||
* @org.springframework.transaction.interceptor.NoRollbackRule ( "ServletException" )
|
||||
*/
|
||||
public void echoException(Exception ex) throws Exception {
|
||||
if (ex != null)
|
||||
throw ex;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,39 +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.autoproxy;
|
||||
|
||||
import org.springframework.aop.framework.autoproxy.target.AbstractBeanFactoryBasedTargetSourceCreator;
|
||||
import org.springframework.aop.target.AbstractBeanFactoryBasedTargetSource;
|
||||
import org.springframework.aop.target.PrototypeTargetSource;
|
||||
|
||||
/**
|
||||
* Overrides generic PrototypeTargetSourceCreator to create a prototype only for beans
|
||||
* with names beginning with "prototype".
|
||||
*
|
||||
* @author Rod Johnson
|
||||
*/
|
||||
public class SelectivePrototypeTargetSourceCreator extends AbstractBeanFactoryBasedTargetSourceCreator {
|
||||
|
||||
protected AbstractBeanFactoryBasedTargetSource createBeanFactoryBasedTargetSource(
|
||||
Class beanClass, String beanName) {
|
||||
if (!beanName.startsWith("prototype")) {
|
||||
return null;
|
||||
}
|
||||
return new PrototypeTargetSource();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -39,17 +39,25 @@ import org.springframework.core.io.ClassPathResource;
|
|||
*/
|
||||
public class ScopedProxyTests {
|
||||
|
||||
private static final Class<?> CLASS = ScopedProxyTests.class;
|
||||
private static final String CLASSNAME = CLASS.getSimpleName();
|
||||
|
||||
private static final ClassPathResource LIST_CONTEXT = new ClassPathResource(CLASSNAME + "-list.xml", CLASS);
|
||||
private static final ClassPathResource MAP_CONTEXT = new ClassPathResource(CLASSNAME + "-map.xml", CLASS);
|
||||
private static final ClassPathResource OVERRIDE_CONTEXT = new ClassPathResource(CLASSNAME + "-override.xml", CLASS);
|
||||
private static final ClassPathResource TESTBEAN_CONTEXT = new ClassPathResource(CLASSNAME + "-testbean.xml", CLASS);
|
||||
|
||||
/* SPR-2108 */
|
||||
@Test
|
||||
public void testProxyAssignable() throws Exception {
|
||||
XmlBeanFactory bf = new XmlBeanFactory(new ClassPathResource("scopedMap.xml", getClass()));
|
||||
XmlBeanFactory bf = new XmlBeanFactory(MAP_CONTEXT);
|
||||
Object baseMap = bf.getBean("singletonMap");
|
||||
assertTrue(baseMap instanceof Map);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSimpleProxy() throws Exception {
|
||||
XmlBeanFactory bf = new XmlBeanFactory(new ClassPathResource("scopedMap.xml", getClass()));
|
||||
XmlBeanFactory bf = new XmlBeanFactory(MAP_CONTEXT);
|
||||
Object simpleMap = bf.getBean("simpleMap");
|
||||
assertTrue(simpleMap instanceof Map);
|
||||
assertTrue(simpleMap instanceof HashMap);
|
||||
|
|
@ -58,7 +66,7 @@ public class ScopedProxyTests {
|
|||
@Test
|
||||
public void testScopedOverride() throws Exception {
|
||||
GenericApplicationContext ctx = new GenericApplicationContext();
|
||||
new XmlBeanDefinitionReader(ctx).loadBeanDefinitions(new ClassPathResource("scopedOverride.xml", getClass()));
|
||||
new XmlBeanDefinitionReader(ctx).loadBeanDefinitions(OVERRIDE_CONTEXT);
|
||||
SimpleMapScope scope = new SimpleMapScope();
|
||||
ctx.getBeanFactory().registerScope("request", scope);
|
||||
ctx.refresh();
|
||||
|
|
@ -73,7 +81,7 @@ public class ScopedProxyTests {
|
|||
|
||||
@Test
|
||||
public void testJdkScopedProxy() throws Exception {
|
||||
XmlBeanFactory bf = new XmlBeanFactory(new ClassPathResource("scopedTestBean.xml", getClass()));
|
||||
XmlBeanFactory bf = new XmlBeanFactory(TESTBEAN_CONTEXT);
|
||||
SimpleMapScope scope = new SimpleMapScope();
|
||||
bf.registerScope("request", scope);
|
||||
|
||||
|
|
@ -90,7 +98,7 @@ public class ScopedProxyTests {
|
|||
|
||||
@Test
|
||||
public void testCglibScopedProxy() {
|
||||
XmlBeanFactory bf = new XmlBeanFactory(new ClassPathResource("scopedList.xml", getClass()));
|
||||
XmlBeanFactory bf = new XmlBeanFactory(LIST_CONTEXT);
|
||||
SimpleMapScope scope = new SimpleMapScope();
|
||||
bf.registerScope("request", scope);
|
||||
|
||||
|
|
|
|||
|
|
@ -54,7 +54,7 @@ public class CommonsPoolTargetSourceTests {
|
|||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
this.beanFactory = new XmlBeanFactory(new ClassPathResource("org/springframework/aop/target/commonsPoolTests.xml"));
|
||||
this.beanFactory = new XmlBeanFactory(new ClassPathResource(getClass().getSimpleName() + "-context.xml", getClass()));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in New Issue