Java Code Examples for org.springframework.beans.BeanWrapper#registerCustomEditor()
The following examples show how to use
org.springframework.beans.BeanWrapper#registerCustomEditor() .
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 java-technology-stack with MIT License | 6 votes |
@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")); }
Example 2
Source File: CustomEditorTests.java From java-technology-stack with MIT License | 6 votes |
@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); }
Example 3
Source File: CustomEditorTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@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 4
Source File: CustomEditorTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@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); }
Example 5
Source File: CustomEditorTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@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 6
Source File: CustomEditorTests.java From spring-analysis-note with MIT License | 6 votes |
@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()); }
Example 7
Source File: CustomEditorTests.java From java-technology-stack with MIT License | 6 votes |
@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 8
Source File: CustomEditorTests.java From java-technology-stack with MIT License | 6 votes |
@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 9
Source File: CustomEditorTests.java From spring-analysis-note with MIT License | 6 votes |
@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()); }
Example 10
Source File: GenericPortletBean.java From spring4-understanding with Apache License 2.0 | 5 votes |
/** * 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"); } }
Example 11
Source File: CustomEditorTests.java From java-technology-stack with MIT License | 5 votes |
@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); }
Example 12
Source File: CustomEditorTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@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")); }
Example 13
Source File: CustomEditorTests.java From java-technology-stack with MIT License | 5 votes |
@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); } }
Example 14
Source File: CustomEditorTests.java From java-technology-stack with MIT License | 5 votes |
@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")); }
Example 15
Source File: CustomEditorTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@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); } }
Example 16
Source File: CustomEditorTests.java From java-technology-stack with MIT License | 5 votes |
@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()); }
Example 17
Source File: CustomEditorTests.java From spring-analysis-note with MIT License | 5 votes |
@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")); }
Example 18
Source File: CustomEditorTests.java From spring-analysis-note with MIT License | 5 votes |
@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()); }
Example 19
Source File: CustomEditorTests.java From spring-analysis-note with MIT License | 5 votes |
@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()); }
Example 20
Source File: CustomEditorTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@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); }