1/*
2 * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24package transform;
25
26import java.io.FileInputStream;
27import java.io.FileNotFoundException;
28import java.io.StringWriter;
29
30import javax.xml.transform.Result;
31import javax.xml.transform.Source;
32import javax.xml.transform.SourceLocator;
33import javax.xml.transform.Templates;
34import javax.xml.transform.Transformer;
35import javax.xml.transform.TransformerConfigurationException;
36import javax.xml.transform.TransformerException;
37import javax.xml.transform.TransformerFactory;
38import javax.xml.transform.stream.StreamResult;
39import javax.xml.transform.stream.StreamSource;
40
41import org.testng.Assert;
42import org.testng.annotations.Listeners;
43import org.testng.annotations.Test;
44
45/*
46 * @test
47 * @bug 6940416
48 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
49 * @run testng/othervm -DrunSecMngr=true transform.Bug6940416
50 * @run testng/othervm transform.Bug6940416
51 * @summary Test transforming correctly.
52 */
53@Listeners({jaxp.library.FilePolicy.class})
54public class Bug6940416 {
55
56    @Test
57    public void test() {
58        String xslFilename = getClass().getResource("ViewEditor1.xsl").getFile();
59        String inFilename = getClass().getResource("in.xml").getFile();
60        // String outFilename =
61        // getClass().getResource("out-6u17.xml").getFile();
62        // the xml result
63        StringWriter xmlResultString = new StringWriter();
64        try {
65            // Create transformer factory
66            TransformerFactory factory = TransformerFactory.newInstance();
67            factory.setAttribute("debug", true);
68            // Use the factory to create a template containing the xsl file
69            Templates template = factory.newTemplates(new StreamSource(new FileInputStream(xslFilename)));
70            // Use the template to create a transformer
71            Transformer xformer = template.newTransformer();
72            // Prepare the input and output files
73            Source source = new StreamSource(new FileInputStream(inFilename));
74            // Result result = new StreamResult(new
75            // FileOutputStream(outFilename));
76            Result result = new StreamResult(xmlResultString);
77            // Apply the xsl file to the source file and write the result to the
78            // output file
79            xformer.transform(source, result);
80
81            // 6u17 results contain the following:
82            /**
83             * var g_strInitialTabID = "VIEWEDITOR_TAB_FIELDS";
84             *
85             * var g_strCurrentDataEditorTabID = "DATA_OBJECTS"; var
86             * g_strCurrentPropertyEditorTabID = "VIEWEDITOR_TAB_GENERAL";
87             *
88             * while 6u18: var g_strInitialTabID = "";
89             *
90             * var g_strCurrentDataEditorTabID = ""; var
91             * g_strCurrentPropertyEditorTabID = "VIEWEDITOR_TAB_GENERAL";
92             */
93            System.out.println(xmlResultString.toString());
94            if (xmlResultString.toString().indexOf("VIEWEDITOR_TAB_FIELDS") == -1) {
95                Assert.fail("regression from 6u17");
96            }
97        } catch (FileNotFoundException e) {
98            e.printStackTrace();
99            Assert.fail(e.toString());
100        } catch (TransformerConfigurationException e) {
101            // An error occurred in the XSL file
102            e.printStackTrace();
103            Assert.fail(e.toString());
104        } catch (TransformerException e) {
105            e.printStackTrace();
106            // An error occurred while applying the XSL file
107            // Get location of error in input file
108            SourceLocator locator = e.getLocator();
109            int col = locator.getColumnNumber();
110            int line = locator.getLineNumber();
111            String publicId = locator.getPublicId();
112            String systemId = locator.getSystemId();
113            Assert.fail("error while applying the XSL file." + "systemId : " + systemId + ". publicId : " + publicId + ". col : " + col + ". line : " + line);
114        }
115    }
116
117}
118