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 package javax.xml.transform;
  25 
  26 import java.io.StringReader;
  27 import java.io.StringWriter;
  28 
  29 import javax.xml.transform.stream.StreamResult;
  30 import javax.xml.transform.stream.StreamSource;
  31 
  32 import org.testng.annotations.DataProvider;
  33 import org.testng.annotations.Test;
  34 import static org.testng.Assert.assertEquals;
  35 
  36 /*
  37  * @summary This class contains tests for XSLT functions.
  38  */
  39 
  40 public class XSLTFunctionsTest {
  41 
  42     /**
  43      * @bug 8062518
  44      * Verifies that a reference to the DTM created by XSLT document function is
  45      * actually read from the DTM by an extension function.
  46      * @param xml Content of xml file to process
  47      * @param xsl stylesheet content that loads external document {@code externalDoc}
  48      *        with XSLT 'document' function and then reads it with
  49      *        DocumentExtFunc.test() function
  50      * @param externalDoc Content of the external xml document
  51      * @param expectedResult Expected transformation result
  52      **/
  53     @Test(dataProvider = "document")
  54     public void testDocument(final String xml, final String xsl,
  55                              final String externalDoc, final String expectedResult) throws Exception {
  56         // Prepare sources for transormation
  57         Source src = new StreamSource(new StringReader(xml));
  58         Source xslsrc = new StreamSource(new StringReader(xsl));
  59 
  60         // Create factory and transformer
  61         TransformerFactory tf = TransformerFactory.newInstance();
  62         Transformer t = tf.newTransformer( xslsrc );
  63         t.setErrorListener(tf.getErrorListener());
  64 
  65         // Set URI Resolver to return the newly constructed xml
  66         // stream source object from xml test string
  67         t.setURIResolver(new URIResolver() {
  68             @Override
  69             public Source resolve(String href, String base)
  70                     throws TransformerException {
  71                 if (href.contains("externalDoc")) {
  72                     return new StreamSource(new StringReader(externalDoc));
  73                 } else {
  74                     return new StreamSource(new StringReader(xml));
  75                 }
  76             }
  77         });
  78 
  79         // Prepare output stream
  80         StringWriter xmlResultString = new StringWriter();
  81         StreamResult xmlResultStream = new StreamResult(xmlResultString);
  82 
  83         //Transform the xml
  84         t.transform(src, xmlResultStream);
  85 
  86         // If the document can't be accessed and the bug is in place then
  87         // reported exception will be thrown during transformation
  88         System.out.println("Transformation result:"+xmlResultString.toString().trim());
  89 
  90         // Check the result - it should contain two (node name, node values) entries -
  91         // one for original document, another for a document created with
  92         // call to 'document' function
  93         assertEquals(xmlResultString.toString().trim(), expectedResult);
  94     }
  95 
  96     @DataProvider(name = "document")
  97     public static Object[][] documentTestData() {
  98         return new Object[][] {
  99             {documentTestXml, documentTestXsl, documentTestExternalDoc, documentTesteExpectedResult},
 100         };
 101     }
 102 
 103     static final String documentTestXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Test>Doc</Test>";
 104 
 105     static final String documentTestExternalDoc = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Test>External Doc</Test>";
 106 
 107     static final String documentTestXsl = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
 108             + "<xsl:transform version=\"1.0\""
 109             + " xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" "
 110             + " xmlns:cfunc=\"http://xml.apache.org/xalan/java/\">"
 111             + "<xsl:template match=\"/\">"
 112             + "<xsl:element name=\"root\">"
 113             + "<xsl:variable name=\"other_doc\" select=\"document('externalDoc')\"/>"
 114             + "<!-- Source -->"
 115             + "<xsl:value-of select=\"cfunc:javax.xml.transform.DocumentExtFunc.test(/Test)\"/>"
 116             + "<!-- document() -->"
 117             + "<xsl:value-of select=\"cfunc:javax.xml.transform.DocumentExtFunc.test($other_doc/Test)\"/>"
 118             + "</xsl:element></xsl:template></xsl:transform>";
 119 
 120     static final String documentTesteExpectedResult = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
 121                                                     + "<root>[Test:Doc][Test:External Doc]</root>";
 122 }