1
1
/*
2
- * Copyright 2002-2014 the original author or authors.
2
+ * Copyright 2002-2015 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.
38
38
import org .hibernate .validator .HibernateValidator ;
39
39
import org .junit .Test ;
40
40
41
+ import org .springframework .beans .factory .annotation .Autowired ;
42
+ import org .springframework .context .ApplicationContext ;
43
+ import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
44
+ import org .springframework .core .env .Environment ;
41
45
import org .springframework .validation .BeanPropertyBindingResult ;
42
46
import org .springframework .validation .Errors ;
43
47
import org .springframework .validation .FieldError ;
@@ -60,6 +64,7 @@ public class ValidatorFactoryTests {
60
64
public void testSimpleValidation () throws Exception {
61
65
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean ();
62
66
validator .afterPropertiesSet ();
67
+
63
68
ValidPerson person = new ValidPerson ();
64
69
Set <ConstraintViolation <ValidPerson >> result = validator .validate (person );
65
70
assertEquals (2 , result .size ());
@@ -80,6 +85,7 @@ public void testSimpleValidationWithCustomProvider() throws Exception {
80
85
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean ();
81
86
validator .setProviderClass (HibernateValidator .class );
82
87
validator .afterPropertiesSet ();
88
+
83
89
ValidPerson person = new ValidPerson ();
84
90
Set <ConstraintViolation <ValidPerson >> result = validator .validate (person );
85
91
assertEquals (2 , result .size ());
@@ -114,6 +120,7 @@ public void testSimpleValidationWithClassLevel() throws Exception {
114
120
public void testSpringValidationFieldType () throws Exception {
115
121
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean ();
116
122
validator .afterPropertiesSet ();
123
+
117
124
ValidPerson person = new ValidPerson ();
118
125
person .setName ("Phil" );
119
126
person .getAddress ().setStreet ("Phil's Street" );
@@ -128,6 +135,7 @@ public void testSpringValidationFieldType() throws Exception {
128
135
public void testSpringValidation () throws Exception {
129
136
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean ();
130
137
validator .afterPropertiesSet ();
138
+
131
139
ValidPerson person = new ValidPerson ();
132
140
BeanPropertyBindingResult result = new BeanPropertyBindingResult (person , "person" );
133
141
validator .validate (person , result );
@@ -155,6 +163,7 @@ public void testSpringValidation() throws Exception {
155
163
public void testSpringValidationWithClassLevel () throws Exception {
156
164
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean ();
157
165
validator .afterPropertiesSet ();
166
+
158
167
ValidPerson person = new ValidPerson ();
159
168
person .setName ("Juergen" );
160
169
person .getAddress ().setStreet ("Juergen's Street" );
@@ -168,10 +177,30 @@ public void testSpringValidationWithClassLevel() throws Exception {
168
177
assertTrue (errorCodes .contains ("NameAddressValid" ));
169
178
}
170
179
180
+ @ Test
181
+ public void testSpringValidationWithAutowiredValidator () throws Exception {
182
+ ApplicationContext ctx = new AnnotationConfigApplicationContext (LocalValidatorFactoryBean .class );
183
+ LocalValidatorFactoryBean validator = ctx .getBean (LocalValidatorFactoryBean .class );
184
+
185
+ ValidPerson person = new ValidPerson ();
186
+ person .expectsAutowiredValidator = true ;
187
+ person .setName ("Juergen" );
188
+ person .getAddress ().setStreet ("Juergen's Street" );
189
+ BeanPropertyBindingResult result = new BeanPropertyBindingResult (person , "person" );
190
+ validator .validate (person , result );
191
+ assertEquals (1 , result .getErrorCount ());
192
+ ObjectError globalError = result .getGlobalError ();
193
+ List <String > errorCodes = Arrays .asList (globalError .getCodes ());
194
+ assertEquals (2 , errorCodes .size ());
195
+ assertTrue (errorCodes .contains ("NameAddressValid.person" ));
196
+ assertTrue (errorCodes .contains ("NameAddressValid" ));
197
+ }
198
+
171
199
@ Test
172
200
public void testSpringValidationWithErrorInListElement () throws Exception {
173
201
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean ();
174
202
validator .afterPropertiesSet ();
203
+
175
204
ValidPerson person = new ValidPerson ();
176
205
person .getAddressList ().add (new ValidAddress ());
177
206
BeanPropertyBindingResult result = new BeanPropertyBindingResult (person , "person" );
@@ -189,6 +218,7 @@ public void testSpringValidationWithErrorInListElement() throws Exception {
189
218
public void testSpringValidationWithErrorInSetElement () throws Exception {
190
219
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean ();
191
220
validator .afterPropertiesSet ();
221
+
192
222
ValidPerson person = new ValidPerson ();
193
223
person .getAddressSet ().add (new ValidAddress ());
194
224
BeanPropertyBindingResult result = new BeanPropertyBindingResult (person , "person" );
@@ -242,6 +272,8 @@ public static class ValidPerson {
242
272
@ Valid
243
273
private Set <ValidAddress > addressSet = new LinkedHashSet <ValidAddress >();
244
274
275
+ public boolean expectsAutowiredValidator = false ;
276
+
245
277
public String getName () {
246
278
return name ;
247
279
}
@@ -306,12 +338,18 @@ public void setStreet(String street) {
306
338
307
339
public static class NameAddressValidator implements ConstraintValidator <NameAddressValid , ValidPerson > {
308
340
341
+ @ Autowired
342
+ private Environment environment ;
343
+
309
344
@ Override
310
345
public void initialize (NameAddressValid constraintAnnotation ) {
311
346
}
312
347
313
348
@ Override
314
349
public boolean isValid (ValidPerson value , ConstraintValidatorContext context ) {
350
+ if (value .expectsAutowiredValidator ) {
351
+ assertNotNull (this .environment );
352
+ }
315
353
boolean valid = (value .name == null || !value .address .street .contains (value .name ));
316
354
if (!valid && "Phil" .equals (value .name )) {
317
355
context .buildConstraintViolationWithTemplate (
0 commit comments