• 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  * TransformStateTest.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.util.Properties;
32 import java.util.Vector;
33 
34 import org.apache.qetest.FileBasedTest;
35 import org.apache.qetest.Logger;
36 import org.junit.Test;
37 
38 //-------------------------------------------------------------------------
39 
40 /**
41  * Basic functionality testing of TransformState interface.
42  * This is basically just a test driver class for an explicit
43  * list of TransformStateTestlet/Datalets.  In the future we
44  * should enable better data-driven testing by being able to
45  * read in a list of TransformStateDatalets somehow.
46  *
47  * @author shane_curcuru@lotus.com
48  * @version $Id$
49  */
50 public class TransformStateTest extends FileBasedTest
51 {
52 
53     /** Subdirectory under test\tests\api for our xsl/xml files.  */
54     public static final String X2J_SUBDIR = "xalanj2";
55 
56 
57     /** Just initialize test name, comment, numTestCases. */
TransformStateTest()58     public TransformStateTest()
59     {
60         numTestCases = 1;  // REPLACE_num
61         testName = "TransformStateTest";
62         testComment = "Basic functionality testing of TransformState interface";
63     }
64 
65 
66     /**
67      * Initialize this test - create output dir.
68      *
69      * @param p Properties to initialize from (if needed)
70      * @return false if we should abort the test; true otherwise
71      */
doTestFileInit(Properties p)72     public boolean doTestFileInit(Properties p)
73     {
74         // NOTE: 'reporter' variable is already initialized at this point
75 
76         // Used for all tests; just dump files in trax subdir
77         File outSubDir = new File(outputDir + File.separator + X2J_SUBDIR);
78         if (!outSubDir.mkdirs())
79             reporter.logWarningMsg("Could not create output dir: " + outSubDir);
80 
81         return true;
82     }
83 
84 
85     /**
86      * Use a TransformStateTestlet to test some datalets.
87      *
88      * @return false if we should abort the test; true otherwise
89      */
testCase1()90     public boolean testCase1()
91     {
92         // Use a 'fake' testCase1 to initialize everything
93         reporter.testCaseInit("Use a TransformStateTestlet to test some datalets");
94         reporter.logWarningMsg("Note: limited validation: only certain events checked.");
95 
96         // First arg is list of files; currently unused
97         // Note: several method signatures are copied from
98         //  StylesheetTestletDriver for future growth
99         Vector datalets = buildDatalets(null,
100                                         new File(inputDir + File.separator + X2J_SUBDIR),
101                                         new File(outputDir + File.separator + X2J_SUBDIR),
102                                         new File(goldDir + File.separator + X2J_SUBDIR));
103 
104         // Validate datalets
105         if ((null == datalets) || (0 == datalets.size()))
106         {
107             // No datalets to test, report it as an error
108             reporter.checkErr("Testlet or datalets are null/blank, nothing to test!");
109             return true;
110         }
111         else
112         {
113             reporter.checkPass("Found " + datalets.size() + " datalets to test...");
114         }
115 
116         // Now just go through the list and process each set
117         int numDatalets = 0;
118         numDatalets = datalets.size();
119         reporter.logInfoMsg("processFileList-equivalent() with " + numDatalets
120                             + " potential tests");
121         // Close out our 'fake' initialization testCase1
122         reporter.testCaseClose();
123         // Iterate over every datalet and test it
124         for (int ctr = 0; ctr < numDatalets; ctr++)
125         {
126             try
127             {
128                 reporter.testCaseInit("Testing datalet(" + ctr + ") validation size="
129                         + ((TransformStateDatalet)datalets.elementAt(ctr)).validate99.size());
130                 // Create a Testlet to execute a test with this
131                 //  next datalet - the Testlet will log all info
132                 //  about the test, including calling check*()
133                 getTestlet().execute((TransformStateDatalet)datalets.elementAt(ctr));
134             }
135             catch (Throwable t)
136             {
137                 // Log any exceptions as fails and keep going
138                 //@todo improve the below to output more useful info
139                 reporter.checkFail("Datalet num " + ctr + " threw: " + t.toString());
140                 reporter.logThrowable(Logger.ERRORMSG, t, "Datalet threw");
141             }
142             reporter.testCaseClose();
143         }  // of while...
144 
145         return true;
146     }
147 
148 
149     /**
150      * Transform a vector of individual test names into a Vector
151      * of filled-in datalets to be tested
152      *
153      * This currently is hard-coded to return a static Vector
154      * of Datalets that are simply constructed here.
155      * In the future we should add a way to read them in from disk.
156      *
157      * @param files Vector of local path\filenames to be tested
158      * This is currently ignored
159      * @param testLocation File denoting directory where all
160      * .xml/.xsl tests are found
161      * @param outLocation File denoting directory where all
162      * output files should be put
163      * @param goldLocation File denoting directory where all
164      * gold files are found
165      * @return Vector of StylesheetDatalets that are fully filled in,
166      * i.e. outputName, goldName, etc are filled in respectively
167      * to inputName
168      */
buildDatalets(Vector files, File testLocation, File outLocation, File goldLocation)169     public Vector buildDatalets(Vector files, File testLocation,
170                                 File outLocation, File goldLocation)
171     {
172         Vector v = new Vector();
173 
174 /***********************************************
175 // Comment out validation using ExpectedObjects since they hang 28-Jun-01 -sc
176         // A simple datalet for identity transform
177         TransformStateDatalet d = new TransformStateDatalet();
178         String testFileName = "identity";
179         d.inputName = testLocation.getPath() + File.separator + testFileName + ".xsl";
180         d.xmlName = testLocation.getPath() + File.separator + testFileName + ".xml";
181         d.outputName = outLocation.getPath() + File.separator + testFileName + ".out";
182         d.goldName = goldLocation.getPath() + File.separator + testFileName + ".out";
183         // Validation TransformStates for RootTemplate
184         ExpectedTransformState ets = new ExpectedTransformState();
185         ets.setName("doc");
186         ets.set("line", ExpectedObject.MUST_EQUAL, new Integer(6));
187         ets.set("column", ExpectedObject.MUST_EQUAL, new Integer(8));
188         ets.set("event", ExpectedObject.MUST_EQUAL, "startElement:");
189         ets.set("current.match", ExpectedObject.MUST_EQUAL, "@*|node()");
190         ets.set("matched.match", ExpectedObject.MUST_EQUAL, "@*|node()");
191         // Add the expected object(s) to the datalet..
192         d.expectedTransformStates.put(ets.getHashKey(), ets);
193 
194         ets = new ExpectedTransformState();
195         ets.setName("copy");
196         ets.set("line", ExpectedObject.MUST_EQUAL, new Integer(7));
197         ets.set("column", ExpectedObject.MUST_EQUAL, new Integer(15));
198         ets.set("event", ExpectedObject.MUST_EQUAL, "startElement:");
199         ets.set("current.match", ExpectedObject.MUST_EQUAL, "@*|node()");
200         // Add the expected object(s) to the datalet..
201         d.expectedTransformStates.put(ets.getHashKey(), ets);
202 
203 // Comment out validation using ExpectedObjects since they hang 28-Jun-01 -sc
204 ***********************************************/
205         // Simple single file with call-template, modes
206         TransformStateDatalet d = new TransformStateDatalet();
207         String testFileName = "TransformState99a";
208         d.inputName = testLocation.getPath() + File.separator + testFileName + ".xsl";
209         d.xmlName = testLocation.getPath() + File.separator + testFileName + ".xml";
210         d.outputName = outLocation.getPath() + File.separator + testFileName + ".out";
211         d.goldName = goldLocation.getPath() + File.separator + testFileName + ".out";
212         d.validate99.put("42.current.name", "apple");
213         d.validate99.put("42.current.match", "pies-are-good");
214         d.validate99.put("42.matched.name", "template-1-root");
215         d.validate99.put("42.matched.match", "/");
216 
217         // .. and Add the datalet to the vector
218         v.addElement(d);
219 
220         // Simple included file
221         d = new TransformStateDatalet();
222         testFileName = "TransformState99b"; // and TransformState99binc.xsl
223         d.inputName = testLocation.getPath() + File.separator + testFileName + ".xsl";
224         d.xmlName = testLocation.getPath() + File.separator + testFileName + ".xml";
225         d.outputName = outLocation.getPath() + File.separator + testFileName + ".out";
226         d.goldName = goldLocation.getPath() + File.separator + testFileName + ".out";
227         // Note this should still be cross-checked for line numbers when using included files!
228         d.validate99.put("27.current.name", "apple");
229         d.validate99.put("27.current.match", "pies-are-good");
230         d.validate99.put("27.matched.name", "template-1-root");
231         d.validate99.put("27.matched.match", "/");
232 
233         // .. and Add the datalet to the vector
234         v.addElement(d);
235 
236         // Simple imported file
237         d = new TransformStateDatalet();
238         testFileName = "TransformState99c"; // and TransformState99cimp.xsl
239         d.inputName = testLocation.getPath() + File.separator + testFileName + ".xsl";
240         d.xmlName = testLocation.getPath() + File.separator + testFileName + ".xml";
241         d.outputName = outLocation.getPath() + File.separator + testFileName + ".out";
242         d.goldName = goldLocation.getPath() + File.separator + testFileName + ".out";
243         // Note this should still be cross-checked for line numbers when using included files!
244         d.validate99.put("32.current.name", "apple");
245         d.validate99.put("32.current.match", "pies-are-good");
246         d.validate99.put("32.matched.name", "template-1-root");
247         d.validate99.put("32.matched.match", "/");
248 
249         // .. and Add the datalet to the vector
250         v.addElement(d);
251 
252         // All done: return full vector
253         return v;
254     }
255 
256 
257     /**
258      * Convenience method to get a Testlet to use.
259      * Hard-coded to return a TransformStateTestlet.
260      *
261      * @return Testlet for use in this test; null if error
262      */
getTestlet()263     public TransformStateTestlet getTestlet()
264     {
265         try
266         {
267             // Create it and set our reporter into it
268             TransformStateTestlet t = new TransformStateTestlet();
269             t.setLogger((Logger)reporter);
270             return t;
271         }
272         catch (Exception e)
273         {
274             // Ooops, problem, should get logged somehow
275             return null;
276         }
277     }
278 
279 
280     /**
281      * Convenience method to print out usage information - update if needed.
282      * @return String denoting usage of this test class
283      */
usage()284     public String usage()
285     {
286         return ("Common [optional] options supported by TransformStateTest:\n"
287                 + "(Note: assumes inputDir=.\\tests\\api)\n"
288                 + super.usage());   // Grab our parent classes usage as well
289     }
290 
291 
292     /**
293      * Main method to run test from the command line - can be left alone.
294      * @param args command line argument array
295      */
main(String[] args)296     public static void main(String[] args)
297     {
298         TransformStateTest app = new TransformStateTest();
299         app.doMain(args);
300     }
301 
302     // Android-added: Run main method as a JUnit test case.
303     @FlakyTest(bugId = 292520220)
304     @Test
main()305     public void main() {
306         main(new String[0]);
307     }
308 }
309