org.springframework.beans.BeanWrapper#registerCustomEditor ( )源码实例Demo

下面列出了org.springframework.beans.BeanWrapper#registerCustomEditor ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: java-technology-stack   文件: CustomEditorTests.java
@Test
public void testConversionToOldCollections() throws PropertyVetoException {
	OldCollectionsBean tb = new OldCollectionsBean();
	BeanWrapper bw = new BeanWrapperImpl(tb);
	bw.registerCustomEditor(Vector.class, new CustomCollectionEditor(Vector.class));
	bw.registerCustomEditor(Hashtable.class, new CustomMapEditor(Hashtable.class));

	bw.setPropertyValue("vector", new String[] {"a", "b"});
	assertEquals(2, tb.getVector().size());
	assertEquals("a", tb.getVector().get(0));
	assertEquals("b", tb.getVector().get(1));

	bw.setPropertyValue("hashtable", Collections.singletonMap("foo", "bar"));
	assertEquals(1, tb.getHashtable().size());
	assertEquals("bar", tb.getHashtable().get("foo"));
}
 
源代码2 项目: spring-analysis-note   文件: CustomEditorTests.java
@Test
public void testComplexObjectWithOldValueAccess() {
	TestBean tb = new TestBean();
	String newName = "Rod";
	String tbString = "Kerry_34";

	BeanWrapper bw = new BeanWrapperImpl(tb);
	bw.setExtractOldValueForEditor(true);
	bw.registerCustomEditor(ITestBean.class, new OldValueAccessingTestBeanEditor());
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.addPropertyValue(new PropertyValue("age", new Integer(55)));
	pvs.addPropertyValue(new PropertyValue("name", newName));
	pvs.addPropertyValue(new PropertyValue("touchy", "valid"));
	pvs.addPropertyValue(new PropertyValue("spouse", tbString));

	bw.setPropertyValues(pvs);
	assertTrue("spouse is non-null", tb.getSpouse() != null);
	assertTrue("spouse name is Kerry and age is 34",
			tb.getSpouse().getName().equals("Kerry") && tb.getSpouse().getAge() == 34);
	ITestBean spouse = tb.getSpouse();

	bw.setPropertyValues(pvs);
	assertSame("Should have remained same object", spouse, tb.getSpouse());
}
 
源代码3 项目: java-technology-stack   文件: 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());
}
 
源代码4 项目: java-technology-stack   文件: 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));
}
 
源代码5 项目: spring-analysis-note   文件: CustomEditorTests.java
@Test
public void testCharacterEditorWithAllowEmpty() {
	CharBean cb = new CharBean();
	BeanWrapper bw = new BeanWrapperImpl(cb);
	bw.registerCustomEditor(Character.class, new CharacterEditor(true));

	bw.setPropertyValue("myCharacter", new Character('c'));
	assertEquals(new Character('c'), cb.getMyCharacter());

	bw.setPropertyValue("myCharacter", "c");
	assertEquals(new Character('c'), cb.getMyCharacter());

	bw.setPropertyValue("myCharacter", "\u0041");
	assertEquals(new Character('A'), cb.getMyCharacter());

	bw.setPropertyValue("myCharacter", " ");
	assertEquals(new Character(' '), cb.getMyCharacter());

	bw.setPropertyValue("myCharacter", "");
	assertNull(cb.getMyCharacter());
}
 
源代码6 项目: spring4-understanding   文件: 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());
}
 
源代码7 项目: spring4-understanding   文件: CustomEditorTests.java
@Test
public void testComplexObject() {
	TestBean tb = new TestBean();
	String newName = "Rod";
	String tbString = "Kerry_34";

	BeanWrapper bw = new BeanWrapperImpl(tb);
	bw.registerCustomEditor(ITestBean.class, new TestBeanEditor());
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.addPropertyValue(new PropertyValue("age", new Integer(55)));
	pvs.addPropertyValue(new PropertyValue("name", newName));
	pvs.addPropertyValue(new PropertyValue("touchy", "valid"));
	pvs.addPropertyValue(new PropertyValue("spouse", tbString));
	bw.setPropertyValues(pvs);
	assertTrue("spouse is non-null", tb.getSpouse() != null);
	assertTrue("spouse name is Kerry and age is 34",
			tb.getSpouse().getName().equals("Kerry") && tb.getSpouse().getAge() == 34);
}
 
源代码8 项目: spring4-understanding   文件: 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());
}
 
源代码9 项目: java-technology-stack   文件: CustomEditorTests.java
@Test
public void testComplexObject() {
	TestBean tb = new TestBean();
	String newName = "Rod";
	String tbString = "Kerry_34";

	BeanWrapper bw = new BeanWrapperImpl(tb);
	bw.registerCustomEditor(ITestBean.class, new TestBeanEditor());
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.addPropertyValue(new PropertyValue("age", new Integer(55)));
	pvs.addPropertyValue(new PropertyValue("name", newName));
	pvs.addPropertyValue(new PropertyValue("touchy", "valid"));
	pvs.addPropertyValue(new PropertyValue("spouse", tbString));
	bw.setPropertyValues(pvs);
	assertTrue("spouse is non-null", tb.getSpouse() != null);
	assertTrue("spouse name is Kerry and age is 34",
			tb.getSpouse().getName().equals("Kerry") && tb.getSpouse().getAge() == 34);
}
 
源代码10 项目: spring4-understanding   文件: CustomEditorTests.java
@Test
public void testCustomNumberEditorWithFrenchBigDecimal() throws Exception {
	NumberFormat nf = NumberFormat.getNumberInstance(Locale.FRENCH);
	NumberTestBean tb = new NumberTestBean();
	BeanWrapper bw = new BeanWrapperImpl(tb);
	bw.registerCustomEditor(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, nf, true));
	bw.setPropertyValue("bigDecimal", "1000");
	assertEquals(1000.0f, tb.getBigDecimal().floatValue(), 0f);
	bw.setPropertyValue("bigDecimal", "1000,5");
	assertEquals(1000.5f, tb.getBigDecimal().floatValue(), 0f);
	bw.setPropertyValue("bigDecimal", "1 000,5");
	assertEquals(1000.5f, tb.getBigDecimal().floatValue(), 0f);
}
 
源代码11 项目: java-technology-stack   文件: CustomEditorTests.java
@Test
public void testArrayToStringConversion() throws PropertyVetoException {
	TestBean tb = new TestBean();
	BeanWrapper bw = new BeanWrapperImpl(tb);
	bw.registerCustomEditor(String.class, new PropertyEditorSupport() {
		@Override
		public void setAsText(String text) throws IllegalArgumentException {
			setValue("-" + text + "-");
		}
	});
	bw.setPropertyValue("name", new String[] {"a", "b"});
	assertEquals("-a,b-", tb.getName());
}
 
源代码12 项目: java-technology-stack   文件: CustomEditorTests.java
@Test
public void testUninitializedArrayPropertyWithCustomEditor() {
	IndexedTestBean bean = new IndexedTestBean(false);
	BeanWrapper bw = new BeanWrapperImpl(bean);
	PropertyEditor pe = new CustomNumberEditor(Integer.class, true);
	bw.registerCustomEditor(null, "list.age", pe);
	TestBean tb = new TestBean();
	bw.setPropertyValue("list", new ArrayList<>());
	bw.setPropertyValue("list[0]", tb);
	assertEquals(tb, bean.getList().get(0));
	assertEquals(pe, bw.findCustomEditor(int.class, "list.age"));
	assertEquals(pe, bw.findCustomEditor(null, "list.age"));
	assertEquals(pe, bw.findCustomEditor(int.class, "list[0].age"));
	assertEquals(pe, bw.findCustomEditor(null, "list[0].age"));
}
 
源代码13 项目: spring4-understanding   文件: CustomEditorTests.java
@Test
public void testUninitializedArrayPropertyWithCustomEditor() {
	IndexedTestBean bean = new IndexedTestBean(false);
	BeanWrapper bw = new BeanWrapperImpl(bean);
	PropertyEditor pe = new CustomNumberEditor(Integer.class, true);
	bw.registerCustomEditor(null, "list.age", pe);
	TestBean tb = new TestBean();
	bw.setPropertyValue("list", new ArrayList<Object>());
	bw.setPropertyValue("list[0]", tb);
	assertEquals(tb, bean.getList().get(0));
	assertEquals(pe, bw.findCustomEditor(int.class, "list.age"));
	assertEquals(pe, bw.findCustomEditor(null, "list.age"));
	assertEquals(pe, bw.findCustomEditor(int.class, "list[0].age"));
	assertEquals(pe, bw.findCustomEditor(null, "list[0].age"));
}
 
源代码14 项目: spring4-understanding   文件: GenericPortletBean.java
/**
 * Map config parameters onto bean properties of this portlet, and
 * invoke subclass initialization.
 * @throws PortletException if bean properties are invalid (or required
 * properties are missing), or if subclass initialization fails.
 */
@Override
public final void init() throws PortletException {
	if (logger.isInfoEnabled()) {
		logger.info("Initializing portlet '" + getPortletName() + "'");
	}

	// Set bean properties from init parameters.
	try {
		PropertyValues pvs = new PortletConfigPropertyValues(getPortletConfig(), this.requiredProperties);
		BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
		ResourceLoader resourceLoader = new PortletContextResourceLoader(getPortletContext());
		bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, getEnvironment()));
		initBeanWrapper(bw);
		bw.setPropertyValues(pvs, true);
	}
	catch (BeansException ex) {
		logger.error("Failed to set bean properties on portlet '" + getPortletName() + "'", ex);
		throw ex;
	}

	// let subclasses do whatever initialization they like
	initPortletBean();

	if (logger.isInfoEnabled()) {
		logger.info("Portlet '" + getPortletName() + "' configured successfully");
	}
}
 
源代码15 项目: java-technology-stack   文件: CustomEditorTests.java
@Test
public void testCustomNumberEditorWithFrenchBigDecimal() throws Exception {
	NumberFormat nf = NumberFormat.getNumberInstance(Locale.FRENCH);
	NumberTestBean tb = new NumberTestBean();
	BeanWrapper bw = new BeanWrapperImpl(tb);
	bw.registerCustomEditor(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, nf, true));
	bw.setPropertyValue("bigDecimal", "1000");
	assertEquals(1000.0f, tb.getBigDecimal().floatValue(), 0f);
	bw.setPropertyValue("bigDecimal", "1000,5");
	assertEquals(1000.5f, tb.getBigDecimal().floatValue(), 0f);
	bw.setPropertyValue("bigDecimal", "1 000,5");
	assertEquals(1000.5f, tb.getBigDecimal().floatValue(), 0f);
}
 
源代码16 项目: java-technology-stack   文件: CustomEditorTests.java
@Test
public void testCustomNumberEditorWithAllowEmpty() {
	NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN);
	NumberTestBean tb = new NumberTestBean();
	BeanWrapper bw = new BeanWrapperImpl(tb);
	bw.registerCustomEditor(long.class, new CustomNumberEditor(Long.class, nf, true));
	bw.registerCustomEditor(Long.class, new CustomNumberEditor(Long.class, nf, true));

	bw.setPropertyValue("long1", "5");
	bw.setPropertyValue("long2", "6");
	assertTrue("Correct long1 value", new Long("5").equals(bw.getPropertyValue("long1")));
	assertTrue("Correct long1 value", tb.getLong1() == 5);
	assertTrue("Correct long2 value", new Long("6").equals(bw.getPropertyValue("long2")));
	assertTrue("Correct long2 value", new Long("6").equals(tb.getLong2()));

	bw.setPropertyValue("long2", "");
	assertTrue("Correct long2 value", bw.getPropertyValue("long2") == null);
	assertTrue("Correct long2 value", tb.getLong2() == null);

	try {
		bw.setPropertyValue("long1", "");
		fail("Should have thrown BeansException");
	}
	catch (BeansException ex) {
		// expected
		assertTrue("Correct long1 value", new Long("5").equals(bw.getPropertyValue("long1")));
		assertTrue("Correct long1 value", tb.getLong1() == 5);
	}
}
 
源代码17 项目: spring4-understanding   文件: CustomEditorTests.java
@Test
public void testCustomNumberEditorWithAllowEmpty() {
	NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN);
	NumberTestBean tb = new NumberTestBean();
	BeanWrapper bw = new BeanWrapperImpl(tb);
	bw.registerCustomEditor(long.class, new CustomNumberEditor(Long.class, nf, true));
	bw.registerCustomEditor(Long.class, new CustomNumberEditor(Long.class, nf, true));

	bw.setPropertyValue("long1", "5");
	bw.setPropertyValue("long2", "6");
	assertTrue("Correct long1 value", new Long("5").equals(bw.getPropertyValue("long1")));
	assertTrue("Correct long1 value", tb.getLong1() == 5);
	assertTrue("Correct long2 value", new Long("6").equals(bw.getPropertyValue("long2")));
	assertTrue("Correct long2 value", new Long("6").equals(tb.getLong2()));

	bw.setPropertyValue("long2", "");
	assertTrue("Correct long2 value", bw.getPropertyValue("long2") == null);
	assertTrue("Correct long2 value", tb.getLong2() == null);

	try {
		bw.setPropertyValue("long1", "");
		fail("Should have thrown BeansException");
	}
	catch (BeansException ex) {
		// expected
		assertTrue("Correct long1 value", new Long("5").equals(bw.getPropertyValue("long1")));
		assertTrue("Correct long1 value", tb.getLong1() == 5);
	}
}
 
源代码18 项目: spring-analysis-note   文件: CustomEditorTests.java
@Test
public void testUninitializedArrayPropertyWithCustomEditor() {
	IndexedTestBean bean = new IndexedTestBean(false);
	BeanWrapper bw = new BeanWrapperImpl(bean);
	PropertyEditor pe = new CustomNumberEditor(Integer.class, true);
	bw.registerCustomEditor(null, "list.age", pe);
	TestBean tb = new TestBean();
	bw.setPropertyValue("list", new ArrayList<>());
	bw.setPropertyValue("list[0]", tb);
	assertEquals(tb, bean.getList().get(0));
	assertEquals(pe, bw.findCustomEditor(int.class, "list.age"));
	assertEquals(pe, bw.findCustomEditor(null, "list.age"));
	assertEquals(pe, bw.findCustomEditor(int.class, "list[0].age"));
	assertEquals(pe, bw.findCustomEditor(null, "list[0].age"));
}
 
源代码19 项目: spring-analysis-note   文件: CustomEditorTests.java
@Test
public void testArrayToArrayConversion() throws PropertyVetoException {
	IndexedTestBean tb = new IndexedTestBean();
	BeanWrapper bw = new BeanWrapperImpl(tb);
	bw.registerCustomEditor(TestBean.class, new PropertyEditorSupport() {
		@Override
		public void setAsText(String text) throws IllegalArgumentException {
			setValue(new TestBean(text, 99));
		}
	});
	bw.setPropertyValue("array", new String[] {"a", "b"});
	assertEquals(2, tb.getArray().length);
	assertEquals("a", tb.getArray()[0].getName());
	assertEquals("b", tb.getArray()[1].getName());
}
 
源代码20 项目: spring-analysis-note   文件: CustomEditorTests.java
@Test
public void testArrayToStringConversion() throws PropertyVetoException {
	TestBean tb = new TestBean();
	BeanWrapper bw = new BeanWrapperImpl(tb);
	bw.registerCustomEditor(String.class, new PropertyEditorSupport() {
		@Override
		public void setAsText(String text) throws IllegalArgumentException {
			setValue("-" + text + "-");
		}
	});
	bw.setPropertyValue("name", new String[] {"a", "b"});
	assertEquals("-a,b-", tb.getName());
}