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 .*;
56
58
import static java .lang .annotation .RetentionPolicy .*;
57
59
import static org .hamcrest .core .Is .*;
60
+ import static org .hamcrest .core .StringContains .*;
58
61
import static org .junit .Assert .*;
59
62
60
63
/**
@@ -73,7 +76,7 @@ public class SpringValidatorAdapterTests {
73
76
@ Before
74
77
public void setupSpringValidatorAdapter () {
75
78
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}" );
79
+ messageSource .addMessage ("Same" , Locale .ENGLISH , "{2} must be same value as {1}" );
77
80
messageSource .addMessage ("password" , Locale .ENGLISH , "Password" );
78
81
messageSource .addMessage ("confirmPassword" , Locale .ENGLISH , "Password(Confirm)" );
79
82
}
@@ -96,8 +99,11 @@ public void testNoStringArgumentValue() {
96
99
97
100
assertThat (errors .getFieldErrorCount ("password" ), is (1 ));
98
101
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" ));
102
+ FieldError error = errors .getFieldError ("password" );
103
+ assertNotNull (error );
104
+ assertThat (messageSource .getMessage (error , Locale .ENGLISH ), is ("Size of Password is must be between 8 and 128" ));
105
+ assertTrue (error .contains (ConstraintViolation .class ));
106
+ assertThat (error .unwrap (ConstraintViolation .class ).getPropertyPath ().toString (), is ("password" ));
101
107
}
102
108
103
109
@ Test // SPR-13406
@@ -111,8 +117,11 @@ public void testApplyMessageSourceResolvableToStringArgumentValueWithResolvedLog
111
117
112
118
assertThat (errors .getFieldErrorCount ("password" ), is (1 ));
113
119
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)" ));
120
+ FieldError error = errors .getFieldError ("password" );
121
+ assertNotNull (error );
122
+ assertThat (messageSource .getMessage (error , Locale .ENGLISH ), is ("Password must be same value as Password(Confirm)" ));
123
+ assertTrue (error .contains (ConstraintViolation .class ));
124
+ assertThat (error .unwrap (ConstraintViolation .class ).getPropertyPath ().toString (), is ("password" ));
116
125
}
117
126
118
127
@ Test // SPR-13406
@@ -127,10 +136,16 @@ public void testApplyMessageSourceResolvableToStringArgumentValueWithUnresolvedL
127
136
assertThat (errors .getFieldErrorCount ("email" ), is (1 ));
128
137
assertThat (
errors .
getFieldValue (
"email" ),
is (
"[email protected] " ));
129
138
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" ));
139
+ FieldError error1 = errors .getFieldError ("email" );
140
+ FieldError error2 = errors .getFieldError ("confirmEmail" );
141
+ assertNotNull (error1 );
142
+ assertNotNull (error2 );
143
+ assertThat (messageSource .getMessage (error1 , Locale .ENGLISH ), is ("email must be same value as confirmEmail" ));
144
+ assertThat (messageSource .getMessage (error2 , Locale .ENGLISH ), is ("Email required" ));
145
+ assertTrue (error1 .contains (ConstraintViolation .class ));
146
+ assertThat (error1 .unwrap (ConstraintViolation .class ).getPropertyPath ().toString (), is ("email" ));
147
+ assertTrue (error2 .contains (ConstraintViolation .class ));
148
+ assertThat (error2 .unwrap (ConstraintViolation .class ).getPropertyPath ().toString (), is ("confirmEmail" ));
134
149
}
135
150
136
151
@ Test // SPR-15123
@@ -147,10 +162,34 @@ public void testApplyMessageSourceResolvableToStringArgumentValueWithAlwaysUseMe
147
162
assertThat (errors .getFieldErrorCount ("email" ), is (1 ));
148
163
assertThat (
errors .
getFieldValue (
"email" ),
is (
"[email protected] " ));
149
164
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" ));
165
+ FieldError error1 = errors .getFieldError ("email" );
166
+ FieldError error2 = errors .getFieldError ("confirmEmail" );
167
+ assertNotNull (error1 );
168
+ assertNotNull (error2 );
169
+ assertThat (messageSource .getMessage (error1 , Locale .ENGLISH ), is ("email must be same value as confirmEmail" ));
170
+ assertThat (messageSource .getMessage (error2 , Locale .ENGLISH ), is ("Email required" ));
171
+ assertTrue (error1 .contains (ConstraintViolation .class ));
172
+ assertThat (error1 .unwrap (ConstraintViolation .class ).getPropertyPath ().toString (), is ("email" ));
173
+ assertTrue (error2 .contains (ConstraintViolation .class ));
174
+ assertThat (error2 .unwrap (ConstraintViolation .class ).getPropertyPath ().toString (), is ("confirmEmail" ));
175
+ }
176
+
177
+ @ Test
178
+ public void testPatternMessage () {
179
+ TestBean testBean = new TestBean ();
180
+ testBean .setEmail ("X" );
181
+ testBean .setConfirmEmail ("X" );
182
+
183
+ BeanPropertyBindingResult errors = new BeanPropertyBindingResult (testBean , "testBean" );
184
+ validatorAdapter .validate (testBean , errors );
185
+
186
+ assertThat (errors .getFieldErrorCount ("email" ), is (1 ));
187
+ assertThat (errors .getFieldValue ("email" ), is ("X" ));
188
+ FieldError error = errors .getFieldError ("email" );
189
+ assertNotNull (error );
190
+ assertThat (messageSource .getMessage (error , Locale .ENGLISH ), containsString ("[\\ w.'-]{1,}@[\\ w.'-]{1,}" ));
191
+ assertTrue (error .contains (ConstraintViolation .class ));
192
+ assertThat (error .unwrap (ConstraintViolation .class ).getPropertyPath ().toString (), is ("email" ));
154
193
}
155
194
156
195
@ Test // SPR-16177
@@ -243,6 +282,7 @@ static class TestBean {
243
282
244
283
private String confirmPassword ;
245
284
285
+ @ Pattern (regexp = "[\\ w.'-]{1,}@[\\ w.'-]{1,}" )
246
286
private String email ;
247
287
248
288
@ Pattern (regexp = "[\\ p{L} -]*" , message = "Email required" )
@@ -403,13 +443,13 @@ public static class Child {
403
443
404
444
private Integer id ;
405
445
406
- @ javax . validation . constraints . NotNull
446
+ @ NotNull
407
447
private String name ;
408
448
409
- @ javax . validation . constraints . NotNull
449
+ @ NotNull
410
450
private Integer age ;
411
451
412
- @ javax . validation . constraints . NotNull
452
+ @ NotNull
413
453
private Parent parent ;
414
454
415
455
public Integer getId () {
0 commit comments