-
Notifications
You must be signed in to change notification settings - Fork 37.7k
/
TransactionAspectTests.java
209 lines (167 loc) · 6.3 KB
/
TransactionAspectTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
* Copyright 2002-2022 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
*
* https://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.aspectj;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.transaction.testfixture.CallCountingTransactionManager;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatException;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatRuntimeException;
/**
* @author Rod Johnson
* @author Ramnivas Laddad
* @author Juergen Hoeller
* @author Sam Brannen
*/
public class TransactionAspectTests {
private final CallCountingTransactionManager txManager = new CallCountingTransactionManager();
private final TransactionalAnnotationOnlyOnClassWithNoInterface annotationOnlyOnClassWithNoInterface =
new TransactionalAnnotationOnlyOnClassWithNoInterface();
private final ClassWithProtectedAnnotatedMember beanWithAnnotatedProtectedMethod =
new ClassWithProtectedAnnotatedMember();
private final ClassWithPrivateAnnotatedMember beanWithAnnotatedPrivateMethod =
new ClassWithPrivateAnnotatedMember();
private final MethodAnnotationOnClassWithNoInterface methodAnnotationOnly =
new MethodAnnotationOnClassWithNoInterface();
@BeforeEach
public void initContext() {
AnnotationTransactionAspect.aspectOf().setTransactionManager(txManager);
}
@Test
public void testCommitOnAnnotatedClass() throws Throwable {
txManager.clear();
assertThat(txManager.begun).isEqualTo(0);
annotationOnlyOnClassWithNoInterface.echo(null);
assertThat(txManager.commits).isEqualTo(1);
}
@Test
public void commitOnAnnotatedProtectedMethod() throws Throwable {
txManager.clear();
assertThat(txManager.begun).isEqualTo(0);
beanWithAnnotatedProtectedMethod.doInTransaction();
assertThat(txManager.commits).isEqualTo(1);
}
@Test
public void commitOnAnnotatedPrivateMethod() throws Throwable {
txManager.clear();
assertThat(txManager.begun).isEqualTo(0);
beanWithAnnotatedPrivateMethod.doSomething();
assertThat(txManager.commits).isEqualTo(1);
}
@Test
public void commitOnNonAnnotatedNonPublicMethodInTransactionalType() throws Throwable {
txManager.clear();
assertThat(txManager.begun).isEqualTo(0);
annotationOnlyOnClassWithNoInterface.nonTransactionalMethod();
assertThat(txManager.begun).isEqualTo(0);
}
@Test
public void commitOnAnnotatedMethod() throws Throwable {
txManager.clear();
assertThat(txManager.begun).isEqualTo(0);
methodAnnotationOnly.echo(null);
assertThat(txManager.commits).isEqualTo(1);
}
@Test
public void notTransactional() throws Throwable {
txManager.clear();
assertThat(txManager.begun).isEqualTo(0);
new NotTransactional().noop();
assertThat(txManager.begun).isEqualTo(0);
}
@Test
public void defaultCommitOnAnnotatedClass() throws Throwable {
Exception ex = new Exception();
assertThatException()
.isThrownBy(() -> testRollback(() -> annotationOnlyOnClassWithNoInterface.echo(ex), false))
.isSameAs(ex);
}
@Test
public void defaultRollbackOnAnnotatedClass() throws Throwable {
RuntimeException ex = new RuntimeException();
assertThatRuntimeException()
.isThrownBy(() -> testRollback(() -> annotationOnlyOnClassWithNoInterface.echo(ex), true))
.isSameAs(ex);
}
@Test
public void defaultCommitOnSubclassOfAnnotatedClass() throws Throwable {
Exception ex = new Exception();
assertThatException()
.isThrownBy(() -> testRollback(() -> new SubclassOfClassWithTransactionalAnnotation().echo(ex), false))
.isSameAs(ex);
}
@Test
public void defaultCommitOnSubclassOfClassWithTransactionalMethodAnnotated() throws Throwable {
Exception ex = new Exception();
assertThatException()
.isThrownBy(() -> testRollback(() -> new SubclassOfClassWithTransactionalMethodAnnotation().echo(ex), false))
.isSameAs(ex);
}
@Test
public void noCommitOnImplementationOfAnnotatedInterface() throws Throwable {
Exception ex = new Exception();
testNotTransactional(() -> new ImplementsAnnotatedInterface().echo(ex), ex);
}
@Test
public void noRollbackOnImplementationOfAnnotatedInterface() throws Throwable {
Exception rollbackProvokingException = new RuntimeException();
testNotTransactional(() -> new ImplementsAnnotatedInterface().echo(rollbackProvokingException),
rollbackProvokingException);
}
protected void testRollback(TransactionOperationCallback toc, boolean rollback) throws Throwable {
txManager.clear();
assertThat(txManager.begun).isEqualTo(0);
try {
toc.performTransactionalOperation();
}
finally {
assertThat(txManager.begun).isEqualTo(1);
long expected1 = rollback ? 0 : 1;
assertThat(txManager.commits).isEqualTo(expected1);
long expected = rollback ? 1 : 0;
assertThat(txManager.rollbacks).isEqualTo(expected);
}
}
protected void testNotTransactional(TransactionOperationCallback toc, Throwable expected) throws Throwable {
txManager.clear();
assertThat(txManager.begun).isEqualTo(0);
assertThatExceptionOfType(Throwable.class)
.isThrownBy(toc::performTransactionalOperation)
.isSameAs(expected);
assertThat(txManager.begun).isEqualTo(0);
}
private interface TransactionOperationCallback {
Object performTransactionalOperation() throws Throwable;
}
public static class SubclassOfClassWithTransactionalAnnotation extends TransactionalAnnotationOnlyOnClassWithNoInterface {
}
public static class SubclassOfClassWithTransactionalMethodAnnotation extends MethodAnnotationOnClassWithNoInterface {
}
public static class ImplementsAnnotatedInterface implements ITransactional {
@Override
public Object echo(Throwable t) throws Throwable {
if (t != null) {
throw t;
}
return t;
}
}
public static class NotTransactional {
public void noop() {
}
}
}