in java/src/org/apache/qetest/trax/sax/TemplatesHandlerAPITest.java [203:357]
public boolean testCase2()
{
reporter.testCaseInit("Basic functionality of TemplatesHandler");
// Provide local copies of URLized filenames, so that we can
// later run tests with either Strings or URLs
String xslURI = QetestUtils.filenameToURL(testFileInfo.inputName);
String xmlURI = QetestUtils.filenameToURL(testFileInfo.xmlName);
String xslImpInclURI = QetestUtils.filenameToURL(impInclFileInfo.inputName);
String xmlImpInclURI = QetestUtils.filenameToURL(impInclFileInfo.xmlName);
TransformerFactory factory = null;
SAXTransformerFactory saxFactory = null;
TemplatesHandler templatesHandler = null;
Templates templates = null;
Transformer transformer = null;
try
{
factory = TransformerFactory.newInstance();
factory.setErrorListener(new DefaultErrorHandler());
saxFactory = (SAXTransformerFactory)factory;
// Validate a templatesHandler can create a valid stylesheet
templatesHandler = saxFactory.newTemplatesHandler();
XMLReader reader = getJAXPXMLReader();
reader.setContentHandler(templatesHandler);
// Parse the stylesheet, which means we should be able to getTemplates()
reader.parse(xslURI);
//Get the Templates object from the ContentHandler
templates = templatesHandler.getTemplates();
reporter.check((templates != null), true, "getTemplates() returns non-null with valid stylesheet");
Properties xslOutProps = templates.getOutputProperties();
reporter.check((xslOutProps != null), true, "getTemplates().getOutputProperties() returns non-null with valid stylesheet");
//@todo validate a specific output property
transformer = templates.newTransformer();
transformer.setErrorListener(new DefaultErrorHandler());
reporter.check((transformer != null), true, "getTemplates().newTransformer() returns non-null with valid stylesheet");
// Validate that this transformer actually works
FileOutputStream fos = new FileOutputStream(outNames.nextName());
Result result = new StreamResult(fos);
Source xmlSource = new StreamSource(xmlURI);
transformer.transform(xmlSource, result);
fos.close(); // must close ostreams we own
if (Logger.PASS_RESULT
!= fileChecker.check(reporter,
new File(outNames.currentName()),
new File(testFileInfo.goldName),
"SAX-built simple transform into: " + outNames.currentName())
)
reporter.logInfoMsg("SAX-built simple transform failure reason:" + fileChecker.getExtendedInfo());
}
catch (Throwable t)
{
reporter.checkFail("Problem TemplatesHandler(1)");
reporter.logThrowable(reporter.ERRORMSG, t,"Problem TemplatesHandler(1)");
}
try
{
factory = TransformerFactory.newInstance();
factory.setErrorListener(new DefaultErrorHandler());
saxFactory = (SAXTransformerFactory)factory;
// Validate a templatesHandler can create a stylesheet
// with imports/includes, with the default systemId
templatesHandler = saxFactory.newTemplatesHandler();
XMLReader reader = getJAXPXMLReader();
reader.setContentHandler(templatesHandler);
// Parse the stylesheet, which means we should be able to getTemplates()
reader.parse(xslImpInclURI);
//Get the Templates object from the ContentHandler
templates = templatesHandler.getTemplates();
reporter.check((templates != null), true, "getTemplates() returns non-null with impincl stylesheet");
Properties xslOutProps = templates.getOutputProperties();
reporter.check((xslOutProps != null), true, "getTemplates().getOutputProperties() returns non-null with impincl stylesheet");
//@todo validate a specific output property
transformer = templates.newTransformer();
transformer.setErrorListener(new DefaultErrorHandler());
reporter.check((transformer != null), true, "getTemplates().newTransformer() returns non-null with impincl stylesheet");
// Validate that this transformer actually works
FileOutputStream fos = new FileOutputStream(outNames.nextName());
Result result = new StreamResult(fos);
Source xmlSource = new StreamSource(xmlImpInclURI);
transformer.transform(xmlSource, result);
fos.close(); // must close ostreams we own
if (Logger.PASS_RESULT
!= fileChecker.check(reporter,
new File(outNames.currentName()),
new File(impInclFileInfo.goldName),
"SAX-built impincl transform into: " + outNames.currentName())
)
reporter.logInfoMsg("SAX-built impincl transform failure reason:" + fileChecker.getExtendedInfo());
}
catch (Throwable t)
{
reporter.checkFail("Problem TemplatesHandler(2)");
reporter.logThrowable(reporter.ERRORMSG, t,"Problem TemplatesHandler(2)");
}
try
{
factory = TransformerFactory.newInstance();
factory.setErrorListener(new DefaultErrorHandler());
saxFactory = (SAXTransformerFactory)factory;
// Validate a templatesHandler with an incorrect
// systemId reports an error nicely
templatesHandler = saxFactory.newTemplatesHandler();
// Set the base systemId for the handler to a bogus one
templatesHandler.setSystemId(NONSENSE_SYSTEMID);
XMLReader reader = getJAXPXMLReader();
reader.setContentHandler(templatesHandler);
// Parse the stylesheet, which should throw some
// exception, since the imports/includes won't be
// found at the bogus systemId
reader.parse(xslImpInclURI);
// This line will only get run if above didn't throw an exception
reporter.checkFail("No exception when expected: parsing stylesheet with bad systemId");
}
catch (Throwable t)
{
String msg = t.toString();
if (msg != null)
{
// Note: 'impincl/SimpleImport.xsl' comes from the stylesheet itself,
// so to reduce dependencies, only validate that portion of the msg
reporter.check((msg.indexOf("impincl/SimpleImport.xsl") > 0), true,
"Expected Exception has proper message for bad systemId");
}
else
{
reporter.checkFail("Expected Exception has proper message for bad systemId");
}
reporter.logThrowable(reporter.STATUSMSG, t,"(potentially) Expected Exception");
}
finally
{
// Validate the templatesHandler still has systemId set
reporter.check(templatesHandler.getSystemId(), NONSENSE_SYSTEMID,
"templatesHandler still has systemId set");
}
reporter.testCaseClose();
return true;
}