16
16
17
17
package org .springframework .aop .framework ;
18
18
19
- import java .io .Serial ;
20
19
import java .lang .reflect .Proxy ;
21
20
import java .lang .reflect .UndeclaredThrowableException ;
22
21
import java .util .Objects ;
36
35
import static org .mockito .BDDMockito .doThrow ;
37
36
import static org .mockito .BDDMockito .mock ;
38
37
38
+ /**
39
+ * @author Mikaël Francoeur
40
+ * @since 6.2
41
+ */
39
42
abstract class ProxyExceptionHandlingTests implements WithAssertions {
40
43
41
44
private static final RuntimeException uncheckedException = new RuntimeException ();
45
+
42
46
private static final DeclaredCheckedException declaredCheckedException = new DeclaredCheckedException ();
47
+
43
48
private static final UndeclaredCheckedException undeclaredCheckedException = new UndeclaredCheckedException ();
44
49
45
50
protected final MyClass target = mock (MyClass .class );
51
+
46
52
protected final ProxyFactory proxyFactory = new ProxyFactory (target );
47
53
48
54
@ Nullable
49
55
protected MyInterface proxy ;
56
+
50
57
@ Nullable
51
58
private Throwable throwableSeenByCaller ;
52
59
53
- static class ObjenesisCglibAopProxyTests extends ProxyExceptionHandlingTests {
54
- @ BeforeEach
55
- void beforeEach () {
56
- proxyFactory .setProxyTargetClass (true );
57
- }
58
60
59
- @ Override
60
- protected void assertProxyType (Object proxy ) {
61
- assertThat (Enhancer .isEnhanced (proxy .getClass ())).isTrue ();
62
- }
61
+ @ BeforeEach
62
+ void clear () {
63
+ Mockito .clearInvocations (target );
63
64
}
64
65
66
+ protected void assertProxyType (Object proxy ) {
67
+ }
68
+
69
+ private void invokeProxy () {
70
+ throwableSeenByCaller = catchThrowable (() -> Objects .requireNonNull (proxy ).doSomething ());
71
+ }
72
+
73
+ @ SuppressWarnings ("SameParameterValue" )
74
+ private Answer <?> sneakyThrow (Throwable throwable ) {
75
+ return invocation -> {
76
+ throw throwable ;
77
+ };
78
+ }
79
+
80
+
65
81
static class JdkAopProxyTests extends ProxyExceptionHandlingTests {
82
+
66
83
@ Override
67
84
protected void assertProxyType (Object proxy ) {
68
85
assertThat (Proxy .isProxyClass (proxy .getClass ())).isTrue ();
69
86
}
70
87
}
71
88
72
- protected void assertProxyType (Object proxy ) {
73
- }
74
89
75
- @ BeforeEach
76
- void beforeEach () {
77
- Mockito .clearInvocations (target );
90
+ static class CglibAopProxyTests extends ProxyExceptionHandlingTests {
91
+
92
+ @ BeforeEach
93
+ void setup () {
94
+ proxyFactory .setProxyTargetClass (true );
95
+ }
96
+
97
+ @ Override
98
+ protected void assertProxyType (Object proxy ) {
99
+ assertThat (Enhancer .isEnhanced (proxy .getClass ())).isTrue ();
100
+ }
78
101
}
79
102
103
+
80
104
@ Nested
81
105
class WhenThereIsOneInterceptor {
82
106
@@ -86,18 +110,14 @@ class WhenThereIsOneInterceptor {
86
110
@ BeforeEach
87
111
void beforeEach () {
88
112
proxyFactory .addAdvice (captureThrowable ());
89
-
90
- proxy = (MyInterface ) proxyFactory .getProxy ();
91
-
113
+ proxy = (MyInterface ) proxyFactory .getProxy (ProxyExceptionHandlingTests .class .getClassLoader ());
92
114
assertProxyType (proxy );
93
115
}
94
116
95
117
@ Test
96
118
void targetThrowsUndeclaredCheckedException () throws DeclaredCheckedException {
97
119
doAnswer (sneakyThrow (undeclaredCheckedException )).when (target ).doSomething ();
98
-
99
120
invokeProxy ();
100
-
101
121
assertThat (throwableSeenByInterceptor ).isSameAs (undeclaredCheckedException );
102
122
assertThat (throwableSeenByCaller )
103
123
.isInstanceOf (UndeclaredThrowableException .class )
@@ -107,19 +127,15 @@ void targetThrowsUndeclaredCheckedException() throws DeclaredCheckedException {
107
127
@ Test
108
128
void targetThrowsDeclaredCheckedException () throws DeclaredCheckedException {
109
129
doThrow (declaredCheckedException ).when (target ).doSomething ();
110
-
111
130
invokeProxy ();
112
-
113
131
assertThat (throwableSeenByInterceptor ).isSameAs (declaredCheckedException );
114
132
assertThat (throwableSeenByCaller ).isSameAs (declaredCheckedException );
115
133
}
116
134
117
135
@ Test
118
136
void targetThrowsUncheckedException () throws DeclaredCheckedException {
119
137
doThrow (uncheckedException ).when (target ).doSomething ();
120
-
121
138
invokeProxy ();
122
-
123
139
assertThat (throwableSeenByInterceptor ).isSameAs (uncheckedException );
124
140
assertThat (throwableSeenByCaller ).isSameAs (uncheckedException );
125
141
}
@@ -129,30 +145,28 @@ private MethodInterceptor captureThrowable() {
129
145
try {
130
146
return invocation .proceed ();
131
147
}
132
- catch (Exception e ) {
133
- throwableSeenByInterceptor = e ;
134
- throw e ;
148
+ catch (Exception ex ) {
149
+ throwableSeenByInterceptor = ex ;
150
+ throw ex ;
135
151
}
136
152
};
137
153
}
138
154
}
139
155
156
+
140
157
@ Nested
141
158
class WhenThereAreNoInterceptors {
142
159
143
160
@ BeforeEach
144
161
void beforeEach () {
145
- proxy = (MyInterface ) proxyFactory .getProxy ();
146
-
162
+ proxy = (MyInterface ) proxyFactory .getProxy (ProxyExceptionHandlingTests .class .getClassLoader ());
147
163
assertProxyType (proxy );
148
164
}
149
165
150
166
@ Test
151
167
void targetThrowsUndeclaredCheckedException () throws DeclaredCheckedException {
152
168
doAnswer (sneakyThrow (undeclaredCheckedException )).when (target ).doSomething ();
153
-
154
169
invokeProxy ();
155
-
156
170
assertThat (throwableSeenByCaller )
157
171
.isInstanceOf (UndeclaredThrowableException .class )
158
172
.hasCauseReference (undeclaredCheckedException );
@@ -161,51 +175,38 @@ void targetThrowsUndeclaredCheckedException() throws DeclaredCheckedException {
161
175
@ Test
162
176
void targetThrowsDeclaredCheckedException () throws DeclaredCheckedException {
163
177
doThrow (declaredCheckedException ).when (target ).doSomething ();
164
-
165
178
invokeProxy ();
166
-
167
179
assertThat (throwableSeenByCaller ).isSameAs (declaredCheckedException );
168
180
}
169
181
170
182
@ Test
171
183
void targetThrowsUncheckedException () throws DeclaredCheckedException {
172
184
doThrow (uncheckedException ).when (target ).doSomething ();
173
-
174
185
invokeProxy ();
175
-
176
186
assertThat (throwableSeenByCaller ).isSameAs (uncheckedException );
177
187
}
178
188
}
179
189
180
- private void invokeProxy () {
181
- throwableSeenByCaller = catchThrowable (() -> Objects .requireNonNull (proxy ).doSomething ());
182
- }
183
190
184
- private Answer <?> sneakyThrow (@ SuppressWarnings ("SameParameterValue" ) Throwable throwable ) {
185
- return invocation -> {
186
- throw throwable ;
187
- };
191
+ protected interface MyInterface {
192
+
193
+ void doSomething () throws DeclaredCheckedException ;
188
194
}
189
195
190
196
static class MyClass implements MyInterface {
197
+
191
198
@ Override
192
199
public void doSomething () throws DeclaredCheckedException {
193
200
throw declaredCheckedException ;
194
201
}
195
202
}
196
203
197
- protected interface MyInterface {
198
- void doSomething () throws DeclaredCheckedException ;
199
- }
200
-
204
+ @ SuppressWarnings ("serial" )
201
205
protected static class UndeclaredCheckedException extends Exception {
202
- @ Serial
203
- private static final long serialVersionUID = -4199611059122356651L ;
204
206
}
205
207
208
+ @ SuppressWarnings ("serial" )
206
209
protected static class DeclaredCheckedException extends Exception {
207
- @ Serial
208
- private static final long serialVersionUID = 8851375818059230518L ;
209
210
}
210
211
211
212
}
0 commit comments