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();
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("stringArray", checkboxElement1.attribute("name").getValue());
assertEquals("checked", checkboxElement1.attribute("checked").getValue());
assertEquals("foo", checkboxElement1.attribute("value").getValue());
assertEquals("FOO", 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("stringArray", checkboxElement2.attribute("name").getValue());
assertEquals("checked", checkboxElement2.attribute("checked").getValue());
assertEquals("bar", checkboxElement2.attribute("value").getValue());
assertEquals(delimiter + "BAR", 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("stringArray", checkboxElement3.attribute("name").getValue());
assertNull("not checked", checkboxElement3.attribute("checked"));
assertEquals("baz", checkboxElement3.attribute("value").getValue());
assertEquals(delimiter + "BAZ", spanElement3.getStringValue());
}
use of org.dom4j.io.SAXReader in project spring-framework by spring-projects.
the class CheckboxesTagTests method withMultiValueWithReverseEditor.
@Test
public void withMultiValueWithReverseEditor() throws Exception {
this.tag.setPath("stringArray");
this.tag.setItems(new Object[] { "FOO", "BAR", "BAZ" });
BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(this.bean, COMMAND_NAME);
MyLowerCaseEditor editor = new MyLowerCaseEditor();
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);
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("stringArray", checkboxElement1.attribute("name").getValue());
assertEquals("checked", checkboxElement1.attribute("checked").getValue());
assertEquals("FOO", checkboxElement1.attribute("value").getValue());
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("stringArray", checkboxElement2.attribute("name").getValue());
assertEquals("checked", checkboxElement2.attribute("checked").getValue());
assertEquals("BAR", checkboxElement2.attribute("value").getValue());
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("stringArray", checkboxElement3.attribute("name").getValue());
assertNull("not checked", checkboxElement3.attribute("checked"));
assertEquals("BAZ", checkboxElement3.attribute("value").getValue());
}
use of org.dom4j.io.SAXReader in project spring-framework by spring-projects.
the class CheckboxesTagTests method collectionOfItemPets.
/**
* Test case where items toString() doesn't fit the item ID
*/
@Test
public void collectionOfItemPets() throws Exception {
this.tag.setPath("someSet");
List allPets = new ArrayList();
allPets.add(new ItemPet("PET1"));
allPets.add(new ItemPet("PET2"));
allPets.add(new ItemPet("PET3"));
this.tag.setItems(allPets);
this.tag.setItemValue("name");
this.tag.setItemLabel("label");
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());
assertNotNull("should be checked", checkboxElement1.attribute("checked"));
assertEquals("checked", checkboxElement1.attribute("checked").getValue());
assertEquals("PET1", checkboxElement1.attribute("value").getValue());
assertEquals("PET1", 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());
assertNotNull("should be checked", checkboxElement2.attribute("checked"));
assertEquals("checked", checkboxElement2.attribute("checked").getValue());
assertEquals("PET2", checkboxElement2.attribute("value").getValue());
assertEquals("PET2", 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("PET3", spanElement3.getStringValue());
}
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();
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("stringArray", checkboxElement1.attribute("name").getValue());
assertEquals("checked", checkboxElement1.attribute("checked").getValue());
assertEquals("foo", checkboxElement1.attribute("value").getValue());
assertEquals("foo", spanElement1.getStringValue());
assertEquals(dynamicAttribute1, checkboxElement1.attribute(dynamicAttribute1).getValue());
assertEquals(dynamicAttribute2, checkboxElement1.attribute(dynamicAttribute2).getValue());
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("stringArray", checkboxElement2.attribute("name").getValue());
assertEquals("checked", checkboxElement2.attribute("checked").getValue());
assertEquals("bar", checkboxElement2.attribute("value").getValue());
assertEquals("bar", spanElement2.getStringValue());
assertEquals(dynamicAttribute1, checkboxElement2.attribute(dynamicAttribute1).getValue());
assertEquals(dynamicAttribute2, checkboxElement2.attribute(dynamicAttribute2).getValue());
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("stringArray", checkboxElement3.attribute("name").getValue());
assertNull("not checked", checkboxElement3.attribute("checked"));
assertEquals("baz", checkboxElement3.attribute("value").getValue());
assertEquals("baz", spanElement3.getStringValue());
assertEquals(dynamicAttribute1, checkboxElement3.attribute(dynamicAttribute1).getValue());
assertEquals(dynamicAttribute2, checkboxElement3.attribute(dynamicAttribute2).getValue());
}
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();
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("stringArray", checkboxElement1.attribute("name").getValue());
assertEquals("checked", checkboxElement1.attribute("checked").getValue());
assertEquals(" foo", checkboxElement1.attribute("value").getValue());
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("stringArray", checkboxElement2.attribute("name").getValue());
assertEquals("checked", checkboxElement2.attribute("checked").getValue());
assertEquals(" bar", checkboxElement2.attribute("value").getValue());
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("stringArray", checkboxElement3.attribute("name").getValue());
assertNull("not checked", checkboxElement3.attribute("checked"));
assertEquals(" baz", checkboxElement3.attribute("value").getValue());
}
Aggregations