aspose file tools*
The moose likes JSP and the fly likes How do *you* test custom tags? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "How do *you* test custom tags?" Watch "How do *you* test custom tags?" New topic
Author

How do *you* test custom tags?

Simon Brown
sharp shooter, and author
Ranch Hand

Joined: May 10, 2000
Posts: 1913
    
    6
A few of us are throwing around some ideas at the moment and doing some prototyping for an open source framework to test (specifically) custom tags. There are a few different ways in which tags can be tested directly and indirectly, with tools such as Cactus and HttpUnit being two good examples.
I have a few questions that I'd like to throw out to the JavaRanch community.
  • Do you specifically test the custom tags in your web applications?
  • If so, how much testing do you perform?
  • Do you use any specific tools, and what are their pros and cons?

  • Any thoughts?
    Simon
    Simon Brown
    sharp shooter, and author
    Ranch Hand

    Joined: May 10, 2000
    Posts: 1913
        
        6
    Hmmm ... no responses which is kind of what I was expecting. Most people that I've spoken to don't specifically test their tags.
    Shawn Bayern
    Author
    Ranch Hand

    Joined: May 06, 2002
    Posts: 160
    Originally posted by Simon Brown:
    Hmmm ... no responses which is kind of what I was expecting. Most people that I've spoken to don't specifically test their tags.

    Heh.
    One approach I favor (favor, but hardly ever use , since most of my testing is admittedly ad hoc and informal -- I've had the benefit of an outstanding TCK team to do formal testing for the JSTL RI) is to use custom tags to unit-test blocks of JSP code. This allows for flexible test of individual tags, combinations of tags, tags with particular relationships to body content, and so forth. For instance, one could imagine a tag like
    <test:compare key="does.url.encode.session.id">
    <c:url value="foo.bar"/>
    </test:compare>
    where "does.url.encode.session.id" is a key corresponding to the expected body. A flexible framework could also ideally accept a direct string as the target of the comparison; this would be great if you didn't want to bother with outside dependencies. For instance:
    <c:set var="expected">
    1==1
    </c:set>
    <test:compare string="${expected}">
    <c:if test="${1==1}">
    1==1
    </c:if>
    <c:if test="${1==2}">
    1==2
    </c:if>
    </test:compare>
    (I'm using examples based on JSTL's conventions: "var" exposes a variable, and ${} introduces an expression.) Using the one tag that JSTL offers for exception handling (<c:catch> would even let you navigate and report errors flexibly; you'd probably want <test:compare> to throw an exception on bad comparison, which you could catch with <c:catch> if it's expected (e.g., for a "negative" test where you *expect* an exception).
    Sounds like fun, actually. I'd love to see the framework you end up with!


    Shawn Bayern<br />"JSTL in Action" <a href="http://www.jstlbook.com" target="_blank" rel="nofollow">http://www.jstlbook.com</a>
    Simon Brown
    sharp shooter, and author
    Ranch Hand

    Joined: May 10, 2000
    Posts: 1913
        
        6
    Hi Shawn,
    Testing tags with tags is (IMHO too) certainly the way to go since it is much easier to unit test tags in the environment in which they will eventually be run. As you say, you can also test the interactions between tags, generated body content and, also, side-effects on the environment such as introducing variables, page context attributes, cookies, etc. In fact, you can also run assertions over the TLD file to ensure that you've not changed the contract for a particular tag.
    I've been doing some prototyping, throwing some ideas around on a Yahoo! group and I'm in the process of setting up a website and sorting out somewhere to host the project (e.g. sourceforge).
    The website is up and although there's not much on there at the moment, there's some background information, a short FAQ, some ideas and some examples (with screenshots) of the prototype currently looks like.
    Take a look at http://www.tagunit.org for more information and feel free to sign up on the Yahoo! group.
    Once the project is setup I'll post an announcement and hope that it doesn't get thrown into Blatant Advertising! :roll:
    Cheers
    Simon
    p.s. looking forward to the JSTL Book
    ersin eser
    Ranch Hand

    Joined: Feb 22, 2001
    Posts: 1072
    hope that it doesn't get thrown into Blatant Advertising!

    LoL...
    ersin eser
    Ranch Hand

    Joined: Feb 22, 2001
    Posts: 1072
    I am a greenhorn JSP developer. Most of my testing is admittedly ad hoc and informal ++
    Simon, I am definitely going to follow your group and I would like to volunteer too. May be in documentation or tech review ;-) or basic coding :roll: Keep me in the loop.
    regards
    Simon Brown
    sharp shooter, and author
    Ranch Hand

    Joined: May 10, 2000
    Posts: 1913
        
        6
    Originally posted by ersin eser:
    LoL...

    I am keeping my fingers crossed on that one!
    Simon Brown
    sharp shooter, and author
    Ranch Hand

    Joined: May 10, 2000
    Posts: 1913
        
        6
    Originally posted by ersin eser:
    Simon, I am definitely going to follow your group and I would like to volunteer too. May be in documentation or tech review ;-) or basic coding :roll: Keep me in the loop.

    Will do - it would be good to get your feedback. The Sourceforge project has been approved (hurray!) and I'm going to start migrating everything over there shortly. The project homepage can be found at http://sourceforge.net/projects/tagunit .
    Cheers
    Simon
    Jessica Sant
    Sheriff

    Joined: Oct 17, 2001
    Posts: 4313

    Very cool Simon!! if I have any bandwidth in the near future, I'd definately like to help out.
    On a somewhat related note, if you'd like me to introduce you to Russ Gold, the lead guy on HttpUnit, he works here in my office...


    - Jess
    Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
    Simon Brown
    sharp shooter, and author
    Ranch Hand

    Joined: May 10, 2000
    Posts: 1913
        
        6
    Originally posted by Jessica Sant:
    On a somewhat related note, if you'd like me to introduce you to Russ Gold, the lead guy on HttpUnit, he works here in my office...

    How is that!
    Simon
    (I'm thinking that we could use HttpUnit to automate the TagUnit tests)
    Jessica Sant
    Sheriff

    Joined: Oct 17, 2001
    Posts: 4313

    Originally posted by Simon Brown:

    How is that!
    Simon
    (I'm thinking that we could use HttpUnit to automate the TagUnit tests)

    I thought so too -- that's one of the reasons our entire application server was developed with automated testing using JUnit and HTTPUnit -- very slick.
    Michael Yuan
    author
    Ranch Hand

    Joined: Mar 07, 2002
    Posts: 1427
    Hi Simon,
    The book "Java Tools for eXtreme Programming" have some good examples on how to do in-container tag testing using apache cactus.
    Can you tell us more about how your new tagunit framework would be different? I looked up the web site but did not find much info ...
    michael


    Seam Framework: http://www.amazon.com/exec/obidos/ASIN/0137129394/mobileenterpr-20/
    Ringful: http://www.ringful.com/
    Simon Brown
    sharp shooter, and author
    Ranch Hand

    Joined: May 10, 2000
    Posts: 1913
        
        6
    Originally posted by Michael Yuan:
    Can you tell us more about how your new tagunit framework would be different? I looked up the web site but did not find much info ...

    Sorry about that, I'm still in the process of putting together the information and the website.
    The "Java Tools for XP" book is really good and you're right, it has got some examples of how you can test tags using Cactus. The difference between Cactus and TagUnit is that Cactus is a much, much more flexible framework for testing serverside components, including Servlets, JSPs and EJBs. TagUnit on the other hand is specifically for testing custom tags.
    Here's a brief overview of the framework...
    TagUnit is a framework whereby JSP custom tags can be tested inside the container, and in isolation to the pages on which they will ultimately be used. In essence, it's a tag library for testing tags within JSP pages. This means that it is easy to unit test tags, including the content that they generate and the side-effects that they have on the environment such as the introduction of scripting variables, page context attributes, cookies and so on.
    In addition to "unit testing" tags, it is also useful to be able to test the cooperation between tags in a tag library, to ensure that the correct results are produced when tags are used in conjunction with one another. This is more like integration or component testing than unit testing, but is still important and possible within the same framework.
    Finally, assertions can be made on the constraints specified within the tag library descriptor file, allowing tests to be made on the contract that a tag provides.

    As you've probably seen from the tag testing examples from the XP book, you have to write the code that mimics the tag lifecycle. With the TagUnit framework, we're approaching this the other way and letting the container do this for us by testing tags within JSP pages. IMHO, it just makes testing tags easy - you don't have to write any Java code.
    As well as the website, I'm also (thanks to Ersin) going to create an archive of the mailing list messages before the project was hosted at sourceforge, in addition to uploading the current codebase into CVS. If you're interested in seeing how tags are tested within the framework, for the moment, take a look at the examples section of the TagUnit website.
    I hope that gives you more of an insight into what it's all about...
    Cheers
    Simon
    Michael Yuan
    author
    Ranch Hand

    Joined: Mar 07, 2002
    Posts: 1427
    Originally posted by Simon Brown:

    As you've probably seen from the tag testing examples from the XP book, you have to write the code that mimics the tag lifecycle. With the TagUnit framework, we're approaching this the other way and letting the container do this for us by testing tags within JSP pages. IMHO, it just makes testing tags easy - you don't have to write any Java code.

    It sounds like a very interesting project and I will join the mailing list. I just looked at the example page on your site.
    I am also wondering whether it is possible to set up a framework/environment that could automatically generate tag test cases for cactus from an XML config file? That would make cactus painless to use too ... Well, I realize it is probaly beyond what you want to do with TagUnit. But public forums are for wild ideas!
    cheers
    Michael
    [ May 12, 2002: Message edited by: Michael Yuan ]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: How do *you* test custom tags?