• 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  * SystemIdImpInclTest.java
25  *
26  */
27 package org.apache.qetest.trax;
28 
29 import java.io.File;
30 import java.io.FileInputStream;
31 import java.io.FileOutputStream;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.io.InputStreamReader;
35 import java.net.URL;
36 import java.net.URLConnection;
37 import java.util.Properties;
38 
39 import javax.xml.parsers.DocumentBuilder;
40 import javax.xml.parsers.DocumentBuilderFactory;
41 import javax.xml.parsers.SAXParser;
42 import javax.xml.parsers.SAXParserFactory;
43 import javax.xml.transform.Result;
44 import javax.xml.transform.Source;
45 import javax.xml.transform.Templates;
46 import javax.xml.transform.Transformer;
47 import javax.xml.transform.TransformerFactory;
48 import javax.xml.transform.dom.DOMSource;
49 import javax.xml.transform.sax.SAXSource;
50 import javax.xml.transform.sax.SAXTransformerFactory;
51 import javax.xml.transform.stream.StreamResult;
52 import javax.xml.transform.stream.StreamSource;
53 
54 import org.apache.qetest.FileBasedTest;
55 import org.apache.qetest.Logger;
56 import org.apache.qetest.OutputNameManager;
57 import org.apache.qetest.QetestUtils;
58 import org.apache.qetest.xsl.XSLTestfileInfo;
59 import org.w3c.dom.Node;
60 import org.xml.sax.InputSource;
61 import org.xml.sax.XMLReader;
62 
63 //-------------------------------------------------------------------------
64 
65 /**
66  * Test behavior of imports/includes with various setSystemId sources.
67  * <b>Note:</b> This test is directory-dependent, so if there are
68  * any fails, check the code to see what the test file is expecting
69  * the path/directory/etc. to be.
70  * //@todo More variations on kinds of systemIds: file: id's that
71  * are absolute, etc.; any/all other forms of id's like http:
72  * (which will require network resources available).
73  *
74  * @author shane_curcuru@lotus.com
75  * @version $Id$
76  */
77 public class SystemIdImpInclTest extends FileBasedTest
78 {
79 
80     /**
81      * Provides nextName(), currentName() functionality for tests
82      * that may produce any number of output files.
83      */
84     protected OutputNameManager outNames;
85 
86     /**
87      * Name of a valid, known-good xsl/xml file pair we can use.
88      */
89     protected XSLTestfileInfo testFileInfo = new XSLTestfileInfo();
90 
91     /**
92      * Just basename of a valid, known-good file, both .xsl/.xml .
93      */
94     protected String knownGoodBaseName = null;
95 
96     /** Gold filename for level0, i.e. one directory above the testfile.  */
97     protected String goldFileLevel0 = "SystemIdImpInclLevel0.out";
98 
99     /** Gold filename for level1, i.e. the directory of the testfile.  */
100     protected String goldFileLevel1 = "SystemIdImpInclLevel1.out";
101 
102     /** Gold filename for level2, i.e. a directory below the testfile.  */
103     protected String goldFileLevel2 = "SystemIdImpInclLevel2.out";
104 
105     /** Gold filename for http, i.e. a from a webserver.  */
106     protected String goldFileHttp = "SystemIdImpInclHttp.out";
107 
108     /** Subdirectory under test\tests\api for our xsl/xml files.  */
109     public static final String TRAX_SUBDIR = "trax";
110 
111     /** Convenience variable for user.dir - cached during test.  */
112     protected String savedUserDir = null;
113 
114     /** Just initialize test name, comment, numTestCases. */
SystemIdImpInclTest()115     public SystemIdImpInclTest()
116     {
117         numTestCases = 3; // Set numTestCases to 3 to skip the 4th one that does http:
118         testName = "SystemIdImpInclTest";
119         testComment = "Test behavior of imports/includes with various setSystemId sources";
120     }
121 
122 
123     /**
124      * Initialize this test - Set names of xml/xsl test files,
125      * cache user.dir property.
126      *
127      * @param p Properties to initialize from (unused)
128      * @return false if we should abort the test; true otherwise
129      */
doTestFileInit(Properties p)130     public boolean doTestFileInit(Properties p)
131     {
132         // Used for all tests; just dump files in trax subdir
133         File outSubDir = new File(outputDir + File.separator + TRAX_SUBDIR);
134         if (!outSubDir.mkdirs())
135             reporter.logWarningMsg("Could not create output dir: " + outSubDir);
136         // Initialize an output name manager to that dir with .out extension
137         outNames = new OutputNameManager(outputDir + File.separator + TRAX_SUBDIR
138                                          + File.separator + testName, ".out");
139 
140         String testBasePath = inputDir
141                               + File.separator
142                               + TRAX_SUBDIR
143                               + File.separator;
144         String goldBasePath = goldDir
145                               + File.separator
146                               + TRAX_SUBDIR
147                               + File.separator;
148 
149         // Just bare pathnames, not URI's
150         knownGoodBaseName = "SystemIdImpIncl";
151         testFileInfo.inputName = testBasePath + knownGoodBaseName + ".xsl";
152         testFileInfo.xmlName = testBasePath + knownGoodBaseName + ".xml";
153         testFileInfo.goldName = goldBasePath + knownGoodBaseName + ".out";
154         goldFileLevel0 = goldBasePath + goldFileLevel0; // just prepend path
155         goldFileLevel1 = goldBasePath + goldFileLevel1; // just prepend path
156         goldFileLevel2 = goldBasePath + goldFileLevel2; // just prepend path
157         goldFileHttp = goldBasePath + goldFileHttp; // just prepend path
158 
159         // Cache user.dir property
160         savedUserDir = System.getProperty("user.dir");
161         reporter.logHashtable(Logger.STATUSMSG, System.getProperties(), "System.getProperties()");
162         reporter.logHashtable(Logger.STATUSMSG, testProps, "testProps");
163 
164         return true;
165     }
166 
167 
168     /**
169      * Cleanup this test - uncache user.dir property.
170      *
171      * @param p Properties to initialize from (if needed)
172      * @return false if we should abort the test; true otherwise
173      */
doTestFileClose(Properties p)174     public boolean doTestFileClose(Properties p)
175     {
176         // Uncache user.dir property
177         System.getProperties().put("user.dir", savedUserDir);
178         return true;
179     }
180 
181 
182     /**
183      * Simple StreamSources with different setSystemIds.
184      *
185      * @return false if we should abort the test; true otherwise
186      */
testCase1()187     public boolean testCase1()
188     {
189         reporter.testCaseInit("Simple StreamSources with different setSystemIds");
190 
191         TransformerFactory factory = null;
192         try
193         {
194             factory = TransformerFactory.newInstance();
195         }
196         catch (Throwable t)
197         {
198             reporter.checkFail("Problem creating factory; can't continue testcase");
199             reporter.logThrowable(reporter.ERRORMSG, t,
200                                   "Problem creating factory; can't continue testcase");
201             return true;
202         }
203         try
204         {
205             // Verify we can do basic transforms with readers/streams,
206             //  with the 'normal' systemId
207             reporter.logInfoMsg("StreamSource.setSystemId(level1)");
208             InputStream xslStream1 = new FileInputStream(testFileInfo.inputName);
209             Source xslSource1 = new StreamSource(xslStream1);
210             xslSource1.setSystemId(QetestUtils.filenameToURL(testFileInfo.inputName));
211 
212             InputStream xmlStream1 = new FileInputStream(testFileInfo.xmlName);
213             Source xmlSource1 = new StreamSource(xmlStream1);
214             xmlSource1.setSystemId(QetestUtils.filenameToURL(testFileInfo.xmlName));
215 
216             reporter.logTraceMsg("Create FileOutputStream to " + outNames.nextName());
217             FileOutputStream fos1 = new FileOutputStream(outNames.currentName());
218             Result result1 = new StreamResult(fos1);
219 
220             Templates templates1 = factory.newTemplates(xslSource1);
221             Transformer transformer1 = templates1.newTransformer();
222             reporter.logInfoMsg("About to transform, systemId(level1)");
223             transformer1.transform(xmlSource1, result1);
224             fos1.close(); // must close ostreams we own
225             if (Logger.PASS_RESULT
226                 != fileChecker.check(reporter,
227                               new File(outNames.currentName()),
228                               new File(goldFileLevel1),
229                               "transform after setSystemId(level1) into " + outNames.currentName())
230                )
231                 reporter.logInfoMsg("transform after setSystemId(level1)... failure reason:" + fileChecker.getExtendedInfo());
232         }
233         catch (Throwable t)
234         {
235             reporter.checkFail("Problem with setSystemId(level1)");
236             reporter.logThrowable(reporter.ERRORMSG, t, "Problem with after setSystemId(level1)");
237         }
238 
239         try
240         {
241             // Verify we can do basic transforms with readers/streams,
242             //  systemId set up one level
243             reporter.logInfoMsg("StreamSource.setSystemId(level0)");
244             InputStream xslStream1 = new FileInputStream(testFileInfo.inputName);
245             Source xslSource1 = new StreamSource(xslStream1);
246             xslSource1.setSystemId(QetestUtils.filenameToURL(inputDir + File.separator + knownGoodBaseName + ".xsl"));
247 
248             InputStream xmlStream1 = new FileInputStream(testFileInfo.xmlName);
249             Source xmlSource1 = new StreamSource(xmlStream1);
250             xmlSource1.setSystemId(QetestUtils.filenameToURL(inputDir + File.separator + knownGoodBaseName + ".xml"));
251 
252             reporter.logTraceMsg("Create FileOutputStream to " + outNames.nextName());
253             FileOutputStream fos1 = new FileOutputStream(outNames.currentName());
254             Result result1 = new StreamResult(fos1);
255 
256             Templates templates1 = factory.newTemplates(xslSource1);
257             Transformer transformer1 = templates1.newTransformer();
258             reporter.logInfoMsg("About to transform, systemId(level0)");
259             transformer1.transform(xmlSource1, result1);
260             fos1.close(); // must close ostreams we own
261             if (Logger.PASS_RESULT
262                 != fileChecker.check(reporter,
263                               new File(outNames.currentName()),
264                               new File(goldFileLevel0),
265                               "transform after setSystemId(level0) into " + outNames.currentName())
266                )
267                 reporter.logInfoMsg("transform after setSystemId(level0)... failure reason:" + fileChecker.getExtendedInfo());
268         }
269         catch (Throwable t)
270         {
271             reporter.checkFail("Problem with setSystemId(level0)");
272             reporter.logThrowable(reporter.ERRORMSG, t, "Problem with setSystemId(level0)");
273         }
274 
275         try
276         {
277             // Verify we can do basic transforms with readers/streams,
278             //  with the systemId down one level
279             reporter.logInfoMsg("StreamSource.setSystemId(level2)");
280             InputStream xslStream1 = new FileInputStream(testFileInfo.inputName);
281             Source xslSource1 = new StreamSource(xslStream1);
282             xslSource1.setSystemId(QetestUtils.filenameToURL(inputDir + "/trax/systemid/" + knownGoodBaseName + ".xsl"));
283 
284             InputStream xmlStream1 = new FileInputStream(testFileInfo.xmlName);
285             Source xmlSource1 = new StreamSource(xmlStream1);
286             xmlSource1.setSystemId(QetestUtils.filenameToURL(inputDir + "/trax/systemid/" + knownGoodBaseName + ".xml"));
287 
288             reporter.logTraceMsg("Create FileOutputStream to " + outNames.nextName());
289             FileOutputStream fos1 = new FileOutputStream(outNames.currentName());
290             Result result1 = new StreamResult(fos1);
291 
292             Templates templates1 = factory.newTemplates(xslSource1);
293             Transformer transformer1 = templates1.newTransformer();
294             reporter.logInfoMsg("About to transform, systemId(level2)");
295             transformer1.transform(xmlSource1, result1);
296             fos1.close(); // must close ostreams we own
297             if (Logger.PASS_RESULT
298                 != fileChecker.check(reporter,
299                               new File(outNames.currentName()),
300                               new File(goldFileLevel2),
301                               "transform after setSystemId(level2) into " + outNames.currentName())
302                )
303                 reporter.logInfoMsg("transform after setSystemId(level2)... failure reason:" + fileChecker.getExtendedInfo());
304         }
305         catch (Throwable t)
306         {
307             reporter.checkFail("Problem with setSystemId(level2)");
308             reporter.logThrowable(reporter.ERRORMSG, t, "Problem with setSystemId(level2)");
309         }
310 
311         try
312         {
313             // Verify we can do basic transforms with readers/streams,
314             //  with the systemId down one level
315             reporter.logInfoMsg("StreamSource.setSystemId(xslonly level2)");
316             InputStream xslStream1 = new FileInputStream(testFileInfo.inputName);
317             Source xslSource1 = new StreamSource(xslStream1);
318             xslSource1.setSystemId(QetestUtils.filenameToURL(inputDir + "/trax/systemid/" + knownGoodBaseName + ".xsl"));
319 
320             InputStream xmlStream1 = new FileInputStream(testFileInfo.xmlName);
321             Source xmlSource1 = new StreamSource(xmlStream1);
322             // Explicitly don't set the xmlId - shouldn't be needed
323 
324             reporter.logTraceMsg("Create FileOutputStream to " + outNames.nextName());
325             FileOutputStream fos1 = new FileOutputStream(outNames.currentName());
326             Result result1 = new StreamResult(fos1);
327 
328             Templates templates1 = factory.newTemplates(xslSource1);
329             Transformer transformer1 = templates1.newTransformer();
330             reporter.logInfoMsg("About to transform, systemId(xslonly level2)");
331             transformer1.transform(xmlSource1, result1);
332             fos1.close(); // must close ostreams we own
333             if (Logger.PASS_RESULT
334                 != fileChecker.check(reporter,
335                               new File(outNames.currentName()),
336                               new File(goldFileLevel2),
337                               "transform after setSystemId(xslonly level2) into " + outNames.currentName())
338                )
339                 reporter.logInfoMsg("transform after setSystemId(xslonly level2)... failure reason:" + fileChecker.getExtendedInfo());
340         }
341         catch (Throwable t)
342         {
343             reporter.checkFail("Problem with setSystemId(xslonly level2)");
344             reporter.logThrowable(reporter.ERRORMSG, t, "Problem with setSystemId(xslonly level2)");
345         }
346         reporter.testCaseClose();
347         return true;
348     }
349 
350     /**
351      * Verify simple SAXSources with systemIds.
352      *
353      * @return false if we should abort the test; true otherwise
354      */
testCase2()355     public boolean testCase2()
356     {
357         reporter.testCaseInit("Verify simple SAXSources with systemIds");
358 
359         TransformerFactory factory = null;
360         SAXTransformerFactory saxFactory = null;
361         InputSource xslInpSrc = null;
362         Source xslSource = null;
363         Source xmlSource = null;
364         try
365         {
366             factory = TransformerFactory.newInstance();
367             saxFactory = (SAXTransformerFactory)factory;
368         }
369         catch (Throwable t)
370         {
371             reporter.checkFail("Problem creating factory; can't continue testcase");
372             reporter.logThrowable(reporter.ERRORMSG, t,
373                                   "Problem creating factory; can't continue testcase");
374             return true;
375         }
376         try
377         {
378             // Verify basic transforms with with various systemId
379             //  and a SAXSource(InputSource(String))
380             // level0: one level up
381 /********************************
382 // SAXSource(impSrc(str)) level0, level2
383 // Note: these cases may not be valid to test, since the setSystemId
384 //  call will effectively overwrite the underlying InputSource's
385 //  real systemId, and thus the stylesheet can't be built
386 //  Answer: write a custom test case that parses the stylesheet
387 //  and builds it, but doesn't get imports/includes until later
388 //  when we have setSystemId
389             xslInpSrc = new InputSource(new InputStreamReader(new FileInputStream(testFileInfo.inputName), "UTF-8")); //@DEM
390 //            xslInpSrc = new InputSource(new FileReader(testFileInfo.inputName));  @DEM
391             xslSource = new SAXSource(xslInpSrc);
392 
393             xmlSource = new StreamSource(new FileInputStream(testFileInfo.xmlName));
394             xmlSource.setSystemId(QetestUtils.filenameToURL(testFileInfo.xmlName));
395 
396             checkSourceWithSystemId(xslSource, QetestUtils.filenameToURL(inputDir + File.separator + knownGoodBaseName + ".xsl"),
397                                     xmlSource, goldFileLevel0,
398                                     "SAXSource(inpSrc(str)).systemId(level0: one up)");
399 ********************************/
400             // level1: same systemId as actual file
401 			// @DEM changes are to allow test to run on various
402 			//	platforms regardeless of encodings; force it to
403 			//	be UTF-8 since that's what's checked in
404             xslInpSrc = new InputSource(new InputStreamReader(new FileInputStream(testFileInfo.inputName), "UTF-8")); //@DEM
405 //            xslInpSrc = new InputSource(new FileReader(testFileInfo.inputName));  @DEM
406             xslSource = new SAXSource(xslInpSrc);
407 
408             xmlSource = new StreamSource(new FileInputStream(testFileInfo.xmlName));
409             xmlSource.setSystemId(QetestUtils.filenameToURL(testFileInfo.xmlName));
410 
411             checkSourceWithSystemId(xslSource, QetestUtils.filenameToURL(testFileInfo.inputName),
412                                     xmlSource, goldFileLevel1,
413                                     "SAXSource(inpSrc(str)).systemId(level1: same level)");
414 
415             // level2: one level down
416 /********************************
417 // SAXSource(impSrc(str)) level0, level2
418 // Note: these cases may not be valid to test, since the setSystemId
419 //  call will effectively overwrite the underlying InputSource's
420 //  real systemId, and thus the stylesheet can't be built
421 //  Answer: write a custom test case that parses the stylesheet
422 //  and builds it, but doesn't get imports/includes until later
423 //  when we have setSystemId
424             xslInpSrc = new InputSource(new InputStreamReader(new FileInputStream(testFileInfo.inputName), "UTF-8")); //@DEM
425 //            xslInpSrc = new InputSource(new FileReader(testFileInfo.inputName));  @DEM
426             xslSource = new SAXSource(xslInpSrc);
427 
428             xmlSource = new StreamSource(new FileInputStream(testFileInfo.xmlName));
429             xmlSource.setSystemId(QetestUtils.filenameToURL(testFileInfo.xmlName));
430 
431             checkSourceWithSystemId(xslSource, QetestUtils.filenameToURL(inputDir + "/trax/systemid/" + knownGoodBaseName + ".xsl"),
432                                     xmlSource, goldFileLevel2,
433                                     "SAXSource(inpSrc(str)).systemId(level2: one down)");
434 ********************************/
435             reporter.logTraceMsg("@todo: add test for SAXSource with reset systemId (see code comments)");
436         }
437         catch (Throwable t)
438         {
439             reporter.checkFail("Problem with SAXSources(1)");
440             reporter.logThrowable(reporter.ERRORMSG, t, "Problem with SAXSources(1)");
441         }
442         try
443         {
444             // Verify basic transforms with with various systemId
445             //  and a SAXSource(InputSource(InputStream))
446             // level0: one level up
447             xslInpSrc = new InputSource(new FileInputStream(testFileInfo.inputName));
448             xslSource = new SAXSource(xslInpSrc);
449 
450             xmlSource = new StreamSource(new FileInputStream(testFileInfo.xmlName));
451             xmlSource.setSystemId(QetestUtils.filenameToURL(testFileInfo.xmlName));
452 
453             checkSourceWithSystemId(xslSource, QetestUtils.filenameToURL(inputDir + File.separator + knownGoodBaseName + ".xsl"),
454                                     xmlSource, goldFileLevel0,
455                                     "SAXSource(inpSrc(byteS)).systemId(level0: one up)");
456 
457             // level1: same systemId as actual file
458             xslInpSrc = new InputSource(new FileInputStream(testFileInfo.inputName));
459             xslSource = new SAXSource(xslInpSrc);
460 
461             xmlSource = new StreamSource(new FileInputStream(testFileInfo.xmlName));
462             xmlSource.setSystemId(QetestUtils.filenameToURL(testFileInfo.xmlName));
463 
464             checkSourceWithSystemId(xslSource, QetestUtils.filenameToURL(testFileInfo.inputName),
465                                     xmlSource, goldFileLevel1,
466                                     "SAXSource(inpSrc(byteS)).systemId(level1: same level)");
467 
468             // level2: one level down
469             xslInpSrc = new InputSource(new FileInputStream(testFileInfo.inputName));
470             xslSource = new SAXSource(xslInpSrc);
471 
472             xmlSource = new StreamSource(new FileInputStream(testFileInfo.xmlName));
473             xmlSource.setSystemId(QetestUtils.filenameToURL(testFileInfo.xmlName));
474 
475             checkSourceWithSystemId(xslSource, QetestUtils.filenameToURL(inputDir + "/trax/systemid/" + knownGoodBaseName + ".xsl"),
476                                     xmlSource, goldFileLevel2,
477                                     "SAXSource(inpSrc(byteS)).systemId(level2: one down)");
478 
479             // Verify basic transforms with with various systemId
480             //  and a SAXSource(InputSource(Reader))
481             // level0: one level up
482             xslInpSrc = new InputSource(new InputStreamReader(new FileInputStream(testFileInfo.inputName), "UTF-8")); //@DEM
483 //            xslInpSrc = new InputSource(new FileReader(testFileInfo.inputName));  @DEM
484             xslSource = new SAXSource(xslInpSrc);
485 
486             xmlSource = new StreamSource(new FileInputStream(testFileInfo.xmlName));
487             xmlSource.setSystemId(QetestUtils.filenameToURL(testFileInfo.xmlName));
488 
489             checkSourceWithSystemId(xslSource, QetestUtils.filenameToURL(inputDir + File.separator + knownGoodBaseName + ".xsl"),
490                                     xmlSource, goldFileLevel0,
491                                     "SAXSource(inpSrc(charS)).systemId(level0: one up)");
492 
493             // level1: same systemId as actual file
494             xslInpSrc = new InputSource(new InputStreamReader(new FileInputStream(testFileInfo.inputName), "UTF-8")); //@DEM
495 //            xslInpSrc = new InputSource(new FileReader(testFileInfo.inputName));  @DEM
496             xslSource = new SAXSource(xslInpSrc);
497 
498             xmlSource = new StreamSource(new FileInputStream(testFileInfo.xmlName));
499             xmlSource.setSystemId(QetestUtils.filenameToURL(testFileInfo.xmlName));
500 
501             checkSourceWithSystemId(xslSource, QetestUtils.filenameToURL(testFileInfo.inputName),
502                                     xmlSource, goldFileLevel1,
503                                     "SAXSource(inpSrc(charS)).systemId(level1: same level)");
504 
505             // level2: one level down
506             xslInpSrc = new InputSource(new InputStreamReader(new FileInputStream(testFileInfo.inputName), "UTF-8")); //@DEM
507 //            xslInpSrc = new InputSource(new FileReader(testFileInfo.inputName));  @DEM
508             xslSource = new SAXSource(xslInpSrc);
509 
510             xmlSource = new StreamSource(new FileInputStream(testFileInfo.xmlName));
511             xmlSource.setSystemId(QetestUtils.filenameToURL(testFileInfo.xmlName));
512 
513             checkSourceWithSystemId(xslSource, QetestUtils.filenameToURL(inputDir + "/trax/systemid/" + knownGoodBaseName + ".xsl"),
514                                     xmlSource, goldFileLevel2,
515                                     "SAXSource(inpSrc(charS)).systemId(level2: one down)");
516 
517             // Verify basic transforms with with various systemId
518             //  and a SAXSource(XMLReader, InputSource(various))
519             // Be sure to use the JAXP methods only!
520             SAXParserFactory spfactory = SAXParserFactory.newInstance();
521             spfactory.setNamespaceAware(true);
522             SAXParser saxParser = spfactory.newSAXParser();
523     	    XMLReader reader = saxParser.getXMLReader();
524             // level0: one level up, with a character stream
525             xslInpSrc = new InputSource(new InputStreamReader(new FileInputStream(testFileInfo.inputName), "UTF-8")); //@DEM
526 //            xslInpSrc = new InputSource(new FileReader(testFileInfo.inputName));  @DEM
527             xslSource = new SAXSource(reader, xslInpSrc);
528 
529             xmlSource = new StreamSource(new FileInputStream(testFileInfo.xmlName));
530             xmlSource.setSystemId(QetestUtils.filenameToURL(testFileInfo.xmlName));
531 
532             checkSourceWithSystemId(xslSource, QetestUtils.filenameToURL(inputDir + File.separator + knownGoodBaseName + ".xsl"),
533                                     xmlSource, goldFileLevel0,
534                                     "SAXSource(reader, inpSrc(charS)).systemId(level0: one up)");
535 
536             // level1: same systemId as actual file, with a systemId
537             saxParser = spfactory.newSAXParser();
538     	    reader = saxParser.getXMLReader();
539             xslInpSrc = new InputSource(testFileInfo.inputName);
540             xslSource = new SAXSource(reader, xslInpSrc);
541 
542             xmlSource = new StreamSource(new FileInputStream(testFileInfo.xmlName));
543             xmlSource.setSystemId(QetestUtils.filenameToURL(testFileInfo.xmlName));
544 
545             checkSourceWithSystemId(xslSource, QetestUtils.filenameToURL(testFileInfo.inputName),
546                                     xmlSource, goldFileLevel1,
547                                     "SAXSource(reader, inpSrc(str)).systemId(level1: same level)");
548 
549             // level2: one level down, with a byte stream
550             saxParser = spfactory.newSAXParser();
551     	    reader = saxParser.getXMLReader();
552             xslInpSrc = new InputSource(new FileInputStream(testFileInfo.inputName));
553             xslSource = new SAXSource(reader, xslInpSrc);
554 
555             xmlSource = new StreamSource(new FileInputStream(testFileInfo.xmlName));
556             xmlSource.setSystemId(QetestUtils.filenameToURL(testFileInfo.xmlName));
557 
558             checkSourceWithSystemId(xslSource, QetestUtils.filenameToURL(inputDir + "/trax/systemid/" + knownGoodBaseName + ".xsl"),
559                                     xmlSource, goldFileLevel2,
560                                     "SAXSource(reader, inpSrc(byteS)).systemId(level2: one down)");
561         }
562         catch (Throwable t)
563         {
564             reporter.checkFail("Problem with SAXSources(2)");
565             reporter.logThrowable(reporter.ERRORMSG, t, "Problem with SAXSources(2)");
566         }
567 
568         reporter.testCaseClose();
569         return true;
570     }
571 
572     /**
573      * Verify simple DOMSources with systemIds.
574      *
575      * @return false if we should abort the test; true otherwise
576      */
testCase3()577     public boolean testCase3()
578     {
579         reporter.testCaseInit("Verify simple DOMSources with systemIds");
580         TransformerFactory factory = null;
581         DocumentBuilderFactory dfactory = null;
582         DocumentBuilder docBuilder = null;
583         Node xslNode = null;
584         Source xslSource = null;
585         Source xmlSource = null;
586 
587         try
588         {
589             factory = TransformerFactory.newInstance();
590             dfactory = DocumentBuilderFactory.newInstance();
591             dfactory.setNamespaceAware(true);
592         }
593         catch (Throwable t)
594         {
595             reporter.checkFail("Problem creating factory; can't continue testcase");
596             reporter.logThrowable(reporter.ERRORMSG, t,
597                                   "Problem creating factory; can't continue testcase");
598             return true;
599         }
600         try
601         {
602             docBuilder = dfactory.newDocumentBuilder();
603 
604             // Verify basic transforms with with various systemId
605             //  and a DOMSource(InputSource(String))
606             // level0: one level up
607             reporter.logTraceMsg("about to parse(InputSource(" + QetestUtils.filenameToURL(testFileInfo.inputName) + "))");
608             xslNode = docBuilder.parse(new InputSource(QetestUtils.filenameToURL(testFileInfo.inputName)));
609             xslSource = new DOMSource(xslNode);
610 
611             xmlSource = new StreamSource(new FileInputStream(testFileInfo.xmlName));
612             xmlSource.setSystemId(QetestUtils.filenameToURL(testFileInfo.xmlName));
613 
614             checkSourceWithSystemId(xslSource, QetestUtils.filenameToURL(inputDir + File.separator + knownGoodBaseName + ".xsl"),
615                                     xmlSource, goldFileLevel0,
616                                     "DOMSource(inpSrc(str)).systemId(level0: one up)");
617 
618             // level1: same systemId as actual file
619             reporter.logTraceMsg("about to parse(InputSource(" + QetestUtils.filenameToURL(testFileInfo.inputName) + "))");
620             xslNode = docBuilder.parse(new InputSource(QetestUtils.filenameToURL(testFileInfo.inputName)));
621             xslSource = new DOMSource(xslNode);
622 
623             xmlSource = new StreamSource(new FileInputStream(testFileInfo.xmlName));
624             xmlSource.setSystemId(QetestUtils.filenameToURL(testFileInfo.xmlName));
625 
626             checkSourceWithSystemId(xslSource, QetestUtils.filenameToURL(testFileInfo.inputName),
627                                     xmlSource, goldFileLevel1,
628                                     "DOMSource(inpSrc(str)).systemId(level1: same level)");
629 
630             // level2: one level down
631             reporter.logTraceMsg("about to parse(InputSource(" + QetestUtils.filenameToURL(testFileInfo.inputName) + "))");
632             xslNode = docBuilder.parse(new InputSource(QetestUtils.filenameToURL(testFileInfo.inputName)));
633             xslSource = new DOMSource(xslNode);
634 
635             xmlSource = new StreamSource(new FileInputStream(testFileInfo.xmlName));
636             xmlSource.setSystemId(QetestUtils.filenameToURL(testFileInfo.xmlName));
637 
638             checkSourceWithSystemId(xslSource, QetestUtils.filenameToURL(inputDir + "/trax/systemid/" + knownGoodBaseName + ".xsl"),
639                                     xmlSource, goldFileLevel2,
640                                     "DOMSource(inpSrc(str)).systemId(level2: one down)");
641 
642             // Sample extra test: DOMSource that had systemId set
643             //  differently in the constructor - tests that you can
644             //  later call setSystemId and have it work
645             // level0: one level up
646             reporter.logTraceMsg("about to parse(InputSource(" + QetestUtils.filenameToURL(testFileInfo.inputName) + "))");
647             xslNode = docBuilder.parse(new InputSource(QetestUtils.filenameToURL(testFileInfo.inputName)));
648             // Set the original systemId to itself, or level1
649             xslSource = new DOMSource(xslNode, QetestUtils.filenameToURL(testFileInfo.inputName));
650 
651             xmlSource = new StreamSource(new FileInputStream(testFileInfo.xmlName));
652             xmlSource.setSystemId(QetestUtils.filenameToURL(testFileInfo.xmlName));
653 
654             // Test it with a level0, or one level up systemId
655             checkSourceWithSystemId(xslSource, QetestUtils.filenameToURL(inputDir + File.separator + knownGoodBaseName + ".xsl"),
656                                     xmlSource, goldFileLevel0,
657                                     "DOMSource(inpSrc(str),sysId-level1).systemId(level0: one up)");
658         }
659         catch (Throwable t)
660         {
661             reporter.checkFail("Problem with DOMSources(1)");
662             reporter.logThrowable(reporter.ERRORMSG, t, "Problem with DOMSources(1)");
663         }
664 
665 
666 
667         reporter.testCaseClose();
668         return true;
669     }
670 
671 
672     /**
673      * Verify various simple Sources with http: systemIds.
674      * Test may be commented out until we have a better way to
675      * maintain and check for existence and correct content of
676      * stylesheets on the http server.
677      *
678      * @return false if we should abort the test; true otherwise
679      */
testCase4()680     public boolean testCase4()
681     {
682         reporter.testCaseInit("Verify various simple Sources with http: systemIds");
683 
684         // This is the name of a directory on the apache server
685         //  that has impincl\SystemIdInclude.xsl and
686         //  impincl\SystemIdImport.xsl files on it - obviously,
687         //  your JVM must have access to this server to successfully
688         //  run this portion of the test!
689         String httpSystemIdBase = "http://xml.apache.org/xalan-j/test";
690 
691         // Verify http connectivity
692         //  If your JVM environment can't connect to the http:
693         //  server, then we can't complete the test
694         // This could happen due to various network problems,
695         //  not being connected, firewalls, etc.
696         try
697         {
698             reporter.logInfoMsg("verifing http connectivity before continuing testCase");
699             // Note hard-coded path to one of the two files we'll be relying on
700             URL testURL = new URL(httpSystemIdBase + "/impincl/SystemIdInclude.xsl");
701             URLConnection urlConnection = testURL.openConnection();
702             // Ensure we don't get a cached copy
703             urlConnection.setUseCaches(false);
704             // Ensure we don't get asked interactive questions
705             urlConnection.setAllowUserInteraction(false);
706             // Actually connect to the document; will throw
707             //  IOException if anything goes wrong
708             urlConnection.connect();
709             // Convenience: log out when the doc was last modified
710             reporter.logInfoMsg(testURL.toString() + " last modified: "
711                                   + urlConnection.getLastModified());
712             int contentLen = urlConnection.getContentLength();
713             reporter.logStatusMsg("URL.getContentLength() was: " + contentLen);
714             if (contentLen < 1)
715             {
716                 // if no content, throw 'fake' exception to
717                 //  short-circut test case
718                 throw new IOException("URL.getContentLength() was: " + contentLen);
719             }
720             // Also verify that the file there contains (some of) the data we expect!
721             reporter.logTraceMsg("calling urlConnection.getContent()...");
722             Object content = urlConnection.getContent();
723             if (null == content)
724             {
725                 // if no content, throw 'fake' exception to
726                 //  short-circut test case
727                 throw new IOException("URL.getContent() was null!");
728             }
729             reporter.logTraceMsg("getContent().toString() is now: " + content.toString());
730 
731             //@todo we should also verify some key strings in the
732             //  expected .xsl file here, if possible
733         }
734         catch (IOException ioe)
735         {
736             reporter.logThrowable(Logger.ERRORMSG, ioe, "Can't connect threw");
737             reporter.logErrorMsg("Can't connect to: " + httpSystemIdBase
738                                  + "/impincl/SystemIdInclude.xsl, skipping testcase");
739             reporter.checkPass("FAKE PASS RECORD; testCase was skipped");
740             // Skip the rest of the testcase
741             reporter.testCaseClose();
742             return true;
743         }
744 
745 
746         TransformerFactory factory = null;
747         Source xslSource = null;
748         Source xmlSource = null;
749 
750         try
751         {
752             factory = TransformerFactory.newInstance();
753         }
754         catch (Throwable t)
755         {
756             reporter.checkFail("Problem creating factory; can't continue testcase");
757             reporter.logThrowable(reporter.ERRORMSG, t,
758                                   "Problem creating factory; can't continue testcase");
759             return true;
760         }
761         try
762         {
763             // Verify StreamSource from local disk with a
764             //  http: systemId for imports/includes
765             xslSource = new StreamSource(new FileInputStream(testFileInfo.inputName));
766 
767             xmlSource = new StreamSource(new FileInputStream(testFileInfo.xmlName));
768             xmlSource.setSystemId(QetestUtils.filenameToURL(testFileInfo.xmlName));
769 
770             // Note that the systemId set (the second argument below)
771             //  must be the path to the proper 'directory' level
772             //  on the webserver: setting it to just ".../test"
773             //  will fail, since it be considered a file of that
774             //  name, not the directory
775             checkSourceWithSystemId(xslSource, httpSystemIdBase + "/",
776                                     xmlSource, goldFileHttp,
777                                     "StreamSource().systemId(http:)");
778 
779             xslSource = new StreamSource(new FileInputStream(testFileInfo.inputName));
780 
781             xmlSource = new StreamSource(new FileInputStream(testFileInfo.xmlName));
782             xmlSource.setSystemId(QetestUtils.filenameToURL(testFileInfo.xmlName));
783 
784             checkSourceWithSystemId(xslSource, httpSystemIdBase + "/" + knownGoodBaseName + ".xsl",
785                                     xmlSource, goldFileHttp,
786                                     "StreamSource().systemId(http:)");
787         }
788         catch (Throwable t)
789         {
790             reporter.checkFail("Problem with http systemIds(1)");
791             reporter.logThrowable(reporter.ERRORMSG, t, "Problem with http systemIds(1)");
792         }
793 
794         reporter.testCaseClose();
795         return true;
796     }
797 
798 
799     /**
800      * Worker method to test setting SystemId and doing transform.
801      * Simply does:<pre>
802      * xslSrc.setSystemId(systemId);
803      * templates = factory.newTemplates(xslSrc);
804      * transformer = templates.newTransformer();
805      * transformer.transform(xmlSrc, StreamResult(...));
806      * fileChecker.check(... goldFileName, desc)
807      * </pre>
808      * Also catches any exceptions and logs them as fails.
809      *
810      * @param xslSrc Source to use for stylesheet
811      * @param systemId systemId to set on the stylesheet
812      * @param xmlSrc Source to use for XML input data
813      * @param goldFileName name of expected file to compare with
814      * @param desc description of this test
815      */
checkSourceWithSystemId(Source xslSrc, String systemId, Source xmlSrc, String goldFileName, String desc)816     public void checkSourceWithSystemId(Source xslSrc, String systemId,
817                                         Source xmlSrc, String goldFileName,
818                                         String desc)
819     {
820         reporter.logTraceMsg(desc + " (" + systemId + ")");
821         try
822         {
823             TransformerFactory factory = TransformerFactory.newInstance();
824             xslSrc.setSystemId(systemId);
825 
826             // Use the next available output name for result
827             FileOutputStream fos = new FileOutputStream(outNames.nextName());
828             Result outputResult = new StreamResult(fos);
829 
830             Templates templates = factory.newTemplates(xslSrc);
831             Transformer transformer = templates.newTransformer();
832             transformer.transform(xmlSrc, outputResult);
833             fos.close(); // must close ostreams we own
834             if (Logger.PASS_RESULT
835                 != fileChecker.check(reporter,
836                               new File(outNames.currentName()),
837                               new File(goldFileName),
838                               desc + " (" + systemId + ") into: " + outNames.currentName())
839                )
840                 reporter.logInfoMsg(desc + "... failure reason:" + fileChecker.getExtendedInfo());
841         }
842         catch (Throwable t)
843         {
844             reporter.checkFail(desc + " threw: " + t.toString());
845             reporter.logThrowable(reporter.ERRORMSG, t, desc + " threw");
846         }
847     }
848 
849 
850     /**
851      * Convenience method to print out usage information - update if needed.
852      * @return String denoting usage of this test class
853      */
usage()854     public String usage()
855     {
856         return ("Common [optional] options supported by SystemIdImpInclTest:\n"
857                 + "(Note: assumes inputDir=.\\tests\\api)\n"
858                 + "(Note: test is directory-dependent!)\n"
859                 + super.usage());   // Grab our parent classes usage as well
860     }
861 
862 
863     /**
864      * Main method to run test from the command line - can be left alone.
865      * @param args command line argument array
866      */
main(String[] args)867     public static void main(String[] args)
868     {
869         SystemIdImpInclTest app = new SystemIdImpInclTest();
870         app.doMain(args);
871     }
872 }
873