Search in sources :

Example 91 with SAXReader

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

the class SelectTagTests method multipleExplicitlyFalse.

@Test
public void multipleExplicitlyFalse() throws Exception {
    this.tag.setPath("name");
    this.tag.setItems(Country.getCountries());
    this.tag.setItemValue("isoCode");
    this.tag.setMultiple("false");
    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(1);
    Element selectElement = rootElement.element("select");
    assertThat(selectElement.getName()).isEqualTo("select");
    assertThat(selectElement.attribute("name").getValue()).isEqualTo("name");
    assertThat(selectElement.attribute("multiple")).isNull();
    List children = selectElement.elements();
    assertThat(children.size()).as("Incorrect number of children").isEqualTo(4);
}
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 92 with SAXReader

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

the class CheckboxesTagTests method withMultiValueArrayAndDynamicAttributes.

@Test
public void withMultiValueArrayAndDynamicAttributes() throws Exception {
    String dynamicAttribute1 = "attr1";
    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();
    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 spanElement1 = document.getRootElement().elements().get(0);
    Element checkboxElement1 = spanElement1.elements().get(0);
    assertThat(checkboxElement1.getName()).isEqualTo("input");
    assertThat(checkboxElement1.attribute("type").getValue()).isEqualTo("checkbox");
    assertThat(checkboxElement1.attribute("name").getValue()).isEqualTo("stringArray");
    assertThat(checkboxElement1.attribute("checked").getValue()).isEqualTo("checked");
    assertThat(checkboxElement1.attribute("value").getValue()).isEqualTo("foo");
    assertThat(spanElement1.getStringValue()).isEqualTo("foo");
    assertThat(checkboxElement1.attribute(dynamicAttribute1).getValue()).isEqualTo(dynamicAttribute1);
    assertThat(checkboxElement1.attribute(dynamicAttribute2).getValue()).isEqualTo(dynamicAttribute2);
    Element spanElement2 = document.getRootElement().elements().get(1);
    Element checkboxElement2 = spanElement2.elements().get(0);
    assertThat(checkboxElement2.getName()).isEqualTo("input");
    assertThat(checkboxElement2.attribute("type").getValue()).isEqualTo("checkbox");
    assertThat(checkboxElement2.attribute("name").getValue()).isEqualTo("stringArray");
    assertThat(checkboxElement2.attribute("checked").getValue()).isEqualTo("checked");
    assertThat(checkboxElement2.attribute("value").getValue()).isEqualTo("bar");
    assertThat(spanElement2.getStringValue()).isEqualTo("bar");
    assertThat(checkboxElement2.attribute(dynamicAttribute1).getValue()).isEqualTo(dynamicAttribute1);
    assertThat(checkboxElement2.attribute(dynamicAttribute2).getValue()).isEqualTo(dynamicAttribute2);
    Element spanElement3 = document.getRootElement().elements().get(2);
    Element checkboxElement3 = spanElement3.elements().get(0);
    assertThat(checkboxElement3.getName()).isEqualTo("input");
    assertThat(checkboxElement3.attribute("type").getValue()).isEqualTo("checkbox");
    assertThat(checkboxElement3.attribute("name").getValue()).isEqualTo("stringArray");
    assertThat(checkboxElement3.attribute("checked")).as("not checked").isNull();
    assertThat(checkboxElement3.attribute("value").getValue()).isEqualTo("baz");
    assertThat(spanElement3.getStringValue()).isEqualTo("baz");
    assertThat(checkboxElement3.attribute(dynamicAttribute1).getValue()).isEqualTo(dynamicAttribute1);
    assertThat(checkboxElement3.attribute(dynamicAttribute2).getValue()).isEqualTo(dynamicAttribute2);
}
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 93 with SAXReader

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

the class CheckboxesTagTests method withMultiValueWithFormatter.

@Test
public void withMultiValueWithFormatter() throws Exception {
    this.tag.setPath("stringArray");
    this.tag.setItems(new Object[] { "   foo", "   bar", "   baz" });
    BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(this.bean, COMMAND_NAME);
    FormattingConversionService cs = new FormattingConversionService();
    cs.addFormatterForFieldType(String.class, new Formatter<String>() {

        @Override
        public String print(String object, Locale locale) {
            return object;
        }

        @Override
        public String parse(String text, Locale locale) throws ParseException {
            return text.trim();
        }
    });
    bindingResult.initConversion(cs);
    getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + COMMAND_NAME, bindingResult);
    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 spanElement1 = document.getRootElement().elements().get(0);
    Element checkboxElement1 = spanElement1.elements().get(0);
    assertThat(checkboxElement1.getName()).isEqualTo("input");
    assertThat(checkboxElement1.attribute("type").getValue()).isEqualTo("checkbox");
    assertThat(checkboxElement1.attribute("name").getValue()).isEqualTo("stringArray");
    assertThat(checkboxElement1.attribute("checked").getValue()).isEqualTo("checked");
    assertThat(checkboxElement1.attribute("value").getValue()).isEqualTo("   foo");
    Element spanElement2 = document.getRootElement().elements().get(1);
    Element checkboxElement2 = spanElement2.elements().get(0);
    assertThat(checkboxElement2.getName()).isEqualTo("input");
    assertThat(checkboxElement2.attribute("type").getValue()).isEqualTo("checkbox");
    assertThat(checkboxElement2.attribute("name").getValue()).isEqualTo("stringArray");
    assertThat(checkboxElement2.attribute("checked").getValue()).isEqualTo("checked");
    assertThat(checkboxElement2.attribute("value").getValue()).isEqualTo("   bar");
    Element spanElement3 = document.getRootElement().elements().get(2);
    Element checkboxElement3 = spanElement3.elements().get(0);
    assertThat(checkboxElement3.getName()).isEqualTo("input");
    assertThat(checkboxElement3.attribute("type").getValue()).isEqualTo("checkbox");
    assertThat(checkboxElement3.attribute("name").getValue()).isEqualTo("stringArray");
    assertThat(checkboxElement3.attribute("checked")).as("not checked").isNull();
    assertThat(checkboxElement3.attribute("value").getValue()).isEqualTo("   baz");
}
Also used : Locale(java.util.Locale) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) StringReader(java.io.StringReader) ParseException(java.text.ParseException) Document(org.dom4j.Document) FormattingConversionService(org.springframework.format.support.FormattingConversionService) Test(org.junit.jupiter.api.Test)

Example 94 with SAXReader

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

the class CheckboxesTagTests method withPetItemsMap.

@Test
public void withPetItemsMap() throws Exception {
    this.tag.setPath("someSet");
    Map m = new LinkedHashMap();
    m.put(new ItemPet("PET1"), "PET1Label");
    m.put(new ItemPet("PET2"), "PET2Label");
    m.put(new ItemPet("PET3"), "PET3Label");
    this.tag.setItems(m);
    tag.setItemValue("name");
    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 spanElement1 = document.getRootElement().elements().get(0);
    Element checkboxElement1 = spanElement1.elements().get(0);
    assertThat(checkboxElement1.getName()).isEqualTo("input");
    assertThat(checkboxElement1.attribute("type").getValue()).isEqualTo("checkbox");
    assertThat(checkboxElement1.attribute("name").getValue()).isEqualTo("someSet");
    assertThat(checkboxElement1.attribute("checked").getValue()).isEqualTo("checked");
    assertThat(checkboxElement1.attribute("value").getValue()).isEqualTo("PET1");
    assertThat(spanElement1.getStringValue()).isEqualTo("PET1Label");
    Element spanElement2 = document.getRootElement().elements().get(1);
    Element checkboxElement2 = spanElement2.elements().get(0);
    assertThat(checkboxElement2.getName()).isEqualTo("input");
    assertThat(checkboxElement2.attribute("type").getValue()).isEqualTo("checkbox");
    assertThat(checkboxElement2.attribute("name").getValue()).isEqualTo("someSet");
    assertThat(checkboxElement2.attribute("checked").getValue()).isEqualTo("checked");
    assertThat(checkboxElement2.attribute("value").getValue()).isEqualTo("PET2");
    assertThat(spanElement2.getStringValue()).isEqualTo("PET2Label");
    Element spanElement3 = document.getRootElement().elements().get(2);
    Element checkboxElement3 = spanElement3.elements().get(0);
    assertThat(checkboxElement3.getName()).isEqualTo("input");
    assertThat(checkboxElement3.attribute("type").getValue()).isEqualTo("checkbox");
    assertThat(checkboxElement3.attribute("name").getValue()).isEqualTo("someSet");
    assertThat(checkboxElement3.attribute("checked")).as("not checked").isNull();
    assertThat(checkboxElement3.attribute("value").getValue()).isEqualTo("PET3");
    assertThat(spanElement3.getStringValue()).isEqualTo("PET3Label");
}
Also used : SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) StringReader(java.io.StringReader) Document(org.dom4j.Document) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.jupiter.api.Test)

Example 95 with SAXReader

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

the class CheckboxesTagTests method withMultiValueMapWithDelimiter.

@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();
    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 spanElement1 = document.getRootElement().elements().get(0);
    Element checkboxElement1 = spanElement1.elements().get(0);
    assertThat(checkboxElement1.getName()).isEqualTo("input");
    assertThat(checkboxElement1.attribute("type").getValue()).isEqualTo("checkbox");
    assertThat(checkboxElement1.attribute("name").getValue()).isEqualTo("stringArray");
    assertThat(checkboxElement1.attribute("checked").getValue()).isEqualTo("checked");
    assertThat(checkboxElement1.attribute("value").getValue()).isEqualTo("foo");
    assertThat(spanElement1.getStringValue()).isEqualTo("FOO");
    Element spanElement2 = document.getRootElement().elements().get(1);
    Element checkboxElement2 = spanElement2.elements().get(0);
    assertThat(checkboxElement2.getName()).isEqualTo("input");
    assertThat(checkboxElement2.attribute("type").getValue()).isEqualTo("checkbox");
    assertThat(checkboxElement2.attribute("name").getValue()).isEqualTo("stringArray");
    assertThat(checkboxElement2.attribute("checked").getValue()).isEqualTo("checked");
    assertThat(checkboxElement2.attribute("value").getValue()).isEqualTo("bar");
    assertThat(spanElement2.getStringValue()).isEqualTo((delimiter + "BAR"));
    Element spanElement3 = document.getRootElement().elements().get(2);
    Element checkboxElement3 = spanElement3.elements().get(0);
    assertThat(checkboxElement3.getName()).isEqualTo("input");
    assertThat(checkboxElement3.attribute("type").getValue()).isEqualTo("checkbox");
    assertThat(checkboxElement3.attribute("name").getValue()).isEqualTo("stringArray");
    assertThat(checkboxElement3.attribute("checked")).as("not checked").isNull();
    assertThat(checkboxElement3.attribute("value").getValue()).isEqualTo("baz");
    assertThat(spanElement3.getStringValue()).isEqualTo((delimiter + "BAZ"));
}
Also used : SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) StringReader(java.io.StringReader) Document(org.dom4j.Document) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) 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