1
1
/*
2
- * Copyright 2002-2017 the original author or authors.
2
+ * Copyright 2002-2019 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.
34
34
import javax .validation .Constraint ;
35
35
import javax .validation .ConstraintValidator ;
36
36
import javax .validation .ConstraintValidatorContext ;
37
+ import javax .validation .ConstraintViolation ;
37
38
import javax .validation .Payload ;
38
39
import javax .validation .Valid ;
39
40
import javax .validation .Validation ;
50
51
import org .springframework .context .support .StaticMessageSource ;
51
52
import org .springframework .util .ObjectUtils ;
52
53
import org .springframework .validation .BeanPropertyBindingResult ;
54
+ import org .springframework .validation .FieldError ;
53
55
import org .springframework .validation .beanvalidation .SpringValidatorAdapter ;
54
56
55
57
import static java .lang .annotation .ElementType .*;
@@ -73,7 +75,7 @@ public class SpringValidatorAdapterTests {
73
75
@ Before
74
76
public void setupSpringValidatorAdapter () {
75
77
messageSource .addMessage ("Size" , Locale .ENGLISH , "Size of {0} is must be between {2} and {1}" );
76
- messageSource .addMessage ("Same" , Locale .ENGLISH , "{2} must be same value with {1}" );
78
+ messageSource .addMessage ("Same" , Locale .ENGLISH , "{2} must be same value as {1}" );
77
79
messageSource .addMessage ("password" , Locale .ENGLISH , "Password" );
78
80
messageSource .addMessage ("confirmPassword" , Locale .ENGLISH , "Password(Confirm)" );
79
81
}
@@ -96,8 +98,11 @@ public void testNoStringArgumentValue() {
96
98
97
99
assertThat (errors .getFieldErrorCount ("password" ), is (1 ));
98
100
assertThat (errors .getFieldValue ("password" ), is ("pass" ));
99
- assertThat (messageSource .getMessage (errors .getFieldError ("password" ), Locale .ENGLISH ),
100
- is ("Size of Password is must be between 8 and 128" ));
101
+ FieldError error = errors .getFieldError ("password" );
102
+ assertNotNull (error );
103
+ assertThat (messageSource .getMessage (error , Locale .ENGLISH ), is ("Size of Password is must be between 8 and 128" ));
104
+ assertTrue (error .contains (ConstraintViolation .class ));
105
+ assertThat (error .unwrap (ConstraintViolation .class ).getPropertyPath ().toString (), is ("password" ));
101
106
}
102
107
103
108
@ Test // SPR-13406
@@ -111,8 +116,11 @@ public void testApplyMessageSourceResolvableToStringArgumentValueWithResolvedLog
111
116
112
117
assertThat (errors .getFieldErrorCount ("password" ), is (1 ));
113
118
assertThat (errors .getFieldValue ("password" ), is ("password" ));
114
- assertThat (messageSource .getMessage (errors .getFieldError ("password" ), Locale .ENGLISH ),
115
- is ("Password must be same value with Password(Confirm)" ));
119
+ FieldError error = errors .getFieldError ("password" );
120
+ assertNotNull (error );
121
+ assertThat (messageSource .getMessage (error , Locale .ENGLISH ), is ("Password must be same value as Password(Confirm)" ));
122
+ assertTrue (error .contains (ConstraintViolation .class ));
123
+ assertThat (error .unwrap (ConstraintViolation .class ).getPropertyPath ().toString (), is ("password" ));
116
124
}
117
125
118
126
@ Test // SPR-13406
@@ -127,10 +135,16 @@ public void testApplyMessageSourceResolvableToStringArgumentValueWithUnresolvedL
127
135
assertThat (errors .getFieldErrorCount ("email" ), is (1 ));
128
136
assertThat (
errors .
getFieldValue (
"email" ),
is (
"[email protected] " ));
129
137
assertThat (errors .getFieldErrorCount ("confirmEmail" ), is (1 ));
130
- assertThat (messageSource .getMessage (errors .getFieldError ("email" ), Locale .ENGLISH ),
131
- is ("email must be same value with confirmEmail" ));
132
- assertThat (messageSource .getMessage (errors .getFieldError ("confirmEmail" ), Locale .ENGLISH ),
133
- is ("Email required" ));
138
+ FieldError error1 = errors .getFieldError ("email" );
139
+ FieldError error2 = errors .getFieldError ("confirmEmail" );
140
+ assertNotNull (error1 );
141
+ assertNotNull (error2 );
142
+ assertThat (messageSource .getMessage (error1 , Locale .ENGLISH ), is ("email must be same value as confirmEmail" ));
143
+ assertThat (messageSource .getMessage (error2 , Locale .ENGLISH ), is ("Email required" ));
144
+ assertTrue (error1 .contains (ConstraintViolation .class ));
145
+ assertThat (error1 .unwrap (ConstraintViolation .class ).getPropertyPath ().toString (), is ("email" ));
146
+ assertTrue (error2 .contains (ConstraintViolation .class ));
147
+ assertThat (error2 .unwrap (ConstraintViolation .class ).getPropertyPath ().toString (), is ("confirmEmail" ));
134
148
}
135
149
136
150
@ Test // SPR-15123
@@ -147,10 +161,16 @@ public void testApplyMessageSourceResolvableToStringArgumentValueWithAlwaysUseMe
147
161
assertThat (errors .getFieldErrorCount ("email" ), is (1 ));
148
162
assertThat (
errors .
getFieldValue (
"email" ),
is (
"[email protected] " ));
149
163
assertThat (errors .getFieldErrorCount ("confirmEmail" ), is (1 ));
150
- assertThat (messageSource .getMessage (errors .getFieldError ("email" ), Locale .ENGLISH ),
151
- is ("email must be same value with confirmEmail" ));
152
- assertThat (messageSource .getMessage (errors .getFieldError ("confirmEmail" ), Locale .ENGLISH ),
153
- is ("Email required" ));
164
+ FieldError error1 = errors .getFieldError ("email" );
165
+ FieldError error2 = errors .getFieldError ("confirmEmail" );
166
+ assertNotNull (error1 );
167
+ assertNotNull (error2 );
168
+ assertThat (messageSource .getMessage (error1 , Locale .ENGLISH ), is ("email must be same value as confirmEmail" ));
169
+ assertThat (messageSource .getMessage (error2 , Locale .ENGLISH ), is ("Email required" ));
170
+ assertTrue (error1 .contains (ConstraintViolation .class ));
171
+ assertThat (error1 .unwrap (ConstraintViolation .class ).getPropertyPath ().toString (), is ("email" ));
172
+ assertTrue (error2 .contains (ConstraintViolation .class ));
173
+ assertThat (error2 .unwrap (ConstraintViolation .class ).getPropertyPath ().toString (), is ("confirmEmail" ));
154
174
}
155
175
156
176
@ Test // SPR-16177
@@ -403,13 +423,13 @@ public static class Child {
403
423
404
424
private Integer id ;
405
425
406
- @ javax . validation . constraints . NotNull
426
+ @ NotNull
407
427
private String name ;
408
428
409
- @ javax . validation . constraints . NotNull
429
+ @ NotNull
410
430
private Integer age ;
411
431
412
- @ javax . validation . constraints . NotNull
432
+ @ NotNull
413
433
private Parent parent ;
414
434
415
435
public Integer getId () {
0 commit comments