1 /*
2  * Copyright (c) 2019, 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 
24 package transform;
25 
26 import java.io.StringReader;
27 import java.io.StringWriter;
28 import java.util.Properties;
29 import javax.xml.parsers.DocumentBuilderFactory;
30 import javax.xml.transform.OutputKeys;
31 import javax.xml.transform.Templates;
32 import javax.xml.transform.Transformer;
33 import javax.xml.transform.TransformerFactory;
34 import javax.xml.transform.stream.StreamResult;
35 import javax.xml.transform.stream.StreamSource;
36 import org.testng.Assert;
37 import org.testng.annotations.DataProvider;
38 import org.testng.annotations.Test;
39 import org.w3c.dom.Document;
40 import org.xml.sax.InputSource;
41 
42 /*
43  * @test
44  * @bug 8219705 8223291
45  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
46  * @run testng transform.OutputPropertiesTest
47  * @summary Verifies the output properties are set correctly
48  */
49 public class OutputPropertiesTest {
50     /*
testXMLStackOverflowBug()51        DataProvider: for testing indentation
52        Data: xml, expected result
53      */
54     @DataProvider(name = "Indentation")
55     public Object[][] getData() {
56         String mix = "\n" +
57                 "        abc\n" +
58                 "        mix\n" +
59                 "        xyz\n" +
60                 "    ";
61         return new Object[][]{
62             {"abc<![CDATA[data]]>xyz", "abcdataxyz"},
63             {"abc<![CDATA[ & ]]>xyz", "abc & xyz"},
64             {"<![CDATA[data]]>", "data"},
65             {"abc<mix>mix</mix>xyz", mix}
66         };
67     }
68 
69 
70     /**
71      * bug 8223291
72      * Verifies that no extra indentation is added for CDATA.
73      * @param xml the xml content to be tested
74      * @param expected the expected result
75      * @throws Exception
76      */
77     @Test(dataProvider = "Indentation")
78     public void testIndentation(String xml, String expected) throws Exception
79     {
80         StreamSource source = new StreamSource(new StringReader("<foo><bar>" + xml + "</bar></foo>"));
81         StreamResult result = new StreamResult(new StringWriter());
82 
83         Transformer tform = TransformerFactory.newInstance().newTransformer();
84         tform.setOutputProperty(OutputKeys.INDENT, "yes");
85         tform.transform(source, result);
86 
87         String xml1 = result.getWriter().toString();
88 
89         Document document = DocumentBuilderFactory.newInstance()
90             .newDocumentBuilder()
91             .parse(new InputSource(new StringReader(xml1)));
92 
93         String resultData = document.getElementsByTagName("bar")
94             .item(0)
95             .getTextContent();
96 
97         Assert.assertEquals(resultData, expected);
98     }
99 
100     @Test
101     public void testOutputProperties() throws Exception {
102         String xslData = "<?xml version='1.0'?>"
103                 + "<xsl:stylesheet"
104                 + " xmlns:xsl='http://www.w3.org/1999/XSL/Transform'"
105                 + " version='1.0'"
106                 + ">\n"
107                 + "   <xsl:output method='html'/>\n"
108                 + "   <xsl:template match='/'>\n"
109                 + "     Hello World! \n"
110                 + "   </xsl:template>\n"
111                 + "</xsl:stylesheet>";
112 
113         System.out.println(xslData);
114 
115         Templates templates = TransformerFactory.newInstance()
116                     .newTemplates(new StreamSource(new StringReader(xslData)));
117 
118         Properties properties = templates.getOutputProperties();
119         String[] prNames = new String[]{"method", "version", "indent", "media-type"};
120         String[] prValues = new String[]{"html", "4.0", "yes", "text/html"};
121 
122         for (int i = 0; i < prNames.length; i++) {
123             String value = properties.getProperty(prNames[i]);
124             String msg = "The value of the property '" + prNames[i] + "' should be '"
125                     + prValues[i] + "' when the method is '" + prValues[0] + "'. \n";
126             Assert.assertEquals(value, prValues[i], msg);
127             System.out.println(
128                     prNames[i] + ": actual: " + value + ", expected: " + prValues[i]);
129         }
130     }
131 
132 }
133