jaxp/test/javax/xml/jaxp/functional/javax/xml/parsers/ptests/SAXParserTest03.java
author joehw
Thu, 15 Jan 2015 19:10:56 -0800
changeset 28445 5a87f52ca380
parent 27983 f5f19fe0e83b
child 40223 64662417aa2d
permissions -rw-r--r--
8051563: Update JAXP functional tests Reviewed-by: lancea, joehw Contributed-by: tristan.yan@oracle.com

/*
 * Copyright (c) 1999, 2015, 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 org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
import java.io.File;
import java.io.FilePermission;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import static javax.xml.parsers.ptests.ParserTestConst.XML_DIR;
import jaxp.library.JAXPFileReadOnlyBaseTest;
import static org.testng.Assert.fail;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.xml.sax.SAXException;

/**
 * Class contains the test cases for SAXParser API
 */
public class SAXParserTest03 extends JAXPFileReadOnlyBaseTest {

    /**
     * Provide SAXParserFactory.
     *
     * @return a dimensional contains.
     */
    @DataProvider(name = "input-provider")
    public Object[][] getFactory() {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setValidating(true);
        return new Object[][] { { spf, MyErrorHandler.newInstance() } };
    }

    /**
     * parsertest.xml holds a valid document. This method tests the validating
     * parser.
     *
     * @param spf a Parser factory.
     * @param handler an error handler for capturing events.
     * @throws Exception If any errors occur.
     */
    @Test(groups = {"readLocalFiles"}, dataProvider = "input-provider")
    public void testParseValidate01(SAXParserFactory spf, MyErrorHandler handler)
            throws Exception {
            spf.newSAXParser().parse(new File(XML_DIR, "parsertest.xml"), handler);
            assertFalse(handler.isErrorOccured());
    }

    /**
     * validns.xml holds a valid document with XML namespaces in it. This method
     * tests the Validating parser with namespace processing on.
     *
     * @param spf a Parser factory.
     * @param handler an error handler for capturing events.
     * @throws Exception If any errors occur.
     */
    @Test(groups = {"readLocalFiles"}, dataProvider = "input-provider")
    public void testParseValidate02(SAXParserFactory spf, MyErrorHandler handler)
            throws Exception {
            spf.setNamespaceAware(true);
            spf.newSAXParser().parse(new File(XML_DIR, "validns.xml"), handler);
            assertFalse(handler.isErrorOccured());
    }

    /**
     * invalidns.xml holds an invalid document with XML namespaces in it. This
     * method tests the validating parser with namespace processing on. It
     * should throw validation error.
     *
     * @param spf a Parser factory.
     * @param handler an error handler for capturing events.
     * @throws Exception If any errors occur.
     */
    @Test(groups = {"readLocalFiles"}, dataProvider = "input-provider")
    public void testParseValidate03(SAXParserFactory spf, MyErrorHandler handler)
            throws Exception {
        try {
            spf.setNamespaceAware(true);
            SAXParser saxparser = spf.newSAXParser();
            saxparser.parse(new File(XML_DIR, "invalidns.xml"), handler);
            fail("Expecting SAXException here");
        } catch (SAXException e) {
            assertTrue(handler.isErrorOccured());
        }
    }

}