Testing Simple Tags


You can quickly put together a unit test for a simple tag. Let's start with the equivalent of "hello world," a custom tag that prints one line of output to the page's JspWriter and ignores any body. The tag handler class appears in the following listing.

 public class ExampleTag extends BodyTagSupport {        public int doStartTag() {     try {       JspWriter out = pageContext.getOut();       out.print("simple tag");     } catch(IOException ioe) {       throw new RuntimeException(ioe.toString());     }     return(SKIP_BODY);   } } 

This tag submits to Cactus testing without a whimper. Because the tag handler implements only one method, all we have to do is initialize the custom tag, call its one method (doStartTag ()), and inspect the results.

We begin creating the test case by extending JspTestCase. JspTestCase operates like ServletTestCase, except that it uses a JSP to execute the test methods and provides two additional implicit variables available in JSPs: pageContext and out. These variables are simply set to the variables of the same name in redirector.jsp. To test our tag, we initialize it in the setUp() method. The initialization consists of two steps: instantiating the tag and setting the pageContext:

 public void setUp(){   tag = new ExampleTag();   /*pageContext set with the JspTestCase instance variable*/   tag.setPageContext(this.pageContext); } 

The pageContext object grants the tag access to all the servlet implicit objects, as well as the output writer from the page.

Once the test case has initialized the tag, we exercise its behavior in our test method. In this case, we call doStartTag() and verify that it returns the SKIP_BODY constant from the javax.servlet.jsp. tagext .Tag interface:

 public void testDoStartTag() {   System.out.println("testDoStartTag");   int result = tag.doStartTag();   assertEquals(tag.SKIP_BODY, result); } 

But wait! How do we verify that the String "simple tag" was written to the output? Easy enough: We simply use the endXXX() method and inspect the response. Because doStartTag() is the only candidate for writing to the output, it must be responsible for anything that shows up in the response. Here is the endDoStartTag() method:

 public void endDoStartTag(WebResponse resp) throws Exception{   String response = resp.getText();   boolean containsText = response.indexOf("simple tag") > -1;   assert(containsText); } 

These are the basic steps in any tag test: Set up the tag according to the specification, call the tag handler's life-cycle method in isolation, and inspect the response if necessary. Of course, a test like this will not verify the whole tag because it does not interact with the tag deployment descriptor and/or TagExtraInfo class. See the "Testing Auxiliary Tag Components" section for strategies related to these extra elements of a custom tag.




Professional Java Tools for Extreme Programming
Professional Java Tools for Extreme Programming: Ant, XDoclet, JUnit, Cactus, and Maven (Programmer to Programmer)
ISBN: 0764556177
EAN: 2147483647
Year: 2003
Pages: 228

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net