Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/xml/jaxp/transform/8062518/XSLTFunctionsTest.java
38860 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 8062518 813392425* @summary This class contains tests for XSLT functions26* @compile DocumentExtFunc.java27* @run testng/othervm XSLTFunctionsTest28*/2930import java.io.StringReader;31import java.io.StringWriter;3233import javax.xml.transform.Source;34import javax.xml.transform.Transformer;35import javax.xml.transform.TransformerException;36import javax.xml.transform.TransformerFactory;37import javax.xml.transform.URIResolver;38import javax.xml.transform.stream.StreamResult;39import javax.xml.transform.stream.StreamSource;4041import org.testng.annotations.DataProvider;42import org.testng.annotations.Test;43import static org.testng.Assert.assertEquals;4445public class XSLTFunctionsTest {4647/**48* bug 806251849* Verifies that a reference to the DTM created by XSLT document function is50* actually read from the DTM by an extension function.51* @param xml Content of xml file to process52* @param xsl stylesheet content that loads external document {@code externalDoc}53* with XSLT 'document' function and then reads it with54* DocumentExtFunc.test() function55* @param externalDoc Content of the external xml document56* @param expectedResult Expected transformation result57**/58@Test(dataProvider = "document")59public void testDocument(final String xml, final String xsl,60final String externalDoc, final String expectedResult) throws Exception {61// Prepare sources for transormation62Source src = new StreamSource(new StringReader(xml));63Source xslsrc = new StreamSource(new StringReader(xsl));6465// Create factory and transformer66TransformerFactory tf = TransformerFactory.newInstance();67Transformer t = tf.newTransformer( xslsrc );68t.setErrorListener(tf.getErrorListener());6970// Set URI Resolver to return the newly constructed xml71// stream source object from xml test string72t.setURIResolver(new URIResolver() {73@Override74public Source resolve(String href, String base)75throws TransformerException {76if (href.contains("externalDoc")) {77return new StreamSource(new StringReader(externalDoc));78} else {79return new StreamSource(new StringReader(xml));80}81}82});8384// Prepare output stream85StringWriter xmlResultString = new StringWriter();86StreamResult xmlResultStream = new StreamResult(xmlResultString);8788//Transform the xml89t.transform(src, xmlResultStream);9091// If the document can't be accessed and the bug is in place then92// reported exception will be thrown during transformation93System.out.println("Transformation result:"+xmlResultString.toString().trim());9495// Check the result - it should contain two (node name, node values) entries -96// one for original document, another for a document created with97// call to 'document' function98assertEquals(xmlResultString.toString().trim(), expectedResult);99}100101@DataProvider(name = "document")102public static Object[][] documentTestData() {103return new Object[][] {104// 8062518105{documentTestXml, documentTestXsl, documentTestExternalDoc, documentTesteExpectedResult},106// 8133924107{documentTestXml, nonExistingNodeXsl, documentTestExternalDoc, nonExistNodeExpectedResult},108};109}110111// bug 8133924 xsl: test data to trigger the NPE when non-existing node is specified in xsl112static final String nonExistingNodeXsl = "<?xml version='1.0' encoding=\"UTF-8\"?>"113+ "<xsl:transform xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\""114+ " xmlns:set=\"http://exslt.org/sets\""115+ " exclude-result-prefixes=\"set\">"116+ " <xsl:template match=\"/\">"117+ " <xsl:copy-of select=\"set:leading(/Test, /Test/non-existing)\"/>"118+ "</xsl:template>"119+ "</xsl:transform>";120121//For bug 8133924 xsl the empty transformation result is expected instead of NPE122static final String nonExistNodeExpectedResult = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"123+ "<Test>Doc</Test>";124125static final String documentTestXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Test>Doc</Test>";126127static final String documentTestExternalDoc = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Test>External Doc</Test>";128129static final String documentTestXsl = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"130+ "<xsl:transform version=\"1.0\""131+ " xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\""132+ " xmlns:cfunc=\"http://xml.apache.org/xalan/java/\">"133+ "<xsl:template match=\"/\">"134+ "<xsl:element name=\"root\">"135+ "<xsl:variable name=\"other_doc\" select=\"document('externalDoc')\"/>"136+ "<!-- Source -->"137+ "<xsl:value-of select=\"cfunc:DocumentExtFunc.test(/Test)\"/>"138+ "<!-- document() -->"139+ "<xsl:value-of select=\"cfunc:DocumentExtFunc.test($other_doc/Test)\"/>"140+ "</xsl:element></xsl:template></xsl:transform>";141142static final String documentTesteExpectedResult = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"143+ "<root>[Test:Doc][Test:External Doc]</root>";144}145146147