Search in sources :

Example 86 with SAXReader

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);
}
Also used : SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) Node(org.dom4j.Node) StringReader(java.io.StringReader) Document(org.dom4j.Document) Test(org.junit.jupiter.api.Test)

Example 87 with SAXReader

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();
}
Also used : SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) List(java.util.List) Document(org.dom4j.Document) Test(org.junit.jupiter.api.Test)

Example 88 with SAXReader

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();
    }
}
Also used : Attribute(org.dom4j.Attribute) SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) List(java.util.List) Document(org.dom4j.Document)

Example 89 with SAXReader

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");
}
Also used : SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) List(java.util.List) Document(org.dom4j.Document)

Example 90 with SAXReader

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");
}
Also used : CustomCollectionEditor(org.springframework.beans.propertyeditors.CustomCollectionEditor) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) List(java.util.List) Document(org.dom4j.Document) Test(org.junit.jupiter.api.Test)

Aggregations

SAXReader (org.dom4j.io.SAXReader)322 Document (org.dom4j.Document)256 Element (org.dom4j.Element)195 StringReader (java.io.StringReader)120 DocumentException (org.dom4j.DocumentException)74 Test (org.junit.jupiter.api.Test)74 File (java.io.File)54 List (java.util.List)49 IOException (java.io.IOException)48 InputStream (java.io.InputStream)48 ArrayList (java.util.ArrayList)47 Node (org.dom4j.Node)28 FileInputStream (java.io.FileInputStream)25 HashMap (java.util.HashMap)24 XMLWriter (org.dom4j.io.XMLWriter)22 ByteArrayInputStream (java.io.ByteArrayInputStream)20 URL (java.net.URL)18 OutputFormat (org.dom4j.io.OutputFormat)18 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)17 Test (org.junit.Test)14