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