• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the  "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 /*
19  * $Id$
20  */
21 
22 package org.apache.qetest.trax;
23 
24 import android.platform.test.annotations.FlakyTest;
25 import java.io.File;
26 import java.io.FileOutputStream;
27 import java.util.Enumeration;
28 import java.util.Hashtable;
29 import java.util.Properties;
30 
31 import javax.xml.transform.ErrorListener;
32 import javax.xml.transform.OutputKeys;
33 import javax.xml.transform.Result;
34 import javax.xml.transform.Source;
35 import javax.xml.transform.Templates;
36 import javax.xml.transform.Transformer;
37 import javax.xml.transform.TransformerException;
38 import javax.xml.transform.TransformerFactory;
39 import javax.xml.transform.stream.StreamResult;
40 import javax.xml.transform.stream.StreamSource;
41 
42 import org.apache.qetest.Datalet;
43 import org.apache.qetest.FileBasedTest;
44 import org.apache.qetest.Logger;
45 import org.apache.qetest.OutputNameManager;
46 import org.apache.qetest.QetestUtils;
47 import org.apache.qetest.TestletImpl;
48 import org.apache.qetest.xsl.XSLTestfileInfo;
49 import org.apache.xml.utils.DefaultErrorHandler;
50 import org.junit.Test;
51 
52 //-------------------------------------------------------------------------
53 
54 /**
55  * Basic API coverage test for the Transformer class of TRAX.
56  * This test focuses on coverage testing for the API's, and
57  * very brief functional testing.  Also see tests in the
58  * trax\sax, trax\dom, and trax\stream directories for specific
59  * coverage of Transformer API's in those usage cases.
60  * @author shane_curcuru@lotus.com
61  */
62 public class TransformerAPITest extends FileBasedTest
63 {
64 
65     /** Cheap-o filename for various output files.  */
66     protected OutputNameManager outNames;
67 
68     /** Cheap-o filename set for general API tests. */
69     protected XSLTestfileInfo simpleTest = new XSLTestfileInfo();
70 
71     /** TransformerAPIParam.xsl used for set/getParameter related tests  */
72     protected XSLTestfileInfo paramTest = new XSLTestfileInfo();
73 
74     /** Parameter names from TransformerAPIParam.xsl  */
75     public static final String PARAM1S = "param1s";
76     public static final String PARAM2S = "param2s";
77     public static final String PARAM3S = "param3s";
78     public static final String PARAM1N = "param1n";
79     public static final String PARAM2N = "param2n";
80     public static final String PARAM3N = "param3n";
81 
82     /** TransformerAPIOutputFormat.xsl used for set/getOutputFormat related tests  */
83     protected XSLTestfileInfo outputFormatTest = new XSLTestfileInfo();
84 
85     /** Just goldName for outputFormatTest with UTF-8 */
86     protected String outputFormatTestUTF8 = null;
87 
88     /** TransformerAPIHTMLFormat.xsl.xsl used for set/getOutputFormat related tests  */
89     protected XSLTestfileInfo htmlFormatTest = new XSLTestfileInfo();
90 
91     /** Known outputFormat values from TransformerAPIOutputFormat.xsl  */
92     public static final String METHOD_VALUE = "xml";
93     public static final String VERSION_VALUE ="123.45";
94     public static final String ENCODING_VALUE ="UTF-16";
95     public static final String STANDALONE_VALUE = "yes";
96     public static final String DOCTYPE_PUBLIC_VALUE = "this-is-doctype-public";
97     public static final String DOCTYPE_SYSTEM_VALUE = "this-is-doctype-system";
98     public static final String CDATA_SECTION_ELEMENTS_VALUE = "cdataHere";
99     public static final String INDENT_VALUE  =  "yes";
100     public static final String MEDIA_TYPE_VALUE = "text/test/xml";
101     public static final String OMIT_XML_DECLARATION_VALUE = "yes";
102 
103     /** Cheap-o filename set(s) for multiple transform tests. */
104     protected XSLTestfileInfo multiTest = new XSLTestfileInfo();
105     protected XSLTestfileInfo multi2Test = new XSLTestfileInfo();
106 
107     /** Subdir name under test\tests\api for files.  */
108     public static final String TRAX_SUBDIR = "trax";
109 
110     /** Default ctor initializes test name, comment, numTestCases. */
TransformerAPITest()111     public TransformerAPITest()
112     {
113 
114         numTestCases = 6;  // REPLACE_num
115         testName = "TransformerAPITest";
116         testComment = "Basic API coverage test for the Transformer class";
117     }
118 
119     /**
120      * Initialize this test - Set names of xml/xsl test files, cache system property.
121      *
122      * @param p Properties to initialize with (may be unused)
123      * @return false if test should be aborted, true otherwise
124      */
doTestFileInit(Properties p)125     public boolean doTestFileInit(Properties p)
126     {
127 
128         // Used for all tests; just dump files in trax subdir
129         File outSubDir = new File(outputDir + File.separator + TRAX_SUBDIR);
130 
131         if (!outSubDir.mkdirs())
132             reporter.logWarningMsg("Could not create output dir: "
133                                    + outSubDir);
134 
135         outNames = new OutputNameManager(outputDir + File.separator + TRAX_SUBDIR
136                                          + File.separator + testName, ".out");
137 
138         // We assume inputDir=...tests\api, and use the trax subdir
139         //  also assume inputDir, etc. exist already
140         String testBasePath = inputDir + File.separator + TRAX_SUBDIR
141                               + File.separator;
142         String goldBasePath = goldDir + File.separator + TRAX_SUBDIR
143                               + File.separator;
144 
145         simpleTest.xmlName = QetestUtils.filenameToURL(testBasePath + "identity.xml");
146         simpleTest.inputName = QetestUtils.filenameToURL(testBasePath + "identity.xsl");
147         simpleTest.goldName = goldBasePath + "identity.out";
148 
149         paramTest.xmlName = QetestUtils.filenameToURL(testBasePath + "TransformerAPIParam.xml");
150         paramTest.inputName = QetestUtils.filenameToURL(testBasePath + "TransformerAPIParam.xsl");
151         paramTest.goldName = goldBasePath + "TransformerAPIParam.out";
152 
153         outputFormatTest.xmlName = QetestUtils.filenameToURL(testBasePath + "TransformerAPIOutputFormat.xml");
154         outputFormatTest.inputName = QetestUtils.filenameToURL(testBasePath + "TransformerAPIOutputFormat.xsl");
155         outputFormatTest.goldName = goldBasePath + "TransformerAPIOutputFormatUTF16.out";
156         outputFormatTestUTF8 = goldBasePath + "TransformerAPIOutputFormatUTF8.out";
157 
158         htmlFormatTest.xmlName = QetestUtils.filenameToURL(testBasePath + "TransformerAPIHTMLFormat.xml");
159         htmlFormatTest.inputName = QetestUtils.filenameToURL(testBasePath + "TransformerAPIHTMLFormat.xsl");
160         htmlFormatTest.goldName = goldBasePath + "TransformerAPIHTMLFormat.out";
161 
162         multiTest.xmlName = QetestUtils.filenameToURL(testBasePath + "TransformerAPIVar.xml");
163         multiTest.inputName = QetestUtils.filenameToURL(testBasePath + "TransformerAPIVar.xsl");
164         multiTest.goldName = goldBasePath + "TransformerAPIVar.out";
165 
166         multi2Test.xmlName = QetestUtils.filenameToURL(testBasePath + "TransformerAPIVar2.xml");
167         multi2Test.goldName = goldBasePath + "TransformerAPIVar2.out";
168         try
169         {
170             TransformerFactory tf = TransformerFactory.newInstance();
171             if (!(tf.getFeature(StreamSource.FEATURE)
172                   && tf.getFeature(StreamResult.FEATURE)))
173             {   // The rest of this test relies on Streams only
174                 reporter.logErrorMsg("Streams not supported! Some tests may be invalid!");
175             }
176         }
177         catch (Exception e)
178         {
179             reporter.checkFail(
180                 "Problem creating factory; Some tests may be invalid!");
181             reporter.logThrowable(reporter.ERRORMSG, e,
182                                   "Problem creating factory; Some tests may be invalid!");
183         }
184 
185         return true;
186     }
187 
188 
189     /**
190      * TRAX Transformer: cover basic get/setParameter(s) APIs.
191      * See {@link ParameterTest ParameterTest} for more
192      * functional test coverage on setting different kinds
193      * and types of parameters, etc.
194      *
195      * @return false if we should abort the test
196      */
testCase1()197     public boolean testCase1()
198     {
199 
200         reporter.testCaseInit(
201             "TRAX Transformer: cover basic get/setParameter(s) APIs");
202 
203         TransformerFactory factory = null;
204         Templates templates = null;
205         Transformer transformer = null;
206         Transformer identityTransformer = null;
207         try
208         {
209             factory = TransformerFactory.newInstance();
210             factory.setErrorListener(new DefaultErrorHandler());
211             identityTransformer = factory.newTransformer();
212             identityTransformer.setErrorListener(new DefaultErrorHandler());
213             templates = factory.newTemplates(new StreamSource(paramTest.inputName));
214         }
215         catch (Exception e)
216         {
217             reporter.checkFail("Problem creating Templates; cannot continue testcase");
218             reporter.logThrowable(reporter.ERRORMSG, e,
219                                   "Problem creating Templates; cannot continue testcase");
220             return true;
221         }
222         // Note: large number of try...catch blocks so that early
223         // exceptions won't blow out the whole testCase
224         try
225         {
226             // See what the default 'identity' transform has by default
227             Object tmp = identityTransformer.getParameter("This-param-does-not-exist");
228             reporter.checkObject(tmp, null, "This-param-does-not-exist is null by default identityTransformer");
229             // Can you set properties on this transformer?
230             identityTransformer.setParameter("foo", "bar");
231             tmp = identityTransformer.getParameter("foo");
232             if (tmp == null)
233             {
234                 reporter.checkFail("identityTransformer set/getParameter is:" + tmp);
235             }
236             else
237             {
238                 reporter.checkString((String)tmp, "bar", "identityTransformer set/getParameter value: " + tmp);
239                 reporter.check((tmp instanceof String), true, "identityTransformer set/getParameter datatype");
240             }
241         }
242         catch (Exception e)
243         {
244             reporter.logThrowable(reporter.ERRORMSG, e, "Problem(1) with identity parameters");
245             reporter.checkFail("Problem(1) with identity parameters");
246         }
247 
248         try
249         {
250             transformer = templates.newTransformer(); // may throw TransformerConfigurationException
251             transformer.setErrorListener(new DefaultErrorHandler());
252             // Default Transformer should not have any parameters..
253             Object tmp = transformer.getParameter("This-param-does-not-exist");
254             reporter.checkObject(tmp, null, "This-param-does-not-exist is null by default");
255             //  .. including params in the stylesheet
256             tmp = transformer.getParameter(PARAM1S);
257             if (tmp == null)
258             {   // @todo should use checkObject instead of this if... construct
259                 reporter.checkPass(PARAM1S + " is null by default");
260             }
261             else
262             {
263                 reporter.checkFail(PARAM1S + " is " + tmp + " by default");
264             }
265 
266             // Verify simple set/get of a single parameter - String
267             transformer.setParameter(PARAM1S, "new value1s");
268             reporter.logTraceMsg("Just reset " + PARAM1S + " to new value1s");
269             tmp = transformer.getParameter(PARAM1S);    // SPR SCUU4QWTVZ - returns an XString - fixed
270             if (tmp == null)
271             {
272                 reporter.checkFail(PARAM1S + " is still set to null!");
273             }
274             else
275             {   // Validate SPR SCUU4QWTVZ - should return the same type you set
276                 if (tmp instanceof String)
277                 {
278                     reporter.checkString((String)tmp, "new value1s", PARAM1S + " is now set to ?" + tmp + "?");
279                 }
280                 else
281                 {
282                     reporter.checkFail(PARAM1S + " is now ?" + tmp + "?, isa " + tmp.getClass().getName());
283                 }
284             }
285         }
286         catch (Exception e)
287         {
288             reporter.logThrowable(reporter.ERRORMSG, e, "Problem(2) set/getParameter testing");
289             reporter.checkFail("Problem(2) set/getParameter testing");
290         }
291 
292         try
293         {
294             transformer = templates.newTransformer();
295             transformer.setErrorListener(new DefaultErrorHandler());
296             // Verify simple set/get of a single parameter - Integer
297             transformer.setParameter(PARAM3S, new Integer(1234));
298             reporter.logTraceMsg("Just set " + PARAM3S + " to Integer(1234)");
299             Object tmp = transformer.getParameter(PARAM3S);    // SPR SCUU4QWTVZ - returns an XObject - fixed
300             if (tmp == null)
301             {
302                 reporter.checkFail(PARAM3S + " is still set to null!");
303             }
304             else
305             {   // Validate SPR SCUU4QWTVZ - should return the same type you set
306                 if (tmp instanceof Integer)
307                 {
308                     reporter.checkObject(tmp, new Integer(1234), PARAM3S + " is now set to ?" + tmp + "?");
309                 }
310                 else
311                 {
312                     reporter.checkFail(PARAM3S + " is now ?" + tmp + "?, isa " + tmp.getClass().getName());
313                 }
314             }
315 
316             // Verify simple re-set/get of a single parameter - new Integer
317             transformer.setParameter(PARAM3S, new Integer(99));   // SPR SCUU4R3JGY - can't re-set
318             reporter.logTraceMsg("Just reset " + PARAM3S + " to new Integer(99)");
319             tmp = null;
320             tmp = transformer.getParameter(PARAM3S);
321             if (tmp == null)
322             {
323                 reporter.checkFail(PARAM3S + " is still set to null!");
324             }
325             else
326             {   // Validate SPR SCUU4QWTVZ - should return the same type you set
327                 if (tmp instanceof Integer)
328                 {
329                     reporter.checkObject(tmp, new Integer(99), PARAM3S + " is now set to ?" + tmp + "?");
330                 }
331                 else
332                 {
333                     reporter.checkFail(PARAM3S + " is now ?" + tmp + "?, isa " + tmp.getClass().getName());
334                 }
335             }
336 
337             // Verify simple re-set/get of a single parameter - now a new String
338             transformer.setParameter(PARAM3S, "new value3s");
339             reporter.logTraceMsg("Just reset " + PARAM3S + " to new value3s");
340             tmp = null;
341             tmp = transformer.getParameter(PARAM3S);
342             if (tmp == null)
343             {
344                 reporter.checkFail(PARAM3S + " is still set to null!");
345             }
346             else
347             {   // Validate SPR SCUU4QWTVZ - should return the same type you set
348                 if (tmp instanceof String)
349                 {
350                     reporter.checkString((String)tmp, "new value3s", PARAM3S + " is now set to ?" + tmp + "?");
351                 }
352                 else
353                 {
354                     reporter.checkFail(PARAM3S + " is now ?" + tmp + "?, isa " + tmp.getClass().getName());
355                 }
356             }
357         }
358         catch (Exception e)
359         {
360             reporter.logThrowable(reporter.ERRORMSG, e, "Problem(3) set/getParameters testing");
361             reporter.checkFail("Problem(3) set/getParameters testing");
362         }
363 
364         try
365         {
366             transformer = templates.newTransformer();
367             transformer.setErrorListener(new DefaultErrorHandler());
368             transformer.setParameter(PARAM1S, "'test-param-1s'"); // note single quotes
369             transformer.setParameter(PARAM1N, new Integer(1234));
370             // Verify basic params actually affect transformation
371             //   Use the transformer we set the params onto above!
372             FileOutputStream fos = new FileOutputStream(outNames.nextName());
373             if (doTransform(transformer,
374                             new StreamSource(paramTest.xmlName),
375                             new StreamResult(fos)))
376             {
377                 fos.close(); // must close ostreams we own
378                 // @todo should update goldFile!
379                 if (Logger.PASS_RESULT
380                     != fileChecker.check(reporter,
381                         new File(outNames.currentName()),
382                         new File(paramTest.goldName),
383                         "transform with param1s,param1n into: " + outNames.currentName())
384                    )
385                     reporter.logInfoMsg("transform with param1s,param1n failure reason:" + fileChecker.getExtendedInfo());
386             }
387             String gotStr = (String)transformer.getParameter(PARAM1S);
388             reporter.check(gotStr, "'test-param-1s'",
389                            PARAM1S + " is still set after transform to ?" + gotStr + "?");
390             Integer gotInt = (Integer)transformer.getParameter(PARAM1N);
391             reporter.checkInt(gotInt.intValue(), 1234,
392                            PARAM1N + " is still set after transform to ?" + gotInt + "?");
393         }
394         catch (Exception e)
395         {
396             reporter.logThrowable(reporter.ERRORMSG, e, "Problem(4) with parameter transform");
397             reporter.checkFail("Problem(4) with parameter transform");
398         }
399 
400         reporter.testCaseClose();
401         return true;
402     }
403 
404 
405     /**
406      * API coverage test of Transformer.set/getOutputProperty()
407      * See {@link OutputPropertiesTest} for more coverage on setting
408      * different kinds of outputs, etc.
409      *
410      * @return false if we should abort the test
411      */
testCase2()412     public boolean testCase2()
413     {
414         //@todo I can't decide how to split tests up between
415         //  testCase2/testCase3 - they really should be reorganized
416         reporter.testCaseInit("API coverage test of Transformer.set/getOutputProperty()");
417         TransformerFactory factory = null;
418         Templates outputTemplates = null;
419         Transformer outputTransformer = null;
420         Templates htmlTemplates = null;
421         Transformer htmlTransformer = null;
422         Templates identityTemplates = null;
423         Transformer identityTransformer = null; // an .xsl file defining an identity transform
424         Transformer defaultTransformer = null; // the default 'identity' transform
425         try
426         {
427             factory = TransformerFactory.newInstance();
428             factory.setErrorListener(new DefaultErrorHandler());
429             outputTemplates = factory.newTemplates(new StreamSource(outputFormatTest.inputName));
430             outputTransformer = outputTemplates.newTransformer();
431             outputTransformer.setErrorListener(new DefaultErrorHandler());
432 
433             htmlTemplates = factory.newTemplates(new StreamSource(htmlFormatTest.inputName));
434             htmlTransformer = htmlTemplates.newTransformer();
435             htmlTransformer.setErrorListener(new DefaultErrorHandler());
436 
437             identityTemplates = factory.newTemplates(new StreamSource(simpleTest.inputName));
438             identityTransformer = identityTemplates.newTransformer();
439             identityTransformer.setErrorListener(new DefaultErrorHandler());
440 
441             defaultTransformer = factory.newTransformer();
442             defaultTransformer.setErrorListener(new DefaultErrorHandler());
443         }
444         catch (Throwable t)
445         {
446             reporter.checkFail("Problem creating Templates; cannot continue");
447             reporter.logThrowable(reporter.ERRORMSG, t,
448                                   "Problem creating Templates; cannot continue");
449             return true;
450         }
451 
452         try
453         {
454             // See what the default 'identity' transform has by default
455             Properties defaultProps = defaultTransformer.getOutputProperties(); // SPR SCUU4RXQYH throws npe
456             reporter.logHashtable(reporter.STATUSMSG, defaultProps,
457                                   "default defaultTransformer.getOutputProperties()");
458 
459             // Check that the local stylesheet.getProperty has default set, cf. getOutputProperties javadoc
460             reporter.check(("xml".equals(defaultProps.getProperty(OutputKeys.METHOD))), true, "defaultTransformer.op.getProperty("
461                            + OutputKeys.METHOD + ") is default value, act: " + defaultProps.getProperty(OutputKeys.METHOD));
462             // Check that the local stylesheet.get has nothing set, cf. getOutputProperties javadoc
463             reporter.check((null == defaultProps.get(OutputKeys.METHOD)), true, "defaultTransformer.op.get("
464                            + OutputKeys.METHOD + ") is null value, act: " + defaultProps.get(OutputKeys.METHOD));
465 
466             // Can you set properties on this transformer?
467             defaultTransformer.setOutputProperty(OutputKeys.METHOD, "text");
468             reporter.logTraceMsg("Just defaultTransformer setOutputProperty(method,text)");
469             String tmp = defaultTransformer.getOutputProperty(OutputKeys.METHOD); // SPR SCUU4R3JPH - throws npe
470             reporter.check(tmp, "text", "defaultTransformer set/getOutputProperty, is ?" + tmp + "?");
471         }
472         catch (Exception e)
473         {
474             reporter.logThrowable(reporter.ERRORMSG, e, "Problem(1) with default output property");
475             reporter.checkFail("Problem(1) with default output property", "SCUU4RXQYH");
476         }
477 
478         try
479         {
480             // See what the our .xsl file 'identity' transform has
481             Properties identityProps = identityTransformer.getOutputProperties();
482             reporter.logHashtable(reporter.STATUSMSG, identityProps,
483                                   "default identityTransformer.getOutputProperties()");
484 
485             // Check that the local stylesheet.getProperty has default set, cf. getOutputProperties javadoc
486             reporter.check(("xml".equals(identityProps.getProperty(OutputKeys.METHOD))), true, "identityTransformer.op.getProperty("
487                            + OutputKeys.METHOD + ") is default value, act: " + identityProps.getProperty(OutputKeys.METHOD));
488             // Check that the local stylesheet.get has nothing set, cf. getOutputProperties javadoc
489             reporter.check((null == identityProps.get(OutputKeys.METHOD)), true, "identityTransformer.op.get("
490                            + OutputKeys.METHOD + ") is null value, act: " + identityProps.get(OutputKeys.METHOD));
491 
492             // Check that the local stylesheet.getProperty has default set, cf. getOutputProperties javadoc
493             reporter.check(("no".equals(identityProps.getProperty(OutputKeys.INDENT))), true, "identityTransformer.op.getProperty("
494                            + OutputKeys.INDENT + ") is default value, act: " + identityProps.getProperty(OutputKeys.INDENT));
495             // Check that the local stylesheet.get has nothing set, cf. getOutputProperties javadoc
496             reporter.check((null == (identityProps.get(OutputKeys.INDENT))), true, "identityTransformer.op.get("
497                            + OutputKeys.INDENT + ") is default value, act: " + identityProps.get(OutputKeys.INDENT));
498 
499             // Can you set properties on this transformer?
500             defaultTransformer.setOutputProperty(OutputKeys.METHOD, "text");
501             reporter.logTraceMsg("Just identityTransformer setOutputProperty(method,text)");
502             String tmp = defaultTransformer.getOutputProperty(OutputKeys.METHOD); // SPR SCUU4R3JPH - throws npe
503             reporter.check(tmp, "text", "identityTransformer set/getOutputProperty, is ?" + tmp + "?");
504         }
505         catch (Exception e)
506         {
507             reporter.logThrowable(reporter.ERRORMSG, e, "Problem(2) with identity output property");
508             reporter.checkFail("Problem(2) with identity output property");
509         }
510 
511         try
512         {
513             // See what the our html-format output has
514             Properties htmlProps = htmlTransformer.getOutputProperties();
515             reporter.logHashtable(reporter.STATUSMSG, htmlProps,
516                                   "default htmlTransformer.getOutputProperties()");
517 
518             // Check that the local stylesheet.getProperty has stylesheet val set, cf. getOutputProperties javadoc
519             reporter.check(("html".equals(htmlProps.getProperty(OutputKeys.METHOD))), true, "htmlTransformer.op.getProperty("
520                            + OutputKeys.METHOD + ") is stylesheet value, act: " + htmlProps.getProperty(OutputKeys.METHOD));
521             // Check that the local stylesheet.get has stylesheet val set, cf. getOutputProperties javadoc
522             reporter.check(("html".equals(htmlProps.get(OutputKeys.METHOD))), true, "htmlTransformer.op.get("
523                            + OutputKeys.METHOD + ") is stylesheet value, act: " + htmlProps.get(OutputKeys.METHOD));
524 
525             // Check that the local stylesheet.getProperty has default set, cf. getOutputProperties javadoc
526             reporter.check(("yes".equals(htmlProps.getProperty(OutputKeys.INDENT))), true, "htmlTransformer.op.getProperty("
527                            + OutputKeys.INDENT + ") is default value, act: " + htmlProps.getProperty(OutputKeys.INDENT));
528             // Check that the local stylesheet.get has nothing set, cf. getOutputProperties javadoc
529             reporter.check((null == (htmlProps.get(OutputKeys.INDENT))), true, "htmlTransformer.op.get("
530                            + OutputKeys.INDENT + ") is default value, act: " + htmlProps.get(OutputKeys.INDENT));
531 
532             // Can you set properties on this transformer?
533             defaultTransformer.setOutputProperty(OutputKeys.METHOD, "text");
534             reporter.logTraceMsg("Just htmlTransformer setOutputProperty(method,text)");
535             String tmp = defaultTransformer.getOutputProperty(OutputKeys.METHOD); // SPR SCUU4R3JPH - throws npe
536             reporter.check(tmp, "text", "htmlTransformer set/getOutputProperty, is ?" + tmp + "?");
537         }
538         catch (Exception e)
539         {
540             reporter.logThrowable(reporter.ERRORMSG, e, "Problem(3) with html output property");
541             reporter.checkFail("Problem(3) with html output property");
542         }
543 
544         try
545         {
546             // See what our outputTemplates parent has
547             Properties tmpltProps = outputTemplates.getOutputProperties();
548             reporter.logHashtable(reporter.STATUSMSG, tmpltProps,
549                                   "default outputTemplates.getOutputProperties()");
550 
551             // See what we have by default, from our testfile
552             outputTransformer = outputTemplates.newTransformer();
553             outputTransformer.setErrorListener(new DefaultErrorHandler());
554             try
555             {
556                 // Inner try-catch
557                 Properties outProps = outputTransformer.getOutputProperties(); // SPR SCUU4RXQYH throws npe
558                 reporter.logHashtable(reporter.STATUSMSG, outProps,
559                                       "default outputTransformer.getOutputProperties()");
560 
561                 // Validate the two have the same properties (which they
562                 //  should, since we just got the templates now)
563                 for (Enumeration names = tmpltProps.propertyNames();
564                         names.hasMoreElements(); /* no increment portion */ )
565                 {
566                     String key = (String)names.nextElement();
567                     String value = tmpltProps.getProperty(key);
568                     reporter.check(value, outProps.getProperty(key),
569                                    "Template, transformer identical outProp: " + key);
570                 }
571 
572                 // Validate known output properties from our testfile
573                 String knownOutputProps[][] =
574                 {
575                     { OutputKeys.METHOD, METHOD_VALUE },
576                     { OutputKeys.VERSION, VERSION_VALUE },
577                     { OutputKeys.ENCODING, ENCODING_VALUE },
578                     { OutputKeys.STANDALONE, STANDALONE_VALUE },
579                     { OutputKeys.DOCTYPE_PUBLIC, DOCTYPE_PUBLIC_VALUE }, // SPR SCUU4R3JRR - not returned
580                     { OutputKeys.DOCTYPE_SYSTEM, DOCTYPE_SYSTEM_VALUE }, // SPR SCUU4R3JRR - not returned
581                     { OutputKeys.CDATA_SECTION_ELEMENTS, CDATA_SECTION_ELEMENTS_VALUE }, // SPR SCUU4R3JRR - not returned
582                     { OutputKeys.INDENT, INDENT_VALUE },
583                     { OutputKeys.MEDIA_TYPE, MEDIA_TYPE_VALUE },
584                     { OutputKeys.OMIT_XML_DECLARATION, OMIT_XML_DECLARATION_VALUE }
585                 };
586 
587                 for (int i = 0; i < knownOutputProps.length; i++)
588                 {
589                     String item = outProps.getProperty(knownOutputProps[i][0]);
590                     reporter.check(item, knownOutputProps[i][1],
591                                    "Known prop(1) " + knownOutputProps[i][0]
592                                    + " is: ?" + item + "?");
593                 }
594                 reporter.logStatusMsg("@todo validate getting individual properties");
595             }
596             catch (Exception e)
597             {
598                 reporter.logThrowable(reporter.ERRORMSG, e, "Problem(a1) with set/get output properties");
599                 reporter.checkFail("Problem(a1) with set/get output properties", "SCUU4RXQYH");
600             }
601 
602             /*
603             NOTE (SB):
604             Shane omits the xml-decl in the stylesheet, which I don't think
605             will create a valid XML with UTF-16 encoding (I could be wrong).
606             Also, Xerces 1.2.3 is pretty broken for UTF-16 right now.
607             So just comment this out for the moment.
608 
609             // Try doing a transform (will be UTF-16), to get some output
610             if (doTransform(outputTransformer,
611                             new StreamSource(outputFormatTest.xmlName),
612                             new StreamResult(new FileOutputStream(outNames.nextName()))))
613             {
614                 // @todo should update goldFile!
615                 fileChecker.check(reporter,
616                                   new File(outNames.currentName()),
617                                   new File(outputFormatTest.goldName),
618                                   "transform(UTF-16,1) outputParams into: " + outNames.currentName());
619             }
620             */
621 
622             // Change a single property (makes for simpler encoding output!)
623             outputTransformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
624             String encoding = outputTransformer.getOutputProperty(OutputKeys.ENCODING);
625             reporter.check(encoding, "UTF-8", "outputTransformer set/getOutputProperty value to ?" + encoding + "?");
626             // Try doing another transform (will be UTF-8), to get some output
627             // Verify that other output properties stay the same
628             FileOutputStream fos = new FileOutputStream(outNames.nextName());
629             if (doTransform(outputTransformer,
630                             new StreamSource(outputFormatTest.xmlName),
631                             new StreamResult(fos)))
632             {
633                 fos.close(); // must close ostreams we own
634                 // @todo should update goldFile!
635                 if (Logger.PASS_RESULT
636                     != fileChecker.check(reporter,
637                         new File(outNames.currentName()),
638                         new File(outputFormatTestUTF8),
639                         "transform(UTF-8) outputParams into: " + outNames.currentName())
640                    )
641                     reporter.logInfoMsg("transform(UTF-8) outputParams failure reason:" + fileChecker.getExtendedInfo());
642             }
643             // Try getting the whole block and logging it out, just to see what's there
644             Properties moreOutProps = outputTransformer.getOutputProperties();
645             reporter.logHashtable(reporter.STATUSMSG, moreOutProps,
646                                   "After several transforms getOutputProperties()");
647 
648             try
649             {   // Inner try-catch
650                 // Simple set/getOutputProperty
651                 outputTransformer = outputTemplates.newTransformer();
652                 outputTransformer.setErrorListener(new DefaultErrorHandler());
653                 String tmp = outputTransformer.getOutputProperty(OutputKeys.OMIT_XML_DECLARATION); // SPR SCUU4RXR6E
654                     // SPR SCUU4R3JZ7 - throws npe
655                 reporter.logTraceMsg(OutputKeys.OMIT_XML_DECLARATION + " is currently: " + tmp);
656                 outputTransformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
657                 tmp = outputTransformer.getOutputProperty(OutputKeys.OMIT_XML_DECLARATION);
658                 reporter.check(tmp, "no", "outputTransformer set/getOutputProperty value to ?" + tmp + "?");
659             }
660             catch (Exception e)
661             {
662                 reporter.logThrowable(reporter.ERRORMSG, e, "Problem(a2) with set/get output properties");
663                 reporter.checkFail("Problem(a2) with set/get output properties", "SCUU4RXR6E");
664             }
665             try
666             {   // Inner try-catch
667                 // Try getting the whole properties block, so we can see what it thinks it has
668                 outputTransformer = outputTemplates.newTransformer();
669                 outputTransformer.setErrorListener(new DefaultErrorHandler());
670                 Properties newOutProps = outputTransformer.getOutputProperties();
671                 reporter.logHashtable(reporter.STATUSMSG, newOutProps,
672                                       "Another getOutputProperties()");
673 
674                 // Simple set/getOutputProperty
675                 String tmp = outputTransformer.getOutputProperty(OutputKeys.ENCODING);
676                 reporter.logTraceMsg(OutputKeys.ENCODING + " is currently: " + tmp);
677                 outputTransformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
678                 tmp = outputTransformer.getOutputProperty(OutputKeys.ENCODING);
679                 reporter.check(tmp, "UTF-8", "outputTransformer set/getOutputProperty value to ?" + tmp + "?");
680             }
681             catch (Exception e)
682             {
683                 reporter.logThrowable(reporter.ERRORMSG, e,
684                                       "Problem(a3) with set/get output property");
685             }
686 
687             // OutputKeys.METHOD = xml|html|text|qname-but-not-ncname
688             // OutputKeys.VERSION = number
689             // OutputKeys.ENCODING = string
690             // OutputKeys.OMIT_XML_DECLARATION = yes|no
691             // OutputKeys.STANDALONE = yes|no
692             // OutputKeys.DOCTYPE_PUBLIC = string
693             // OutputKeys.DOCTYPE_SYSTEM = string
694             // OutputKeys.CDATA_SECTION_ELEMENTS = qnames
695             // OutputKeys.INDENT = qnames
696             // OutputKeys.MEDIA_TYPE = qnames
697             // OutputKeys.CDATA_SECTION_ELEMENTS = qnames
698 
699             reporter.logTraceMsg("//@todo Cover setOutputProperties(Properties oformat)");
700         }
701         catch (Exception e)
702         {
703             reporter.logThrowable(reporter.ERRORMSG, e,
704                                   "Problem(4) with set/get output properties");
705             reporter.checkFail("Problem(4) with set/get output properties");
706         }
707 
708         reporter.logTraceMsg("//@todo: Negative testing: various illegal arguments, etc. - in separate testcase");
709 
710         reporter.testCaseClose();
711         return true;
712     }
713 
714     /**
715      * API coverage test of Transformer.set/getOutputProperties()
716      * See {@link OutputPropertiesTest} for more coverage on setting
717      * different kinds of outputs, etc.
718      *
719      * @return false if we should abort the test
720      */
testCase3()721     public boolean testCase3()
722     {
723         reporter.testCaseInit("API coverage test of Transformer.set/getOutputProperties()");
724         TransformerFactory factory = null;
725         Templates outputTemplates = null;
726         Transformer outputTransformer = null;
727         Transformer identityTransformer = null;
728         try
729         {
730             factory = TransformerFactory.newInstance();
731             factory.setErrorListener(new DefaultErrorHandler());
732             identityTransformer = factory.newTransformer();
733             identityTransformer.setErrorListener(new DefaultErrorHandler());
734             outputTemplates = factory.newTemplates(new StreamSource(outputFormatTest.inputName));
735         }
736         catch (Throwable t)
737         {
738             reporter.checkFail("Problem creating Templates; cannot continue");
739             reporter.logThrowable(reporter.ERRORMSG, t,
740                                   "Problem creating Templates; cannot continue");
741             return true;
742         }
743         try
744         {
745             // See what the default 'identity' transform has by default
746             Properties identityProps = identityTransformer.getOutputProperties(); // SPR SCUU4RXQYH throws npe
747             reporter.check((null != identityProps), true, "identityTransformer.getOutputProperties() is non-null");
748             reporter.logHashtable(reporter.STATUSMSG, identityProps,
749                                   "default identityTransformer.getOutputProperties()");
750         }
751         catch (Exception e)
752         {
753             reporter.logThrowable(reporter.ERRORMSG, e,
754                                   "Problem with identity OutputProperties");
755             reporter.checkFail("Problem with identity OutputProperties", "SCUU4RXQYH");
756         }
757 
758         reporter.logTraceMsg("//@todo: coverage of non-identity transformers and set/get of props");
759         reporter.testCaseClose();
760         return true;
761     } // end testCase3
762 
763 
764     /**
765      * Negative tests of Transformer.set/getOutputProperty/ies()
766      *
767      * @return false if we should abort the test
768      */
testCase4()769     public boolean testCase4()
770     {
771         reporter.testCaseInit("Negative tests of Transformer.set/getOutputProperty/ies()");
772         TransformerFactory factory = null;
773         Templates outputTemplates = null;
774         Transformer outputTransformer = null;
775         Transformer identityTransformer = null;
776         try
777         {
778             factory = TransformerFactory.newInstance();
779             factory.setErrorListener(new DefaultErrorHandler());
780             identityTransformer = factory.newTransformer();
781             identityTransformer.setErrorListener(new DefaultErrorHandler());
782             outputTemplates = factory.newTemplates(new StreamSource(outputFormatTest.inputName));
783         }
784         catch (Throwable t)
785         {
786             reporter.checkFail("Problem creating Templates; cannot continue");
787             reporter.logThrowable(reporter.ERRORMSG, t,
788                                   "Problem creating Templates; cannot continue");
789             return true;
790         }
791 
792         GetOutputPropertyTestlet testlet = new GetOutputPropertyTestlet();
793         reporter.logTraceMsg("Using GetOutputPropertyTestlet for negative tests");
794         testlet.setLogger(reporter);
795 
796         // Negative tests of getOutputProperty()
797         GetOutputPropertyDatalet datalet = new GetOutputPropertyDatalet();
798         reporter.logTraceMsg("Using GetOutputPropertyDatalet for negative tests");
799 
800         try
801         {
802             datalet.transformer = identityTransformer;
803             datalet.propName = "bogus-name";
804             datalet.expectedValue = null;
805             datalet.expectedException = "java.lang.IllegalArgumentException";
806             datalet.setDescription("bogus-name identityTransformer throws IllegalArgumentException");
807             testlet.execute(datalet);
808 
809             datalet.transformer = identityTransformer;
810             datalet.propName = "bogus-{name}";
811             datalet.expectedValue = null;
812             datalet.expectedException = "java.lang.IllegalArgumentException";
813             datalet.setDescription("bogus-{name} throws IllegalArgumentException");
814             testlet.execute(datalet);
815 
816             datalet.transformer = identityTransformer;
817             datalet.propName = "{bogus-name";
818             datalet.expectedValue = null;
819             datalet.expectedException = "java.lang.IllegalArgumentException";
820             datalet.setDescription("{bogus-name throws IllegalArgumentException (bracket not closed)");
821             testlet.execute(datalet);
822 
823             datalet.transformer = identityTransformer;
824             datalet.propName = "{some-namespace}bogus-name";
825             datalet.expectedValue = datalet.NULL_VALUE_EXPECTED;
826             datalet.expectedException = null;
827             datalet.setDescription("{some-namespace}bogus-name returns null");
828             testlet.execute(datalet);
829 
830             datalet.transformer = identityTransformer;
831             datalet.propName = "{just-some-namespace}";
832             datalet.expectedValue = datalet.NULL_VALUE_EXPECTED;
833             datalet.expectedException = null;
834             datalet.setDescription("{just-some-namespace}bogus-name returns null");
835             testlet.execute(datalet);
836 
837             datalet.transformer = identityTransformer;
838             datalet.propName = "{}no-namespace-at-all";
839             datalet.expectedValue = datalet.NULL_VALUE_EXPECTED;
840             datalet.expectedException = null;
841             datalet.setDescription("{}no-namespace-at-all returns null (is this correct?)");
842             testlet.execute(datalet);
843 
844             datalet.transformer = identityTransformer;
845             datalet.propName = OutputKeys.METHOD;
846             datalet.expectedValue = "xml";
847             datalet.expectedException = null;
848             datalet.setDescription(OutputKeys.METHOD + " returns xml on identity transformer (is this really correct?)");
849             testlet.execute(datalet);
850 
851         }
852         catch (Throwable t)
853         {
854             reporter.logThrowable(reporter.STATUSMSG, t, "Problem(1) with negative identityTransformer getOutputProperty");
855             reporter.checkErr("Problem(1) with negative identityTransformer getOutputProperty: " + t.toString());
856         }
857         try
858         {
859             datalet.transformer = outputTemplates.newTransformer();
860             datalet.propName = "bogus-name";
861             datalet.expectedValue = null;
862             datalet.expectedException = "java.lang.IllegalArgumentException";
863             datalet.setDescription("bogus-name regular transformer throws IllegalArgumentException");
864             testlet.execute(datalet);
865 
866             datalet.transformer = outputTemplates.newTransformer();
867             datalet.propName = "bogus-{name}";
868             datalet.expectedValue = null;
869             datalet.expectedException = "java.lang.IllegalArgumentException";
870             datalet.setDescription("bogus-{name} throws IllegalArgumentException");
871             testlet.execute(datalet);
872 
873             datalet.transformer = outputTemplates.newTransformer();
874             datalet.propName = "{bogus-name";
875             datalet.expectedValue = null;
876             datalet.expectedException = "java.lang.IllegalArgumentException";
877             datalet.setDescription("{bogus-name throws IllegalArgumentException (bracket not closed)");
878             /* testlet.execute(datalet); comment out 10-May-01 -sc Bugzilla 890 */
879             /* This is a fairly unimportant bug that may well be
880                fixed in the javadoc, so I'm commenting this case
881                out so we can run this test in the smoketest and
882                get more regular coverage of it! */
883 
884             datalet.transformer = outputTemplates.newTransformer();
885             datalet.propName = "{some-namespace}bogus-name";
886             datalet.expectedValue = datalet.NULL_VALUE_EXPECTED;
887             datalet.expectedException = null;
888             datalet.setDescription("{some-namespace}bogus-name returns null");
889             testlet.execute(datalet);
890 
891             datalet.transformer = outputTemplates.newTransformer();
892             datalet.propName = "{just-some-namespace}";
893             datalet.expectedValue = datalet.NULL_VALUE_EXPECTED;
894             datalet.expectedException = null;
895             datalet.setDescription("{just-some-namespace}bogus-name returns null");
896             testlet.execute(datalet);
897 
898             datalet.transformer = outputTemplates.newTransformer();
899             datalet.propName = "{}no-namespace-at-all";
900             datalet.expectedValue = datalet.NULL_VALUE_EXPECTED;
901             datalet.expectedException = null;
902             datalet.setDescription("{}no-namespace-at-all returns null (is this correct?)");
903             testlet.execute(datalet);
904 
905             datalet.transformer = outputTemplates.newTransformer();
906             datalet.propName = OutputKeys.METHOD;
907             datalet.expectedValue = METHOD_VALUE;
908             datalet.expectedException = null;
909             datalet.setDescription(OutputKeys.METHOD + " returns " + METHOD_VALUE + " on transformer");
910             testlet.execute(datalet);
911 
912         }
913         catch (Throwable t)
914         {
915             reporter.logThrowable(reporter.STATUSMSG, t, "Problem(2) with negative transformer getOutputProperty");
916             reporter.checkErr("Problem(2) with negative transformer getOutputProperty: " + t.toString());
917         }
918 
919         reporter.testCaseClose();
920         return true;
921     } // end testCase4
922 
923 
924     /**
925      * TRAX Transformer: cover transform() API and multiple
926      * Transformations from single transformer.
927      *
928      * Note: obviously as the most important API, transform() is
929      * also covered in many other API tests as well as in various
930      * Stylesheet*Testlet tests.
931      *
932      * @return false if we should abort the test
933      */
testCase5()934     public boolean testCase5()
935     {
936         reporter.testCaseInit(
937             "TRAX Transformer: cover multiple calls to transform()");
938         TransformerFactory factory = null;
939         Templates templates = null;
940         try
941         {
942             factory = TransformerFactory.newInstance();
943             factory.setErrorListener(new DefaultErrorHandler());
944         }
945         catch (Throwable t)
946         {
947             reporter.logThrowable(reporter.STATUSMSG, t, "Can't continue testcase, factory.newInstance threw:");
948             reporter.checkErr("Can't continue testcase, factory.newInstance threw: " + t.toString());
949             return true;
950         }
951 
952         try
953         {
954             Transformer transformer = factory.newTransformer(new StreamSource(simpleTest.inputName));
955             transformer.setErrorListener(new DefaultErrorHandler());
956             // Re-use the transformer multiple times on identity
957             transformer.transform(new StreamSource(simpleTest.xmlName),
958                                   new StreamResult(outNames.nextName()));
959             fileChecker.check(reporter,
960                 new File(outNames.currentName()),
961                 new File(simpleTest.goldName),
962                 "transform(#1) identity into: " + outNames.currentName());
963 
964             transformer.transform(new StreamSource(simpleTest.xmlName),
965                                   new StreamResult(outNames.nextName()));
966             fileChecker.check(reporter,
967                 new File(outNames.currentName()),
968                 new File(simpleTest.goldName),
969                 "transform(#2) identity into: " + outNames.currentName());
970 
971             transformer.transform(new StreamSource(simpleTest.xmlName),
972                                   new StreamResult(outNames.nextName()));
973             fileChecker.check(reporter,
974                 new File(outNames.currentName()),
975                 new File(simpleTest.goldName),
976                 "transform(#3) identity into: " + outNames.currentName());
977 
978             transformer = factory.newTransformer(new StreamSource(multiTest.inputName));
979             transformer.setErrorListener(new DefaultErrorHandler());
980             // Re-use the transformer multiple times on file with variable
981             transformer.transform(new StreamSource(multiTest.xmlName),
982                                   new StreamResult(outNames.nextName()));
983             fileChecker.check(reporter,
984                 new File(outNames.currentName()),
985                 new File(multiTest.goldName),
986                 "transform(#1-a) var test into: " + outNames.currentName());
987 
988             transformer.transform(new StreamSource(multiTest.xmlName),
989                                   new StreamResult(outNames.nextName()));
990             fileChecker.check(reporter,
991                 new File(outNames.currentName()),
992                 new File(multiTest.goldName),
993                 "transform(#2-a) var test into: " + outNames.currentName());
994 
995             // Reset the transformer to its original state
996             transformer.reset();
997 
998             transformer.transform(new StreamSource(multiTest.xmlName),
999                                   new StreamResult(outNames.nextName()));
1000             fileChecker.check(reporter,
1001                 new File(outNames.currentName()),
1002                 new File(multiTest.goldName),
1003                 "transform(#3-a) var test into: " + outNames.currentName());
1004 
1005             // Now re-use with different xml doc
1006             transformer.transform(new StreamSource(multi2Test.xmlName),
1007                                   new StreamResult(outNames.nextName()));
1008             fileChecker.check(reporter,
1009                 new File(outNames.currentName()),
1010                 new File(multi2Test.goldName),
1011                 "transform(#4-b) var test into: " + outNames.currentName());
1012 
1013             // Reset the transformer to its original state
1014             transformer.reset();
1015 
1016             // Now re-use with original xml doc
1017             transformer.transform(new StreamSource(multiTest.xmlName),
1018                                   new StreamResult(outNames.nextName()));
1019             fileChecker.check(reporter,
1020                 new File(outNames.currentName()),
1021                 new File(multiTest.goldName),
1022                 "transform(#5-a) var test into: " + outNames.currentName());
1023         }
1024         catch (Throwable t)
1025         {
1026             reporter.logThrowable(reporter.WARNINGMSG, t, "Multiple transform() calls threw");
1027             reporter.checkErr("Multiple transform() calls threw: " + t.toString());
1028         }
1029         reporter.testCaseClose();
1030         return true;
1031     }
1032 
1033     /**
1034      * TRAX Transformer: cover set/getURIResolver() API;
1035      * plus set/getErrorListener() API.
1036      *
1037      * Note: These are simply coverage tests for these api's,
1038      * for feature testing see links below
1039      *
1040      * @see ErrorListenerTest
1041      * @see ErrorListenerAPITest
1042      * @see URIResolverTest
1043      * @return false if we should abort the test
1044      */
testCase6()1045     public boolean testCase6()
1046     {
1047         reporter.testCaseInit(
1048             "TRAX Transformer: cover transform() and set/getURIResolver API and functionality");
1049         TransformerFactory factory = null;
1050         Templates templates = null;
1051         try
1052         {
1053             factory = TransformerFactory.newInstance();
1054             // Grab a stylesheet to use for this testcase
1055             factory.setErrorListener(new DefaultErrorHandler());
1056             templates = factory.newTemplates(new StreamSource(simpleTest.inputName));
1057         }
1058         catch (Throwable t)
1059         {
1060             reporter.checkErr("Can't continue testcase, factory.newInstance threw: " + t.toString());
1061             reporter.logThrowable(reporter.STATUSMSG, t, "Can't continue testcase, factory.newInstance threw:");
1062             return true;
1063         }
1064 
1065         try
1066         {
1067             Transformer transformer = templates.newTransformer();
1068             ErrorListener errListener = transformer.getErrorListener(); // SPR SCUU4R3K6G - is null
1069             if (errListener == null)
1070             {
1071                 reporter.checkFail("getErrorListener() non-null by default");
1072             }
1073             else
1074             {
1075                 reporter.checkPass("getErrorListener() non-null by default, is: " + errListener);
1076             }
1077 
1078             LoggingErrorListener loggingErrListener = new LoggingErrorListener(reporter);
1079             transformer.setErrorListener(loggingErrListener);
1080             reporter.checkObject(transformer.getErrorListener(), loggingErrListener, "set/getErrorListener API coverage(1)");
1081             try
1082             {
1083                 transformer.setErrorListener(null);
1084                 reporter.checkFail("setErrorListener(null) worked, should have thrown exception");
1085             }
1086             catch (IllegalArgumentException iae)
1087             {
1088                 reporter.checkPass("setErrorListener(null) properly threw: " + iae.toString());
1089             }
1090             // Verify the previous ErrorListener is still set
1091             reporter.checkObject(transformer.getErrorListener(), loggingErrListener, "set/getErrorListener API coverage(2)");
1092         }
1093         catch (Throwable t)
1094         {
1095             reporter.checkErr("Coverage of get/setErrorListener threw: " + t.toString());
1096             reporter.logThrowable(reporter.STATUSMSG, t, "Coverage of get/setErrorListener threw:");
1097         }
1098         reporter.logStatusMsg("@todo feature testing for ErrorListener; see ErrorListenerTest, ErrorListenerAPITest");
1099 
1100         try
1101         {
1102             Transformer transformer = templates.newTransformer();
1103             transformer.setErrorListener(new DefaultErrorHandler());
1104             // URIResolver should be null by default; try to set/get one
1105             reporter.checkObject(transformer.getURIResolver(), null, "getURIResolver is null by default");
1106             LoggingURIResolver myURIResolver = new LoggingURIResolver(reporter);
1107             transformer.setURIResolver(myURIResolver);
1108             reporter.checkObject(transformer.getURIResolver(), myURIResolver, "set/getURIResolver API coverage");
1109             reporter.logTraceMsg("myURIres.getQuickCounters = " + myURIResolver.getQuickCounters());
1110 
1111             // Assumes we support Streams
1112             FileOutputStream fos = new FileOutputStream(outNames.nextName());
1113             if (doTransform(transformer,
1114                             new StreamSource(simpleTest.xmlName),
1115                             new StreamResult(fos)))
1116             {
1117                 fos.close(); // must close ostreams we own
1118                 fileChecker.check(reporter,
1119                         new File(outNames.currentName()),
1120                         new File(simpleTest.goldName),
1121                         "transform(Stream, Stream) into: " + outNames.currentName());
1122             }
1123             reporter.logTraceMsg("myURIres.getQuickCounters = " + myURIResolver.getQuickCounters());
1124 
1125             reporter.logStatusMsg("@todo feature testing for URIResolver; see URIResolverTest");
1126         }
1127         catch (Throwable t)
1128         {
1129             reporter.logThrowable(reporter.ERRORMSG, t, "TestCase threw:");
1130             reporter.checkFail("TestCase threw: " + t.toString());
1131         }
1132         reporter.testCaseClose();
1133         return true;
1134     }
1135 
1136 
1137     /**
1138      * Worker method performs transforms (and catches exceptions, etc.)
1139      * Side effect: checkFail() if exception thrown
1140      *
1141      * @param Transformer to use
1142      * @param Source to pull in XML from
1143      * @param Result to put output in; may be modified
1144      * @return false if exception thrown, true otherwise
1145      */
doTransform(Transformer t, Source s, Result r)1146     public boolean doTransform(Transformer t, Source s, Result r)
1147     {
1148         try
1149         {
1150             t.transform(s, r);
1151             return true;
1152         }
1153         catch (TransformerException e)
1154         {
1155             reporter.checkFail("doTransform threw: " + e.toString());
1156             reporter.logThrowable(reporter.ERRORMSG, e, "doTransform threw:");
1157             return false;
1158         }
1159     }
1160 
1161     /**
1162      * Datalet for output property testing.
1163      * Fields: transformer, propName, (expectedValue|expectedException)
1164      */
1165     public class GetOutputPropertyDatalet implements Datalet
1166     {
GetOutputPropertyDatalet()1167         public GetOutputPropertyDatalet() {} // no-op
GetOutputPropertyDatalet(String[] args)1168         public GetOutputPropertyDatalet(String[] args)
1169         {
1170             load(args);
1171         }
1172         public final String IDENTITY = "identity";
1173         public final String NULL_VALUE_EXPECTED = "NULL_VALUE_EXPECTED";
1174         protected String description = "no data";
getDescription()1175         public String getDescription() { return description; }
setDescription(String d)1176         public void setDescription(String d) { description = d; }
1177         public Transformer transformer = null;
1178         public String propName = null;
1179         public String expectedValue = null;
1180         public String expectedException = null;
load(String[] args)1181         public void load(String[] args)
1182         {
1183             try
1184             {
1185                 if (IDENTITY.equals(args[0]))
1186                     transformer = (TransformerFactory.newInstance()).newTransformer();
1187                 else
1188                     transformer = (TransformerFactory.newInstance()).newTransformer(new StreamSource(args[0]));
1189                 propName = args[1];
1190                 String tmp = args[2];
1191                 // Semi-hack: if it looks like the FQCN of a
1192                 //  Throwable derivative, then use one
1193                 //  of those; otherwise, assume it's the expected
1194                 //  value to get back from getOutputProperty
1195                 if ((tmp.indexOf("Exception") >= 0) || (tmp.indexOf("Error") >= 0))
1196                     expectedException = tmp;
1197                 else
1198                     expectedValue = tmp;
1199             }
1200             catch (Throwable t)
1201             { /* no-op, let it fail elsewhere */
1202             }
1203         }
load(Hashtable h)1204         public void load(Hashtable h)
1205         {
1206             transformer = (Transformer)h.get("transformer");
1207             propName = (String)h.get("propName");
1208             expectedValue  = (String)h.get("expectedValue ");
1209             expectedException = (String)h.get("expectedException");
1210         }
1211 
1212     } // end class GetOutputPropertyDatalet
1213 
1214     /**
1215      * Calls getOutputProperty() on the Transformer supplied, and
1216      * then either validates the returned String, or the classname
1217      * of any exception thrown.
1218      *
1219      * This is almost more complex to implement as a Testlet than
1220      * is really worth it, but I wanted to experiment with using one.
1221      */
1222     public class GetOutputPropertyTestlet extends TestletImpl
1223     {
1224         { thisClassName = "org.apache.qetest.xsl.GetOutputPropertyTestlet"; }
getDescription()1225         public String getDescription() { return "gets OutputProperty and validates"; }
getDefaultDatalet()1226         public Datalet getDefaultDatalet()
1227         {
1228             return new GetOutputPropertyDatalet(new String[] { "identity", "method", "xml" });
1229         }
execute(Datalet d)1230         public void execute(Datalet d)
1231         {
1232             GetOutputPropertyDatalet datalet = null;
1233             try
1234             {
1235                 datalet = (GetOutputPropertyDatalet)d;
1236             }
1237             catch (ClassCastException e)
1238             {
1239                 logger.checkErr("Datalet provided is not a GetOutputPropertyDatalet; cannot continue");
1240                 return;
1241             }
1242             try
1243             {
1244                 // Perform the test
1245                 String val = datalet.transformer.getOutputProperty(datalet.propName);
1246                 // Validate non-throwing of expected exceptions
1247                 if (null != datalet.expectedException)
1248                 {
1249                     logger.checkFail(datalet.getDescription() + ", did not throw:" + datalet.expectedException
1250                                      + ", act:" + val);
1251                     return;
1252                 }
1253 
1254                 // Validate any return data
1255                 if (null != val)
1256                 {
1257                     // Check for positive values that exist
1258                     if ((null != datalet.expectedValue)
1259                         && datalet.expectedValue.equals(val))
1260                         logger.checkPass(datalet.getDescription());
1261                     else
1262                         logger.checkFail(datalet.getDescription() + " act:" + val
1263                                          + ", exp:" + datalet.expectedValue);
1264                 }
1265                 else if (datalet.NULL_VALUE_EXPECTED == datalet.expectedValue)
1266                 {
1267                     // If expectedValue is the special 'null' string,
1268                     //  and we're not expecting an exception, then
1269                     //  we pass here
1270                    logger.checkPass(datalet.getDescription());
1271                 }
1272                 else
1273                 {
1274                     // Otherwise, we fail
1275                     logger.checkFail(datalet.getDescription() + " act:" + val
1276                                      + ", exp:" + datalet.expectedValue);
1277                 }
1278             }
1279             catch (Throwable t)
1280             {
1281                 // Validate any Exceptions thrown
1282                 if (null != datalet.expectedException)
1283                 {
1284                     if (datalet.expectedException.equals(t.getClass().getName()))
1285                         logger.checkPass(datalet.getDescription());
1286                     else
1287                         logger.checkFail(datalet.getDescription() + ", threw:" + t.toString()
1288                                          + ", exp:" + datalet.expectedException);
1289                 }
1290                 else
1291                     logger.checkFail(datalet.getDescription() + ", threw: " + t.toString());
1292             }
1293         }
1294     } // end class GetOutputPropertyTestlet
1295 
1296     /**
1297      * Convenience method to print out usage information - update if needed.
1298      * @return usage string
1299      */
usage()1300     public String usage()
1301     {
1302         return ("Common [optional] options supported by TransformerAPITest:\n"
1303                 + "(Note: assumes inputDir=.\\tests\\api)\n"
1304                 + "-processorClassname classname.of.processor  (to override setPlatformDefaultProcessor to Xalan 2.x)\n"
1305                 + super.usage());
1306     }
1307 
1308     /**
1309      * Main method to run test from the command line - can be left alone.
1310      * @param args command line argument array
1311      */
main(String[] args)1312     public static void main(String[] args)
1313     {
1314         TransformerAPITest app = new TransformerAPITest();
1315         app.doMain(args);
1316     }
1317 
1318     // Android-added: Run main method as a JUnit test case.
1319     @FlakyTest(bugId = 292520220)
1320     @Test
main()1321     public void main() {
1322         main(new String[0]);
1323     }
1324 }
1325