1 /*
   2  * Copyright (c) 2014, 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.File;
  27 
  28 import javax.xml.transform.Source;
  29 import javax.xml.transform.Transformer;
  30 import javax.xml.transform.TransformerFactory;
  31 import javax.xml.transform.dom.DOMResult;
  32 import javax.xml.transform.sax.SAXResult;
  33 import javax.xml.transform.stax.StAXResult;
  34 import javax.xml.transform.stream.StreamResult;
  35 
  36 import org.testng.Assert;
  37 import org.testng.annotations.AfterMethod;
  38 import org.testng.annotations.BeforeMethod;
  39 import org.testng.annotations.Listeners;
  40 import org.testng.annotations.Test;
  41 
  42 import transform.util.DOMUtil;
  43 import transform.util.SAXUtil;
  44 import transform.util.StAXUtil;
  45 import transform.util.StreamUtil;
  46 
  47 /*
  48  * @test
  49  * @bug 4892774
  50  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
  51  * @run testng/othervm -DrunSecMngr=true transform.Bug4892774
  52  * @run testng/othervm transform.Bug4892774
  53  * @summary Test identity transformer with all possible types of Source and Result combinations for doucment version and encoding information.
  54  */
  55 
  56 @Listeners({jaxp.library.FilePolicy.class})
  57 public class Bug4892774 {
  58 
  59     private final String XML_FILE = "catalog.xml";
  60     private final String XML10_FILE = "catalog_10.xml"; // 1.0 version document
  61     private final String TEMP_FILE = "tmp.xml";
  62     private final String EXPECTED_VERSION = "1.1";
  63     static private Transformer idTransform = null;
  64 
  65     private static DOMUtil domUtil = null;
  66     private static StreamUtil streamUtil = null;
  67     private static SAXUtil saxUtil = null;
  68     private static StAXUtil staxUtil = null;
  69 
  70     @BeforeMethod
  71     public void setUp() {
  72         File tmpFile = new File(TEMP_FILE);
  73         if (tmpFile.exists())
  74             tmpFile.delete();
  75         try {
  76 
  77             if (idTransform == null)
  78                 idTransform = getIdTransformer();
  79             else
  80                 idTransform.reset();
  81 
  82             initializeUtils();
  83         } catch (Exception e) {
  84             e.printStackTrace();
  85             Assert.fail("Exception occured during setUp(): " + e.getMessage());
  86         }
  87     }
  88 
  89     @AfterMethod
  90     public void tearDown() {
  91         File tmpFile = new File(TEMP_FILE);
  92         if (tmpFile.exists())
  93             tmpFile.delete();
  94     }
  95 
  96     private void initializeUtils() throws Exception {
  97         if (domUtil == null)
  98             domUtil = (DOMUtil) TransformerUtilFactory.getUtil(TransformerUtilFactory.DOM);
  99         if (saxUtil == null)
 100             saxUtil = (SAXUtil) TransformerUtilFactory.getUtil(TransformerUtilFactory.SAX);
 101         if (streamUtil == null)
 102             streamUtil = (StreamUtil) TransformerUtilFactory.getUtil(TransformerUtilFactory.STREAM);
 103         if (staxUtil == null)
 104             staxUtil = (StAXUtil) TransformerUtilFactory.getUtil(TransformerUtilFactory.StAX);
 105     }
 106 
 107     @Test
 108     public void testDOM2DOM() {
 109         try {
 110             Source input = domUtil.prepareSource(this.getClass().getResourceAsStream(XML_FILE));
 111             DOMResult domResult = (DOMResult) domUtil.prepareResult();
 112             idTransform.transform(input, domResult);
 113             domUtil.checkResult(domResult, EXPECTED_VERSION);
 114         } catch (Exception e) {
 115             e.printStackTrace();
 116             Assert.fail("Exception occured: " + e.getMessage());
 117         }
 118     }
 119 
 120     private Transformer getIdTransformer() throws Exception {
 121         return TransformerFactory.newInstance().newTransformer();
 122     }
 123 
 124     @Test
 125     public void testDOM2Stream() {
 126         try {
 127 
 128             Source input = domUtil.prepareSource(this.getClass().getResourceAsStream(XML_FILE));
 129             StreamResult strResult = (StreamResult) streamUtil.prepareResult();
 130             idTransform.transform(input, strResult);
 131             streamUtil.checkResult(strResult, EXPECTED_VERSION, "UTF-8");
 132 
 133         } catch (Exception e) {
 134             e.printStackTrace();
 135             Assert.fail("Exception occured: " + e.getMessage());
 136         }
 137     }
 138 
 139     @Test
 140     public void testDOM2SAX() {
 141         try {
 142             Source input = domUtil.prepareSource(this.getClass().getResourceAsStream(XML_FILE));
 143             SAXResult saxResult = (SAXResult) saxUtil.prepareResult();
 144             idTransform.transform(input, saxResult);
 145             saxUtil.checkResult(saxResult, EXPECTED_VERSION, "UTF-8");
 146 
 147         } catch (Exception e) {
 148             e.printStackTrace();
 149             Assert.fail("Exception occured: " + e.getMessage());
 150         }
 151     }
 152 
 153     @Test
 154     public void testDOM2StAX() {
 155         try {
 156             Source input = domUtil.prepareSource(this.getClass().getResourceAsStream(XML_FILE));
 157             StAXResult staxResult = (StAXResult) staxUtil.prepareResult();
 158             idTransform.transform(input, staxResult);
 159             staxUtil.checkResult(staxResult, EXPECTED_VERSION, "UTF-8");
 160 
 161         } catch (Exception e) {
 162             e.printStackTrace();
 163             Assert.fail("Exception occured: " + e.getMessage());
 164         }
 165     }
 166 
 167     @Test
 168     public void testDOM2StAXStream() {
 169         try {
 170             Source input = domUtil.prepareSource(this.getClass().getResourceAsStream(XML_FILE));
 171             StAXResult staxResult = (StAXResult) staxUtil.prepareStreamResult();
 172             idTransform.transform(input, staxResult);
 173             staxUtil.checkStreamResult(staxResult, EXPECTED_VERSION);
 174 
 175         } catch (Exception e) {
 176             e.printStackTrace();
 177             Assert.fail("Exception occured: " + e.getMessage());
 178         }
 179     }
 180 
 181     @Test
 182     public void testSAX2DOM() {
 183         try {
 184             Source input = saxUtil.prepareSource(this.getClass().getResourceAsStream(XML_FILE));
 185             DOMResult domResult = (DOMResult) domUtil.prepareResult();
 186             idTransform.transform(input, domResult);
 187             domUtil.checkResult(domResult, EXPECTED_VERSION);
 188 
 189         } catch (Exception e) {
 190             e.printStackTrace();
 191             Assert.fail("Exception occured: " + e.getMessage());
 192         }
 193     }
 194 
 195     @Test
 196     public void testSAX2SAX() {
 197         try {
 198             Source input = saxUtil.prepareSource(this.getClass().getResourceAsStream(XML_FILE));
 199             SAXResult saxResult = (SAXResult) saxUtil.prepareResult();
 200             idTransform.transform(input, saxResult);
 201             saxUtil.checkResult(saxResult, EXPECTED_VERSION, "UTF-8");
 202 
 203         } catch (Exception e) {
 204             e.printStackTrace();
 205             Assert.fail("Exception occured: " + e.getMessage());
 206         }
 207     }
 208 
 209     @Test
 210     public void testSAX2Stream() {
 211         try {
 212             Source input = saxUtil.prepareSource(this.getClass().getResourceAsStream(XML_FILE));
 213             StreamResult strResult = (StreamResult) streamUtil.prepareResult();
 214             idTransform.transform(input, strResult);
 215             streamUtil.checkResult(strResult, EXPECTED_VERSION, "UTF-8");
 216         } catch (Exception e) {
 217             e.printStackTrace();
 218             Assert.fail("Exception occured: " + e.getMessage());
 219         }
 220     }
 221 
 222     @Test
 223     public void testSAX2StAX() {
 224         try {
 225             Source input = saxUtil.prepareSource(this.getClass().getResourceAsStream(XML_FILE));
 226             StAXResult staxResult = (StAXResult) staxUtil.prepareResult();
 227             idTransform.transform(input, staxResult);
 228             staxUtil.checkResult(staxResult, EXPECTED_VERSION, "UTF-8");
 229         } catch (Exception e) {
 230             e.printStackTrace();
 231             Assert.fail("Exception occured: " + e.getMessage());
 232         }
 233     }
 234 
 235     @Test
 236     public void testSAX2StAXStream() {
 237         try {
 238             Source input = saxUtil.prepareSource(this.getClass().getResourceAsStream(XML_FILE));
 239             StAXResult staxResult = (StAXResult) staxUtil.prepareStreamResult();
 240             idTransform.transform(input, staxResult);
 241             staxUtil.checkStreamResult(staxResult, EXPECTED_VERSION);
 242 
 243         } catch (Exception e) {
 244             e.printStackTrace();
 245             Assert.fail("Exception occured: " + e.getMessage());
 246         }
 247     }
 248 
 249     @Test
 250     public void testStream2DOM() {
 251         try {
 252             Source input = streamUtil.prepareSource(this.getClass().getResourceAsStream(XML_FILE));
 253             DOMResult domResult = (DOMResult) domUtil.prepareResult();
 254             idTransform.transform(input, domResult);
 255             domUtil.checkResult(domResult, EXPECTED_VERSION);
 256         } catch (Exception e) {
 257             e.printStackTrace();
 258             Assert.fail("Exception occured: " + e.getMessage());
 259         }
 260     }
 261 
 262     @Test
 263     public void testStream2Stream() {
 264         try {
 265             Source input = streamUtil.prepareSource(this.getClass().getResourceAsStream(XML_FILE));
 266             StreamResult strResult = (StreamResult) streamUtil.prepareResult();
 267             idTransform.transform(input, strResult);
 268             streamUtil.checkResult(strResult, EXPECTED_VERSION, "UTF-8");
 269         } catch (Exception e) {
 270             e.printStackTrace();
 271             Assert.fail("Exception occured: " + e.getMessage());
 272         }
 273     }
 274 
 275     @Test
 276     public void testStream2Stax() {
 277         try {
 278             Source input = streamUtil.prepareSource(this.getClass().getResourceAsStream(XML_FILE));
 279             StAXResult staxResult = (StAXResult) staxUtil.prepareResult();
 280             idTransform.transform(input, staxResult);
 281             staxUtil.checkResult(staxResult, EXPECTED_VERSION, "UTF-8");
 282         } catch (Exception e) {
 283             e.printStackTrace();
 284             Assert.fail("Exception occured: " + e.getMessage());
 285         }
 286     }
 287 
 288     @Test
 289     public void testStream2StaxStream() {
 290         try {
 291             Source input = streamUtil.prepareSource(this.getClass().getResourceAsStream(XML_FILE));
 292             StAXResult staxResult = (StAXResult) staxUtil.prepareStreamResult();
 293             idTransform.transform(input, staxResult);
 294             staxUtil.checkStreamResult(staxResult, EXPECTED_VERSION);
 295 
 296         } catch (Exception e) {
 297             e.printStackTrace();
 298             Assert.fail("Exception occured: " + e.getMessage());
 299         }
 300     }
 301 
 302     @Test
 303     public void testStream2SAX() {
 304         try {
 305             Source input = streamUtil.prepareSource(this.getClass().getResourceAsStream(XML_FILE));
 306             SAXResult saxResult = (SAXResult) saxUtil.prepareResult();
 307             idTransform.transform(input, saxResult);
 308             saxUtil.checkResult(saxResult, EXPECTED_VERSION, "UTF-8");
 309         } catch (Exception e) {
 310             e.printStackTrace();
 311             Assert.fail("Exception occured: " + e.getMessage());
 312         }
 313     }
 314 
 315     @Test
 316     public void testStAX2DOM() {
 317         try {
 318             Source input = staxUtil.prepareStreamSource(this.getClass().getResourceAsStream(XML10_FILE));
 319             DOMResult domResult = (DOMResult) domUtil.prepareResult();
 320             idTransform.transform(input, domResult);
 321             domUtil.checkResult(domResult, "1.0");
 322         } catch (Exception e) {
 323             e.printStackTrace();
 324             Assert.fail("Exception occured: " + e.getMessage());
 325         }
 326     }
 327 
 328     @Test
 329     public void testStAX2Stream() {
 330         try {
 331             Source input = staxUtil.prepareStreamSource(this.getClass().getResourceAsStream(XML10_FILE));
 332             StreamResult strResult = (StreamResult) streamUtil.prepareResult();
 333             idTransform.transform(input, strResult);
 334             streamUtil.checkResult(strResult, "1.0", "UTF-8");
 335         } catch (Exception e) {
 336             e.printStackTrace();
 337             Assert.fail("Exception occured: " + e.getMessage());
 338         }
 339     }
 340 
 341     @Test
 342     public void testStAX2StAX() {
 343         try {
 344             Source input = staxUtil.prepareStreamSource(this.getClass().getResourceAsStream(XML10_FILE));
 345             StAXResult staxResult = (StAXResult) staxUtil.prepareResult();
 346             idTransform.transform(input, staxResult);
 347             staxUtil.checkResult(staxResult, "1.0", "UTF-8");
 348         } catch (Exception e) {
 349             e.printStackTrace();
 350             Assert.fail("Exception occured: " + e.getMessage());
 351         }
 352     }
 353 
 354     @Test
 355     public void testStAXEvent2DOM() {
 356         try {
 357             Source input = staxUtil.prepareSource(this.getClass().getResourceAsStream(XML10_FILE));
 358             DOMResult domResult = (DOMResult) domUtil.prepareResult();
 359             idTransform.transform(input, domResult);
 360             domUtil.checkResult(domResult, "1.0");
 361         } catch (Exception e) {
 362             e.printStackTrace();
 363             Assert.fail("Exception occured: " + e.getMessage());
 364         }
 365     }
 366 
 367     @Test
 368     public void testStAXEvent2Stream() {
 369         try {
 370             Source input = staxUtil.prepareSource(this.getClass().getResourceAsStream(XML10_FILE));
 371             StreamResult strResult = (StreamResult) streamUtil.prepareResult();
 372             idTransform.transform(input, strResult);
 373             streamUtil.checkResult(strResult, "1.0", "UTF-8");
 374         } catch (Exception e) {
 375             e.printStackTrace();
 376             Assert.fail("Exception occured: " + e.getMessage());
 377         }
 378     }
 379 }