@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 public void testAutoGrowWithinCustomLimit() { TestBean testBean = new TestBean(); DataBinder binder = new DataBinder(testBean, "testBean"); binder.setAutoGrowCollectionLimit(10); MutablePropertyValues mpvs = new MutablePropertyValues(); mpvs.add("friends[4]", ""); binder.bind(mpvs); assertEquals(5, testBean.getFriends().size()); }
@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); }
@Test public void testAutoGrowBeyondCustomLimit() { TestBean testBean = new TestBean(); DataBinder binder = new DataBinder(testBean, "testBean"); binder.setAutoGrowCollectionLimit(10); MutablePropertyValues mpvs = new MutablePropertyValues(); mpvs.add("friends[16]", ""); try { binder.bind(mpvs); fail("Should have thrown InvalidPropertyException"); } catch (InvalidPropertyException ex) { // expected assertTrue(ex.getRootCause() instanceof IndexOutOfBoundsException); } }
@Test // SPR-15009 public void testSetCustomMessageCodesResolverBeforeInitializeBindingResultForBeanPropertyAccess() { TestBean testBean = new TestBean(); DataBinder binder = new DataBinder(testBean, "testBean"); DefaultMessageCodesResolver messageCodesResolver = new DefaultMessageCodesResolver(); messageCodesResolver.setPrefix("errors."); binder.setMessageCodesResolver(messageCodesResolver); binder.setAutoGrowCollectionLimit(512); // allow configuration after set a MessageCodesResolver binder.initBeanPropertyAccess(); MutablePropertyValues mpv = new MutablePropertyValues(); mpv.add("age", "invalid"); binder.bind(mpv); assertEquals("errors.typeMismatch", binder.getBindingResult().getFieldError("age").getCode()); assertEquals(512, BeanWrapper.class.cast(binder.getInternalBindingResult().getPropertyAccessor()).getAutoGrowCollectionLimit()); }