Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/xml/jaxp/transform/8062518/XSLTFunctionsTest.java
38860 views
1
/*
2
* Copyright (c) 2015, 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
/* @test
25
* @bug 8062518 8133924
26
* @summary This class contains tests for XSLT functions
27
* @compile DocumentExtFunc.java
28
* @run testng/othervm XSLTFunctionsTest
29
*/
30
31
import java.io.StringReader;
32
import java.io.StringWriter;
33
34
import javax.xml.transform.Source;
35
import javax.xml.transform.Transformer;
36
import javax.xml.transform.TransformerException;
37
import javax.xml.transform.TransformerFactory;
38
import javax.xml.transform.URIResolver;
39
import javax.xml.transform.stream.StreamResult;
40
import javax.xml.transform.stream.StreamSource;
41
42
import org.testng.annotations.DataProvider;
43
import org.testng.annotations.Test;
44
import static org.testng.Assert.assertEquals;
45
46
public class XSLTFunctionsTest {
47
48
/**
49
* bug 8062518
50
* Verifies that a reference to the DTM created by XSLT document function is
51
* actually read from the DTM by an extension function.
52
* @param xml Content of xml file to process
53
* @param xsl stylesheet content that loads external document {@code externalDoc}
54
* with XSLT 'document' function and then reads it with
55
* DocumentExtFunc.test() function
56
* @param externalDoc Content of the external xml document
57
* @param expectedResult Expected transformation result
58
**/
59
@Test(dataProvider = "document")
60
public void testDocument(final String xml, final String xsl,
61
final String externalDoc, final String expectedResult) throws Exception {
62
// Prepare sources for transormation
63
Source src = new StreamSource(new StringReader(xml));
64
Source xslsrc = new StreamSource(new StringReader(xsl));
65
66
// Create factory and transformer
67
TransformerFactory tf = TransformerFactory.newInstance();
68
Transformer t = tf.newTransformer( xslsrc );
69
t.setErrorListener(tf.getErrorListener());
70
71
// Set URI Resolver to return the newly constructed xml
72
// stream source object from xml test string
73
t.setURIResolver(new URIResolver() {
74
@Override
75
public Source resolve(String href, String base)
76
throws TransformerException {
77
if (href.contains("externalDoc")) {
78
return new StreamSource(new StringReader(externalDoc));
79
} else {
80
return new StreamSource(new StringReader(xml));
81
}
82
}
83
});
84
85
// Prepare output stream
86
StringWriter xmlResultString = new StringWriter();
87
StreamResult xmlResultStream = new StreamResult(xmlResultString);
88
89
//Transform the xml
90
t.transform(src, xmlResultStream);
91
92
// If the document can't be accessed and the bug is in place then
93
// reported exception will be thrown during transformation
94
System.out.println("Transformation result:"+xmlResultString.toString().trim());
95
96
// Check the result - it should contain two (node name, node values) entries -
97
// one for original document, another for a document created with
98
// call to 'document' function
99
assertEquals(xmlResultString.toString().trim(), expectedResult);
100
}
101
102
@DataProvider(name = "document")
103
public static Object[][] documentTestData() {
104
return new Object[][] {
105
// 8062518
106
{documentTestXml, documentTestXsl, documentTestExternalDoc, documentTesteExpectedResult},
107
// 8133924
108
{documentTestXml, nonExistingNodeXsl, documentTestExternalDoc, nonExistNodeExpectedResult},
109
};
110
}
111
112
// bug 8133924 xsl: test data to trigger the NPE when non-existing node is specified in xsl
113
static final String nonExistingNodeXsl = "<?xml version='1.0' encoding=\"UTF-8\"?>"
114
+ "<xsl:transform xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\""
115
+ " xmlns:set=\"http://exslt.org/sets\""
116
+ " exclude-result-prefixes=\"set\">"
117
+ " <xsl:template match=\"/\">"
118
+ " <xsl:copy-of select=\"set:leading(/Test, /Test/non-existing)\"/>"
119
+ "</xsl:template>"
120
+ "</xsl:transform>";
121
122
//For bug 8133924 xsl the empty transformation result is expected instead of NPE
123
static final String nonExistNodeExpectedResult = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
124
+ "<Test>Doc</Test>";
125
126
static final String documentTestXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Test>Doc</Test>";
127
128
static final String documentTestExternalDoc = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Test>External Doc</Test>";
129
130
static final String documentTestXsl = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
131
+ "<xsl:transform version=\"1.0\""
132
+ " xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\""
133
+ " xmlns:cfunc=\"http://xml.apache.org/xalan/java/\">"
134
+ "<xsl:template match=\"/\">"
135
+ "<xsl:element name=\"root\">"
136
+ "<xsl:variable name=\"other_doc\" select=\"document(&#39;externalDoc&#39;)\"/>"
137
+ "<!-- Source -->"
138
+ "<xsl:value-of select=\"cfunc:DocumentExtFunc.test(/Test)\"/>"
139
+ "<!-- document() -->"
140
+ "<xsl:value-of select=\"cfunc:DocumentExtFunc.test($other_doc/Test)\"/>"
141
+ "</xsl:element></xsl:template></xsl:transform>";
142
143
static final String documentTesteExpectedResult = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
144
+ "<root>[Test:Doc][Test:External Doc]</root>";
145
}
146
147