"James Mitchell" <[EMAIL PROTECTED]> wrote in message
0cf101c2d2bb$4e5250d0$1f64f80a@atlanta">news:0cf101c2d2bb$4e5250d0$1f64f80a@atlanta...
> Lately, I've been focusing on building tests that cover the entire core
> struts taglibs.  I've completed logic and I'm now hacking away on the bean
> tags.

Cool!

>
> I don't claim to be expert with Cactus, but there seem to be 2 different
> approaches with respect to testing taglibs.  (There are probably more, but
> these 2 really seem to stick out in my mind)
>
> The existing logic tags use the first approach which is programmatically
> mimicing what the container will do wrt life cycle calls, the testing the
> output or (in the case of boolean results) tag.condition(0, 0).
>
> The second approach is to actually use a jsp to test the tag (this seems
> more natural to me, and tests more peices of the puzzle IMHO).  But this
> comes at the cost of time for page compilation.  Personnally, I can live
> with that cost, for the benefits of using the real thing.  Doing it this
way
> also covers a long time bullet item that I've been wanting to complete for
a
> quite some time. (Complete examples of every tag, with every conceivable
> configuration)

I prefer this second approach, because then we're testing the real behaviour
of the tags in a real container. Using a mock approach makes me nervous
because of the intricacies of tag lifecycles, and I wouldn't feel as
confident that the tags would work in the real world just because they
worked in a mock environment. I too can live with the cost of page
compilation time, traded against a better assurance that the tags are being
properly tested.

I do, however, agree with David that we need to test the tags in isolation.
My preferred solution to this would be to use JSTL tags for the surrounding
logic, instead of Struts tags. Unfortunately, this would preclude testing
the tags in a JSP 1.1 environment. Personally, I'd be OK with that - having
the tests run only in a JSP 1.2 environment is still way better than having
no tests! However, if others are not comfortable with this, then I think
we'd have to resort to using scriptlets in the test pages to isolate the tag
tests.

--
Martin Cooper


>
>
> For example:
> When building the tests for the o.a.s.t.b.TestCookieTag, a typical test
> looks like this:
>
> ...
> ...
> //============================================================
>     public void beginCookieName(WebRequest testRequest) {
>        testRequest.addCookie(COOKIE_KEY, COOKIE_VAL);
>     }
>
>     public void testCookieName()
>      throws ServletException,  JspException, IOException {
>
>         CookieTag tag = new CookieTag();
>   tag.setPageContext(pageContext);
>         tag.setName(COOKIE_KEY);
>         tag.setId("theId");
>         tag.doStartTag();
>
>         Cookie cookie = (Cookie)pageContext.getAttribute("theId");
>         assertEquals("Verify that the cookie was defined properly as a
> scripting variable",
>            COOKIE_VAL,
>            cookie.getValue());
>
>
>
>
> I rewrote this test to call a jsp instead:
>    //====================================================
>     public void beginCookieName(WebRequest webRequest) {
>         webRequest.addCookie(COOKIE_KEY, COOKIE_VAL);
>         webRequest.addParameter("cacheId", "1");
>     }
>
>     public void testCookieName()
>         throws ServletException,  JspException, IOException {
>
>         request.setAttribute("runTest", "testCookieName");
>
>
pageContext.forward("/test/org/apache/struts/taglib/bean/TestCookieTag.jsp")
> ;
>
>     }
>
>
> then in the jsp, I do this:
> -----------------------------------------------------------------
> <%@ page contentType="text/html;charset=UTF-8" language="java" %>
> <%@ page import="junit.framework.Assert"%>
> <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
> <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
>
> <bean:define id="COOKIE_KEY"
> value="org.apache.struts.taglib.bean.COOKIE_KEY"/>
> <bean:define id="COOKIE_VAL" value="Testing"/>
>
> <logic:equal name="runTest" value="testCookieName">
>  <bean:cookie id="cookie"
name="org.apache.struts.taglib.bean.COOKIE_KEY"/>
>  <bean:define id="cookieId" name="cookie" property="value"/>
>  <% Assert.assertEquals(COOKIE_VAL, cookieId); %>
> </logic:equal>
>
>
>
> I wanted to know if any of you have a preference to how we do this.
>
> Comments?
>
>
>
> --
> James Mitchell




---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to