use of org.springframework.beans.testfixture.beans.TestBean in project spring-framework by spring-projects.
the class ErrorsTagTests method assertWhenNoErrorsExistingMessagesInScopeAreNotClobbered.
private void assertWhenNoErrorsExistingMessagesInScopeAreNotClobbered(int scope) throws JspException {
String existingAttribute = "something";
getPageContext().setAttribute(ErrorsTag.MESSAGES_ATTRIBUTE, existingAttribute, scope);
Errors errors = new BeanPropertyBindingResult(new TestBean(), "COMMAND_NAME");
exposeBindingResult(errors);
int result = this.tag.doStartTag();
assertThat(result).isEqualTo(Tag.SKIP_BODY);
result = this.tag.doEndTag();
assertThat(result).isEqualTo(Tag.EVAL_PAGE);
String output = getOutput();
assertThat(output.length()).isEqualTo(0);
assertThat(getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE, scope)).isEqualTo(existingAttribute);
}
use of org.springframework.beans.testfixture.beans.TestBean in project spring-framework by spring-projects.
the class ErrorsTagTests method withoutErrors.
@Test
public void withoutErrors() throws Exception {
Errors errors = new BeanPropertyBindingResult(new TestBean(), "COMMAND_NAME");
exposeBindingResult(errors);
int result = this.tag.doStartTag();
assertThat(result).isEqualTo(Tag.SKIP_BODY);
result = this.tag.doEndTag();
assertThat(result).isEqualTo(Tag.EVAL_PAGE);
String output = getOutput();
assertThat(output.length()).isEqualTo(0);
}
use of org.springframework.beans.testfixture.beans.TestBean in project spring-framework by spring-projects.
the class ErrorsTagTests method withErrorsAndCustomElement.
@Test
public void withErrorsAndCustomElement() throws Exception {
// construct an errors instance of the tag
TestBean target = new TestBean();
target.setName("Rob Harrop");
Errors errors = new BeanPropertyBindingResult(target, COMMAND_NAME);
errors.rejectValue("name", "some.code", "Default Message");
errors.rejectValue("name", "too.short", "Too Short");
exposeBindingResult(errors);
this.tag.setElement("div");
int result = this.tag.doStartTag();
assertThat(result).isEqualTo(BodyTag.EVAL_BODY_BUFFERED);
result = this.tag.doEndTag();
assertThat(result).isEqualTo(Tag.EVAL_PAGE);
String output = getOutput();
assertElementTagOpened(output);
assertElementTagClosed(output);
assertContainsAttribute(output, "id", "name.errors");
assertBlockTagContains(output, "<br/>");
assertBlockTagContains(output, "Default Message");
assertBlockTagContains(output, "Too Short");
}
use of org.springframework.beans.testfixture.beans.TestBean in project spring-framework by spring-projects.
the class ErrorsTagTests method withNonEscapedErrors.
@Test
public void withNonEscapedErrors() throws Exception {
this.tag.setHtmlEscape(false);
// construct an errors instance of the tag
TestBean target = new TestBean();
target.setName("Rob Harrop");
Errors errors = new BeanPropertyBindingResult(target, COMMAND_NAME);
errors.rejectValue("name", "some.code", "Default <> Message");
errors.rejectValue("name", "too.short", "Too & Short");
exposeBindingResult(errors);
int result = this.tag.doStartTag();
assertThat(result).isEqualTo(BodyTag.EVAL_BODY_BUFFERED);
result = this.tag.doEndTag();
assertThat(result).isEqualTo(Tag.EVAL_PAGE);
String output = getOutput();
assertElementTagOpened(output);
assertElementTagClosed(output);
assertContainsAttribute(output, "id", "name.errors");
assertBlockTagContains(output, "<br/>");
assertBlockTagContains(output, "Default <> Message");
assertBlockTagContains(output, "Too & Short");
}
use of org.springframework.beans.testfixture.beans.TestBean in project spring-framework by spring-projects.
the class OptionsTagTests method withCollectionAndCustomEditor.
@Test
void withCollectionAndCustomEditor() throws Exception {
PropertyEditor propertyEditor = new SimpleFloatEditor();
TestBean target = new TestBean();
target.setMyFloat(Float.valueOf("12.34"));
BeanPropertyBindingResult errors = new BeanPropertyBindingResult(target, COMMAND_NAME);
errors.getPropertyAccessor().registerCustomEditor(Float.class, propertyEditor);
exposeBindingResult(errors);
getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, new BindStatus(getRequestContext(), "testBean.myFloat", false));
List<Float> floats = new ArrayList<>();
floats.add(Float.valueOf("12.30"));
floats.add(Float.valueOf("12.31"));
floats.add(Float.valueOf("12.32"));
floats.add(Float.valueOf("12.33"));
floats.add(Float.valueOf("12.34"));
floats.add(Float.valueOf("12.35"));
this.tag.setItems(floats);
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();
List children = rootElement.elements();
assertThat(children.size()).as("Incorrect number of children").isEqualTo(6);
Element element = (Element) rootElement.selectSingleNode("option[text() = '12.34f']");
assertThat(element).as("Option node should not be null").isNotNull();
assertThat(element.attribute("selected").getValue()).as("12.34 node not selected").isEqualTo("selected");
assertThat(element.attribute("id")).as("No id rendered").isNull();
element = (Element) rootElement.selectSingleNode("option[text() = '12.35f']");
assertThat(element).as("Option node should not be null").isNotNull();
assertThat(element.attribute("selected")).as("12.35 node incorrectly selected").isNull();
assertThat(element.attribute("id")).as("No id rendered").isNull();
}
Aggregations