Detect invalid configuration for autoGrowCollectionLimit on DataBinder

Issue: SPR-14888
This commit is contained in:
Juergen Hoeller
2016-11-08 17:47:29 +01:00
parent 7ffed858f0
commit 62631bfe33
2 changed files with 39 additions and 4 deletions

View File

@@ -36,7 +36,9 @@ import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.beans.InvalidPropertyException;
import org.springframework.beans.MutablePropertyValues;
@@ -69,9 +71,14 @@ import static org.junit.Assert.*;
* @author Rod Johnson
* @author Juergen Hoeller
* @author Rob Harrop
* @author Kazuki Shimizu
*/
public class DataBinderTests {
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Test
public void testBindingNoErrors() throws Exception {
TestBean rod = new TestBean();
@@ -1982,6 +1989,30 @@ public class DataBinderTests {
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")
private static class BeanWithIntegerList {
@@ -2112,7 +2143,7 @@ public class DataBinderTests {
private List<E> list;
public GrowingList() {
this.list = new ArrayList<E>();
this.list = new ArrayList<>();
}
public List<E> getWrappedList() {
@@ -2200,8 +2231,8 @@ public class DataBinderTests {
private final Map<String, Object> f;
public Form() {
f = new HashMap<String, Object>();
f.put("list", new GrowingList<Object>());
f = new HashMap<>();
f.put("list", new GrowingList<>());
}
public Map<String, Object> getF() {