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