1
1
/*
2
- * Copyright 2002-2021 the original author or authors.
2
+ * Copyright 2002-2023 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.
28
28
import org .springframework .aop .Pointcut ;
29
29
import org .springframework .aop .support .AopUtils ;
30
30
import org .springframework .aop .support .DefaultPointcutAdvisor ;
31
+ import org .springframework .aop .support .annotation .AnnotationMatchingPointcut ;
31
32
import org .springframework .aop .testfixture .advice .CountingBeforeAdvice ;
32
33
import org .springframework .aop .testfixture .interceptor .NopInterceptor ;
33
34
import org .springframework .beans .testfixture .beans .ITestBean ;
34
35
import org .springframework .beans .testfixture .beans .TestBean ;
35
36
import org .springframework .context .ApplicationContext ;
36
37
import org .springframework .context .ApplicationContextException ;
37
38
import org .springframework .context .support .ClassPathXmlApplicationContext ;
39
+ import org .springframework .lang .NonNull ;
40
+ import org .springframework .lang .Nullable ;
38
41
39
42
import static org .assertj .core .api .Assertions .assertThat ;
40
43
import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
@@ -87,8 +90,7 @@ public void testNoTarget() {
87
90
AdvisedSupport pc = new AdvisedSupport (ITestBean .class );
88
91
pc .addAdvice (new NopInterceptor ());
89
92
AopProxy aop = createAopProxy (pc );
90
- assertThatExceptionOfType (AopConfigException .class ).isThrownBy (
91
- aop ::getProxy );
93
+ assertThatExceptionOfType (AopConfigException .class ).isThrownBy (aop ::getProxy );
92
94
}
93
95
94
96
@ Test
@@ -136,8 +138,8 @@ public void testProxyCanBeClassNotInterface() {
136
138
137
139
Object proxy = aop .getProxy ();
138
140
assertThat (AopUtils .isCglibProxy (proxy )).isTrue ();
139
- assertThat (proxy instanceof ITestBean ). isTrue ( );
140
- assertThat (proxy instanceof TestBean ). isTrue ( );
141
+ assertThat (proxy ). isInstanceOf ( ITestBean . class );
142
+ assertThat (proxy ). isInstanceOf ( TestBean . class );
141
143
142
144
TestBean tb = (TestBean ) proxy ;
143
145
assertThat (tb .getAge ()).isEqualTo (32 );
@@ -304,6 +306,8 @@ public void testProxyAProxyWithAdditionalInterface() {
304
306
CglibAopProxy cglib = new CglibAopProxy (as );
305
307
306
308
ITestBean proxy1 = (ITestBean ) cglib .getProxy ();
309
+ ITestBean proxy1a = (ITestBean ) cglib .getProxy ();
310
+ assertThat (proxy1a .getClass ()).isSameAs (proxy1 .getClass ());
307
311
308
312
mockTargetSource .setTarget (proxy1 );
309
313
as = new AdvisedSupport (new Class <?>[]{});
@@ -312,7 +316,40 @@ public void testProxyAProxyWithAdditionalInterface() {
312
316
cglib = new CglibAopProxy (as );
313
317
314
318
ITestBean proxy2 = (ITestBean ) cglib .getProxy ();
315
- assertThat (proxy2 instanceof Serializable ).isTrue ();
319
+ assertThat (proxy2 ).isInstanceOf (Serializable .class );
320
+ assertThat (proxy2 .getClass ()).isNotSameAs (proxy1 .getClass ());
321
+
322
+ ITestBean proxy2a = (ITestBean ) cglib .getProxy ();
323
+ assertThat (proxy2a ).isInstanceOf (Serializable .class );
324
+ assertThat (proxy2a .getClass ()).isSameAs (proxy2 .getClass ());
325
+
326
+ mockTargetSource .setTarget (proxy1 );
327
+ as = new AdvisedSupport (new Class <?>[]{});
328
+ as .setTargetSource (mockTargetSource );
329
+ as .addAdvisor (new DefaultPointcutAdvisor (new AnnotationMatchingPointcut (Nullable .class ), new NopInterceptor ()));
330
+ cglib = new CglibAopProxy (as );
331
+
332
+ ITestBean proxy3 = (ITestBean ) cglib .getProxy ();
333
+ assertThat (proxy3 ).isInstanceOf (Serializable .class );
334
+ assertThat (proxy3 .getClass ()).isNotSameAs (proxy2 .getClass ());
335
+
336
+ ITestBean proxy3a = (ITestBean ) cglib .getProxy ();
337
+ assertThat (proxy3a ).isInstanceOf (Serializable .class );
338
+ assertThat (proxy3a .getClass ()).isSameAs (proxy3 .getClass ());
339
+
340
+ mockTargetSource .setTarget (proxy1 );
341
+ as = new AdvisedSupport (new Class <?>[]{});
342
+ as .setTargetSource (mockTargetSource );
343
+ as .addAdvisor (new DefaultPointcutAdvisor (new AnnotationMatchingPointcut (NonNull .class ), new NopInterceptor ()));
344
+ cglib = new CglibAopProxy (as );
345
+
346
+ ITestBean proxy4 = (ITestBean ) cglib .getProxy ();
347
+ assertThat (proxy4 ).isInstanceOf (Serializable .class );
348
+ assertThat (proxy4 .getClass ()).isNotSameAs (proxy3 .getClass ());
349
+
350
+ ITestBean proxy4a = (ITestBean ) cglib .getProxy ();
351
+ assertThat (proxy4a ).isInstanceOf (Serializable .class );
352
+ assertThat (proxy4a .getClass ()).isSameAs (proxy4 .getClass ());
316
353
}
317
354
318
355
@ Test
@@ -331,7 +368,7 @@ public void testExceptionHandling() {
331
368
proxy .doTest ();
332
369
}
333
370
catch (Exception ex ) {
334
- assertThat (ex instanceof ApplicationContextException ).as ("Invalid exception class" ).isTrue ( );
371
+ assertThat (ex ).as ("Invalid exception class" ).isInstanceOf ( ApplicationContextException . class );
335
372
}
336
373
337
374
assertThat (proxy .isCatchInvoked ()).as ("Catch was not invoked" ).isTrue ();
0 commit comments