1 /* 2 * Copyright (c) 2014, 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 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 } | 1 /* 2 * Copyright (c) 2003, 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 javax.xml.transform.ptests; 25 26 import java.io.File; 27 import java.io.FileInputStream; 28 import java.io.IOException; 29 import javax.xml.parsers.DocumentBuilderFactory; 30 import javax.xml.transform.dom.DOMSource; 31 import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR; 32 import javax.xml.transform.sax.SAXSource; 33 import javax.xml.transform.stream.StreamSource; 34 import jaxp.library.JAXPFileReadOnlyBaseTest; 35 import static org.testng.Assert.assertEquals; 36 import static org.testng.Assert.assertNotNull; 37 import static org.testng.Assert.assertNull; 38 import org.testng.annotations.Test; 39 import org.xml.sax.InputSource; 40 41 42 /** 43 * Unit test for SAXSource sourceToInputSource API. 44 */ 45 public class SAXSourceTest extends JAXPFileReadOnlyBaseTest { 46 /** 47 * Test style-sheet file name 48 */ 49 private final String TEST_FILE = XML_DIR + "cities.xsl"; 50 51 /** 52 * Test obtaining a SAX InputSource object from a Source object. 53 * 54 * @throws IOException reading file error. 55 */ 56 @Test(groups = {"readLocalFiles"}) 57 public void source2inputsource01() throws IOException { 58 try (FileInputStream fis = new FileInputStream(TEST_FILE)) { 59 StreamSource streamSource = new StreamSource(fis); 60 assertNotNull(SAXSource.sourceToInputSource(streamSource)); 61 } 62 } 63 64 /** 65 * This test case tries to get InputSource from DOMSource using 66 * sourceToInputSource method. It is not possible and hence null is 67 * expected. This is a negative test case, 68 * 69 * @throws Exception If any errors occur. 70 */ 71 @Test(groups = {"readLocalFiles"}) 72 public void source2inputsource02() throws Exception { 73 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 74 dbf.setNamespaceAware(true); 75 dbf.newDocumentBuilder().parse(new File(TEST_FILE)); 76 assertNull(SAXSource.sourceToInputSource(new DOMSource(null))); 77 } 78 79 /** 80 * This test case tries to get InputSource from SAXSource using 81 * sourceToInputSource method. This will also check if the systemId 82 * remained the same. This is a positive test case. 83 * 84 * @throws IOException reading file error. 85 */ 86 @Test(groups = {"readLocalFiles"}) 87 public void source2inputsource03() throws IOException { 88 String SYSTEM_ID = "file:///" + XML_DIR; 89 try (FileInputStream fis = new FileInputStream(TEST_FILE)) { 90 SAXSource saxSource = 91 new SAXSource(new InputSource(fis)); 92 saxSource.setSystemId(SYSTEM_ID); 93 assertEquals(SAXSource.sourceToInputSource(saxSource).getSystemId(), 94 SYSTEM_ID); 95 } 96 } 97 } |