• 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  * SerializedStylesheetTest.java
25  *
26  */
27 package org.apache.qetest.xalanj2;
28 
29 import android.platform.test.annotations.FlakyTest;
30 import java.io.File;
31 import java.io.FileInputStream;
32 import java.io.FileOutputStream;
33 import java.io.ObjectInputStream;
34 import java.io.ObjectOutputStream;
35 import java.util.Properties;
36 
37 import javax.xml.transform.Templates;
38 import javax.xml.transform.Transformer;
39 import javax.xml.transform.TransformerFactory;
40 import javax.xml.transform.stream.StreamResult;
41 import javax.xml.transform.stream.StreamSource;
42 
43 import org.apache.qetest.FileBasedTest;
44 import org.apache.qetest.Logger;
45 import org.apache.qetest.OutputNameManager;
46 import org.apache.qetest.QetestUtils;
47 import org.apache.qetest.xsl.XSLTestfileInfo;
48 import org.junit.Test;
49 
50 //-------------------------------------------------------------------------
51 
52 /**
53  * Basic functional test of serialized Templates objects.
54  * @author shane_curcuru@lotus.com
55  * @version $Id$
56  */
57 public class SerializedStylesheetTest extends FileBasedTest
58 {
59 
60     /** Provides nextName(), currentName() functionality.  */
61     protected OutputNameManager outNames;
62 
63     /** Simple identity.xml/xsl file pair.  */
64     protected XSLTestfileInfo testFileInfo = new XSLTestfileInfo();
65 
66     /** Complex minitest.xml/xsl file pair.  */
67     protected XSLTestfileInfo minitestFileInfo = new XSLTestfileInfo();
68 
69     /** Subdirectory under test\tests\api for our xsl/xml files.  */
70     public static final String TRAX_SUBDIR = File.separator + "trax" + File.separator;
71 
72     /** Just initialize test name, comment, numTestCases. */
SerializedStylesheetTest()73     public SerializedStylesheetTest()
74     {
75         numTestCases = 1;  // REPLACE_num
76         testName = "SerializedStylesheetTest";
77         testComment = "Basic functional test of serialized Templates objects";
78     }
79 
80 
81     /**
82      * Initialize this test - Set names of xml/xsl test files.
83      *
84      * @param p Properties to initialize from (if needed)
85      * @return false if we should abort the test; true otherwise
86      */
doTestFileInit(Properties p)87     public boolean doTestFileInit(Properties p)
88     {
89         // Used for all tests; just dump files in trax subdir
90         File outSubDir = new File(outputDir + TRAX_SUBDIR);
91         if (!outSubDir.mkdirs())
92             reporter.logWarningMsg("Could not create output dir: " + outSubDir);
93         // Initialize an output name manager to that dir with .out extension
94         outNames = new OutputNameManager(outputDir + TRAX_SUBDIR
95                                          + testName, ".out");
96 
97         testFileInfo.inputName = QetestUtils.filenameToURL(inputDir
98                               + TRAX_SUBDIR + "identity.xsl");
99         testFileInfo.xmlName = QetestUtils.filenameToURL(inputDir
100                               + TRAX_SUBDIR + "identity.xml");
101         testFileInfo.goldName = goldDir + TRAX_SUBDIR + "identity.out";
102 
103         minitestFileInfo.inputName = QetestUtils.filenameToURL(inputDir
104                               + File.separator + "Minitest.xsl");
105         minitestFileInfo.xmlName = QetestUtils.filenameToURL(inputDir
106                               + File.separator + "Minitest.xml");
107         minitestFileInfo.goldName = goldDir + File.separator + "Minitest-xalanj2.out";
108 
109         return true;
110     }
111 
112 
113     /**
114      * Basic functional test of serialized Templates objects.
115      * Reproduce Bugzilla 2005 by rob.stanley@geac.com
116      * http://nagoya.apache.org/bugzilla/show_bug.cgi?id=2005
117      *
118      * @return false if we should abort the test; true otherwise
119      */
testCase1()120     public boolean testCase1()
121     {
122         reporter.testCaseInit("Basic functional test of serialized Templates objects");
123 
124         try
125         {
126             TransformerFactory factory = TransformerFactory.newInstance();
127             // Create templates from normal stylesheet
128             Templates origTemplates = factory.newTemplates(new StreamSource(testFileInfo.inputName));
129 
130             // Serialize the Templates to disk
131             reporter.logInfoMsg("About to serialize " + testFileInfo.inputName + " templates to: " + outNames.nextName());
132             FileOutputStream ostream = new FileOutputStream(outNames.currentName());
133             ObjectOutputStream oos = new java.io.ObjectOutputStream(ostream);
134             oos.writeObject(origTemplates);
135             oos.flush();
136             ostream.close();
137 
138             // Read the Templates back in
139             reporter.logInfoMsg("About to read templates back");
140             FileInputStream istream = new FileInputStream(outNames.currentName());
141             ObjectInputStream ois = new ObjectInputStream(istream);
142             Templates templates = (Templates)ois.readObject();
143             istream.close();
144 
145             // Use the Templates in a transform
146             reporter.logInfoMsg("About to call newTransformer");
147             Transformer transformer = templates.newTransformer();
148             reporter.logInfoMsg("About to transform(xmlDoc, StreamResult(" + outNames.nextName() + "))");
149             transformer.transform(new StreamSource(testFileInfo.xmlName), new StreamResult(outNames.currentName()));
150             fileChecker.check(reporter,
151                               new File(outNames.currentName()),
152                               new File(testFileInfo.goldName),
153                               "Using serialized Templates, transform into " + outNames.currentName());
154         }
155         catch (Throwable t)
156         {
157             reporter.checkFail("Problem with serialized Template");
158             reporter.logThrowable(reporter.ERRORMSG, t, "Problem with serialized Template");
159         }
160 
161         try
162         {
163             reporter.logTraceMsg("Try again, using the Minitest (with imports/includes/etc.)");
164             TransformerFactory factory = TransformerFactory.newInstance();
165             // Create templates from normal stylesheet
166             Templates origTemplates = factory.newTemplates(new StreamSource(minitestFileInfo.inputName));
167 
168             // Serialize the Templates to disk
169             reporter.logInfoMsg("About to serialize " + minitestFileInfo.inputName + " templates to: " + outNames.nextName());
170             FileOutputStream ostream = new FileOutputStream(outNames.currentName());
171             ObjectOutputStream oos = new java.io.ObjectOutputStream(ostream);
172             oos.writeObject(origTemplates);
173             oos.flush();
174             ostream.close();
175 
176             // Read the Templates back in
177             reporter.logInfoMsg("About to read templates back");
178             FileInputStream istream = new FileInputStream(outNames.currentName());
179             ObjectInputStream ois = new ObjectInputStream(istream);
180             Templates templates = (Templates)ois.readObject();
181             istream.close();
182 
183             // Use the Templates in a transform
184             reporter.logInfoMsg("About to call newTransformer");
185             Transformer transformer = templates.newTransformer();
186             reporter.logInfoMsg("About to transform(xmlDoc, StreamResult(" + outNames.nextName() + "))");
187             transformer.transform(new StreamSource(minitestFileInfo.xmlName), new StreamResult(outNames.currentName()));
188             if (Logger.PASS_RESULT !=
189                 fileChecker.check(reporter,
190                         new File(outNames.currentName()),
191                         new File(minitestFileInfo.goldName),
192                         "Using serialized Templates, transform into " + outNames.currentName())
193                )
194                 reporter.logStatusMsg("Using serialized Templates: failure reason:" + fileChecker.getExtendedInfo());
195         }
196         catch (Throwable t)
197         {
198             reporter.checkFail("Problem with serialized Minitest Template");
199             reporter.logThrowable(reporter.ERRORMSG, t, "Problem with serialized Minitest Template");
200         }
201         reporter.testCaseClose();
202         return true;
203     }
204 
205 
206     /**
207      * Convenience method to print out usage information - update if needed.
208      * @return String denoting usage of this test class
209      */
usage()210     public String usage()
211     {
212         return ("Common [optional] options supported by SerializedStylesheetTest:\n"
213                 + "(Note: assumes inputDir=.\\tests\\api)\n"
214                 + super.usage());   // Grab our parent classes usage as well
215     }
216 
217 
218     /**
219      * Main method to run test from the command line - can be left alone.
220      * @param args command line argument array
221      */
main(String[] args)222     public static void main(String[] args)
223     {
224         SerializedStylesheetTest app = new SerializedStylesheetTest();
225         app.doMain(args);
226     }
227 
228     // Android-added: Run main method as a JUnit test case.
229     @FlakyTest(bugId = 292520220)
230     @Test
main()231     public void main() {
232         main(new String[0]);
233     }
234 }
235