Methods Summary |
---|
public static void | main(java.lang.String[] args)Allow the unit tests to be invoked from the command line
in text-only mode.
TestRunner.run(suite());
|
public void | setUp()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.Test | suite()
// uses reflection to locate each method named test[...]
return new TestSuite(SampleUnitTest.class);
|
public void | tearDown()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 void | testTransformWithTemplates()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 void | testTransformer()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 void | validateCondensedFile(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());
}
|