You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cactus-user@jakarta.apache.org by "Glania, Damian" <Da...@IZB-SOFT.de> on 2002/10/25 15:45:04 UTC

using body content in conditional custom tags (if then else)

Hi all,
I'm trying to test my own "if", "then" and "else" custom tags using Cactus
(1.4.1). 
I previously tested the tags using simple JSPs, so I assume, they work
properly.

All my cactus tests work properly with Tomcat4.0. If I use Tomcat 4.1 then
one test fails.
The failing test case is very simple: after setting the if-condition to
"false" I expect (in endXXX)
to find in the output only the content of the "else" tag but not the content
of "then" tag.
The assertion fails, because it both contents (from "then" and "else") are
found.
(The test with condition set to "true" works on both Tomcat versions)

I assume, I'm using the pageContext.pushBody() and/or pageContext.popBody()
somewhere wrong.
Could you please suggest a proper way to test such "conditional body tags"?

Here, how I'm setting the "then" and "else" tags:
..
        // create a 'then' tag
        IfThenTag thenTag = new IfThenTag();
        thenTag.setPageContext(pageContext);
        thenTag.setParent(tag);  // 'if' is parent of 'then'
        assertEquals("Eval body expexted", BodyTag.EVAL_BODY_TAG,
thenTag.doStartTag());

        //obtain the bodyContent object for 'then' tag
        BodyContent bodyContent = pageContext.pushBody();
        thenTag.setBodyContent(bodyContent);
        thenTag.doInitBody();

        //write some "output" into the bodyContent so that endIfTrueThenElse
can test for it.
        bodyContent.println("This is the content of 'then' (if condition was
true)");

        // doAfterBody() actually handles the processing of the body
        assertEquals("Skip body expexted", BodyTag.SKIP_BODY,
thenTag.doAfterBody());

        //after the body processing completes
        thenTag.doEndTag();

        //finally call popBody for thenTag
        //necessary for tag to output anything on most servlet engines.
        pageContext.popBody();


        // create a 'else' tag
        IfElseTag elseTag = new IfElseTag();
        elseTag.setPageContext(pageContext);
        elseTag.setParent(tag);  // 'if' parent of 'else'
        assertEquals("Eval body expexted", BodyTag.EVAL_BODY_TAG,
elseTag.doStartTag());

        //obtain the bodyContent object for 'else' tag
        bodyContent = pageContext.pushBody();
        elseTag.setBodyContent(bodyContent);
        elseTag.doInitBody();

        //write some "output" into the bodyContent so that endIfTrueThenElse
can test for it.
        bodyContent.println("This is the content of 'else' (if condition was
false )");

        // doAfterBody() actually handles the processing of the body
        assertEquals("Skip body expexted", BodyTag.SKIP_BODY,
elseTag.doAfterBody());

        //after the body processing completes
        elseTag.doEndTag();

        //finally call popBody for elseTag
        //necessary for tag to output anything on most servlet engines.
        pageContext.popBody();

        assertEquals("Skip body expexted", Tag.SKIP_BODY,
tag.doAfterBody());


As I said, the "setting" code above works perfectly well on both Tomcat
versions. 
The endIfFalseXXX() however fails on Tomcat 4.1 because the following
assertion:

        assertTrue("Unexpected content of 'then' found",
            content.indexOf("This is the content of 'then' (if condition was
true)") == -1);

fails.

Thanks in advance,
  Damian

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>