< prev index next >

test/javax/xml/jaxp/functional/javax/xml/transform/ptests/SAXSourceTest01.java

Print this page

        

*** 23,104 **** package javax.xml.transform.ptests; import java.io.File; import java.io.FileInputStream; ! import java.io.FileNotFoundException; import java.io.IOException; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.dom.DOMSource; import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR; import javax.xml.transform.sax.SAXSource; import javax.xml.transform.stream.StreamSource; ! import static jaxp.library.JAXPTestUtilities.failUnexpected; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertNull; import org.testng.annotations.Test; import org.xml.sax.InputSource; import org.xml.sax.SAXException; /** * Unit test for SAXSource sourceToInputSource API. */ ! public class SAXSourceTest01 { /** ! * Test file name */ private final String TEST_FILE = XML_DIR + "cities.xsl"; /** * Test obtaining a SAX InputSource object from a Source object. */ ! @Test ! public void source2inputsource01() { ! try { ! StreamSource streamSource = new StreamSource ( ! new FileInputStream (TEST_FILE)); assertNotNull(SAXSource.sourceToInputSource(streamSource)); - } catch (FileNotFoundException ex) { - failUnexpected(ex); } } /** * This test case tries to get InputSource from DOMSource using * sourceToInputSource method. It is not possible and hence null is ! * expected. This is a negative test case */ ! @Test ! public void source2inputsource02() { ! try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); dbf.newDocumentBuilder().parse(new File(TEST_FILE)); assertNull(SAXSource.sourceToInputSource(new DOMSource(null))); - } catch (ParserConfigurationException | SAXException | IOException ex) { - failUnexpected(ex); - } - } /** * This test case tries to get InputSource from SAXSource using * sourceToInputSource method. This will also check if the systemId * remained the same. This is a positive test case. */ ! @Test ! public void source2inputsource03() { String SYSTEM_ID = "file:///" + XML_DIR; ! try { SAXSource saxSource = ! new SAXSource(new InputSource(new FileInputStream(TEST_FILE))); saxSource.setSystemId(SYSTEM_ID); assertEquals(SAXSource.sourceToInputSource(saxSource).getSystemId(), SYSTEM_ID); - } catch (FileNotFoundException ex) { - failUnexpected(ex); } } } --- 23,124 ---- package javax.xml.transform.ptests; import java.io.File; import java.io.FileInputStream; ! import java.io.FilePermission; import java.io.IOException; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.dom.DOMSource; import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR; import javax.xml.transform.sax.SAXSource; import javax.xml.transform.stream.StreamSource; ! import jaxp.library.JAXPBaseTest; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertNull; + import org.testng.annotations.AfterGroups; + import org.testng.annotations.BeforeGroups; import org.testng.annotations.Test; import org.xml.sax.InputSource; import org.xml.sax.SAXException; /** * Unit test for SAXSource sourceToInputSource API. */ ! public class SAXSourceTest01 extends JAXPBaseTest { /** ! * Test style-sheet file name */ private final String TEST_FILE = XML_DIR + "cities.xsl"; /** + * Save system property for restoring. + */ + @BeforeGroups (groups = {"readLocalFiles"}) + public void setFilePermissions() { + setPermissions(new FilePermission(TEST_FILE, "read")); + } + + /** + * Restore the system property. + */ + @AfterGroups (groups = {"readLocalFiles"}) + public void restoreFilePermissions() { + setPermissions(); + } + + /** * Test obtaining a SAX InputSource object from a Source object. + * + * @throws IOException reading file error. */ ! @Test(groups = {"readLocalFiles"}) ! public void source2inputsource01() throws IOException { ! try (FileInputStream fis = new FileInputStream(TEST_FILE)) { ! StreamSource streamSource = new StreamSource(fis); assertNotNull(SAXSource.sourceToInputSource(streamSource)); } } /** * This test case tries to get InputSource from DOMSource using * sourceToInputSource method. It is not possible and hence null is ! * expected. This is a negative test case, ! * ! * @throws ParserConfigurationException if a DocumentBuilder cannot be ! * created which satisfies the configuration requested. ! * @throws SAXException If any parse errors occur. ! * @throws IOException if the file exists but is a directory rather than ! * a regular file, does not exist but cannot be created, or cannot ! * be opened for any other reason. */ ! @Test(groups = {"readLocalFiles"}) ! public void source2inputsource02() throws ParserConfigurationException, ! SAXException, IOException { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); dbf.newDocumentBuilder().parse(new File(TEST_FILE)); assertNull(SAXSource.sourceToInputSource(new DOMSource(null))); } /** * This test case tries to get InputSource from SAXSource using * sourceToInputSource method. This will also check if the systemId * remained the same. This is a positive test case. + * + * @throws IOException reading file error. */ ! @Test(groups = {"readLocalFiles"}) ! public void source2inputsource03() throws IOException { String SYSTEM_ID = "file:///" + XML_DIR; ! try (FileInputStream fis = new FileInputStream(TEST_FILE)) { SAXSource saxSource = ! new SAXSource(new InputSource(fis)); saxSource.setSystemId(SYSTEM_ID); assertEquals(SAXSource.sourceToInputSource(saxSource).getSystemId(), SYSTEM_ID); } } }
< prev index next >