in java/src/org/apache/qetest/trax/TransformerAPITest.java [410:710]
public boolean testCase2()
{
//@todo I can't decide how to split tests up between
// testCase2/testCase3 - they really should be reorganized
reporter.testCaseInit("API coverage test of Transformer.set/getOutputProperty()");
TransformerFactory factory = null;
Templates outputTemplates = null;
Transformer outputTransformer = null;
Templates htmlTemplates = null;
Transformer htmlTransformer = null;
Templates identityTemplates = null;
Transformer identityTransformer = null; // an .xsl file defining an identity transform
Transformer defaultTransformer = null; // the default 'identity' transform
try
{
factory = TransformerFactory.newInstance();
factory.setErrorListener(new DefaultErrorHandler());
outputTemplates = factory.newTemplates(new StreamSource(outputFormatTest.inputName));
outputTransformer = outputTemplates.newTransformer();
outputTransformer.setErrorListener(new DefaultErrorHandler());
htmlTemplates = factory.newTemplates(new StreamSource(htmlFormatTest.inputName));
htmlTransformer = htmlTemplates.newTransformer();
htmlTransformer.setErrorListener(new DefaultErrorHandler());
identityTemplates = factory.newTemplates(new StreamSource(simpleTest.inputName));
identityTransformer = identityTemplates.newTransformer();
identityTransformer.setErrorListener(new DefaultErrorHandler());
defaultTransformer = factory.newTransformer();
defaultTransformer.setErrorListener(new DefaultErrorHandler());
}
catch (Throwable t)
{
reporter.checkFail("Problem creating Templates; cannot continue");
reporter.logThrowable(reporter.ERRORMSG, t,
"Problem creating Templates; cannot continue");
return true;
}
try
{
// See what the default 'identity' transform has by default
Properties defaultProps = defaultTransformer.getOutputProperties(); // SPR SCUU4RXQYH throws npe
reporter.logHashtable(reporter.STATUSMSG, defaultProps,
"default defaultTransformer.getOutputProperties()");
// Check that the local stylesheet.getProperty has default set, cf. getOutputProperties javadoc
reporter.check(("xml".equals(defaultProps.getProperty(OutputKeys.METHOD))), true, "defaultTransformer.op.getProperty("
+ OutputKeys.METHOD + ") is default value, act: " + defaultProps.getProperty(OutputKeys.METHOD));
// Check that the local stylesheet.get has nothing set, cf. getOutputProperties javadoc
reporter.check((null == defaultProps.get(OutputKeys.METHOD)), true, "defaultTransformer.op.get("
+ OutputKeys.METHOD + ") is null value, act: " + defaultProps.get(OutputKeys.METHOD));
// Can you set properties on this transformer?
defaultTransformer.setOutputProperty(OutputKeys.METHOD, "text");
reporter.logTraceMsg("Just defaultTransformer setOutputProperty(method,text)");
String tmp = defaultTransformer.getOutputProperty(OutputKeys.METHOD); // SPR SCUU4R3JPH - throws npe
reporter.check(tmp, "text", "defaultTransformer set/getOutputProperty, is ?" + tmp + "?");
}
catch (Exception e)
{
reporter.logThrowable(reporter.ERRORMSG, e, "Problem(1) with default output property");
reporter.checkFail("Problem(1) with default output property", "SCUU4RXQYH");
}
try
{
// See what the our .xsl file 'identity' transform has
Properties identityProps = identityTransformer.getOutputProperties();
reporter.logHashtable(reporter.STATUSMSG, identityProps,
"default identityTransformer.getOutputProperties()");
// Check that the local stylesheet.getProperty has default set, cf. getOutputProperties javadoc
reporter.check(("xml".equals(identityProps.getProperty(OutputKeys.METHOD))), true, "identityTransformer.op.getProperty("
+ OutputKeys.METHOD + ") is default value, act: " + identityProps.getProperty(OutputKeys.METHOD));
// Check that the local stylesheet.get has nothing set, cf. getOutputProperties javadoc
reporter.check((null == identityProps.get(OutputKeys.METHOD)), true, "identityTransformer.op.get("
+ OutputKeys.METHOD + ") is null value, act: " + identityProps.get(OutputKeys.METHOD));
// Check that the local stylesheet.getProperty has default set, cf. getOutputProperties javadoc
reporter.check(("no".equals(identityProps.getProperty(OutputKeys.INDENT))), true, "identityTransformer.op.getProperty("
+ OutputKeys.INDENT + ") is default value, act: " + identityProps.getProperty(OutputKeys.INDENT));
// Check that the local stylesheet.get has nothing set, cf. getOutputProperties javadoc
reporter.check((null == (identityProps.get(OutputKeys.INDENT))), true, "identityTransformer.op.get("
+ OutputKeys.INDENT + ") is default value, act: " + identityProps.get(OutputKeys.INDENT));
// Can you set properties on this transformer?
defaultTransformer.setOutputProperty(OutputKeys.METHOD, "text");
reporter.logTraceMsg("Just identityTransformer setOutputProperty(method,text)");
String tmp = defaultTransformer.getOutputProperty(OutputKeys.METHOD); // SPR SCUU4R3JPH - throws npe
reporter.check(tmp, "text", "identityTransformer set/getOutputProperty, is ?" + tmp + "?");
}
catch (Exception e)
{
reporter.logThrowable(reporter.ERRORMSG, e, "Problem(2) with identity output property");
reporter.checkFail("Problem(2) with identity output property");
}
try
{
// See what the our html-format output has
Properties htmlProps = htmlTransformer.getOutputProperties();
reporter.logHashtable(reporter.STATUSMSG, htmlProps,
"default htmlTransformer.getOutputProperties()");
// Check that the local stylesheet.getProperty has stylesheet val set, cf. getOutputProperties javadoc
reporter.check(("html".equals(htmlProps.getProperty(OutputKeys.METHOD))), true, "htmlTransformer.op.getProperty("
+ OutputKeys.METHOD + ") is stylesheet value, act: " + htmlProps.getProperty(OutputKeys.METHOD));
// Check that the local stylesheet.get has stylesheet val set, cf. getOutputProperties javadoc
reporter.check(("html".equals(htmlProps.get(OutputKeys.METHOD))), true, "htmlTransformer.op.get("
+ OutputKeys.METHOD + ") is stylesheet value, act: " + htmlProps.get(OutputKeys.METHOD));
// Check that the local stylesheet.getProperty has default set, cf. getOutputProperties javadoc
reporter.check(("yes".equals(htmlProps.getProperty(OutputKeys.INDENT))), true, "htmlTransformer.op.getProperty("
+ OutputKeys.INDENT + ") is default value, act: " + htmlProps.getProperty(OutputKeys.INDENT));
// Check that the local stylesheet.get has nothing set, cf. getOutputProperties javadoc
reporter.check((null == (htmlProps.get(OutputKeys.INDENT))), true, "htmlTransformer.op.get("
+ OutputKeys.INDENT + ") is default value, act: " + htmlProps.get(OutputKeys.INDENT));
// Can you set properties on this transformer?
defaultTransformer.setOutputProperty(OutputKeys.METHOD, "text");
reporter.logTraceMsg("Just htmlTransformer setOutputProperty(method,text)");
String tmp = defaultTransformer.getOutputProperty(OutputKeys.METHOD); // SPR SCUU4R3JPH - throws npe
reporter.check(tmp, "text", "htmlTransformer set/getOutputProperty, is ?" + tmp + "?");
}
catch (Exception e)
{
reporter.logThrowable(reporter.ERRORMSG, e, "Problem(3) with html output property");
reporter.checkFail("Problem(3) with html output property");
}
try
{
// See what our outputTemplates parent has
Properties tmpltProps = outputTemplates.getOutputProperties();
reporter.logHashtable(reporter.STATUSMSG, tmpltProps,
"default outputTemplates.getOutputProperties()");
// See what we have by default, from our testfile
outputTransformer = outputTemplates.newTransformer();
outputTransformer.setErrorListener(new DefaultErrorHandler());
try
{
// Inner try-catch
Properties outProps = outputTransformer.getOutputProperties(); // SPR SCUU4RXQYH throws npe
reporter.logHashtable(reporter.STATUSMSG, outProps,
"default outputTransformer.getOutputProperties()");
// Validate the two have the same properties (which they
// should, since we just got the templates now)
for (Enumeration names = tmpltProps.propertyNames();
names.hasMoreElements(); /* no increment portion */ )
{
String key = (String)names.nextElement();
String value = tmpltProps.getProperty(key);
reporter.check(value, outProps.getProperty(key),
"Template, transformer identical outProp: " + key);
}
// Validate known output properties from our testfile
String knownOutputProps[][] =
{
{ OutputKeys.METHOD, METHOD_VALUE },
{ OutputKeys.VERSION, VERSION_VALUE },
{ OutputKeys.ENCODING, ENCODING_VALUE },
{ OutputKeys.STANDALONE, STANDALONE_VALUE },
{ OutputKeys.DOCTYPE_PUBLIC, DOCTYPE_PUBLIC_VALUE }, // SPR SCUU4R3JRR - not returned
{ OutputKeys.DOCTYPE_SYSTEM, DOCTYPE_SYSTEM_VALUE }, // SPR SCUU4R3JRR - not returned
{ OutputKeys.CDATA_SECTION_ELEMENTS, CDATA_SECTION_ELEMENTS_VALUE }, // SPR SCUU4R3JRR - not returned
{ OutputKeys.INDENT, INDENT_VALUE },
{ OutputKeys.MEDIA_TYPE, MEDIA_TYPE_VALUE },
{ OutputKeys.OMIT_XML_DECLARATION, OMIT_XML_DECLARATION_VALUE }
};
for (int i = 0; i < knownOutputProps.length; i++)
{
String item = outProps.getProperty(knownOutputProps[i][0]);
reporter.check(item, knownOutputProps[i][1],
"Known prop(1) " + knownOutputProps[i][0]
+ " is: ?" + item + "?");
}
reporter.logStatusMsg("@todo validate getting individual properties");
}
catch (Exception e)
{
reporter.logThrowable(reporter.ERRORMSG, e, "Problem(a1) with set/get output properties");
reporter.checkFail("Problem(a1) with set/get output properties", "SCUU4RXQYH");
}
/*
NOTE (SB):
Shane omits the xml-decl in the stylesheet, which I don't think
will create a valid XML with UTF-16 encoding (I could be wrong).
Also, Xerces 1.2.3 is pretty broken for UTF-16 right now.
So just comment this out for the moment.
// Try doing a transform (will be UTF-16), to get some output
if (doTransform(outputTransformer,
new StreamSource(outputFormatTest.xmlName),
new StreamResult(new FileOutputStream(outNames.nextName()))))
{
// @todo should update goldFile!
fileChecker.check(reporter,
new File(outNames.currentName()),
new File(outputFormatTest.goldName),
"transform(UTF-16,1) outputParams into: " + outNames.currentName());
}
*/
// Change a single property (makes for simpler encoding output!)
outputTransformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
String encoding = outputTransformer.getOutputProperty(OutputKeys.ENCODING);
reporter.check(encoding, "UTF-8", "outputTransformer set/getOutputProperty value to ?" + encoding + "?");
// Try doing another transform (will be UTF-8), to get some output
// Verify that other output properties stay the same
FileOutputStream fos = new FileOutputStream(outNames.nextName());
if (doTransform(outputTransformer,
new StreamSource(outputFormatTest.xmlName),
new StreamResult(fos)))
{
fos.close(); // must close ostreams we own
// @todo should update goldFile!
if (Logger.PASS_RESULT
!= fileChecker.check(reporter,
new File(outNames.currentName()),
new File(outputFormatTestUTF8),
"transform(UTF-8) outputParams into: " + outNames.currentName())
)
reporter.logInfoMsg("transform(UTF-8) outputParams failure reason:" + fileChecker.getExtendedInfo());
}
// Try getting the whole block and logging it out, just to see what's there
Properties moreOutProps = outputTransformer.getOutputProperties();
reporter.logHashtable(reporter.STATUSMSG, moreOutProps,
"After several transforms getOutputProperties()");
try
{ // Inner try-catch
// Simple set/getOutputProperty
outputTransformer = outputTemplates.newTransformer();
outputTransformer.setErrorListener(new DefaultErrorHandler());
String tmp = outputTransformer.getOutputProperty(OutputKeys.OMIT_XML_DECLARATION); // SPR SCUU4RXR6E
// SPR SCUU4R3JZ7 - throws npe
reporter.logTraceMsg(OutputKeys.OMIT_XML_DECLARATION + " is currently: " + tmp);
outputTransformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
tmp = outputTransformer.getOutputProperty(OutputKeys.OMIT_XML_DECLARATION);
reporter.check(tmp, "no", "outputTransformer set/getOutputProperty value to ?" + tmp + "?");
}
catch (Exception e)
{
reporter.logThrowable(reporter.ERRORMSG, e, "Problem(a2) with set/get output properties");
reporter.checkFail("Problem(a2) with set/get output properties", "SCUU4RXR6E");
}
try
{ // Inner try-catch
// Try getting the whole properties block, so we can see what it thinks it has
outputTransformer = outputTemplates.newTransformer();
outputTransformer.setErrorListener(new DefaultErrorHandler());
Properties newOutProps = outputTransformer.getOutputProperties();
reporter.logHashtable(reporter.STATUSMSG, newOutProps,
"Another getOutputProperties()");
// Simple set/getOutputProperty
String tmp = outputTransformer.getOutputProperty(OutputKeys.ENCODING);
reporter.logTraceMsg(OutputKeys.ENCODING + " is currently: " + tmp);
outputTransformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tmp = outputTransformer.getOutputProperty(OutputKeys.ENCODING);
reporter.check(tmp, "UTF-8", "outputTransformer set/getOutputProperty value to ?" + tmp + "?");
}
catch (Exception e)
{
reporter.logThrowable(reporter.ERRORMSG, e,
"Problem(a3) with set/get output property");
}
// OutputKeys.METHOD = xml|html|text|qname-but-not-ncname
// OutputKeys.VERSION = number
// OutputKeys.ENCODING = string
// OutputKeys.OMIT_XML_DECLARATION = yes|no
// OutputKeys.STANDALONE = yes|no
// OutputKeys.DOCTYPE_PUBLIC = string
// OutputKeys.DOCTYPE_SYSTEM = string
// OutputKeys.CDATA_SECTION_ELEMENTS = qnames
// OutputKeys.INDENT = qnames
// OutputKeys.MEDIA_TYPE = qnames
// OutputKeys.CDATA_SECTION_ELEMENTS = qnames
reporter.logTraceMsg("//@todo Cover setOutputProperties(Properties oformat)");
}
catch (Exception e)
{
reporter.logThrowable(reporter.ERRORMSG, e,
"Problem(4) with set/get output properties");
reporter.checkFail("Problem(4) with set/get output properties");
}
reporter.logTraceMsg("//@todo: Negative testing: various illegal arguments, etc. - in separate testcase");
reporter.testCaseClose();
return true;
}