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);
}
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);
}
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");
}
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");
}
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"));
}
Aggregations