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 testSetNestedPropertyPolymorphic() throws Exception {
    ITestBean rod = new TestBean("rod", 31);
    ITestBean kerry = new Employee();

    BeanWrapper bw = new BeanWrapperImpl(rod);
    bw.setPropertyValue("spouse", kerry);
    bw.setPropertyValue("spouse.age", new Integer(35));
    bw.setPropertyValue("spouse.name", "Kerry");
    bw.setPropertyValue("spouse.company", "Lewisham");
    assertTrue("kerry name is Kerry", kerry.getName().equals("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);

    BeanWrapper kbw = new BeanWrapperImpl(kerry);
    assertTrue("spouse.spouse.spouse.spouse.company=Lewisham",
            "Lewisham".equals(kbw.getPropertyValue("spouse.spouse.spouse.spouse.company")));
}

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

@Test
public void testNestedProperties() {
    String doctorCompany = "";
    String lawyerCompany = "Dr. Sueem";
    TestBean tb = new TestBean();
    BeanWrapper bw = new BeanWrapperImpl(tb);
    bw.setPropertyValue("doctor.company", doctorCompany);
    bw.setPropertyValue("lawyer.company", lawyerCompany);
    assertEquals(doctorCompany, tb.getDoctor().getCompany());
    assertEquals(lawyerCompany, tb.getLawyer().getCompany());
}

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

@Test
public void testTypedMapReadOnlyMap() {
    TypedReadOnlyMap map = new TypedReadOnlyMap(Collections.singletonMap("key", new TestBean()));
    TypedReadOnlyMapClient bean = new TypedReadOnlyMapClient();
    BeanWrapper bw = new BeanWrapperImpl(bean);
    bw.setPropertyValue("map", map);
}

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

@Test
public void testPrimitiveArray() {
    PrimitiveArrayBean tb = new PrimitiveArrayBean();
    BeanWrapper bw = new BeanWrapperImpl(tb);
    bw.setPropertyValue("array", new String[] { "1", "2" });
    assertEquals(2, tb.getArray().length);
    assertEquals(1, tb.getArray()[0]);// w  w  w .  j av a  2  s .c  om
    assertEquals(2, tb.getArray()[1]);
}

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

@Test
public void testLargeMatchingPrimitiveArray() {
    Assume.group(TestGroup.PERFORMANCE);
    Assume.notLogging(LogFactory.getLog(BeanWrapperTests.class));

    PrimitiveArrayBean tb = new PrimitiveArrayBean();
    BeanWrapper bw = new BeanWrapperImpl(tb);
    int[] input = new int[1024];
    StopWatch sw = new StopWatch();
    sw.start("array1");
    for (int i = 0; i < 1000; i++) {
        bw.setPropertyValue("array", input);
    }/*from  w w  w.j  a  va2s.  c o  m*/
    sw.stop();
    assertEquals(1024, tb.getArray().length);
    assertEquals(0, tb.getArray()[0]);
    long time1 = sw.getLastTaskTimeMillis();
    assertTrue("Took too long", sw.getLastTaskTimeMillis() < 100);

    bw.registerCustomEditor(String.class, new StringTrimmerEditor(false));
    sw.start("array2");
    for (int i = 0; i < 1000; i++) {
        bw.setPropertyValue("array", input);
    }
    sw.stop();
    assertTrue("Took too long", sw.getLastTaskTimeMillis() < 125);

    bw.registerCustomEditor(int.class, "array.somePath", new CustomNumberEditor(Integer.class, false));
    sw.start("array3");
    for (int i = 0; i < 1000; i++) {
        bw.setPropertyValue("array", input);
    }
    sw.stop();
    assertTrue("Took too long", sw.getLastTaskTimeMillis() < 100);

    bw.registerCustomEditor(int.class, "array[0].somePath", new CustomNumberEditor(Integer.class, false));
    sw.start("array3");
    for (int i = 0; i < 1000; i++) {
        bw.setPropertyValue("array", input);
    }
    sw.stop();
    assertTrue("Took too long", sw.getLastTaskTimeMillis() < 100);

    bw.registerCustomEditor(int.class, new CustomNumberEditor(Integer.class, false));
    sw.start("array4");
    for (int i = 0; i < 100; i++) {
        bw.setPropertyValue("array", input);
    }
    sw.stop();
    assertEquals(1024, tb.getArray().length);
    assertEquals(0, tb.getArray()[0]);
    assertTrue("Took too long", sw.getLastTaskTimeMillis() > time1);
}

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

@Test
public void testLargeMatchingPrimitiveArrayWithSpecificEditor() {
    PrimitiveArrayBean tb = new PrimitiveArrayBean();
    BeanWrapper bw = new BeanWrapperImpl(tb);
    bw.registerCustomEditor(int.class, "array", new PropertyEditorSupport() {
        @Override/*w ww. j ava 2  s .c  o  m*/
        public void setValue(Object value) {
            if (value instanceof Integer) {
                super.setValue(new Integer(((Integer) value).intValue() + 1));
            }
        }
    });
    int[] input = new int[1024];
    bw.setPropertyValue("array", input);
    assertEquals(1024, tb.getArray().length);
    assertEquals(1, tb.getArray()[0]);
    assertEquals(1, tb.getArray()[1]);
}

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

@Test
public void testLargeMatchingPrimitiveArrayWithIndexSpecificEditor() {
    PrimitiveArrayBean tb = new PrimitiveArrayBean();
    BeanWrapper bw = new BeanWrapperImpl(tb);
    bw.registerCustomEditor(int.class, "array[1]", new PropertyEditorSupport() {
        @Override//w  w  w.  j  av  a  2s  .c o  m
        public void setValue(Object value) {
            if (value instanceof Integer) {
                super.setValue(new Integer(((Integer) value).intValue() + 1));
            }
        }
    });
    int[] input = new int[1024];
    bw.setPropertyValue("array", input);
    assertEquals(1024, tb.getArray().length);
    assertEquals(0, tb.getArray()[0]);
    assertEquals(1, tb.getArray()[1]);
}

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

@Test
public void testPropertiesInProtectedBaseBean() {
    DerivedFromProtectedBaseBean bean = new DerivedFromProtectedBaseBean();
    BeanWrapper bw = new BeanWrapperImpl(bean);
    bw.setPropertyValue("someProperty", "someValue");
    assertEquals("someValue", bw.getPropertyValue("someProperty"));
    assertEquals("someValue", bean.getSomeProperty());
}

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

@Test
public void testMatchingCollections() {
    IndexedTestBean tb = new IndexedTestBean();
    BeanWrapper bw = new BeanWrapperImpl(tb);
    Collection<String> coll = new HashSet<String>();
    coll.add("coll1");
    bw.setPropertyValue("collection", coll);
    Set<String> set = new HashSet<String>();
    set.add("set1");
    bw.setPropertyValue("set", set);
    SortedSet<String> sortedSet = new TreeSet<String>();
    sortedSet.add("sortedSet1");
    bw.setPropertyValue("sortedSet", sortedSet);
    List<String> list = new LinkedList<String>();
    list.add("list1");
    bw.setPropertyValue("list", list);
    assertSame(coll, tb.getCollection());
    assertSame(set, tb.getSet());//from   w w  w  .  j a v  a 2s.  c  om
    assertSame(sortedSet, tb.getSortedSet());
    assertSame(list, tb.getList());
}

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

@SuppressWarnings("unchecked") // list cannot be properly parameterized as it breaks other tests
@Test//from   ww w. j a v a  2s .  c  o m
public void testNonMatchingCollections() {
    IndexedTestBean tb = new IndexedTestBean();
    BeanWrapper bw = new BeanWrapperImpl(tb);
    Collection<String> coll = new ArrayList<String>();
    coll.add("coll1");
    bw.setPropertyValue("collection", coll);
    List<String> set = new LinkedList<String>();
    set.add("set1");
    bw.setPropertyValue("set", set);
    List<String> sortedSet = new ArrayList<String>();
    sortedSet.add("sortedSet1");
    bw.setPropertyValue("sortedSet", sortedSet);
    Set<String> list = new HashSet<String>();
    list.add("list1");
    bw.setPropertyValue("list", list);
    assertEquals(1, tb.getCollection().size());
    assertTrue(tb.getCollection().containsAll(coll));
    assertEquals(1, tb.getSet().size());
    assertTrue(tb.getSet().containsAll(set));
    assertEquals(1, tb.getSortedSet().size());
    assertTrue(tb.getSortedSet().containsAll(sortedSet));
    assertEquals(1, tb.getList().size());
    assertTrue(tb.getList().containsAll(list));
}