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