Search in sources :

Example 11 with IndexedTestBean

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

the class AbstractPropertyAccessorTests method setMapPropertyWithUnmodifiableMap.

@Test
public void setMapPropertyWithUnmodifiableMap() {
    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<Integer, String> inputMap = new HashMap<>();
    inputMap.put(1, "rod");
    inputMap.put(2, "rob");
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("map", Collections.unmodifiableMap(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) 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) HashMap(java.util.HashMap) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.Test)

Example 12 with IndexedTestBean

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

the class AbstractPropertyAccessorTests method getAndSetIndexedProperties.

@Test
public void getAndSetIndexedProperties() {
    IndexedTestBean target = new IndexedTestBean();
    AbstractPropertyAccessor accessor = createAccessor(target);
    TestBean tb0 = target.getArray()[0];
    TestBean tb1 = target.getArray()[1];
    TestBean tb2 = ((TestBean) target.getList().get(0));
    TestBean tb3 = ((TestBean) target.getList().get(1));
    TestBean tb6 = ((TestBean) target.getSet().toArray()[0]);
    TestBean tb7 = ((TestBean) target.getSet().toArray()[1]);
    TestBean tb4 = ((TestBean) target.getMap().get("key1"));
    TestBean tb5 = ((TestBean) target.getMap().get("key.3"));
    assertEquals("name0", tb0.getName());
    assertEquals("name1", tb1.getName());
    assertEquals("name2", tb2.getName());
    assertEquals("name3", tb3.getName());
    assertEquals("name6", tb6.getName());
    assertEquals("name7", tb7.getName());
    assertEquals("name4", tb4.getName());
    assertEquals("name5", tb5.getName());
    assertEquals("name0", accessor.getPropertyValue("array[0].name"));
    assertEquals("name1", accessor.getPropertyValue("array[1].name"));
    assertEquals("name2", accessor.getPropertyValue("list[0].name"));
    assertEquals("name3", accessor.getPropertyValue("list[1].name"));
    assertEquals("name6", accessor.getPropertyValue("set[0].name"));
    assertEquals("name7", accessor.getPropertyValue("set[1].name"));
    assertEquals("name4", accessor.getPropertyValue("map[key1].name"));
    assertEquals("name5", accessor.getPropertyValue("map[key.3].name"));
    assertEquals("name4", accessor.getPropertyValue("map['key1'].name"));
    assertEquals("name5", accessor.getPropertyValue("map[\"key.3\"].name"));
    assertEquals("nameX", accessor.getPropertyValue("map[key4][0].name"));
    assertEquals("nameY", accessor.getPropertyValue("map[key4][1].name"));
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("array[0].name", "name5");
    pvs.add("array[1].name", "name4");
    pvs.add("list[0].name", "name3");
    pvs.add("list[1].name", "name2");
    pvs.add("set[0].name", "name8");
    pvs.add("set[1].name", "name9");
    pvs.add("map[key1].name", "name1");
    pvs.add("map['key.3'].name", "name0");
    pvs.add("map[key4][0].name", "nameA");
    pvs.add("map[key4][1].name", "nameB");
    accessor.setPropertyValues(pvs);
    assertEquals("name5", tb0.getName());
    assertEquals("name4", tb1.getName());
    assertEquals("name3", tb2.getName());
    assertEquals("name2", tb3.getName());
    assertEquals("name1", tb4.getName());
    assertEquals("name0", tb5.getName());
    assertEquals("name5", accessor.getPropertyValue("array[0].name"));
    assertEquals("name4", accessor.getPropertyValue("array[1].name"));
    assertEquals("name3", accessor.getPropertyValue("list[0].name"));
    assertEquals("name2", accessor.getPropertyValue("list[1].name"));
    assertEquals("name8", accessor.getPropertyValue("set[0].name"));
    assertEquals("name9", accessor.getPropertyValue("set[1].name"));
    assertEquals("name1", accessor.getPropertyValue("map[\"key1\"].name"));
    assertEquals("name0", accessor.getPropertyValue("map['key.3'].name"));
    assertEquals("nameA", accessor.getPropertyValue("map[key4][0].name"));
    assertEquals("nameB", accessor.getPropertyValue("map[key4][1].name"));
}
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) Test(org.junit.Test)

Example 13 with IndexedTestBean

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

the class AbstractPropertyAccessorTests method setCollectionProperty.

@Test
public void setCollectionProperty() {
    IndexedTestBean target = new IndexedTestBean();
    AbstractPropertyAccessor accessor = createAccessor(target);
    Collection<String> coll = new HashSet<>();
    coll.add("coll1");
    accessor.setPropertyValue("collection", coll);
    Set<String> set = new HashSet<>();
    set.add("set1");
    accessor.setPropertyValue("set", set);
    SortedSet<String> sortedSet = new TreeSet<>();
    sortedSet.add("sortedSet1");
    accessor.setPropertyValue("sortedSet", sortedSet);
    List<String> list = new LinkedList<>();
    list.add("list1");
    accessor.setPropertyValue("list", list);
    assertSame(coll, target.getCollection());
    assertSame(set, target.getSet());
    assertSame(sortedSet, target.getSortedSet());
    assertSame(list, target.getList());
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) TreeSet(java.util.TreeSet) LinkedList(java.util.LinkedList) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 14 with IndexedTestBean

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

the class AbstractPropertyAccessorTests method setCollectionPropertyWithIntegerValue.

// list cannot be properly parameterized as it breaks other tests
@SuppressWarnings("unchecked")
@Test
public void setCollectionPropertyWithIntegerValue() {
    IndexedTestBean target = new IndexedTestBean();
    AbstractPropertyAccessor accessor = createAccessor(target);
    Collection<Integer> coll = new HashSet<>();
    coll.add(0);
    accessor.setPropertyValue("collection", new Integer(0));
    List<Integer> set = new LinkedList<>();
    set.add(1);
    accessor.setPropertyValue("set", new Integer(1));
    List<Integer> sortedSet = new ArrayList<>();
    sortedSet.add(2);
    accessor.setPropertyValue("sortedSet", new Integer(2));
    Set<Integer> list = new HashSet<>();
    list.add(3);
    accessor.setPropertyValue("list", new Integer(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 15 with IndexedTestBean

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

the class AbstractPropertyAccessorTests method setCollectionPropertyWithStringValueAndCustomEditor.

@Test
public void setCollectionPropertyWithStringValueAndCustomEditor() {
    IndexedTestBean target = new IndexedTestBean();
    AbstractPropertyAccessor accessor = createAccessor(target);
    accessor.registerCustomEditor(String.class, "set", new StringTrimmerEditor(false));
    accessor.registerCustomEditor(String.class, "list", new StringTrimmerEditor(false));
    accessor.setPropertyValue("set", "set1 ");
    accessor.setPropertyValue("sortedSet", "sortedSet1");
    accessor.setPropertyValue("list", "list1 ");
    assertEquals(1, target.getSet().size());
    assertTrue(target.getSet().contains("set1"));
    assertEquals(1, target.getSortedSet().size());
    assertTrue(target.getSortedSet().contains("sortedSet1"));
    assertEquals(1, target.getList().size());
    assertTrue(target.getList().contains("list1"));
    accessor.setPropertyValue("list", Collections.singletonList("list1 "));
    assertTrue(target.getList().contains("list1"));
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) StringTrimmerEditor(org.springframework.beans.propertyeditors.StringTrimmerEditor) 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