in java/src/org/apache/qetest/trax/stream/StreamResultAPITest.java [197:345]
public boolean testCase2()
{
reporter.testCaseInit("Basic functionality of StreamResults");
TransformerFactory factory = null;
Source xslSource = null;
Source xmlSource = null;
Templates templates = null;
try
{
factory = TransformerFactory.newInstance();
factory.setErrorListener(new DefaultErrorHandler());
// Create re-useable sources
xslSource = new StreamSource(new FileInputStream(outputFileInfo.inputName));
reporter.logTraceMsg("Create stream sources, templates");
templates = factory.newTemplates(xslSource);
}
catch (Throwable t)
{
reporter.checkFail("Problem creating factory; can't continue testcase");
reporter.logThrowable(reporter.ERRORMSG, t,
"Problem creating factory; can't continue testcase");
return true;
}
try
{
// Test some OutputStreams
// Simple FileOutputStream is tested in numerous other tests
Transformer transformer = templates.newTransformer();
transformer.setErrorListener(new DefaultErrorHandler());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Result result1 = new StreamResult(baos);
reporter.logTraceMsg("About to Transform into ByteArrayOutputStream");
// Note: must get a new xmlSource for each transform
// Should this really be necessary? I suppose
// FileInputStreams don't just get 'reset' for you,
// but it would be nice to reuse the StreamSources
xmlSource = new StreamSource(new FileInputStream(outputFileInfo.xmlName));
transformer.transform(xmlSource, result1);
reporter.logTraceMsg("baos.size() is: " + baos.size());
ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos2);
Result result2 = new StreamResult(ps);
reporter.logTraceMsg("About to Transform into PrintStream");
xmlSource = new StreamSource(new FileInputStream(outputFileInfo.xmlName));
transformer.transform(xmlSource, result2);
reporter.logTraceMsg("ps(baos2).size() is: " + baos2.size());
if (!reporter.checkString(baos.toString(), baos2.toString(), "BAOS and PS output comparison"))
{
reporter.logArbitrary(reporter.TRACEMSG, "baos was: " + baos.toString());
reporter.logArbitrary(reporter.TRACEMSG, "ps(baos2) was: " + baos2.toString());
}
writeFileAndValidate(baos.toString("UTF-8"), outputFileInfo.goldName);
}
catch (Throwable t)
{
reporter.checkFail("Problem with transform-streams(1)");
reporter.logThrowable(reporter.ERRORMSG, t, "Problem with transform-streams(1)");
}
try
{
// Test some Writers
Transformer transformer = templates.newTransformer();
transformer.setErrorListener(new DefaultErrorHandler());
StringWriter sw = new StringWriter();
Result result1 = new StreamResult(sw);
reporter.logTraceMsg("About to Transform into StringWriter");
xmlSource = new StreamSource(new FileInputStream(outputFileInfo.xmlName));
transformer.transform(xmlSource, result1);
CharArrayWriter cw = new CharArrayWriter();
Result result2 = new StreamResult(cw);
reporter.logTraceMsg("About to Transform into CharArrayWriter");
xmlSource = new StreamSource(new FileInputStream(outputFileInfo.xmlName));
transformer.transform(xmlSource, result2);
if (!reporter.checkString(sw.toString(), cw.toString(), "SW and CW output comparison"))
{
reporter.logArbitrary(reporter.TRACEMSG, "sw was: " + sw.toString());
reporter.logArbitrary(reporter.TRACEMSG, "cw was: " + cw.toString());
}
writeFileAndValidate(sw.toString(), outputFileInfo.goldName);
}
catch (Throwable t)
{
reporter.checkFail("Problem with transform-streams(2)");
reporter.logThrowable(reporter.ERRORMSG, t, "Problem with transform-streams(2)");
}
try
{
// Test with systemId set
// Note: may be affected by user.dir property; if we're
// already in the correct place, this won't be different
try
{
reporter.logTraceMsg("System.getProperty(user.dir) = " + System.getProperty("user.dir"));
}
catch (SecurityException e) // in case of Applet context
{
reporter.logTraceMsg("System.getProperty(user.dir) threw SecurityException");
}
Transformer transformer = templates.newTransformer();
transformer.setErrorListener(new DefaultErrorHandler());
StringWriter sw1 = new StringWriter();
Result result1 = new StreamResult(sw1);
reporter.logTraceMsg("About to Transform into StringWriter w/out systemId set");
xmlSource = new StreamSource(new FileInputStream(outputFileInfo.xmlName));
transformer.transform(xmlSource, result1);
StringWriter sw2 = new StringWriter();
Result result2 = new StreamResult(sw2);
result2.setSystemId("random-system-id");
reporter.logTraceMsg("About to Transform into StringWriter w/ systemId set");
xmlSource = new StreamSource(new FileInputStream(outputFileInfo.xmlName));
transformer.transform(xmlSource, result2);
reporter.check(result2.getSystemId(), "random-system-id", "systemId remains set after transform");
if (!reporter.checkString(sw1.toString(), sw2.toString(), "Output comparison, with/without systemId"))
{
reporter.logArbitrary(reporter.TRACEMSG, "sw1 w/out systemId was: " + sw1.toString());
reporter.logArbitrary(reporter.TRACEMSG, "sw2 w/ systemId was: " + sw2.toString());
}
writeFileAndValidate(sw1.toString(), outputFileInfo.goldName);
reporter.logInfoMsg("@todo we should update XHTComparator for bogus systemId's like we have in this test");
// @todo we should update XHTComparator for bogus systemId's like we have in this test
// Note that using XHTFileCheckService, it always compares our
// outputs using [text] since the XML parser usually throws:
// warning;org.xml.sax.SAXParseException: File "file:/E:/builds/xml-xalan/test/tests/api-gold/trax/stream/this-is-doctype-system" not found.
if (reporter.getLoggingLevel() >= Reporter.TRACEMSG)
{
reporter.logArbitrary(reporter.TRACEMSG, fileChecker.getExtendedInfo());
}
}
catch (Throwable t)
{
reporter.checkFail("Problem with transform-streams(3)");
reporter.logThrowable(reporter.ERRORMSG, t, "Problem with transform-streams(3)");
}
reporter.testCaseClose();
return true;
}