use of org.springframework.validation.Errors in project spring-framework by spring-projects.
the class BindTagTests method bindTagWithGlobalErrorsAndDefaultMessageOnly.
@Test
public void bindTagWithGlobalErrorsAndDefaultMessageOnly() throws JspException {
PageContext pc = createPageContext();
Errors errors = new ServletRequestDataBinder(new TestBean(), "tb").getBindingResult();
errors.reject(null, "message1");
pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
BindTag tag = new BindTag();
tag.setPageContext(pc);
tag.setPath("tb");
assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
assertTrue("Has status variable", status != null);
assertTrue("Correct expression", status.getExpression() == null);
assertTrue("Correct value", status.getValue() == null);
assertTrue("Correct displayValue", "".equals(status.getDisplayValue()));
assertTrue("Correct isError", status.isError());
assertTrue("Correct errorMessages", status.getErrorMessages().length == 1);
assertTrue("Correct errorMessage", "message1".equals(status.getErrorMessage()));
assertTrue("Correct errorMessagesAsString", "message1".equals(status.getErrorMessagesAsString(",")));
tag = new BindTag();
tag.setPageContext(pc);
tag.setPath("tb.*");
assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
assertTrue("Has status variable", status != null);
assertTrue("Correct expression", "*".equals(status.getExpression()));
assertTrue("Correct value", status.getValue() == null);
assertTrue("Correct displayValue", "".equals(status.getDisplayValue()));
assertTrue("Correct isError", status.isError());
assertTrue("Correct errorMessages", status.getErrorMessages().length == 1);
assertTrue("Correct errorMessage", "message1".equals(status.getErrorMessage()));
assertTrue("Correct errorMessagesAsString", "message1".equals(status.getErrorMessagesAsString(",")));
}
use of org.springframework.validation.Errors in project spring-framework by spring-projects.
the class BindTagTests method bindTagWithMappedProperties.
@Test
public void bindTagWithMappedProperties() throws JspException {
PageContext pc = createPageContext();
IndexedTestBean tb = new IndexedTestBean();
Errors errors = new ServletRequestDataBinder(tb, "tb").getBindingResult();
errors.rejectValue("map[key1]", "code1", "message1");
errors.rejectValue("map[key1]", "code2", "message2");
pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
BindTag tag = new BindTag();
tag.setPageContext(pc);
tag.setPath("tb.map[key1]");
assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
assertTrue("Has status variable", status != null);
assertTrue("Correct expression", "map[key1]".equals(status.getExpression()));
assertTrue("Value is TestBean", status.getValue() instanceof TestBean);
assertTrue("Correct value", "name4".equals(((TestBean) status.getValue()).getName()));
assertTrue("Correct isError", status.isError());
assertTrue("Correct errorCodes", status.getErrorCodes().length == 2);
assertTrue("Correct errorMessages", status.getErrorMessages().length == 2);
assertTrue("Correct errorCode", "code1".equals(status.getErrorCodes()[0]));
assertTrue("Correct errorCode", "code2".equals(status.getErrorCodes()[1]));
assertTrue("Correct errorMessage", "message1".equals(status.getErrorMessages()[0]));
assertTrue("Correct errorMessage", "message2".equals(status.getErrorMessages()[1]));
}
use of org.springframework.validation.Errors in project spring-framework by spring-projects.
the class BindTagTests method nestedPathWithBindTag.
@Test
public void nestedPathWithBindTag() throws JspException {
PageContext pc = createPageContext();
Errors errors = new ServletRequestDataBinder(new TestBean(), "tb").getBindingResult();
pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
NestedPathTag nestedPathTag = new NestedPathTag();
nestedPathTag.setPath("tb");
nestedPathTag.setPageContext(pc);
nestedPathTag.doStartTag();
BindTag bindTag = new BindTag();
bindTag.setPageContext(pc);
bindTag.setPath("name");
assertTrue("Correct doStartTag return value", bindTag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
assertTrue("Has status variable", status != null);
assertEquals("tb.name", status.getPath());
assertEquals("Correct field value", "", status.getDisplayValue());
BindTag bindTag2 = new BindTag();
bindTag2.setPageContext(pc);
bindTag2.setPath("age");
assertTrue("Correct doStartTag return value", bindTag2.doStartTag() == Tag.EVAL_BODY_INCLUDE);
BindStatus status2 = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
assertTrue("Has status variable", status2 != null);
assertEquals("tb.age", status2.getPath());
assertEquals("Correct field value", "0", status2.getDisplayValue());
bindTag2.doEndTag();
BindStatus status3 = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
assertSame("Status matches previous status", status, status3);
assertEquals("tb.name", status.getPath());
assertEquals("Correct field value", "", status.getDisplayValue());
bindTag.doEndTag();
nestedPathTag.doEndTag();
}
use of org.springframework.validation.Errors in project spring-framework by spring-projects.
the class BindTagTests method bindTagWithGlobalErrors.
@Test
public void bindTagWithGlobalErrors() throws JspException {
PageContext pc = createPageContext();
Errors errors = new ServletRequestDataBinder(new TestBean(), "tb").getBindingResult();
errors.reject("code1", "message1");
pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
BindTag tag = new BindTag();
tag.setPageContext(pc);
tag.setPath("tb");
assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
assertTrue("Has status variable", status != null);
assertTrue("Correct expression", status.getExpression() == null);
assertTrue("Correct value", status.getValue() == null);
assertTrue("Correct displayValue", "".equals(status.getDisplayValue()));
assertTrue("Correct isError", status.isError());
assertTrue("Correct errorCodes", status.getErrorCodes().length == 1);
assertTrue("Correct errorMessages", status.getErrorMessages().length == 1);
assertTrue("Correct errorCode", "code1".equals(status.getErrorCode()));
assertTrue("Correct errorMessage", "message1".equals(status.getErrorMessage()));
assertTrue("Correct errorMessagesAsString", "message1".equals(status.getErrorMessagesAsString(",")));
tag = new BindTag();
tag.setPageContext(pc);
tag.setPath("tb.*");
assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
assertTrue("Has status variable", status != null);
assertTrue("Correct expression", "*".equals(status.getExpression()));
assertTrue("Correct value", status.getValue() == null);
assertTrue("Correct displayValue", "".equals(status.getDisplayValue()));
assertTrue("Correct isError", status.isError());
assertTrue("Correct errorCodes", status.getErrorCodes().length == 1);
assertTrue("Correct errorMessages", status.getErrorMessages().length == 1);
assertTrue("Correct errorCode", "code1".equals(status.getErrorCode()));
assertTrue("Correct errorMessage", "message1".equals(status.getErrorMessage()));
assertTrue("Correct errorMessagesAsString", "message1".equals(status.getErrorMessagesAsString(",")));
}
use of org.springframework.validation.Errors in project spring-framework by spring-projects.
the class BindTagTests method bindTagWithIndexedProperties.
@Test
public void bindTagWithIndexedProperties() throws JspException {
PageContext pc = createPageContext();
IndexedTestBean tb = new IndexedTestBean();
Errors errors = new ServletRequestDataBinder(tb, "tb").getBindingResult();
errors.rejectValue("array[0]", "code1", "message1");
errors.rejectValue("array[0]", "code2", "message2");
pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
BindTag tag = new BindTag();
tag.setPageContext(pc);
tag.setPath("tb.array[0]");
assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
assertTrue("Has status variable", status != null);
assertTrue("Correct expression", "array[0]".equals(status.getExpression()));
assertTrue("Value is TestBean", status.getValue() instanceof TestBean);
assertTrue("Correct value", "name0".equals(((TestBean) status.getValue()).getName()));
assertTrue("Correct isError", status.isError());
assertTrue("Correct errorCodes", status.getErrorCodes().length == 2);
assertTrue("Correct errorMessages", status.getErrorMessages().length == 2);
assertTrue("Correct errorCode", "code1".equals(status.getErrorCodes()[0]));
assertTrue("Correct errorCode", "code2".equals(status.getErrorCodes()[1]));
assertTrue("Correct errorMessage", "message1".equals(status.getErrorMessages()[0]));
assertTrue("Correct errorMessage", "message2".equals(status.getErrorMessages()[1]));
}
Aggregations