Search in sources :

Example 96 with SAXReader

use of org.dom4j.io.SAXReader in project spring-framework by spring-projects.

the class CheckboxesTagTests method hiddenElementOmittedOnDisabled.

@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();
    assertThat(result).isEqualTo(Tag.SKIP_BODY);
    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();
    assertThat(rootElement.elements().size()).as("Both tag and hidden element rendered incorrectly").isEqualTo(3);
    Element spanElement = document.getRootElement().elements().get(0);
    Element checkboxElement = spanElement.elements().get(0);
    assertThat(checkboxElement.getName()).isEqualTo("input");
    assertThat(checkboxElement.attribute("type").getValue()).isEqualTo("checkbox");
    assertThat(checkboxElement.attribute("name").getValue()).isEqualTo("stringArray");
    assertThat(checkboxElement.attribute("checked").getValue()).isEqualTo("checked");
    assertThat(checkboxElement.attribute("disabled").getValue()).isEqualTo("disabled");
    assertThat(checkboxElement.attribute("value").getValue()).isEqualTo("foo");
}
Also used : SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) StringReader(java.io.StringReader) Document(org.dom4j.Document) Test(org.junit.jupiter.api.Test)

Example 97 with SAXReader

use of org.dom4j.io.SAXReader in project spring-framework by spring-projects.

the class OptionsTagTests method withoutItems.

@Test
void withoutItems() throws Exception {
    this.tag.setItemValue("isoCode");
    this.tag.setItemLabel("name");
    this.selectTag.setPath("testBean");
    this.selectTag.doStartTag();
    int result = this.tag.doStartTag();
    assertThat(result).isEqualTo(Tag.SKIP_BODY);
    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();
    assertThat(children.size()).as("Incorrect number of children").isEqualTo(0);
}
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) Test(org.junit.jupiter.api.Test)

Example 98 with SAXReader

use of org.dom4j.io.SAXReader in project spring-framework by spring-projects.

the class OptionsTagTests method withoutItemsEnumParent.

@Test
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();
    assertThat(result).isEqualTo(BodyTag.SKIP_BODY);
    result = this.tag.doEndTag();
    assertThat(result).isEqualTo(Tag.EVAL_PAGE);
    this.selectTag.doEndTag();
    String output = getWriter().toString();
    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("option[@value = 'VALUE_1']");
    Node value2 = rootElement.selectSingleNode("option[@value = 'VALUE_2']");
    assertThat(value1.getText()).isEqualTo("TestEnum: VALUE_1");
    assertThat(value2.getText()).isEqualTo("TestEnum: VALUE_2");
    assertThat(rootElement.selectSingleNode("option[@selected]")).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 99 with SAXReader

use of org.dom4j.io.SAXReader in project spring-framework by spring-projects.

the class OptionsTagTests method withCollectionAndCustomEditor.

@Test
void withCollectionAndCustomEditor() throws Exception {
    PropertyEditor propertyEditor = new SimpleFloatEditor();
    TestBean target = new TestBean();
    target.setMyFloat(Float.valueOf("12.34"));
    BeanPropertyBindingResult errors = new BeanPropertyBindingResult(target, COMMAND_NAME);
    errors.getPropertyAccessor().registerCustomEditor(Float.class, propertyEditor);
    exposeBindingResult(errors);
    getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, new BindStatus(getRequestContext(), "testBean.myFloat", false));
    List<Float> floats = new ArrayList<>();
    floats.add(Float.valueOf("12.30"));
    floats.add(Float.valueOf("12.31"));
    floats.add(Float.valueOf("12.32"));
    floats.add(Float.valueOf("12.33"));
    floats.add(Float.valueOf("12.34"));
    floats.add(Float.valueOf("12.35"));
    this.tag.setItems(floats);
    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();
    List children = rootElement.elements();
    assertThat(children.size()).as("Incorrect number of children").isEqualTo(6);
    Element element = (Element) rootElement.selectSingleNode("option[text() = '12.34f']");
    assertThat(element).as("Option node should not be null").isNotNull();
    assertThat(element.attribute("selected").getValue()).as("12.34 node not selected").isEqualTo("selected");
    assertThat(element.attribute("id")).as("No id rendered").isNull();
    element = (Element) rootElement.selectSingleNode("option[text() = '12.35f']");
    assertThat(element).as("Option node should not be null").isNotNull();
    assertThat(element.attribute("selected")).as("12.35 node incorrectly selected").isNull();
    assertThat(element.attribute("id")).as("No id rendered").isNull();
}
Also used : BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) BindStatus(org.springframework.web.servlet.support.BindStatus) Document(org.dom4j.Document) TestBean(org.springframework.beans.testfixture.beans.TestBean) StringReader(java.io.StringReader) PropertyEditor(java.beans.PropertyEditor) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 100 with SAXReader

use of org.dom4j.io.SAXReader in project spring-framework by spring-projects.

the class RadioButtonTagTests method collectionOfPetsNotSelected.

@Test
void collectionOfPetsNotSelected() throws Exception {
    this.tag.setPath("pets");
    this.tag.setValue(new Pet("Santa's Little Helper"));
    int result = this.tag.doStartTag();
    assertThat(result).isEqualTo(Tag.SKIP_BODY);
    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 checkboxElement = document.getRootElement().elements().get(0);
    assertThat(checkboxElement.getName()).isEqualTo("input");
    assertThat(checkboxElement.attribute("type").getValue()).isEqualTo("radio");
    assertThat(checkboxElement.attribute("name").getValue()).isEqualTo("pets");
    assertThat(checkboxElement.attribute("value").getValue()).isEqualTo("Santa's Little Helper");
    assertThat(checkboxElement.attribute("checked")).isNull();
}
Also used : SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) StringReader(java.io.StringReader) Document(org.dom4j.Document) Pet(org.springframework.beans.testfixture.beans.Pet) 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