23
23
import jakarta .validation .ConstraintViolationException ;
24
24
import jakarta .validation .Validation ;
25
25
import jakarta .validation .Validator ;
26
- import jakarta .validation .ValidatorFactory ;
27
26
import jakarta .validation .constraints .Max ;
28
27
import jakarta .validation .constraints .NotNull ;
29
28
import jakarta .validation .groups .Default ;
30
29
import org .aopalliance .intercept .MethodInterceptor ;
31
30
import org .aopalliance .intercept .MethodInvocation ;
32
31
import org .junit .jupiter .api .Test ;
32
+ import org .junit .jupiter .params .ParameterizedTest ;
33
+ import org .junit .jupiter .params .provider .ValueSource ;
33
34
34
35
import org .springframework .aop .framework .ProxyFactory ;
35
36
import org .springframework .beans .MutablePropertyValues ;
47
48
import org .springframework .scheduling .annotation .AsyncAnnotationBeanPostProcessor ;
48
49
import org .springframework .util .ClassUtils ;
49
50
import org .springframework .util .ReflectionUtils ;
50
- import org .springframework .util .function .SingletonSupplier ;
51
51
import org .springframework .validation .annotation .Validated ;
52
52
import org .springframework .validation .method .MethodValidationException ;
53
53
63
63
*/
64
64
class MethodValidationProxyTests {
65
65
66
- @ Test
66
+ @ ParameterizedTest
67
+ @ ValueSource (booleans = {true , false })
67
68
@ SuppressWarnings ("unchecked" )
68
- public void testMethodValidationInterceptor () {
69
+ void testMethodValidationInterceptor (boolean adaptViolations ) {
69
70
MyValidBean bean = new MyValidBean ();
70
71
ProxyFactory factory = new ProxyFactory (bean );
71
- factory .addAdvice (new MethodValidationInterceptor ());
72
+ factory .addAdvice (adaptViolations ?
73
+ new MethodValidationInterceptor (() -> Validation .buildDefaultValidatorFactory ().getValidator (), true ) :
74
+ new MethodValidationInterceptor ());
72
75
factory .addAdvisor (new AsyncAnnotationAdvisor ());
73
- doTestProxyValidation ((MyValidInterface <String >) factory .getProxy (), ConstraintViolationException .class );
74
- }
75
-
76
- @ Test
77
- @ SuppressWarnings ("unchecked" )
78
- public void testMethodValidationInterceptorWithAdaptConstraintViolations () {
79
- MyValidBean bean = new MyValidBean ();
80
- ProxyFactory factory = new ProxyFactory (bean );
81
- try (ValidatorFactory validatorFactory = Validation .buildDefaultValidatorFactory ()) {
82
- factory .addAdvice (new MethodValidationInterceptor (SingletonSupplier .of (validatorFactory .getValidator ()), true ));
83
- factory .addAdvisor (new AsyncAnnotationAdvisor ());
84
- doTestProxyValidation ((MyValidInterface <String >) factory .getProxy (), MethodValidationException .class );
85
- }
86
- }
87
-
88
- @ Test
89
- @ SuppressWarnings ("unchecked" )
90
- public void testMethodValidationPostProcessor () {
91
- StaticApplicationContext context = new StaticApplicationContext ();
92
- context .registerSingleton ("mvpp" , MethodValidationPostProcessor .class );
93
- MutablePropertyValues pvs = new MutablePropertyValues ();
94
- pvs .add ("beforeExistingAdvisors" , false );
95
- context .registerSingleton ("aapp" , AsyncAnnotationBeanPostProcessor .class , pvs );
96
- context .registerSingleton ("bean" , MyValidBean .class );
97
- context .refresh ();
98
- doTestProxyValidation (context .getBean ("bean" , MyValidInterface .class ), ConstraintViolationException .class );
99
- context .close ();
76
+ doTestProxyValidation ((MyValidInterface <String >) factory .getProxy (),
77
+ (adaptViolations ? MethodValidationException .class : ConstraintViolationException .class ));
100
78
}
101
79
102
- @ Test
80
+ @ ParameterizedTest
81
+ @ ValueSource (booleans = {true , false })
103
82
@ SuppressWarnings ("unchecked" )
104
- public void testMethodValidationPostProcessorWithAdaptConstraintViolations ( ) {
83
+ void testMethodValidationPostProcessor ( boolean adaptViolations ) {
105
84
StaticApplicationContext context = new StaticApplicationContext ();
106
- context .registerBean (MethodValidationPostProcessor .class , () -> {
107
- MethodValidationPostProcessor postProcessor = new MethodValidationPostProcessor ();
108
- postProcessor .setAdaptConstraintViolations (true );
109
- return postProcessor ;
110
- });
111
-
85
+ context .registerBean (MethodValidationPostProcessor .class , adaptViolations ?
86
+ () -> {
87
+ MethodValidationPostProcessor postProcessor = new MethodValidationPostProcessor ();
88
+ postProcessor .setAdaptConstraintViolations (true );
89
+ return postProcessor ;
90
+ } :
91
+ MethodValidationPostProcessor ::new );
112
92
MutablePropertyValues pvs = new MutablePropertyValues ();
113
93
pvs .add ("beforeExistingAdvisors" , false );
114
94
context .registerSingleton ("aapp" , AsyncAnnotationBeanPostProcessor .class , pvs );
115
95
context .registerSingleton ("bean" , MyValidBean .class );
116
96
context .refresh ();
117
- doTestProxyValidation (context .getBean ("bean" , MyValidInterface .class ), MethodValidationException .class );
97
+ doTestProxyValidation (context .getBean ("bean" , MyValidInterface .class ),
98
+ adaptViolations ? MethodValidationException .class : ConstraintViolationException .class );
118
99
context .close ();
119
100
}
120
101
121
102
@ Test // gh-29782
122
- public void testMethodValidationPostProcessorForInterfaceOnlyProxy () {
103
+ void testMethodValidationPostProcessorForInterfaceOnlyProxy () {
123
104
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ();
124
105
context .register (MethodValidationPostProcessor .class );
125
106
context .registerBean (MyValidInterface .class , () ->
@@ -129,23 +110,6 @@ public void testMethodValidationPostProcessorForInterfaceOnlyProxy() {
129
110
context .close ();
130
111
}
131
112
132
- @ Test
133
- @ SuppressWarnings ("unchecked" )
134
- public void testMethodValidationPostProcessorForInterfaceOnlyProxyWithAdaptConstraintViolations () {
135
- AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ();
136
- context .registerBean (MethodValidationPostProcessor .class , () -> {
137
- MethodValidationPostProcessor postProcessor = new MethodValidationPostProcessor ();
138
- postProcessor .setAdaptConstraintViolations (true );
139
- return postProcessor ;
140
- });
141
-
142
- context .registerBean (MyValidInterface .class , () ->
143
- ProxyFactory .getProxy (MyValidInterface .class , new MyValidClientInterfaceMethodInterceptor ()));
144
- context .refresh ();
145
- doTestProxyValidation (context .getBean (MyValidInterface .class ), MethodValidationException .class );
146
- context .close ();
147
- }
148
-
149
113
@ SuppressWarnings ("DataFlowIssue" )
150
114
private void doTestProxyValidation (MyValidInterface <String > proxy , Class <? extends Exception > expectedExceptionClass ) {
151
115
assertThat (proxy .myValidMethod ("value" , 5 )).isNotNull ();
@@ -174,11 +138,6 @@ void testLazyValidatorForMethodValidationWithValidatorProvider() {
174
138
doTestLazyValidatorForMethodValidation (LazyMethodValidationConfigWithValidatorProvider .class );
175
139
}
176
140
177
- @ Test
178
- void testLazyValidatorForMethodValidationWithAdaptConstraintViolations () {
179
- doTestLazyValidatorForMethodValidation (LazyMethodValidationConfigWithAdaptConstraintViolations .class );
180
- }
181
-
182
141
private void doTestLazyValidatorForMethodValidation (Class <?> configClass ) {
183
142
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ();
184
143
context .register (configClass , CustomValidatorBean .class , MyValidBean .class , MyValidFactoryBean .class );
@@ -335,16 +294,4 @@ public static MethodValidationPostProcessor methodValidationPostProcessor(Object
335
294
}
336
295
}
337
296
338
- @ Configuration
339
- public static class LazyMethodValidationConfigWithAdaptConstraintViolations {
340
-
341
- @ Bean
342
- public static MethodValidationPostProcessor methodValidationPostProcessor (ObjectProvider <Validator > validator ) {
343
- MethodValidationPostProcessor postProcessor = new MethodValidationPostProcessor ();
344
- postProcessor .setValidatorProvider (validator );
345
- postProcessor .setAdaptConstraintViolations (true );
346
- return postProcessor ;
347
- }
348
- }
349
-
350
297
}
0 commit comments