32
32
import org .springframework .context .annotation .Bean ;
33
33
import org .springframework .context .annotation .Configuration ;
34
34
import org .springframework .validation .annotation .Validated ;
35
+ import org .springframework .validation .beanvalidation .LocalValidatorFactoryBean ;
35
36
import org .springframework .validation .beanvalidation .MethodValidationPostProcessor ;
37
+ import org .springframework .validation .beanvalidation .OptionalValidatorFactoryBean ;
36
38
37
39
import static org .assertj .core .api .Assertions .assertThat ;
40
+ import static org .mockito .Mockito .mock ;
38
41
39
42
/**
40
43
* Tests for {@link ValidationAutoConfiguration}.
41
44
*
42
45
* @author Stephane Nicoll
46
+ * @author Phillip Webb
43
47
*/
44
48
public class ValidationAutoConfigurationTests {
45
49
@@ -55,6 +59,62 @@ public void close() {
55
59
}
56
60
}
57
61
62
+ @ Test
63
+ public void validationAutoConfigurationShouldConfigureDefaultValidator () {
64
+ load (Config .class );
65
+ String [] jsrValidatorNames = this .context .getBeanNamesForType (Validator .class );
66
+ String [] springValidatorNames = this .context
67
+ .getBeanNamesForType (org .springframework .validation .Validator .class );
68
+ assertThat (jsrValidatorNames ).containsExactly ("defaultValidator" );
69
+ assertThat (springValidatorNames ).containsExactly ("defaultValidator" );
70
+ Validator jsrValidator = this .context .getBean (Validator .class );
71
+ org .springframework .validation .Validator springValidator = this .context
72
+ .getBean (org .springframework .validation .Validator .class );
73
+ assertThat (jsrValidator ).isInstanceOf (LocalValidatorFactoryBean .class );
74
+ assertThat (jsrValidator ).isEqualTo (springValidator );
75
+ }
76
+
77
+ @ Test
78
+ public void validationAutoConfigurationWhenUserProvidesValidatorShouldBackOff () {
79
+ load (UserDefinedValidatorConfig .class );
80
+ String [] jsrValidatorNames = this .context .getBeanNamesForType (Validator .class );
81
+ String [] springValidatorNames = this .context
82
+ .getBeanNamesForType (org .springframework .validation .Validator .class );
83
+ assertThat (jsrValidatorNames ).containsExactly ("customValidator" );
84
+ assertThat (springValidatorNames ).containsExactly ("customValidator" );
85
+ org .springframework .validation .Validator springValidator = this .context
86
+ .getBean (org .springframework .validation .Validator .class );
87
+ Validator jsrValidator = this .context .getBean (Validator .class );
88
+ assertThat (jsrValidator ).isInstanceOf (OptionalValidatorFactoryBean .class );
89
+ assertThat (jsrValidator ).isEqualTo (springValidator );
90
+ }
91
+
92
+ @ Test
93
+ public void validationAutoConfigurationWhenUserProvidesJsrValidatorShouldBackOff () {
94
+ load (UserDefinedJsrValidatorConfig .class );
95
+ String [] jsrValidatorNames = this .context .getBeanNamesForType (Validator .class );
96
+ String [] springValidatorNames = this .context
97
+ .getBeanNamesForType (org .springframework .validation .Validator .class );
98
+ assertThat (jsrValidatorNames ).containsExactly ("customValidator" );
99
+ assertThat (springValidatorNames ).isEmpty ();
100
+ }
101
+
102
+ @ Test
103
+ public void validationAutoConfigurationWhenUserProvidesSpringValidatorShouldCreateJsrValidator () {
104
+ load (UserDefinedSpringValidatorConfig .class );
105
+ String [] jsrValidatorNames = this .context .getBeanNamesForType (Validator .class );
106
+ String [] springValidatorNames = this .context
107
+ .getBeanNamesForType (org .springframework .validation .Validator .class );
108
+ assertThat (jsrValidatorNames ).containsExactly ("defaultValidator" );
109
+ assertThat (springValidatorNames )
110
+ .containsExactly ("customValidator" , "defaultValidator" );
111
+ Validator jsrValidator = this .context .getBean (Validator .class );
112
+ org .springframework .validation .Validator springValidator = this .context
113
+ .getBean (org .springframework .validation .Validator .class );
114
+ assertThat (jsrValidator ).isInstanceOf (LocalValidatorFactoryBean .class );
115
+ assertThat (jsrValidator ).isEqualTo (springValidator );
116
+ }
117
+
58
118
@ Test
59
119
public void validationIsEnabled () {
60
120
load (SampleService .class );
@@ -115,6 +175,41 @@ public void load(Class<?> config, String... environment) {
115
175
this .context = ctx ;
116
176
}
117
177
178
+ @ Configuration
179
+ static class Config {
180
+
181
+ }
182
+
183
+ @ Configuration
184
+ static class UserDefinedValidatorConfig {
185
+
186
+ @ Bean
187
+ public OptionalValidatorFactoryBean customValidator () {
188
+ return new OptionalValidatorFactoryBean ();
189
+ }
190
+
191
+ }
192
+
193
+ @ Configuration
194
+ static class UserDefinedJsrValidatorConfig {
195
+
196
+ @ Bean
197
+ public Validator customValidator () {
198
+ return mock (Validator .class );
199
+ }
200
+
201
+ }
202
+
203
+ @ Configuration
204
+ static class UserDefinedSpringValidatorConfig {
205
+
206
+ @ Bean
207
+ public org .springframework .validation .Validator customValidator () {
208
+ return mock (org .springframework .validation .Validator .class );
209
+ }
210
+
211
+ }
212
+
118
213
@ Validated
119
214
static class SampleService {
120
215
0 commit comments