use of org.springframework.validation.BeanPropertyBindingResult in project spring-framework by spring-projects.
the class ErrorsTagTests method withExplicitWhitespaceBodyContent.
@Test
public void withExplicitWhitespaceBodyContent() throws Exception {
this.tag.setBodyContent(new MockBodyContent("\t\n ", getWriter()));
// 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");
exposeBindingResult(errors);
int result = this.tag.doStartTag();
assertEquals(BodyTag.EVAL_BODY_BUFFERED, result);
result = this.tag.doEndTag();
assertEquals(Tag.EVAL_PAGE, result);
String output = getOutput();
assertElementTagOpened(output);
assertElementTagClosed(output);
assertContainsAttribute(output, "id", "name.errors");
assertBlockTagContains(output, "Default Message");
}
use of org.springframework.validation.BeanPropertyBindingResult in project spring-framework by spring-projects.
the class ErrorsTagTests method asBodyTagWithErrorsAndExistingMessagesAttributeInNonPageScopeAreNotClobbered.
/**
* https://jira.spring.io/browse/SPR-2788
*/
@Test
public void asBodyTagWithErrorsAndExistingMessagesAttributeInNonPageScopeAreNotClobbered() throws Exception {
String existingAttribute = "something";
getPageContext().setAttribute(ErrorsTag.MESSAGES_ATTRIBUTE, existingAttribute, PageContext.APPLICATION_SCOPE);
Errors errors = new BeanPropertyBindingResult(new TestBean(), "COMMAND_NAME");
errors.rejectValue("name", "some.code", "Default Message");
errors.rejectValue("name", "too.short", "Too Short");
exposeBindingResult(errors);
int result = this.tag.doStartTag();
assertEquals(BodyTag.EVAL_BODY_BUFFERED, result);
assertNotNull(getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE));
assertTrue(getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE) instanceof List);
String bodyContent = "Foo";
this.tag.setBodyContent(new MockBodyContent(bodyContent, getWriter()));
this.tag.doEndTag();
this.tag.doFinally();
assertEquals(bodyContent, getOutput());
assertEquals(existingAttribute, getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE, PageContext.APPLICATION_SCOPE));
}
use of org.springframework.validation.BeanPropertyBindingResult 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();
assertEquals(Tag.SKIP_BODY, result);
result = this.tag.doEndTag();
assertEquals(Tag.EVAL_PAGE, result);
String output = getOutput();
assertEquals(0, output.length());
assertEquals(existingAttribute, getPageContext().getAttribute(ErrorsTag.MESSAGES_ATTRIBUTE, scope));
}
use of org.springframework.validation.BeanPropertyBindingResult 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();
assertEquals(Tag.SKIP_BODY, result);
result = this.tag.doEndTag();
assertEquals(Tag.EVAL_PAGE, result);
String output = getOutput();
assertEquals(0, output.length());
}
use of org.springframework.validation.BeanPropertyBindingResult in project spring-framework by spring-projects.
the class OptionsTagTests method withCollectionAndCustomEditor.
@Test
public void withCollectionAndCustomEditor() throws Exception {
PropertyEditor propertyEditor = new SimpleFloatEditor();
TestBean target = new TestBean();
target.setMyFloat(new Float("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(new Float("12.30"));
floats.add(new Float("12.31"));
floats.add(new Float("12.32"));
floats.add(new Float("12.33"));
floats.add(new Float("12.34"));
floats.add(new Float("12.35"));
this.tag.setItems(floats);
int result = this.tag.doStartTag();
assertEquals(Tag.SKIP_BODY, result);
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();
assertEquals("Incorrect number of children", 6, children.size());
Element element = (Element) rootElement.selectSingleNode("option[text() = '12.34f']");
assertNotNull("Option node should not be null", element);
assertEquals("12.34 node not selected", "selected", element.attribute("selected").getValue());
assertNull("No id rendered", element.attribute("id"));
element = (Element) rootElement.selectSingleNode("option[text() = '12.35f']");
assertNotNull("Option node should not be null", element);
assertNull("12.35 node incorrectly selected", element.attribute("selected"));
assertNull("No id rendered", element.attribute("id"));
}
Aggregations