Heikki Doeleman wrote:
> 
> Hi Vincent,
> 
> not really, I've found more questions and other issues than answers and
> examples :)
> 
> It would be useful if the documentation on this was more extensive and
> contained some examples of this.
> 
> Did anyone write tests for custom tag libraries in the Struts framework?
> please post some examples of how you did it ..
> 
>



Ok. 

You can use the JspTestCase to test custom tag libraries, it requires a
good understanding of the TagLib api.

I keep promising to write this up and haven't yet... :( (my wedding is
coming up along with new work project).

Basically you can do the following:

The following would be a simple test of a Tag (not BodyTag). It is a
very simple sample tag called CheckboxTag to make a checkbox in a JSP
page (something we discarded, but hey, it's good enough for a sample :)

I will summarise here, but you just need to create and setPageContext
when you create the tag. You may want to call release() in the
tearDown() method, and/or write a testRelease method to check it works
properly.

Have a look at a generated JSP page to see how your container compiles
it into Java. That will give you an idea of how it is called. I have not
doen the equivalent exercise for a BodyTag yet, although Nicholas
Lesiecki I think has. Nick can you comment?

see following posts also:

http://www.mail-archive.com/cactus-user%40jakarta.apache.org/msg00286.html
as the start of a thread about Tag testing.

So if you had a tag description like so:
        <tag name='checkbox' tagclass='com.hyperlink.eos.web.thoth.CheckboxTag'
bodycontent='empty'>
        <attribute name='name' required='true' />
        <attribute name='value' required='false' />
        <attribute name='expr' required='false' rtexprvalue='true'/>
        <info>
                'Checkbox is set if the expression is true. The default is unchecked' 
        </info>

and a simple CheckboxTag class like so:

public class CheckboxTag extends TagSupport
{
        private String _name="";
        private String _value="";
        private boolean _expr=false;

        public void setExpr(boolean expr) { _expr = expr; }
        public boolean getExpr() { return _expr; }

        public void setName(String name) { _name = name; }
        public String getName() { return _name; }

        public void setValue(String value) { _value = value; }
        public String getValue() { return _value; }

        public int doEndTag() throws JspException
        {

                StringBuffer b = new StringBuffer("<input type=\"checkbox\" name=\"");
                b.append(_name).append("\"");

                if (!"".equals(_value))
                {
                        b.append(" value=\"").append(_value).append("\"");
                }

                if (_expr)
                {
                        b.append(" CHECKED");
                }
                b.append(">");

                try
                {
                        pageContext.getOut().print(b.toString());
                }
                catch (IOException e)
                {
                        throw new JspException("IO Exception: " + e.getMessage());
                }

                return EVAL_PAGE;
        }
}



Then you could test the class like so:

public class TestCheckboxTag extends JspTestCase
{
    private CheckboxTag _tag;

    public TestCheckboxTag(String name)
    {
        super(name);
    }

    public static Test suite()
    {
        return new TestSuite(TestCheckboxTag.class);
    }

    public void setUp()
    {
        _tag = new CheckboxTag();
        _tag.setPageContext(this.pageContext);
    }

    public void tearDown()
    {
        _tag = null;
    }

    /**
     * Test with name value and expression
     */
    public void testNameValueExpression()
        throws JspException
    {
        _tag.setName("name");
        _tag.setValue("value");
        _tag.setExpr(true);
        _tag.doEndTag();
    }

    public void endNameValueExpression(HttpURLConnection connection)
        throws java.io.IOException
    {
        assertEquals("output should be a checkbox",
                        "<input type=\"checkbox\" name=\"name\"
value=\"value\" CHECKED>",
                        AssertUtils.getResponseAsString(connection));
    }

    /**
     * Test with name value and expression
     */
    public void testNameValueExpressionFalse()
        throws JspException
    {
        _tag.setName("name");
        _tag.setValue("value");
        _tag.setExpr(false);
        _tag.doEndTag();
    }

    public void endNameValueExpressionFalse(HttpURLConnection
connection)
        throws java.io.IOException
    {
        assertEquals("output should be a checkbox",
                        "<input type=\"checkbox\" name=\"name\"
value=\"value\">",
                        AssertUtils.getResponseAsString(connection));
    }

    public void testNameValue()
        throws JspException
    {
        _tag.setName("name");
        _tag.setValue("value");
        _tag.doEndTag();
    }

    public void endNameValue(HttpURLConnection connection)
        throws java.io.IOException
    {
        assertEquals("output should be a checkbox",
                        "<input type=\"checkbox\" name=\"name\"
value=\"value\">",
                        AssertUtils.getResponseAsString(connection));
    }

    public void testNameExpression()
       throws JspException
    {
        _tag.setName("name");
        _tag.setExpr(true);
        _tag.doEndTag();
    }

    public void endNameExpr(HttpURLConnection connection)
        throws java.io.IOException
    {
        assertEquals("output should be a checkbox",
                        "<input type=\"checkbox\" name=\"name\"
CHECKED>",
                        AssertUtils.getResponseAsString(connection));
    }


}

--
Jari Worsley
Senior Programmer
Hyperlink Interactive Ltd

Reply via email to