private void assertStringArray() throws JspException, DocumentException { 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("name", rootElement.attribute("name").getValue()); List children = rootElement.elements(); assertEquals("Incorrect number of children", 4, children.size()); Element e = (Element) rootElement.selectSingleNode("option[text() = 'Rob']"); assertEquals("Rob node not selected", "selected", e.attribute("selected").getValue()); }
@Test public void withoutItems() throws Exception { this.tag.setItemValue("isoCode"); this.tag.setItemLabel("name"); this.selectTag.setPath("testBean"); this.selectTag.doStartTag(); int result = this.tag.doStartTag(); assertEquals(Tag.SKIP_BODY, result); this.tag.doEndTag(); this.selectTag.doEndTag(); String output = getOutput(); SAXReader reader = new SAXReader(); Document document = reader.read(new StringReader(output)); Element rootElement = document.getRootElement(); List children = rootElement.elements(); assertEquals("Incorrect number of children", 0, children.size()); }
@Test public void multipleExplicitlyFalse() throws Exception { this.tag.setPath("name"); this.tag.setItems(Country.getCountries()); this.tag.setItemValue("isoCode"); this.tag.setMultiple("false"); int result = this.tag.doStartTag(); assertEquals(Tag.SKIP_BODY, result); String output = getOutput(); output = "<doc>" + output + "</doc>"; SAXReader reader = new SAXReader(); Document document = reader.read(new StringReader(output)); Element rootElement = document.getRootElement(); assertEquals(1, rootElement.elements().size()); Element selectElement = rootElement.element("select"); assertEquals("select", selectElement.getName()); assertEquals("name", selectElement.attribute("name").getValue()); assertNull(selectElement.attribute("multiple")); List children = selectElement.elements(); assertEquals("Incorrect number of children", 4, children.size()); }
@Test public void multipleWithBooleanFalse() throws Exception { this.tag.setPath("name"); this.tag.setItems(Country.getCountries()); this.tag.setItemValue("isoCode"); this.tag.setMultiple(false); int result = this.tag.doStartTag(); assertEquals(Tag.SKIP_BODY, result); String output = getOutput(); output = "<doc>" + output + "</doc>"; SAXReader reader = new SAXReader(); Document document = reader.read(new StringReader(output)); Element rootElement = document.getRootElement(); assertEquals(1, rootElement.elements().size()); Element selectElement = rootElement.element("select"); assertEquals("select", selectElement.getName()); assertEquals("name", selectElement.attribute("name").getValue()); assertNull(selectElement.attribute("multiple")); List children = selectElement.elements(); assertEquals("Incorrect number of children", 4, children.size()); }
@Test public void multipleForCollection() throws Exception { this.bean.setSomeList(new ArrayList()); this.tag.setPath("someList"); this.tag.setItems(Country.getCountries()); this.tag.setItemValue("isoCode"); int result = this.tag.doStartTag(); assertEquals(Tag.SKIP_BODY, result); String output = getOutput(); output = "<doc>" + output + "</doc>"; SAXReader reader = new SAXReader(); Document document = reader.read(new StringReader(output)); Element rootElement = document.getRootElement(); assertEquals(2, rootElement.elements().size()); Element selectElement = rootElement.element("select"); assertEquals("select", selectElement.getName()); assertEquals("someList", selectElement.attribute("name").getValue()); assertEquals("multiple", selectElement.attribute("multiple").getValue()); List children = selectElement.elements(); assertEquals("Incorrect number of children", 4, children.size()); Element inputElement = rootElement.element("input"); assertNotNull(inputElement); }
@Test public void withMap() throws Exception { this.tag.setPath("sex"); this.tag.setItems(getSexes()); int result = this.tag.doStartTag(); assertEquals(Tag.SKIP_BODY, result); }
@Test public void multipleExplicitlyTrue() throws Exception { this.tag.setPath("name"); this.tag.setItems(Country.getCountries()); this.tag.setItemValue("isoCode"); this.tag.setMultiple("true"); int result = this.tag.doStartTag(); assertEquals(Tag.SKIP_BODY, result); String output = getOutput(); output = "<doc>" + output + "</doc>"; SAXReader reader = new SAXReader(); Document document = reader.read(new StringReader(output)); Element rootElement = document.getRootElement(); assertEquals(2, rootElement.elements().size()); Element selectElement = rootElement.element("select"); assertEquals("select", selectElement.getName()); assertEquals("name", selectElement.attribute("name").getValue()); assertEquals("multiple", selectElement.attribute("multiple").getValue()); List children = selectElement.elements(); assertEquals("Incorrect number of children", 4, children.size()); Element inputElement = rootElement.element("input"); assertNotNull(inputElement); }
@Test public void multipleWithBooleanTrue() throws Exception { this.tag.setPath("name"); this.tag.setItems(Country.getCountries()); this.tag.setItemValue("isoCode"); this.tag.setMultiple(true); int result = this.tag.doStartTag(); assertEquals(Tag.SKIP_BODY, result); String output = getOutput(); output = "<doc>" + output + "</doc>"; SAXReader reader = new SAXReader(); Document document = reader.read(new StringReader(output)); Element rootElement = document.getRootElement(); assertEquals(2, rootElement.elements().size()); Element selectElement = rootElement.element("select"); assertEquals("select", selectElement.getName()); assertEquals("name", selectElement.attribute("name").getValue()); assertEquals("multiple", selectElement.attribute("multiple").getValue()); List children = selectElement.elements(); assertEquals("Incorrect number of children", 4, children.size()); Element inputElement = rootElement.element("input"); assertNotNull(inputElement); }
@Test public void multipleWithStringValue() throws Exception { this.tag.setPath("name"); this.tag.setItems(Country.getCountries()); this.tag.setItemValue("isoCode"); this.tag.setMultiple("multiple"); int result = this.tag.doStartTag(); assertEquals(Tag.SKIP_BODY, result); String output = getOutput(); output = "<doc>" + output + "</doc>"; SAXReader reader = new SAXReader(); Document document = reader.read(new StringReader(output)); Element rootElement = document.getRootElement(); assertEquals(2, rootElement.elements().size()); Element selectElement = rootElement.element("select"); assertEquals("select", selectElement.getName()); assertEquals("name", selectElement.attribute("name").getValue()); assertEquals("multiple", selectElement.attribute("multiple").getValue()); List children = selectElement.elements(); assertEquals("Incorrect number of children", 4, children.size()); Element inputElement = rootElement.element("input"); assertNotNull(inputElement); }
@Test public void withInvalidList() throws Exception { this.tag.setPath("country"); this.tag.setItems(new TestBean()); this.tag.setItemValue("isoCode"); try { this.tag.doStartTag(); fail("Must not be able to use a non-Collection typed value as the value of 'items'"); } catch (JspException expected) { String message = expected.getMessage(); assertTrue(message.contains("items")); assertTrue(message.contains("org.springframework.tests.sample.beans.TestBean")); } }
@Test public void withoutItemsEnumParent() throws Exception { BeanWithEnum testBean = new BeanWithEnum(); testBean.setTestEnum(TestEnum.VALUE_2); getPageContext().getRequest().setAttribute("testBean", testBean); this.selectTag.setPath("testBean.testEnum"); this.selectTag.doStartTag(); int result = this.tag.doStartTag(); assertEquals(BodyTag.SKIP_BODY, result); result = this.tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, result); this.selectTag.doEndTag(); String output = getWriter().toString(); SAXReader reader = new SAXReader(); Document document = reader.read(new StringReader(output)); Element rootElement = document.getRootElement(); assertEquals(2, rootElement.elements().size()); Node value1 = rootElement.selectSingleNode("option[@value = 'VALUE_1']"); Node value2 = rootElement.selectSingleNode("option[@value = 'VALUE_2']"); assertEquals("TestEnum: VALUE_1", value1.getText()); assertEquals("TestEnum: VALUE_2", value2.getText()); assertEquals(value2, rootElement.selectSingleNode("option[@selected]")); }
private void assertList(boolean selected) throws JspException, DocumentException { this.tag.setItemValue("isoCode"); this.tag.setItemLabel("name"); this.tag.setSize("5"); int result = this.tag.doStartTag(); assertEquals(Tag.SKIP_BODY, result); String output = getOutput(); validateOutput(output, selected); assertContainsAttribute(output, "size", "5"); }
@Test public void nullItems() throws Exception { this.tag.setPath("country"); this.tag.setItems(null); this.tag.setItemValue("isoCode"); this.tag.setItemLabel("name"); int result = this.tag.doStartTag(); assertEquals(Tag.SKIP_BODY, result); String output = getOutput(); assertEquals("<select id=\"country\" name=\"country\"></select>", output); }
@Test public void withoutItemsEnumParentWithExplicitLabelsAndValues() throws Exception { BeanWithEnum testBean = new BeanWithEnum(); testBean.setTestEnum(TestEnum.VALUE_2); getPageContext().getRequest().setAttribute("testBean", testBean); this.selectTag.setPath("testBean.testEnum"); this.tag.setItemLabel("enumLabel"); this.tag.setItemValue("enumValue"); this.selectTag.doStartTag(); int result = this.tag.doStartTag(); assertEquals(BodyTag.SKIP_BODY, result); result = this.tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, result); this.selectTag.doEndTag(); String output = getWriter().toString(); SAXReader reader = new SAXReader(); Document document = reader.read(new StringReader(output)); Element rootElement = document.getRootElement(); assertEquals(2, rootElement.elements().size()); Node value1 = rootElement.selectSingleNode("option[@value = 'Value: VALUE_1']"); Node value2 = rootElement.selectSingleNode("option[@value = 'Value: VALUE_2']"); assertEquals("Label: VALUE_1", value1.getText()); assertEquals("Label: VALUE_2", value2.getText()); assertEquals(value2, rootElement.selectSingleNode("option[@selected]")); }
@Test public void emptyItems() throws Exception { this.tag.setPath("country"); this.tag.setItems(Collections.EMPTY_LIST); this.tag.setItemValue("isoCode"); this.tag.setItemLabel("name"); int result = this.tag.doStartTag(); assertEquals(Tag.SKIP_BODY, result); String output = getOutput(); assertEquals("<select id=\"country\" name=\"country\"></select>", output); }
@Test public void withListAndNoLabel() throws Exception { this.tag.setPath("country"); this.tag.setItems(Country.getCountries()); this.tag.setItemValue("isoCode"); int result = this.tag.doStartTag(); assertEquals(Tag.SKIP_BODY, result); validateOutput(getOutput(), true); }
@Test public void withNestedOptions() throws Exception { this.tag.setPath("country"); int result = this.tag.doStartTag(); assertEquals(Tag.EVAL_BODY_INCLUDE, result); BindStatus value = (BindStatus) getPageContext().getAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE); assertEquals("Selected country not exposed in page context", "UK", value.getValue()); result = this.tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, result); this.tag.doFinally(); String output = getOutput(); assertTrue(output.startsWith("<select ")); assertTrue(output.endsWith("</select>")); assertContainsAttribute(output, "name", "country"); }
@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 dynamicAttributes() throws JspException { String dynamicAttribute1 = "attr1"; String dynamicAttribute2 = "attr2"; this.tag.setPath("country"); this.tag.setItems(Collections.EMPTY_LIST); this.tag.setItemValue("isoCode"); this.tag.setItemLabel("name"); this.tag.setDynamicAttribute(null, dynamicAttribute1, dynamicAttribute1); this.tag.setDynamicAttribute(null, dynamicAttribute2, dynamicAttribute2); int result = this.tag.doStartTag(); assertEquals(Tag.SKIP_BODY, result); String output = getOutput(); assertContainsAttribute(output, dynamicAttribute1, dynamicAttribute1); assertContainsAttribute(output, dynamicAttribute2, dynamicAttribute2); }