Example usage for org.springframework.beans AbstractPropertyAccessor setPropertyValue

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

Introduction

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

Prototype

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

Source Link

Document

Actually set a property value.

Usage

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

@Test
public void getPropertyIntermediateMapEntryIsNullWithAutoGrow() {
    Foo target = new Foo();
    AbstractPropertyAccessor accessor = createAccessor(target);
    accessor.setConversionService(new DefaultConversionService());
    accessor.setAutoGrowNestedPaths(true);
    accessor.setPropertyValue("listOfMaps[0]['luckyNumber']", "9");
    assertEquals("9", target.listOfMaps.get(0).get("luckyNumber"));
}

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

@Test
public void setSimpleProperty() {
    Simple target = new Simple("John", 2);
    AbstractPropertyAccessor accessor = createAccessor(target);

    accessor.setPropertyValue("name", "SomeValue");

    assertThat(target.name, is("SomeValue"));
    assertThat(target.getName(), is("SomeValue"));
}

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

@Test
public void setNestedProperty() {
    Person target = createPerson("John", "Paris", "FR");
    AbstractPropertyAccessor accessor = createAccessor(target);

    accessor.setPropertyValue("address.city", "London");
    assertThat(target.address.city, is("London"));
}

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

@Test
public void setNestedPropertyPolymorphic() throws Exception {
    ITestBean target = new TestBean("rod", 31);
    ITestBean kerry = new Employee();

    AbstractPropertyAccessor accessor = createAccessor(target);
    accessor.setPropertyValue("spouse", kerry);
    accessor.setPropertyValue("spouse.age", new Integer(35));
    accessor.setPropertyValue("spouse.name", "Kerry");
    accessor.setPropertyValue("spouse.company", "Lewisham");
    assertTrue("kerry name is Kerry", kerry.getName().equals("Kerry"));

    assertTrue("nested set worked", target.getSpouse() == kerry);
    assertTrue("no back relation", kerry.getSpouse() == null);
    accessor.setPropertyValue(new PropertyValue("spouse.spouse", target));
    assertTrue("nested set worked", kerry.getSpouse() == target);

    AbstractPropertyAccessor kerryAccessor = createAccessor(kerry);
    assertTrue("spouse.spouse.spouse.spouse.company=Lewisham",
            "Lewisham".equals(kerryAccessor.getPropertyValue("spouse.spouse.spouse.spouse.company")));
}

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

@Test
public void setAnotherNestedProperty() throws Exception {
    ITestBean target = new TestBean("rod", 31);
    ITestBean kerry = new TestBean("kerry", 0);

    AbstractPropertyAccessor accessor = createAccessor(target);
    accessor.setPropertyValue("spouse", kerry);

    assertTrue("nested set worked", target.getSpouse() == kerry);
    assertTrue("no back relation", kerry.getSpouse() == null);
    accessor.setPropertyValue(new PropertyValue("spouse.spouse", target));
    assertTrue("nested set worked", kerry.getSpouse() == target);
    assertTrue("kerry age not set", kerry.getAge() == 0);
    accessor.setPropertyValue(new PropertyValue("spouse.age", 35));
    assertTrue("Set primitive on spouse", kerry.getAge() == 35);

    assertEquals(kerry, accessor.getPropertyValue("spouse"));
    assertEquals(target, accessor.getPropertyValue("spouse.spouse"));
}

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

@Test
public void setYetAnotherNestedProperties() {
    String doctorCompany = "";
    String lawyerCompany = "Dr. Sueem";
    TestBean target = new TestBean();
    AbstractPropertyAccessor accessor = createAccessor(target);
    accessor.setPropertyValue("doctor.company", doctorCompany);
    accessor.setPropertyValue("lawyer.company", lawyerCompany);
    assertEquals(doctorCompany, target.getDoctor().getCompany());
    assertEquals(lawyerCompany, target.getLawyer().getCompany());
}

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

@Test
public void setNestedDeepProperty() {
    Person target = createPerson("John", "Paris", "FR");
    AbstractPropertyAccessor accessor = createAccessor(target);

    accessor.setPropertyValue("address.country.name", "UK");
    assertThat(target.address.country.name, is("UK"));
}

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

@Test
public void setPropertyIntermediatePropertyIsNull() {
    Person target = createPerson("John", "Paris", "FR");
    target.address.country = null;/* w  ww.  j  a  v  a  2 s. c o  m*/
    AbstractPropertyAccessor accessor = createAccessor(target);

    try {
        accessor.setPropertyValue("address.country.name", "UK");
        fail("Should have failed to set value with intermediate null value");
    } catch (NullValueInNestedPathException e) {
        assertEquals("address.country", e.getPropertyName());
        assertEquals(Person.class, e.getBeanClass());
    }
    assertThat(target.address.country, is(nullValue())); // Not touched
}

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

@Test
public void setAnotherPropertyIntermediatePropertyIsNull() throws Exception {
    ITestBean target = new TestBean("rod", 31);
    AbstractPropertyAccessor accessor = createAccessor(target);
    try {//from w w  w.ja  v  a 2 s.c  o m
        accessor.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"));
    }
}

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

@Test
public void setPropertyIntermediatePropertyIsNullWithAutoGrow() {
    Person target = createPerson("John", "Paris", "FR");
    target.address.country = null;/*from  ww w  .j  ava 2  s .c o m*/
    AbstractPropertyAccessor accessor = createAccessor(target);
    accessor.setAutoGrowNestedPaths(true);

    accessor.setPropertyValue("address.country.name", "UK");
    assertThat(target.address.country.name, is("UK"));
}