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 javax.xml.transform.ptests; 25 26 import java.io.File; 27 import java.io.FileInputStream; 28 import java.io.FileNotFoundException; 29 import java.io.IOException; 30 import javax.xml.parsers.DocumentBuilderFactory; 31 import javax.xml.parsers.ParserConfigurationException; 32 import javax.xml.transform.dom.DOMSource; 33 import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR; 34 import javax.xml.transform.sax.SAXSource; 35 import javax.xml.transform.stream.StreamSource; 36 import static jaxp.library.JAXPTestUtilities.failUnexpected; 37 import static org.testng.Assert.assertEquals; 38 import static org.testng.Assert.assertNotNull; 39 import static org.testng.Assert.assertNull; 40 import org.testng.annotations.Test; 41 import org.xml.sax.InputSource; 42 import org.xml.sax.SAXException; 43 44 45 /** 46 * Unit test for SAXSource sourceToInputSource API. 47 */ 48 public class SAXSourceTest01 { 49 /** 50 * Test file name 51 */ 52 private final String TEST_FILE = XML_DIR + "cities.xsl"; 53 54 /** 55 * Test obtaining a SAX InputSource object from a Source object. 56 */ 57 @Test 58 public void source2inputsource01() { 59 try { 60 StreamSource streamSource = new StreamSource ( 61 new FileInputStream (TEST_FILE)); 62 assertNotNull(SAXSource.sourceToInputSource(streamSource)); 63 } catch (FileNotFoundException ex) { 64 failUnexpected(ex); 65 } 66 } 67 68 /** 69 * This test case tries to get InputSource from DOMSource using 70 * sourceToInputSource method. It is not possible and hence null is 71 * expected. This is a negative test case 72 */ 73 @Test 74 public void source2inputsource02() { 75 try { 76 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 77 dbf.setNamespaceAware(true); 78 dbf.newDocumentBuilder().parse(new File(TEST_FILE)); 79 assertNull(SAXSource.sourceToInputSource(new DOMSource(null))); 80 } catch (ParserConfigurationException | SAXException | IOException ex) { 81 failUnexpected(ex); 82 } 83 84 } 85 86 /** 87 * This test case tries to get InputSource from SAXSource using 88 * sourceToInputSource method. This will also check if the systemId 89 * remained the same. This is a positive test case. 90 */ 91 @Test 92 public void source2inputsource03() { 93 String SYSTEM_ID = "file:///" + XML_DIR; 94 try { 95 SAXSource saxSource = 96 new SAXSource(new InputSource(new FileInputStream(TEST_FILE))); 97 saxSource.setSystemId(SYSTEM_ID); 98 assertEquals(SAXSource.sourceToInputSource(saxSource).getSystemId(), 99 SYSTEM_ID); 100 } catch (FileNotFoundException ex) { 101 failUnexpected(ex); 102 } 103 } 104 } | 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 javax.xml.transform.ptests; 25 26 import java.io.File; 27 import java.io.FileInputStream; 28 import java.io.FilePermission; 29 import java.io.IOException; 30 import javax.xml.parsers.DocumentBuilderFactory; 31 import javax.xml.parsers.ParserConfigurationException; 32 import javax.xml.transform.dom.DOMSource; 33 import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR; 34 import javax.xml.transform.sax.SAXSource; 35 import javax.xml.transform.stream.StreamSource; 36 import jaxp.library.JAXPBaseTest; 37 import static org.testng.Assert.assertEquals; 38 import static org.testng.Assert.assertNotNull; 39 import static org.testng.Assert.assertNull; 40 import org.testng.annotations.AfterGroups; 41 import org.testng.annotations.BeforeGroups; 42 import org.testng.annotations.Test; 43 import org.xml.sax.InputSource; 44 import org.xml.sax.SAXException; 45 46 47 /** 48 * Unit test for SAXSource sourceToInputSource API. 49 */ 50 public class SAXSourceTest01 extends JAXPBaseTest { 51 /** 52 * Test style-sheet file name 53 */ 54 private final String TEST_FILE = XML_DIR + "cities.xsl"; 55 56 /** 57 * Save system property for restoring. 58 */ 59 @BeforeGroups (groups = {"readLocalFiles"}) 60 public void setFilePermissions() { 61 setPermissions(new FilePermission(TEST_FILE, "read")); 62 } 63 64 /** 65 * Restore the system property. 66 */ 67 @AfterGroups (groups = {"readLocalFiles"}) 68 public void restoreFilePermissions() { 69 setPermissions(); 70 } 71 72 /** 73 * Test obtaining a SAX InputSource object from a Source object. 74 * 75 * @throws IOException reading file error. 76 */ 77 @Test(groups = {"readLocalFiles"}) 78 public void source2inputsource01() throws IOException { 79 try (FileInputStream fis = new FileInputStream(TEST_FILE)) { 80 StreamSource streamSource = new StreamSource(fis); 81 assertNotNull(SAXSource.sourceToInputSource(streamSource)); 82 } 83 } 84 85 /** 86 * This test case tries to get InputSource from DOMSource using 87 * sourceToInputSource method. It is not possible and hence null is 88 * expected. This is a negative test case, 89 * 90 * @throws ParserConfigurationException if a DocumentBuilder cannot be 91 * created which satisfies the configuration requested. 92 * @throws SAXException If any parse errors occur. 93 * @throws IOException if the file exists but is a directory rather than 94 * a regular file, does not exist but cannot be created, or cannot 95 * be opened for any other reason. 96 */ 97 @Test(groups = {"readLocalFiles"}) 98 public void source2inputsource02() throws ParserConfigurationException, 99 SAXException, IOException { 100 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 101 dbf.setNamespaceAware(true); 102 dbf.newDocumentBuilder().parse(new File(TEST_FILE)); 103 assertNull(SAXSource.sourceToInputSource(new DOMSource(null))); 104 } 105 106 /** 107 * This test case tries to get InputSource from SAXSource using 108 * sourceToInputSource method. This will also check if the systemId 109 * remained the same. This is a positive test case. 110 * 111 * @throws IOException reading file error. 112 */ 113 @Test(groups = {"readLocalFiles"}) 114 public void source2inputsource03() throws IOException { 115 String SYSTEM_ID = "file:///" + XML_DIR; 116 try (FileInputStream fis = new FileInputStream(TEST_FILE)) { 117 SAXSource saxSource = 118 new SAXSource(new InputSource(fis)); 119 saxSource.setSystemId(SYSTEM_ID); 120 assertEquals(SAXSource.sourceToInputSource(saxSource).getSystemId(), 121 SYSTEM_ID); 122 } 123 } 124 } |