java.beans.PropertyEditorSupport Java Examples

The following examples show how to use java.beans.PropertyEditorSupport. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CustomEditorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testCustomEditorForSingleNestedProperty() {
	TestBean tb = new TestBean();
	tb.setSpouse(new TestBean());
	BeanWrapper bw = new BeanWrapperImpl(tb);
	bw.registerCustomEditor(String.class, "spouse.name", new PropertyEditorSupport() {
		@Override
		public void setAsText(String text) throws IllegalArgumentException {
			setValue("prefix" + text);
		}
	});
	bw.setPropertyValue("spouse.name", "value");
	bw.setPropertyValue("touchy", "value");
	assertEquals("prefixvalue", bw.getPropertyValue("spouse.name"));
	assertEquals("prefixvalue", tb.getSpouse().getName());
	assertEquals("value", bw.getPropertyValue("touchy"));
	assertEquals("value", tb.getTouchy());
}
 
Example #2
Source File: DataBinderTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testBindingWithNestedObjectCreation() {
	TestBean tb = new TestBean();

	DataBinder binder = new DataBinder(tb, "person");
	binder.registerCustomEditor(ITestBean.class, new PropertyEditorSupport() {
		@Override
		public void setAsText(String text) throws IllegalArgumentException {
			setValue(new TestBean());
		}
	});

	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("spouse", "someValue");
	pvs.add("spouse.name", "test");
	binder.bind(pvs);

	assertNotNull(tb.getSpouse());
	assertEquals("test", tb.getSpouse().getName());
}
 
Example #3
Source File: DataBinderTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testCustomEditorForPrimitiveProperty() {
	TestBean tb = new TestBean();
	DataBinder binder = new DataBinder(tb, "tb");

	binder.registerCustomEditor(int.class, "age", new PropertyEditorSupport() {
		@Override
		public void setAsText(String text) throws IllegalArgumentException {
			setValue(new Integer(99));
		}
		@Override
		public String getAsText() {
			return "argh";
		}
	});

	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("age", "");
	binder.bind(pvs);

	assertEquals("argh", binder.getBindingResult().getFieldValue("age"));
	assertEquals(99, tb.getAge());
}
 
Example #4
Source File: CustomEditorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testCustomEditorForSingleNestedProperty() {
	TestBean tb = new TestBean();
	tb.setSpouse(new TestBean());
	BeanWrapper bw = new BeanWrapperImpl(tb);
	bw.registerCustomEditor(String.class, "spouse.name", new PropertyEditorSupport() {
		@Override
		public void setAsText(String text) throws IllegalArgumentException {
			setValue("prefix" + text);
		}
	});
	bw.setPropertyValue("spouse.name", "value");
	bw.setPropertyValue("touchy", "value");
	assertEquals("prefixvalue", bw.getPropertyValue("spouse.name"));
	assertEquals("prefixvalue", tb.getSpouse().getName());
	assertEquals("value", bw.getPropertyValue("touchy"));
	assertEquals("value", tb.getTouchy());
}
 
Example #5
Source File: DataBinderTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testEditorForNestedIndexedField() {
	IndexedTestBean tb = new IndexedTestBean();
	tb.getArray()[0].setNestedIndexedBean(new IndexedTestBean());
	tb.getArray()[1].setNestedIndexedBean(new IndexedTestBean());
	DataBinder binder = new DataBinder(tb, "tb");
	binder.registerCustomEditor(String.class, "array.nestedIndexedBean.list.name", new PropertyEditorSupport() {
		@Override
		public void setAsText(String text) throws IllegalArgumentException {
			setValue("list" + text);
		}
		@Override
		public String getAsText() {
			return ((String) getValue()).substring(4);
		}
	});
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("array[0].nestedIndexedBean.list[0].name", "test1");
	pvs.add("array[1].nestedIndexedBean.list[1].name", "test2");
	binder.bind(pvs);
	assertEquals("listtest1", ((TestBean)tb.getArray()[0].getNestedIndexedBean().getList().get(0)).getName());
	assertEquals("listtest2", ((TestBean)tb.getArray()[1].getNestedIndexedBean().getList().get(1)).getName());
	assertEquals("test1", binder.getBindingResult().getFieldValue("array[0].nestedIndexedBean.list[0].name"));
	assertEquals("test2", binder.getBindingResult().getFieldValue("array[1].nestedIndexedBean.list[1].name"));
}
 
Example #6
Source File: DataBinderTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testSpecificEditorForNestedIndexedField() {
	IndexedTestBean tb = new IndexedTestBean();
	tb.getArray()[0].setNestedIndexedBean(new IndexedTestBean());
	tb.getArray()[1].setNestedIndexedBean(new IndexedTestBean());
	DataBinder binder = new DataBinder(tb, "tb");
	binder.registerCustomEditor(String.class, "array[0].nestedIndexedBean.list.name", new PropertyEditorSupport() {
		@Override
		public void setAsText(String text) throws IllegalArgumentException {
			setValue("list" + text);
		}
		@Override
		public String getAsText() {
			return ((String) getValue()).substring(4);
		}
	});
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("array[0].nestedIndexedBean.list[0].name", "test1");
	pvs.add("array[1].nestedIndexedBean.list[1].name", "test2");
	binder.bind(pvs);
	assertEquals("listtest1", ((TestBean)tb.getArray()[0].getNestedIndexedBean().getList().get(0)).getName());
	assertEquals("test2", ((TestBean)tb.getArray()[1].getNestedIndexedBean().getList().get(1)).getName());
	assertEquals("test1", binder.getBindingResult().getFieldValue("array[0].nestedIndexedBean.list[0].name"));
	assertEquals("test2", binder.getBindingResult().getFieldValue("array[1].nestedIndexedBean.list[1].name"));
}
 
Example #7
Source File: DataBinderTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testInnerSpecificEditorForNestedIndexedField() {
	IndexedTestBean tb = new IndexedTestBean();
	tb.getArray()[0].setNestedIndexedBean(new IndexedTestBean());
	tb.getArray()[1].setNestedIndexedBean(new IndexedTestBean());
	DataBinder binder = new DataBinder(tb, "tb");
	binder.registerCustomEditor(String.class, "array.nestedIndexedBean.list[0].name", new PropertyEditorSupport() {
		@Override
		public void setAsText(String text) throws IllegalArgumentException {
			setValue("list" + text);
		}
		@Override
		public String getAsText() {
			return ((String) getValue()).substring(4);
		}
	});
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("array[0].nestedIndexedBean.list[0].name", "test1");
	pvs.add("array[1].nestedIndexedBean.list[1].name", "test2");
	binder.bind(pvs);
	assertEquals("listtest1", ((TestBean)tb.getArray()[0].getNestedIndexedBean().getList().get(0)).getName());
	assertEquals("test2", ((TestBean)tb.getArray()[1].getNestedIndexedBean().getList().get(1)).getName());
	assertEquals("test1", binder.getBindingResult().getFieldValue("array[0].nestedIndexedBean.list[0].name"));
	assertEquals("test2", binder.getBindingResult().getFieldValue("array[1].nestedIndexedBean.list[1].name"));
}
 
Example #8
Source File: DataBinderTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testBindToStringArrayWithArrayEditor() {
	TestBean tb = new TestBean();
	DataBinder binder = new DataBinder(tb, "tb");
	binder.registerCustomEditor(String[].class, "stringArray", new PropertyEditorSupport() {
		@Override
		public void setAsText(String text) throws IllegalArgumentException {
			setValue(StringUtils.delimitedListToStringArray(text, "-"));
		}
	});
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("stringArray", "a1-b2");
	binder.bind(pvs);
	assertTrue(!binder.getBindingResult().hasErrors());
	assertEquals(2, tb.getStringArray().length);
	assertEquals("a1", tb.getStringArray()[0]);
	assertEquals("b2", tb.getStringArray()[1]);
}
 
Example #9
Source File: CustomEditorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testCustomEditorForAllNestedStringProperties() {
	TestBean tb = new TestBean();
	tb.setSpouse(new TestBean());
	BeanWrapper bw = new BeanWrapperImpl(tb);
	bw.registerCustomEditor(String.class, new PropertyEditorSupport() {
		@Override
		public void setAsText(String text) throws IllegalArgumentException {
			setValue("prefix" + text);
		}
	});
	bw.setPropertyValue("spouse.name", "value");
	bw.setPropertyValue("touchy", "value");
	assertEquals("prefixvalue", bw.getPropertyValue("spouse.name"));
	assertEquals("prefixvalue", tb.getSpouse().getName());
	assertEquals("prefixvalue", bw.getPropertyValue("touchy"));
	assertEquals("prefixvalue", tb.getTouchy());
}
 
Example #10
Source File: SelectTagTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void nestedPathWithListAndEditor() throws Exception {
	this.tag.setPath("bean.realCountry");
	this.tag.setItems(Country.getCountries());
	this.tag.setItemValue("isoCode");
	this.tag.setItemLabel("name");
	TestBeanWrapper testBean = new TestBeanWrapper();
	testBean.setBean(getTestBean());
	BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(testBean , "testBean");
	bindingResult.getPropertyAccessor().registerCustomEditor(Country.class, new PropertyEditorSupport() {
		@Override
		public void setAsText(String text) throws IllegalArgumentException {
			setValue(Country.getCountryWithIsoCode(text));
		}
		@Override
		public String getAsText() {
			return ((Country) getValue()).getName();
		}
	});
	getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "testBean", bindingResult);
	this.tag.doStartTag();
	String output = getOutput();
	assertTrue(output.startsWith("<select "));
	assertTrue(output.endsWith("</select>"));
	assertTrue(output.contains("option value=\"AT\" selected=\"selected\">Austria"));
}
 
Example #11
Source File: WebRequestDataBinderTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testBindingWithNestedObjectCreation() throws Exception {
	TestBean tb = new TestBean();

	WebRequestDataBinder binder = new WebRequestDataBinder(tb, "person");
	binder.registerCustomEditor(ITestBean.class, new PropertyEditorSupport() {
		@Override
		public void setAsText(String text) throws IllegalArgumentException {
			setValue(new TestBean());
		}
	});

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addParameter("spouse", "someValue");
	request.addParameter("spouse.name", "test");
	binder.bind(new ServletWebRequest(request));

	assertNotNull(tb.getSpouse());
	assertEquals("test", tb.getSpouse().getName());
}
 
Example #12
Source File: ServletRequestDataBinderTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testBindingWithNestedObjectCreation() throws Exception {
	TestBean tb = new TestBean();

	ServletRequestDataBinder binder = new ServletRequestDataBinder(tb, "person");
	binder.registerCustomEditor(ITestBean.class, new PropertyEditorSupport() {
		@Override
		public void setAsText(String text) throws IllegalArgumentException {
			setValue(new TestBean());
		}
	});

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addParameter("spouse", "someValue");
	request.addParameter("spouse.name", "test");
	binder.bind(request);

	assertNotNull(tb.getSpouse());
	assertEquals("test", tb.getSpouse().getName());
}
 
Example #13
Source File: ServletRequestDataBinderTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testBindingWithNestedObjectCreationAndWrongOrder() throws Exception {
	TestBean tb = new TestBean();

	ServletRequestDataBinder binder = new ServletRequestDataBinder(tb, "person");
	binder.registerCustomEditor(ITestBean.class, new PropertyEditorSupport() {
		@Override
		public void setAsText(String text) throws IllegalArgumentException {
			setValue(new TestBean());
		}
	});

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addParameter("spouse.name", "test");
	request.addParameter("spouse", "someValue");
	binder.bind(request);

	assertNotNull(tb.getSpouse());
	assertEquals("test", tb.getSpouse().getName());
}
 
Example #14
Source File: CustomEditorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testCustomEditorForSingleProperty() {
	TestBean tb = new TestBean();
	BeanWrapper bw = new BeanWrapperImpl(tb);
	bw.registerCustomEditor(String.class, "name", new PropertyEditorSupport() {
		@Override
		public void setAsText(String text) throws IllegalArgumentException {
			setValue("prefix" + text);
		}
	});
	bw.setPropertyValue("name", "value");
	bw.setPropertyValue("touchy", "value");
	assertEquals("prefixvalue", bw.getPropertyValue("name"));
	assertEquals("prefixvalue", tb.getName());
	assertEquals("value", bw.getPropertyValue("touchy"));
	assertEquals("value", tb.getTouchy());
}
 
Example #15
Source File: CustomEditorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testCustomEditorForAllStringProperties() {
	TestBean tb = new TestBean();
	BeanWrapper bw = new BeanWrapperImpl(tb);
	bw.registerCustomEditor(String.class, new PropertyEditorSupport() {
		@Override
		public void setAsText(String text) throws IllegalArgumentException {
			setValue("prefix" + text);
		}
	});
	bw.setPropertyValue("name", "value");
	bw.setPropertyValue("touchy", "value");
	assertEquals("prefixvalue", bw.getPropertyValue("name"));
	assertEquals("prefixvalue", tb.getName());
	assertEquals("prefixvalue", bw.getPropertyValue("touchy"));
	assertEquals("prefixvalue", tb.getTouchy());
}
 
Example #16
Source File: DataBinderTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testBindingWithNestedObjectCreation() {
	TestBean tb = new TestBean();

	DataBinder binder = new DataBinder(tb, "person");
	binder.registerCustomEditor(ITestBean.class, new PropertyEditorSupport() {
		@Override
		public void setAsText(String text) throws IllegalArgumentException {
			setValue(new TestBean());
		}
	});

	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("spouse", "someValue");
	pvs.add("spouse.name", "test");
	binder.bind(pvs);

	assertNotNull(tb.getSpouse());
	assertEquals("test", tb.getSpouse().getName());
}
 
Example #17
Source File: CustomEditorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testCustomEditorForAllNestedStringProperties() {
	TestBean tb = new TestBean();
	tb.setSpouse(new TestBean());
	BeanWrapper bw = new BeanWrapperImpl(tb);
	bw.registerCustomEditor(String.class, new PropertyEditorSupport() {
		@Override
		public void setAsText(String text) throws IllegalArgumentException {
			setValue("prefix" + text);
		}
	});
	bw.setPropertyValue("spouse.name", "value");
	bw.setPropertyValue("touchy", "value");
	assertEquals("prefixvalue", bw.getPropertyValue("spouse.name"));
	assertEquals("prefixvalue", tb.getSpouse().getName());
	assertEquals("prefixvalue", bw.getPropertyValue("touchy"));
	assertEquals("prefixvalue", tb.getTouchy());
}
 
Example #18
Source File: DataBinderTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testEditorForNestedIndexedField() {
	IndexedTestBean tb = new IndexedTestBean();
	tb.getArray()[0].setNestedIndexedBean(new IndexedTestBean());
	tb.getArray()[1].setNestedIndexedBean(new IndexedTestBean());
	DataBinder binder = new DataBinder(tb, "tb");
	binder.registerCustomEditor(String.class, "array.nestedIndexedBean.list.name", new PropertyEditorSupport() {
		@Override
		public void setAsText(String text) throws IllegalArgumentException {
			setValue("list" + text);
		}
		@Override
		public String getAsText() {
			return ((String) getValue()).substring(4);
		}
	});
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("array[0].nestedIndexedBean.list[0].name", "test1");
	pvs.add("array[1].nestedIndexedBean.list[1].name", "test2");
	binder.bind(pvs);
	assertEquals("listtest1", ((TestBean)tb.getArray()[0].getNestedIndexedBean().getList().get(0)).getName());
	assertEquals("listtest2", ((TestBean)tb.getArray()[1].getNestedIndexedBean().getList().get(1)).getName());
	assertEquals("test1", binder.getBindingResult().getFieldValue("array[0].nestedIndexedBean.list[0].name"));
	assertEquals("test2", binder.getBindingResult().getFieldValue("array[1].nestedIndexedBean.list[1].name"));
}
 
Example #19
Source File: CustomEditorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testIndexedPropertiesWithListPropertyEditor() {
	IndexedTestBean bean = new IndexedTestBean();
	BeanWrapper bw = new BeanWrapperImpl(bean);
	bw.registerCustomEditor(List.class, "list", new PropertyEditorSupport() {
		@Override
		public void setAsText(String text) throws IllegalArgumentException {
			List<TestBean> result = new ArrayList<>();
			result.add(new TestBean("list" + text, 99));
			setValue(result);
		}
	});
	bw.setPropertyValue("list", "1");
	assertEquals("list1", ((TestBean) bean.getList().get(0)).getName());
	bw.setPropertyValue("list[0]", "test");
	assertEquals("test", bean.getList().get(0));
}
 
Example #20
Source File: SelectTagTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void withListAndEditor() throws Exception {
	this.tag.setPath("realCountry");
	this.tag.setItems(Country.getCountries());
	this.tag.setItemValue("isoCode");
	this.tag.setItemLabel("name");
	BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(getTestBean(), "testBean");
	bindingResult.getPropertyAccessor().registerCustomEditor(Country.class, new PropertyEditorSupport() {
		@Override
		public void setAsText(String text) throws IllegalArgumentException {
			setValue(Country.getCountryWithIsoCode(text));
		}
		@Override
		public String getAsText() {
			return ((Country) getValue()).getName();
		}
	});
	getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "testBean", bindingResult);
	this.tag.doStartTag();
	String output = getOutput();
	assertTrue(output.startsWith("<select "));
	assertTrue(output.endsWith("</select>"));
	assertTrue(output.contains("option value=\"AT\" selected=\"selected\">Austria"));
}
 
Example #21
Source File: SelectTagTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void withListAndTransformTagAndEditor() throws Exception {
	this.tag.setPath("realCountry");
	this.tag.setItems(Country.getCountries());
	BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(getTestBean(), "testBean");
	bindingResult.getPropertyAccessor().registerCustomEditor(Country.class, new PropertyEditorSupport() {
		@Override
		public void setAsText(String text) throws IllegalArgumentException {
			setValue(Country.getCountryWithIsoCode(text));
		}
		@Override
		public String getAsText() {
			return ((Country) getValue()).getName();
		}
	});
	getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "testBean", bindingResult);
	this.tag.doStartTag();

	TransformTag transformTag = new TransformTag();
	transformTag.setValue(Country.getCountries().get(0));
	transformTag.setVar("key");
	transformTag.setParent(this.tag);
	transformTag.setPageContext(getPageContext());
	transformTag.doStartTag();
	assertEquals("Austria", getPageContext().findAttribute("key"));
}
 
Example #22
Source File: DataBinderTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testSpecificEditorForNestedIndexedField() {
	IndexedTestBean tb = new IndexedTestBean();
	tb.getArray()[0].setNestedIndexedBean(new IndexedTestBean());
	tb.getArray()[1].setNestedIndexedBean(new IndexedTestBean());
	DataBinder binder = new DataBinder(tb, "tb");
	binder.registerCustomEditor(String.class, "array[0].nestedIndexedBean.list.name", new PropertyEditorSupport() {
		@Override
		public void setAsText(String text) throws IllegalArgumentException {
			setValue("list" + text);
		}
		@Override
		public String getAsText() {
			return ((String) getValue()).substring(4);
		}
	});
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("array[0].nestedIndexedBean.list[0].name", "test1");
	pvs.add("array[1].nestedIndexedBean.list[1].name", "test2");
	binder.bind(pvs);
	assertEquals("listtest1", ((TestBean)tb.getArray()[0].getNestedIndexedBean().getList().get(0)).getName());
	assertEquals("test2", ((TestBean)tb.getArray()[1].getNestedIndexedBean().getList().get(1)).getName());
	assertEquals("test1", binder.getBindingResult().getFieldValue("array[0].nestedIndexedBean.list[0].name"));
	assertEquals("test2", binder.getBindingResult().getFieldValue("array[1].nestedIndexedBean.list[1].name"));
}
 
Example #23
Source File: AbstractPropertyAccessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void setStringPropertyWithCustomEditor() throws Exception {
	TestBean target = new TestBean();
	AbstractPropertyAccessor accessor = createAccessor(target);
	accessor.registerCustomEditor(String.class, "name", new PropertyEditorSupport() {
		@Override
		public void setValue(Object value) {
			if (value instanceof String[]) {
				setValue(StringUtils.arrayToDelimitedString(((String[]) value), "-"));
			}
			else {
				super.setValue(value != null ? value : "");
			}
		}
	});
	accessor.setPropertyValue("name", new String[] {});
	assertEquals("", target.getName());
	accessor.setPropertyValue("name", new String[] {"a1", "b2"});
	assertEquals("a1-b2", target.getName());
	accessor.setPropertyValue("name", null);
	assertEquals("", target.getName());
}
 
Example #24
Source File: DataBinderTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testInnerSpecificEditorForNestedIndexedField() {
	IndexedTestBean tb = new IndexedTestBean();
	tb.getArray()[0].setNestedIndexedBean(new IndexedTestBean());
	tb.getArray()[1].setNestedIndexedBean(new IndexedTestBean());
	DataBinder binder = new DataBinder(tb, "tb");
	binder.registerCustomEditor(String.class, "array.nestedIndexedBean.list[0].name", new PropertyEditorSupport() {
		@Override
		public void setAsText(String text) throws IllegalArgumentException {
			setValue("list" + text);
		}
		@Override
		public String getAsText() {
			return ((String) getValue()).substring(4);
		}
	});
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("array[0].nestedIndexedBean.list[0].name", "test1");
	pvs.add("array[1].nestedIndexedBean.list[1].name", "test2");
	binder.bind(pvs);
	assertEquals("listtest1", ((TestBean)tb.getArray()[0].getNestedIndexedBean().getList().get(0)).getName());
	assertEquals("test2", ((TestBean)tb.getArray()[1].getNestedIndexedBean().getList().get(1)).getName());
	assertEquals("test1", binder.getBindingResult().getFieldValue("array[0].nestedIndexedBean.list[0].name"));
	assertEquals("test2", binder.getBindingResult().getFieldValue("array[1].nestedIndexedBean.list[1].name"));
}
 
Example #25
Source File: DataBinderTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testBindToStringArrayWithComponentEditor() {
	TestBean tb = new TestBean();
	DataBinder binder = new DataBinder(tb, "tb");
	binder.registerCustomEditor(String.class, "stringArray", new PropertyEditorSupport() {
		@Override
		public void setAsText(String text) throws IllegalArgumentException {
			setValue("X" + text);
		}
	});
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("stringArray", new String[] {"a1", "b2"});
	binder.bind(pvs);
	assertTrue(!binder.getBindingResult().hasErrors());
	assertEquals(2, tb.getStringArray().length);
	assertEquals("Xa1", tb.getStringArray()[0]);
	assertEquals("Xb2", tb.getStringArray()[1]);
}
 
Example #26
Source File: AbstractPropertyAccessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void setPrimitiveArrayPropertyLargeMatchingWithSpecificEditor() {
	PrimitiveArrayBean target = new PrimitiveArrayBean();
	AbstractPropertyAccessor accessor = createAccessor(target);
	accessor.registerCustomEditor(int.class, "array", new PropertyEditorSupport() {
		@Override
		public void setValue(Object value) {
			if (value instanceof Integer) {
				super.setValue(new Integer((Integer) value + 1));
			}
		}
	});
	int[] input = new int[1024];
	accessor.setPropertyValue("array", input);
	assertEquals(1024, target.getArray().length);
	assertEquals(1, target.getArray()[0]);
	assertEquals(1, target.getArray()[1]);
}
 
Example #27
Source File: AbstractPropertyAccessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void setPrimitiveArrayPropertyLargeMatchingWithIndexSpecificEditor() {
	PrimitiveArrayBean target = new PrimitiveArrayBean();
	AbstractPropertyAccessor accessor = createAccessor(target);
	accessor.registerCustomEditor(int.class, "array[1]", new PropertyEditorSupport() {
		@Override
		public void setValue(Object value) {
			if (value instanceof Integer) {
				super.setValue(new Integer((Integer) value + 1));
			}
		}
	});
	int[] input = new int[1024];
	accessor.setPropertyValue("array", input);
	assertEquals(1024, target.getArray().length);
	assertEquals(0, target.getArray()[0]);
	assertEquals(1, target.getArray()[1]);
}
 
Example #28
Source File: DataBinderTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testBindToStringArrayWithArrayEditor() {
	TestBean tb = new TestBean();
	DataBinder binder = new DataBinder(tb, "tb");
	binder.registerCustomEditor(String[].class, "stringArray", new PropertyEditorSupport() {
		@Override
		public void setAsText(String text) throws IllegalArgumentException {
			setValue(StringUtils.delimitedListToStringArray(text, "-"));
		}
	});
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("stringArray", "a1-b2");
	binder.bind(pvs);
	assertTrue(!binder.getBindingResult().hasErrors());
	assertEquals(2, tb.getStringArray().length);
	assertEquals("a1", tb.getStringArray()[0]);
	assertEquals("b2", tb.getStringArray()[1]);
}
 
Example #29
Source File: AbstractPropertyAccessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void setMapPropertyWithUnmodifiableMap() {
	IndexedTestBean target = new IndexedTestBean();
	AbstractPropertyAccessor accessor = createAccessor(target);
	accessor.registerCustomEditor(TestBean.class, "map", new PropertyEditorSupport() {
		@Override
		public void setAsText(String text) throws IllegalArgumentException {
			if (!StringUtils.hasLength(text)) {
				throw new IllegalArgumentException();
			}
			setValue(new TestBean(text));
		}
	});

	Map<Integer, String> inputMap = new HashMap<>();
	inputMap.put(1, "rod");
	inputMap.put(2, "rob");
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("map", Collections.unmodifiableMap(inputMap));
	accessor.setPropertyValues(pvs);
	assertEquals("rod", ((TestBean) target.getMap().get(1)).getName());
	assertEquals("rob", ((TestBean) target.getMap().get(2)).getName());
}
 
Example #30
Source File: AbstractPropertyAccessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void setMapPropertyWithCustomUnmodifiableMap() {
	IndexedTestBean target = new IndexedTestBean();
	AbstractPropertyAccessor accessor = createAccessor(target);
	accessor.registerCustomEditor(TestBean.class, "map", new PropertyEditorSupport() {
		@Override
		public void setAsText(String text) throws IllegalArgumentException {
			if (!StringUtils.hasLength(text)) {
				throw new IllegalArgumentException();
			}
			setValue(new TestBean(text));
		}
	});

	Map<Object, Object> inputMap = new HashMap<>();
	inputMap.put(1, "rod");
	inputMap.put(2, "rob");
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("map", new ReadOnlyMap<>(inputMap));
	accessor.setPropertyValues(pvs);
	assertEquals("rod", ((TestBean) target.getMap().get(1)).getName());
	assertEquals("rob", ((TestBean) target.getMap().get(2)).getName());
}