Search in sources :

Example 51 with IndexedTestBean

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

the class AbstractPropertyAccessorTests method setCollectionPropertyWithArrayValue.

// list cannot be properly parameterized as it breaks other tests
@SuppressWarnings("unchecked")
@Test
public void setCollectionPropertyWithArrayValue() {
    IndexedTestBean target = new IndexedTestBean();
    AbstractPropertyAccessor accessor = createAccessor(target);
    Collection<String> coll = new HashSet<>();
    coll.add("coll1");
    accessor.setPropertyValue("collection", coll.toArray());
    List<String> set = new LinkedList<>();
    set.add("set1");
    accessor.setPropertyValue("set", set.toArray());
    List<String> sortedSet = new ArrayList<>();
    sortedSet.add("sortedSet1");
    accessor.setPropertyValue("sortedSet", sortedSet.toArray());
    Set<String> list = new HashSet<>();
    list.add("list1");
    accessor.setPropertyValue("list", list.toArray());
    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) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 52 with IndexedTestBean

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

the class AbstractPropertyAccessorTests method setMapPropertyNonMatchingType.

@Test
public void setMapPropertyNonMatchingType() {
    IndexedTestBean target = new IndexedTestBean();
    AbstractPropertyAccessor accessor = createAccessor(target);
    Map<String, String> map = new TreeMap<>();
    map.put("key", "value");
    accessor.setPropertyValue("map", map);
    Map<String, String> sortedMap = new TreeMap<>();
    sortedMap.put("sortedKey", "sortedValue");
    accessor.setPropertyValue("sortedMap", sortedMap);
    assertEquals(1, target.getMap().size());
    assertEquals("value", target.getMap().get("key"));
    assertEquals(1, target.getSortedMap().size());
    assertEquals("sortedValue", target.getSortedMap().get("sortedKey"));
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) TreeMap(java.util.TreeMap) Test(org.junit.Test)

Example 53 with IndexedTestBean

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

the class AbstractPropertyAccessorTests method getAndSetIndexedPropertiesWithDirectAccess.

@Test
public void getAndSetIndexedPropertiesWithDirectAccess() {
    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("key2"));
    assertEquals(tb0, accessor.getPropertyValue("array[0]"));
    assertEquals(tb1, accessor.getPropertyValue("array[1]"));
    assertEquals(tb2, accessor.getPropertyValue("list[0]"));
    assertEquals(tb3, accessor.getPropertyValue("list[1]"));
    assertEquals(tb6, accessor.getPropertyValue("set[0]"));
    assertEquals(tb7, accessor.getPropertyValue("set[1]"));
    assertEquals(tb4, accessor.getPropertyValue("map[key1]"));
    assertEquals(tb5, accessor.getPropertyValue("map[key2]"));
    assertEquals(tb4, accessor.getPropertyValue("map['key1']"));
    assertEquals(tb5, accessor.getPropertyValue("map[\"key2\"]"));
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("array[0]", tb5);
    pvs.add("array[1]", tb4);
    pvs.add("list[0]", tb3);
    pvs.add("list[1]", tb2);
    pvs.add("list[2]", tb0);
    pvs.add("list[4]", tb1);
    pvs.add("map[key1]", tb1);
    pvs.add("map['key2']", tb0);
    pvs.add("map[key5]", tb4);
    pvs.add("map['key9']", tb5);
    accessor.setPropertyValues(pvs);
    assertEquals(tb5, target.getArray()[0]);
    assertEquals(tb4, target.getArray()[1]);
    assertEquals(tb3, (target.getList().get(0)));
    assertEquals(tb2, (target.getList().get(1)));
    assertEquals(tb0, (target.getList().get(2)));
    assertEquals(null, (target.getList().get(3)));
    assertEquals(tb1, (target.getList().get(4)));
    assertEquals(tb1, (target.getMap().get("key1")));
    assertEquals(tb0, (target.getMap().get("key2")));
    assertEquals(tb4, (target.getMap().get("key5")));
    assertEquals(tb5, (target.getMap().get("key9")));
    assertEquals(tb5, accessor.getPropertyValue("array[0]"));
    assertEquals(tb4, accessor.getPropertyValue("array[1]"));
    assertEquals(tb3, accessor.getPropertyValue("list[0]"));
    assertEquals(tb2, accessor.getPropertyValue("list[1]"));
    assertEquals(tb0, accessor.getPropertyValue("list[2]"));
    assertEquals(null, accessor.getPropertyValue("list[3]"));
    assertEquals(tb1, accessor.getPropertyValue("list[4]"));
    assertEquals(tb1, accessor.getPropertyValue("map[\"key1\"]"));
    assertEquals(tb0, accessor.getPropertyValue("map['key2']"));
    assertEquals(tb4, accessor.getPropertyValue("map[\"key5\"]"));
    assertEquals(tb5, accessor.getPropertyValue("map['key9']"));
}
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 54 with IndexedTestBean

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

the class PropertyResourceConfigurerTests method testPropertyOverrideConfigurerWithNestedPropertyAndDotInBeanName.

@Test
public void testPropertyOverrideConfigurerWithNestedPropertyAndDotInBeanName() {
    BeanDefinition def = BeanDefinitionBuilder.genericBeanDefinition(IndexedTestBean.class).getBeanDefinition();
    factory.registerBeanDefinition("my.tb", def);
    PropertyOverrideConfigurer poc;
    poc = new PropertyOverrideConfigurer();
    Properties props = new Properties();
    props.setProperty("my.tb_array[0].age", "99");
    props.setProperty("my.tb_list[1].name", "test");
    poc.setProperties(props);
    poc.setBeanNameSeparator("_");
    poc.postProcessBeanFactory(factory);
    IndexedTestBean tb = (IndexedTestBean) factory.getBean("my.tb");
    assertEquals(99, tb.getArray()[0].getAge());
    assertEquals("test", ((TestBean) tb.getList().get(1)).getName());
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ChildBeanDefinition(org.springframework.beans.factory.support.ChildBeanDefinition) Properties(java.util.Properties) Test(org.junit.Test)

Example 55 with IndexedTestBean

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

the class BindTagTests method bindTagWithIndexedPropertiesAndCustomEditor.

@Test
public void bindTagWithIndexedPropertiesAndCustomEditor() throws JspException {
    PageContext pc = createPageContext();
    IndexedTestBean tb = new IndexedTestBean();
    DataBinder binder = new ServletRequestDataBinder(tb, "tb");
    binder.registerCustomEditor(TestBean.class, null, new PropertyEditorSupport() {

        @Override
        public String getAsText() {
            return "something";
        }
    });
    Errors errors = binder.getBindingResult();
    errors.rejectValue("array[0]", "code1", "message1");
    errors.rejectValue("array[0]", "code2", "message2");
    pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
    BindTag tag = new BindTag();
    tag.setPageContext(pc);
    tag.setPath("tb.array[0]");
    assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
    BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
    assertTrue("Has status variable", status != null);
    assertTrue("Correct expression", "array[0]".equals(status.getExpression()));
    // because of the custom editor getValue() should return a String
    assertTrue("Value is TestBean", status.getValue() instanceof String);
    assertTrue("Correct value", "something".equals(status.getValue()));
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) Errors(org.springframework.validation.Errors) ServletRequestDataBinder(org.springframework.web.bind.ServletRequestDataBinder) PageContext(javax.servlet.jsp.PageContext) ServletRequestDataBinder(org.springframework.web.bind.ServletRequestDataBinder) DataBinder(org.springframework.validation.DataBinder) BindStatus(org.springframework.web.servlet.support.BindStatus) 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