📄 bindtagtests.java
字号:
assertEquals("", pc.getAttribute(NestedPathTag.NESTED_PATH_VARIABLE_NAME, PageContext.REQUEST_SCOPE));
}
public void testNestedPathDoEndTagWithNesting() throws JspException {
PageContext pc = createPageContext();
NestedPathTag tag = new NestedPathTag();
tag.setPath("foo");
tag.setPageContext(pc);
tag.doStartTag();
NestedPathTag anotherTag = new NestedPathTag();
anotherTag.setPageContext(pc);
anotherTag.setPath("bar");
anotherTag.doStartTag();
anotherTag.doEndTag();
assertEquals("foo.", pc.getAttribute(NestedPathTag.NESTED_PATH_VARIABLE_NAME, PageContext.REQUEST_SCOPE));
tag.doEndTag();
assertEquals("", pc.getAttribute(NestedPathTag.NESTED_PATH_VARIABLE_NAME, PageContext.REQUEST_SCOPE));
}
public void testNestedPathDoStartTagInternal() throws JspException {
PageContext pc = createPageContext();
NestedPathTag tag = new NestedPathTag();
tag.setPath("foo");
tag.setPageContext(pc);
int returnValue = tag.doStartTag();
assertEquals(Tag.EVAL_BODY_INCLUDE, returnValue);
assertEquals("foo.", pc.getAttribute(NestedPathTag.NESTED_PATH_VARIABLE_NAME, PageContext.REQUEST_SCOPE));
}
public void testNestedPathDoStartTagInternalWithNesting() throws JspException {
PageContext pc = createPageContext();
NestedPathTag tag = new NestedPathTag();
tag.setPath("foo");
tag.setPageContext(pc);
tag.doStartTag();
assertEquals("foo.", pc.getAttribute(NestedPathTag.NESTED_PATH_VARIABLE_NAME, PageContext.REQUEST_SCOPE));
NestedPathTag anotherTag = new NestedPathTag();
anotherTag.setPageContext(pc);
anotherTag.setPath("bar");
anotherTag.doStartTag();
assertEquals("foo.bar.", pc.getAttribute(NestedPathTag.NESTED_PATH_VARIABLE_NAME, PageContext.REQUEST_SCOPE));
NestedPathTag yetAnotherTag = new NestedPathTag();
yetAnotherTag.setPageContext(pc);
yetAnotherTag.setPath("boo");
yetAnotherTag.doStartTag();
assertEquals("foo.bar.boo.", pc.getAttribute(NestedPathTag.NESTED_PATH_VARIABLE_NAME, PageContext.REQUEST_SCOPE));
yetAnotherTag.doEndTag();
NestedPathTag andAnotherTag = new NestedPathTag();
andAnotherTag.setPageContext(pc);
andAnotherTag.setPath("boo2");
andAnotherTag.doStartTag();
assertEquals("foo.bar.boo2.", pc.getAttribute(NestedPathTag.NESTED_PATH_VARIABLE_NAME, PageContext.REQUEST_SCOPE));
}
public void testNestedPathWithBindTag() throws JspException {
PageContext pc = createPageContext();
BindException errors = new ServletRequestDataBinder(new TestBean(), "tb").getErrors();
pc.getRequest().setAttribute(BindException.ERROR_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);
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);
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);
assertSame("Status matches previous status", status, status3);
assertEquals("tb.name", status.getPath());
assertEquals("Correct field value", "", status.getDisplayValue());
bindTag.doEndTag();
nestedPathTag.doEndTag();
}
public void testNestedPathWithBindTagWithIgnoreNestedPath() throws JspException {
PageContext pc = createPageContext();
BindException errors = new ServletRequestDataBinder(new TestBean(), "tb2").getErrors();
pc.getRequest().setAttribute(BindException.ERROR_KEY_PREFIX + "tb2", errors);
NestedPathTag tag = new NestedPathTag();
tag.setPath("tb");
tag.setPageContext(pc);
tag.doStartTag();
BindTag bindTag = new BindTag();
bindTag.setPageContext(pc);
bindTag.setIgnoreNestedPath(true);
bindTag.setPath("tb2.name");
assertTrue("Correct doStartTag return value", bindTag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME);
assertTrue("Has status variable", status != null);
assertEquals("tb2.name", status.getPath());
}
public void testTransformTagCorrectBehavior() throws JspException {
// first set up the pagecontext and the bean
PageContext pc = createPageContext();
TestBean tb = new TestBean();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
ServletRequestDataBinder binder = new ServletRequestDataBinder(tb, "tb");
CustomDateEditor l = new CustomDateEditor(df, true);
binder.registerCustomEditor(Date.class, l);
pc.getRequest().setAttribute(BindException.ERROR_KEY_PREFIX + "tb", binder.getErrors());
// execute the bind tag using the date property
BindTag bind = new BindTag();
bind.setPageContext(pc);
bind.setPath("tb.date");
bind.doStartTag();
// transform stuff
TransformTag transform = new TransformTag();
transform.setPageContext(pc);
pc.setAttribute("date", tb.getDate());
transform.setParent(bind);
transform.setValue("${date}");
transform.setVar("theDate");
transform.doStartTag();
assertNotNull(pc.getAttribute("theDate"));
assertEquals(pc.getAttribute("theDate"), df.format(tb.getDate()));
// try another time, this time using Strings
bind = new BindTag();
bind.setPageContext(pc);
bind.setPath("tb.name");
bind.doStartTag();
transform = new TransformTag();
transform.setPageContext(pc);
pc.setAttribute("string", "name");
transform.setValue("${string}");
transform.setParent(bind);
transform.setVar("theString");
transform.doStartTag();
assertNotNull(pc.getAttribute("theString"));
assertEquals(pc.getAttribute("theString"), "name");
}
public void testTransformTagWithHtmlEscape() throws JspException {
// first set up the PageContext and the bean
PageContext pc = createPageContext();
TestBean tb = new TestBean();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
ServletRequestDataBinder binder = new ServletRequestDataBinder(tb, "tb");
CustomDateEditor l = new CustomDateEditor(df, true);
binder.registerCustomEditor(Date.class, l);
pc.getRequest().setAttribute(BindException.ERROR_KEY_PREFIX + "tb", binder.getErrors());
// try another time, this time using Strings
BindTag bind = new BindTag();
bind.setPageContext(pc);
bind.setPath("tb.name");
bind.doStartTag();
TransformTag transform = new TransformTag();
transform.setPageContext(pc);
pc.setAttribute("string", "na<me");
transform.setValue("${string}");
transform.setParent(bind);
transform.setVar("theString");
transform.setHtmlEscape("true");
transform.doStartTag();
assertNotNull(pc.getAttribute("theString"));
assertEquals(pc.getAttribute("theString"), "na<me");
}
public void testTransformTagOutsideBindTag() throws JspException {
// first set up the pagecontext and the bean
PageContext pc = createPageContext();
TestBean tb = new TestBean();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
ServletRequestDataBinder binder = new ServletRequestDataBinder(tb, "tb");
CustomDateEditor l = new CustomDateEditor(df, true);
binder.registerCustomEditor(Date.class, l);
pc.getRequest().setAttribute(BindException.ERROR_KEY_PREFIX + "tb", binder.getErrors());
// now try to execute the tag outside a bindtag
TransformTag transform = new TransformTag();
transform.setPageContext(pc);
transform.setVar("var");
transform.setValue("bla");
try {
transform.doStartTag();
fail("Tag can be executed outside BindTag");
}
catch (JspException e) {
// this is ok!
}
// now try to execute the tag outside a bindtag, but inside a messageTag
MessageTag message = new MessageTag();
message.setPageContext(pc);
transform = new TransformTag();
transform.setPageContext(pc);
transform.setVar("var");
transform.setValue("bla");
transform.setParent(message);
try {
transform.doStartTag();
fail("Tag can be executed outside BindTag and inside messagtag");
}
catch (JspException e) {
// this is ok!
}
}
public void testTransformTagNonExistingValue() throws JspException {
// first set up the pagecontext and the bean
PageContext pc = createPageContext();
TestBean tb = new TestBean();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
ServletRequestDataBinder binder = new ServletRequestDataBinder(tb, "tb");
CustomDateEditor l = new CustomDateEditor(df, true);
binder.registerCustomEditor(Date.class, l);
pc.getRequest().setAttribute(BindException.ERROR_KEY_PREFIX + "tb", binder.getErrors());
// try with non-existing value
BindTag bind = new BindTag();
bind.setPageContext(pc);
bind.setPath("tb.name");
bind.doStartTag();
TransformTag transform = new TransformTag();
transform.setPageContext(pc);
transform.setValue("${string2}");
transform.setParent(bind);
transform.setVar("theString2");
transform.doStartTag();
assertNull(pc.getAttribute("theString2"));
}
public void testTransformTagWithSettingOfScope() throws JspException {
// first set up the pagecontext and the bean
PageContext pc = createPageContext();
TestBean tb = new TestBean();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
ServletRequestDataBinder binder = new ServletRequestDataBinder(tb, "tb");
CustomDateEditor l = new CustomDateEditor(df, true);
binder.registerCustomEditor(Date.class, l);
pc.getRequest().setAttribute(BindException.ERROR_KEY_PREFIX + "tb", binder.getErrors());
// execute the bind tag using the date property
BindTag bind = new BindTag();
bind.setPageContext(pc);
bind.setPath("tb.date");
bind.doStartTag();
// transform stuff
TransformTag transform = new TransformTag();
transform.setPageContext(pc);
pc.setAttribute("date", tb.getDate());
transform.setParent(bind);
transform.setValue("${date}");
transform.setVar("theDate");
transform.setScope("page");
transform.doStartTag();
transform.release();
assertNotNull(pc.getAttribute("theDate"));
assertEquals(pc.getAttribute("theDate"), df.format(tb.getDate()));
// try another time, this time using Strings
bind = new BindTag();
bind.setPageContext(pc);
bind.setPath("tb.name");
bind.doStartTag();
transform = new TransformTag();
transform.setPageContext(pc);
pc.setAttribute("string", "name");
pc.setAttribute("scopy", "page");
transform.setValue("${string}");
transform.setParent(bind);
transform.setVar("theString");
transform.setScope("${scopy}");
transform.doStartTag();
transform.release();
assertNotNull(pc.getAttribute("theString"));
assertEquals(pc.getAttribute("theString"), "name");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -