Search in sources :

Example 81 with SAXReader

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

the class RadioButtonsTagTests method withMultiValueMap.

@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();
    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 radioButtonElement1 = spanElement1.elements().get(0);
    assertThat(radioButtonElement1.getName()).isEqualTo("input");
    assertThat(radioButtonElement1.attribute("type").getValue()).isEqualTo("radio");
    assertThat(radioButtonElement1.attribute("name").getValue()).isEqualTo("stringArray");
    assertThat(radioButtonElement1.attribute("checked").getValue()).isEqualTo("checked");
    assertThat(radioButtonElement1.attribute("value").getValue()).isEqualTo("foo");
    assertThat(spanElement1.getStringValue()).isEqualTo("FOO");
    Element spanElement2 = document.getRootElement().elements().get(1);
    Element radioButtonElement2 = spanElement2.elements().get(0);
    assertThat(radioButtonElement2.getName()).isEqualTo("input");
    assertThat(radioButtonElement2.attribute("type").getValue()).isEqualTo("radio");
    assertThat(radioButtonElement2.attribute("name").getValue()).isEqualTo("stringArray");
    assertThat(radioButtonElement2.attribute("checked").getValue()).isEqualTo("checked");
    assertThat(radioButtonElement2.attribute("value").getValue()).isEqualTo("bar");
    assertThat(spanElement2.getStringValue()).isEqualTo("BAR");
    Element spanElement3 = document.getRootElement().elements().get(2);
    Element radioButtonElement3 = spanElement3.elements().get(0);
    assertThat(radioButtonElement3.getName()).isEqualTo("input");
    assertThat(radioButtonElement3.attribute("type").getValue()).isEqualTo("radio");
    assertThat(radioButtonElement3.attribute("name").getValue()).isEqualTo("stringArray");
    assertThat(radioButtonElement3.attribute("checked")).as("not checked").isNull();
    assertThat(radioButtonElement3.attribute("value").getValue()).isEqualTo("baz");
    assertThat(spanElement3.getStringValue()).isEqualTo("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)

Example 82 with SAXReader

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

the class RadioButtonsTagTests method withMultiValueWithEditor.

@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();
    bindingResult.getPropertyEditorRegistry().registerCustomEditor(String.class, editor);
    getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + COMMAND_NAME, bindingResult);
    int result = this.tag.doStartTag();
    assertThat(result).isEqualTo(Tag.SKIP_BODY);
    assertThat(editor.allProcessedValues.size()).isEqualTo(3);
    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 radioButtonElement1 = spanElement1.elements().get(0);
    assertThat(radioButtonElement1.getName()).isEqualTo("input");
    assertThat(radioButtonElement1.attribute("type").getValue()).isEqualTo("radio");
    assertThat(radioButtonElement1.attribute("name").getValue()).isEqualTo("stringArray");
    assertThat(radioButtonElement1.attribute("checked").getValue()).isEqualTo("checked");
    assertThat(radioButtonElement1.attribute("value").getValue()).isEqualTo("   foo");
    Element spanElement2 = document.getRootElement().elements().get(1);
    Element radioButtonElement2 = spanElement2.elements().get(0);
    assertThat(radioButtonElement2.getName()).isEqualTo("input");
    assertThat(radioButtonElement2.attribute("type").getValue()).isEqualTo("radio");
    assertThat(radioButtonElement2.attribute("name").getValue()).isEqualTo("stringArray");
    assertThat(radioButtonElement2.attribute("checked").getValue()).isEqualTo("checked");
    assertThat(radioButtonElement2.attribute("value").getValue()).isEqualTo("   bar");
    Element spanElement3 = document.getRootElement().elements().get(2);
    Element radioButtonElement3 = spanElement3.elements().get(0);
    assertThat(radioButtonElement3.getName()).isEqualTo("input");
    assertThat(radioButtonElement3.attribute("type").getValue()).isEqualTo("radio");
    assertThat(radioButtonElement3.attribute("name").getValue()).isEqualTo("stringArray");
    assertThat(radioButtonElement3.attribute("checked")).as("not checked").isNull();
    assertThat(radioButtonElement3.attribute("value").getValue()).isEqualTo("   baz");
}
Also used : BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) StringReader(java.io.StringReader) Document(org.dom4j.Document) Test(org.junit.jupiter.api.Test)

Example 83 with SAXReader

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

the class RadioButtonsTagTests 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 radioButtonElement = spanElement.elements().get(0);
    assertThat(radioButtonElement.getName()).isEqualTo("input");
    assertThat(radioButtonElement.attribute("type").getValue()).isEqualTo("radio");
    assertThat(radioButtonElement.attribute("name").getValue()).isEqualTo("stringArray");
    assertThat(radioButtonElement.attribute("checked").getValue()).isEqualTo("checked");
    assertThat(radioButtonElement.attribute("disabled").getValue()).isEqualTo("disabled");
    assertThat(radioButtonElement.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 84 with SAXReader

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

the class RadioButtonsTagTests method withMultiValueArray.

@Test
public void withMultiValueArray() throws Exception {
    this.tag.setPath("stringArray");
    this.tag.setItems(new Object[] { "foo", "bar", "baz" });
    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 radioButtonElement1 = spanElement1.elements().get(0);
    assertThat(radioButtonElement1.getName()).isEqualTo("input");
    assertThat(radioButtonElement1.attribute("type").getValue()).isEqualTo("radio");
    assertThat(radioButtonElement1.attribute("name").getValue()).isEqualTo("stringArray");
    assertThat(radioButtonElement1.attribute("checked").getValue()).isEqualTo("checked");
    assertThat(radioButtonElement1.attribute("value").getValue()).isEqualTo("foo");
    assertThat(spanElement1.getStringValue()).isEqualTo("foo");
    Element spanElement2 = document.getRootElement().elements().get(1);
    Element radioButtonElement2 = spanElement2.elements().get(0);
    assertThat(radioButtonElement2.getName()).isEqualTo("input");
    assertThat(radioButtonElement2.attribute("type").getValue()).isEqualTo("radio");
    assertThat(radioButtonElement2.attribute("name").getValue()).isEqualTo("stringArray");
    assertThat(radioButtonElement2.attribute("checked").getValue()).isEqualTo("checked");
    assertThat(radioButtonElement2.attribute("value").getValue()).isEqualTo("bar");
    assertThat(spanElement2.getStringValue()).isEqualTo("bar");
    Element spanElement3 = document.getRootElement().elements().get(2);
    Element radioButtonElement3 = spanElement3.elements().get(0);
    assertThat(radioButtonElement3.getName()).isEqualTo("input");
    assertThat(radioButtonElement3.attribute("type").getValue()).isEqualTo("radio");
    assertThat(radioButtonElement3.attribute("name").getValue()).isEqualTo("stringArray");
    assertThat(radioButtonElement3.attribute("checked")).as("not checked").isNull();
    assertThat(radioButtonElement3.attribute("value").getValue()).isEqualTo("baz");
    assertThat(spanElement3.getStringValue()).isEqualTo("baz");
}
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 85 with SAXReader

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

the class RadioButtonsTagTests method collectionOfPetsWithEditor.

@Test
public void collectionOfPetsWithEditor() throws Exception {
    this.tag.setPath("pets");
    List allPets = new ArrayList();
    allPets.add(new ItemPet("Rudiger"));
    allPets.add(new ItemPet("Spot"));
    allPets.add(new ItemPet("Checkers"));
    allPets.add(new ItemPet("Fluffy"));
    allPets.add(new ItemPet("Mufty"));
    this.tag.setItems(allPets);
    this.tag.setItemLabel("label");
    this.tag.setId("myId");
    BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(this.bean, COMMAND_NAME);
    PropertyEditorSupport editor = new ItemPet.CustomEditor();
    bindingResult.getPropertyEditorRegistry().registerCustomEditor(ItemPet.class, editor);
    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 radioButtonElement1 = spanElement1.elements().get(0);
    assertThat(radioButtonElement1.getName()).isEqualTo("input");
    assertThat(radioButtonElement1.attribute("type").getValue()).isEqualTo("radio");
    assertThat(radioButtonElement1.attribute("name").getValue()).isEqualTo("pets");
    assertThat(radioButtonElement1.attribute("checked").getValue()).isEqualTo("checked");
    assertThat(radioButtonElement1.attribute("value").getValue()).isEqualTo("Rudiger");
    assertThat(spanElement1.getStringValue()).isEqualTo("RUDIGER");
    Element spanElement2 = document.getRootElement().elements().get(1);
    Element radioButtonElement2 = spanElement2.elements().get(0);
    assertThat(radioButtonElement2.getName()).isEqualTo("input");
    assertThat(radioButtonElement2.attribute("type").getValue()).isEqualTo("radio");
    assertThat(radioButtonElement2.attribute("name").getValue()).isEqualTo("pets");
    assertThat(radioButtonElement2.attribute("checked").getValue()).isEqualTo("checked");
    assertThat(radioButtonElement2.attribute("value").getValue()).isEqualTo("Spot");
    assertThat(spanElement2.getStringValue()).isEqualTo("SPOT");
    Element spanElement3 = document.getRootElement().elements().get(2);
    Element radioButtonElement3 = spanElement3.elements().get(0);
    assertThat(radioButtonElement3.getName()).isEqualTo("input");
    assertThat(radioButtonElement3.attribute("type").getValue()).isEqualTo("radio");
    assertThat(radioButtonElement3.attribute("name").getValue()).isEqualTo("pets");
    assertThat(radioButtonElement3.attribute("checked")).as("not checked").isNull();
    assertThat(radioButtonElement3.attribute("value").getValue()).isEqualTo("Checkers");
    assertThat(spanElement3.getStringValue()).isEqualTo("CHECKERS");
    Element spanElement4 = document.getRootElement().elements().get(3);
    Element radioButtonElement4 = spanElement4.elements().get(0);
    assertThat(radioButtonElement4.getName()).isEqualTo("input");
    assertThat(radioButtonElement4.attribute("type").getValue()).isEqualTo("radio");
    assertThat(radioButtonElement4.attribute("name").getValue()).isEqualTo("pets");
    assertThat(radioButtonElement4.attribute("checked").getValue()).isEqualTo("checked");
    assertThat(radioButtonElement4.attribute("value").getValue()).isEqualTo("Fluffy");
    assertThat(spanElement4.getStringValue()).isEqualTo("FLUFFY");
    Element spanElement5 = document.getRootElement().elements().get(4);
    Element radioButtonElement5 = spanElement5.elements().get(0);
    assertThat(radioButtonElement5.getName()).isEqualTo("input");
    assertThat(radioButtonElement5.attribute("type").getValue()).isEqualTo("radio");
    assertThat(radioButtonElement5.attribute("name").getValue()).isEqualTo("pets");
    assertThat(radioButtonElement5.attribute("checked").getValue()).isEqualTo("checked");
    assertThat(radioButtonElement5.attribute("value").getValue()).isEqualTo("Mufty");
    assertThat(spanElement5.getStringValue()).isEqualTo("MUFTY");
}
Also used : 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) PropertyEditorSupport(java.beans.PropertyEditorSupport) 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