@Override public void setPropertyValues(PropertyValues pvs, boolean ignoreUnknown) throws BeansException { setPropertyValues(pvs, ignoreUnknown, false); }
@Override public void setPropertyValues(PropertyValues pvs) throws BeansException { setPropertyValues(pvs, false, false); }
@Override public void setPropertyValues(PropertyValues pvs, boolean ignoreUnknown) throws BeansException { setPropertyValues(pvs, ignoreUnknown, false); }
@Override public void setPropertyValues(PropertyValues pvs) throws BeansException { setPropertyValues(pvs, false, false); }
@Override public void setPropertyValues(Map<?, ?> map) throws BeansException { setPropertyValues(new MutablePropertyValues(map)); }
@Override public void setPropertyValues(Map<?, ?> map) throws BeansException { setPropertyValues(new MutablePropertyValues(map)); }
@Test public void setIndexedPropertyIgnored() { MutablePropertyValues values = new MutablePropertyValues(); values.add("toBeIgnored[0]", 42); AbstractPropertyAccessor accessor = createAccessor(new Object()); accessor.setPropertyValues(values, true); }
@Test public void setPropertyValuesIgnoresInvalidNestedOnRequest() { ITestBean target = new TestBean(); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.addPropertyValue(new PropertyValue("name", "rod")); pvs.addPropertyValue(new PropertyValue("graceful.rubbish", "tony")); pvs.addPropertyValue(new PropertyValue("more.garbage", new Object())); AbstractPropertyAccessor accessor = createAccessor(target); accessor.setPropertyValues(pvs, true); assertTrue("Set valid and ignored invalid", target.getName().equals("rod")); try { // Don't ignore: should fail accessor.setPropertyValues(pvs, false); fail("Shouldn't have ignored invalid updates"); } catch (NotWritablePropertyException ex) { // OK: but which exception?? } }
@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()); }
@SuppressWarnings("unchecked") // must work with raw map in this test @Test public void setRawMapPropertyWithNoEditorRegistered() { IndexedTestBean target = new IndexedTestBean(); AbstractPropertyAccessor accessor = createAccessor(target); Map inputMap = new HashMap(); inputMap.put(1, "rod"); inputMap.put(2, "rob"); ReadOnlyMap readOnlyMap = new ReadOnlyMap(inputMap); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("map", readOnlyMap); accessor.setPropertyValues(pvs); assertSame(readOnlyMap, target.getMap()); assertFalse(readOnlyMap.isAccessed()); }
@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()); }
@Test public void setValidPropertyValues() { TestBean target = new TestBean(); String newName = "tony"; int newAge = 65; String newTouchy = "valid"; try { AbstractPropertyAccessor accessor = createAccessor(target); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.addPropertyValue(new PropertyValue("age", newAge)); pvs.addPropertyValue(new PropertyValue("name", newName)); pvs.addPropertyValue(new PropertyValue("touchy", newTouchy)); accessor.setPropertyValues(pvs); assertTrue("Name property should have changed", target.getName().equals(newName)); assertTrue("Touchy property should have changed", target.getTouchy().equals(newTouchy)); assertTrue("Age property should have changed", target.getAge() == newAge); } catch (BeansException ex) { fail("Shouldn't throw exception when everything is valid"); } }
@Test public void setPropertyWithCustomEditor() { MutablePropertyValues values = new MutablePropertyValues(); values.add("name", Integer.class); TestBean target = new TestBean(); AbstractPropertyAccessor accessor = createAccessor(target); accessor.registerCustomEditor(String.class, new PropertyEditorSupport() { @Override public void setValue(Object value) { super.setValue(value.toString()); } }); accessor.setPropertyValues(values); assertEquals(Integer.class.toString(), target.getName()); }
@Test public void setPropertyWithPrimitiveConversion() { MutablePropertyValues values = new MutablePropertyValues(); values.add("name", 42); TestBean target = new TestBean(); AbstractPropertyAccessor accessor = createAccessor(target); accessor.setPropertyValues(values); assertEquals("42", target.getName()); }
public void setPropertyValues(PropertyValues pvs, boolean ignoreUnknown) throws BeansException { setPropertyValues(pvs, ignoreUnknown, false); }
@Test public void setEmptyPropertyValues() { TestBean target = new TestBean(); int age = 50; String name = "Tony"; target.setAge(age); target.setName(name); try { AbstractPropertyAccessor accessor = createAccessor(target); assertTrue("age is OK", target.getAge() == age); assertTrue("name is OK", name.equals(target.getName())); accessor.setPropertyValues(new MutablePropertyValues()); // Check its unchanged assertTrue("age is OK", target.getAge() == age); assertTrue("name is OK", name.equals(target.getName())); } catch (BeansException ex) { fail("Shouldn't throw exception when everything is valid"); } }
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.add("map[key2]", ""); try { accessor.setPropertyValues(pvs); fail("Should have thrown TypeMismatchException");
public void setPropertyValues(Map<?, ?> map) throws BeansException { setPropertyValues(new MutablePropertyValues(map)); }
pvs.add("map[key5]", tb4); pvs.add("map['key9']", tb5); accessor.setPropertyValues(pvs); assertEquals(tb5, target.getArray()[0]); assertEquals(tb4, target.getArray()[1]);
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());