< prev index next >

test/javax/xml/jaxp/unittest/transform/XSLTFunctionsTest.java

Print this page




  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);


 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 


  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.Assert;
  38 import org.testng.annotations.DataProvider;
  39 import org.testng.annotations.Listeners;
  40 import org.testng.annotations.Test;
  41 
  42 import static org.testng.Assert.assertEquals;
  43 import static jaxp.library.JAXPTestUtilities.runWithAllPerm;
  44 import static jaxp.library.JAXPTestUtilities.clearSystemProperty;
  45 import static jaxp.library.JAXPTestUtilities.setSystemProperty;
  46 import static jaxp.library.JAXPTestUtilities.getSystemProperty;
  47 
  48 /*
  49  * @test
  50  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
  51  * @compile DocumentExtFunc.java
  52  * @run testng/othervm -DrunSecMngr=true transform.XSLTFunctionsTest
  53  * @run testng/othervm transform.XSLTFunctionsTest
  54  * @summary This class contains tests for XSLT functions.
  55  */
  56 
  57 @Listeners({jaxp.library.BasePolicy.class}) 
  58 public class XSLTFunctionsTest {
  59   
  60     /**
  61      * @bug 8161454
  62      * Verifies that the new / correct name is supported, as is the old / incorrect
  63      * one for compatibility
  64      */ 
  65     @Test 
  66     public void testNameChange() {
  67         
  68         boolean feature;
  69         TransformerFactory tf = TransformerFactory.newInstance();
  70         feature = tf.getFeature(ORACLE_ENABLE_EXTENSION_FUNCTION);
  71         System.out.println("Default setting: " + feature);
  72         // The default: true if no SecurityManager, false otherwise
  73         Assert.assertTrue(feature == getDefault());
  74         
  75         setSystemProperty(SP_ENABLE_EXTENSION_FUNCTION, getDefaultOpposite());
  76         tf = TransformerFactory.newInstance();
  77         feature = tf.getFeature(ORACLE_ENABLE_EXTENSION_FUNCTION);
  78         System.out.println("After setting " + SP_ENABLE_EXTENSION_FUNCTION + ": " + feature);
  79         clearSystemProperty(SP_ENABLE_EXTENSION_FUNCTION);
  80         // old/incorrect name is still supported
  81         Assert.assertTrue(feature != getDefault());
  82         
  83         setSystemProperty(SP_ENABLE_EXTENSION_FUNCTION_SPEC, getDefaultOpposite());
  84         tf = TransformerFactory.newInstance();
  85         feature = tf.getFeature(ORACLE_ENABLE_EXTENSION_FUNCTION);
  86         System.out.println("After setting " + SP_ENABLE_EXTENSION_FUNCTION_SPEC + ": " + feature);
  87         clearSystemProperty(SP_ENABLE_EXTENSION_FUNCTION_SPEC);
  88         // new/correct name is effective
  89         Assert.assertTrue(feature != getDefault());
  90     }
  91     
  92     final boolean isSecure;
  93     {
  94         String runSecMngr = getSystemProperty("runSecMngr");
  95         isSecure = runSecMngr != null && runSecMngr.equals("true");
  96     }
  97 
  98     // The default: true if no SecurityManager, false otherwise  
  99     private boolean getDefault() {
 100         if (isSecure) {
 101             return false;
 102         } else { 
 103             return true;
 104         }
 105     }
 106 
 107     // Gets a String value that is opposite to the default value
 108     private String getDefaultOpposite() {
 109         if (isSecure) {
 110             return "true";
 111         } else {
 112             return "false";
 113         }
 114     }
 115 
 116     /**
 117      * @bug 8062518 8153082
 118      * Verifies that a reference to the DTM created by XSLT document function is
 119      * actually read from the DTM by an extension function.
 120      * @param xml Content of xml file to process
 121      * @param xsl stylesheet content that loads external document {@code externalDoc}
 122      *        with XSLT 'document' function and then reads it with
 123      *        DocumentExtFunc.test() function
 124      * @param externalDoc Content of the external xml document
 125      * @param expectedResult Expected transformation result
 126      **/
 127     @Test(dataProvider = "document")
 128     public void testDocument(final String xml, final String xsl,
 129                              final String externalDoc, final String expectedResult) throws Exception {
 130         // Prepare sources for transormation
 131         Source src = new StreamSource(new StringReader(xml));
 132         Source xslsrc = new StreamSource(new StringReader(xsl));
 133 
 134         // Create factory and transformer
 135         TransformerFactory tf = TransformerFactory.newInstance();
 136         tf.setFeature(ORACLE_ENABLE_EXTENSION_FUNCTION, true);
 137         tf.setAttribute(EXTENSION_CLASS_LOADER, 
 138                 runWithAllPerm(() -> Thread.currentThread().getContextClassLoader()));
 139         Transformer t = tf.newTransformer( xslsrc );
 140         t.setErrorListener(tf.getErrorListener());
 141 
 142         // Set URI Resolver to return the newly constructed xml
 143         // stream source object from xml test string
 144         t.setURIResolver(new URIResolver() {
 145             @Override
 146             public Source resolve(String href, String base)
 147                     throws TransformerException {
 148                 if (href.contains("externalDoc")) {
 149                     return new StreamSource(new StringReader(externalDoc));
 150                 } else {
 151                     return new StreamSource(new StringReader(xml));
 152                 }
 153             }
 154         });
 155 
 156         // Prepare output stream
 157         StringWriter xmlResultString = new StringWriter();
 158         StreamResult xmlResultStream = new StreamResult(xmlResultString);


 179 
 180     static final String documentTestXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Test>Doc</Test>";
 181 
 182     static final String documentTestExternalDoc = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Test>External Doc</Test>";
 183 
 184     static final String documentTestXsl = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
 185             + "<xsl:transform version=\"1.0\""
 186             + " xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" "
 187             + " xmlns:cfunc=\"http://xml.apache.org/xalan/java/\">"
 188             + "<xsl:template match=\"/\">"
 189             + "<xsl:element name=\"root\">"
 190             + "<xsl:variable name=\"other_doc\" select=\"document('externalDoc')\"/>"
 191             + "<!-- Source -->"
 192             + "<xsl:value-of select=\"cfunc:transform.DocumentExtFunc.test(/Test)\"/>"
 193             + "<!-- document() -->"
 194             + "<xsl:value-of select=\"cfunc:transform.DocumentExtFunc.test($other_doc/Test)\"/>"
 195             + "</xsl:element></xsl:template></xsl:transform>";
 196 
 197     static final String documentTesteExpectedResult = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
 198                                                     + "<root>[Test:Doc][Test:External Doc]</root>";
 199 
 200     public static final String ORACLE_JAXP_PROPERTY_PREFIX =
 201         "http://www.oracle.com/xml/jaxp/properties/";
 202     /**
 203      * Feature enableExtensionFunctions
 204      */
 205     public static final String ORACLE_ENABLE_EXTENSION_FUNCTION =
 206             ORACLE_JAXP_PROPERTY_PREFIX + "enableExtensionFunctions";
 207     static final String SP_ENABLE_EXTENSION_FUNCTION = "javax.xml.enableExtensionFunctions";
 208     // This is the correct name by the spec
 209     static final String SP_ENABLE_EXTENSION_FUNCTION_SPEC = "jdk.xml.enableExtensionFunctions";
 210     private static final String EXTENSION_CLASS_LOADER = "jdk.xml.transform.extensionClassLoader";
 211 }
 212 
< prev index next >