1
1
/*
2
- * Copyright 2002-2015 the original author or authors.
2
+ * Copyright 2002-2017 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
18
18
19
19
import java .lang .reflect .Method ;
20
20
21
+ import org .junit .Before ;
22
+ import org .junit .Test ;
23
+
21
24
import org .springframework .tests .transaction .CallCountingTransactionManager ;
22
25
import org .springframework .transaction .annotation .AnnotationTransactionAttributeSource ;
23
- import org .springframework .transaction .interceptor .TransactionAspectSupport ;
24
26
import org .springframework .transaction .interceptor .TransactionAttribute ;
25
27
28
+ import static org .junit .Assert .*;
29
+
26
30
/**
27
31
* @author Rod Johnson
28
32
* @author Ramnivas Laddad
29
33
* @author Juergen Hoeller
30
34
* @author Sam Brannen
31
35
*/
32
- @ SuppressWarnings ("deprecation" )
33
- public class TransactionAspectTests extends org .springframework .test .AbstractDependencyInjectionSpringContextTests {
36
+ public class TransactionAspectTests {
34
37
35
- private CallCountingTransactionManager txManager ;
38
+ private final CallCountingTransactionManager txManager = new CallCountingTransactionManager () ;
36
39
37
- private TransactionalAnnotationOnlyOnClassWithNoInterface annotationOnlyOnClassWithNoInterface ;
40
+ private final TransactionalAnnotationOnlyOnClassWithNoInterface annotationOnlyOnClassWithNoInterface =
41
+ new TransactionalAnnotationOnlyOnClassWithNoInterface ();
38
42
39
- private ClassWithProtectedAnnotatedMember beanWithAnnotatedProtectedMethod ;
43
+ private final ClassWithProtectedAnnotatedMember beanWithAnnotatedProtectedMethod =
44
+ new ClassWithProtectedAnnotatedMember ();
40
45
41
- private ClassWithPrivateAnnotatedMember beanWithAnnotatedPrivateMethod ;
46
+ private final ClassWithPrivateAnnotatedMember beanWithAnnotatedPrivateMethod =
47
+ new ClassWithPrivateAnnotatedMember ();
42
48
43
- private MethodAnnotationOnClassWithNoInterface methodAnnotationOnly = new MethodAnnotationOnClassWithNoInterface ();
49
+ private final MethodAnnotationOnClassWithNoInterface methodAnnotationOnly =
50
+ new MethodAnnotationOnClassWithNoInterface ();
44
51
45
52
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 );
67
56
}
68
57
69
58
59
+ @ Test
70
60
public void testCommitOnAnnotatedClass () throws Throwable {
71
61
txManager .clear ();
72
62
assertEquals (0 , txManager .begun );
73
63
annotationOnlyOnClassWithNoInterface .echo (null );
74
64
assertEquals (1 , txManager .commits );
75
65
}
76
66
67
+ @ Test
77
68
public void testCommitOnAnnotatedProtectedMethod () throws Throwable {
78
69
txManager .clear ();
79
70
assertEquals (0 , txManager .begun );
80
71
beanWithAnnotatedProtectedMethod .doInTransaction ();
81
72
assertEquals (1 , txManager .commits );
82
73
}
83
74
75
+ @ Test
84
76
public void testCommitOnAnnotatedPrivateMethod () throws Throwable {
85
77
txManager .clear ();
86
78
assertEquals (0 , txManager .begun );
87
79
beanWithAnnotatedPrivateMethod .doSomething ();
88
80
assertEquals (1 , txManager .commits );
89
81
}
90
82
83
+ @ Test
91
84
public void testNoCommitOnNonAnnotatedNonPublicMethodInTransactionalType () throws Throwable {
92
85
txManager .clear ();
93
86
assertEquals (0 ,txManager .begun );
94
87
annotationOnlyOnClassWithNoInterface .nonTransactionalMethod ();
95
88
assertEquals (0 ,txManager .begun );
96
89
}
97
90
91
+ @ Test
98
92
public void testCommitOnAnnotatedMethod () throws Throwable {
99
93
txManager .clear ();
100
94
assertEquals (0 , txManager .begun );
101
95
methodAnnotationOnly .echo (null );
102
96
assertEquals (1 , txManager .commits );
103
97
}
104
98
99
+ @ Test
105
100
public void testNotTransactional () throws Throwable {
106
101
txManager .clear ();
107
102
assertEquals (0 , txManager .begun );
108
103
new NotTransactional ().noop ();
109
104
assertEquals (0 , txManager .begun );
110
105
}
111
106
107
+ @ Test
112
108
public void testDefaultCommitOnAnnotatedClass () throws Throwable {
113
109
final Exception ex = new Exception ();
114
110
try {
@@ -125,6 +121,7 @@ public Object performTransactionalOperation() throws Throwable {
125
121
}
126
122
}
127
123
124
+ @ Test
128
125
public void testDefaultRollbackOnAnnotatedClass () throws Throwable {
129
126
final RuntimeException ex = new RuntimeException ();
130
127
try {
@@ -141,10 +138,11 @@ public Object performTransactionalOperation() throws Throwable {
141
138
}
142
139
}
143
140
141
+ @ Test
144
142
public void testDefaultCommitOnSubclassOfAnnotatedClass () throws Throwable {
145
143
final Exception ex = new Exception ();
146
144
try {
147
- testRollback (new TransactionOperationCallback () {
145
+ testRollback (new TransactionOperationCallback () {
148
146
@ Override
149
147
public Object performTransactionalOperation () throws Throwable {
150
148
return new SubclassOfClassWithTransactionalAnnotation ().echo (ex );
@@ -157,6 +155,7 @@ public Object performTransactionalOperation() throws Throwable {
157
155
}
158
156
}
159
157
158
+ @ Test
160
159
public void testDefaultCommitOnSubclassOfClassWithTransactionalMethodAnnotated () throws Throwable {
161
160
final Exception ex = new Exception ();
162
161
try {
@@ -173,6 +172,7 @@ public Object performTransactionalOperation() throws Throwable {
173
172
}
174
173
}
175
174
175
+ @ Test
176
176
public void testDefaultCommitOnImplementationOfAnnotatedInterface () throws Throwable {
177
177
final Exception ex = new Exception ();
178
178
testNotTransactional (new TransactionOperationCallback () {
@@ -185,16 +185,19 @@ public Object performTransactionalOperation() throws Throwable {
185
185
186
186
/**
187
187
* Note: resolution does not occur. Thus we can't make a class transactional if
188
- * it implements a transactionally annotated interface. This behaviour could only
188
+ * it implements a transactionally annotated interface. This behavior could only
189
189
* be changed in AbstractFallbackTransactionAttributeSource in Spring proper.
190
+ * See SPR-14322.
190
191
*/
192
+ @ Test
191
193
public void testDoesNotResolveTxAnnotationOnMethodFromClassImplementingAnnotatedInterface () throws Exception {
192
194
AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource ();
193
- Method m = ImplementsAnnotatedInterface .class .getMethod ("echo" , Throwable .class );
194
- TransactionAttribute ta = atas .getTransactionAttribute (m , ImplementsAnnotatedInterface .class );
195
+ Method method = ImplementsAnnotatedInterface .class .getMethod ("echo" , Throwable .class );
196
+ TransactionAttribute ta = atas .getTransactionAttribute (method , ImplementsAnnotatedInterface .class );
195
197
assertNull (ta );
196
198
}
197
199
200
+ @ Test
198
201
public void testDefaultRollbackOnImplementationOfAnnotatedInterface () throws Throwable {
199
202
final Exception rollbackProvokingException = new RuntimeException ();
200
203
testNotTransactional (new TransactionOperationCallback () {
@@ -237,15 +240,19 @@ protected void testNotTransactional(TransactionOperationCallback toc, Throwable
237
240
238
241
239
242
private interface TransactionOperationCallback {
243
+
240
244
Object performTransactionalOperation () throws Throwable ;
241
245
}
242
246
247
+
243
248
public static class SubclassOfClassWithTransactionalAnnotation extends TransactionalAnnotationOnlyOnClassWithNoInterface {
244
249
}
245
250
251
+
246
252
public static class SubclassOfClassWithTransactionalMethodAnnotation extends MethodAnnotationOnClassWithNoInterface {
247
253
}
248
254
255
+
249
256
public static class ImplementsAnnotatedInterface implements ITransactional {
250
257
251
258
@ Override
@@ -257,6 +264,7 @@ public Object echo(Throwable t) throws Throwable {
257
264
}
258
265
}
259
266
267
+
260
268
public static class NotTransactional {
261
269
262
270
public void noop () {
0 commit comments