Search in sources :

Example 31 with TestBean

use of org.springframework.tests.sample.beans.TestBean in project spring-framework by spring-projects.

the class BeanWrapperTests method getPropertyWithOptional.

@Test
public void getPropertyWithOptional() {
    GetterWithOptional target = new GetterWithOptional();
    TestBean tb = new TestBean("x");
    BeanWrapper accessor = createAccessor(target);
    accessor.setPropertyValue("object", tb);
    assertSame(tb, target.value);
    assertSame(tb, target.getObject().get());
    assertSame(tb, ((Optional<String>) accessor.getPropertyValue("object")).get());
    assertEquals("x", target.value.getName());
    assertEquals("x", target.getObject().get().getName());
    assertEquals("x", accessor.getPropertyValue("object.name"));
    accessor.setPropertyValue("object.name", "y");
    assertSame(tb, target.value);
    assertSame(tb, target.getObject().get());
    assertSame(tb, ((Optional<String>) accessor.getPropertyValue("object")).get());
    assertEquals("y", target.value.getName());
    assertEquals("y", target.getObject().get().getName());
    assertEquals("y", accessor.getPropertyValue("object.name"));
}
Also used : TestBean(org.springframework.tests.sample.beans.TestBean) Test(org.junit.Test)

Example 32 with TestBean

use of org.springframework.tests.sample.beans.TestBean in project spring-framework by spring-projects.

the class AbstractPropertyAccessorTests method setPropertyToNull.

@Test
public void setPropertyToNull() {
    TestBean target = new TestBean();
    // we need to change it back
    target.setName("Frank");
    target.setSpouse(target);
    AbstractPropertyAccessor accessor = createAccessor(target);
    assertTrue("name is not null to start off", target.getName() != null);
    accessor.setPropertyValue("name", null);
    assertTrue("name is now null", target.getName() == null);
    // now test with non-string
    assertTrue("spouse is not null to start off", target.getSpouse() != null);
    accessor.setPropertyValue("spouse", null);
    assertTrue("spouse is now null", target.getSpouse() == null);
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) BooleanTestBean(org.springframework.tests.sample.beans.BooleanTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NumberTestBean(org.springframework.tests.sample.beans.NumberTestBean) Test(org.junit.Test)

Example 33 with TestBean

use of org.springframework.tests.sample.beans.TestBean in project spring-framework by spring-projects.

the class AbstractPropertyAccessorTests method setMapPropertyWithTypeConversion.

@Test
public void setMapPropertyWithTypeConversion() {
    IndexedTestBean target = new IndexedTestBean();
    AbstractPropertyAccessor accessor = createAccessor(target);
    accessor.registerCustomEditor(TestBean.class, new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            if (!StringUtils.hasLength(text)) {
                throw new IllegalArgumentException();
            }
            setValue(new TestBean(text));
        }
    });
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("map[key1]", "rod");
    pvs.add("map[key2]", "rob");
    accessor.setPropertyValues(pvs);
    assertEquals("rod", ((TestBean) target.getMap().get("key1")).getName());
    assertEquals("rob", ((TestBean) target.getMap().get("key2")).getName());
    pvs = new MutablePropertyValues();
    pvs.add("map[key1]", "rod");
    pvs.add("map[key2]", "");
    try {
        accessor.setPropertyValues(pvs);
        fail("Should have thrown TypeMismatchException");
    } catch (PropertyBatchUpdateException ex) {
        PropertyAccessException pae = ex.getPropertyAccessException("map[key2]");
        assertTrue(pae instanceof TypeMismatchException);
    }
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) BooleanTestBean(org.springframework.tests.sample.beans.BooleanTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NumberTestBean(org.springframework.tests.sample.beans.NumberTestBean) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.Test)

Example 34 with TestBean

use of org.springframework.tests.sample.beans.TestBean in project spring-framework by spring-projects.

the class AbstractPropertyAccessorTests method setMapPropertyWithCustomUnmodifiableMap.

@Test
public void setMapPropertyWithCustomUnmodifiableMap() {
    IndexedTestBean target = new IndexedTestBean();
    AbstractPropertyAccessor accessor = createAccessor(target);
    accessor.registerCustomEditor(TestBean.class, "map", new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            if (!StringUtils.hasLength(text)) {
                throw new IllegalArgumentException();
            }
            setValue(new TestBean(text));
        }
    });
    Map<Object, Object> inputMap = new HashMap<>();
    inputMap.put(1, "rod");
    inputMap.put(2, "rob");
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("map", new ReadOnlyMap<>(inputMap));
    accessor.setPropertyValues(pvs);
    assertEquals("rod", ((TestBean) target.getMap().get(1)).getName());
    assertEquals("rob", ((TestBean) target.getMap().get(2)).getName());
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) BooleanTestBean(org.springframework.tests.sample.beans.BooleanTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NumberTestBean(org.springframework.tests.sample.beans.NumberTestBean) HashMap(java.util.HashMap) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.Test)

Example 35 with TestBean

use of org.springframework.tests.sample.beans.TestBean in project spring-framework by spring-projects.

the class AbstractPropertyAccessorTests method getAnotherNestedDeepProperty.

@Test
public void getAnotherNestedDeepProperty() {
    ITestBean target = new TestBean("rod", 31);
    ITestBean kerry = new TestBean("kerry", 35);
    target.setSpouse(kerry);
    kerry.setSpouse(target);
    AbstractPropertyAccessor accessor = createAccessor(target);
    Integer KA = (Integer) accessor.getPropertyValue("spouse.age");
    assertTrue("kerry is 35", KA == 35);
    Integer RA = (Integer) accessor.getPropertyValue("spouse.spouse.age");
    assertTrue("rod is 31, not" + RA, RA == 31);
    ITestBean spousesSpouse = (ITestBean) accessor.getPropertyValue("spouse.spouse");
    assertTrue("spousesSpouse = initial point", target == spousesSpouse);
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) BigInteger(java.math.BigInteger) IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) BooleanTestBean(org.springframework.tests.sample.beans.BooleanTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NumberTestBean(org.springframework.tests.sample.beans.NumberTestBean) Test(org.junit.Test)

Aggregations

TestBean (org.springframework.tests.sample.beans.TestBean)788 Test (org.junit.Test)740 ITestBean (org.springframework.tests.sample.beans.ITestBean)456 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)248 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)225 NestedTestBean (org.springframework.tests.sample.beans.NestedTestBean)164 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)160 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)144 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)86 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)44 BooleanTestBean (org.springframework.tests.sample.beans.BooleanTestBean)40 NumberTestBean (org.springframework.tests.sample.beans.NumberTestBean)40 Properties (java.util.Properties)35 Errors (org.springframework.validation.Errors)34 NopInterceptor (org.springframework.tests.aop.interceptor.NopInterceptor)33 PropertyEditorSupport (java.beans.PropertyEditorSupport)31 HashMap (java.util.HashMap)30 List (java.util.List)27 Map (java.util.Map)27 PageContext (javax.servlet.jsp.PageContext)27