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.util;
  25 
  26 import java.io.File;
  27 import java.io.FileInputStream;
  28 import java.io.FileOutputStream;
  29 import java.io.InputStream;
  30 
  31 import javax.xml.parsers.DocumentBuilder;
  32 import javax.xml.parsers.SAXParser;
  33 import javax.xml.parsers.SAXParserFactory;
  34 import javax.xml.transform.Result;
  35 import javax.xml.transform.Source;
  36 import javax.xml.transform.stream.StreamResult;
  37 import javax.xml.transform.stream.StreamSource;
  38 
  39 import org.testng.Assert;
  40 import org.w3c.dom.Document;
  41 
  42 import transform.VersionDefaultHandler;
  43 
  44 public class StreamUtil extends TransformerUtil {
  45 
  46     DocumentBuilder docBuilder = null;
  47 
  48     private static StreamUtil instance = null;
  49 
  50     /** Creates a new instance of StreamUtil */
  51     private StreamUtil() throws Exception {
  52         if (docBuilder == null)
  53             docBuilder = getDomParser();
  54     }
  55 
  56     public static synchronized StreamUtil getInstance() throws Exception {
  57         if (instance == null)
  58             instance = new StreamUtil();
  59         return instance;
  60     }
  61 
  62     public Source prepareSource(InputStream is) throws Exception {
  63         return new StreamSource(is);
  64     }
  65 
  66     public Result prepareResult() throws Exception {
  67         FileOutputStream fos = new FileOutputStream(TEMP_FILE);
  68         return new StreamResult(fos);
  69     }
  70 
  71     public void checkResult(Result result, String inputVersion) throws Exception {
  72         ((StreamResult) result).getOutputStream().close();
  73         FileInputStream fis = new FileInputStream(TEMP_FILE);
  74         checkStream(fis, inputVersion);
  75     }
  76 
  77     public void checkStream(FileInputStream fis, String inputVersion) throws Exception {
  78         docBuilder.reset();
  79         Document output = docBuilder.parse(fis);
  80         String version = output.getXmlVersion();
  81         Assert.assertTrue(inputVersion.equals(version), "Expected XML Version is 1.1, but actual version " + version);
  82     }
  83 
  84     public void checkResult(Result result, String version, String encoding) throws Exception {
  85         // use sax parser, as encoding info cannot be set on DOM document
  86         SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
  87         VersionDefaultHandler dh = new VersionDefaultHandler();
  88         parser.parse(new File(TEMP_FILE), dh);
  89         Assert.assertTrue(dh.getVersion().equals(version), "Expected version is " + version + " actual version " + dh.getVersion());
  90         Assert.assertTrue(dh.getEncoding().equals(encoding), "Expected version is " + encoding + " actual version " + dh.getEncoding());
  91     }
  92 }