List of usage examples for org.springframework.beans BeanWrapper setPropertyValue
void setPropertyValue(String propertyName, @Nullable Object value) throws BeansException;
From source file:org.springframework.beans.BeanWrapperTests.java
@Test public void testStringArrayPropertyWithCustomStringDelimiter() throws Exception { PropsTester pt = new PropsTester(); BeanWrapper bw = new BeanWrapperImpl(pt); bw.registerCustomEditor(String[].class, "stringArray", new StringArrayPropertyEditor("-")); bw.setPropertyValue("stringArray", "a1-b2"); assertTrue("stringArray length = 2", pt.stringArray.length == 2); assertTrue("correct values", pt.stringArray[0].equals("a1") && pt.stringArray[1].equals("b2")); }
From source file:org.springframework.beans.BeanWrapperTests.java
@Test public void testStringPropertyWithCustomEditor() throws Exception { TestBean tb = new TestBean(); BeanWrapper bw = new BeanWrapperImpl(tb); bw.registerCustomEditor(String.class, "name", new PropertyEditorSupport() { @Override//ww w . j ava 2s . c o m public void setValue(Object value) { if (value instanceof String[]) { setValue(StringUtils.arrayToDelimitedString(((String[]) value), "-")); } else { super.setValue(value != null ? value : ""); } } }); bw.setPropertyValue("name", new String[] {}); assertEquals("", tb.getName()); bw.setPropertyValue("name", new String[] { "a1", "b2" }); assertEquals("a1-b2", tb.getName()); bw.setPropertyValue("name", null); assertEquals("", tb.getName()); }
From source file:org.springframework.beans.BeanWrapperTests.java
@Test public void testIntArrayProperty() { PropsTester pt = new PropsTester(); BeanWrapper bw = new BeanWrapperImpl(pt); bw.setPropertyValue("intArray", new int[] { 4, 5, 2, 3 }); assertTrue("intArray length = 4", pt.intArray.length == 4); assertTrue("correct values", pt.intArray[0] == 4 && pt.intArray[1] == 5 && pt.intArray[2] == 2 && pt.intArray[3] == 3); bw.setPropertyValue("intArray", new String[] { "4", "5", "2", "3" }); assertTrue("intArray length = 4", pt.intArray.length == 4); assertTrue("correct values", pt.intArray[0] == 4 && pt.intArray[1] == 5 && pt.intArray[2] == 2 && pt.intArray[3] == 3); List<Object> list = new ArrayList<Object>(); list.add(new Integer(4)); list.add("5"); list.add(new Integer(2)); list.add("3"); bw.setPropertyValue("intArray", list); assertTrue("intArray length = 4", pt.intArray.length == 4); assertTrue("correct values", pt.intArray[0] == 4 && pt.intArray[1] == 5 && pt.intArray[2] == 2 && pt.intArray[3] == 3); Set<Object> set = new HashSet<Object>(); set.add("4"); set.add(new Integer(5)); set.add("3"); bw.setPropertyValue("intArray", set); assertTrue("intArray length = 3", pt.intArray.length == 3); List<Integer> result = new ArrayList<Integer>(); result.add(new Integer(pt.intArray[0])); result.add(new Integer(pt.intArray[1])); result.add(new Integer(pt.intArray[2])); assertTrue("correct values", result.contains(new Integer(4)) && result.contains(new Integer(5)) && result.contains(new Integer(3))); bw.setPropertyValue("intArray", new Integer[] { new Integer(1) }); assertTrue("intArray length = 4", pt.intArray.length == 1); assertTrue("correct values", pt.intArray[0] == 1); bw.setPropertyValue("intArray", new Integer(1)); assertTrue("intArray length = 4", pt.intArray.length == 1); assertTrue("correct values", pt.intArray[0] == 1); bw.setPropertyValue("intArray", new String[] { "1" }); assertTrue("intArray length = 4", pt.intArray.length == 1); assertTrue("correct values", pt.intArray[0] == 1); bw.setPropertyValue("intArray", "1"); assertTrue("intArray length = 4", pt.intArray.length == 1); assertTrue("correct values", pt.intArray[0] == 1); }
From source file:org.springframework.beans.BeanWrapperTests.java
@Test public void testIntArrayPropertyWithCustomEditor() { PropsTester pt = new PropsTester(); BeanWrapper bw = new BeanWrapperImpl(pt); bw.registerCustomEditor(int.class, new PropertyEditorSupport() { @Override//from w w w .ja v a2 s. co m public void setAsText(String text) { setValue(new Integer(Integer.parseInt(text) + 1)); } }); bw.setPropertyValue("intArray", new int[] { 4, 5, 2, 3 }); assertTrue("intArray length = 4", pt.intArray.length == 4); assertTrue("correct values", pt.intArray[0] == 4 && pt.intArray[1] == 5 && pt.intArray[2] == 2 && pt.intArray[3] == 3); bw.setPropertyValue("intArray", new String[] { "3", "4", "1", "2" }); assertTrue("intArray length = 4", pt.intArray.length == 4); assertTrue("correct values", pt.intArray[0] == 4 && pt.intArray[1] == 5 && pt.intArray[2] == 2 && pt.intArray[3] == 3); bw.setPropertyValue("intArray", new Integer(1)); assertTrue("intArray length = 4", pt.intArray.length == 1); assertTrue("correct values", pt.intArray[0] == 1); bw.setPropertyValue("intArray", new String[] { "0" }); assertTrue("intArray length = 4", pt.intArray.length == 1); assertTrue("correct values", pt.intArray[0] == 1); bw.setPropertyValue("intArray", "0"); assertTrue("intArray length = 4", pt.intArray.length == 1); assertTrue("correct values", pt.intArray[0] == 1); }
From source file:org.springframework.beans.BeanWrapperTests.java
@Test public void testIndividualAllValid() { TestBean t = new TestBean(); String newName = "tony"; int newAge = 65; String newTouchy = "valid"; try {/* ww w .j a v a 2 s.c o m*/ BeanWrapper bw = new BeanWrapperImpl(t); bw.setPropertyValue("age", new Integer(newAge)); bw.setPropertyValue(new PropertyValue("name", newName)); bw.setPropertyValue(new PropertyValue("touchy", newTouchy)); assertTrue("Validly set property must stick", t.getName().equals(newName)); assertTrue("Validly set property must stick", t.getTouchy().equals(newTouchy)); assertTrue("Validly set property must stick", t.getAge() == newAge); } catch (BeansException ex) { fail("Shouldn't throw exception when everything is valid"); } }
From source file:org.springframework.beans.BeanWrapperTests.java
@Test public void testPossibleMatches() { TestBean tb = new TestBean(); try {/*from ww w . j a v a 2s . c o m*/ BeanWrapper bw = new BeanWrapperImpl(tb); bw.setPropertyValue("ag", "foobar"); fail("Should throw exception on invalid property"); } catch (NotWritablePropertyException ex) { // expected assertEquals(1, ex.getPossibleMatches().length); assertEquals("age", ex.getPossibleMatches()[0]); } }
From source file:org.springframework.beans.BeanWrapperTests.java
@Test public void testTypeMismatch() { TestBean t = new TestBean(); try {/* ww w. j a v a 2 s .co m*/ BeanWrapper bw = new BeanWrapperImpl(t); bw.setPropertyValue("age", "foobar"); fail("Should throw exception on type mismatch"); } catch (TypeMismatchException ex) { // expected } }
From source file:org.springframework.beans.BeanWrapperTests.java
@Test public void testEmptyValueForPrimitiveProperty() { TestBean t = new TestBean(); try {//from www. j a v a2 s . c om BeanWrapper bw = new BeanWrapperImpl(t); bw.setPropertyValue("age", ""); fail("Should throw exception on type mismatch"); } catch (TypeMismatchException ex) { // expected } catch (Exception ex) { fail("Shouldn't throw exception other than Type mismatch"); } }
From source file:org.springframework.beans.BeanWrapperTests.java
@Test public void testSetNestedProperty() throws Exception { ITestBean rod = new TestBean("rod", 31); ITestBean kerry = new TestBean("kerry", 0); BeanWrapper bw = new BeanWrapperImpl(rod); bw.setPropertyValue("spouse", kerry); assertTrue("nested set worked", rod.getSpouse() == kerry); assertTrue("no back relation", kerry.getSpouse() == null); bw.setPropertyValue(new PropertyValue("spouse.spouse", rod)); assertTrue("nested set worked", kerry.getSpouse() == rod); assertTrue("kerry age not set", kerry.getAge() == 0); bw.setPropertyValue(new PropertyValue("spouse.age", new Integer(35))); assertTrue("Set primitive on spouse", kerry.getAge() == 35); assertEquals(kerry, bw.getPropertyValue("spouse")); assertEquals(rod, bw.getPropertyValue("spouse.spouse")); }
From source file:org.springframework.beans.BeanWrapperTests.java
@Test public void testSetNestedPropertyNullValue() throws Exception { ITestBean rod = new TestBean("rod", 31); BeanWrapper bw = new BeanWrapperImpl(rod); try {/*www . j a va 2 s . com*/ bw.setPropertyValue("spouse.age", new Integer(31)); fail("Shouldn't have succeeded with null path"); } catch (NullValueInNestedPathException ex) { // expected assertTrue("it was the spouse property that was null, not " + ex.getPropertyName(), ex.getPropertyName().equals("spouse")); } }