31
31
import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
32
32
import org .springframework .context .annotation .Bean ;
33
33
import org .springframework .context .annotation .Configuration ;
34
+ import org .springframework .context .annotation .Primary ;
34
35
import org .springframework .validation .annotation .Validated ;
36
+ import org .springframework .validation .beanvalidation .LocalValidatorFactoryBean ;
35
37
import org .springframework .validation .beanvalidation .MethodValidationPostProcessor ;
38
+ import org .springframework .validation .beanvalidation .OptionalValidatorFactoryBean ;
36
39
37
40
import static org .assertj .core .api .Assertions .assertThat ;
41
+ import static org .mockito .Mockito .mock ;
38
42
39
43
/**
40
44
* Tests for {@link ValidationAutoConfiguration}.
41
45
*
42
46
* @author Stephane Nicoll
47
+ * @author Phillip Webb
43
48
*/
44
49
public class ValidationAutoConfigurationTests {
45
50
@@ -55,6 +60,95 @@ public void close() {
55
60
}
56
61
}
57
62
63
+ @ Test
64
+ public void validationAutoConfigurationShouldConfigureDefaultValidator () {
65
+ load (Config .class );
66
+ String [] jsrValidatorNames = this .context .getBeanNamesForType (Validator .class );
67
+ String [] springValidatorNames = this .context
68
+ .getBeanNamesForType (org .springframework .validation .Validator .class );
69
+ assertThat (jsrValidatorNames ).containsExactly ("defaultValidator" );
70
+ assertThat (springValidatorNames ).containsExactly ("defaultValidator" );
71
+ Validator jsrValidator = this .context .getBean (Validator .class );
72
+ org .springframework .validation .Validator springValidator = this .context
73
+ .getBean (org .springframework .validation .Validator .class );
74
+ assertThat (jsrValidator ).isInstanceOf (LocalValidatorFactoryBean .class );
75
+ assertThat (jsrValidator ).isEqualTo (springValidator );
76
+ assertThat (isPrimaryBean ("defaultValidator" )).isTrue ();
77
+ }
78
+
79
+ @ Test
80
+ public void validationAutoConfigurationWhenUserProvidesValidatorShouldBackOff () {
81
+ load (UserDefinedValidatorConfig .class );
82
+ String [] jsrValidatorNames = this .context .getBeanNamesForType (Validator .class );
83
+ String [] springValidatorNames = this .context
84
+ .getBeanNamesForType (org .springframework .validation .Validator .class );
85
+ assertThat (jsrValidatorNames ).containsExactly ("customValidator" );
86
+ assertThat (springValidatorNames ).containsExactly ("customValidator" );
87
+ org .springframework .validation .Validator springValidator = this .context
88
+ .getBean (org .springframework .validation .Validator .class );
89
+ Validator jsrValidator = this .context .getBean (Validator .class );
90
+ assertThat (jsrValidator ).isInstanceOf (OptionalValidatorFactoryBean .class );
91
+ assertThat (jsrValidator ).isEqualTo (springValidator );
92
+ assertThat (isPrimaryBean ("customValidator" )).isFalse ();
93
+ }
94
+
95
+ @ Test
96
+ public void validationAutoConfigurationWhenUserProvidesDefaultValidatorShouldNotEnablePrimary () {
97
+ load (UserDefinedDefaultValidatorConfig .class );
98
+ String [] jsrValidatorNames = this .context .getBeanNamesForType (Validator .class );
99
+ String [] springValidatorNames = this .context
100
+ .getBeanNamesForType (org .springframework .validation .Validator .class );
101
+ assertThat (jsrValidatorNames ).containsExactly ("defaultValidator" );
102
+ assertThat (springValidatorNames ).containsExactly ("defaultValidator" );
103
+ assertThat (isPrimaryBean ("defaultValidator" )).isFalse ();
104
+ }
105
+
106
+ @ Test
107
+ public void validationAutoConfigurationWhenUserProvidesJsrValidatorShouldBackOff () {
108
+ load (UserDefinedJsrValidatorConfig .class );
109
+ String [] jsrValidatorNames = this .context .getBeanNamesForType (Validator .class );
110
+ String [] springValidatorNames = this .context
111
+ .getBeanNamesForType (org .springframework .validation .Validator .class );
112
+ assertThat (jsrValidatorNames ).containsExactly ("customValidator" );
113
+ assertThat (springValidatorNames ).isEmpty ();
114
+ assertThat (isPrimaryBean ("customValidator" )).isFalse ();
115
+ }
116
+
117
+ @ Test
118
+ public void validationAutoConfigurationWhenUserProvidesSpringValidatorShouldCreateJsrValidator () {
119
+ load (UserDefinedSpringValidatorConfig .class );
120
+ String [] jsrValidatorNames = this .context .getBeanNamesForType (Validator .class );
121
+ String [] springValidatorNames = this .context
122
+ .getBeanNamesForType (org .springframework .validation .Validator .class );
123
+ assertThat (jsrValidatorNames ).containsExactly ("defaultValidator" );
124
+ assertThat (springValidatorNames ).containsExactly (
125
+ "customValidator" , "anotherCustomValidator" , "defaultValidator" );
126
+ Validator jsrValidator = this .context .getBean (Validator .class );
127
+ org .springframework .validation .Validator springValidator = this .context
128
+ .getBean (org .springframework .validation .Validator .class );
129
+ assertThat (jsrValidator ).isInstanceOf (LocalValidatorFactoryBean .class );
130
+ assertThat (jsrValidator ).isEqualTo (springValidator );
131
+ assertThat (isPrimaryBean ("defaultValidator" )).isTrue ();
132
+ }
133
+
134
+ @ Test
135
+ public void validationAutoConfigurationWhenUserProvidesPrimarySpringValidatorShouldRemovePrimaryFlag () {
136
+ load (UserDefinedPrimarySpringValidatorConfig .class );
137
+ String [] jsrValidatorNames = this .context .getBeanNamesForType (Validator .class );
138
+ String [] springValidatorNames = this .context
139
+ .getBeanNamesForType (org .springframework .validation .Validator .class );
140
+ assertThat (jsrValidatorNames ).containsExactly ("defaultValidator" );
141
+ assertThat (springValidatorNames ).containsExactly (
142
+ "customValidator" , "anotherCustomValidator" , "defaultValidator" );
143
+ Validator jsrValidator = this .context .getBean (Validator .class );
144
+ org .springframework .validation .Validator springValidator = this .context
145
+ .getBean (org .springframework .validation .Validator .class );
146
+ assertThat (jsrValidator ).isInstanceOf (LocalValidatorFactoryBean .class );
147
+ assertThat (springValidator ).isEqualTo (
148
+ this .context .getBean ("anotherCustomValidator" ));
149
+ assertThat (isPrimaryBean ("defaultValidator" )).isFalse ();
150
+ }
151
+
58
152
@ Test
59
153
public void validationIsEnabled () {
60
154
load (SampleService .class );
@@ -104,7 +198,11 @@ public void userDefinedMethodValidationPostProcessorTakesPrecedence() {
104
198
.getPropertyValue ("validator" ));
105
199
}
106
200
107
- public void load (Class <?> config , String ... environment ) {
201
+ private boolean isPrimaryBean (String beanName ) {
202
+ return this .context .getBeanDefinition (beanName ).isPrimary ();
203
+ }
204
+
205
+ private void load (Class <?> config , String ... environment ) {
108
206
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
109
207
EnvironmentTestUtils .addEnvironment (ctx , environment );
110
208
if (config != null ) {
@@ -115,6 +213,72 @@ public void load(Class<?> config, String... environment) {
115
213
this .context = ctx ;
116
214
}
117
215
216
+ @ Configuration
217
+ static class Config {
218
+
219
+ }
220
+
221
+ @ Configuration
222
+ static class UserDefinedValidatorConfig {
223
+
224
+ @ Bean
225
+ public OptionalValidatorFactoryBean customValidator () {
226
+ return new OptionalValidatorFactoryBean ();
227
+ }
228
+
229
+ }
230
+
231
+ @ Configuration
232
+ static class UserDefinedDefaultValidatorConfig {
233
+
234
+ @ Bean
235
+ public OptionalValidatorFactoryBean defaultValidator () {
236
+ return new OptionalValidatorFactoryBean ();
237
+ }
238
+
239
+ }
240
+
241
+ @ Configuration
242
+ static class UserDefinedJsrValidatorConfig {
243
+
244
+ @ Bean
245
+ public Validator customValidator () {
246
+ return mock (Validator .class );
247
+ }
248
+
249
+ }
250
+
251
+ @ Configuration
252
+ static class UserDefinedSpringValidatorConfig {
253
+
254
+ @ Bean
255
+ public org .springframework .validation .Validator customValidator () {
256
+ return mock (org .springframework .validation .Validator .class );
257
+ }
258
+
259
+ @ Bean
260
+ public org .springframework .validation .Validator anotherCustomValidator () {
261
+ return mock (org .springframework .validation .Validator .class );
262
+ }
263
+
264
+ }
265
+
266
+ @ Configuration
267
+ static class UserDefinedPrimarySpringValidatorConfig {
268
+
269
+ @ Bean
270
+ public org .springframework .validation .Validator customValidator () {
271
+ return mock (org .springframework .validation .Validator .class );
272
+ }
273
+
274
+ @ Bean
275
+ @ Primary
276
+ public org .springframework .validation .Validator anotherCustomValidator () {
277
+ return mock (org .springframework .validation .Validator .class );
278
+ }
279
+
280
+ }
281
+
118
282
@ Validated
119
283
static class SampleService {
120
284
0 commit comments