Test your JSP tag library deployment

Một phần của tài liệu Manning JUnit recipes practical methods for program (Trang 505 - 508)

Problem

You want to verify that your JSP tag library has been deployed correctly.

Background

It is possible to write all the right Object Tests for your web components but have the system fail an End-to-End Test. It is easy to forget to write the deployment descriptors for your web components when you do the majority of your web com- ponent testing outside of a web container. The approach you take to deal with this problem depends in part on how often you make the mistake and how much it hurts you when you do. (Do not feel bad: it happens to the best of us.)

Recipe

Let us first recommend you use your End-to-End Tests to detect this kind of prob- lem. If you are executing End-to-End Tests (“Customer Tests” in the Extreme Pro- gramming vernacular) as you complete features or as you execute your Object Tests, then you can easily let your End-to-End Tests detect any defect arising from deploying your web components incorrectly. For example, if you deploy a JSP tag library incorrectly, your Object Tests will pass but your End-to-End Tests will fail.

As a result, when this failure happens, the first question to ask yourself is, “Did I deploy this stuff correctly?”

475 Test your JSP tag library deployment

We understand that this solution might not satisfy you. It might seem unneces- sarily informal or haphazard. If you feel that way or decide for any other reason that you need some more focused tests in place, then we recommend verifying the deployment descriptors themselves, either using the Gold Master technique (which we discussed in chapter 10, “Testing and JDBC”) or by parsing the XML documents and making assertions about them (see chapter 9, “Testing and XML”). We recom- mend the latter approach over the former. You might even wish to compare the Gold Master file against the current deployment descriptor using XMLUnit rather than performing a straight text-content comparison. If you use XMLUnit you will not be bothered by false failures resulting from purely innocuous differences in formatting: tabs or spaces, different white space, different line breaks.

The files you need to verify are the tag library descriptors (*.tld) and the web deployment descriptor itself (web.xml). Check the former to ensure that you have specified the tag name, attributes, requirements, and tag handler class name cor- rectly. Check the latter to ensure that your application has access to all the tag libraries it needs.

The following is an example of using XMLUnit to verify that you have specified a custom tag correctly in your tag library descriptor. The entire test revolves around checking the existence of a number of increasingly specific XPath expressions.

Listing 12.10 is an example.

package junit.cookbook.coffee.deployment.test;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import junit.cookbook.coffee.display.ShopcartItemBean;

import org.apache.crimson.jaxp.DocumentBuilderFactoryImpl;

import org.custommonkey.xmlunit.XMLTestCase;

import org.w3c.dom.Document;

public class CoffeeTagLibraryDeploymentTest extends XMLTestCase {

private Document tagLibraryDescriptorDocument;

protected void setUp() throws Exception { DocumentBuilderFactory factory = new DocumentBuilderFactoryImpl();

factory.setNamespaceAware(true);

Listing 12.10 CoffeeTagLibraryDeploymentTest

476 CHAPTER 12

Testing web components

factory.setValidating(false);

DocumentBuilder documentBuilder = factory.newDocumentBuilder();

documentBuilder.setEntityResolver(

new StringEntityResolver(""));

File file = new File(

"../CoffeeShopWeb/Web Content", "WEB-INF/coffeeShop.tld");

tagLibraryDescriptorDocument = documentBuilder.parse(file);

}

public void testShopcartTagDeployedCorrectly() throws Exception {

String[] expectedRelativeXpaths = new String[] {

"",

"/attribute[name='shopcartBean']", "/attribute[name='each']",

"/attribute[name='each' and required='true']", "/variable[name-from-attribute='each']", "/variable[name-from-attribute='each' and "

+ "variable-class='"

+ ShopcartItemBean.class.getName() + "']" };

for (int i = 0;

i < expectedRelativeXpaths.length;

i++) {

assertXpathExists(

"/taglib/tag[name='eachShopcartItem']"

+ expectedRelativeXpaths[i], tagLibraryDescriptorDocument);

} } }

You now have the skeleton from which you can make two main enhancements. First, add more XPath-based assertions to cover the other parts of the tag library descriptor that you expect to find. Next, build a customized Document Object Model for this document, similar to the work that Mike Bowler has done in creating a domain- oriented Document Object Model for HTML in HtmlUnit. If you get that far, con- sider sharing it with the rest of the world through the medium of open source!

Avoid loading the DTD

Verify each expected XPath expression

477 Test servlet initialization

Discussion

In our example we avoided loading the DTD for our tag library descriptor10 because that was not part of the problem we were trying to solve. XMLUnit pro- vides support for validating documents against a DTD, and you should use them in testing wherever you can. Validating XML documents—either against a DTD or a schema—catches perhaps 90% of the silly mistakes we make when writing XML by hand or when generating it. See chapter 9, “Testing and XML” for more informa- tion on testing with XMLUnit.

We recommend building a Deployment Test Suite that you execute when you deploy the application. This suite would include all manner of tests that verify deployment descriptors, including checking for tag libraries. Not only will the programmers find this useful during development, but the deployers will find it useful to avoid silly problems during deployment. Everyone wins.

If you are test-driving your JSP tag library, then you will typically use XMLUnit to build the content of your tag library descriptor and web deployment descriptor as you write the tag library. In that case, you are more likely to use XMLUnit directly on the deployment descriptor and place the expected contents in your test method than you would be to use the Gold Master technique. If you are adding deployment tests to an existing application, you will probably find the Gold Mas- ter technique sufficient until you notice a pattern in the kinds of deployment defects your organization tends to make. If that happens, we recommend adding the more direct-style tests to cover those “blind spots.”

Related

■ Chapter 9—Testing and XML

■ Chapter 10—Testing and JDBC

Một phần của tài liệu Manning JUnit recipes practical methods for program (Trang 505 - 508)

Tải bản đầy đủ (PDF)

(753 trang)