Object/FieldError provides access to source object (exception/violation)

Issue: SPR-16372
This commit is contained in:
Juergen Hoeller 2018-02-09 18:42:56 +01:00
parent e873b18f43
commit 9bb7fcd997
6 changed files with 122 additions and 67 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -59,9 +59,9 @@ public class DefaultBindingErrorProcessor implements BindingErrorProcessor {
String fixedField = bindingResult.getNestedPath() + missingField; String fixedField = bindingResult.getNestedPath() + missingField;
String[] codes = bindingResult.resolveMessageCodes(MISSING_FIELD_ERROR_CODE, missingField); String[] codes = bindingResult.resolveMessageCodes(MISSING_FIELD_ERROR_CODE, missingField);
Object[] arguments = getArgumentsForBindError(bindingResult.getObjectName(), fixedField); Object[] arguments = getArgumentsForBindError(bindingResult.getObjectName(), fixedField);
bindingResult.addError(new FieldError( FieldError error = new FieldError(bindingResult.getObjectName(), fixedField, "", true,
bindingResult.getObjectName(), fixedField, "", true, codes, arguments, "Field '" + fixedField + "' is required");
codes, arguments, "Field '" + fixedField + "' is required")); bindingResult.addError(error);
} }
@Override @Override
@ -72,12 +72,13 @@ public class DefaultBindingErrorProcessor implements BindingErrorProcessor {
String[] codes = bindingResult.resolveMessageCodes(ex.getErrorCode(), field); String[] codes = bindingResult.resolveMessageCodes(ex.getErrorCode(), field);
Object[] arguments = getArgumentsForBindError(bindingResult.getObjectName(), field); Object[] arguments = getArgumentsForBindError(bindingResult.getObjectName(), field);
Object rejectedValue = ex.getValue(); Object rejectedValue = ex.getValue();
if (rejectedValue != null && rejectedValue.getClass().isArray()) { if (ObjectUtils.isArray(rejectedValue)) {
rejectedValue = StringUtils.arrayToCommaDelimitedString(ObjectUtils.toObjectArray(rejectedValue)); rejectedValue = StringUtils.arrayToCommaDelimitedString(ObjectUtils.toObjectArray(rejectedValue));
} }
bindingResult.addError(new FieldError( FieldError error = new FieldError(bindingResult.getObjectName(), field, rejectedValue, true,
bindingResult.getObjectName(), field, rejectedValue, true, codes, arguments, ex.getLocalizedMessage());
codes, arguments, ex.getLocalizedMessage())); error.initSource(ex);
bindingResult.addError(error);
} }
/** /**

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -99,13 +99,6 @@ public class FieldError extends ObjectError {
} }
@Override
public String toString() {
return "Field error in object '" + getObjectName() + "' on field '" + this.field +
"': rejected value [" + ObjectUtils.nullSafeToString(this.rejectedValue) + "]; " +
resolvableToString();
}
@Override @Override
public boolean equals(@Nullable Object other) { public boolean equals(@Nullable Object other) {
if (this == other) { if (this == other) {
@ -129,4 +122,11 @@ public class FieldError extends ObjectError {
return hashCode; return hashCode;
} }
@Override
public String toString() {
return "Field error in object '" + getObjectName() + "' on field '" + this.field +
"': rejected value [" + ObjectUtils.nullSafeToString(this.rejectedValue) + "]; " +
resolvableToString();
}
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -37,6 +37,9 @@ public class ObjectError extends DefaultMessageSourceResolvable {
private final String objectName; private final String objectName;
@Nullable
private Object source;
/** /**
* Create a new instance of the ObjectError class. * Create a new instance of the ObjectError class.
@ -70,12 +73,31 @@ public class ObjectError extends DefaultMessageSourceResolvable {
return this.objectName; return this.objectName;
} }
/**
@Override * Initialize the source behind this error: possibly an {@link Exception}
public String toString() { * (typically {@link org.springframework.beans.PropertyAccessException})
return "Error in object '" + this.objectName + "': " + resolvableToString(); * or a Bean Validation {@link javax.validation.ConstraintViolation}.
* @param source the source object
* @since 5.0.4
*/
public void initSource(Object source) {
Assert.state(this.source == null, "Source already initialized");
this.source = source;
} }
/**
* Return the source behind this error: possibly an {@link Exception}
* (typically {@link org.springframework.beans.PropertyAccessException})
* or a Bean Validation {@link javax.validation.ConstraintViolation}.
* @return the source object, or {@code null} if none
* @since 5.0.4
*/
@Nullable
public Object getSource() {
return this.source;
}
@Override @Override
public boolean equals(@Nullable Object other) { public boolean equals(@Nullable Object other) {
if (this == other) { if (this == other) {
@ -93,4 +115,9 @@ public class ObjectError extends DefaultMessageSourceResolvable {
return super.hashCode() * 29 + getObjectName().hashCode(); return super.hashCode() * 29 + getObjectName().hashCode();
} }
@Override
public String toString() {
return "Error in object '" + this.objectName + "': " + resolvableToString();
}
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -142,15 +142,18 @@ public class SpringValidatorAdapter implements SmartValidator, javax.validation.
String nestedField = bindingResult.getNestedPath() + field; String nestedField = bindingResult.getNestedPath() + field;
if ("".equals(nestedField)) { if ("".equals(nestedField)) {
String[] errorCodes = bindingResult.resolveMessageCodes(errorCode); String[] errorCodes = bindingResult.resolveMessageCodes(errorCode);
bindingResult.addError(new ObjectError( ObjectError error = new ObjectError(
errors.getObjectName(), errorCodes, errorArgs, violation.getMessage())); errors.getObjectName(), errorCodes, errorArgs, violation.getMessage());
error.initSource(violation);
bindingResult.addError(error);
} }
else { else {
Object rejectedValue = getRejectedValue(field, violation, bindingResult); Object rejectedValue = getRejectedValue(field, violation, bindingResult);
String[] errorCodes = bindingResult.resolveMessageCodes(errorCode, field); String[] errorCodes = bindingResult.resolveMessageCodes(errorCode, field);
bindingResult.addError(new FieldError( FieldError error = new FieldError(errors.getObjectName(), nestedField,
errors.getObjectName(), nestedField, rejectedValue, false, rejectedValue, false, errorCodes, errorArgs, violation.getMessage());
errorCodes, errorArgs, violation.getMessage())); error.initSource(violation);
bindingResult.addError(error);
} }
} }
else { else {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -42,9 +42,11 @@ import org.junit.rules.ExpectedException;
import org.springframework.beans.BeanWrapper; import org.springframework.beans.BeanWrapper;
import org.springframework.beans.InvalidPropertyException; import org.springframework.beans.InvalidPropertyException;
import org.springframework.beans.MethodInvocationException;
import org.springframework.beans.MutablePropertyValues; import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.NotWritablePropertyException; import org.springframework.beans.NotWritablePropertyException;
import org.springframework.beans.NullValueInNestedPathException; import org.springframework.beans.NullValueInNestedPathException;
import org.springframework.beans.TypeMismatchException;
import org.springframework.beans.propertyeditors.CustomCollectionEditor; import org.springframework.beans.propertyeditors.CustomCollectionEditor;
import org.springframework.beans.propertyeditors.CustomNumberEditor; import org.springframework.beans.propertyeditors.CustomNumberEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor; import org.springframework.beans.propertyeditors.StringTrimmerEditor;
@ -78,11 +80,11 @@ import static org.junit.Assert.*;
public class DataBinderTests { public class DataBinderTests {
@Rule @Rule
public ExpectedException expectedException = ExpectedException.none(); public final ExpectedException expectedException = ExpectedException.none();
@Test @Test
public void testBindingNoErrors() throws Exception { public void testBindingNoErrors() throws BindException {
TestBean rod = new TestBean(); TestBean rod = new TestBean();
DataBinder binder = new DataBinder(rod, "person"); DataBinder binder = new DataBinder(rod, "person");
assertTrue(binder.isIgnoreUnknownFields()); assertTrue(binder.isIgnoreUnknownFields());
@ -116,7 +118,7 @@ public class DataBinderTests {
} }
@Test @Test
public void testBindingWithDefaultConversionNoErrors() throws Exception { public void testBindingWithDefaultConversionNoErrors() throws BindException {
TestBean rod = new TestBean(); TestBean rod = new TestBean();
DataBinder binder = new DataBinder(rod, "person"); DataBinder binder = new DataBinder(rod, "person");
assertTrue(binder.isIgnoreUnknownFields()); assertTrue(binder.isIgnoreUnknownFields());
@ -132,7 +134,7 @@ public class DataBinderTests {
} }
@Test @Test
public void testNestedBindingWithDefaultConversionNoErrors() throws Exception { public void testNestedBindingWithDefaultConversionNoErrors() throws BindException {
TestBean rod = new TestBean(new TestBean()); TestBean rod = new TestBean(new TestBean());
DataBinder binder = new DataBinder(rod, "person"); DataBinder binder = new DataBinder(rod, "person");
assertTrue(binder.isIgnoreUnknownFields()); assertTrue(binder.isIgnoreUnknownFields());
@ -148,7 +150,7 @@ public class DataBinderTests {
} }
@Test @Test
public void testBindingNoErrorsNotIgnoreUnknown() throws Exception { public void testBindingNoErrorsNotIgnoreUnknown() {
TestBean rod = new TestBean(); TestBean rod = new TestBean();
DataBinder binder = new DataBinder(rod, "person"); DataBinder binder = new DataBinder(rod, "person");
binder.setIgnoreUnknownFields(false); binder.setIgnoreUnknownFields(false);
@ -167,7 +169,7 @@ public class DataBinderTests {
} }
@Test @Test
public void testBindingNoErrorsWithInvalidField() throws Exception { public void testBindingNoErrorsWithInvalidField() {
TestBean rod = new TestBean(); TestBean rod = new TestBean();
DataBinder binder = new DataBinder(rod, "person"); DataBinder binder = new DataBinder(rod, "person");
MutablePropertyValues pvs = new MutablePropertyValues(); MutablePropertyValues pvs = new MutablePropertyValues();
@ -184,7 +186,7 @@ public class DataBinderTests {
} }
@Test @Test
public void testBindingNoErrorsWithIgnoreInvalid() throws Exception { public void testBindingNoErrorsWithIgnoreInvalid() {
TestBean rod = new TestBean(); TestBean rod = new TestBean();
DataBinder binder = new DataBinder(rod, "person"); DataBinder binder = new DataBinder(rod, "person");
binder.setIgnoreInvalidFields(true); binder.setIgnoreInvalidFields(true);
@ -196,7 +198,7 @@ public class DataBinderTests {
} }
@Test @Test
public void testBindingWithErrors() throws Exception { public void testBindingWithErrors() {
TestBean rod = new TestBean(); TestBean rod = new TestBean();
DataBinder binder = new DataBinder(rod, "person"); DataBinder binder = new DataBinder(rod, "person");
MutablePropertyValues pvs = new MutablePropertyValues(); MutablePropertyValues pvs = new MutablePropertyValues();
@ -239,6 +241,7 @@ public class DataBinderTests {
assertEquals("typeMismatch", binder.getBindingResult().getFieldError("age").getCode()); assertEquals("typeMismatch", binder.getBindingResult().getFieldError("age").getCode());
assertEquals("32x", binder.getBindingResult().getFieldValue("age")); assertEquals("32x", binder.getBindingResult().getFieldValue("age"));
assertEquals("32x", binder.getBindingResult().getFieldError("age").getRejectedValue()); assertEquals("32x", binder.getBindingResult().getFieldError("age").getRejectedValue());
assertTrue(binder.getBindingResult().getFieldError("age").getSource() instanceof TypeMismatchException);
assertEquals(0, tb.getAge()); assertEquals(0, tb.getAge());
assertTrue("Has touchy errors", br.hasFieldErrors("touchy")); assertTrue("Has touchy errors", br.hasFieldErrors("touchy"));
@ -246,6 +249,7 @@ public class DataBinderTests {
assertEquals("methodInvocation", binder.getBindingResult().getFieldError("touchy").getCode()); assertEquals("methodInvocation", binder.getBindingResult().getFieldError("touchy").getCode());
assertEquals("m.y", binder.getBindingResult().getFieldValue("touchy")); assertEquals("m.y", binder.getBindingResult().getFieldValue("touchy"));
assertEquals("m.y", binder.getBindingResult().getFieldError("touchy").getRejectedValue()); assertEquals("m.y", binder.getBindingResult().getFieldError("touchy").getRejectedValue());
assertTrue(binder.getBindingResult().getFieldError("touchy").getSource() instanceof MethodInvocationException);
assertNull(tb.getTouchy()); assertNull(tb.getTouchy());
rod = new TestBean(); rod = new TestBean();
@ -260,7 +264,7 @@ public class DataBinderTests {
} }
@Test @Test
public void testBindingWithSystemFieldError() throws Exception { public void testBindingWithSystemFieldError() {
TestBean rod = new TestBean(); TestBean rod = new TestBean();
DataBinder binder = new DataBinder(rod, "person"); DataBinder binder = new DataBinder(rod, "person");
MutablePropertyValues pvs = new MutablePropertyValues(); MutablePropertyValues pvs = new MutablePropertyValues();
@ -277,7 +281,7 @@ public class DataBinderTests {
} }
@Test @Test
public void testBindingWithErrorsAndCustomEditors() throws Exception { public void testBindingWithErrorsAndCustomEditors() {
TestBean rod = new TestBean(); TestBean rod = new TestBean();
DataBinder binder = new DataBinder(rod, "person"); DataBinder binder = new DataBinder(rod, "person");
binder.registerCustomEditor(String.class, "touchy", new PropertyEditorSupport() { binder.registerCustomEditor(String.class, "touchy", new PropertyEditorSupport() {
@ -683,7 +687,7 @@ public class DataBinderTests {
} }
@Test @Test
public void testBindingWithAllowedFields() throws Exception { public void testBindingWithAllowedFields() throws BindException {
TestBean rod = new TestBean(); TestBean rod = new TestBean();
DataBinder binder = new DataBinder(rod); DataBinder binder = new DataBinder(rod);
binder.setAllowedFields("name", "myparam"); binder.setAllowedFields("name", "myparam");
@ -698,7 +702,7 @@ public class DataBinderTests {
} }
@Test @Test
public void testBindingWithDisallowedFields() throws Exception { public void testBindingWithDisallowedFields() throws BindException {
TestBean rod = new TestBean(); TestBean rod = new TestBean();
DataBinder binder = new DataBinder(rod); DataBinder binder = new DataBinder(rod);
binder.setDisallowedFields("age"); binder.setDisallowedFields("age");
@ -716,7 +720,7 @@ public class DataBinderTests {
} }
@Test @Test
public void testBindingWithAllowedAndDisallowedFields() throws Exception { public void testBindingWithAllowedAndDisallowedFields() throws BindException {
TestBean rod = new TestBean(); TestBean rod = new TestBean();
DataBinder binder = new DataBinder(rod); DataBinder binder = new DataBinder(rod);
binder.setAllowedFields("name", "myparam"); binder.setAllowedFields("name", "myparam");
@ -735,7 +739,7 @@ public class DataBinderTests {
} }
@Test @Test
public void testBindingWithOverlappingAllowedAndDisallowedFields() throws Exception { public void testBindingWithOverlappingAllowedAndDisallowedFields() throws BindException {
TestBean rod = new TestBean(); TestBean rod = new TestBean();
DataBinder binder = new DataBinder(rod); DataBinder binder = new DataBinder(rod);
binder.setAllowedFields("name", "age"); binder.setAllowedFields("name", "age");
@ -754,7 +758,7 @@ public class DataBinderTests {
} }
@Test @Test
public void testBindingWithAllowedFieldsUsingAsterisks() throws Exception { public void testBindingWithAllowedFieldsUsingAsterisks() throws BindException {
TestBean rod = new TestBean(); TestBean rod = new TestBean();
DataBinder binder = new DataBinder(rod, "person"); DataBinder binder = new DataBinder(rod, "person");
binder.setAllowedFields("nam*", "*ouchy"); binder.setAllowedFields("nam*", "*ouchy");
@ -781,7 +785,7 @@ public class DataBinderTests {
} }
@Test @Test
public void testBindingWithAllowedAndDisallowedMapFields() throws Exception { public void testBindingWithAllowedAndDisallowedMapFields() throws BindException {
TestBean rod = new TestBean(); TestBean rod = new TestBean();
DataBinder binder = new DataBinder(rod); DataBinder binder = new DataBinder(rod);
binder.setAllowedFields("someMap[key1]", "someMap[key2]"); binder.setAllowedFields("someMap[key1]", "someMap[key2]");
@ -809,7 +813,7 @@ public class DataBinderTests {
* Tests for required field, both null, non-existing and empty strings. * Tests for required field, both null, non-existing and empty strings.
*/ */
@Test @Test
public void testBindingWithRequiredFields() throws Exception { public void testBindingWithRequiredFields() {
TestBean tb = new TestBean(); TestBean tb = new TestBean();
tb.setSpouse(new TestBean()); tb.setSpouse(new TestBean());
@ -840,7 +844,7 @@ public class DataBinderTests {
} }
@Test @Test
public void testBindingWithRequiredMapFields() throws Exception { public void testBindingWithRequiredMapFields() {
TestBean tb = new TestBean(); TestBean tb = new TestBean();
tb.setSpouse(new TestBean()); tb.setSpouse(new TestBean());
@ -860,7 +864,7 @@ public class DataBinderTests {
} }
@Test @Test
public void testBindingWithNestedObjectCreation() throws Exception { public void testBindingWithNestedObjectCreation() {
TestBean tb = new TestBean(); TestBean tb = new TestBean();
DataBinder binder = new DataBinder(tb, "person"); DataBinder binder = new DataBinder(tb, "person");
@ -1828,7 +1832,7 @@ public class DataBinderTests {
} }
@Test @Test
public void testRejectWithoutDefaultMessage() throws Exception { public void testRejectWithoutDefaultMessage() {
TestBean tb = new TestBean(); TestBean tb = new TestBean();
tb.setName("myName"); tb.setName("myName");
tb.setAge(99); tb.setAge(99);
@ -1875,7 +1879,7 @@ public class DataBinderTests {
} }
@Test @Test
public void testTrackDisallowedFields() throws Exception { public void testTrackDisallowedFields() {
TestBean testBean = new TestBean(); TestBean testBean = new TestBean();
DataBinder binder = new DataBinder(testBean, "testBean"); DataBinder binder = new DataBinder(testBean, "testBean");
binder.setAllowedFields("name", "age"); binder.setAllowedFields("name", "age");
@ -1895,7 +1899,7 @@ public class DataBinderTests {
} }
@Test @Test
public void testAutoGrowWithinDefaultLimit() throws Exception { public void testAutoGrowWithinDefaultLimit() {
TestBean testBean = new TestBean(); TestBean testBean = new TestBean();
DataBinder binder = new DataBinder(testBean, "testBean"); DataBinder binder = new DataBinder(testBean, "testBean");
@ -1907,7 +1911,7 @@ public class DataBinderTests {
} }
@Test @Test
public void testAutoGrowBeyondDefaultLimit() throws Exception { public void testAutoGrowBeyondDefaultLimit() {
TestBean testBean = new TestBean(); TestBean testBean = new TestBean();
DataBinder binder = new DataBinder(testBean, "testBean"); DataBinder binder = new DataBinder(testBean, "testBean");
@ -1924,7 +1928,7 @@ public class DataBinderTests {
} }
@Test @Test
public void testAutoGrowWithinCustomLimit() throws Exception { public void testAutoGrowWithinCustomLimit() {
TestBean testBean = new TestBean(); TestBean testBean = new TestBean();
DataBinder binder = new DataBinder(testBean, "testBean"); DataBinder binder = new DataBinder(testBean, "testBean");
binder.setAutoGrowCollectionLimit(10); binder.setAutoGrowCollectionLimit(10);
@ -1937,7 +1941,7 @@ public class DataBinderTests {
} }
@Test @Test
public void testAutoGrowBeyondCustomLimit() throws Exception { public void testAutoGrowBeyondCustomLimit() {
TestBean testBean = new TestBean(); TestBean testBean = new TestBean();
DataBinder binder = new DataBinder(testBean, "testBean"); DataBinder binder = new DataBinder(testBean, "testBean");
binder.setAutoGrowCollectionLimit(10); binder.setAutoGrowCollectionLimit(10);
@ -1971,7 +1975,7 @@ public class DataBinderTests {
} }
@Test @Test
public void testFieldErrorAccessVariations() throws Exception { public void testFieldErrorAccessVariations() {
TestBean testBean = new TestBean(); TestBean testBean = new TestBean();
DataBinder binder = new DataBinder(testBean, "testBean"); DataBinder binder = new DataBinder(testBean, "testBean");
assertNull(binder.getBindingResult().getGlobalError()); assertNull(binder.getBindingResult().getGlobalError());

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -32,6 +32,7 @@ import java.util.Set;
import javax.validation.Constraint; import javax.validation.Constraint;
import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext; import javax.validation.ConstraintValidatorContext;
import javax.validation.ConstraintViolation;
import javax.validation.Payload; import javax.validation.Payload;
import javax.validation.Valid; import javax.validation.Valid;
import javax.validation.Validation; import javax.validation.Validation;
@ -48,6 +49,7 @@ import org.springframework.beans.BeanWrapperImpl;
import org.springframework.context.support.StaticMessageSource; import org.springframework.context.support.StaticMessageSource;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
import org.springframework.validation.BeanPropertyBindingResult; import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.validation.FieldError;
import static java.lang.annotation.ElementType.*; import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.*; import static java.lang.annotation.RetentionPolicy.*;
@ -93,8 +95,11 @@ public class SpringValidatorAdapterTests {
assertThat(errors.getFieldErrorCount("password"), is(1)); assertThat(errors.getFieldErrorCount("password"), is(1));
assertThat(errors.getFieldValue("password"), is("pass")); assertThat(errors.getFieldValue("password"), is("pass"));
assertThat(messageSource.getMessage(errors.getFieldError("password"), Locale.ENGLISH), FieldError error = errors.getFieldError("password");
is("Size of Password is must be between 8 and 128")); assertNotNull(error);
assertThat(messageSource.getMessage(error, Locale.ENGLISH), is("Size of Password is must be between 8 and 128"));
assertTrue(error.getSource() instanceof ConstraintViolation);
assertThat(((ConstraintViolation) error.getSource()).getPropertyPath().toString(), is("password"));
} }
@Test // SPR-13406 @Test // SPR-13406
@ -108,8 +113,11 @@ public class SpringValidatorAdapterTests {
assertThat(errors.getFieldErrorCount("password"), is(1)); assertThat(errors.getFieldErrorCount("password"), is(1));
assertThat(errors.getFieldValue("password"), is("password")); assertThat(errors.getFieldValue("password"), is("password"));
assertThat(messageSource.getMessage(errors.getFieldError("password"), Locale.ENGLISH), FieldError error = errors.getFieldError("password");
is("Password must be same value with Password(Confirm)")); assertNotNull(error);
assertThat(messageSource.getMessage(error, Locale.ENGLISH), is("Password must be same value with Password(Confirm)"));
assertTrue(error.getSource() instanceof ConstraintViolation);
assertThat(((ConstraintViolation) error.getSource()).getPropertyPath().toString(), is("password"));
} }
@Test // SPR-13406 @Test // SPR-13406
@ -124,10 +132,16 @@ public class SpringValidatorAdapterTests {
assertThat(errors.getFieldErrorCount("email"), is(1)); assertThat(errors.getFieldErrorCount("email"), is(1));
assertThat(errors.getFieldValue("email"), is("test@example.com")); assertThat(errors.getFieldValue("email"), is("test@example.com"));
assertThat(errors.getFieldErrorCount("confirmEmail"), is(1)); assertThat(errors.getFieldErrorCount("confirmEmail"), is(1));
assertThat(messageSource.getMessage(errors.getFieldError("email"), Locale.ENGLISH), FieldError error1 = errors.getFieldError("email");
is("email must be same value with confirmEmail")); FieldError error2 = errors.getFieldError("confirmEmail");
assertThat(messageSource.getMessage(errors.getFieldError("confirmEmail"), Locale.ENGLISH), assertNotNull(error1);
is("Email required")); assertNotNull(error2);
assertThat(messageSource.getMessage(error1, Locale.ENGLISH), is("email must be same value with confirmEmail"));
assertThat(messageSource.getMessage(error2, Locale.ENGLISH), is("Email required"));
assertTrue(error1.getSource() instanceof ConstraintViolation);
assertThat(((ConstraintViolation) error1.getSource()).getPropertyPath().toString(), is("email"));
assertTrue(error2.getSource() instanceof ConstraintViolation);
assertThat(((ConstraintViolation) error2.getSource()).getPropertyPath().toString(), is("confirmEmail"));
} }
@Test // SPR-15123 @Test // SPR-15123
@ -144,10 +158,16 @@ public class SpringValidatorAdapterTests {
assertThat(errors.getFieldErrorCount("email"), is(1)); assertThat(errors.getFieldErrorCount("email"), is(1));
assertThat(errors.getFieldValue("email"), is("test@example.com")); assertThat(errors.getFieldValue("email"), is("test@example.com"));
assertThat(errors.getFieldErrorCount("confirmEmail"), is(1)); assertThat(errors.getFieldErrorCount("confirmEmail"), is(1));
assertThat(messageSource.getMessage(errors.getFieldError("email"), Locale.ENGLISH), FieldError error1 = errors.getFieldError("email");
is("email must be same value with confirmEmail")); FieldError error2 = errors.getFieldError("confirmEmail");
assertThat(messageSource.getMessage(errors.getFieldError("confirmEmail"), Locale.ENGLISH), assertNotNull(error1);
is("Email required")); assertNotNull(error2);
assertThat(messageSource.getMessage(error1, Locale.ENGLISH), is("email must be same value with confirmEmail"));
assertThat(messageSource.getMessage(error2, Locale.ENGLISH), is("Email required"));
assertTrue(error1.getSource() instanceof ConstraintViolation);
assertThat(((ConstraintViolation) error1.getSource()).getPropertyPath().toString(), is("email"));
assertTrue(error2.getSource() instanceof ConstraintViolation);
assertThat(((ConstraintViolation) error2.getSource()).getPropertyPath().toString(), is("confirmEmail"));
} }
@Test // SPR-16177 @Test // SPR-16177