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