Search in sources :

Example 16 with IndexedTestBean

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

the class AbstractPropertyAccessorTests method setCollectionPropertyWithIntArrayValue.

// list cannot be properly parameterized as it breaks other tests
@SuppressWarnings("unchecked")
@Test
public void setCollectionPropertyWithIntArrayValue() {
    IndexedTestBean target = new IndexedTestBean();
    AbstractPropertyAccessor accessor = createAccessor(target);
    Collection<Integer> coll = new HashSet<>();
    coll.add(0);
    accessor.setPropertyValue("collection", new int[] { 0 });
    List<Integer> set = new LinkedList<>();
    set.add(1);
    accessor.setPropertyValue("set", new int[] { 1 });
    List<Integer> sortedSet = new ArrayList<>();
    sortedSet.add(2);
    accessor.setPropertyValue("sortedSet", new int[] { 2 });
    Set<Integer> list = new HashSet<>();
    list.add(3);
    accessor.setPropertyValue("list", new int[] { 3 });
    assertEquals(1, target.getCollection().size());
    assertTrue(target.getCollection().containsAll(coll));
    assertEquals(1, target.getSet().size());
    assertTrue(target.getSet().containsAll(set));
    assertEquals(1, target.getSortedSet().size());
    assertTrue(target.getSortedSet().containsAll(sortedSet));
    assertEquals(1, target.getList().size());
    assertTrue(target.getList().containsAll(list));
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) BigInteger(java.math.BigInteger) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 17 with IndexedTestBean

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

the class AbstractPropertyAccessorTests method propertyTypeIndexedProperty.

@Test
public void propertyTypeIndexedProperty() {
    IndexedTestBean target = new IndexedTestBean();
    AbstractPropertyAccessor accessor = createAccessor(target);
    assertEquals(null, accessor.getPropertyType("map[key0]"));
    accessor = createAccessor(target);
    accessor.setPropertyValue("map[key0]", "my String");
    assertEquals(String.class, accessor.getPropertyType("map[key0]"));
    accessor = createAccessor(target);
    accessor.registerCustomEditor(String.class, "map[key0]", new StringTrimmerEditor(false));
    assertEquals(String.class, accessor.getPropertyType("map[key0]"));
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) StringTrimmerEditor(org.springframework.beans.propertyeditors.StringTrimmerEditor) Test(org.junit.Test)

Example 18 with IndexedTestBean

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

the class AbstractPropertyAccessorTests method isReadableWritableForIndexedProperties.

@Test
public void isReadableWritableForIndexedProperties() {
    IndexedTestBean target = new IndexedTestBean();
    AbstractPropertyAccessor accessor = createAccessor(target);
    assertTrue(accessor.isReadableProperty("array"));
    assertTrue(accessor.isReadableProperty("list"));
    assertTrue(accessor.isReadableProperty("set"));
    assertTrue(accessor.isReadableProperty("map"));
    assertFalse(accessor.isReadableProperty("xxx"));
    assertTrue(accessor.isWritableProperty("array"));
    assertTrue(accessor.isWritableProperty("list"));
    assertTrue(accessor.isWritableProperty("set"));
    assertTrue(accessor.isWritableProperty("map"));
    assertFalse(accessor.isWritableProperty("xxx"));
    assertTrue(accessor.isReadableProperty("array[0]"));
    assertTrue(accessor.isReadableProperty("array[0].name"));
    assertTrue(accessor.isReadableProperty("list[0]"));
    assertTrue(accessor.isReadableProperty("list[0].name"));
    assertTrue(accessor.isReadableProperty("set[0]"));
    assertTrue(accessor.isReadableProperty("set[0].name"));
    assertTrue(accessor.isReadableProperty("map[key1]"));
    assertTrue(accessor.isReadableProperty("map[key1].name"));
    assertTrue(accessor.isReadableProperty("map[key4][0]"));
    assertTrue(accessor.isReadableProperty("map[key4][0].name"));
    assertTrue(accessor.isReadableProperty("map[key4][1]"));
    assertTrue(accessor.isReadableProperty("map[key4][1].name"));
    assertFalse(accessor.isReadableProperty("array[key1]"));
    assertTrue(accessor.isWritableProperty("array[0]"));
    assertTrue(accessor.isWritableProperty("array[0].name"));
    assertTrue(accessor.isWritableProperty("list[0]"));
    assertTrue(accessor.isWritableProperty("list[0].name"));
    assertTrue(accessor.isWritableProperty("set[0]"));
    assertTrue(accessor.isWritableProperty("set[0].name"));
    assertTrue(accessor.isWritableProperty("map[key1]"));
    assertTrue(accessor.isWritableProperty("map[key1].name"));
    assertTrue(accessor.isWritableProperty("map[key4][0]"));
    assertTrue(accessor.isWritableProperty("map[key4][0].name"));
    assertTrue(accessor.isWritableProperty("map[key4][1]"));
    assertTrue(accessor.isWritableProperty("map[key4][1].name"));
    assertFalse(accessor.isWritableProperty("array[key1]"));
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) Test(org.junit.Test)

Example 19 with IndexedTestBean

use of org.springframework.tests.sample.beans.IndexedTestBean 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 20 with IndexedTestBean

use of org.springframework.tests.sample.beans.IndexedTestBean 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)

Aggregations

IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)60 Test (org.junit.Test)59 TestBean (org.springframework.tests.sample.beans.TestBean)28 ITestBean (org.springframework.tests.sample.beans.ITestBean)26 PropertyEditorSupport (java.beans.PropertyEditorSupport)22 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)17 BooleanTestBean (org.springframework.tests.sample.beans.BooleanTestBean)15 NumberTestBean (org.springframework.tests.sample.beans.NumberTestBean)15 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)14 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)11 BeanWrapper (org.springframework.beans.BeanWrapper)10 BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)10 NestedTestBean (org.springframework.tests.sample.beans.NestedTestBean)9 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)8 ChildBeanDefinition (org.springframework.beans.factory.support.ChildBeanDefinition)7 ArrayList (java.util.ArrayList)6 HashSet (java.util.HashSet)6 LinkedList (java.util.LinkedList)6 HashMap (java.util.HashMap)4 Properties (java.util.Properties)4