use of org.springframework.tests.sample.beans.TestBean in project spring-framework by spring-projects.
the class BeanWrapperTests method getPropertyWithOptional.
@Test
public void getPropertyWithOptional() {
GetterWithOptional target = new GetterWithOptional();
TestBean tb = new TestBean("x");
BeanWrapper accessor = createAccessor(target);
accessor.setPropertyValue("object", tb);
assertSame(tb, target.value);
assertSame(tb, target.getObject().get());
assertSame(tb, ((Optional<String>) accessor.getPropertyValue("object")).get());
assertEquals("x", target.value.getName());
assertEquals("x", target.getObject().get().getName());
assertEquals("x", accessor.getPropertyValue("object.name"));
accessor.setPropertyValue("object.name", "y");
assertSame(tb, target.value);
assertSame(tb, target.getObject().get());
assertSame(tb, ((Optional<String>) accessor.getPropertyValue("object")).get());
assertEquals("y", target.value.getName());
assertEquals("y", target.getObject().get().getName());
assertEquals("y", accessor.getPropertyValue("object.name"));
}
use of org.springframework.tests.sample.beans.TestBean in project spring-framework by spring-projects.
the class AbstractPropertyAccessorTests method setPropertyToNull.
@Test
public void setPropertyToNull() {
TestBean target = new TestBean();
// we need to change it back
target.setName("Frank");
target.setSpouse(target);
AbstractPropertyAccessor accessor = createAccessor(target);
assertTrue("name is not null to start off", target.getName() != null);
accessor.setPropertyValue("name", null);
assertTrue("name is now null", target.getName() == null);
// now test with non-string
assertTrue("spouse is not null to start off", target.getSpouse() != null);
accessor.setPropertyValue("spouse", null);
assertTrue("spouse is now null", target.getSpouse() == null);
}
use of org.springframework.tests.sample.beans.TestBean 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.TestBean 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());
}
use of org.springframework.tests.sample.beans.TestBean in project spring-framework by spring-projects.
the class AbstractPropertyAccessorTests method getAnotherNestedDeepProperty.
@Test
public void getAnotherNestedDeepProperty() {
ITestBean target = new TestBean("rod", 31);
ITestBean kerry = new TestBean("kerry", 35);
target.setSpouse(kerry);
kerry.setSpouse(target);
AbstractPropertyAccessor accessor = createAccessor(target);
Integer KA = (Integer) accessor.getPropertyValue("spouse.age");
assertTrue("kerry is 35", KA == 35);
Integer RA = (Integer) accessor.getPropertyValue("spouse.spouse.age");
assertTrue("rod is 31, not" + RA, RA == 31);
ITestBean spousesSpouse = (ITestBean) accessor.getPropertyValue("spouse.spouse");
assertTrue("spousesSpouse = initial point", target == spousesSpouse);
}
Aggregations