@Test public void testGenericMapElementWithCollectionValue() { GenericBean<?> gb = new GenericBean<>(); gb.setCollectionMap(new HashMap<>()); BeanWrapper bw = new BeanWrapperImpl(gb); bw.registerCustomEditor(Number.class, new CustomNumberEditor(Integer.class, false)); HashSet<Integer> value1 = new HashSet<>(); value1.add(new Integer(1)); bw.setPropertyValue("collectionMap[1]", value1); assertTrue(gb.getCollectionMap().get(new Integer(1)) instanceof HashSet); }
@Test public void testGenericMapWithCollectionValue() { GenericBean<?> gb = new GenericBean<>(); BeanWrapper bw = new BeanWrapperImpl(gb); bw.registerCustomEditor(Number.class, new CustomNumberEditor(Integer.class, false)); Map<String, Collection> input = new HashMap<>(); HashSet<Integer> value1 = new HashSet<>(); value1.add(new Integer(1)); input.put("1", value1); ArrayList<Boolean> value2 = new ArrayList<>(); value2.add(Boolean.TRUE); input.put("2", value2); bw.setPropertyValue("collectionMap", input); assertTrue(gb.getCollectionMap().get(new Integer(1)) instanceof HashSet); assertTrue(gb.getCollectionMap().get(new Integer(2)) instanceof ArrayList); }
@Test public void testGenericLowerBoundedSet() { GenericBean<?> gb = new GenericBean<>(); BeanWrapper bw = new BeanWrapperImpl(gb); bw.registerCustomEditor(Number.class, new CustomNumberEditor(Integer.class, true)); Set<String> input = new HashSet<>(); input.add("4"); input.add("5"); bw.setPropertyValue("numberSet", input); assertTrue(gb.getNumberSet().contains(new Integer(4))); assertTrue(gb.getNumberSet().contains(new Integer(5))); }
@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")); }
@Test public void testGenericListOfArraysWithElementConversion() throws MalformedURLException { GenericBean<String> gb = new GenericBean<>(); ArrayList<String[]> list = new ArrayList<>(); list.add(new String[] {"str1", "str2"}); gb.setListOfArrays(list); BeanWrapper bw = new BeanWrapperImpl(gb); bw.registerCustomEditor(String.class, new StringTrimmerEditor(false)); bw.setPropertyValue("listOfArrays[0][1]", "str3 "); assertEquals("str3", bw.getPropertyValue("listOfArrays[0][1]")); assertEquals("str3", gb.getListOfArrays().get(0)[1]); }
@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()); }
@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); }
@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)); }
/** * Map config parameters onto bean properties of this servlet, and * invoke subclass initialization. * @throws ServletException if bean properties are invalid (or required * properties are missing), or if subclass initialization fails. */ @Override public final void init() throws ServletException { // Set bean properties from init parameters. PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties); if (!pvs.isEmpty()) { try { BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this); ResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext()); bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, getEnvironment())); initBeanWrapper(bw); bw.setPropertyValues(pvs, true); } catch (BeansException ex) { if (logger.isErrorEnabled()) { logger.error("Failed to set bean properties on servlet '" + getServletName() + "'", ex); } throw ex; } } // Let subclasses do whatever initialization they like. initServletBean(); }
@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); }
@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()); }
@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); } }
@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()); }
@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")); }
@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()); }
@Test public void testCustomBooleanEditorWithAllowEmpty() { BooleanTestBean tb = new BooleanTestBean(); BeanWrapper bw = new BeanWrapperImpl(tb); bw.registerCustomEditor(Boolean.class, new CustomBooleanEditor(true)); bw.setPropertyValue("bool2", "true"); assertTrue("Correct bool2 value", Boolean.TRUE.equals(bw.getPropertyValue("bool2"))); assertTrue("Correct bool2 value", tb.getBool2().booleanValue()); bw.setPropertyValue("bool2", "false"); assertTrue("Correct bool2 value", Boolean.FALSE.equals(bw.getPropertyValue("bool2"))); assertTrue("Correct bool2 value", !tb.getBool2().booleanValue()); bw.setPropertyValue("bool2", "on"); assertTrue("Correct bool2 value", tb.getBool2().booleanValue()); bw.setPropertyValue("bool2", "off"); assertTrue("Correct bool2 value", !tb.getBool2().booleanValue()); bw.setPropertyValue("bool2", "yes"); assertTrue("Correct bool2 value", tb.getBool2().booleanValue()); bw.setPropertyValue("bool2", "no"); assertTrue("Correct bool2 value", !tb.getBool2().booleanValue()); bw.setPropertyValue("bool2", "1"); assertTrue("Correct bool2 value", tb.getBool2().booleanValue()); bw.setPropertyValue("bool2", "0"); assertTrue("Correct bool2 value", !tb.getBool2().booleanValue()); bw.setPropertyValue("bool2", ""); assertTrue("Correct bool2 value", bw.getPropertyValue("bool2") == null); assertTrue("Correct bool2 value", tb.getBool2() == null); }
@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()); }
@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()); }
@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()); }
@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()); }