11/*
2- * Copyright 2002-2015 the original author or authors.
2+ * Copyright 2002-2016 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1818
1919import java .lang .reflect .Method ;
2020
21+ import org .junit .Before ;
22+ import org .junit .Test ;
23+
2124import org .springframework .tests .transaction .CallCountingTransactionManager ;
2225import org .springframework .transaction .annotation .AnnotationTransactionAttributeSource ;
23- import org .springframework .transaction .interceptor .TransactionAspectSupport ;
2426import org .springframework .transaction .interceptor .TransactionAttribute ;
2527
28+ import static org .junit .Assert .*;
29+
2630/**
2731 * @author Rod Johnson
2832 * @author Ramnivas Laddad
2933 * @author Juergen Hoeller
3034 * @author Sam Brannen
3135 */
32- @ SuppressWarnings ("deprecation" )
33- public class TransactionAspectTests extends org .springframework .test .AbstractDependencyInjectionSpringContextTests {
36+ public class TransactionAspectTests {
3437
35- private CallCountingTransactionManager txManager ;
38+ private final CallCountingTransactionManager txManager = new CallCountingTransactionManager () ;
3639
37- private TransactionalAnnotationOnlyOnClassWithNoInterface annotationOnlyOnClassWithNoInterface ;
40+ private final TransactionalAnnotationOnlyOnClassWithNoInterface annotationOnlyOnClassWithNoInterface =
41+ new TransactionalAnnotationOnlyOnClassWithNoInterface ();
3842
39- private ClassWithProtectedAnnotatedMember beanWithAnnotatedProtectedMethod ;
43+ private final ClassWithProtectedAnnotatedMember beanWithAnnotatedProtectedMethod =
44+ new ClassWithProtectedAnnotatedMember ();
4045
41- private ClassWithPrivateAnnotatedMember beanWithAnnotatedPrivateMethod ;
46+ private final ClassWithPrivateAnnotatedMember beanWithAnnotatedPrivateMethod =
47+ new ClassWithPrivateAnnotatedMember ();
4248
43- private MethodAnnotationOnClassWithNoInterface methodAnnotationOnly = new MethodAnnotationOnClassWithNoInterface ();
49+ private final MethodAnnotationOnClassWithNoInterface methodAnnotationOnly =
50+ new MethodAnnotationOnClassWithNoInterface ();
4451
4552
46- public void setAnnotationOnlyOnClassWithNoInterface (
47- TransactionalAnnotationOnlyOnClassWithNoInterface annotationOnlyOnClassWithNoInterface ) {
48- this .annotationOnlyOnClassWithNoInterface = annotationOnlyOnClassWithNoInterface ;
49- }
50-
51- public void setClassWithAnnotatedProtectedMethod (ClassWithProtectedAnnotatedMember aBean ) {
52- this .beanWithAnnotatedProtectedMethod = aBean ;
53- }
54-
55- public void setClassWithAnnotatedPrivateMethod (ClassWithPrivateAnnotatedMember aBean ) {
56- this .beanWithAnnotatedPrivateMethod = aBean ;
57- }
58-
59- public void setTransactionAspect (TransactionAspectSupport transactionAspect ) {
60- this .txManager = (CallCountingTransactionManager ) transactionAspect .getTransactionManager ();
61- }
62-
63-
64- @ Override
65- protected String [] getConfigPaths () {
66- return new String [] { "TransactionAspectTests-context.xml" };
53+ @ Before
54+ public void initContext () {
55+ AnnotationTransactionAspect .aspectOf ().setTransactionManager (txManager );
6756 }
6857
6958
59+ @ Test
7060 public void testCommitOnAnnotatedClass () throws Throwable {
7161 txManager .clear ();
7262 assertEquals (0 , txManager .begun );
7363 annotationOnlyOnClassWithNoInterface .echo (null );
7464 assertEquals (1 , txManager .commits );
7565 }
7666
67+ @ Test
7768 public void testCommitOnAnnotatedProtectedMethod () throws Throwable {
7869 txManager .clear ();
7970 assertEquals (0 , txManager .begun );
8071 beanWithAnnotatedProtectedMethod .doInTransaction ();
8172 assertEquals (1 , txManager .commits );
8273 }
8374
75+ @ Test
8476 public void testCommitOnAnnotatedPrivateMethod () throws Throwable {
8577 txManager .clear ();
8678 assertEquals (0 , txManager .begun );
8779 beanWithAnnotatedPrivateMethod .doSomething ();
8880 assertEquals (1 , txManager .commits );
8981 }
9082
83+ @ Test
9184 public void testNoCommitOnNonAnnotatedNonPublicMethodInTransactionalType () throws Throwable {
9285 txManager .clear ();
9386 assertEquals (0 ,txManager .begun );
9487 annotationOnlyOnClassWithNoInterface .nonTransactionalMethod ();
9588 assertEquals (0 ,txManager .begun );
9689 }
9790
91+ @ Test
9892 public void testCommitOnAnnotatedMethod () throws Throwable {
9993 txManager .clear ();
10094 assertEquals (0 , txManager .begun );
10195 methodAnnotationOnly .echo (null );
10296 assertEquals (1 , txManager .commits );
10397 }
10498
99+ @ Test
105100 public void testNotTransactional () throws Throwable {
106101 txManager .clear ();
107102 assertEquals (0 , txManager .begun );
108103 new NotTransactional ().noop ();
109104 assertEquals (0 , txManager .begun );
110105 }
111106
107+ @ Test
112108 public void testDefaultCommitOnAnnotatedClass () throws Throwable {
113109 final Exception ex = new Exception ();
114110 try {
@@ -125,6 +121,7 @@ public Object performTransactionalOperation() throws Throwable {
125121 }
126122 }
127123
124+ @ Test
128125 public void testDefaultRollbackOnAnnotatedClass () throws Throwable {
129126 final RuntimeException ex = new RuntimeException ();
130127 try {
@@ -141,10 +138,11 @@ public Object performTransactionalOperation() throws Throwable {
141138 }
142139 }
143140
141+ @ Test
144142 public void testDefaultCommitOnSubclassOfAnnotatedClass () throws Throwable {
145143 final Exception ex = new Exception ();
146144 try {
147- testRollback (new TransactionOperationCallback () {
145+ testRollback (new TransactionOperationCallback () {
148146 @ Override
149147 public Object performTransactionalOperation () throws Throwable {
150148 return new SubclassOfClassWithTransactionalAnnotation ().echo (ex );
@@ -157,6 +155,7 @@ public Object performTransactionalOperation() throws Throwable {
157155 }
158156 }
159157
158+ @ Test
160159 public void testDefaultCommitOnSubclassOfClassWithTransactionalMethodAnnotated () throws Throwable {
161160 final Exception ex = new Exception ();
162161 try {
@@ -173,6 +172,7 @@ public Object performTransactionalOperation() throws Throwable {
173172 }
174173 }
175174
175+ @ Test
176176 public void testDefaultCommitOnImplementationOfAnnotatedInterface () throws Throwable {
177177 final Exception ex = new Exception ();
178178 testNotTransactional (new TransactionOperationCallback () {
@@ -188,13 +188,15 @@ public Object performTransactionalOperation() throws Throwable {
188188 * it implements a transactionally annotated interface. This behaviour could only
189189 * be changed in AbstractFallbackTransactionAttributeSource in Spring proper.
190190 */
191+ @ Test
191192 public void testDoesNotResolveTxAnnotationOnMethodFromClassImplementingAnnotatedInterface () throws Exception {
192193 AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource ();
193194 Method m = ImplementsAnnotatedInterface .class .getMethod ("echo" , Throwable .class );
194195 TransactionAttribute ta = atas .getTransactionAttribute (m , ImplementsAnnotatedInterface .class );
195196 assertNull (ta );
196197 }
197198
199+ @ Test
198200 public void testDefaultRollbackOnImplementationOfAnnotatedInterface () throws Throwable {
199201 final Exception rollbackProvokingException = new RuntimeException ();
200202 testNotTransactional (new TransactionOperationCallback () {
@@ -237,15 +239,19 @@ protected void testNotTransactional(TransactionOperationCallback toc, Throwable
237239
238240
239241 private interface TransactionOperationCallback {
242+
240243 Object performTransactionalOperation () throws Throwable ;
241244 }
242245
246+
243247 public static class SubclassOfClassWithTransactionalAnnotation extends TransactionalAnnotationOnlyOnClassWithNoInterface {
244248 }
245249
250+
246251 public static class SubclassOfClassWithTransactionalMethodAnnotation extends MethodAnnotationOnClassWithNoInterface {
247252 }
248253
254+
249255 public static class ImplementsAnnotatedInterface implements ITransactional {
250256
251257 @ Override
@@ -257,6 +263,7 @@ public Object echo(Throwable t) throws Throwable {
257263 }
258264 }
259265
266+
260267 public static class NotTransactional {
261268
262269 public void noop () {
0 commit comments