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