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