• 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  * EmbeddedStylesheetTest.java
25  *
26  */
27 package org.apache.qetest.trax;
28 
29 import java.io.File;
30 import java.io.FileInputStream;
31 import java.util.Properties;
32 
33 import javax.xml.transform.Source;
34 import javax.xml.transform.Templates;
35 import javax.xml.transform.Transformer;
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 kinds of embedded stylesheets.
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  *
54  * @author shane_curcuru@lotus.com
55  * @version $Id$
56  */
57 public class EmbeddedStylesheetTest extends FileBasedTest
58 {
59 
60     /**
61      * Provides nextName(), currentName() functionality for tests
62      * that may produce any number of output files.
63      */
64     protected OutputNameManager outNames;
65 
66     /**
67      * Name of a valid, known-good xsl/xml file pair we can use.
68      */
69     protected XSLTestfileInfo testFileInfo = new XSLTestfileInfo();
70 
71     /** Embedded identity test file for getEmbedded....  */
72     protected XSLTestfileInfo embeddedFileInfo = new XSLTestfileInfo();
73 
74     /** Embedded fragment test file for getEmbedded....  */
75     protected XSLTestfileInfo embeddedFragmentFileInfo = new XSLTestfileInfo();
76 
77     /** Embedded types test file for getEmbedded.... text/xsl  */
78     protected String typeNameTextXsl = null;
79 
80     /** Embedded types test file for getEmbedded.... text/xml  */
81     protected String typeNameTextXml = null;
82 
83     /** Embedded types test file for getEmbedded.... application/xml+xslt  */
84     protected String typeNameApplicationXmlXslt = null;
85 
86     /** Embedded types gold file for all types  */
87     protected String typeGoldName = null;
88 
89     /** Embedded relative path test file for getEmbedded....  */
90     protected String embeddedRelativeXmlName = null;
91 
92     /** Gold embedded relative path test file for getEmbedded, at up level....  */
93     protected String relativeGoldFileLevel0 = null;
94     /** Gold embedded relative path test file for getEmbedded, at default level....  */
95     protected String relativeGoldFileLevel1 = null;
96     /** Gold embedded relative path test file for getEmbedded, at down level....  */
97     protected String relativeGoldFileLevel2 = null;
98 
99     /** SystemId identity test file for getEmbedded....  */
100     protected XSLTestfileInfo systemIdFileInfo = new XSLTestfileInfo();
101 
102     /** Subdirectory under test\tests\api for our xsl/xml files.  */
103     public static final String TRAX_SUBDIR = "trax";
104 
105     /** Convenience variable for user.dir - cached during test.  */
106     protected String savedUserDir = null;
107 
108     /** Just initialize test name, comment, numTestCases. */
EmbeddedStylesheetTest()109     public EmbeddedStylesheetTest()
110     {
111         numTestCases = 2;  // REPLACE_num
112         testName = "EmbeddedStylesheetTest";
113         testComment = "Test behavior of various kinds of embedded stylesheets";
114     }
115 
116 
117     /**
118      * Initialize this test - Set names of xml/xsl test files,
119      * cache user.dir property.
120      *
121      * @param p Properties to initialize from (unused)
122      * @return false if we should abort the test; true otherwise
123      */
doTestFileInit(Properties p)124     public boolean doTestFileInit(Properties p)
125     {
126         // Used for all tests; just dump files in trax subdir
127         File outSubDir = new File(outputDir + File.separator + TRAX_SUBDIR);
128         if (!outSubDir.mkdirs())
129             reporter.logWarningMsg("Could not create output dir: " + outSubDir);
130         // Initialize an output name manager to that dir with .out extension
131         outNames = new OutputNameManager(outputDir + File.separator + TRAX_SUBDIR
132                                          + File.separator + testName, ".out");
133 
134         String testBasePath = inputDir
135                               + File.separator
136                               + TRAX_SUBDIR
137                               + File.separator;
138         String goldBasePath = goldDir
139                               + File.separator
140                               + TRAX_SUBDIR
141                               + File.separator;
142 
143         // Just bare pathnames, not URI's
144         testFileInfo.inputName = testBasePath + testName + ".xsl";
145         testFileInfo.xmlName = testBasePath + testName + ".xml";
146         testFileInfo.goldName = goldBasePath + testName + ".out";
147 
148         embeddedFileInfo.xmlName = testBasePath + "embeddedIdentity.xml";
149         embeddedFileInfo.goldName = goldBasePath + "embeddedIdentity.out";
150 
151         typeNameTextXsl = testBasePath + "EmbeddedType-text-xsl.xml";
152         typeNameTextXml = testBasePath + "EmbeddedType-text-xml.xml";
153         typeNameApplicationXmlXslt = testBasePath + "EmbeddedType-application-xml-xslt.xml";
154         typeGoldName = goldBasePath + "EmbeddedType.out";
155 
156         embeddedFragmentFileInfo.xmlName = testBasePath + "EmbeddedFragment.xml";
157         embeddedFragmentFileInfo.goldName = goldBasePath + "EmbeddedFragment.out";
158 
159         embeddedRelativeXmlName = testBasePath + "EmbeddedRelative.xml";
160         relativeGoldFileLevel0 = goldBasePath + "EmbeddedRelative0.out";
161         relativeGoldFileLevel1 = goldBasePath + "EmbeddedRelative1.out";
162         relativeGoldFileLevel2 = goldBasePath + "EmbeddedRelative2.out";
163 
164         systemIdFileInfo.xmlName = testBasePath + "SystemIdTest.xml";
165         systemIdFileInfo.goldName = goldBasePath + "SystemIdTest.out";
166 
167         // Cache user.dir property
168         savedUserDir = System.getProperty("user.dir");
169         reporter.logHashtable(Logger.STATUSMSG, System.getProperties(), "System.getProperties()");
170         reporter.logHashtable(Logger.STATUSMSG, testProps, "testProps");
171 
172         return true;
173     }
174 
175 
176     /**
177      * Cleanup this test - uncache user.dir property.
178      *
179      * @param p Properties to initialize from (if needed)
180      * @return false if we should abort the test; true otherwise
181      */
doTestFileClose(Properties p)182     public boolean doTestFileClose(Properties p)
183     {
184         // Uncache user.dir property
185         System.getProperties().put("user.dir", savedUserDir);
186         return true;
187     }
188 
189 
190     /**
191      * Simple xml documents with xml-stylesheet PI's.
192      *
193      * @return false if we should abort the test; true otherwise
194      */
testCase1()195     public boolean testCase1()
196     {
197         reporter.testCaseInit("Simple xml documents with xml-stylesheet PI's");
198 
199         TransformerFactory factory = null;
200         try
201         {
202             factory = TransformerFactory.newInstance();
203         }
204         catch (Throwable t)
205         {
206             reporter.checkFail("Problem creating factory; can't continue testcase");
207             reporter.logThrowable(reporter.ERRORMSG, t,
208                                   "Problem creating factory; can't continue testcase");
209             return true;
210         }
211         String media= null;     // often ignored
212         String title = null;    // often ignored
213         String charset = null;  // often ignored
214         try
215         {
216             // Verify you can process a simple embedded stylesheet
217             //  (also tested in TransformerFactoryAPITest)
218             Source stylesheet = factory.getAssociatedStylesheet(new StreamSource(QetestUtils.filenameToURL(embeddedFileInfo.xmlName)),
219                                                                 media, title, charset);
220             reporter.logTraceMsg("got AssociatedStylesheet");
221             Templates embedTemplates = factory.newTemplates(stylesheet);
222             Transformer embedTransformer = embedTemplates.newTransformer();
223             reporter.logTraceMsg("Got embedded templates, about to transform.");
224             embedTransformer.transform(new StreamSource(QetestUtils.filenameToURL(embeddedFileInfo.xmlName)),
225                                        new StreamResult(outNames.nextName()));
226 
227             int result = fileChecker.check(reporter,
228                                            new File(outNames.currentName()),
229                                            new File(embeddedFileInfo.goldName),
230                                           "(1)embedded transform into " + outNames.currentName());
231             if (result == Logger.FAIL_RESULT)
232                 reporter.logInfoMsg("(1)embedded transform failure reason:" + fileChecker.getExtendedInfo());
233 
234             // Verify the stylesheet you get from an embedded source
235             //  can be reused for other documents
236             embedTransformer = embedTemplates.newTransformer();
237             embedTransformer.transform(new StreamSource(QetestUtils.filenameToURL(embeddedFileInfo.xmlName)),
238                                        new StreamResult(outNames.nextName()));
239 
240             result = fileChecker.check(reporter,
241                                            new File(outNames.currentName()),
242                                            new File(embeddedFileInfo.goldName),
243                                           "(1a)embedded transform into " + outNames.currentName());
244             if (result == Logger.FAIL_RESULT)
245                 reporter.logInfoMsg("(1a)embedded transform failure reason:" + fileChecker.getExtendedInfo());
246 
247             // Verify the transformer itself can be reused
248             //  on a *different* document
249             embedTransformer.transform(new StreamSource(QetestUtils.filenameToURL(systemIdFileInfo.xmlName)),
250                                        new StreamResult(outNames.nextName()));
251 
252             result = fileChecker.check(reporter,
253                                            new File(outNames.currentName()),
254                                            new File(systemIdFileInfo.goldName),
255                                           "(2)embedded transform into " + outNames.currentName());
256             if (result == Logger.FAIL_RESULT)
257                 reporter.logInfoMsg("(2)embedded transform failure reason:" + fileChecker.getExtendedInfo());
258         }
259         catch (Throwable t)
260         {
261             reporter.checkFail("Problem with simple embedded reuse");
262             reporter.logThrowable(reporter.ERRORMSG, t, "Problem with simple embedded reuse");
263         }
264 
265         try
266         {
267             // Verify you can process an embedded stylesheet as fragment
268             testEmbeddedTransform(new StreamSource(QetestUtils.filenameToURL(embeddedFragmentFileInfo.xmlName)),
269                                   new StreamSource(QetestUtils.filenameToURL(embeddedFragmentFileInfo.xmlName)),
270                                   "(10)embedded fragment transform",
271                                   embeddedFragmentFileInfo.goldName);
272 
273             // Verify you can process an embedded stylesheet that
274             //  comes from a relative path - default systemId
275             testEmbeddedTransform(new StreamSource(QetestUtils.filenameToURL(embeddedRelativeXmlName)),
276                                   new StreamSource(QetestUtils.filenameToURL(embeddedRelativeXmlName)),
277                                   "(11)embedded relative transform",
278                                   relativeGoldFileLevel1);
279 
280             // ...Verify relative paths, explicit systemId up one level0
281             // sysId for level0 up one: inputDir + File.separator + "EmbeddedRelative.xml"
282             Source relativeXmlSrc = new StreamSource(new FileInputStream(embeddedRelativeXmlName));
283             relativeXmlSrc.setSystemId(QetestUtils.filenameToURL(inputDir + File.separator + "EmbeddedRelative.xml"));
284             Source relativeTransformSrc = new StreamSource(new FileInputStream(embeddedRelativeXmlName));
285             relativeTransformSrc.setSystemId(QetestUtils.filenameToURL(inputDir + File.separator + "EmbeddedRelative.xml"));
286             testEmbeddedTransform(relativeXmlSrc,
287                                   relativeTransformSrc,
288                                   "(12a)embedded relative, explicit sysId up level0",
289                                   relativeGoldFileLevel0);
290             // ...Verify relative paths, explicit systemId same level1
291             relativeXmlSrc = new StreamSource(new FileInputStream(embeddedRelativeXmlName));
292             relativeXmlSrc.setSystemId(QetestUtils.filenameToURL(embeddedRelativeXmlName));
293             relativeTransformSrc = new StreamSource(new FileInputStream(embeddedRelativeXmlName));
294             relativeTransformSrc.setSystemId(QetestUtils.filenameToURL(embeddedRelativeXmlName));
295             testEmbeddedTransform(relativeXmlSrc,
296                                   relativeTransformSrc,
297                                   "(12b)embedded relative, explicit sysId same level1",
298                                   relativeGoldFileLevel1);
299 
300             // ...Verify relative paths, explicit systemId down one level2
301             // sysId for level2 down one: inputDir + "/trax/systemid/" + "EmbeddedRelative.xml"
302             relativeXmlSrc = new StreamSource(new FileInputStream(embeddedRelativeXmlName));
303             relativeXmlSrc.setSystemId(QetestUtils.filenameToURL(inputDir + "/trax/systemid/" + "EmbeddedRelative.xml"));
304             relativeTransformSrc = new StreamSource(new FileInputStream(embeddedRelativeXmlName));
305             relativeTransformSrc.setSystemId(QetestUtils.filenameToURL(inputDir + "/trax/systemid/" + "EmbeddedRelative.xml"));
306             testEmbeddedTransform(relativeXmlSrc,
307                                   relativeTransformSrc,
308                                   "(12c)embedded relative, explicit sysId down level2",
309                                   relativeGoldFileLevel2);
310 
311             // Verify you can process various types of embedded stylesheets
312             // This also verifies that a type of 'not/found' is skipped
313             // Xalan-specific: text/xsl
314             testEmbeddedTransform(new StreamSource(QetestUtils.filenameToURL(typeNameTextXsl)),
315                                   new StreamSource(QetestUtils.filenameToURL(typeNameTextXsl)),
316                                   "(20a)xml:stylesheet type=text/xsl",
317                                   typeGoldName);
318 
319             // Proposed standard: text/xml
320             testEmbeddedTransform(new StreamSource(QetestUtils.filenameToURL(typeNameTextXml)),
321                                   new StreamSource(QetestUtils.filenameToURL(typeNameTextXml)),
322                                   "(20b)xml:stylesheet type=text/xml",
323                                   typeGoldName);
324 
325             // Proposed standard: application/xml+xslt
326             testEmbeddedTransform(new StreamSource(QetestUtils.filenameToURL(typeNameApplicationXmlXslt)),
327                                   new StreamSource(QetestUtils.filenameToURL(typeNameApplicationXmlXslt)),
328                                   "(20b)xml:stylesheet type=application/xml+xslt",
329                                   typeGoldName);
330 
331         }
332         catch (Throwable t)
333         {
334             reporter.checkFail("Problem with other embedded");
335             reporter.logThrowable(reporter.ERRORMSG, t, "Problem with other embedded");
336         }
337 
338         reporter.testCaseClose();
339         return true;
340     }
341 
342     /**
343      * Test media, title, charset types of xml-stylesheet PI's.
344      *
345      * @return false if we should abort the test; true otherwise
346      */
testCase2()347     public boolean testCase2()
348     {
349         reporter.testCaseInit("Test media, title, charset types of xml-stylesheet PI's");
350 
351         TransformerFactory factory = null;
352         try
353         {
354             factory = TransformerFactory.newInstance();
355         }
356         catch (Throwable t)
357         {
358             reporter.checkFail("Problem creating factory; can't continue testcase");
359             reporter.logThrowable(reporter.ERRORMSG, t,
360                                   "Problem creating factory; can't continue testcase");
361             return true;
362         }
363         String mediaTitleName = inputDir + File.separator
364                                 + TRAX_SUBDIR + File.separator
365                                 + "EmbeddedMediaTitle.xml";
366         try
367         {
368             String media= null;
369             String title = null;
370             String charset = null;
371             media = "foo/media";
372             reporter.logTraceMsg("About to getAssociatedStylesheet(" + QetestUtils.filenameToURL(mediaTitleName)
373                                  + ", media=" + media + ")");
374             Source xslSrc = factory.getAssociatedStylesheet(new StreamSource(QetestUtils.filenameToURL(mediaTitleName)),
375                                                                 media, title, charset);
376             Transformer transformer = factory.newTransformer(xslSrc);
377             reporter.logTraceMsg("Got embedded templates, media=" + media + " , about to transform.");
378             transformer.transform(new StreamSource(QetestUtils.filenameToURL(mediaTitleName)),
379                                        new StreamResult(outNames.nextName()));
380 
381             if (Logger.PASS_RESULT
382                 != fileChecker.check(reporter,
383                                      new File(outNames.currentName()),
384                                      new File(relativeGoldFileLevel1),
385                                      "(20)embedded media=" + media + " transform into " + outNames.currentName())
386                )
387             {
388                 reporter.logInfoMsg("(20)embedded media=" + media + " failure reason:" + fileChecker.getExtendedInfo());
389             }
390 
391 
392             media = "bar/media";
393             reporter.logTraceMsg("About to getAssociatedStylesheet(" + QetestUtils.filenameToURL(mediaTitleName)
394                                  + ", media=" + media + ")");
395             xslSrc = factory.getAssociatedStylesheet(new StreamSource(QetestUtils.filenameToURL(mediaTitleName)),
396                                                                 media, title, charset);
397             transformer = factory.newTransformer(xslSrc);
398             reporter.logTraceMsg("Got embedded templates, media=" + media + " , about to transform.");
399             transformer.transform(new StreamSource(QetestUtils.filenameToURL(mediaTitleName)),
400                                        new StreamResult(outNames.nextName()));
401 
402             if (Logger.PASS_RESULT
403                 != fileChecker.check(reporter,
404                                      new File(outNames.currentName()),
405                                      new File(relativeGoldFileLevel0),
406                                      "(20a)embedded media=" + media + " transform into " + outNames.currentName())
407                )
408             {
409                 reporter.logInfoMsg("(20a)embedded media=" + media + " failure reason:" + fileChecker.getExtendedInfo());
410             }
411         }
412         catch (Throwable t)
413         {
414             reporter.checkFail("Problem with testcase(media)");
415             reporter.logThrowable(reporter.ERRORMSG, t, "Problem with testcase(media)");
416         }
417 
418         try
419         {
420             String media= null;
421             String title = null;
422             String charset = null;
423             title = "foo-title";
424             reporter.logTraceMsg("About to getAssociatedStylesheet(" + QetestUtils.filenameToURL(mediaTitleName)
425                                  + ", title=" + title + ")");
426             Source xslSrc = factory.getAssociatedStylesheet(new StreamSource(QetestUtils.filenameToURL(mediaTitleName)),
427                                                                 media, title, charset);
428             Transformer transformer = factory.newTransformer(xslSrc);
429             reporter.logTraceMsg("Got embedded templates, title=" + title + " , about to transform.");
430             transformer.transform(new StreamSource(QetestUtils.filenameToURL(mediaTitleName)),
431                                        new StreamResult(outNames.nextName()));
432 
433             if (Logger.PASS_RESULT
434                 != fileChecker.check(reporter,
435                                      new File(outNames.currentName()),
436                                      new File(relativeGoldFileLevel1),
437                                      "(21)embedded title=" + title + " transform into " + outNames.currentName())
438                )
439             {
440                 reporter.logInfoMsg("(21)embedded title=" + title + " failure reason:" + fileChecker.getExtendedInfo());
441             }
442 
443 
444             title = "bar-title";
445             reporter.logTraceMsg("About to getAssociatedStylesheet(" + QetestUtils.filenameToURL(mediaTitleName)
446                                  + ", title=" + title + ")");
447             xslSrc = factory.getAssociatedStylesheet(new StreamSource(QetestUtils.filenameToURL(mediaTitleName)),
448                                                                 media, title, charset);
449             transformer = factory.newTransformer(xslSrc);
450             reporter.logTraceMsg("Got embedded templates, title=" + title + " , about to transform.");
451             transformer.transform(new StreamSource(QetestUtils.filenameToURL(mediaTitleName)),
452                                        new StreamResult(outNames.nextName()));
453 
454             if (Logger.PASS_RESULT
455                 != fileChecker.check(reporter,
456                                      new File(outNames.currentName()),
457                                      new File(relativeGoldFileLevel0),
458                                      "(21a)embedded title=" + title + " transform into " + outNames.currentName())
459                )
460             {
461                 reporter.logInfoMsg("(21a)embedded title=" + title + " failure reason:" + fileChecker.getExtendedInfo());
462             }
463         }
464         catch (Throwable t)
465         {
466             reporter.checkFail("Problem with testcase(title)");
467             reporter.logThrowable(reporter.ERRORMSG, t, "Problem with testcase(title)");
468         }
469 
470         try
471         {
472             String media= null;
473             String title = null;
474             String charset = null;
475             media = "alt/media"; // Should use alternate, I think
476             reporter.logTraceMsg("About to getAssociatedStylesheet(" + QetestUtils.filenameToURL(mediaTitleName)
477                                  + ", media=" + media + ")");
478             Source xslSrc = factory.getAssociatedStylesheet(new StreamSource(QetestUtils.filenameToURL(mediaTitleName)),
479                                                                 media, title, charset);
480             Transformer transformer = factory.newTransformer(xslSrc);
481             reporter.logTraceMsg("Got embedded templates, media=" + media + " , about to transform.");
482             transformer.transform(new StreamSource(QetestUtils.filenameToURL(mediaTitleName)),
483                                        new StreamResult(outNames.nextName()));
484 
485             if (Logger.PASS_RESULT
486                 != fileChecker.check(reporter,
487                                      new File(outNames.currentName()),
488                                      new File(relativeGoldFileLevel2),
489                                      "(22)embedded media=" + media + " transform into " + outNames.currentName())
490                )
491             {
492                 reporter.logInfoMsg("(22)embedded media=" + media + " failure reason:" + fileChecker.getExtendedInfo());
493             }
494         }
495         catch (Throwable t)
496         {
497             reporter.checkFail("Problem with testcase(alternate)");
498             reporter.logThrowable(reporter.ERRORMSG, t, "Problem with testcase(alternate)");
499         }
500         try
501         {
502             String media= null;
503             String title = null;
504             String charset = null;
505             title = "title-not-found"; // negative test: there is no title like this
506             reporter.logTraceMsg("About to getAssociatedStylesheet(" + QetestUtils.filenameToURL(mediaTitleName)
507                                  + ", title=" + title + ")");
508             Source xslSrc = factory.getAssociatedStylesheet(new StreamSource(QetestUtils.filenameToURL(mediaTitleName)),
509                                                                 media, title, charset);
510             if (null == xslSrc)
511             {
512                 reporter.checkPass("getAssociatedStylesheet returns null for not found title");
513             }
514             else
515             {
516                 reporter.checkFail("getAssociatedStylesheet returns null for not found title");
517                 reporter.logErrorMsg("xslSrc is: " + xslSrc);
518             }
519             title = null;
520             media = "media/notfound"; // negative test: there is no media like this
521             reporter.logTraceMsg("About to getAssociatedStylesheet(" + QetestUtils.filenameToURL(mediaTitleName)
522                                  + ", media=" + media + ")");
523             xslSrc = factory.getAssociatedStylesheet(new StreamSource(QetestUtils.filenameToURL(mediaTitleName)),
524                                                                 media, title, charset);
525             if (null == xslSrc)
526             {
527                 reporter.checkPass("getAssociatedStylesheet returns null for not found media");
528             }
529             else
530             {
531                 reporter.checkFail("getAssociatedStylesheet returns null for not found media");
532                 reporter.logErrorMsg("xslSrc is: " + xslSrc);
533             }
534 
535             title = "alt-title";        // This title is in there, but
536             media = "media/notfound"; // negative test: there is no media like this
537             reporter.logTraceMsg("About to getAssociatedStylesheet(" + QetestUtils.filenameToURL(mediaTitleName)
538                                  + ", media=" + media + ")"
539                                  + ", title=" + title + ")");
540             xslSrc = factory.getAssociatedStylesheet(new StreamSource(QetestUtils.filenameToURL(mediaTitleName)),
541                                                                 media, title, charset);
542             if (null == xslSrc)
543             {
544                 reporter.checkPass("getAssociatedStylesheet returns null bad media, good title");
545             }
546             else
547             {
548                 reporter.checkFail("getAssociatedStylesheet returns null bad media, good title");
549                 reporter.logErrorMsg("xslSrc is: " + xslSrc);
550             }
551 
552             title = "title-not-found"; // No title like this, but
553             media = "alt/media"; // there is a media like this
554             reporter.logTraceMsg("About to getAssociatedStylesheet(" + QetestUtils.filenameToURL(mediaTitleName)
555                                  + ", media=" + media + ")"
556                                  + ", title=" + title + ")");
557             xslSrc = factory.getAssociatedStylesheet(new StreamSource(QetestUtils.filenameToURL(mediaTitleName)),
558                                                                 media, title, charset);
559             if (null == xslSrc)
560             {
561                 reporter.checkPass("getAssociatedStylesheet returns null bad title, good media");
562             }
563             else
564             {
565                 reporter.checkFail("getAssociatedStylesheet returns null bad title, good media");
566                 reporter.logErrorMsg("xslSrc is: " + xslSrc);
567             }
568         }
569         catch (Throwable t)
570         {
571             reporter.checkFail("Problem with testcase(negative)");
572             reporter.logThrowable(reporter.ERRORMSG, t, "Problem with testcase(negative)");
573         }
574 
575         reporter.logTraceMsg("//@todo testing with charset");
576         reporter.testCaseClose();
577         return true;
578     }
579 
580 
581     /**
582      * Worker method to test transforming embedded stylesheets.
583      * Calls getAssociatedStylesheet on the xml source, and then
584      * uses the resulting stylesheet to transform the other source.
585      * Two sources used since StreamSources may not be re-useable.
586      * Calls fileChecker.check to validate.
587      *
588      * @param xmlSrc Source of XML file to use to get stylesheet from
589      * @param transformSrc Source of XML file to transform
590      * @param desc description of test, used in check() calls
591      * @param goldName path\filename of gold file
592      */
testEmbeddedTransform(Source xmlSrc, Source transformSrc, String desc, String goldName)593     protected void testEmbeddedTransform(Source xmlSrc, Source transformSrc,
594                                          String desc, String goldName)
595     {
596         TransformerFactory factory = null;
597         String media= null;     // often ignored
598         String title = null;    // often ignored
599         String charset = null;  // often ignored
600         try
601         {
602             factory = TransformerFactory.newInstance();
603             Source stylesheet = factory.getAssociatedStylesheet(xmlSrc,
604                                                                 media, title, charset);
605             Templates embedTemplates = factory.newTemplates(stylesheet);
606             Transformer embedTransformer = embedTemplates.newTransformer();
607 
608             reporter.logTraceMsg("Got embedded(" + xmlSrc.getSystemId()
609                                  + "), about to transform(" + transformSrc.getSystemId() + ").");
610             embedTransformer.transform(transformSrc,
611                                        new StreamResult(outNames.nextName()));
612 
613             if (Logger.PASS_RESULT
614                 != fileChecker.check(reporter,
615                                      new File(outNames.currentName()),
616                                      new File(goldName),
617                                      desc + " into " + outNames.currentName())
618                )
619             {
620                 reporter.logInfoMsg(desc + " failure reason:" + fileChecker.getExtendedInfo());
621             }
622         }
623         catch (Throwable t)
624         {
625             // We only expect transforms that work
626             reporter.checkFail("Transform(" + desc + ") threw:" + t.toString());
627             reporter.logThrowable(reporter.ERRORMSG, t, "Transform(" + desc + ") threw");
628         }
629     }
630 
631     /**
632      * Convenience method to print out usage information - update if needed.
633      * @return String denoting usage of this test class
634      */
usage()635     public String usage()
636     {
637         return ("Common [optional] options supported by EmbeddedStylesheetTest:\n"
638                 + "(Note: assumes inputDir=tests\\api)\n"
639                 + "(Note: test is directory-dependent!)\n"
640                 + super.usage());   // Grab our parent classes usage as well
641     }
642 
643 
644     /**
645      * Main method to run test from the command line - can be left alone.
646      * @param args command line argument array
647      */
main(String[] args)648     public static void main(String[] args)
649     {
650         EmbeddedStylesheetTest app = new EmbeddedStylesheetTest();
651         app.doMain(args);
652     }
653 }
654