diff options
Diffstat (limited to 'spring-tx/src/test/java/org/springframework/transaction')
8 files changed, 193 insertions, 30 deletions
diff --git a/spring-tx/src/test/java/org/springframework/transaction/annotation/AnnotationTransactionAttributeSourceTests.java b/spring-tx/src/test/java/org/springframework/transaction/annotation/AnnotationTransactionAttributeSourceTests.java index ce346859..5e20987d 100644 --- a/spring-tx/src/test/java/org/springframework/transaction/annotation/AnnotationTransactionAttributeSourceTests.java +++ b/spring-tx/src/test/java/org/springframework/transaction/annotation/AnnotationTransactionAttributeSourceTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 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. @@ -21,9 +21,10 @@ import java.io.Serializable; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.reflect.Method; - import javax.ejb.TransactionAttributeType; +import groovy.lang.GroovyObject; +import groovy.lang.MetaClass; import org.junit.Test; import org.springframework.aop.framework.Advised; @@ -54,7 +55,7 @@ public class AnnotationTransactionAttributeSourceTests { TransactionInterceptor ti = new TransactionInterceptor(ptm, tas); ProxyFactory proxyFactory = new ProxyFactory(); - proxyFactory.setInterfaces(new Class[] {ITestBean.class}); + proxyFactory.setInterfaces(ITestBean.class); proxyFactory.addAdvice(ti); proxyFactory.setTarget(tb); ITestBean proxy = (ITestBean) proxyFactory.getProxy(); @@ -369,6 +370,20 @@ public class AnnotationTransactionAttributeSourceTests { assertEquals(TransactionAttribute.PROPAGATION_SUPPORTS, getNameAttr.getPropagationBehavior()); } + @Test + public void transactionAttributeDeclaredOnGroovyClass() throws Exception { + Method getAgeMethod = ITestBean.class.getMethod("getAge"); + Method getNameMethod = ITestBean.class.getMethod("getName"); + Method getMetaClassMethod = GroovyObject.class.getMethod("getMetaClass"); + + AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource(); + TransactionAttribute getAgeAttr = atas.getTransactionAttribute(getAgeMethod, GroovyTestBean.class); + assertEquals(TransactionAttribute.PROPAGATION_REQUIRED, getAgeAttr.getPropagationBehavior()); + TransactionAttribute getNameAttr = atas.getTransactionAttribute(getNameMethod, GroovyTestBean.class); + assertEquals(TransactionAttribute.PROPAGATION_REQUIRED, getNameAttr.getPropagationBehavior()); + assertNull(atas.getTransactionAttribute(getMetaClassMethod, GroovyTestBean.class)); + } + interface ITestBean { @@ -470,7 +485,7 @@ public class AnnotationTransactionAttributeSourceTests { } @Override - @Transactional(rollbackFor=Exception.class) + @Transactional(rollbackFor = Exception.class) public int getAge() { return age; } @@ -543,8 +558,8 @@ public class AnnotationTransactionAttributeSourceTests { } @Override - @Transactional(propagation=Propagation.REQUIRES_NEW, isolation=Isolation.REPEATABLE_READ, timeout=5, - readOnly=true, rollbackFor=Exception.class, noRollbackFor={IOException.class}) + @Transactional(propagation = Propagation.REQUIRES_NEW, isolation=Isolation.REPEATABLE_READ, + timeout = 5, readOnly = true, rollbackFor = Exception.class, noRollbackFor = IOException.class) public int getAge() { return age; } @@ -556,7 +571,7 @@ public class AnnotationTransactionAttributeSourceTests { } - @Transactional(rollbackFor=Exception.class, noRollbackFor={IOException.class}) + @Transactional(rollbackFor = Exception.class, noRollbackFor = IOException.class) static class TestBean4 implements ITestBean3 { private String name; @@ -594,7 +609,7 @@ public class AnnotationTransactionAttributeSourceTests { @Retention(RetentionPolicy.RUNTIME) - @Transactional(rollbackFor=Exception.class, noRollbackFor={IOException.class}) + @Transactional(rollbackFor = Exception.class, noRollbackFor = IOException.class) @interface Tx { } @@ -618,13 +633,13 @@ public class AnnotationTransactionAttributeSourceTests { @Retention(RetentionPolicy.RUNTIME) - @Transactional(rollbackFor=Exception.class, noRollbackFor={IOException.class}) + @Transactional(rollbackFor = Exception.class, noRollbackFor = IOException.class) @interface TxWithAttribute { boolean readOnly(); } - @TxWithAttribute(readOnly=true) + @TxWithAttribute(readOnly = true) static class TestBean7 { public int getAge() { @@ -641,11 +656,14 @@ public class AnnotationTransactionAttributeSourceTests { } } + @TxWithAttribute(readOnly = true) interface TestInterface9 { + int getAge(); } + static class TestBean9 implements TestInterface9 { @Override @@ -654,12 +672,14 @@ public class AnnotationTransactionAttributeSourceTests { } } + interface TestInterface10 { - @TxWithAttribute(readOnly=true) + @TxWithAttribute(readOnly = true) int getAge(); } + static class TestBean10 implements TestInterface10 { @Override @@ -888,4 +908,56 @@ public class AnnotationTransactionAttributeSourceTests { } } + + @Transactional + static class GroovyTestBean implements ITestBean, GroovyObject { + + private String name; + + private int age; + + @Override + public String getName() { + return name; + } + + @Override + public void setName(String name) { + this.name = name; + } + + @Override + public int getAge() { + return age; + } + + @Override + public void setAge(int age) { + this.age = age; + } + + @Override + public Object invokeMethod(String name, Object args) { + return null; + } + + @Override + public Object getProperty(String propertyName) { + return null; + } + + @Override + public void setProperty(String propertyName, Object newValue) { + } + + @Override + public MetaClass getMetaClass() { + return null; + } + + @Override + public void setMetaClass(MetaClass metaClass) { + } + } + } diff --git a/spring-tx/src/test/java/org/springframework/transaction/annotation/EnableTransactionManagementTests.java b/spring-tx/src/test/java/org/springframework/transaction/annotation/EnableTransactionManagementTests.java index d413269d..4c301fea 100644 --- a/spring-tx/src/test/java/org/springframework/transaction/annotation/EnableTransactionManagementTests.java +++ b/spring-tx/src/test/java/org/springframework/transaction/annotation/EnableTransactionManagementTests.java @@ -94,6 +94,7 @@ public class EnableTransactionManagementTests { * get loaded -- or in this case, attempted to be loaded at which point the test fails. */ @Test + @SuppressWarnings("resource") public void proxyTypeAspectJCausesRegistrationOfAnnotationTransactionAspect() { try { new AnnotationConfigApplicationContext(EnableAspectJTxConfig.class, TxManagerConfig.class); diff --git a/spring-tx/src/test/java/org/springframework/transaction/config/AnnotationDrivenTests.java b/spring-tx/src/test/java/org/springframework/transaction/config/AnnotationDrivenTests.java index 3e561c04..f58d648c 100644 --- a/spring-tx/src/test/java/org/springframework/transaction/config/AnnotationDrivenTests.java +++ b/spring-tx/src/test/java/org/springframework/transaction/config/AnnotationDrivenTests.java @@ -20,10 +20,10 @@ import java.io.Serializable; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; - import org.junit.Test; import org.springframework.aop.support.AopUtils; +import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; @@ -47,8 +47,16 @@ public class AnnotationDrivenTests { @Test public void withConfigurationClass() throws Exception { + ApplicationContext parent = new AnnotationConfigApplicationContext(TransactionManagerConfiguration.class); + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"annotationDrivenConfigurationClassTests.xml"}, getClass(), parent); + doTestWithMultipleTransactionManagers(context); + } + + @Test + public void withAnnotatedTransactionManagers() throws Exception { AnnotationConfigApplicationContext parent = new AnnotationConfigApplicationContext(); - parent.register(TransactionManagerConfiguration.class); + parent.registerBeanDefinition("transactionManager1", new RootBeanDefinition(SynchTransactionManager.class)); + parent.registerBeanDefinition("transactionManager2", new RootBeanDefinition(NoSynchTransactionManager.class)); parent.refresh(); ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"annotationDrivenConfigurationClassTests.xml"}, getClass(), parent); doTestWithMultipleTransactionManagers(context); diff --git a/spring-tx/src/test/java/org/springframework/transaction/config/NoSynch.java b/spring-tx/src/test/java/org/springframework/transaction/config/NoSynch.java new file mode 100644 index 00000000..828e835e --- /dev/null +++ b/spring-tx/src/test/java/org/springframework/transaction/config/NoSynch.java @@ -0,0 +1,31 @@ +/* + * Copyright 2002-2015 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.transaction.config; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +import org.springframework.beans.factory.annotation.Qualifier; + +/** + * @author Juergen Hoeller + */ +@Qualifier("noSynch") +@Retention(RetentionPolicy.RUNTIME) +public @interface NoSynch { + +} diff --git a/spring-tx/src/test/java/org/springframework/transaction/config/NoSynchTransactionManager.java b/spring-tx/src/test/java/org/springframework/transaction/config/NoSynchTransactionManager.java new file mode 100644 index 00000000..1f6ec382 --- /dev/null +++ b/spring-tx/src/test/java/org/springframework/transaction/config/NoSynchTransactionManager.java @@ -0,0 +1,32 @@ +/* + * Copyright 2002-2015 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.transaction.config; + +import org.springframework.tests.transaction.CallCountingTransactionManager; + +/** + * @author Juergen Hoeller + */ +@NoSynch +@SuppressWarnings("serial") +public class NoSynchTransactionManager extends CallCountingTransactionManager { + + public NoSynchTransactionManager() { + setTransactionSynchronization(CallCountingTransactionManager.SYNCHRONIZATION_NEVER); + } + +} diff --git a/spring-tx/src/test/java/org/springframework/transaction/config/SynchTransactionManager.java b/spring-tx/src/test/java/org/springframework/transaction/config/SynchTransactionManager.java new file mode 100644 index 00000000..65c4496b --- /dev/null +++ b/spring-tx/src/test/java/org/springframework/transaction/config/SynchTransactionManager.java @@ -0,0 +1,29 @@ +/* + * Copyright 2002-2015 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.transaction.config; + +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.tests.transaction.CallCountingTransactionManager; + +/** + * @author Juergen Hoeller + */ +@Qualifier("synch") +@SuppressWarnings("serial") +public class SynchTransactionManager extends CallCountingTransactionManager { + +} diff --git a/spring-tx/src/test/java/org/springframework/transaction/config/TransactionManagerConfiguration.java b/spring-tx/src/test/java/org/springframework/transaction/config/TransactionManagerConfiguration.java index 965a0db0..fb3b21f4 100644 --- a/spring-tx/src/test/java/org/springframework/transaction/config/TransactionManagerConfiguration.java +++ b/spring-tx/src/test/java/org/springframework/transaction/config/TransactionManagerConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2015 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. @@ -35,7 +35,7 @@ public class TransactionManagerConfiguration { } @Bean - @Qualifier("noSynch") + @NoSynch public PlatformTransactionManager transactionManager2() { CallCountingTransactionManager tm = new CallCountingTransactionManager(); tm.setTransactionSynchronization(CallCountingTransactionManager.SYNCHRONIZATION_NEVER); diff --git a/spring-tx/src/test/java/org/springframework/transaction/event/ApplicationListenerMethodTransactionalAdapterTests.java b/spring-tx/src/test/java/org/springframework/transaction/event/ApplicationListenerMethodTransactionalAdapterTests.java index 901398f3..5ed470b9 100644 --- a/spring-tx/src/test/java/org/springframework/transaction/event/ApplicationListenerMethodTransactionalAdapterTests.java +++ b/spring-tx/src/test/java/org/springframework/transaction/event/ApplicationListenerMethodTransactionalAdapterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 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. @@ -25,6 +25,7 @@ import org.junit.rules.ExpectedException; import org.springframework.context.PayloadApplicationEvent; import org.springframework.context.event.ApplicationListenerMethodAdapter; import org.springframework.core.ResolvableType; +import org.springframework.core.annotation.AnnotatedElementUtils; import org.springframework.util.ReflectionUtils; import static org.junit.Assert.*; @@ -37,15 +38,6 @@ public class ApplicationListenerMethodTransactionalAdapterTests { @Rule public final ExpectedException thrown = ExpectedException.none(); - @Test - public void noAnnotation() { - Method m = ReflectionUtils.findMethod(SampleEvents.class, - "noAnnotation", String.class); - - thrown.expect(IllegalStateException.class); - thrown.expectMessage("noAnnotation"); - ApplicationListenerMethodTransactionalAdapter.findAnnotation(m); - } @Test public void defaultPhase() { @@ -78,7 +70,8 @@ public class ApplicationListenerMethodTransactionalAdapterTests { private void assertPhase(Method method, TransactionPhase expected) { assertNotNull("Method must not be null", method); - TransactionalEventListener annotation = ApplicationListenerMethodTransactionalAdapter.findAnnotation(method); + TransactionalEventListener annotation = + AnnotatedElementUtils.findMergedAnnotation(method, TransactionalEventListener.class); assertEquals("Wrong phase for '" + method + "'", expected, annotation.phase()); } @@ -96,10 +89,8 @@ public class ApplicationListenerMethodTransactionalAdapterTests { return ResolvableType.forClassWithGenerics(PayloadApplicationEvent.class, payloadType); } - static class SampleEvents { - public void noAnnotation(String data) { - } + static class SampleEvents { @TransactionalEventListener public void defaultPhase(String data) { @@ -117,7 +108,6 @@ public class ApplicationListenerMethodTransactionalAdapterTests { @TransactionalEventListener(String.class) public void valueSet() { } - } } |