@Test public void withFloatCustom() throws Exception { PropertyEditor propertyEditor = new SimpleFloatEditor(); BeanPropertyBindingResult errors = new BeanPropertyBindingResult(getTestBean(), COMMAND_NAME); errors.getPropertyAccessor().registerCustomEditor(Float.class, propertyEditor); exposeBindingResult(errors); this.tag.setPath("myFloat"); Float[] array = new Float[] { new Float("12.30"), new Float("12.32"), new Float("12.34"), new Float("12.36"), new Float("12.38"), new Float("12.40"), new Float("12.42"), new Float("12.44"), new Float("12.46"), new Float("12.48") }; this.tag.setItems(array); int result = this.tag.doStartTag(); assertEquals(Tag.SKIP_BODY, result); String output = getOutput(); assertTrue(output.startsWith("<select ")); assertTrue(output.endsWith("</select>")); SAXReader reader = new SAXReader(); Document document = reader.read(new StringReader(output)); Element rootElement = document.getRootElement(); assertEquals("select", rootElement.getName()); assertEquals("myFloat", rootElement.attribute("name").getValue()); List children = rootElement.elements(); assertEquals("Incorrect number of children", array.length, children.size()); Element e = (Element) rootElement.selectSingleNode("option[text() = '12.34f']"); assertEquals("'12.34' node not selected", "selected", e.attribute("selected").getValue()); e = (Element) rootElement.selectSingleNode("option[text() = '12.32f']"); assertNull("'12.32' node incorrectly selected", e.attribute("selected")); }
errors.getPropertyAccessor().registerCustomEditor(Float.class, propertyEditor); exposeBindingResult(errors);
errors.getPropertyAccessor().registerCustomEditor(List.class, new CustomCollectionEditor(LinkedList.class) { @Override public String getAsText() {
@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 withListAndEditorAndNullValue() throws Exception { this.tag.setPath("realCountry"); this.tag.setItems(Country.getCountries()); this.tag.setItemValue("isoCode"); this.tag.setItemLabel("name"); TestBeanWithRealCountry testBean = (TestBeanWithRealCountry) getTestBean(); testBean.setRealCountry(null); 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() { Country value = (Country) getValue(); if (value==null) { return ""; } return value.getName(); } }); getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "testBean", bindingResult); this.tag.doStartTag(); String output = getOutput(); assertTrue(output.startsWith("<select ")); assertTrue(output.endsWith("</select>")); assertFalse(output.contains("selected=\"selected\"")); }
bindingResult.getPropertyAccessor().registerCustomEditor(Country.class, new PropertyEditorSupport() { @Override public void setAsText(final String text) throws IllegalArgumentException {
@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")); }
testBean.setBean(withCountry); BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(testBean , "testBean"); bindingResult.getPropertyAccessor().registerCustomEditor(Country.class, new PropertyEditorSupport() { @Override public void setAsText(String text) throws IllegalArgumentException {
@Test public void customBind() throws Exception { BeanPropertyBindingResult result = new BeanPropertyBindingResult(createTestBean(), "testBean"); result.getPropertyAccessor().registerCustomEditor(Float.class, new SimpleFloatEditor()); exposeBindingResult(result); this.tag.setPath("myFloat"); assertEquals(Tag.SKIP_BODY, this.tag.doStartTag()); String output = getOutput(); assertContainsAttribute(output, "name", "myFloat"); assertBlockTagContains(output, "12.34f"); }
@Test public void withCustomBinder() throws Exception { this.tag.setPath("myFloat"); BeanPropertyBindingResult errors = new BeanPropertyBindingResult(this.rob, COMMAND_NAME); errors.getPropertyAccessor().registerCustomEditor(Float.class, new SimpleFloatEditor()); exposeBindingResult(errors); assertEquals(Tag.SKIP_BODY, this.tag.doStartTag()); String output = getOutput(); assertTagOpened(output); assertTagClosed(output); assertContainsAttribute(output, "type", getType()); assertValueAttribute(output, "12.34f"); }
@Test public void withCustomBinder() throws Exception { this.tag.setPath("myFloat"); BeanPropertyBindingResult errors = new BeanPropertyBindingResult(this.bean, COMMAND_NAME); errors.getPropertyAccessor().registerCustomEditor(Float.class, new SimpleFloatEditor()); exposeBindingResult(errors); assertEquals(Tag.SKIP_BODY, this.tag.doStartTag()); String output = getOutput(); assertTagOpened(output); assertTagClosed(output); assertContainsAttribute(output, "type", "hidden"); assertContainsAttribute(output, "value", "12.34f"); }
@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 multiBind() throws Exception { BeanPropertyBindingResult result = new BeanPropertyBindingResult(new TestBean(), "testBean"); result.getPropertyAccessor().registerCustomEditor(TestBean.class, "friends", new FriendEditor()); exposeBindingResult(result); BindStatus bindStatus = new BindStatus(getRequestContext(), "testBean.friends", false); getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, bindStatus); this.tag.setValue(new TestBean("foo")); this.tag.doStartTag(); this.tag.doEndTag(); assertEquals("<option value=\"foo\">foo</option>", getOutput()); }