/* * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package javax.xml.parsers.ptests; import static jaxp.library.JAXPTestUtilities.FILE_SEP; import static jaxp.library.JAXPTestUtilities.USER_DIR; import static jaxp.library.JAXPTestUtilities.failUnexpected; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; import org.xml.sax.HandlerBase; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; /** * Class contains the test cases for SAXParser API */ public class SAXParserTest { /** * Copy necessary dtd files to user directory before any test run. */ @BeforeTest protected void setup() throws IOException { TestUtils.copyFiles(TestUtils.XML_DIR, USER_DIR, "firstdtd.dtd"); } /** * Test case with FileInputStream null, parsing should fail and throw * IllegalArgumentException. * * @throws IllegalArgumentException */ @Test(expectedExceptions = IllegalArgumentException.class) public void testParse01() throws IllegalArgumentException { try { FileInputStream instream = null; HandlerBase handler = new HandlerBase(); SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser saxparser = spf.newSAXParser(); saxparser.parse(instream, handler); } catch (ParserConfigurationException | SAXException | IOException e) { failUnexpected(e); } } /** * Testcase with an error in xml file, parsing should fail and throw * SAXException. * * @throws SAXException */ @Test(expectedExceptions = SAXException.class) public void testParse02() throws SAXException { try { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser saxparser = null; try { saxparser = spf.newSAXParser(); } catch (SAXException e) { failUnexpected(e); } HandlerBase handler = new HandlerBase(); FileInputStream instream = new FileInputStream(new File(TestUtils.XML_DIR, "invalid.xml")); saxparser.parse(instream, handler); } catch (ParserConfigurationException | IOException e) { failUnexpected(e); } } /** * Testcase with a valid in xml file, parser should parse the xml document. */ @Test public void testParse03() { try { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser saxparser = spf.newSAXParser(); HandlerBase handler = new HandlerBase(); FileInputStream instream = new FileInputStream(new File(TestUtils.XML_DIR, "parsertest.xml")); saxparser.parse(instream, handler); } catch (ParserConfigurationException | IOException | SAXException e) { failUnexpected(e); } } /** * Testcase with valid input stream, parser should parse the xml document * successfully. */ @Test public void testParse04() { try { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser saxparser = spf.newSAXParser(); HandlerBase handler = new HandlerBase(); FileInputStream instream = new FileInputStream(new File(TestUtils.XML_DIR, "correct.xml")); saxparser.parse(instream, handler); } catch (ParserConfigurationException | SAXException | IOException e) { failUnexpected(e); } } /** * Testcase with valid input source, parser should parse the xml document * successfully. */ @Test public void testParse05() { try { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser saxparser = spf.newSAXParser(); HandlerBase handler = new HandlerBase(); String xmlPath1 = new File(TestUtils.XML_DIR).getAbsolutePath() + FILE_SEP; FileInputStream instream = new FileInputStream(new File(TestUtils.XML_DIR, "parsertest.xml")); saxparser.parse(instream, handler, "file:///" + xmlPath1); } catch (ParserConfigurationException | SAXException | IOException e) { failUnexpected(e); } } /** * Testcase with valid input source, parser should parse the xml document * successfully. */ @Test public void testParse06() { try { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser saxparser = spf.newSAXParser(); HandlerBase handler = new HandlerBase(); FileInputStream instream = new FileInputStream(new File(TestUtils.XML_DIR, "parsertest.xml")); saxparser.parse(instream, handler, null); } catch (ParserConfigurationException | SAXException | IOException e) { failUnexpected(e); } } /** * Testcase with uri null, parsing should fail and throw * IllegalArgumentException. * * @throws IllegalArgumentException */ @Test(expectedExceptions = IllegalArgumentException.class) public void testParse07() throws IllegalArgumentException { try { String uri = null; SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser saxparser = spf.newSAXParser(); HandlerBase handler = new HandlerBase(); saxparser.parse(uri, handler); } catch (ParserConfigurationException | SAXException | IOException e) { failUnexpected(e); } } /** * Testcase with non-existant uri, parsing should fail and throw * IOException. * * @throws SAXException * @throws IOException */ @Test(expectedExceptions = { SAXException.class, IOException.class }) public void testParse08() throws SAXException, IOException { try { String uri = " "; SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser saxparser = null; try { saxparser = spf.newSAXParser(); } catch (SAXException e) { failUnexpected(e); } HandlerBase handler = new HandlerBase(); saxparser.parse(uri, handler); } catch (ParserConfigurationException e) { failUnexpected(e); } } /** * Testcase with proper uri, parser should parse successfully. */ @Test public void testParse09() { try { File file = new File(TestUtils.XML_DIR, "correct.xml"); String Absolutepath = file.getAbsolutePath(); String newAbsolutePath = Absolutepath; if (File.separatorChar == '\\') newAbsolutePath = Absolutepath.replace('\\', '/'); String uri = "file:///" + newAbsolutePath; HandlerBase handler = null; SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser saxparser = spf.newSAXParser(); handler = new HandlerBase(); saxparser.parse(uri, handler); } catch (ParserConfigurationException | SAXException | IOException e) { failUnexpected(e); } } /** * Testcase with File null, parsing should fail and throw * IllegalArgumentException. * * @throws IllegalArgumentException */ @Test(expectedExceptions = IllegalArgumentException.class) public void testParse10() throws IllegalArgumentException { try { File file = null; SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser saxparser = spf.newSAXParser(); HandlerBase handler = new HandlerBase(); saxparser.parse(file, handler); } catch (ParserConfigurationException | SAXException | IOException e) { failUnexpected(e); } } /** * Testcase with empty string as File, parsing should fail and throw * SAXException. * * @throws SAXException */ @Test(expectedExceptions = SAXException.class) public void testParse11() throws SAXException { try { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser saxparser = null; try { saxparser = spf.newSAXParser(); } catch (SAXException e) { failUnexpected(e); } HandlerBase handler = new HandlerBase(); File file = new File(""); saxparser.parse(file, handler); } catch (ParserConfigurationException | IOException e) { failUnexpected(e); } } /** * Testcase with xml file that has errors parsing should fail and throw * SAXException. * * @throws SAXException */ @Test(expectedExceptions = SAXException.class) public void testParse12() throws SAXException { try { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser saxparser = null; try { saxparser = spf.newSAXParser(); } catch (SAXException e) { failUnexpected(e); } HandlerBase handler = new HandlerBase(); File file = new File(TestUtils.XML_DIR, "valid.xml"); saxparser.parse(file, handler); } catch (ParserConfigurationException | IOException e) { failUnexpected(e); } } /** * Testcase with xml file that has no errors Parser should successfully * parse the xml document. */ @Test public void testParse13() { try { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser saxparser = spf.newSAXParser(); HandlerBase handler = new HandlerBase(); File file = new File(TestUtils.XML_DIR, "correct.xml"); saxparser.parse(file, handler); } catch (ParserConfigurationException | SAXException | IOException e) { failUnexpected(e); } } /** * Testcase with input source null, parsing should fail and throw * IllegalArgumentException. * * @throws IllegalArgumentException */ @Test(expectedExceptions = IllegalArgumentException.class) public void testParse14() throws IllegalArgumentException { try { InputSource is = null; SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser saxparser = spf.newSAXParser(); HandlerBase handler = new HandlerBase(); saxparser.parse(is, handler); } catch (ParserConfigurationException | SAXException | IOException e) { failUnexpected(e); } } /** * Testcase with input source attached an invaild xml, parsing should fail * and throw SAXException. * * @throws SAXException */ @Test(expectedExceptions = SAXException.class) public void testParse15() throws SAXException { try { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser saxparser = null; try { saxparser = spf.newSAXParser(); } catch (SAXException e) { failUnexpected(e); } HandlerBase handler = new HandlerBase(); FileInputStream instream = new FileInputStream(new File(TestUtils.XML_DIR, "invalid.xml")); InputSource is = new InputSource(instream); saxparser.parse(is, handler); } catch (ParserConfigurationException | IOException e) { failUnexpected(e); } } /** * Testcase with input source attached an vaild xml, parser should * successfully parse the xml document. */ @Test public void testParse16() { try { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser saxparser = spf.newSAXParser(); HandlerBase handler = new HandlerBase(); FileInputStream instream = new FileInputStream(new File(TestUtils.XML_DIR, "correct.xml")); InputSource is = new InputSource(instream); saxparser.parse(is, handler); } catch (ParserConfigurationException | SAXException | IOException e) { failUnexpected(e); } } /** * Testcase with FileInputStream null, parsing should fail and throw * IllegalArgumentException. * * @throws IllegalArgumentException */ @Test(expectedExceptions = IllegalArgumentException.class) public void testParse17() throws IllegalArgumentException { try { FileInputStream instream = null; SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser saxparser = spf.newSAXParser(); DefaultHandler handler = new DefaultHandler(); saxparser.parse(instream, handler); } catch (ParserConfigurationException | SAXException | IOException e) { failUnexpected(e); } } /** * Testcase with an error in xml file, parsing should fail and throw * SAXException. * * @throws SAXException */ @Test(expectedExceptions = SAXException.class) public void testParse18() throws SAXException { try { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser saxparser = null; try { saxparser = spf.newSAXParser(); } catch (SAXException e) { failUnexpected(e); } DefaultHandler handler = new DefaultHandler(); FileInputStream instream = new FileInputStream(new File(TestUtils.XML_DIR, "invalid.xml")); saxparser.parse(instream, handler); } catch (ParserConfigurationException | IOException e) { failUnexpected(e); } } /** * Testcase with valid input stream, parser should parse the xml document * successfully. */ @Test public void testParse19() { try { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser saxparser = spf.newSAXParser(); DefaultHandler handler = new DefaultHandler(); FileInputStream instream = new FileInputStream(new File(TestUtils.XML_DIR, "parsertest.xml")); saxparser.parse(instream, handler); } catch (ParserConfigurationException | IOException | SAXException e) { failUnexpected(e); } } /** * Testcase with valid input stream, parser should parse the xml document * successfully. */ @Test public void testParse20() { try { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser saxparser = spf.newSAXParser(); DefaultHandler handler = new DefaultHandler(); FileInputStream instream = new FileInputStream(new File(TestUtils.XML_DIR, "correct.xml")); saxparser.parse(instream, handler); } catch (ParserConfigurationException | SAXException | IOException e) { failUnexpected(e); } } /** * Testcase with valid input source, parser should parse the xml document * successfully. */ @Test public void testParse21() { try { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser saxparser = spf.newSAXParser(); DefaultHandler handler = new DefaultHandler(); String xmlPath1 = new File(TestUtils.XML_DIR).getAbsolutePath() + FILE_SEP; FileInputStream instream = new FileInputStream(new File(TestUtils.XML_DIR, "parsertest.xml")); saxparser.parse(instream, handler, "file:///" + xmlPath1); } catch (ParserConfigurationException | SAXException | IOException e) { failUnexpected(e); } } /** * Testcase with valid input source, parser should parse the xml document * successfully. */ @Test public void testParse22() { try { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser saxparser = spf.newSAXParser(); DefaultHandler handler = new DefaultHandler(); FileInputStream instream = new FileInputStream(new File(TestUtils.XML_DIR, "parsertest.xml")); saxparser.parse(instream, handler, null); } catch (ParserConfigurationException | IOException | SAXException e) { failUnexpected(e); } } /** * Testcase with uri null, parsing should fail and throw * IllegalArgumentException. * * @throws IllegalArgumentException */ @Test(expectedExceptions = IllegalArgumentException.class) public void testParse23() throws IllegalArgumentException { try { String uri = null; SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser saxparser = spf.newSAXParser(); DefaultHandler handler = new DefaultHandler(); saxparser.parse(uri, handler); } catch (ParserConfigurationException | SAXException | IOException e) { failUnexpected(e); } } /** * Testcase with non-existant uri, parsing should fail and throw * SAXException or IOException. * * @throws SAXException * @throws IOException */ @Test(expectedExceptions = { SAXException.class, IOException.class }) public void testParse24() throws SAXException, IOException { try { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser saxparser = null; String uri = " "; try { saxparser = spf.newSAXParser(); } catch (SAXException e) { failUnexpected(e); } DefaultHandler handler = new DefaultHandler(); saxparser.parse(uri, handler); } catch (ParserConfigurationException e) { failUnexpected(e); } } /** * Testcase with proper uri, parser should parse successfully. */ @Test public void testParse25() { try { File file = new File(TestUtils.XML_DIR, "correct.xml"); String Absolutepath = file.getAbsolutePath(); String newAbsolutePath = Absolutepath; if (File.separatorChar == '\\') newAbsolutePath = Absolutepath.replace('\\', '/'); String uri = "file:///" + newAbsolutePath; SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser saxparser = spf.newSAXParser(); DefaultHandler handler = new DefaultHandler(); saxparser.parse(uri, handler); } catch (ParserConfigurationException | SAXException | IOException e) { failUnexpected(e); } } /** * Testcase with File null, parsing should fail and throw * IllegalArgumentException. * * @throws IllegalArgumentException */ @Test(expectedExceptions = IllegalArgumentException.class) public void testParse26() throws IllegalArgumentException { try { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser saxparser = spf.newSAXParser(); DefaultHandler handler = new DefaultHandler(); saxparser.parse((File) null, handler); } catch (ParserConfigurationException | SAXException | IOException e) { failUnexpected(e); } } /** * Testcase with empty string as File, parsing should fail and throw * SAXException. * * @throws SAXException */ @Test(expectedExceptions = SAXException.class) public void testParse27() throws SAXException { try { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser saxparser = null; try { saxparser = spf.newSAXParser(); } catch (SAXException e) { failUnexpected(e); } DefaultHandler handler = new DefaultHandler(); File file = new File(""); saxparser.parse(file, handler); } catch (ParserConfigurationException | IOException e) { failUnexpected(e); } } /** * Testcase with xml file that has errors, parsing should fail and throw * SAXException. * * @throws SAXException */ @Test(expectedExceptions = SAXException.class) public void testParse28() throws SAXException { try { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser saxparser = null; try { saxparser = spf.newSAXParser(); } catch (SAXException e) { failUnexpected(e); } DefaultHandler handler = new DefaultHandler(); File file = new File(TestUtils.XML_DIR, "valid.xml"); saxparser.parse(file, handler); } catch (ParserConfigurationException | IOException e) { failUnexpected(e); } } /** * Testcase with xml file that has no errors, parser should successfully * parse the xml document. */ @Test public void testParse29() { try { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser saxparser = spf.newSAXParser(); DefaultHandler handler = new DefaultHandler(); File file = new File(TestUtils.XML_DIR, "correct.xml"); saxparser.parse(file, handler); } catch (ParserConfigurationException | SAXException | IOException e) { failUnexpected(e); } } /** * Testcase with input source null, parsing should fail and throw * IllegalArgumentException. * * @throws IllegalArgumentException */ @Test(expectedExceptions = IllegalArgumentException.class) public void testParse30() throws IllegalArgumentException { try { InputSource is = null; SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser saxparser = spf.newSAXParser(); DefaultHandler handler = new DefaultHandler(); saxparser.parse(is, handler); } catch (ParserConfigurationException | SAXException | IOException e) { failUnexpected(e); } } /** * Testcase with an invalid xml file, parser should throw SAXException. * * @throws SAXException */ @Test(expectedExceptions = SAXException.class) public void testParse31() throws SAXException { try { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser saxparser = null; try { saxparser = spf.newSAXParser(); } catch (SAXException e) { failUnexpected(e); } DefaultHandler handler = new DefaultHandler(); FileInputStream instream = new FileInputStream(new File(TestUtils.XML_DIR, "invalid.xml")); InputSource is = new InputSource(instream); saxparser.parse(is, handler); } catch (ParserConfigurationException | IOException e) { failUnexpected(e); } } /** * Test case to parse an xml file that not use namespaces. */ @Test public void testParse32() { try { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser saxparser = spf.newSAXParser(); DefaultHandler handler = new DefaultHandler(); FileInputStream instream = new FileInputStream(new File(TestUtils.XML_DIR, "correct.xml")); InputSource is = new InputSource(instream); saxparser.parse(is, handler); } catch (ParserConfigurationException | SAXException | IOException e) { failUnexpected(e); } } /** * Test case to parse an xml file that uses namespaces. */ @Test public void testParse33() { try { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); SAXParser saxparser = spf.newSAXParser(); HandlerBase handler = new HandlerBase(); FileInputStream instream = new FileInputStream(new File(TestUtils.XML_DIR, "ns4.xml")); saxparser.parse(instream, handler); } catch (ParserConfigurationException | SAXException | IOException e) { failUnexpected(e); } } }