Example usage for org.springframework.beans BeanWrapper setPropertyValue

List of usage examples for org.springframework.beans BeanWrapper setPropertyValue

Introduction

In this page you can find the example usage for org.springframework.beans BeanWrapper setPropertyValue.

Prototype

void setPropertyValue(String propertyName, @Nullable Object value) throws BeansException;

Source Link

Document

Set the specified value as current property value.

Usage

From source file:org.springframework.beans.BeanWrapperTests.java

@Test
public void testTypeDeterminationForIndexedProperty() {
    BeanWrapper bw = new BeanWrapperImpl(IndexedTestBean.class);
    assertEquals(null, bw.getPropertyType("map[key0]"));

    bw = new BeanWrapperImpl(IndexedTestBean.class);
    bw.setPropertyValue("map[key0]", "my String");
    assertEquals(String.class, bw.getPropertyType("map[key0]"));

    bw = new BeanWrapperImpl(IndexedTestBean.class);
    bw.registerCustomEditor(String.class, "map[key0]", new StringTrimmerEditor(false));
    assertEquals(String.class, bw.getPropertyType("map[key0]"));
}

From source file:org.springframework.beans.BeanWrapperTests.java

@Test
public void testGetterThrowsException() {
    GetterBean gb = new GetterBean();
    BeanWrapper bw = new BeanWrapperImpl(gb);
    bw.setPropertyValue("name", "tom");
    assertTrue("Set name to tom", gb.getName().equals("tom"));
}

From source file:org.springframework.beans.BeanWrapperTests.java

@Test
public void testValidNullUpdate() {
    TestBean t = new TestBean();
    t.setName("Frank"); // we need to change it back
    t.setSpouse(t);/*ww w. ja va2  s  .c  om*/
    BeanWrapper bw = new BeanWrapperImpl(t);
    assertTrue("name is not null to start off", t.getName() != null);
    bw.setPropertyValue("name", null);
    assertTrue("name is now null", t.getName() == null);
    // now test with non-string
    assertTrue("spouse is not null to start off", t.getSpouse() != null);
    bw.setPropertyValue("spouse", null);
    assertTrue("spouse is now null", t.getSpouse() == null);
}

From source file:org.springframework.beans.BeanWrapperTests.java

@Test
public void testBooleanObject() {
    BooleanTestBean tb = new BooleanTestBean();
    BeanWrapper bw = new BeanWrapperImpl(tb);

    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());

}

From source file:org.springframework.beans.BeanWrapperTests.java

@Test
public void testNumberObjects() {
    NumberTestBean tb = new NumberTestBean();
    BeanWrapper bw = new BeanWrapperImpl(tb);

    try {//from   w w  w.  j ava 2 s  . c om
        bw.setPropertyValue("short2", "2");
        bw.setPropertyValue("int2", "8");
        bw.setPropertyValue("long2", "6");
        bw.setPropertyValue("bigInteger", "3");
        bw.setPropertyValue("float2", "8.1");
        bw.setPropertyValue("double2", "6.1");
        bw.setPropertyValue("bigDecimal", "4.0");
    } catch (BeansException ex) {
        fail("Should not throw BeansException: " + ex.getMessage());
    }

    assertTrue("Correct short2 value", new Short("2").equals(bw.getPropertyValue("short2")));
    assertTrue("Correct short2 value", new Short("2").equals(tb.getShort2()));
    assertTrue("Correct int2 value", new Integer("8").equals(bw.getPropertyValue("int2")));
    assertTrue("Correct int2 value", new Integer("8").equals(tb.getInt2()));
    assertTrue("Correct long2 value", new Long("6").equals(bw.getPropertyValue("long2")));
    assertTrue("Correct long2 value", new Long("6").equals(tb.getLong2()));
    assertTrue("Correct bigInteger value", new BigInteger("3").equals(bw.getPropertyValue("bigInteger")));
    assertTrue("Correct bigInteger value", new BigInteger("3").equals(tb.getBigInteger()));
    assertTrue("Correct float2 value", new Float("8.1").equals(bw.getPropertyValue("float2")));
    assertTrue("Correct float2 value", new Float("8.1").equals(tb.getFloat2()));
    assertTrue("Correct double2 value", new Double("6.1").equals(bw.getPropertyValue("double2")));
    assertTrue("Correct double2 value", new Double("6.1").equals(tb.getDouble2()));
    assertTrue("Correct bigDecimal value", new BigDecimal("4.0").equals(bw.getPropertyValue("bigDecimal")));
    assertTrue("Correct bigDecimal value", new BigDecimal("4.0").equals(tb.getBigDecimal()));
}

From source file:org.springframework.beans.BeanWrapperTests.java

@Test
public void testNumberCoercion() {
    NumberTestBean tb = new NumberTestBean();
    BeanWrapper bw = new BeanWrapperImpl(tb);

    try {/* w w w .  j a v  a2 s. co m*/
        bw.setPropertyValue("short2", new Integer(2));
        bw.setPropertyValue("int2", new Long(8));
        bw.setPropertyValue("long2", new BigInteger("6"));
        bw.setPropertyValue("bigInteger", new Integer(3));
        bw.setPropertyValue("float2", new Double(8.1));
        bw.setPropertyValue("double2", new BigDecimal(6.1));
        bw.setPropertyValue("bigDecimal", new Float(4.0));
    } catch (BeansException ex) {
        fail("Should not throw BeansException: " + ex.getMessage());
    }

    assertTrue("Correct short2 value", new Short("2").equals(bw.getPropertyValue("short2")));
    assertTrue("Correct short2 value", new Short("2").equals(tb.getShort2()));
    assertTrue("Correct int2 value", new Integer("8").equals(bw.getPropertyValue("int2")));
    assertTrue("Correct int2 value", new Integer("8").equals(tb.getInt2()));
    assertTrue("Correct long2 value", new Long("6").equals(bw.getPropertyValue("long2")));
    assertTrue("Correct long2 value", new Long("6").equals(tb.getLong2()));
    assertTrue("Correct bigInteger value", new BigInteger("3").equals(bw.getPropertyValue("bigInteger")));
    assertTrue("Correct bigInteger value", new BigInteger("3").equals(tb.getBigInteger()));
    assertTrue("Correct float2 value", new Float("8.1").equals(bw.getPropertyValue("float2")));
    assertTrue("Correct float2 value", new Float("8.1").equals(tb.getFloat2()));
    assertTrue("Correct double2 value", new Double("6.1").equals(bw.getPropertyValue("double2")));
    assertTrue("Correct double2 value", new Double("6.1").equals(tb.getDouble2()));
    assertTrue("Correct bigDecimal value", new BigDecimal("4.0").equals(bw.getPropertyValue("bigDecimal")));
    assertTrue("Correct bigDecimal value", new BigDecimal("4.0").equals(tb.getBigDecimal()));
}

From source file:org.springframework.beans.BeanWrapperTests.java

@Test
public void testEnumByFieldName() {
    EnumTester et = new EnumTester();
    BeanWrapper bw = new BeanWrapperImpl(et);

    bw.setPropertyValue("autowire", "BY_NAME");
    assertEquals(Autowire.BY_NAME, et.getAutowire());

    bw.setPropertyValue("autowire", "  BY_TYPE ");
    assertEquals(Autowire.BY_TYPE, et.getAutowire());

    try {/*from   w w  w.jav  a2  s. co  m*/
        bw.setPropertyValue("autowire", "NHERITED");
        fail("Should have thrown TypeMismatchException");
    } catch (TypeMismatchException ex) {
        // expected
    }
}

From source file:org.springframework.beans.BeanWrapperTests.java

@Test
public void testPropertiesProperty() throws Exception {
    PropsTester pt = new PropsTester();
    BeanWrapper bw = new BeanWrapperImpl(pt);
    bw.setPropertyValue("name", "ptest");

    // Note format...
    String ps = "peace=war\nfreedom=slavery";
    bw.setPropertyValue("properties", ps);

    assertTrue("name was set", pt.name.equals("ptest"));
    assertTrue("props non null", pt.props != null);
    String freedomVal = pt.props.getProperty("freedom");
    String peaceVal = pt.props.getProperty("peace");
    assertTrue("peace==war", peaceVal.equals("war"));
    assertTrue("Freedom==slavery", freedomVal.equals("slavery"));
}

From source file:org.springframework.beans.BeanWrapperTests.java

@Test
public void testStringArrayProperty() throws Exception {
    PropsTester pt = new PropsTester();
    BeanWrapper bw = new BeanWrapperImpl(pt);

    bw.setPropertyValue("stringArray", new String[] { "foo", "fi", "fi", "fum" });
    assertTrue("stringArray length = 4", pt.stringArray.length == 4);
    assertTrue("correct values", pt.stringArray[0].equals("foo") && pt.stringArray[1].equals("fi")
            && pt.stringArray[2].equals("fi") && pt.stringArray[3].equals("fum"));

    List<String> list = new ArrayList<String>();
    list.add("foo");
    list.add("fi");
    list.add("fi");
    list.add("fum");
    bw.setPropertyValue("stringArray", list);
    assertTrue("stringArray length = 4", pt.stringArray.length == 4);
    assertTrue("correct values", pt.stringArray[0].equals("foo") && pt.stringArray[1].equals("fi")
            && pt.stringArray[2].equals("fi") && pt.stringArray[3].equals("fum"));

    Set<String> set = new HashSet<String>();
    set.add("foo");
    set.add("fi");
    set.add("fum");
    bw.setPropertyValue("stringArray", set);
    assertTrue("stringArray length = 3", pt.stringArray.length == 3);
    List<String> result = Arrays.asList(pt.stringArray);
    assertTrue("correct values", result.contains("foo") && result.contains("fi") && result.contains("fum"));

    bw.setPropertyValue("stringArray", "one");
    assertTrue("stringArray length = 1", pt.stringArray.length == 1);
    assertTrue("stringArray elt is ok", pt.stringArray[0].equals("one"));

    bw.setPropertyValue("stringArray", null);
    assertTrue("stringArray is null", pt.stringArray == null);
}

From source file:org.springframework.beans.BeanWrapperTests.java

@Test
public void testStringArrayPropertyWithCustomStringEditor() throws Exception {
    PropsTester pt = new PropsTester();
    BeanWrapper bw = new BeanWrapperImpl(pt);
    bw.registerCustomEditor(String.class, "stringArray", new PropertyEditorSupport() {
        @Override/*  w ww  . ja  va2s  .  c om*/
        public void setAsText(String text) {
            setValue(text.substring(1));
        }
    });

    bw.setPropertyValue("stringArray", new String[] { "4foo", "7fi", "6fi", "5fum" });
    assertTrue("stringArray length = 4", pt.stringArray.length == 4);
    assertTrue("correct values", pt.stringArray[0].equals("foo") && pt.stringArray[1].equals("fi")
            && pt.stringArray[2].equals("fi") && pt.stringArray[3].equals("fum"));

    List<String> list = new ArrayList<String>();
    list.add("4foo");
    list.add("7fi");
    list.add("6fi");
    list.add("5fum");
    bw.setPropertyValue("stringArray", list);
    assertTrue("stringArray length = 4", pt.stringArray.length == 4);
    assertTrue("correct values", pt.stringArray[0].equals("foo") && pt.stringArray[1].equals("fi")
            && pt.stringArray[2].equals("fi") && pt.stringArray[3].equals("fum"));

    Set<String> set = new HashSet<String>();
    set.add("4foo");
    set.add("7fi");
    set.add("6fum");
    bw.setPropertyValue("stringArray", set);
    assertTrue("stringArray length = 3", pt.stringArray.length == 3);
    List<String> result = Arrays.asList(pt.stringArray);
    assertTrue("correct values", result.contains("foo") && result.contains("fi") && result.contains("fum"));

    bw.setPropertyValue("stringArray", "8one");
    assertTrue("stringArray length = 1", pt.stringArray.length == 1);
    assertTrue("correct values", pt.stringArray[0].equals("one"));
}