use of org.dom4j.io.SAXReader in project spring-framework by spring-projects.
the class RadioButtonsTagTests method withoutItemsEnumBindTarget.
@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();
assertThat(result).isEqualTo(Tag.SKIP_BODY);
String output = "<div>" + getOutput() + "</div>";
SAXReader reader = new SAXReader();
Document document = reader.read(new StringReader(output));
Element rootElement = document.getRootElement();
assertThat(rootElement.elements().size()).isEqualTo(2);
Node value1 = rootElement.selectSingleNode("//input[@value = 'VALUE_1']");
Node value2 = rootElement.selectSingleNode("//input[@value = 'VALUE_2']");
assertThat(rootElement.selectSingleNode("//label[@for = '" + value1.valueOf("@id") + "']").getText()).isEqualTo("TestEnum: VALUE_1");
assertThat(rootElement.selectSingleNode("//label[@for = '" + value2.valueOf("@id") + "']").getText()).isEqualTo("TestEnum: VALUE_2");
assertThat(rootElement.selectSingleNode("//input[@checked]")).isEqualTo(value2);
}
use of org.dom4j.io.SAXReader in project spring-framework by spring-projects.
the class SelectTagTests method multipleForCollection.
@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();
assertThat(result).isEqualTo(Tag.SKIP_BODY);
String output = getOutput();
output = "<doc>" + output + "</doc>";
SAXReader reader = new SAXReader();
Document document = reader.read(new StringReader(output));
Element rootElement = document.getRootElement();
assertThat(rootElement.elements().size()).isEqualTo(2);
Element selectElement = rootElement.element("select");
assertThat(selectElement.getName()).isEqualTo("select");
assertThat(selectElement.attribute("name").getValue()).isEqualTo("someList");
assertThat(selectElement.attribute("multiple").getValue()).isEqualTo("multiple");
List children = selectElement.elements();
assertThat(children.size()).as("Incorrect number of children").isEqualTo(4);
Element inputElement = rootElement.element("input");
assertThat(inputElement).isNotNull();
}
use of org.dom4j.io.SAXReader in project spring-framework by spring-projects.
the class SelectTagTests method validateOutput.
private void validateOutput(String output, boolean selected) throws DocumentException {
SAXReader reader = new SAXReader();
Document document = reader.read(new StringReader(output));
Element rootElement = document.getRootElement();
assertThat(rootElement.getName()).isEqualTo("select");
assertThat(rootElement.attribute("name").getValue()).isEqualTo("country");
List children = rootElement.elements();
assertThat(children.size()).as("Incorrect number of children").isEqualTo(4);
Element e = (Element) rootElement.selectSingleNode("option[@value = 'UK']");
Attribute selectedAttr = e.attribute("selected");
if (selected) {
assertThat(selectedAttr != null && "selected".equals(selectedAttr.getValue())).isTrue();
} else {
assertThat(selectedAttr).isNull();
}
}
use of org.dom4j.io.SAXReader in project spring-framework by spring-projects.
the class SelectTagTests method assertStringArray.
private void assertStringArray() throws JspException, DocumentException {
int result = this.tag.doStartTag();
assertThat(result).isEqualTo(Tag.SKIP_BODY);
String output = getOutput();
assertThat(output.startsWith("<select ")).isTrue();
assertThat(output.endsWith("</select>")).isTrue();
SAXReader reader = new SAXReader();
Document document = reader.read(new StringReader(output));
Element rootElement = document.getRootElement();
assertThat(rootElement.getName()).isEqualTo("select");
assertThat(rootElement.attribute("name").getValue()).isEqualTo("name");
List children = rootElement.elements();
assertThat(children.size()).as("Incorrect number of children").isEqualTo(4);
Element e = (Element) rootElement.selectSingleNode("option[text() = 'Rob']");
assertThat(e.attribute("selected").getValue()).as("Rob node not selected").isEqualTo("selected");
}
use of org.dom4j.io.SAXReader in project spring-framework by spring-projects.
the class SelectTagTests method withMultiListAndCustomEditor.
@Test
public void withMultiListAndCustomEditor() throws Exception {
List list = new ArrayList();
list.add(Country.COUNTRY_UK);
list.add(Country.COUNTRY_AT);
this.bean.setSomeList(list);
BeanPropertyBindingResult errors = new BeanPropertyBindingResult(this.bean, COMMAND_NAME);
errors.getPropertyAccessor().registerCustomEditor(List.class, new CustomCollectionEditor(ArrayList.class) {
@Override
public String getAsText() {
return getValue().toString();
}
});
exposeBindingResult(errors);
this.tag.setPath("someList");
this.tag.setItems(Country.getCountries());
this.tag.setItemValue("isoCode");
int result = this.tag.doStartTag();
assertThat(result).isEqualTo(Tag.SKIP_BODY);
String output = getOutput();
output = "<doc>" + output + "</doc>";
SAXReader reader = new SAXReader();
Document document = reader.read(new StringReader(output));
Element rootElement = document.getRootElement();
assertThat(rootElement.elements().size()).isEqualTo(2);
Element selectElement = rootElement.element("select");
assertThat(selectElement.getName()).isEqualTo("select");
assertThat(selectElement.attribute("name").getValue()).isEqualTo("someList");
List children = selectElement.elements();
assertThat(children.size()).as("Incorrect number of children").isEqualTo(4);
Element e = (Element) selectElement.selectSingleNode("option[@value = 'UK']");
assertThat(e.attribute("selected").getValue()).as("UK node not selected").isEqualTo("selected");
e = (Element) selectElement.selectSingleNode("option[@value = 'AT']");
assertThat(e.attribute("selected").getValue()).as("AT node not selected").isEqualTo("selected");
}
Aggregations