Professional Java Tools for Extreme Programming [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Professional Java Tools for Extreme Programming [Electronic resources] - نسخه متنی

Richard Hightower, Warner Onstineet al.

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
توضیحات
افزودن یادداشت جدید

Testing Tag Attributes and Page Interaction

Attributes of a custom tag are implemented as setter methods on the tag handler class, to be called before the tag's processing methods begin. The mapping of tag attributes to methods is specified in the tag library descriptor file, which includes an "attribute" element for each tag attribute, with subelements specifying the name, whether the attribute is required, and whether its value can be specified as a runtime expression. To examine how to test a tag that uses attributes, we'll look at a simple tag. The ifParameterEquals tag checks the request for a named parameter and evaluates the tag body if the parameter is equal to a specified value. In the following snippet, ifParameterEquals prints "Consuelo Jones" to the page writer if the "iWantConsuelo" request parameter is set to "true":

<example:ifParameterEquals name="iWantConsuelo" value="true">
Consuelo Jones
</example:ifParameterEquals>

The following listing contains the tag handler class for ifParameterEquals.

public class IfParameterEqualsTag extends TagSupport {
protected String name = null;
protected String value = null;
public String getName() {
return (this.name);
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return (this.value);
}
public void setValue(String value) {
this.value = value;
}
/**
* Compare the specified parameter to the specified value, and decide
* whether or not to include the body content.
*
* @exception JspException if a JSP exception has occurred
*/
public int doStartTag() throws JspException {
// Retrieve the value of the specified parameter
HttpServletRequest request =
(HttpServletRequest) pageContext.getRequest();
String compare = request.getParameter(name);
if (compare == null)
compare = ";
// Conditionally evaluate the body of our tag
if (compare.equals(value))
return (EVAL_BODY_INCLUDE);
else
return (SKIP_BODY);
}
public void release() {
super.release();
name = null;
value = null;
}
}

A quick examination of the generated servlet shows us how Tomcat would set up the tag:

xptoolkit.cactus.IfParameterEqualsTag       
_jspx_th_example_ifParameterEquals_0 =
new xptoolkit.cactus.IfParameterEqualsTag(); _jspx_th_example_
ifParameterEquals_0.setPageContext(pageContext);
_jspx_th_example_ifParameterEquals_0.setParent(null);
_jspx_th_example_ifParameterEquals_0.setName("iWantConsuelo");
_jspx_th_example_ifParameterEquals_0.setValue("true");
Our test case will attempt to replicate this. First,
we add the required parameter to the request:
public void beginPresent(ServletTestRequest request){
request.addParameter("iWantConsuelo", "true");
}

Then, we initialize the tag's attributes in setUp(). Notice that we do not have to write special steps to allow the tag access to the request; the pageContext variable takes care of that for us:

public void setUp(){
tag = new IfParameterEqualsTag();
tag.setPageContext(this.pageContext);
tag.setName("iWantConsuelo");
tag.setValue("true");
}

We write the test method to call doStartTag() and check that it returns EVAL_BODY_INCLUDE:

public void testPresent() throws Exception{
assertEquals(tag.EVAL_BODY_INCLUDE, tag.doStartTag());
}

This verifies that the tag will include the body—we don't need to add body content and check that it shows up in the response. In order to verify that the tag works when the request does not contain the expected parameter, we write another test method:

public void testNotPresent()throws Exception{
assertEquals(tag.SKIP_BODY, tag.doStartTag());
}

Because we have not specified a beginNotPresent() method, no parameters will be added to the request.


Managing Information in Scopes


Tag handler classes have access to information in four possible scopes: page, request, session, and application. As we saw in the previous example, the pageContext object manages access to these scopes. The pageContext view of the scopes is live, so that anything set in, say, the request object immediately becomes available through a call to pageContext.getAttribute("name", PageContext.REQUEST_SCOPE). This relationship works in reverse as well, so you can use it to verify that your tags are properly modifying the various implicit objects. For instance, to check that a tag has mapped a String into the session under the key "hotProductChoice," you could use this:

tag.doStartTag()//or the appropriate tag lifecycle method
assertNotNull(session.getAttribute("hotProductChoice"));

See "Testing Auxiliary Tag Components" for a more involved example.

/ 228