48697
|
1 |
/*
|
|
2 |
* Copyright (c) 2018, 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 |
package transform.sort;
|
|
24 |
|
|
25 |
import static jaxp.library.JAXPTestUtilities.getSystemProperty;
|
|
26 |
import java.io.StringWriter;
|
|
27 |
import java.net.URI;
|
|
28 |
import java.nio.file.Files;
|
|
29 |
import java.nio.file.Paths;
|
|
30 |
import java.util.List;
|
|
31 |
|
|
32 |
import javax.xml.transform.Result;
|
|
33 |
import javax.xml.transform.Source;
|
|
34 |
import javax.xml.transform.Templates;
|
|
35 |
import javax.xml.transform.Transformer;
|
|
36 |
import javax.xml.transform.TransformerFactory;
|
|
37 |
import javax.xml.transform.stream.StreamResult;
|
|
38 |
import javax.xml.transform.stream.StreamSource;
|
|
39 |
|
|
40 |
import org.testng.Assert;
|
|
41 |
import org.testng.annotations.BeforeClass;
|
|
42 |
import org.testng.annotations.DataProvider;
|
|
43 |
import org.testng.annotations.Listeners;
|
|
44 |
import org.testng.annotations.Test;
|
|
45 |
|
|
46 |
/*
|
|
47 |
* @test
|
|
48 |
* @bug 8193830
|
|
49 |
* @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
|
|
50 |
* @run testng/othervm -DrunSecMngr=true transform.sort.SortTest
|
|
51 |
* @run testng/othervm transform.sort.SortTest
|
|
52 |
* @summary verify xsl:sort lang attribute
|
|
53 |
*/
|
|
54 |
@Listeners({jaxp.library.FilePolicy.class})
|
|
55 |
public class SortTest {
|
|
56 |
|
|
57 |
static final String LAND_EN = "en";
|
|
58 |
static final String LAND_PL = "pl";
|
|
59 |
static final String LAND_RU = "ru";
|
|
60 |
|
|
61 |
String filepath;
|
|
62 |
String slash = "";
|
|
63 |
|
|
64 |
@BeforeClass
|
|
65 |
public void setUpClass() throws Exception {
|
|
66 |
String file1 = getClass().getResource("sort-alphabet-english.xml").getFile();
|
|
67 |
if (getSystemProperty("os.name").contains("Windows")) {
|
|
68 |
filepath = file1.substring(1, file1.lastIndexOf("/") + 1);
|
|
69 |
slash = "/";
|
|
70 |
} else {
|
|
71 |
filepath = file1.substring(0, file1.lastIndexOf("/") + 1);
|
|
72 |
}
|
|
73 |
}
|
|
74 |
|
|
75 |
/*
|
|
76 |
* DataProvider fields:
|
|
77 |
* lang, xml, xsl, gold file
|
|
78 |
*/
|
|
79 |
@DataProvider(name = "parameters")
|
|
80 |
public Object[][] getParameters() {
|
|
81 |
|
|
82 |
return new Object[][]{
|
|
83 |
{LAND_EN, "sort-alphabet-english.xml", "sort-alphabet-english.xsl", "sort-alphabet-english.out"},
|
|
84 |
{LAND_PL, "sort-alphabet-polish.xml", "sort-alphabet-polish.xsl", "sort-alphabet-polish.out"},
|
|
85 |
{LAND_RU, "sort-alphabet-russian.xml", "sort-alphabet-russian.xsl", "sort-alphabet-russian.out"},};
|
|
86 |
}
|
|
87 |
|
|
88 |
@Test(dataProvider = "parameters")
|
|
89 |
public final void testTransform(String lang, String xml, String xsl, String gold)
|
|
90 |
throws Exception {
|
|
91 |
|
|
92 |
StringWriter sw = new StringWriter();
|
|
93 |
// Create transformer factory
|
|
94 |
TransformerFactory factory = TransformerFactory.newInstance();
|
|
95 |
|
|
96 |
// Use the factory to create a template containing the xsl file
|
|
97 |
Templates template = factory.newTemplates(new StreamSource(getClass().getResourceAsStream(xsl)));
|
|
98 |
// Use the template to create a transformer
|
|
99 |
Transformer xformer = template.newTransformer();
|
|
100 |
xformer.setParameter("lang", lang);
|
|
101 |
// Prepare the input and output files
|
|
102 |
Source source = new StreamSource(getClass().getResourceAsStream(xml));
|
|
103 |
|
|
104 |
/*
|
|
105 |
* The following may be used to produce gold files.
|
|
106 |
* Using however the original gold files, and compare without considering
|
|
107 |
* the format
|
|
108 |
*/
|
|
109 |
//String output = getClass().getResource(gold).getPath();
|
|
110 |
//Result result = new StreamResult(new FileOutputStream(output));
|
|
111 |
// use the following to verify the output against the pre-generated one
|
|
112 |
Result result = new StreamResult(sw);
|
|
113 |
|
|
114 |
// Apply the xsl file to the source file and write the result to the
|
|
115 |
// output file
|
|
116 |
xformer.transform(source, result);
|
|
117 |
|
|
118 |
String out = sw.toString();
|
|
119 |
|
|
120 |
List<String> lines = Files.readAllLines(Paths.get(filepath + gold));
|
|
121 |
String[] resultLines = out.split("\n");
|
|
122 |
int i = 0;
|
|
123 |
|
|
124 |
// the purpose is to test sort, so ignore the format of the output
|
|
125 |
for (String line : lines) {
|
|
126 |
Assert.assertEquals(resultLines[i++].trim(), line.trim());
|
|
127 |
}
|
|
128 |
}
|
|
129 |
}
|