类java.beans.PropertyEditorSupport源码实例Demo

下面列出了怎么用java.beans.PropertyEditorSupport的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: spring-analysis-note   文件: DataBinderTests.java
@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());
}
 
源代码2 项目: spring-analysis-note   文件: DataBinderTests.java
@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());
}
 
源代码3 项目: spring-analysis-note   文件: DataBinderTests.java
@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"));
}
 
源代码4 项目: spring-analysis-note   文件: DataBinderTests.java
@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"));
}
 
源代码5 项目: spring-analysis-note   文件: DataBinderTests.java
@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"));
}
 
源代码6 项目: spring-analysis-note   文件: DataBinderTests.java
@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]);
}
 
源代码7 项目: spring-analysis-note   文件: DataBinderTests.java
@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]);
}
 
源代码8 项目: spring-analysis-note   文件: SelectTagTests.java
@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"));
}
 
源代码9 项目: spring-analysis-note   文件: SelectTagTests.java
@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"));
}
 
@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]);
}
 
@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());
}
 
@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());
}
 
@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());
}
 
源代码14 项目: spring-analysis-note   文件: CustomEditorTests.java
@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());
}
 
源代码15 项目: spring-analysis-note   文件: CustomEditorTests.java
@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());
}
 
源代码16 项目: spring-analysis-note   文件: CustomEditorTests.java
@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());
}
 
源代码17 项目: spring-analysis-note   文件: CustomEditorTests.java
@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());
}
 
源代码18 项目: spring-analysis-note   文件: CustomEditorTests.java
@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));
}
 
@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());
}
 
@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]);
}
 
@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]);
}
 
@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());
}
 
@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());
}
 
源代码24 项目: java-technology-stack   文件: DataBinderTests.java
@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());
}
 
源代码25 项目: java-technology-stack   文件: DataBinderTests.java
@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());
}
 
源代码26 项目: java-technology-stack   文件: DataBinderTests.java
@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"));
}
 
源代码27 项目: java-technology-stack   文件: DataBinderTests.java
@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"));
}
 
源代码28 项目: java-technology-stack   文件: DataBinderTests.java
@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"));
}
 
源代码29 项目: java-technology-stack   文件: DataBinderTests.java
@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]);
}
 
源代码30 项目: java-technology-stack   文件: DataBinderTests.java
@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]);
}
 
 类所在包
 同包方法