• 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 /*
23  *
24  * SystemIdTest.java
25  *
26  */
27 package org.apache.qetest.trax;
28 
29 import java.io.File;
30 import java.util.Properties;
31 
32 import javax.xml.transform.Source;
33 import javax.xml.transform.Templates;
34 import javax.xml.transform.Transformer;
35 import javax.xml.transform.TransformerException;
36 import javax.xml.transform.TransformerFactory;
37 import javax.xml.transform.stream.StreamResult;
38 import javax.xml.transform.stream.StreamSource;
39 
40 import org.apache.qetest.FileBasedTest;
41 import org.apache.qetest.Logger;
42 import org.apache.qetest.OutputNameManager;
43 import org.apache.qetest.QetestUtils;
44 import org.apache.qetest.xsl.XSLTestfileInfo;
45 
46 //-------------------------------------------------------------------------
47 
48 /**
49  * Test behavior of various types of systemId forms.
50  * <b>Note:</b> This test is directory-dependent, so if there are
51  * any fails, check the code to see what the test file is expecting
52  * the path/directory/etc. to be.
53  * I'm also using RFC1738 as the definition of what various
54  * systemId's should look like: the validation of this test is
55  * a little tricky, since it's not necessarily clear exactly which
56  * forms of file: always have to be supported by the application.
57  * (Or which forms of file: are supported by the underlying parser)
58  * Also, we really need to write some platform-dependent tests
59  * for this area, to account for 'c:\foo' type paths in Windows,
60  * and for '/usr/bin' type paths in UNIX-land, as well as various
61  * links and such (although that may be beyond the scope of what
62  * Xalan and it's parser do, and more into testing the JDK itself).
63  *
64  * Note RFC1738 specifies that file: URL's always use
65  *  forward slash / as a file separator
66  * ABSOLUTE-PATHS
67  * file:///e:/path/file.txt absolute path to e:/path/file.txt
68  *
69  * file://localhost/e:/path/file.txt path to e:/path/file.txt
70  *  on localhost, but goes direct to filesystem
71  *
72  * file://otherhost/e:/path/file.txt absolute path to
73  *  otherhost, but RFC1738 doesn't actually specify the
74  *  protocol to use to get to the other machine to actually
75  *  get the file
76  *
77  * RELATIVE-PATHS
78  * file:e:/path/file.txt is theoretically a relative URL
79  *  w/r/t the current Base URL
80  *
81  * file:/e:/path/file.txt is theoretically a relative URL
82  *  that starts with /e:/... - not likely to be useful
83  *
84  * file://file.txt theoretically a hostname (not a filename)
85  *  with no path or file portion
86  *
87  * ILLEGAL-PATHS
88  * file://e:\path\file.txt is illegal, since RFC1738
89  *  specifically says / forward slashes
90  *
91  * @author shane_curcuru@lotus.com
92  * @version $Id$
93  */
94 public class SystemIdTest extends FileBasedTest
95 {
96 
97     /**
98      * Provides nextName(), currentName() functionality for tests
99      * that may produce any number of output files.
100      */
101     protected OutputNameManager outNames;
102 
103     /**
104      * Name of a valid, known-good xsl/xml file pair we can use.
105      */
106     protected XSLTestfileInfo knownGoodFileInfo = new XSLTestfileInfo();
107 
108     /**
109      * Just basename of a valid, known-good file, both .xsl/.xml .
110      */
111     protected String knownGoodBaseName = null;
112 
113     /** Subdirectory under test\tests\api for our xsl/xml files.  */
114     public static final String TRAX_SUBDIR = "trax";
115 
116     /** Convenience variable for user.dir - cached during test.  */
117     protected String savedUserDir = null;
118 
119     /** Internal flag for test that we have not yet verified expected result.  */
120     protected static final String EXPECTED_RESULT_UNKNOWN = "EXPECTED_RESULT_UNKNOWN";
121 
122     /** Internal flag for test that should return non-null (and no exceptions).  */
123     protected static final String EXPECTED_RESULT_NONNULL = "EXPECTED_RESULT_NONNULL";
124 
125     /**
126      * Internal flag for test that should do a transform.
127      * Presumably using the systemId item you just tested and
128      * one of our known-good test files.
129      */
130     protected static final String EXPECTED_RESULT_DOTRANSFORM = "EXPECTED_RESULT_DOTRANSFORM";
131 
132     /** Just initialize test name, comment, numTestCases. */
SystemIdTest()133     public SystemIdTest()
134     {
135         numTestCases = 2;  // REPLACE_num
136         testName = "SystemIdTest";
137         testComment = "Test behavior of various types of systemId forms";
138     }
139 
140 
141     /**
142      * Initialize this test - Set names of xml/xsl test files,
143      * cache user.dir property.
144      *
145      * @param p Properties to initialize from (unused)
146      * @return false if we should abort the test; true otherwise
147      */
doTestFileInit(Properties p)148     public boolean doTestFileInit(Properties p)
149     {
150         // Used for all tests; just dump files in trax subdir
151         File outSubDir = new File(outputDir + File.separator + TRAX_SUBDIR);
152         if (!outSubDir.mkdirs())
153             reporter.logWarningMsg("Could not create output dir: " + outSubDir);
154         // Initialize an output name manager to that dir with .out extension
155         outNames = new OutputNameManager(outputDir + File.separator + TRAX_SUBDIR
156                                          + File.separator + testName, ".out");
157 
158         String testBasePath = inputDir
159                               + File.separator
160                               + TRAX_SUBDIR
161                               + File.separator;
162         String goldBasePath = goldDir
163                               + File.separator
164                               + TRAX_SUBDIR
165                               + File.separator;
166 
167         // Just bare pathnames, not URI's
168         knownGoodBaseName = testName;
169         knownGoodFileInfo.inputName = testBasePath + knownGoodBaseName + ".xsl";
170         knownGoodFileInfo.xmlName = testBasePath + knownGoodBaseName + ".xml";
171         knownGoodFileInfo.goldName = goldBasePath + knownGoodBaseName + ".out";
172 
173         // Cache user.dir property
174         savedUserDir = System.getProperty("user.dir");
175         reporter.logHashtable(Logger.STATUSMSG, System.getProperties(), "System.getProperties()");
176         reporter.logHashtable(Logger.STATUSMSG, testProps, "testProps");
177 
178         return true;
179     }
180 
181 
182     /**
183      * Cleanup this test - uncache user.dir property.
184      *
185      * @param p Properties to initialize from (if needed)
186      * @return false if we should abort the test; true otherwise
187      */
doTestFileClose(Properties p)188     public boolean doTestFileClose(Properties p)
189     {
190         // Uncache user.dir property
191         System.getProperties().put("user.dir", savedUserDir);
192         return true;
193     }
194 
195 
196     /**
197      * Test various forms of XSL and XML systemIds to see what happens.
198      *
199      * @return false if we should abort the test; true otherwise
200      */
testCase1()201     public boolean testCase1()
202     {
203         reporter.testCaseInit("Test various forms of XSL and XML systemIds to see what happens");
204 
205         // The path the user gave us in inputDir:
206         // inputDir + "/" + TRAX_SUBDIR + "/" filename
207         String inputDirPath = inputDir.replace('\\', '/')
208                               + "/" + TRAX_SUBDIR + "/" + knownGoodBaseName;
209         //@todo: determine what type of path inputDir itself is:
210         //  downwards relative, upwards relative, or absolute
211 
212         // Assumed correct user.dir path (should be in xml-xalan\test):
213         // System.getProperty("user.dir") + "/tests/api/" + TRAX_SUBDIR
214         // user.dir in theory should always be absolute
215         String userDirPath = System.getProperty("user.dir").replace('\\', '/')
216                              + "/tests/api/" + TRAX_SUBDIR + "/" + knownGoodBaseName;
217 
218         // Verify that user.dir is in the right place relative to
219         //  the checked-in known-good files
220         String userDirExpected = EXPECTED_RESULT_UNKNOWN;
221         File f1 = new File(userDirPath + ".xsl");
222         File f2 = new File(userDirPath + ".xml");
223         if (f1.exists() && f2.exists())
224         {
225             // The known-good files are there, so expect we can use it
226             userDirExpected = EXPECTED_RESULT_DOTRANSFORM;
227         }
228         else
229         {
230             reporter.logWarningMsg("Known good files does not appear to exist at: "
231                                    + userDirPath + ".xml/.xsl");
232         }
233 
234         String xslTestIds[][] =
235         {
236             // { systemId to test,
237             //   description of the test,
238             //   expected XSL behavior or exception,
239             //   expected XSL inner exception,
240             //   expected XML behavior or exception,
241             //   expected XML inner exception
242             // }
243 
244             // Test variations on the inputDir specified by the
245             //  user, to be able to do some adhoc testing
246             { "file:///" + inputDirPath,
247             "file:///, user-specified inputDir, /blah1[1a]",
248             EXPECTED_RESULT_UNKNOWN,
249             null,
250             EXPECTED_RESULT_UNKNOWN,
251             null },
252 
253             { "file://localhost/" + inputDirPath,
254             "file://localhost/, user-specified inputDir, /blah[1b]",
255             EXPECTED_RESULT_UNKNOWN,
256             null,
257             EXPECTED_RESULT_UNKNOWN,
258             null },
259 
260             { inputDirPath,
261             "Just user-specified inputDir, /blah (works normally, if relative)[1c]",
262             EXPECTED_RESULT_UNKNOWN,
263             null,
264             EXPECTED_RESULT_UNKNOWN,
265             null },
266 
267             // Test variations on the System user.dir; validation
268             //  depends on if it's set correctly
269             { "file:///" + userDirPath,
270             "file:///, System(user.dir), /blah (works normally)[2a]",
271             userDirExpected,
272             null,
273             userDirExpected,
274             null },
275 
276             { "file://localhost/" + userDirPath,
277             "file://localhost/, System(user.dir), /blah (works normally)[2b]",
278             userDirExpected,
279             null,
280             userDirExpected,
281             null },
282 
283             { userDirPath,
284             "Just System(user.dir), /blah[2c]",
285             EXPECTED_RESULT_UNKNOWN,
286             null,
287             EXPECTED_RESULT_UNKNOWN,
288             null },
289 
290             // Absolute path with blank . step
291             { "file:///" + System.getProperty("user.dir").replace('\\', '/')
292                          + "/tests/./api/" + TRAX_SUBDIR + "/" + knownGoodBaseName,
293             "file:///, System(user.dir), /./blah (???)[2d]",
294             userDirExpected,
295             null,
296             userDirExpected,
297             null },
298 
299             // Absolute path with up/down steps
300             { "file:///" + System.getProperty("user.dir").replace('\\', '/')
301                          + "/tests/../tests/api/" + TRAX_SUBDIR + "/" + knownGoodBaseName,
302             "file:///, System(user.dir), /updir/../downdir/blah (???)[2e]",
303             userDirExpected,
304             null,
305             userDirExpected,
306             null },
307 
308             // Just relative paths, should work if user.dir correct
309             // Arguable: comment out for 2.0
310             { "file:tests/api/" + TRAX_SUBDIR + "/" + knownGoodBaseName,
311             "Just file:/blah relative path[3a]",
312             EXPECTED_RESULT_UNKNOWN,
313             null,
314             EXPECTED_RESULT_UNKNOWN,
315             null },
316 
317             { "tests/api/" + TRAX_SUBDIR + "/" + knownGoodBaseName,
318             "Just /blah relative path[3b]",
319             userDirExpected,
320             null,
321             userDirExpected,
322             null },
323 
324             // file://blah should be interperted as a hostname,
325             //  not as a filename, and should fail
326             { "file://" + userDirPath,
327             "file://, System(user.dir), /blah (causes hostname error)[4a]",
328             EXPECTED_RESULT_UNKNOWN,
329             null,
330             EXPECTED_RESULT_UNKNOWN,
331             null },
332         // Comment out for 2.0 due to SPR SCUU4SUQXU
333         /*
334             "javax.xml.transform.TransformerConfigurationException",
335             "java.net.UnknownHostException",
336             "javax.xml.transform.TransformerException",
337             "java.net.UnknownHostException" },
338         */
339 
340             { "file://" + inputDirPath,
341             "file://, user-specified inputDir, /blah (causes hostname error)[4b]",
342             EXPECTED_RESULT_UNKNOWN,
343             null,
344             EXPECTED_RESULT_UNKNOWN,
345             null },
346         // Comment out for 2.0 due to SPR SCUU4SUQXU
347         /*
348             "javax.xml.transform.TransformerConfigurationException",
349             "java.net.UnknownHostException",
350             "javax.xml.transform.TransformerException",
351             "java.net.UnknownHostException" },
352         */
353 
354             // file://host.does.not.exist/blah should fail, here we
355             //  can also validate the error message completely
356             { "file://this.host.does.not.exist/" + userDirPath,
357             "file://this.host.does.not.exist/userDir/blah (causes hostname error)[4c]",
358             EXPECTED_RESULT_UNKNOWN,
359             null,
360             EXPECTED_RESULT_UNKNOWN,
361             null },
362         // Comment out for 2.0 due to SPR SCUU4SUQXU
363         /*
364             "javax.xml.transform.TransformerConfigurationException: this.host.does.not.exist",
365             "java.net.UnknownHostException: this.host.does.not.exist",
366             "javax.xml.transform.TransformerException: this.host.does.not.exist",
367             "java.net.UnknownHostException" },
368         */
369 
370             { "file://this.host.does.not.exist/" + inputDirPath,
371             "file://this.host.does.not.exist/inputDir/blah (causes hostname error)[4d]",
372             EXPECTED_RESULT_UNKNOWN,
373             null,
374             EXPECTED_RESULT_UNKNOWN,
375             null },
376         // Comment out for 2.0 due to SPR SCUU4SUQXU
377         /*
378             "javax.xml.transform.TransformerConfigurationException: this.host.does.not.exist",
379             "java.net.UnknownHostException: this.host.does.not.exist",
380             "javax.xml.transform.TransformerException: this.host.does.not.exist",
381             "java.net.UnknownHostException" },
382         */
383 
384 
385             // Too few leading slashes for the file: spec, probably error
386             { "file:/" + userDirPath,
387             "file:/, System(user.dir), /blah (probable error)[5a]",
388             EXPECTED_RESULT_UNKNOWN,
389             null,
390             EXPECTED_RESULT_UNKNOWN,
391             null },
392 
393             { "file:/" + inputDirPath,
394             "file:/, user-specified inputDir, /blah (probable error)[5b]",
395             EXPECTED_RESULT_UNKNOWN,
396             null,
397             EXPECTED_RESULT_UNKNOWN,
398             null },
399 
400             // No leading slashes for the file: spec, behavior is?
401             { "file:" + userDirPath,
402             "file:, System(user.dir), /blah (probable error)[6a]",
403             EXPECTED_RESULT_UNKNOWN,
404             null,
405             EXPECTED_RESULT_UNKNOWN,
406             null },
407 
408             { "file:" + inputDirPath,
409             "file:, user-specified inputDir, /blah (probable error)[6b]",
410             EXPECTED_RESULT_UNKNOWN,
411             null,
412             EXPECTED_RESULT_UNKNOWN,
413             null },
414 
415 
416             // Using backslashes in the path portion is explicitly
417             //  forbidden in the RFC, should give error
418             { "file:///" + userDirPath.replace('/', '\\'),
419             "file:///, System(user.dir) \\blah, (backslashes are illegal)[7a]",
420             EXPECTED_RESULT_UNKNOWN,
421             null,
422             EXPECTED_RESULT_UNKNOWN,
423             null },
424             { "file:///" + inputDirPath.replace('/', '\\'),
425             "file:///, user-specified inputDir \\blah (backslashes are illegal)[7b]",
426             EXPECTED_RESULT_UNKNOWN,
427             null,
428             EXPECTED_RESULT_UNKNOWN,
429             null }
430         };
431 
432         for (int i = 0; i < xslTestIds.length; i++)
433         {
434             // Loop and attempt to do newTemplates() with each
435             testNewTemplatesWithSystemId(xslTestIds[i][0] + ".xsl", xslTestIds[i][1],
436                                          xslTestIds[i][2], xslTestIds[i][3]);
437 
438             // Loop and attempt to do newTransformer() with each
439             //  as well, since they have slightly different codepaths
440             testNewTransformerWithSystemId(xslTestIds[i][0] + ".xsl", xslTestIds[i][1],
441                                            xslTestIds[i][2], xslTestIds[i][3]);
442 
443             // Loop and attempt to do a transform of an xml
444             //  document with each, using known-good stylesheet
445             testTransformWithSystemId(xslTestIds[i][0] + ".xml", xslTestIds[i][1],
446                                       xslTestIds[i][4], xslTestIds[i][5]);
447         }
448 
449         reporter.testCaseClose();
450         return true;
451     }
452 
453     /**
454      * Test setting various forms of systemIds to see what happens.
455      *
456      * @return false if we should abort the test; true otherwise
457      */
testCase2()458     public boolean testCase2()
459     {
460         reporter.testCaseInit("Test setting various forms of systemId to see what happens");
461 
462         // This will have imports/includes on various levels, and then
463         //  set the systemId of a Source to hopefully pull in the
464         //  different imports/includes
465         reporter.checkPass("//@todo implement this testcase");
466 
467         reporter.testCaseClose();
468         return true;
469     }
470 
471     /**
472      * Worker method to test factory.newTemplates.
473      * Performs validation of various types based on the
474      * expected value.
475      *
476      * @param sysId systemId of XSL to test with
477      * @param desc description of test, used in check() calls
478      * @param expected either one of the EXPECTED_RESULT_* flags
479      * defined in this file, or the start of a .toString of the
480      * exception that you expect will be thrown
481      * @param innerExpected the start of a .toString of the
482      * inner or wrapper exception that you expect will be thrown,
483      * presumably wrapped inside a TransformerException; if null,
484      * then this is not checked for
485      * @return Templates object created as side effect
486      */
testNewTemplatesWithSystemId(String sysId, String desc, String expected, String innerExpected)487     protected Templates testNewTemplatesWithSystemId(String sysId, String desc,
488                                                      String expected, String innerExpected)
489     {
490         TransformerFactory factory = null;
491         Templates templates = null;
492         Transformer transformer = null;
493         Throwable thrown = null;
494         try
495         {
496             factory = TransformerFactory.newInstance();
497             // Use a StreamSource with the systemId, which sets itself automatically
498             Source source = new StreamSource(sysId);
499             // Changed order of params for easier logging
500             reporter.logStatusMsg("newTemplates(" + desc + "): " + sysId + ", " + expected);
501             templates = factory.newTemplates(source);
502             reporter.logTraceMsg("newTemplates() no exceptions!");
503             // Just get the templates now for convenience,
504             //  this implicitly tests that they're non-null
505             transformer = templates.newTransformer();
506         }
507         catch (Throwable t)
508         {
509             thrown = t;
510             reporter.logStatusMsg("newTemplates(" + desc + ") threw:" + t.toString());
511             reporter.logThrowable(reporter.ERRORMSG, t, "newTemplates(" + desc + ") threw");
512         }
513         // Call worker method to perform actual validation
514         validateWithSystemId(sysId, desc, expected, innerExpected, thrown, transformer);
515         return templates;
516     }
517 
518     /**
519      * Worker method to test factory.newTransformer.
520      * Performs validation of various types based on the
521      * expected value.
522      *
523      * @param sysId systemId of XSL to test with
524      * @param desc description of test, used in check() calls
525      * @param expected either one of the EXPECTED_RESULT_* flags
526      * defined in this file, or the start of a .toString of the
527      * exception that you expect will be thrown
528      * @param innerExpected the start of a .toString of the
529      * inner or wrapper exception that you expect will be thrown,
530      * presumably wrapped inside a TransformerException; if null,
531      * then this is not checked for
532      * @return Transformer object created as side effect
533      */
testNewTransformerWithSystemId(String sysId, String desc, String expected, String innerExpected)534     protected Transformer testNewTransformerWithSystemId(String sysId, String desc,
535                                                          String expected, String innerExpected)
536     {
537         TransformerFactory factory = null;
538         Transformer transformer = null;
539         Throwable thrown = null;
540         try
541         {
542             factory = TransformerFactory.newInstance();
543             // Use a StreamSource with the systemId, which sets itself automatically
544             Source source = new StreamSource(sysId);
545             reporter.logStatusMsg("newTransformer(" + desc + "): " + sysId + ", " + expected);
546             transformer = factory.newTransformer(source);
547             reporter.logTraceMsg("newTransformer() no exceptions!");
548         }
549         catch (Throwable t)
550         {
551             thrown = t;
552             reporter.logStatusMsg("newTransformer(" + desc + ") threw:" + t.toString());
553             reporter.logThrowable(reporter.ERRORMSG, t, "newTransformer(" + desc + ") threw");
554         }
555         // Call worker method to perform actual validation
556         validateWithSystemId(sysId, desc, expected, innerExpected, thrown, transformer);
557         return transformer;
558     }
559 
560     /**
561      * Worker method to test transformer.transform().
562      * Performs validation of various types based on the
563      * expected value, using a known-good XSL file.
564      *
565      * @param sysId systemId of XML file to test with
566      * @param desc description of test, used in check() calls
567      * @param expected either one of the EXPECTED_RESULT_* flags
568      * defined in this file, or the start of a .toString of the
569      * exception that you expect will be thrown
570      * @param innerExpected the start of a .toString of the
571      * inner or wrapper exception that you expect will be thrown,
572      * presumably wrapped inside a TransformerException; if null,
573      * then this is not checked for
574      */
testTransformWithSystemId(String sysId, String desc, String expected, String innerExpected)575     protected void testTransformWithSystemId(String sysId, String desc,
576                                              String expected, String innerExpected)
577     {
578         TransformerFactory factory = null;
579         Transformer transformer = null;
580         Throwable thrown = null;
581         try
582         {
583             factory = TransformerFactory.newInstance();
584             // Use a StreamSource with the known-good systemId, which sets itself automatically
585             Source source = new StreamSource(knownGoodFileInfo.inputName);
586             reporter.logStatusMsg("Transform(" + desc + "): " + sysId + ", " + expected);
587             transformer = factory.newTransformer(source);
588 
589             // Always try the transform using a new StreamSource
590             reporter.logTraceMsg("About to transform(StreamSource(" + sysId + ", " + outNames.nextName() + ")");
591             transformer.transform(new StreamSource(sysId),
592                                   new StreamResult(outNames.currentName()));
593         }
594         catch (Throwable t)
595         {
596             thrown = t;
597             reporter.logStatusMsg("Transform(" + desc + ") threw:" + t.toString());
598             reporter.logThrowable(reporter.ERRORMSG, t, "Transform(" + desc + ") threw");
599         }
600 
601         // Do our own validation since we've already done a transform implicitly
602         // Sorry for the icky if..else.. statement
603         if (EXPECTED_RESULT_UNKNOWN.equals(expected))
604         {
605             // Just log a message: no validation done
606             reporter.logWarningMsg("(" + desc + ") not validated!");
607         }
608         else if (EXPECTED_RESULT_NONNULL.equals(expected))
609         {
610             // Just validate that our object is non-null
611             reporter.check((transformer != null), true, "(" + desc + ") is non-null");
612         }
613         else if (EXPECTED_RESULT_DOTRANSFORM.equals(expected))
614         {
615             int result = fileChecker.check(reporter,
616                               new File(outNames.currentName()),
617                               new File(knownGoodFileInfo.goldName),
618                               "(" + desc + ") transform into: " + outNames.currentName());
619             if (result == reporter.FAIL_RESULT)
620                 reporter.logInfoMsg("(" + desc + ") transform failure reason:" + fileChecker.getExtendedInfo());
621         }
622         else
623         {
624             // Otherwise, assume it's a string that any exception
625             //  thrown should match the start of our message
626             validateException(sysId, desc, expected, innerExpected, thrown);
627         }
628     }
629 
630 
631     /**
632      * Worker method to validate either a transform or an exception.
633      *
634      * @param sysId systemId that you were testing with; used in logging
635      * @param desc description of test, used in check() calls
636      * @param expected either one of the EXPECTED_RESULT_* flags
637      * defined in this file, or the start of a .toString of the
638      * exception that you expect will be thrown, only
639      * used when expected=EXPECTED_RESULT_DOTRANSFORM
640      * @param innerExpected the start of a .toString of the
641      * inner or wrapper exception that you expect will be thrown,
642      * presumably wrapped inside a TransformerException; if null,
643      * then this is not checked for
644      * @param thrown any Throwable that was thrown in your operation
645      * @param transformer that was created from your operation; only
646      * used for transform when expected=EXPECTED_RESULT_DOTRANSFORM
647      */
validateWithSystemId(String sysId, String desc, String expected, String innerExpected, Throwable thrown, Transformer transformer)648     protected void validateWithSystemId(String sysId, String desc,
649                                         String expected, String innerExpected,
650                                         Throwable thrown,
651                                         Transformer transformer)
652     {
653         // Sorry for the icky if..else.. statement
654         if (EXPECTED_RESULT_UNKNOWN.equals(expected))
655         {
656             // Just log a message: no validation done
657             reporter.logWarningMsg("(" + desc + ") not validated!");
658         }
659         else if (EXPECTED_RESULT_NONNULL.equals(expected))
660         {
661             // Just validate that our object is non-null
662             reporter.check((transformer != null), true, "(" + desc + ") is non-null");
663         }
664         else if (EXPECTED_RESULT_DOTRANSFORM.equals(expected))
665         {
666             try
667             {
668                 // First validate that our object is non-null
669                 if (transformer != null)
670                 {
671                     // Actually try to use the object in a transform
672                     transformer.transform(new StreamSource(QetestUtils.filenameToURL(knownGoodFileInfo.xmlName)),
673                                           new StreamResult(outNames.nextName()));
674                     int result = fileChecker.check(reporter,
675                                       new File(outNames.currentName()),
676                                       new File(knownGoodFileInfo.goldName),
677                                       "(" + desc + ") transform into: " + outNames.currentName());
678                     if (result == reporter.FAIL_RESULT)
679                         reporter.logInfoMsg("(" + desc + ") transform failure reason:" + fileChecker.getExtendedInfo());
680                 }
681                 else
682                 {
683                     reporter.checkFail("(" + desc + ") transformer was null!");
684                 }
685             }
686             catch (Throwable t)
687             {
688                 reporter.checkFail("(" + desc + ") do transform threw:" + t.toString());
689                 reporter.logThrowable(reporter.ERRORMSG, t, "(" + desc + ") do transform threw");
690             }
691         }
692         else
693         {
694             // Otherwise, assume it's a string that any exception
695             //  thrown should match the start of our message
696             validateException(sysId, desc, expected, innerExpected, thrown);
697         }
698     }
699 
700     /**
701      * Worker method to validate just a thrown exception.
702      *
703      * @param sysId systemId that you were testing with; currently unused?
704      * @param desc description of test, used in check() calls
705      * @param expected the start of a .toString of the
706      * exception that you expect will be thrown
707      * @param innerExpected the start of a .toString of the
708      * inner or wrapper exception that you expect will be thrown,
709      * presumably wrapped inside a TransformerException; if null,
710      * then this is not checked for
711      * @param thrown any Throwable that was thrown in your operation
712      */
validateException(String sysId, String desc, String expected, String innerExpected, Throwable thrown)713     protected void validateException(String sysId, String desc,
714                                      String expected, String innerExpected,
715                                      Throwable thrown)
716     {
717         if (thrown == null)
718         {
719             reporter.checkFail("(" + desc + ") No exception was thrown when expected");
720         }
721         else
722         {
723             reporter.check(thrown.toString().startsWith(expected), true, "(" + desc + ") expected exception");
724             if ((innerExpected != null)
725                 && (thrown instanceof TransformerException))
726             {
727                 // Also validate any innerExceptions
728                 Throwable inner = ((TransformerException)thrown).getException();
729                 if (inner != null)
730                 {
731                     reporter.check(inner.toString().startsWith(innerExpected), true, "(" + desc + ") inner expected exception");
732                 }
733                 else
734                 {
735                     // User specified an innerException but none
736                     //  was found, so fail
737                     reporter.checkFail("(" + desc + ") No innerException found like: " + innerExpected);
738                 }
739             }
740         }
741     }
742 
743     /**
744      * Convenience method to print out usage information - update if needed.
745      * @return String denoting usage of this test class
746      */
usage()747     public String usage()
748     {
749         return ("Common [optional] options supported by SystemIdTest:\n"
750                 + "(Note: assumes inputDir=.\\tests\\api)\n"
751                 + "(Note: test is directory-dependent!)\n"
752                 + super.usage());   // Grab our parent classes usage as well
753     }
754 
755 
756     /**
757      * Main method to run test from the command line - can be left alone.
758      * @param args command line argument array
759      */
main(String[] args)760     public static void main(String[] args)
761     {
762         SystemIdTest app = new SystemIdTest();
763         app.doMain(args);
764     }
765 }
766