Example usage for org.springframework.beans BeanWrapperImpl BeanWrapperImpl

List of usage examples for org.springframework.beans BeanWrapperImpl BeanWrapperImpl

Introduction

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

Prototype

public BeanWrapperImpl(Class<?> clazz) 

Source Link

Document

Create a new BeanWrapperImpl, wrapping a new instance of the specified class.

Usage

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

@Test
public void testNullNestedTypeDescriptorWithBadConversionService() {
    Foo foo = new Foo();
    BeanWrapperImpl wrapper = new BeanWrapperImpl(foo);
    wrapper.setConversionService(new GenericConversionService() {
        @Override/*from w w w  . ja va 2  s.c  o m*/
        public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
            throw new ConversionFailedException(sourceType, targetType, source, null);
        }
    });
    wrapper.setAutoGrowNestedPaths(true);
    wrapper.setPropertyValue("listOfMaps[0]['luckyNumber']", "9");
    assertEquals("9", foo.listOfMaps.get(0).get("luckyNumber"));
}

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

@Test
public void testReadableAndWritableForIndexedProperties() {
    BeanWrapper bw = new BeanWrapperImpl(IndexedTestBean.class);

    assertTrue(bw.isReadableProperty("array"));
    assertTrue(bw.isReadableProperty("list"));
    assertTrue(bw.isReadableProperty("set"));
    assertTrue(bw.isReadableProperty("map"));
    assertFalse(bw.isReadableProperty("xxx"));

    assertTrue(bw.isWritableProperty("array"));
    assertTrue(bw.isWritableProperty("list"));
    assertTrue(bw.isWritableProperty("set"));
    assertTrue(bw.isWritableProperty("map"));
    assertFalse(bw.isWritableProperty("xxx"));

    assertTrue(bw.isReadableProperty("array[0]"));
    assertTrue(bw.isReadableProperty("array[0].name"));
    assertTrue(bw.isReadableProperty("list[0]"));
    assertTrue(bw.isReadableProperty("list[0].name"));
    assertTrue(bw.isReadableProperty("set[0]"));
    assertTrue(bw.isReadableProperty("set[0].name"));
    assertTrue(bw.isReadableProperty("map[key1]"));
    assertTrue(bw.isReadableProperty("map[key1].name"));
    assertTrue(bw.isReadableProperty("map[key4][0]"));
    assertTrue(bw.isReadableProperty("map[key4][0].name"));
    assertTrue(bw.isReadableProperty("map[key4][1]"));
    assertTrue(bw.isReadableProperty("map[key4][1].name"));
    assertFalse(bw.isReadableProperty("array[key1]"));

    assertTrue(bw.isWritableProperty("array[0]"));
    assertTrue(bw.isWritableProperty("array[0].name"));
    assertTrue(bw.isWritableProperty("list[0]"));
    assertTrue(bw.isWritableProperty("list[0].name"));
    assertTrue(bw.isWritableProperty("set[0]"));
    assertTrue(bw.isWritableProperty("set[0].name"));
    assertTrue(bw.isWritableProperty("map[key1]"));
    assertTrue(bw.isWritableProperty("map[key1].name"));
    assertTrue(bw.isWritableProperty("map[key4][0]"));
    assertTrue(bw.isWritableProperty("map[key4][0].name"));
    assertTrue(bw.isWritableProperty("map[key4][1]"));
    assertTrue(bw.isWritableProperty("map[key4][1].name"));
    assertFalse(bw.isWritableProperty("array[key1]"));
}

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 testEmptyPropertyValuesSet() {
    TestBean t = new TestBean();
    int age = 50;
    String name = "Tony";
    t.setAge(age);/*from   ww w. j  a v  a 2 s  . com*/
    t.setName(name);
    try {
        BeanWrapper bw = new BeanWrapperImpl(t);
        assertTrue("age is OK", t.getAge() == age);
        assertTrue("name is OK", name.equals(t.getName()));
        bw.setPropertyValues(new MutablePropertyValues());
        // Check its unchanged
        assertTrue("age is OK", t.getAge() == age);
        assertTrue("name is OK", name.equals(t.getName()));
    } catch (BeansException ex) {
        fail("Shouldn't throw exception when everything is valid");
    }
}

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

@Test
public void testAllValid() {
    TestBean t = new TestBean();
    String newName = "tony";
    int newAge = 65;
    String newTouchy = "valid";
    try {/*  w  ww  .jav a 2s .c om*/
        BeanWrapper bw = new BeanWrapperImpl(t);
        MutablePropertyValues pvs = new MutablePropertyValues();
        pvs.addPropertyValue(new PropertyValue("age", new Integer(newAge)));
        pvs.addPropertyValue(new PropertyValue("name", newName));

        pvs.addPropertyValue(new PropertyValue("touchy", newTouchy));
        bw.setPropertyValues(pvs);
        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 testBeanWrapperUpdates() {
    TestBean t = new TestBean();
    int newAge = 33;
    try {/*from w  w  w.j a  v  a  2s.  c  o  m*/
        BeanWrapper bw = new BeanWrapperImpl(t);
        t.setAge(newAge);
        Object bwAge = bw.getPropertyValue("age");
        assertTrue("Age is an integer", bwAge instanceof Integer);
        int bwi = ((Integer) bwAge).intValue();
        assertTrue("Bean wrapper must pick up changes", bwi == newAge);
    } catch (Exception ex) {
        fail("Shouldn't throw exception when everything is valid");
    }
}

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);/*from  w  w  w. ja  va 2  s .c o  m*/
    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 testIgnoringIndexedProperty() {
    MutablePropertyValues values = new MutablePropertyValues();
    values.add("toBeIgnored[0]", new Integer(42));
    BeanWrapper bw = new BeanWrapperImpl(new Object());
    bw.setPropertyValues(values, true);//  ww  w .j  a va  2 s .co m
}

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

@Test
public void testConvertPrimitiveToString() {
    MutablePropertyValues values = new MutablePropertyValues();
    values.add("name", new Integer(42));
    TestBean tb = new TestBean();
    BeanWrapper bw = new BeanWrapperImpl(tb);
    bw.setPropertyValues(values);/*from  w  w w  .ja  v  a  2  s. co  m*/
    assertEquals("42", tb.getName());
}