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