@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 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 dynamicTypeAttribute() throws JspException { try { this.tag.setDynamicAttribute(null, "type", "email"); fail("Expected exception"); } catch (IllegalArgumentException e) { assertEquals("Attribute type=\"email\" is not allowed", e.getMessage()); } }
@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 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 withMultiValueMapWithDelimiter() throws Exception { String delimiter = " | "; this.tag.setDelimiter(delimiter); this.tag.setPath("stringArray"); Map m = new LinkedHashMap(); m.put("foo", "FOO"); m.put("bar", "BAR"); m.put("baz", "BAZ"); this.tag.setItems(m); int result = this.tag.doStartTag(); assertEquals(Tag.SKIP_BODY, result);
@Test public void collectionOfPetsWithEditor() throws Exception { this.tag.setPath("pets"); List allPets = new ArrayList(); allPets.add(new ItemPet("Rudiger")); allPets.add(new ItemPet("Fluffy")); allPets.add(new ItemPet("Mufty")); this.tag.setItems(allPets); this.tag.setItemLabel("label"); this.tag.setId("myId"); getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + COMMAND_NAME, bindingResult); int result = this.tag.doStartTag(); assertEquals(Tag.SKIP_BODY, result);
@Test public void withMultiValueMap() throws Exception { this.tag.setPath("stringArray"); Map m = new LinkedHashMap(); m.put("foo", "FOO"); m.put("bar", "BAR"); m.put("baz", "BAZ"); this.tag.setItems(m); int result = this.tag.doStartTag(); assertEquals(Tag.SKIP_BODY, result);
String dynamicAttribute2 = "attr2"; this.tag.setPath("stringArray"); this.tag.setItems(new Object[] {"foo", "bar", "baz"}); this.tag.setDynamicAttribute(null, dynamicAttribute1, dynamicAttribute1); this.tag.setDynamicAttribute(null, dynamicAttribute2, dynamicAttribute2); int result = this.tag.doStartTag(); assertEquals(Tag.SKIP_BODY, result);
@Override public void doFinally() { super.doFinally(); this.bindStatus = null; } }
@Override protected String getItemLabel() { if (!StringUtils.isEmpty(dict)) { return "label"; } return super.getItemLabel(); }
@Override protected String getItemValue() { if (!StringUtils.isEmpty(dict)) { return "value"; } return super.getItemValue(); }
@Override protected Object getItems() { if (!StringUtils.isEmpty(dict)) { return DictUtils.getDictList(dict); } return super.getItems(); }
@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(); assertEquals(Tag.SKIP_BODY, result);
@Test public void withMultiValueArray() throws Exception { this.tag.setPath("stringArray"); this.tag.setItems(new Object[] {"foo", "bar", "baz"}); int result = this.tag.doStartTag(); assertEquals(Tag.SKIP_BODY, result);
@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 collectionOfPets() throws Exception { this.tag.setPath("pets"); List allPets = new ArrayList(); allPets.add(new ItemPet("Rudiger")); allPets.add(new ItemPet("Fluffy")); allPets.add(new ItemPet("Mufty")); this.tag.setItems(allPets); this.tag.setItemValue("name"); this.tag.setItemLabel("label"); int result = this.tag.doStartTag(); assertEquals(Tag.SKIP_BODY, result);
@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); MyStringTrimmerEditor editor = new MyStringTrimmerEditor(); getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + COMMAND_NAME, bindingResult); int result = this.tag.doStartTag(); assertEquals(Tag.SKIP_BODY, result); assertEquals(3, editor.allProcessedValues.size());