Merge pull request #1233 from kazuki43zoo/SPR-14888
Detect invalid configuration for autoGrowCollectionLimit on DataBinder
This commit is contained in:
@@ -95,6 +95,7 @@ import org.springframework.util.StringUtils;
|
|||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
* @author Rob Harrop
|
* @author Rob Harrop
|
||||||
* @author Stephane Nicoll
|
* @author Stephane Nicoll
|
||||||
|
* @author Kazuki Shimizu
|
||||||
* @see #setAllowedFields
|
* @see #setAllowedFields
|
||||||
* @see #setRequiredFields
|
* @see #setRequiredFields
|
||||||
* @see #registerCustomEditor
|
* @see #registerCustomEditor
|
||||||
@@ -213,8 +214,12 @@ public class DataBinder implements PropertyEditorRegistry, TypeConverter {
|
|||||||
* Specify the limit for array and collection auto-growing.
|
* Specify the limit for array and collection auto-growing.
|
||||||
* <p>Default is 256, preventing OutOfMemoryErrors in case of large indexes.
|
* <p>Default is 256, preventing OutOfMemoryErrors in case of large indexes.
|
||||||
* Raise this limit if your auto-growing needs are unusually high.
|
* Raise this limit if your auto-growing needs are unusually high.
|
||||||
|
* @see #initBeanPropertyAccess()
|
||||||
|
* @see org.springframework.beans.BeanWrapper#setAutoGrowCollectionLimit
|
||||||
*/
|
*/
|
||||||
public void setAutoGrowCollectionLimit(int autoGrowCollectionLimit) {
|
public void setAutoGrowCollectionLimit(int autoGrowCollectionLimit) {
|
||||||
|
Assert.state(this.bindingResult == null,
|
||||||
|
"DataBinder is already initialized - call setAutoGrowCollectionLimit before other configuration methods");
|
||||||
this.autoGrowCollectionLimit = autoGrowCollectionLimit;
|
this.autoGrowCollectionLimit = autoGrowCollectionLimit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -36,8 +36,10 @@ import java.util.Optional;
|
|||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.TreeSet;
|
import java.util.TreeSet;
|
||||||
|
|
||||||
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import org.junit.rules.ExpectedException;
|
||||||
import org.springframework.beans.InvalidPropertyException;
|
import org.springframework.beans.InvalidPropertyException;
|
||||||
import org.springframework.beans.MutablePropertyValues;
|
import org.springframework.beans.MutablePropertyValues;
|
||||||
import org.springframework.beans.NotWritablePropertyException;
|
import org.springframework.beans.NotWritablePropertyException;
|
||||||
@@ -69,9 +71,13 @@ import static org.junit.Assert.*;
|
|||||||
* @author Rod Johnson
|
* @author Rod Johnson
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
* @author Rob Harrop
|
* @author Rob Harrop
|
||||||
|
* @author Kazuki Shimizu
|
||||||
*/
|
*/
|
||||||
public class DataBinderTests {
|
public class DataBinderTests {
|
||||||
|
|
||||||
|
@Rule
|
||||||
|
public ExpectedException expectedException = ExpectedException.none();
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBindingNoErrors() throws Exception {
|
public void testBindingNoErrors() throws Exception {
|
||||||
TestBean rod = new TestBean();
|
TestBean rod = new TestBean();
|
||||||
@@ -1982,6 +1988,30 @@ public class DataBinderTests {
|
|||||||
assertEquals("age", binder.getBindingResult().getFieldError("age").getField());
|
assertEquals("age", binder.getBindingResult().getFieldError("age").getField());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test // SPR-14888
|
||||||
|
public void testSetAutoGrowCollectionLimit() {
|
||||||
|
BeanWithIntegerList tb = new BeanWithIntegerList();
|
||||||
|
DataBinder binder = new DataBinder(tb);
|
||||||
|
binder.setAutoGrowCollectionLimit(257);
|
||||||
|
MutablePropertyValues pvs = new MutablePropertyValues();
|
||||||
|
pvs.add("integerList[256]", "1");
|
||||||
|
|
||||||
|
binder.bind(pvs);
|
||||||
|
assertEquals(257, tb.getIntegerList().size());
|
||||||
|
assertEquals(Integer.valueOf(1), tb.getIntegerList().get(256));
|
||||||
|
assertEquals(Integer.valueOf(1), binder.getBindingResult().getFieldValue("integerList[256]"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test // SPR-14888
|
||||||
|
public void testSetAutoGrowCollectionLimitAfterInitialization() {
|
||||||
|
|
||||||
|
expectedException.expect(IllegalStateException.class);
|
||||||
|
expectedException.expectMessage("DataBinder is already initialized - call setAutoGrowCollectionLimit before other configuration methods");
|
||||||
|
|
||||||
|
DataBinder binder = new DataBinder(new BeanWithIntegerList());
|
||||||
|
binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
|
||||||
|
binder.setAutoGrowCollectionLimit(257);
|
||||||
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
private static class BeanWithIntegerList {
|
private static class BeanWithIntegerList {
|
||||||
|
|||||||
Reference in New Issue
Block a user