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