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 package common;
  24 
  25 import java.io.ByteArrayInputStream;
  26 import java.io.File;
  27 import java.io.StringReader;
  28 import java.io.UnsupportedEncodingException;
  29 import java.net.URISyntaxException;
  30 import javax.xml.parsers.DocumentBuilderFactory;
  31 import javax.xml.parsers.ParserConfigurationException;
  32 import javax.xml.parsers.SAXParserFactory;
  33 import javax.xml.stream.XMLEventReader;
  34 import javax.xml.stream.XMLInputFactory;
  35 import javax.xml.stream.XMLStreamException;
  36 import javax.xml.stream.XMLStreamReader;
  37 import javax.xml.transform.Source;
  38 import javax.xml.transform.dom.DOMSource;
  39 import javax.xml.transform.sax.SAXSource;
  40 import javax.xml.transform.stax.StAXSource;
  41 import javax.xml.transform.stream.StreamSource;
  42 import org.testng.Assert;
  43 import org.testng.annotations.DataProvider;
  44 import org.testng.annotations.Test;
  45 import org.w3c.dom.Document;
  46 import org.xml.sax.InputSource;
  47 import org.xml.sax.SAXException;
  48 import org.xml.sax.XMLReader;
  49 
  50 /*
  51  * @bug 8144967
  52  * @summary Tests related to the javax.xml.transform.Source
  53  * and org.xml.sax.InputSource
  54  */
  55 public class Sources {
  56 
  57     /**
  58      * @bug 8144967
  59      * Tests whether a Source object is empty
  60      * @param source the Source object
  61      */
  62     @Test(dataProvider = "emptySources")
  63     public void testIsEmpty(Source source) {
  64         Assert.assertTrue(source.isEmpty(), "The source is not empty");
  65     }
  66 
  67     /**
  68      * @bug 8144967
  69      * Tests that the source is not empty
  70      * @param source the Source object
  71      */
  72     @Test(dataProvider = "nonEmptySources")
  73     public void testIsNotEmpty(Source source) {
  74         Assert.assertTrue(!source.isEmpty(), "The source is empty");
  75     }
  76 
  77     /**
  78      * @bug 8144967
  79      * Tests whether an InputSource object is empty
  80      * @param source the InputSource object
  81      */
  82     @Test(dataProvider = "emptyInputSource")
  83     public void testISIsEmpty(InputSource source) {
  84         Assert.assertTrue(source.isEmpty(), "The source is not empty");
  85     }
  86 
  87     /*
  88      * DataProvider: sources that are empty
  89      */
  90     @DataProvider(name = "emptySources")
  91     Object[][] getSources() throws URISyntaxException {
  92 
  93         return new Object[][]{
  94             {new DOMSource()},
  95             {new DOMSource(getDocument())},
  96             {new SAXSource()},
  97             {new SAXSource(new InputSource(new StringReader("")))},
  98             {new SAXSource(getXMLReader(), new InputSource(new StringReader("")))},
  99             {new StreamSource()},
 100             {new StreamSource(new ByteArrayInputStream("".getBytes()))},
 101             {new StreamSource(new StringReader(""))},
 102             {new StreamSource(new StringReader(""), null)},
 103             {new StreamSource((String) null)}
 104         };
 105     }
 106 
 107     /*
 108      * DataProvider: sources that are not empty
 109      */
 110     @DataProvider(name = "nonEmptySources")
 111     Object[][] getSourcesEx() throws URISyntaxException {
 112         StAXSource ss = null;
 113         try {
 114             ss = new StAXSource(getXMLEventReader());
 115         } catch (XMLStreamException ex) {}
 116 
 117         return new Object[][]{
 118             //This will set a non-null systemId on the resulting StreamSource
 119             {new StreamSource(new File(""))},
 120             //Can't tell because XMLStreamReader is a pull parser, cursor advancement
 121             //would have been required in order to examine the reader.
 122             {new StAXSource(getXMLStreamReader())},
 123             {ss}
 124         };
 125     }
 126 
 127     /*
 128      * DataProvider: sources that are empty
 129      */
 130     @DataProvider(name = "emptyInputSource")
 131     Object[][] getInputSources() throws URISyntaxException {
 132         byte[] utf8Bytes = null;
 133         try {
 134             utf8Bytes = "".getBytes("UTF8");
 135         } catch (UnsupportedEncodingException ex) {
 136             throw new RuntimeException(ex.getMessage());
 137         }
 138         return new Object[][]{
 139             {new InputSource()},
 140             {new InputSource(new ByteArrayInputStream(utf8Bytes))},
 141             {new InputSource(new StringReader(""))},
 142             {new InputSource((String) null)}
 143         };
 144     }
 145 
 146     /**
 147      * Returns an instance of Document.
 148      *
 149      * @return an instance of Document.
 150      */
 151     private Document getDocument() {
 152         Document doc = null;
 153         try {
 154             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 155             doc = dbf.newDocumentBuilder().newDocument();
 156         } catch (ParserConfigurationException ex) {}
 157         return doc;
 158     }
 159 
 160     /**
 161      * Returns an instance of XMLReader.
 162      *
 163      * @return an instance of XMLReader.
 164      */
 165     private XMLReader getXMLReader() {
 166         XMLReader reader = null;
 167         try {
 168             reader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
 169         } catch (ParserConfigurationException | SAXException ex) {}
 170         return reader;
 171     }
 172 
 173     /**
 174      * Returns an instance of XMLStreamReader.
 175      *
 176      * @return an instance of XMLStreamReader.
 177      */
 178     private XMLStreamReader getXMLStreamReader() {
 179         XMLStreamReader r = null;
 180         try {
 181             XMLInputFactory xif = XMLInputFactory.newInstance();
 182             r = xif.createXMLStreamReader(new ByteArrayInputStream("".getBytes()));
 183         } catch (XMLStreamException ex) {}
 184 
 185         return r;
 186     }
 187 
 188     /**
 189      * Returns an instance of XMLEventReader.
 190      *
 191      * @return an instance of XMLEventReader.
 192      */
 193     private XMLEventReader getXMLEventReader() {
 194         XMLEventReader r = null;
 195         try {
 196             r = XMLInputFactory.newInstance().createXMLEventReader(
 197                             new ByteArrayInputStream("".getBytes()));
 198         } catch (XMLStreamException ex) {}
 199 
 200         return r;
 201     }
 202 }