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));
}
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"));
}
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']"));
}
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());
}
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()));
}
Aggregations