summaryrefslogtreecommitdiff
path: root/test/java/src/org/apache/qetest/trax/TemplatesAPITest.java
blob: 3603145daa5df8cf7b242b4f4455f1b0cdbbe147 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the  "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*
 * $Id$
 */

/*
 *
 * TemplatesAPITest.java
 *
 */
package org.apache.qetest.trax;

import android.platform.test.annotations.FlakyTest;
import java.io.File;
import java.util.Properties;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.apache.qetest.FileBasedTest;
import org.apache.qetest.OutputNameManager;
import org.apache.qetest.QetestUtils;
import org.apache.qetest.xsl.XSLTestfileInfo;
import org.apache.xml.utils.DefaultErrorHandler;
import org.junit.Test;

//-------------------------------------------------------------------------

/**
 * Basic API coverage test for the Templates class of TRAX.
 * @author shane_curcuru@lotus.com
 */
public class TemplatesAPITest extends FileBasedTest
{

    /**
     * Cheap-o filename for various output files.
     *
     */
    protected OutputNameManager outNames;

    /** Cheap-o filename set for both API tests and exampleSimple. */
    protected XSLTestfileInfo simpleTest = new XSLTestfileInfo();

    /** Name of a stylesheet with xsl:output HTML. */
    protected String outputFormatXSL = null;

    /** System property name javax.xml.transform.TransformerFactory.  */
    public static final String TRAX_PROCESSOR_XSLT = "javax.xml.transform.TransformerFactory";

    /** Known outputFormat property name from outputFormatTest  */
    public static final String OUTPUT_FORMAT_NAME = OutputKeys.CDATA_SECTION_ELEMENTS;

    /** Known outputFormat property value from outputFormatTest  */
    public static final String OUTPUT_FORMAT_VALUE = "cdataHere";

    /** NEEDSDOC Field TRAX_SUBDIR          */
    public static final String TRAX_SUBDIR = "trax";

    /** Default ctor initializes test name, comment, numTestCases. */
    public TemplatesAPITest()
    {

        numTestCases = 1;  // REPLACE_num
        testName = "TemplatesAPITest";
        testComment = "Basic API coverage test for the Templates class of TRAX";
    }

    /**
     * Initialize this test - Set names of xml/xsl test files, cache system property.  
     *
     * NEEDSDOC @param p
     *
     * NEEDSDOC ($objectName$) @return
     */
    public boolean doTestFileInit(Properties p)
    {

        // Used for all tests; just dump files in xapi subdir
        File outSubDir = new File(outputDir + File.separator + TRAX_SUBDIR);

        if (!outSubDir.mkdirs())
            reporter.logWarningMsg("Could not create output dir: "
                                   + outSubDir);

        outNames = new OutputNameManager(outputDir + File.separator + TRAX_SUBDIR
                                         + File.separator + testName, ".out");

        // Used for API coverage and exampleSimple
        String testBasePath = inputDir + File.separator + TRAX_SUBDIR
                              + File.separator;
        String goldBasePath = goldDir + File.separator + TRAX_SUBDIR
                              + File.separator;

        simpleTest.xmlName = QetestUtils.filenameToURL(testBasePath + "TransformerAPIParam.xml");
        simpleTest.inputName = QetestUtils.filenameToURL(testBasePath + "TransformerAPIParam.xsl");
        simpleTest.goldName = goldBasePath + "TransformerAPIParam.out";
        outputFormatXSL = QetestUtils.filenameToURL(testBasePath + "TransformerAPIOutputFormat.xsl");

        reporter.logInfoMsg(TRAX_PROCESSOR_XSLT + " property is: "
                            + System.getProperty(TRAX_PROCESSOR_XSLT));
        try
        {
            TransformerFactory tf = TransformerFactory.newInstance();
            if (!(tf.getFeature(StreamSource.FEATURE)
                  && tf.getFeature(StreamResult.FEATURE)))
            {   // The rest of this test relies on Streams
                reporter.logErrorMsg("Streams not supported! Some tests may be invalid!");
            }
        }
        catch (Throwable t)
        {
            reporter.checkFail(
                "Problem creating factory; Some tests may be invalid!");
            reporter.logThrowable(reporter.ERRORMSG, t,
                                  "Problem creating factory; Some tests may be invalid!");
        }

        return true;
    }


    /**
     * TRAX Templates: cover newTransformer(), 
     * getOutputProperties() APIs and basic functionality.
     *
     * NEEDSDOC ($objectName$) @return
     */
    public boolean testCase1()
    {
        reporter.testCaseInit("TRAX Templates: cover APIs and basic functionality");

        TransformerFactory factory = null;
        try
        {
            factory = TransformerFactory.newInstance();
            factory.setErrorListener(new DefaultErrorHandler());
        }
        catch (Exception e)
        {
            reporter.checkFail(
                "Problem creating Processor; cannot continue testcase");
            reporter.logThrowable(reporter.ERRORMSG, e,
                                  "Problem creating Processor");
            return true;
        }

        try
        {
            // Cover APIs newTransformer(), getOutputProperties()
            Templates templates =
                factory.newTemplates(new StreamSource(simpleTest.inputName));
            Transformer transformer = templates.newTransformer();

            reporter.check((transformer != null), true,
                           "newTransformer() is non-null for "
                           + simpleTest.inputName);

            Properties outputFormat = templates.getOutputProperties();

            reporter.check((outputFormat != null), true,
                           "getOutputProperties() is non-null for "
                           + simpleTest.inputName);
            reporter.logHashtable(reporter.STATUSMSG, outputFormat,
                                  "getOutputProperties for " + simpleTest.inputName);

            // Check that the local stylesheet.getProperty has default set, cf. getOutputProperties javadoc
            reporter.check(("xml".equals(outputFormat.getProperty(OutputKeys.METHOD))), true, simpleTest.inputName + ".op.getProperty(" 
                           + OutputKeys.METHOD + ") is default value, act: " + outputFormat.getProperty(OutputKeys.METHOD));
            // Check that the local stylesheet.get has nothing set, cf. getOutputProperties javadoc
            reporter.check((null == outputFormat.get(OutputKeys.METHOD)), true, simpleTest.inputName + ".op.get(" 
                           + OutputKeys.METHOD + ") is null value, act: " + outputFormat.get(OutputKeys.METHOD));

            // Check that the local stylesheet.getProperty has default set, cf. getOutputProperties javadoc
            reporter.check(("no".equals(outputFormat.getProperty(OutputKeys.INDENT))), true, simpleTest.inputName + ".op.getProperty(" 
                           + OutputKeys.INDENT + ") is default value, act: " + outputFormat.getProperty(OutputKeys.INDENT));
            // Check that the local stylesheet.get has nothing set, cf. getOutputProperties javadoc
            reporter.check((null == (outputFormat.get(OutputKeys.INDENT))), true, simpleTest.inputName + ".op.get(" 
                           + OutputKeys.INDENT + ") is null value, act: " + outputFormat.get(OutputKeys.INDENT));
        }
        catch (Exception e)
        {
            reporter.checkErr("newTransformer/getOutputProperties threw: "
                              + e.toString());
            reporter.logThrowable(reporter.STATUSMSG, e,
                                  "newTransformer/getOutputProperties threw:");
        }

        try
        {
            Templates templates2 =
                factory.newTemplates(new StreamSource(outputFormatXSL));
            Properties outputFormat2 = templates2.getOutputProperties();

            reporter.check((outputFormat2 != null), true,
                           "getOutputProperties() is non-null for "
                           + outputFormatXSL);
            reporter.logHashtable(reporter.STATUSMSG, outputFormat2,
                                  "getOutputProperties for " + outputFormatXSL);

            String tmp = outputFormat2.getProperty(OUTPUT_FORMAT_NAME); // SPR SCUU4RXSG5 - has extra space
            if (OUTPUT_FORMAT_VALUE.equals(tmp))    // Use if so we can put out id with checkPass/checkFail lines
                reporter.checkPass("outputProperties " + OUTPUT_FORMAT_NAME + " has known value ?" + tmp + "?", "SCUU4RXSG5");
            else
                reporter.checkFail("outputProperties " + OUTPUT_FORMAT_NAME + " has known value ?" + tmp + "?", "SCUU4RXSG5");

            tmp = outputFormat2.getProperty("omit-xml-declaration");
            reporter.check(tmp, "yes", "outputProperties omit-xml-declaration has known value ?" + tmp + "?");
        }
        catch (Exception e)
        {
            reporter.checkErr("outputFormat() is html... threw: "
                              + e.toString());
            reporter.logThrowable(reporter.STATUSMSG, e,
                                  "outputFormat() is html... threw:");
        }
        reporter.logTraceMsg("Functionality of Transformers covered in TransformerAPITest, elsewhere");
        reporter.testCaseClose();

        return true;
    }


    /**
     * Convenience method to print out usage information - update if needed.  
     *
     * NEEDSDOC ($objectName$) @return
     */
    public String usage()
    {

        return ("Common [optional] options supported by TemplatesAPITest:\n"
                + "(Note: assumes inputDir=.\\tests\\api)\n"
                + "-processorClassname classname.of.processor  (to override setPlatformDefaultProcessor to Xalan 2.x)\n"
                + super.usage());
    }


    /**
     * Main method to run test from the command line - can be left alone.  
     * @param args command line argument array
     */
    public static void main(String[] args)
    {

        TemplatesAPITest app = new TemplatesAPITest();

        app.doMain(args);
    }

    // Android-added: Run main method as a JUnit test case.
    @FlakyTest(bugId = 292520220)
    @Test
    public void main() {
        main(new String[0]);
    }
}