1 /*
   2  * Copyright (c) 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 common;
  25 
  26 import static jaxp.library.JAXPTestUtilities.setSystemProperty;
  27 
  28 import java.io.StringReader;
  29 import java.io.StringWriter;
  30 
  31 import javax.xml.transform.Source;
  32 import javax.xml.transform.Transformer;
  33 import javax.xml.transform.TransformerFactory;
  34 import javax.xml.transform.stream.StreamResult;
  35 import javax.xml.transform.stream.StreamSource;
  36 
  37 import org.testng.annotations.BeforeClass;
  38 import org.testng.annotations.Listeners;
  39 import org.testng.annotations.Test;
  40 
  41 /*
  42  * @test
  43  * @bug 8144593
  44  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
  45  * @compile -XDignore.symbol.file TestSAXDriver.java
  46  * @run testng/othervm -DrunSecMngr=true common.TransformationWarningsTest
  47  * @run testng/othervm common.TransformationWarningsTest
  48  * @summary Check that warnings about unsupported properties from parsers
  49  * are suppressed during the transformation process.
  50  */
  51 @Listeners({jaxp.library.BasePolicy.class, jaxp.library.InternalAPIPolicy.class})
  52 public class TransformationWarningsTest extends WarningsTestBase {
  53 
  54     @BeforeClass
  55     public void setup() {
  56         //Set test SAX driver implementation.
  57         setSystemProperty("org.xml.sax.driver", "common.TestSAXDriver");
  58     }
  59 
  60     @Test
  61     public void testTransformation() throws Exception {
  62         startTest();
  63     }
  64 
  65     //One iteration of xml transformation test case. It will be called from each
  66     //TestWorker task defined in WarningsTestBase class.
  67     void doOneTestIteration() throws Exception {
  68         // Prepare output stream
  69         StringWriter xmlResultString = new StringWriter();
  70         StreamResult xmlResultStream = new StreamResult(xmlResultString);
  71         // Prepare xml source stream
  72         Source src = new StreamSource(new StringReader(xml));
  73         Transformer t = createTransformer();
  74         //Transform the xml
  75         t.transform(src, xmlResultStream);
  76     }
  77 
  78     //Create transformer from xsl test string
  79     Transformer createTransformer() throws Exception {
  80         // Prepare sources for transormation
  81         Source xslsrc = new StreamSource(new StringReader(xsl));
  82 
  83         // Create factory and transformer
  84         TransformerFactory tf;
  85         // newTransformer() method doc states that different transformer
  86         // factories can be used concurrently by different Threads.
  87         synchronized (TransformerFactory.class) {
  88             tf = TransformerFactory.newInstance();
  89         }
  90         Transformer t = tf.newTransformer(xslsrc);
  91 
  92         // Set URI Resolver to return the newly constructed xml
  93         // stream source object from xml test string
  94         t.setURIResolver((String href, String base) -> new StreamSource(new StringReader(xml)));
  95         return t;
  96     }
  97 
  98     //Xsl and Xml contents used in the transformation test
  99     private static final String xsl = "<xsl:stylesheet version='2.0'"
 100             + " xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>"
 101             + " <xsl:output method='xml' indent='yes' omit-xml-declaration='yes'/>"
 102             + " <xsl:template match='/'>"
 103             + " <test>Simple Transformation Result. No warnings should be printed to console</test>"
 104             + " </xsl:template>"
 105             + "</xsl:stylesheet>";
 106     private static final String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><root></root>";
 107 }