SAXParserNSTableTest.java revision 997:540334ae53fe
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 */
23package org.xml.sax.ptests;
24
25import static jaxp.library.JAXPTestUtilities.USER_DIR;
26import static jaxp.library.JAXPTestUtilities.compareWithGold;
27import static org.testng.Assert.assertTrue;
28import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR;
29import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
30
31import java.io.File;
32
33import javax.xml.parsers.SAXParserFactory;
34
35import org.testng.annotations.Listeners;
36import org.testng.annotations.Test;
37
38/**
39 * This class contains the testcases to test SAXParser with regard to
40 * Namespace Table defined at http://www.megginson.com/SAX/Java/namespaces.html
41 */
42/*
43 * @test
44 * @library /javax/xml/jaxp/libs
45 * @run testng/othervm -DrunSecMngr=true org.xml.sax.ptests.SAXParserNSTableTest
46 * @run testng/othervm org.xml.sax.ptests.SAXParserNSTableTest
47 */
48@Listeners({jaxp.library.FilePolicy.class})
49public class SAXParserNSTableTest {
50    /**
51     * namespace processing is enabled. namespace-prefix is also is enabled.
52     * So it is a True-True combination.
53     * The test is to test SAXParser with these conditions.
54     *
55     * @throws Exception If any errors occur.
56     */
57    @Test
58    public void testWithTrueTrue() throws Exception {
59        String outputFile = USER_DIR + "SPNSTableTT.out";
60        String goldFile = GOLDEN_DIR + "NSTableTTGF.out";
61        String xmlFile = XML_DIR + "namespace1.xml";
62        SAXParserFactory spf = SAXParserFactory.newInstance();
63        spf.setNamespaceAware(true);
64        spf.setFeature("http://xml.org/sax/features/namespace-prefixes",
65                                    true);
66        try (MyNSContentHandler handler = new MyNSContentHandler(outputFile)) {
67            spf.newSAXParser().parse(new File(xmlFile), handler);
68        }
69        assertTrue(compareWithGold(goldFile, outputFile));
70
71    }
72    /**
73     * namespace processing is enabled. Hence namespace-prefix is
74     * expected to be automatically off. So it is a True-False combination.
75     * The test is to test SAXParser with these conditions.
76     *
77     * @throws Exception If any errors occur.
78     */
79    public void testWithTrueFalse() throws Exception {
80        String outputFile = USER_DIR + "SPNSTableTF.out";
81        String goldFile = GOLDEN_DIR + "NSTableTFGF.out";
82        String xmlFile = XML_DIR + "namespace1.xml";
83        SAXParserFactory spf = SAXParserFactory.newInstance();
84        spf.setNamespaceAware(true);
85        try (MyNSContentHandler handler = new MyNSContentHandler(outputFile)) {
86            spf.newSAXParser().parse(new File(xmlFile), handler);
87        }
88        assertTrue(compareWithGold(goldFile, outputFile));
89    }
90
91    /**
92     * namespace processing is not enabled. Hence namespace-prefix is
93     * expected to be automatically on. So it is a False-True combination.
94     * The test is to test SAXParser with these conditions.
95     *
96     * @throws Exception If any errors occur.
97     */
98    public void testWithFalseTrue() throws Exception {
99        String outputFile = USER_DIR + "SPNSTableFT.out";
100        String goldFile = GOLDEN_DIR + "NSTableFTGF.out";
101        String xmlFile = XML_DIR + "namespace1.xml";
102        SAXParserFactory spf = SAXParserFactory.newInstance();
103        spf.setNamespaceAware(true);
104        try (MyNSContentHandler handler = new MyNSContentHandler(outputFile)) {
105            spf.newSAXParser().parse(new File(xmlFile), handler);
106        }
107        assertTrue(compareWithGold(goldFile, outputFile));
108    }
109}
110