summaryrefslogtreecommitdiff
path: root/test/java/src/org/apache/qetest/xalanj2/OutputSettingsTest.java
blob: e3e1eed6acf1bd2814269450fa9daeedc027532b (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
/*
 * 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$
 */

/*
 *
 * OutputSettingsTest.java
 *
 */
package org.apache.qetest.xalanj2;

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

import javax.xml.transform.Result;
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.Logger;
import org.apache.qetest.OutputNameManager;
import org.apache.qetest.QetestUtils;
import org.apache.qetest.xsl.XSLTestfileInfo;
import org.junit.Test;

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

/**
 * Verify xalan:-specific output properties.
 * This test is similar to trax.OutputPropertiesTest but tests 
 * some Xalan-J 2.2.x+ specific features for the xalan: namespace, 
 * like: indent-amount, content-handler, entities,
 * use-url-escaping, and omit-meta-tag.
 * 
 * @author shane_curcuru@lotus.com
 * @version $Id$
 */
public class OutputSettingsTest extends FileBasedTest
{

    /** Used for generating names of actual output files.   */
    protected OutputNameManager outNames;

    /** Default OutputSettingsTest.xml/xsl file pair.   */
    protected XSLTestfileInfo xmlFileInfo = new XSLTestfileInfo();

    /** OutputEntities.xml/xsl/ent file pair.   */
    protected XSLTestfileInfo entFileInfo = new XSLTestfileInfo();

    /** Just initialize test name, comment, numTestCases. */
    public OutputSettingsTest()
    {
        numTestCases = 2;  // REPLACE_num
        testName = "OutputSettingsTest";
        testComment = "Verify xalan:-specific output properties";
    }


    /**
     * Initialize this test - Set names of xml/xsl test files, etc.  
     *
     * @param p Properties to initialize from (if needed)
     * @return false if we should abort the test; true otherwise
     */
    public boolean doTestFileInit(Properties p)
    {
        // Used for all tests; just dump files in trax subdir
        File outSubDir = new File(outputDir + File.separator + "xalanj2");
        if (!outSubDir.mkdirs())
            reporter.logWarningMsg("Possible problem creating output dir: " + outSubDir);
        // Initialize an output name manager to that dir with .out extension
        outNames = new OutputNameManager(outputDir + File.separator + "xalanj2"
                                         + File.separator + testName, ".out");

        String testBasePath = inputDir 
                              + File.separator 
                              + "xalanj2"
                              + File.separator;
        String goldBasePath = goldDir 
                              + File.separator 
                              + "xalanj2"
                              + File.separator;

        xmlFileInfo.inputName = testBasePath + "OutputSettingsXML.xsl";
        xmlFileInfo.xmlName = testBasePath + "OutputSettingsXML.xml";
        // Only root of the output gold name
        xmlFileInfo.goldName = goldBasePath + "OutputSettingsXML";

        // xsl file references OutputEntities.ent
        entFileInfo.inputName = testBasePath + "OutputEntities.xsl";
        entFileInfo.xmlName = testBasePath + "identity.xml";
        entFileInfo.goldName = goldBasePath + "OutputEntities.out";
        return true;
    }


    /**
     * Verify xalan:entities output property.
     * @return false if we should abort the test; true otherwise
     */
    public boolean testCase1()
    {
        reporter.testCaseInit("Verify xalan:entities output property");
        TransformerFactory factory = null;
        Templates templates = null;

        try
        {
            // Process stylesheet with replaced entities
            factory = TransformerFactory.newInstance();
            reporter.logInfoMsg("entFileInfo newTemplates(" + QetestUtils.filenameToURL(entFileInfo.inputName) + ")");
            templates = factory.newTemplates(new StreamSource(QetestUtils.filenameToURL(entFileInfo.inputName)));
            reporter.logHashtable(Logger.STATUSMSG, 
                                  templates.getOutputProperties(), "entFileInfo templates output properties");

            // Process the file once with default properties
            Transformer transformer = templates.newTransformer();
            Result result = new StreamResult(outNames.nextName());
            reporter.logInfoMsg("(1)replaced entities transform(" + QetestUtils.filenameToURL(entFileInfo.xmlName) 
                                + ", " + outNames.currentName() + ")");
            transformer.transform(new StreamSource(QetestUtils.filenameToURL(entFileInfo.xmlName)), result);
            // Validate the default transform to base gold file
            fileChecker.check(reporter, 
                              new File(outNames.currentName()), 
                              new File(entFileInfo.goldName), 
                              "(1)replaced entities transform into: " + outNames.currentName()
                              + " gold: " + entFileInfo.goldName);

        }
        catch (Throwable t)
        {
            reporter.checkErr("(1)replaced entities threw:" + t.toString());
            reporter.logThrowable(reporter.ERRORMSG, t, "(1)replaced entities threw ");
            return true;
        }
        reporter.testCaseClose();
        return true;
    }

    /**
     * Verify xalan:indent-amount output property.
     * @return false if we should abort the test; true otherwise
     */
    public boolean testCase2()
    {
        reporter.testCaseInit("Verify xalan:indent-amount output property");
        TransformerFactory factory = null;
        Templates templates = null;
        try
        {
            // Process simple XML output stylesheet
            factory = TransformerFactory.newInstance();
            reporter.logInfoMsg("xmlFileInfo newTemplates(" + QetestUtils.filenameToURL(xmlFileInfo.inputName) + ")");
            templates = factory.newTemplates(new StreamSource(QetestUtils.filenameToURL(xmlFileInfo.inputName)));
            reporter.logHashtable(Logger.STATUSMSG, 
                                  templates.getOutputProperties(), "xmlFileInfo templates output properties");

            // Process the file once with default properties
            Transformer transformer = templates.newTransformer();
            Result result = new StreamResult(outNames.nextName());
            reporter.logInfoMsg("(2)xml transform(" + QetestUtils.filenameToURL(xmlFileInfo.xmlName) 
                                + ", " + outNames.currentName() + ")");
            transformer.transform(new StreamSource(QetestUtils.filenameToURL(xmlFileInfo.xmlName)), result);
            // Validate the default transform to base gold file
            fileChecker.check(reporter, 
                              new File(outNames.currentName()), 
                              new File(xmlFileInfo.goldName + ".out"), 
                              "(2)xml transform into: " + outNames.currentName()
                              + " gold: " + xmlFileInfo.goldName + ".out");

            // Set Xalan-specific output property 
            reporter.logInfoMsg("setOutputProperty({http://xml.apache.org/xslt}indent-amount, 2)");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

            result = new StreamResult(outNames.nextName());
            reporter.logInfoMsg("(2)xml-2 transform(" + QetestUtils.filenameToURL(xmlFileInfo.xmlName) 
                                + ", " + outNames.currentName() + ")");
            transformer.transform(new StreamSource(QetestUtils.filenameToURL(xmlFileInfo.xmlName)), result);
            // Validate the default transform to base gold file
            fileChecker.check(reporter, 
                              new File(outNames.currentName()), 
                              new File(xmlFileInfo.goldName + "-2.out"), 
                              "(2)xml-2 transform into: " + outNames.currentName()
                              + " gold: " + xmlFileInfo.goldName + "-2.out");
            reporter.logHashtable(Logger.STATUSMSG, 
                                  transformer.getOutputProperties(), "xml-2 transformer output properties");

            // Set Xalan-specific output property 
            reporter.logInfoMsg("setOutputProperty({http://xml.apache.org/xslt}indent-amount, 12)");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "12");

            result = new StreamResult(outNames.nextName());
            reporter.logInfoMsg("(2)xml-12 transform(" + QetestUtils.filenameToURL(xmlFileInfo.xmlName) 
                                + ", " + outNames.currentName() + ")");
            transformer.transform(new StreamSource(QetestUtils.filenameToURL(xmlFileInfo.xmlName)), result);
            // Validate the default transform to base gold file
            fileChecker.check(reporter, 
                              new File(outNames.currentName()), 
                              new File(xmlFileInfo.goldName + "-12.out"), 
                              "(2)xml-12 transform into: " + outNames.currentName()
                              + " gold: " + xmlFileInfo.goldName + "-12.out");
            reporter.logHashtable(Logger.STATUSMSG, 
                                  transformer.getOutputProperties(), "xml-12 transformer output properties");
        }
        catch (Throwable t)
        {
            reporter.checkErr("(2)xml stylesheet threw:" + t.toString());
            reporter.logThrowable(reporter.ERRORMSG, t, "(2)xml stylesheet threw ");
            return true;
        }
        reporter.testCaseClose();
        return true;
    }

    /**
     * Convenience method to print out usage information - update if needed.  
     * @return String denoting usage of this test class
     */
    public String usage()
    {
        return ("Common [optional] options supported by OutputSettingsTest:\n"
                + "(Note: assumes inputDir=tests\\api)\n"
                + super.usage());   // Grab our parent classes usage as well
    }

    /**
     * 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)
    {
        OutputSettingsTest app = new OutputSettingsTest();
        app.doMain(args);
    }

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