|
|
|
@ -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()); |
|
|
|
|