|
22 | 22 | import java.lang.annotation.Retention;
|
23 | 23 | import java.lang.annotation.Target;
|
24 | 24 | import java.util.Arrays;
|
| 25 | +import java.util.HashMap; |
25 | 26 | import java.util.List;
|
26 | 27 | import java.util.Locale;
|
| 28 | +import java.util.Map; |
27 | 29 | import javax.validation.Constraint;
|
28 | 30 | import javax.validation.ConstraintValidator;
|
29 | 31 | import javax.validation.ConstraintValidatorContext;
|
30 | 32 | import javax.validation.Payload;
|
| 33 | +import javax.validation.Valid; |
31 | 34 | import javax.validation.Validation;
|
32 | 35 | import javax.validation.Validator;
|
33 | 36 | import javax.validation.constraints.NotNull;
|
@@ -157,6 +160,35 @@ public void testListElementConstraint() {
|
157 | 160 | assertNull(errors.getFieldValue("property[4]"));
|
158 | 161 | }
|
159 | 162 |
|
| 163 | + @Test // SPR-15839 |
| 164 | + public void testMapValueConstraint() { |
| 165 | + Map<String, String> property = new HashMap<>(); |
| 166 | + property.put("no value can be", null); |
| 167 | + |
| 168 | + BeanWithMapEntryConstraint bean = new BeanWithMapEntryConstraint(); |
| 169 | + bean.setProperty(property); |
| 170 | + |
| 171 | + BeanPropertyBindingResult errors = new BeanPropertyBindingResult(bean, "bean"); |
| 172 | + validatorAdapter.validate(bean, errors); |
| 173 | + |
| 174 | + assertThat(errors.getFieldErrorCount("property[no value can be]"), is(1)); |
| 175 | + assertNull(errors.getFieldValue("property[no value can be]")); |
| 176 | + } |
| 177 | + |
| 178 | + @Test // SPR-15839 |
| 179 | + public void testMapEntryConstraint() { |
| 180 | + Map<String, String> property = new HashMap<>(); |
| 181 | + property.put(null, null); |
| 182 | + |
| 183 | + BeanWithMapEntryConstraint bean = new BeanWithMapEntryConstraint(); |
| 184 | + bean.setProperty(property); |
| 185 | + |
| 186 | + BeanPropertyBindingResult errors = new BeanPropertyBindingResult(bean, "bean"); |
| 187 | + validatorAdapter.validate(bean, errors); |
| 188 | + |
| 189 | + assertTrue(errors.hasFieldErrors("property[]")); |
| 190 | + assertNull(errors.getFieldValue("property[]")); |
| 191 | + } |
160 | 192 |
|
161 | 193 |
|
162 | 194 | @Same(field = "password", comparingField = "confirmPassword")
|
@@ -278,14 +310,31 @@ public boolean isValid(Object value, ConstraintValidatorContext context) {
|
278 | 310 |
|
279 | 311 | public class BeanWithListElementConstraint {
|
280 | 312 |
|
| 313 | + @Valid |
281 | 314 | private List<@NotNull String> property;
|
282 | 315 |
|
283 | 316 | public List<String> getProperty() {
|
284 | 317 | return property;
|
285 | 318 | }
|
| 319 | + |
286 | 320 | public void setProperty(List<String> property) {
|
287 | 321 | this.property = property;
|
288 | 322 | }
|
289 | 323 | }
|
290 | 324 |
|
| 325 | + |
| 326 | + public class BeanWithMapEntryConstraint { |
| 327 | + |
| 328 | + @Valid |
| 329 | + private Map<@NotNull String, @NotNull String> property; |
| 330 | + |
| 331 | + public Map<String, String> getProperty() { |
| 332 | + return property; |
| 333 | + } |
| 334 | + |
| 335 | + public void setProperty(Map<String, String> property) { |
| 336 | + this.property = property; |
| 337 | + } |
| 338 | + } |
| 339 | + |
291 | 340 | }
|
0 commit comments