1
1
/*
2
- * Copyright 2002-2014 the original author or authors.
2
+ * Copyright 2002-2017 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.
33
33
import javax .validation .ConstraintViolation ;
34
34
import javax .validation .Payload ;
35
35
import javax .validation .Valid ;
36
+ import javax .validation .Validator ;
37
+ import javax .validation .ValidatorFactory ;
36
38
import javax .validation .constraints .NotNull ;
37
39
38
40
import org .hibernate .validator .HibernateValidator ;
41
+ import org .hibernate .validator .HibernateValidatorFactory ;
39
42
import org .junit .Test ;
40
43
44
+ import org .springframework .beans .factory .annotation .Autowired ;
45
+ import org .springframework .context .ConfigurableApplicationContext ;
46
+ import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
47
+ import org .springframework .core .env .Environment ;
41
48
import org .springframework .validation .BeanPropertyBindingResult ;
42
49
import org .springframework .validation .Errors ;
43
50
import org .springframework .validation .FieldError ;
47
54
import static org .junit .Assert .*;
48
55
49
56
/**
50
- * Tested against Hibernate Validator 4.3, as of Spring 4.0 .
57
+ * Tests against Hibernate Validator 5.x .
51
58
*
52
59
* @author Juergen Hoeller
53
- * @since 3.0
54
60
*/
55
61
public class ValidatorFactoryTests {
56
62
57
63
@ Test
58
64
public void testSimpleValidation () throws Exception {
59
65
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean ();
60
66
validator .afterPropertiesSet ();
67
+
61
68
ValidPerson person = new ValidPerson ();
62
69
Set <ConstraintViolation <ValidPerson >> result = validator .validate (person );
63
70
assertEquals (2 , result .size ());
@@ -70,6 +77,12 @@ public void testSimpleValidation() throws Exception {
70
77
fail ("Invalid constraint violation with path '" + path + "'" );
71
78
}
72
79
}
80
+
81
+ Validator nativeValidator = validator .unwrap (Validator .class );
82
+ assertTrue (nativeValidator .getClass ().getName ().startsWith ("org.hibernate" ));
83
+ assertTrue (validator .unwrap (ValidatorFactory .class ) instanceof HibernateValidatorFactory );
84
+ assertTrue (validator .unwrap (HibernateValidatorFactory .class ) instanceof HibernateValidatorFactory );
85
+
73
86
validator .destroy ();
74
87
}
75
88
@@ -78,6 +91,7 @@ public void testSimpleValidationWithCustomProvider() throws Exception {
78
91
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean ();
79
92
validator .setProviderClass (HibernateValidator .class );
80
93
validator .afterPropertiesSet ();
94
+
81
95
ValidPerson person = new ValidPerson ();
82
96
Set <ConstraintViolation <ValidPerson >> result = validator .validate (person );
83
97
assertEquals (2 , result .size ());
@@ -90,6 +104,12 @@ public void testSimpleValidationWithCustomProvider() throws Exception {
90
104
fail ("Invalid constraint violation with path '" + path + "'" );
91
105
}
92
106
}
107
+
108
+ Validator nativeValidator = validator .unwrap (Validator .class );
109
+ assertTrue (nativeValidator .getClass ().getName ().startsWith ("org.hibernate" ));
110
+ assertTrue (validator .unwrap (ValidatorFactory .class ) instanceof HibernateValidatorFactory );
111
+ assertTrue (validator .unwrap (HibernateValidatorFactory .class ) instanceof HibernateValidatorFactory );
112
+
93
113
validator .destroy ();
94
114
}
95
115
@@ -112,6 +132,7 @@ public void testSimpleValidationWithClassLevel() throws Exception {
112
132
public void testSpringValidationFieldType () throws Exception {
113
133
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean ();
114
134
validator .afterPropertiesSet ();
135
+
115
136
ValidPerson person = new ValidPerson ();
116
137
person .setName ("Phil" );
117
138
person .getAddress ().setStreet ("Phil's Street" );
@@ -126,6 +147,7 @@ public void testSpringValidationFieldType() throws Exception {
126
147
public void testSpringValidation () throws Exception {
127
148
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean ();
128
149
validator .afterPropertiesSet ();
150
+
129
151
ValidPerson person = new ValidPerson ();
130
152
BeanPropertyBindingResult result = new BeanPropertyBindingResult (person , "person" );
131
153
validator .validate (person , result );
@@ -153,6 +175,7 @@ public void testSpringValidation() throws Exception {
153
175
public void testSpringValidationWithClassLevel () throws Exception {
154
176
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean ();
155
177
validator .afterPropertiesSet ();
178
+
156
179
ValidPerson person = new ValidPerson ();
157
180
person .setName ("Juergen" );
158
181
person .getAddress ().setStreet ("Juergen's Street" );
@@ -166,10 +189,32 @@ public void testSpringValidationWithClassLevel() throws Exception {
166
189
assertTrue (errorCodes .contains ("NameAddressValid" ));
167
190
}
168
191
192
+ @ Test
193
+ public void testSpringValidationWithAutowiredValidator () throws Exception {
194
+ ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext (
195
+ LocalValidatorFactoryBean .class );
196
+ LocalValidatorFactoryBean validator = ctx .getBean (LocalValidatorFactoryBean .class );
197
+
198
+ ValidPerson person = new ValidPerson ();
199
+ person .expectsAutowiredValidator = true ;
200
+ person .setName ("Juergen" );
201
+ person .getAddress ().setStreet ("Juergen's Street" );
202
+ BeanPropertyBindingResult result = new BeanPropertyBindingResult (person , "person" );
203
+ validator .validate (person , result );
204
+ assertEquals (1 , result .getErrorCount ());
205
+ ObjectError globalError = result .getGlobalError ();
206
+ List <String > errorCodes = Arrays .asList (globalError .getCodes ());
207
+ assertEquals (2 , errorCodes .size ());
208
+ assertTrue (errorCodes .contains ("NameAddressValid.person" ));
209
+ assertTrue (errorCodes .contains ("NameAddressValid" ));
210
+ ctx .close ();
211
+ }
212
+
169
213
@ Test
170
214
public void testSpringValidationWithErrorInListElement () throws Exception {
171
215
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean ();
172
216
validator .afterPropertiesSet ();
217
+
173
218
ValidPerson person = new ValidPerson ();
174
219
person .getAddressList ().add (new ValidAddress ());
175
220
BeanPropertyBindingResult result = new BeanPropertyBindingResult (person , "person" );
@@ -187,6 +232,7 @@ public void testSpringValidationWithErrorInListElement() throws Exception {
187
232
public void testSpringValidationWithErrorInSetElement () throws Exception {
188
233
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean ();
189
234
validator .afterPropertiesSet ();
235
+
190
236
ValidPerson person = new ValidPerson ();
191
237
person .getAddressSet ().add (new ValidAddress ());
192
238
BeanPropertyBindingResult result = new BeanPropertyBindingResult (person , "person" );
@@ -235,10 +281,12 @@ public static class ValidPerson {
235
281
private ValidAddress address = new ValidAddress ();
236
282
237
283
@ Valid
238
- private List <ValidAddress > addressList = new LinkedList <ValidAddress >();
284
+ private List <ValidAddress > addressList = new LinkedList <>();
239
285
240
286
@ Valid
241
- private Set <ValidAddress > addressSet = new LinkedHashSet <ValidAddress >();
287
+ private Set <ValidAddress > addressSet = new LinkedHashSet <>();
288
+
289
+ public boolean expectsAutowiredValidator = false ;
242
290
243
291
public String getName () {
244
292
return name ;
@@ -304,12 +352,18 @@ public void setStreet(String street) {
304
352
305
353
public static class NameAddressValidator implements ConstraintValidator <NameAddressValid , ValidPerson > {
306
354
355
+ @ Autowired
356
+ private Environment environment ;
357
+
307
358
@ Override
308
359
public void initialize (NameAddressValid constraintAnnotation ) {
309
360
}
310
361
311
362
@ Override
312
363
public boolean isValid (ValidPerson value , ConstraintValidatorContext context ) {
364
+ if (value .expectsAutowiredValidator ) {
365
+ assertNotNull (this .environment );
366
+ }
313
367
boolean valid = (value .name == null || !value .address .street .contains (value .name ));
314
368
if (!valid && "Phil" .equals (value .name )) {
315
369
context .buildConstraintViolationWithTemplate (
@@ -378,7 +432,7 @@ public void initialize(InnerValid constraintAnnotation) {
378
432
public boolean isValid (InnerBean bean , ConstraintValidatorContext context ) {
379
433
context .disableDefaultConstraintViolation ();
380
434
if (bean .getValue () == null ) {
381
- context .buildConstraintViolationWithTemplate ("NULL" ). addNode ("value" ).addConstraintViolation ();
435
+ context .buildConstraintViolationWithTemplate ("NULL" ).addNode ("value" ).addConstraintViolation ();
382
436
return false ;
383
437
}
384
438
return true ;
0 commit comments