FileDocCategorySizeDatePackage
SampleUnitTest.javaAPI DocExample5310Sun Sep 02 14:59:00 BST 2001chap9

SampleUnitTest

public class SampleUnitTest extends TestCase
An example JUnit test. This class performs an XSLT transformation and validates the result.

Fields Summary
private String
workingDir
private File
aidanXMLFile
private File
johnXMLFile
private File
condenseXSLTFile
private File
aidanCondensedXMLFile
private File
johnCondensedXMLFile
private TransformerFactory
transFact
Constructors Summary
public SampleUnitTest(String name)
All JUnit tests have a constructor that takes the test name.

        super(name);
    
Methods Summary
public static voidmain(java.lang.String[] args)
Allow the unit tests to be invoked from the command line in text-only mode.

        TestRunner.run(suite());
    
public voidsetUp()
Initialization before each test[...] method is called.

        // locate a file named test.properties in the chap9 package
        ResourceBundle rb = ResourceBundle.getBundle("chap9.test");
        this.workingDir = rb.getString("chap9.workingDir");

        assertNotNull(workingDir);
        assertTrue("Unable to locate " + this.workingDir,
                new File(this.workingDir).exists());

        this.aidanXMLFile = new File(workingDir + File.separator
                + "aidan.xml");
        this.johnXMLFile = new File(workingDir + File.separator
                + "john.xml");
        this.condenseXSLTFile = new File(workingDir + File.separator
                + "condensePerson.xslt");

        this.aidanCondensedXMLFile = new File(this.workingDir + File.separator
                + "aidanCondensed.xml");
        this.johnCondensedXMLFile = new File(this.workingDir + File.separator
                + "johnCondensed.xml");

        this.transFact = TransformerFactory.newInstance();
    
public static junit.framework.Testsuite()

return
a TestSuite, which is a composite of Test objects.

        // uses reflection to locate each method named test[...]
        return new TestSuite(SampleUnitTest.class);
    
public voidtearDown()
Clean up after each test[...] method

        // the transformation results could be deleted here, but the
        // cleanup code is intentionally commented out so the
        // developer can see the generated files:

        // this.aidanCondensedXMLFile.delete();
        // this.johnCondensedXMLFile.delete();
    
public voidtestTransformWithTemplates()
An individual unit test.

        Templates templates = this.transFact.newTemplates(
                new StreamSource(this.condenseXSLTFile));

        Transformer trans = templates.newTransformer();

        // do two transformations using the same Transformer
        trans.transform(new StreamSource(this.aidanXMLFile),
                new StreamResult(this.aidanCondensedXMLFile));
        trans.transform(new StreamSource(this.johnXMLFile),
                new StreamResult(this.johnCondensedXMLFile));

        // validate both files
        validateCondensedFile(this.aidanCondensedXMLFile,
                "Aidan Garrett Burke", "6/25/1999");
        validateCondensedFile(this.johnCondensedXMLFile,
                "John Fitzgerald Kennedy", "5/29/1917");
    
public voidtestTransformer()
Another unit test.

        Transformer trans = this.transFact.newTransformer(
                new StreamSource(this.condenseXSLTFile));

        trans.transform(new StreamSource(this.aidanXMLFile),
                new StreamResult(this.aidanCondensedXMLFile));

        validateCondensedFile(this.aidanCondensedXMLFile,
                "Aidan Garrett Burke", "6/25/1999");
    
private voidvalidateCondensedFile(java.io.File file, java.lang.String expectedName, java.lang.String expectedBirthDate)

        try {
            // first do a simple validation against the DTD
            SAXBuilder builder = new SAXBuilder(true); // validate
            Document doc = builder.build(file);

            // now perform some additional checks
            Element nameElem = doc.getRootElement().getChild("name");
            assertEquals("Name was not correct",
                    expectedName, nameElem.getText());

            Element birthDateElem = doc.getRootElement().getChild("birthDate");
            assertEquals("Birth date was not correct",
                    expectedBirthDate, birthDateElem.getText());

        } catch (JDOMException jde) {
            fail("XML was not valid: " + jde.getMessage());
        }