@Test public void spanElementCustomizable() throws Exception { this.tag.setPath("stringArray"); this.tag.setItems(new Object[] {"foo", "bar", "baz"}); this.tag.setElement("element"); int result = this.tag.doStartTag(); assertEquals(Tag.SKIP_BODY, result); String output = getOutput(); // wrap the output so it is valid XML output = "<doc>" + output + "</doc>"; SAXReader reader = new SAXReader(); Document document = reader.read(new StringReader(output)); Element spanElement = (Element) document.getRootElement().elements().get(0); assertEquals("element", spanElement.getName()); }
@Test public void withNullValue() throws Exception { try { this.tag.setPath("name"); this.tag.doStartTag(); fail("Should not be able to render with a null value when binding to a non-boolean."); } catch (IllegalArgumentException ex) { // success } }
@Test public void hiddenElementOmittedOnDisabled() throws Exception { this.tag.setPath("stringArray"); this.tag.setItems(new Object[] {"foo", "bar", "baz"}); this.tag.setDisabled(true); int result = this.tag.doStartTag(); assertEquals(Tag.SKIP_BODY, result); String output = getOutput(); // wrap the output so it is valid XML output = "<doc>" + output + "</doc>"; SAXReader reader = new SAXReader(); Document document = reader.read(new StringReader(output)); Element rootElement = document.getRootElement(); assertEquals("Both tag and hidden element rendered incorrectly", 3, rootElement.elements().size()); Element spanElement = (Element) document.getRootElement().elements().get(0); Element radioButtonElement = (Element) spanElement.elements().get(0); assertEquals("input", radioButtonElement.getName()); assertEquals("radio", radioButtonElement.attribute("type").getValue()); assertEquals("stringArray", radioButtonElement.attribute("name").getValue()); assertEquals("checked", radioButtonElement.attribute("checked").getValue()); assertEquals("disabled", radioButtonElement.attribute("disabled").getValue()); assertEquals("foo", radioButtonElement.attribute("value").getValue()); }
@Test public void withoutItemsEnumBindTarget() throws Exception { BeanWithEnum testBean = new BeanWithEnum(); testBean.setTestEnum(TestEnum.VALUE_2); getPageContext().getRequest().setAttribute("testBean", testBean); this.tag.setPath("testEnum"); int result = this.tag.doStartTag(); assertEquals(Tag.SKIP_BODY, result); String output = "<div>" + getOutput() + "</div>"; SAXReader reader = new SAXReader(); Document document = reader.read(new StringReader(output)); Element rootElement = document.getRootElement(); assertEquals(2, rootElement.elements().size()); Node value1 = rootElement.selectSingleNode("//input[@value = 'VALUE_1']"); Node value2 = rootElement.selectSingleNode("//input[@value = 'VALUE_2']"); assertEquals("TestEnum: VALUE_1", rootElement.selectSingleNode("//label[@for = '" + value1.valueOf("@id") + "']").getText()); assertEquals("TestEnum: VALUE_2", rootElement.selectSingleNode("//label[@for = '" + value2.valueOf("@id") + "']").getText()); assertEquals(value2, rootElement.selectSingleNode("//input[@checked]")); }
@Test public void withMultiValueMap() throws Exception { this.tag.setPath("stringArray"); Map m = new LinkedHashMap(); m.put("foo", "FOO");
@Test public void withMultiValueArrayWithDelimiter() throws Exception { this.tag.setDelimiter("<br/>"); this.tag.setPath("stringArray"); this.tag.setItems(new Object[] {"foo", "bar", "baz"}); int result = this.tag.doStartTag();
String delimiter = " | "; this.tag.setDelimiter(delimiter); this.tag.setPath("stringArray"); Map m = new LinkedHashMap(); m.put("foo", "FOO");
@Test public void withMultiValueArray() throws Exception { this.tag.setPath("stringArray"); this.tag.setItems(new Object[] {"foo", "bar", "baz"}); int result = this.tag.doStartTag();
@Test public void withoutItemsEnumBindTargetWithExplicitLabelsAndValues() throws Exception { BeanWithEnum testBean = new BeanWithEnum(); testBean.setTestEnum(TestEnum.VALUE_2); getPageContext().getRequest().setAttribute("testBean", testBean); this.tag.setPath("testEnum"); this.tag.setItemLabel("enumLabel"); this.tag.setItemValue("enumValue"); int result = this.tag.doStartTag(); assertEquals(Tag.SKIP_BODY, result); String output = "<div>" + getOutput() + "</div>"; SAXReader reader = new SAXReader(); Document document = reader.read(new StringReader(output)); Element rootElement = document.getRootElement(); assertEquals(2, rootElement.elements().size()); Node value1 = rootElement.selectSingleNode("//input[@value = 'Value: VALUE_1']"); Node value2 = rootElement.selectSingleNode("//input[@value = 'Value: VALUE_2']"); assertEquals("Label: VALUE_1", rootElement.selectSingleNode("//label[@for = '" + value1.valueOf("@id") + "']").getText()); assertEquals("Label: VALUE_2", rootElement.selectSingleNode("//label[@for = '" + value2.valueOf("@id") + "']").getText()); assertEquals(value2, rootElement.selectSingleNode("//input[@checked]")); }
@Test public void withMultiValueWithEditor() throws Exception { this.tag.setPath("stringArray"); this.tag.setItems(new Object[] {" foo", " bar", " baz"}); BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(this.bean, COMMAND_NAME);
@Test public void collectionOfPets() throws Exception { this.tag.setPath("pets"); List allPets = new ArrayList(); allPets.add(new ItemPet("Rudiger"));
@Test public void collectionOfPetsWithEditor() throws Exception { this.tag.setPath("pets"); List allPets = new ArrayList(); allPets.add(new ItemPet("Rudiger"));
String dynamicAttribute2 = "attr2"; this.tag.setPath("stringArray"); this.tag.setItems(new Object[] {"foo", "bar", "baz"}); this.tag.setDynamicAttribute(null, dynamicAttribute1, dynamicAttribute1);