Search in sources :

Example 21 with SAXReader

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

the class CheckboxTagTests method withMultiValueUnchecked.

@Test
public void withMultiValueUnchecked() throws Exception {
    this.tag.setPath("stringArray");
    this.tag.setValue("abc");
    int result = this.tag.doStartTag();
    assertEquals(Tag.SKIP_BODY, result);
    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 = (Element) document.getRootElement().elements().get(0);
    assertEquals("input", checkboxElement.getName());
    assertEquals("checkbox", checkboxElement.attribute("type").getValue());
    assertEquals("stringArray", checkboxElement.attribute("name").getValue());
    assertNull(checkboxElement.attribute("checked"));
    assertEquals("abc", checkboxElement.attribute("value").getValue());
}
Also used : SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) StringReader(java.io.StringReader) Document(org.dom4j.Document) Test(org.junit.Test)

Example 22 with SAXReader

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

the class CheckboxTagTests method withMultiValueIntegerWithEditor.

@Test
public void withMultiValueIntegerWithEditor() throws Exception {
    this.tag.setPath("someIntegerArray");
    this.tag.setValue("   1");
    BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(this.bean, COMMAND_NAME);
    MyIntegerEditor editor = new MyIntegerEditor();
    bindingResult.getPropertyEditorRegistry().registerCustomEditor(Integer.class, editor);
    getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + COMMAND_NAME, bindingResult);
    int result = this.tag.doStartTag();
    assertEquals(Tag.SKIP_BODY, result);
    assertEquals(1, editor.count);
    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 = (Element) document.getRootElement().elements().get(0);
    assertEquals("input", checkboxElement.getName());
    assertEquals("checkbox", checkboxElement.attribute("type").getValue());
    assertEquals("someIntegerArray", checkboxElement.attribute("name").getValue());
    assertEquals("checked", checkboxElement.attribute("checked").getValue());
    assertEquals("   1", checkboxElement.attribute("value").getValue());
}
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.Test)

Example 23 with SAXReader

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

the class CheckboxTagTests method withMultiValueWithEditor.

@Test
public void withMultiValueWithEditor() throws Exception {
    this.tag.setPath("stringArray");
    this.tag.setValue("   foo");
    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();
    assertEquals(Tag.SKIP_BODY, result);
    assertEquals(1, editor.count);
    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 = (Element) document.getRootElement().elements().get(0);
    assertEquals("input", checkboxElement.getName());
    assertEquals("checkbox", checkboxElement.attribute("type").getValue());
    assertEquals("stringArray", checkboxElement.attribute("name").getValue());
    assertEquals("checked", checkboxElement.attribute("checked").getValue());
    assertEquals("   foo", checkboxElement.attribute("value").getValue());
}
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.Test)

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

Example 25 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();
    assertEquals(Tag.SKIP_BODY, result);
    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 = (Element) document.getRootElement().elements().get(0);
    Element checkboxElement1 = (Element) spanElement1.elements().get(0);
    assertEquals("input", checkboxElement1.getName());
    assertEquals("checkbox", checkboxElement1.attribute("type").getValue());
    assertEquals("someSet", checkboxElement1.attribute("name").getValue());
    assertEquals("checked", checkboxElement1.attribute("checked").getValue());
    assertEquals("PET1", checkboxElement1.attribute("value").getValue());
    assertEquals("PET1Label", spanElement1.getStringValue());
    Element spanElement2 = (Element) document.getRootElement().elements().get(1);
    Element checkboxElement2 = (Element) spanElement2.elements().get(0);
    assertEquals("input", checkboxElement2.getName());
    assertEquals("checkbox", checkboxElement2.attribute("type").getValue());
    assertEquals("someSet", checkboxElement2.attribute("name").getValue());
    assertEquals("checked", checkboxElement2.attribute("checked").getValue());
    assertEquals("PET2", checkboxElement2.attribute("value").getValue());
    assertEquals("PET2Label", spanElement2.getStringValue());
    Element spanElement3 = (Element) document.getRootElement().elements().get(2);
    Element checkboxElement3 = (Element) spanElement3.elements().get(0);
    assertEquals("input", checkboxElement3.getName());
    assertEquals("checkbox", checkboxElement3.attribute("type").getValue());
    assertEquals("someSet", checkboxElement3.attribute("name").getValue());
    assertNull("not checked", checkboxElement3.attribute("checked"));
    assertEquals("PET3", checkboxElement3.attribute("value").getValue());
    assertEquals("PET3Label", spanElement3.getStringValue());
}
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.Test)

Aggregations

SAXReader (org.dom4j.io.SAXReader)172 Document (org.dom4j.Document)143 Element (org.dom4j.Element)128 StringReader (java.io.StringReader)98 Test (org.junit.Test)78 ArrayList (java.util.ArrayList)35 List (java.util.List)35 File (java.io.File)28 DocumentException (org.dom4j.DocumentException)21 IOException (java.io.IOException)18 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)17 LinkedList (java.util.LinkedList)16 Node (org.dom4j.Node)14 HashMap (java.util.HashMap)12 Map (java.util.Map)10 Attribute (org.dom4j.Attribute)10 InputStream (java.io.InputStream)9 XMLWriter (org.dom4j.io.XMLWriter)9 OutputFormat (org.dom4j.io.OutputFormat)8 ByteArrayInputStream (java.io.ByteArrayInputStream)6