32
32
import org .springframework .aop .framework .ProxyFactory ;
33
33
import org .springframework .beans .MutablePropertyValues ;
34
34
import org .springframework .beans .factory .FactoryBean ;
35
+ import org .springframework .context .ApplicationContext ;
35
36
import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
36
37
import org .springframework .context .annotation .Bean ;
37
38
import org .springframework .context .annotation .Configuration ;
50
51
import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
51
52
52
53
/**
54
+ * Tests for proxy-based method validation via {@link MethodValidationInterceptor}
55
+ * and/or {@link MethodValidationPostProcessor}.
56
+ *
53
57
* @author Juergen Hoeller
58
+ * @author Rossen Stoyanchevß
54
59
*/
55
- public class MethodValidationTests {
60
+ public class MethodValidationProxyTests {
56
61
57
62
@ Test
58
63
@ SuppressWarnings ("unchecked" )
59
64
public void testMethodValidationInterceptor () {
60
65
MyValidBean bean = new MyValidBean ();
61
- ProxyFactory proxyFactory = new ProxyFactory (bean );
62
- proxyFactory .addAdvice (new MethodValidationInterceptor ());
63
- proxyFactory .addAdvisor (new AsyncAnnotationAdvisor ());
64
- doTestProxyValidation ((MyValidInterface <String >) proxyFactory .getProxy ());
66
+ ProxyFactory factory = new ProxyFactory (bean );
67
+ factory .addAdvice (new MethodValidationInterceptor ());
68
+ factory .addAdvisor (new AsyncAnnotationAdvisor ());
69
+ doTestProxyValidation ((MyValidInterface <String >) factory .getProxy ());
65
70
}
66
71
67
72
@ Test
68
73
@ SuppressWarnings ("unchecked" )
69
74
public void testMethodValidationPostProcessor () {
70
- StaticApplicationContext ac = new StaticApplicationContext ();
71
- ac .registerSingleton ("mvpp" , MethodValidationPostProcessor .class );
75
+ StaticApplicationContext context = new StaticApplicationContext ();
76
+ context .registerSingleton ("mvpp" , MethodValidationPostProcessor .class );
72
77
MutablePropertyValues pvs = new MutablePropertyValues ();
73
78
pvs .add ("beforeExistingAdvisors" , false );
74
- ac .registerSingleton ("aapp" , AsyncAnnotationBeanPostProcessor .class , pvs );
75
- ac .registerSingleton ("bean" , MyValidBean .class );
76
- ac .refresh ();
77
- doTestProxyValidation (ac .getBean ("bean" , MyValidInterface .class ));
78
- ac .close ();
79
+ context .registerSingleton ("aapp" , AsyncAnnotationBeanPostProcessor .class , pvs );
80
+ context .registerSingleton ("bean" , MyValidBean .class );
81
+ context .refresh ();
82
+ doTestProxyValidation (context .getBean ("bean" , MyValidInterface .class ));
83
+ context .close ();
79
84
}
80
85
81
86
@ Test // gh-29782
@@ -90,46 +95,41 @@ public void testMethodValidationPostProcessorForInterfaceOnlyProxy() {
90
95
context .close ();
91
96
}
92
97
98
+ @ SuppressWarnings ("DataFlowIssue" )
93
99
private void doTestProxyValidation (MyValidInterface <String > proxy ) {
94
100
assertThat (proxy .myValidMethod ("value" , 5 )).isNotNull ();
95
- assertThatExceptionOfType (ValidationException .class ).isThrownBy (() ->
96
- proxy .myValidMethod ("value" , 15 ));
97
- assertThatExceptionOfType (ValidationException .class ).isThrownBy (() ->
98
- proxy .myValidMethod (null , 5 ));
99
- assertThatExceptionOfType (ValidationException .class ).isThrownBy (() ->
100
- proxy .myValidMethod ("value" , 0 ));
101
+ assertThatExceptionOfType (ValidationException .class ).isThrownBy (() -> proxy .myValidMethod ("value" , 15 ));
102
+ assertThatExceptionOfType (ValidationException .class ).isThrownBy (() -> proxy .myValidMethod (null , 5 ));
103
+ assertThatExceptionOfType (ValidationException .class ).isThrownBy (() -> proxy .myValidMethod ("value" , 0 ));
101
104
proxy .myValidAsyncMethod ("value" , 5 );
102
- assertThatExceptionOfType (ValidationException .class ).isThrownBy (() ->
103
- proxy .myValidAsyncMethod ("value" , 15 ));
104
- assertThatExceptionOfType (ValidationException .class ).isThrownBy (() ->
105
- proxy .myValidAsyncMethod (null , 5 ));
105
+ assertThatExceptionOfType (ValidationException .class ).isThrownBy (() -> proxy .myValidAsyncMethod ("value" , 15 ));
106
+ assertThatExceptionOfType (ValidationException .class ).isThrownBy (() -> proxy .myValidAsyncMethod (null , 5 ));
106
107
assertThat (proxy .myGenericMethod ("myValue" )).isEqualTo ("myValue" );
107
- assertThatExceptionOfType (ValidationException .class ).isThrownBy (() ->
108
- proxy .myGenericMethod (null ));
108
+ assertThatExceptionOfType (ValidationException .class ).isThrownBy (() -> proxy .myGenericMethod (null ));
109
109
}
110
110
111
111
@ Test
112
112
public void testLazyValidatorForMethodValidation () {
113
- @ SuppressWarnings ("resource" )
114
- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (
113
+ ApplicationContext context = new AnnotationConfigApplicationContext (
115
114
LazyMethodValidationConfig .class , CustomValidatorBean .class ,
116
115
MyValidBean .class , MyValidFactoryBean .class );
117
- ctx .getBeansOfType (MyValidInterface .class ).values ().forEach (bean -> bean .myValidMethod ("value" , 5 ));
116
+ context .getBeansOfType (MyValidInterface .class ).values ().forEach (bean -> bean .myValidMethod ("value" , 5 ));
118
117
}
119
118
120
119
@ Test
121
120
public void testLazyValidatorForMethodValidationWithProxyTargetClass () {
122
- @ SuppressWarnings ("resource" )
123
- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (
121
+ ApplicationContext context = new AnnotationConfigApplicationContext (
124
122
LazyMethodValidationConfigWithProxyTargetClass .class , CustomValidatorBean .class ,
125
123
MyValidBean .class , MyValidFactoryBean .class );
126
- ctx .getBeansOfType (MyValidInterface .class ).values ().forEach (bean -> bean .myValidMethod ("value" , 5 ));
124
+ context .getBeansOfType (MyValidInterface .class ).values ().forEach (bean -> bean .myValidMethod ("value" , 5 ));
127
125
}
128
126
129
127
130
128
@ MyStereotype
131
129
public static class MyValidBean implements MyValidInterface <String > {
132
130
131
+ @ SuppressWarnings ("DataFlowIssue" )
132
+ @ NotNull
133
133
@ Override
134
134
public Object myValidMethod (String arg1 , int arg2 ) {
135
135
return (arg2 == 0 ? null : "value" );
@@ -159,6 +159,8 @@ public Class<?> getObjectType() {
159
159
return String .class ;
160
160
}
161
161
162
+ @ SuppressWarnings ("DataFlowIssue" )
163
+ @ NotNull
162
164
@ Override
163
165
public Object myValidMethod (String arg1 , int arg2 ) {
164
166
return (arg2 == 0 ? null : "value" );
@@ -178,10 +180,12 @@ public String myGenericMethod(String value) {
178
180
@ MyStereotype
179
181
public interface MyValidInterface <T > {
180
182
181
- @ NotNull Object myValidMethod (@ NotNull (groups = MyGroup .class ) String arg1 , @ Max (10 ) int arg2 );
183
+ @ NotNull
184
+ Object myValidMethod (@ NotNull (groups = MyGroup .class ) String arg1 , @ Max (10 ) int arg2 );
182
185
183
186
@ MyValid
184
- @ Async void myValidAsyncMethod (@ NotNull (groups = OtherGroup .class ) String arg1 , @ Max (10 ) int arg2 );
187
+ @ Async
188
+ void myValidAsyncMethod (@ NotNull (groups = OtherGroup .class ) String arg1 , @ Max (10 ) int arg2 );
185
189
186
190
T myGenericMethod (@ NotNull T value );
187
191
}
0 commit comments