@Override public PropertyEditor getPropertyEditor() { return new PropertyEditorSupport() { @Override public void setAsText(String text) throws IllegalArgumentException { if (text instanceof String) { try { entry.setMessage(!text.equals("") ? text : null); } catch (IOException ex) { History.LOG.log(Level.WARNING, null, ex); } return; } throw new java.lang.IllegalArgumentException(text); } @Override public String getAsText() { return te.getDisplayValue(); } }; }
@Test public void nestedBindWithPropertyEditor() { PortletRequestDataBinder binder = new PortletRequestDataBinder(bean); binder.registerCustomEditor(ITestBean.class, new PropertyEditorSupport() { @Override public void setAsText(String text) throws IllegalArgumentException { setValue(new TestBean(text)); } }); request.addParameter("spouse", "test"); request.addParameter("spouse.age", "32"); binder.bind(request); assertNotNull(bean.getSpouse()); assertEquals("test", bean.getSpouse().getName()); assertEquals(32, bean.getSpouse().getAge()); }
@Test public void testBindingWithNestedObjectCreation() throws Exception { TestBean tb = new TestBean(); DataBinder binder = new DataBinder(tb, "person"); binder.registerCustomEditor(ITestBean.class, new PropertyEditorSupport() { @Override public void setAsText(String text) throws IllegalArgumentException { setValue(new TestBean()); } }); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("spouse", "someValue"); pvs.add("spouse.name", "test"); binder.bind(pvs); assertNotNull(tb.getSpouse()); assertEquals("test", tb.getSpouse().getName()); }
@Test public void testCustomEditorForPrimitiveProperty() { TestBean tb = new TestBean(); DataBinder binder = new DataBinder(tb, "tb"); binder.registerCustomEditor(int.class, "age", new PropertyEditorSupport() { @Override public void setAsText(String text) throws IllegalArgumentException { setValue(new Integer(99)); } @Override public String getAsText() { return "argh"; } }); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("age", ""); binder.bind(pvs); assertEquals("argh", binder.getBindingResult().getFieldValue("age")); assertEquals(99, tb.getAge()); }
@Test public void testEditorForNestedIndexedField() { IndexedTestBean tb = new IndexedTestBean(); tb.getArray()[0].setNestedIndexedBean(new IndexedTestBean()); tb.getArray()[1].setNestedIndexedBean(new IndexedTestBean()); DataBinder binder = new DataBinder(tb, "tb"); binder.registerCustomEditor(String.class, "array.nestedIndexedBean.list.name", new PropertyEditorSupport() { @Override public void setAsText(String text) throws IllegalArgumentException { setValue("list" + text); } @Override public String getAsText() { return ((String) getValue()).substring(4); } }); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("array[0].nestedIndexedBean.list[0].name", "test1"); pvs.add("array[1].nestedIndexedBean.list[1].name", "test2"); binder.bind(pvs); assertEquals("listtest1", ((TestBean)tb.getArray()[0].getNestedIndexedBean().getList().get(0)).getName()); assertEquals("listtest2", ((TestBean)tb.getArray()[1].getNestedIndexedBean().getList().get(1)).getName()); assertEquals("test1", binder.getBindingResult().getFieldValue("array[0].nestedIndexedBean.list[0].name")); assertEquals("test2", binder.getBindingResult().getFieldValue("array[1].nestedIndexedBean.list[1].name")); }
@Test public void testSpecificEditorForNestedIndexedField() { IndexedTestBean tb = new IndexedTestBean(); tb.getArray()[0].setNestedIndexedBean(new IndexedTestBean()); tb.getArray()[1].setNestedIndexedBean(new IndexedTestBean()); DataBinder binder = new DataBinder(tb, "tb"); binder.registerCustomEditor(String.class, "array[0].nestedIndexedBean.list.name", new PropertyEditorSupport() { @Override public void setAsText(String text) throws IllegalArgumentException { setValue("list" + text); } @Override public String getAsText() { return ((String) getValue()).substring(4); } }); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("array[0].nestedIndexedBean.list[0].name", "test1"); pvs.add("array[1].nestedIndexedBean.list[1].name", "test2"); binder.bind(pvs); assertEquals("listtest1", ((TestBean)tb.getArray()[0].getNestedIndexedBean().getList().get(0)).getName()); assertEquals("test2", ((TestBean)tb.getArray()[1].getNestedIndexedBean().getList().get(1)).getName()); assertEquals("test1", binder.getBindingResult().getFieldValue("array[0].nestedIndexedBean.list[0].name")); assertEquals("test2", binder.getBindingResult().getFieldValue("array[1].nestedIndexedBean.list[1].name")); }
@Test public void testInnerSpecificEditorForNestedIndexedField() { IndexedTestBean tb = new IndexedTestBean(); tb.getArray()[0].setNestedIndexedBean(new IndexedTestBean()); tb.getArray()[1].setNestedIndexedBean(new IndexedTestBean()); DataBinder binder = new DataBinder(tb, "tb"); binder.registerCustomEditor(String.class, "array.nestedIndexedBean.list[0].name", new PropertyEditorSupport() { @Override public void setAsText(String text) throws IllegalArgumentException { setValue("list" + text); } @Override public String getAsText() { return ((String) getValue()).substring(4); } }); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("array[0].nestedIndexedBean.list[0].name", "test1"); pvs.add("array[1].nestedIndexedBean.list[1].name", "test2"); binder.bind(pvs); assertEquals("listtest1", ((TestBean)tb.getArray()[0].getNestedIndexedBean().getList().get(0)).getName()); assertEquals("test2", ((TestBean)tb.getArray()[1].getNestedIndexedBean().getList().get(1)).getName()); assertEquals("test1", binder.getBindingResult().getFieldValue("array[0].nestedIndexedBean.list[0].name")); assertEquals("test2", binder.getBindingResult().getFieldValue("array[1].nestedIndexedBean.list[1].name")); }
@Test public void testBindToStringArrayWithArrayEditor() { TestBean tb = new TestBean(); DataBinder binder = new DataBinder(tb, "tb"); binder.registerCustomEditor(String[].class, "stringArray", new PropertyEditorSupport() { @Override public void setAsText(String text) throws IllegalArgumentException { setValue(StringUtils.delimitedListToStringArray(text, "-")); } }); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("stringArray", "a1-b2"); binder.bind(pvs); assertTrue(!binder.getBindingResult().hasErrors()); assertEquals(2, tb.getStringArray().length); assertEquals("a1", tb.getStringArray()[0]); assertEquals("b2", tb.getStringArray()[1]); }
@Test public void testBindToStringArrayWithComponentEditor() { TestBean tb = new TestBean(); DataBinder binder = new DataBinder(tb, "tb"); binder.registerCustomEditor(String.class, "stringArray", new PropertyEditorSupport() { @Override public void setAsText(String text) throws IllegalArgumentException { setValue("X" + text); } }); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("stringArray", new String[] {"a1", "b2"}); binder.bind(pvs); assertTrue(!binder.getBindingResult().hasErrors()); assertEquals(2, tb.getStringArray().length); assertEquals("Xa1", tb.getStringArray()[0]); assertEquals("Xb2", tb.getStringArray()[1]); }
@Test public void withListAndTransformTagAndEditor() throws Exception { this.tag.setPath("realCountry"); this.tag.setItems(Country.getCountries()); BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(getTestBean(), "testBean"); bindingResult.getPropertyAccessor().registerCustomEditor(Country.class, new PropertyEditorSupport() { @Override public void setAsText(String text) throws IllegalArgumentException { setValue(Country.getCountryWithIsoCode(text)); } @Override public String getAsText() { return ((Country) getValue()).getName(); } }); getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "testBean", bindingResult); this.tag.doStartTag(); TransformTag transformTag = new TransformTag(); transformTag.setValue(Country.getCountries().get(0)); transformTag.setVar("key"); transformTag.setParent(this.tag); transformTag.setPageContext(getPageContext()); transformTag.doStartTag(); assertEquals("Austria", getPageContext().findAttribute("key")); }
@Test public void withListAndEditor() throws Exception { this.tag.setPath("realCountry"); this.tag.setItems(Country.getCountries()); this.tag.setItemValue("isoCode"); this.tag.setItemLabel("name"); BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(getTestBean(), "testBean"); bindingResult.getPropertyAccessor().registerCustomEditor(Country.class, new PropertyEditorSupport() { @Override public void setAsText(String text) throws IllegalArgumentException { setValue(Country.getCountryWithIsoCode(text)); } @Override public String getAsText() { return ((Country) getValue()).getName(); } }); getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "testBean", bindingResult); this.tag.doStartTag(); String output = getOutput(); assertTrue(output.startsWith("<select ")); assertTrue(output.endsWith("</select>")); assertTrue(output.contains("option value=\"AT\" selected=\"selected\">Austria")); }
@Test public void nestedPathWithListAndEditor() throws Exception { this.tag.setPath("bean.realCountry"); this.tag.setItems(Country.getCountries()); this.tag.setItemValue("isoCode"); this.tag.setItemLabel("name"); TestBeanWrapper testBean = new TestBeanWrapper(); testBean.setBean(getTestBean()); BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(testBean , "testBean"); bindingResult.getPropertyAccessor().registerCustomEditor(Country.class, new PropertyEditorSupport() { @Override public void setAsText(String text) throws IllegalArgumentException { setValue(Country.getCountryWithIsoCode(text)); } @Override public String getAsText() { return ((Country) getValue()).getName(); } }); getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "testBean", bindingResult); this.tag.doStartTag(); String output = getOutput(); assertTrue(output.startsWith("<select ")); assertTrue(output.endsWith("</select>")); assertTrue(output.contains("option value=\"AT\" selected=\"selected\">Austria")); }
@Test public void testBindingWithNestedObjectCreation() throws Exception { TestBean tb = new TestBean(); WebRequestDataBinder binder = new WebRequestDataBinder(tb, "person"); binder.registerCustomEditor(ITestBean.class, new PropertyEditorSupport() { @Override public void setAsText(String text) throws IllegalArgumentException { setValue(new TestBean()); } }); MockHttpServletRequest request = new MockHttpServletRequest(); request.addParameter("spouse", "someValue"); request.addParameter("spouse.name", "test"); binder.bind(new ServletWebRequest(request)); assertNotNull(tb.getSpouse()); assertEquals("test", tb.getSpouse().getName()); }
@Test public void testBindingWithNestedObjectCreation() throws Exception { TestBean tb = new TestBean(); ServletRequestDataBinder binder = new ServletRequestDataBinder(tb, "person"); binder.registerCustomEditor(ITestBean.class, new PropertyEditorSupport() { @Override public void setAsText(String text) throws IllegalArgumentException { setValue(new TestBean()); } }); MockHttpServletRequest request = new MockHttpServletRequest(); request.addParameter("spouse", "someValue"); request.addParameter("spouse.name", "test"); binder.bind(request); assertNotNull(tb.getSpouse()); assertEquals("test", tb.getSpouse().getName()); }
@Test public void testBindingWithNestedObjectCreationAndWrongOrder() throws Exception { TestBean tb = new TestBean(); ServletRequestDataBinder binder = new ServletRequestDataBinder(tb, "person"); binder.registerCustomEditor(ITestBean.class, new PropertyEditorSupport() { @Override public void setAsText(String text) throws IllegalArgumentException { setValue(new TestBean()); } }); MockHttpServletRequest request = new MockHttpServletRequest(); request.addParameter("spouse.name", "test"); request.addParameter("spouse", "someValue"); binder.bind(request); assertNotNull(tb.getSpouse()); assertEquals("test", tb.getSpouse().getName()); }
@Test public void testCustomEditorForSingleProperty() { TestBean tb = new TestBean(); BeanWrapper bw = new BeanWrapperImpl(tb); bw.registerCustomEditor(String.class, "name", new PropertyEditorSupport() { @Override public void setAsText(String text) throws IllegalArgumentException { setValue("prefix" + text); } }); bw.setPropertyValue("name", "value"); bw.setPropertyValue("touchy", "value"); assertEquals("prefixvalue", bw.getPropertyValue("name")); assertEquals("prefixvalue", tb.getName()); assertEquals("value", bw.getPropertyValue("touchy")); assertEquals("value", tb.getTouchy()); }
@Test public void testCustomEditorForAllStringProperties() { TestBean tb = new TestBean(); BeanWrapper bw = new BeanWrapperImpl(tb); bw.registerCustomEditor(String.class, new PropertyEditorSupport() { @Override public void setAsText(String text) throws IllegalArgumentException { setValue("prefix" + text); } }); bw.setPropertyValue("name", "value"); bw.setPropertyValue("touchy", "value"); assertEquals("prefixvalue", bw.getPropertyValue("name")); assertEquals("prefixvalue", tb.getName()); assertEquals("prefixvalue", bw.getPropertyValue("touchy")); assertEquals("prefixvalue", tb.getTouchy()); }
@Test public void testCustomEditorForSingleNestedProperty() { TestBean tb = new TestBean(); tb.setSpouse(new TestBean()); BeanWrapper bw = new BeanWrapperImpl(tb); bw.registerCustomEditor(String.class, "spouse.name", new PropertyEditorSupport() { @Override public void setAsText(String text) throws IllegalArgumentException { setValue("prefix" + text); } }); bw.setPropertyValue("spouse.name", "value"); bw.setPropertyValue("touchy", "value"); assertEquals("prefixvalue", bw.getPropertyValue("spouse.name")); assertEquals("prefixvalue", tb.getSpouse().getName()); assertEquals("value", bw.getPropertyValue("touchy")); assertEquals("value", tb.getTouchy()); }
@Test public void testCustomEditorForAllNestedStringProperties() { TestBean tb = new TestBean(); tb.setSpouse(new TestBean()); BeanWrapper bw = new BeanWrapperImpl(tb); bw.registerCustomEditor(String.class, new PropertyEditorSupport() { @Override public void setAsText(String text) throws IllegalArgumentException { setValue("prefix" + text); } }); bw.setPropertyValue("spouse.name", "value"); bw.setPropertyValue("touchy", "value"); assertEquals("prefixvalue", bw.getPropertyValue("spouse.name")); assertEquals("prefixvalue", tb.getSpouse().getName()); assertEquals("prefixvalue", bw.getPropertyValue("touchy")); assertEquals("prefixvalue", tb.getTouchy()); }
@Test public void testIndexedPropertiesWithListPropertyEditor() { IndexedTestBean bean = new IndexedTestBean(); BeanWrapper bw = new BeanWrapperImpl(bean); bw.registerCustomEditor(List.class, "list", new PropertyEditorSupport() { @Override public void setAsText(String text) throws IllegalArgumentException { List<TestBean> result = new ArrayList<TestBean>(); result.add(new TestBean("list" + text, 99)); setValue(result); } }); bw.setPropertyValue("list", "1"); assertEquals("list1", ((TestBean) bean.getList().get(0)).getName()); bw.setPropertyValue("list[0]", "test"); assertEquals("test", bean.getList().get(0)); }
@Test public void setStringPropertyWithCustomEditor() throws Exception { TestBean target = new TestBean(); AbstractPropertyAccessor accessor = createAccessor(target); accessor.registerCustomEditor(String.class, "name", new PropertyEditorSupport() { @Override public void setValue(Object value) { if (value instanceof String[]) { setValue(StringUtils.arrayToDelimitedString(((String[]) value), "-")); } else { super.setValue(value != null ? value : ""); } } }); accessor.setPropertyValue("name", new String[] {}); assertEquals("", target.getName()); accessor.setPropertyValue("name", new String[] {"a1", "b2"}); assertEquals("a1-b2", target.getName()); accessor.setPropertyValue("name", null); assertEquals("", target.getName()); }
@Test public void setPrimitiveArrayPropertyLargeMatchingWithSpecificEditor() { PrimitiveArrayBean target = new PrimitiveArrayBean(); AbstractPropertyAccessor accessor = createAccessor(target); accessor.registerCustomEditor(int.class, "array", new PropertyEditorSupport() { @Override public void setValue(Object value) { if (value instanceof Integer) { super.setValue(new Integer((Integer) value + 1)); } } }); int[] input = new int[1024]; accessor.setPropertyValue("array", input); assertEquals(1024, target.getArray().length); assertEquals(1, target.getArray()[0]); assertEquals(1, target.getArray()[1]); }
@Test public void setPrimitiveArrayPropertyLargeMatchingWithIndexSpecificEditor() { PrimitiveArrayBean target = new PrimitiveArrayBean(); AbstractPropertyAccessor accessor = createAccessor(target); accessor.registerCustomEditor(int.class, "array[1]", new PropertyEditorSupport() { @Override public void setValue(Object value) { if (value instanceof Integer) { super.setValue(new Integer((Integer) value + 1)); } } }); int[] input = new int[1024]; accessor.setPropertyValue("array", input); assertEquals(1024, target.getArray().length); assertEquals(0, target.getArray()[0]); assertEquals(1, target.getArray()[1]); }
@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<Integer, String>(); 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()); }
@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<Object, Object>(); 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()); }
/** * 初始化数据绑定 * 1. 将所有传递进来的String进行HTML编码,防止XSS攻击 * 2. 将字段中Date类型转换为String类型 */ @InitBinder protected void initBinder(WebDataBinder binder) { // String类型转换,将所有传递进来的String进行HTML编码,防止XSS攻击 binder.registerCustomEditor(String.class, new PropertyEditorSupport() { public String getAsText() { Object value = getValue(); return value != null ? value.toString() : ""; } public void setAsText(String text) { setValue(text == null ? null : StringEscapeUtils.escapeHtml4(text.trim())); } }); /* * // Date 类型转换 binder.registerCustomEditor(Date.class, new PropertyEditorSupport() { public void * setAsText(String text) { setValue(DateUtils.parseDate(text)); } }); */ }
/** * 初始化数据绑定 * 1. 将所有传递进来的String进行HTML编码,防止XSS攻击 * 2. 将字段中Date类型转换为String类型 */ @InitBinder protected void initBinder(WebDataBinder binder) { // String类型转换,将所有传递进来的String进行HTML编码,防止XSS攻击 /* binder.registerCustomEditor(String.class, new PropertyEditorSupport() { @Override public void setAsText(String text) { setValue(text == null ? null : StringEscapeUtils.escapeHtml4(text.trim())); } @Override public String getAsText() { Object value = getValue(); return value != null ? value.toString() : ""; } });*/ // Date 类型转换 binder.registerCustomEditor(Date.class, new PropertyEditorSupport() { @Override public void setAsText(String text) { setValue(DateFPUtils.parseDateMultiplePattern(text)); } }); }
@Override public PropertyEditor getPropertyEditor() { return new PropertyEditorSupport(spatial.getUserData(name)) { @Override public boolean supportsCustomEditor() { return true; } @Override public Component getCustomEditor() { return new UserDataDialog(new JFrame(), true, node, name); } }; // return new AnimationPropertyEditor(control); }
/** * 初始化参数绑定 * 将所有传递进来的String进行HTML编码,防止XSS攻击 */ @InitBinder protected void initBinder(WebDataBinder binder){ // String类型转换,将所有传递进来的String进行HTML编码,防止XSS攻击 binder.registerCustomEditor(String.class, new PropertyEditorSupport() { @Override public String getAsText(){ Object value = getValue(); return value!=null?value.toString():""; } @Override public void setAsText(String text) throws IllegalArgumentException { setValue(text == null ? null : StringEscapeUtils.escapeHtml4(text.trim())); } }); }
public EditorDialog(Frame owner, PropertyEditorSupport editor) { super(owner, true); propertyEditor = getClonedEditor(editor); setCanceled(false); Container pane = getContentPane(); pane.setLayout(new BorderLayout()); OnClick onClick = new OnClick(); bOK = new JButton(getEditorLocaleString("OK")); bOK.addActionListener(onClick); bCancel = new JButton(getEditorLocaleString("Cancel")); bCancel.addActionListener(onClick); bPane = new JPanel(new FlowLayout(FlowLayout.RIGHT)); bPane.add(bOK); bPane.add(bCancel); pane.add(bPane, BorderLayout.SOUTH); tryToCreateEditorPanel(false); pack(); }
@InitBinder public void initBinder(WebDataBinder dataBinder) { dataBinder.registerCustomEditor(ContactTopicType.class, new PropertyEditorSupport() { @Override public void setAsText(String value) throws IllegalArgumentException { setValue(ContactTopicType.valueOf(value)); } @Override public String getAsText() { if(getValue() == null) return ""; return ((ContactTopicType) getValue()).name(); } }); }
private void createSocketProperty(AbstractSocket socket, Sheet.Set sheetSet) { try { String connecteeType = socket.getConnecteeTypeName(); String connectionName = socket.getName(); PropertySupport.Reflection nextNodeProp = new PropertySupport.Reflection(new ConnectionEditor(socket, this), String.class, "getConnectedToName", "setConnectedToName"); nextNodeProp.setValue("canEditAsText", Boolean.TRUE); nextNodeProp.setValue("suppressCustomEditor", Boolean.TRUE); nextNodeProp.setName(connecteeType + ":" + connectionName); PropertyEditorSupport editor = EditorRegistry.getEditor(connecteeType); if (editor != null) nextNodeProp.setPropertyEditorClass(editor.getClass()); sheetSet.put(nextNodeProp); } catch (NoSuchMethodException ex) { Exceptions.printStackTrace(ex); } }
public void testNestedBindWithPropertyEditor() { TestBean bean = new TestBean(); PortletRequestDataBinder binder = new PortletRequestDataBinder(bean); binder.registerCustomEditor(ITestBean.class, new PropertyEditorSupport() { @Override public void setAsText(String text) throws IllegalArgumentException { setValue(new TestBean(text)); } }); MockPortletRequest request = new MockPortletRequest(); request.addParameter("spouse", "test"); request.addParameter("spouse.age", "32"); binder.bind(request); assertNotNull(bean.getSpouse()); assertEquals("test", bean.getSpouse().getName()); assertEquals(32, bean.getSpouse().getAge()); }
public void testBindingWithNestedObjectCreation() throws Exception { TestBean tb = new TestBean(); DataBinder binder = new DataBinder(tb, "person"); binder.registerCustomEditor(ITestBean.class, new PropertyEditorSupport() { @Override public void setAsText(String text) throws IllegalArgumentException { setValue(new TestBean()); } }); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("spouse", "someValue"); pvs.add("spouse.name", "test"); binder.bind(pvs); assertNotNull(tb.getSpouse()); assertEquals("test", tb.getSpouse().getName()); }
public void testCustomEditorForPrimitiveProperty() { TestBean tb = new TestBean(); DataBinder binder = new DataBinder(tb, "tb"); binder.registerCustomEditor(int.class, "age", new PropertyEditorSupport() { @Override public void setAsText(String text) throws IllegalArgumentException { setValue(new Integer(99)); } @Override public String getAsText() { return "argh"; } }); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("age", ""); binder.bind(pvs); assertEquals("argh", binder.getBindingResult().getFieldValue("age")); assertEquals(99, tb.getAge()); }
public void testCustomEditorWithOldValueAccess() { TestBean tb = new TestBean(); DataBinder binder = new DataBinder(tb, "tb"); binder.registerCustomEditor(String.class, null, new PropertyEditorSupport() { @Override public void setAsText(String text) throws IllegalArgumentException { if (getValue() == null || !text.equalsIgnoreCase(getValue().toString())) { setValue(text); } } }); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("name", "value"); binder.bind(pvs); assertEquals("value", tb.getName()); pvs = new MutablePropertyValues(); pvs.add("name", "vaLue"); binder.bind(pvs); assertEquals("value", tb.getName()); }
public void testEditorForNestedIndexedField() { IndexedTestBean tb = new IndexedTestBean(); tb.getArray()[0].setNestedIndexedBean(new IndexedTestBean()); tb.getArray()[1].setNestedIndexedBean(new IndexedTestBean()); DataBinder binder = new DataBinder(tb, "tb"); binder.registerCustomEditor(String.class, "array.nestedIndexedBean.list.name", new PropertyEditorSupport() { @Override public void setAsText(String text) throws IllegalArgumentException { setValue("list" + text); } @Override public String getAsText() { return ((String) getValue()).substring(4); } }); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("array[0].nestedIndexedBean.list[0].name", "test1"); pvs.add("array[1].nestedIndexedBean.list[1].name", "test2"); binder.bind(pvs); assertEquals("listtest1", ((TestBean)tb.getArray()[0].getNestedIndexedBean().getList().get(0)).getName()); assertEquals("listtest2", ((TestBean)tb.getArray()[1].getNestedIndexedBean().getList().get(1)).getName()); assertEquals("test1", binder.getBindingResult().getFieldValue("array[0].nestedIndexedBean.list[0].name")); assertEquals("test2", binder.getBindingResult().getFieldValue("array[1].nestedIndexedBean.list[1].name")); }
public void testSpecificEditorForNestedIndexedField() { IndexedTestBean tb = new IndexedTestBean(); tb.getArray()[0].setNestedIndexedBean(new IndexedTestBean()); tb.getArray()[1].setNestedIndexedBean(new IndexedTestBean()); DataBinder binder = new DataBinder(tb, "tb"); binder.registerCustomEditor(String.class, "array[0].nestedIndexedBean.list.name", new PropertyEditorSupport() { @Override public void setAsText(String text) throws IllegalArgumentException { setValue("list" + text); } @Override public String getAsText() { return ((String) getValue()).substring(4); } }); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("array[0].nestedIndexedBean.list[0].name", "test1"); pvs.add("array[1].nestedIndexedBean.list[1].name", "test2"); binder.bind(pvs); assertEquals("listtest1", ((TestBean)tb.getArray()[0].getNestedIndexedBean().getList().get(0)).getName()); assertEquals("test2", ((TestBean)tb.getArray()[1].getNestedIndexedBean().getList().get(1)).getName()); assertEquals("test1", binder.getBindingResult().getFieldValue("array[0].nestedIndexedBean.list[0].name")); assertEquals("test2", binder.getBindingResult().getFieldValue("array[1].nestedIndexedBean.list[1].name")); }
public void testInnerSpecificEditorForNestedIndexedField() { IndexedTestBean tb = new IndexedTestBean(); tb.getArray()[0].setNestedIndexedBean(new IndexedTestBean()); tb.getArray()[1].setNestedIndexedBean(new IndexedTestBean()); DataBinder binder = new DataBinder(tb, "tb"); binder.registerCustomEditor(String.class, "array.nestedIndexedBean.list[0].name", new PropertyEditorSupport() { @Override public void setAsText(String text) throws IllegalArgumentException { setValue("list" + text); } @Override public String getAsText() { return ((String) getValue()).substring(4); } }); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("array[0].nestedIndexedBean.list[0].name", "test1"); pvs.add("array[1].nestedIndexedBean.list[1].name", "test2"); binder.bind(pvs); assertEquals("listtest1", ((TestBean)tb.getArray()[0].getNestedIndexedBean().getList().get(0)).getName()); assertEquals("test2", ((TestBean)tb.getArray()[1].getNestedIndexedBean().getList().get(1)).getName()); assertEquals("test1", binder.getBindingResult().getFieldValue("array[0].nestedIndexedBean.list[0].name")); assertEquals("test2", binder.getBindingResult().getFieldValue("array[1].nestedIndexedBean.list[1].name")); }
public void testBindToStringArrayWithArrayEditor() { TestBean tb = new TestBean(); DataBinder binder = new DataBinder(tb, "tb"); binder.registerCustomEditor(String[].class, "stringArray", new PropertyEditorSupport() { @Override public void setAsText(String text) throws IllegalArgumentException { setValue(StringUtils.delimitedListToStringArray(text, "-")); } }); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("stringArray", "a1-b2"); binder.bind(pvs); assertTrue(!binder.getBindingResult().hasErrors()); assertEquals(2, tb.getStringArray().length); assertEquals("a1", tb.getStringArray()[0]); assertEquals("b2", tb.getStringArray()[1]); }