ParserAdapterTest.java revision 687:e7736286abe1
1/*
2 * Copyright (c) 2003, 2015, 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 java.io.FileInputStream;
26import javax.xml.parsers.SAXParserFactory;
27import jaxp.library.JAXPFileReadOnlyBaseTest;
28import static org.testng.Assert.assertFalse;
29import static org.testng.Assert.assertNotNull;
30import static org.testng.Assert.assertTrue;
31import org.testng.annotations.Test;
32import org.xml.sax.ContentHandler;
33import org.xml.sax.InputSource;
34import org.xml.sax.SAXException;
35import org.xml.sax.SAXNotRecognizedException;
36import org.xml.sax.XMLReader;
37import org.xml.sax.helpers.ParserAdapter;
38import org.xml.sax.helpers.XMLFilterImpl;
39import org.xml.sax.helpers.XMLReaderAdapter;
40import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
41
42
43/**
44 * Unit test cases for ParserAdapter API. By default the only features recognized
45 * are namespaces and namespace-prefixes.
46 */
47public class ParserAdapterTest extends JAXPFileReadOnlyBaseTest {
48    /**
49     * namespaces feature name.
50     */
51    private static final String NAMESPACES =
52                "http://xml.org/sax/features/namespaces";
53
54    /**
55     * namespaces-prefiexs feature name.
56     */
57    private static final String NAMESPACE_PREFIXES =
58                "http://xml.org/sax/features/namespace-prefixes";
59
60    /**
61     * ParserAdapter instance to share by all tests.
62     */
63    private final ParserAdapter parserAdapter;
64
65    /**
66     * Initiate ParserAdapter.
67     * @throws Exception If any errors occur.
68     */
69    ParserAdapterTest() throws Exception {
70        SAXParserFactory spf = SAXParserFactory.newInstance();
71        XMLReader xmlReader = spf.newSAXParser().getXMLReader();
72        XMLReaderAdapter xmlReaderAdapter = new XMLReaderAdapter(xmlReader);
73        parserAdapter = new ParserAdapter(xmlReaderAdapter);
74    }
75
76    /**
77     * Verifies parserAdapter.getContentHandler()
78     */
79    @Test
80    public void contentHandler01() {
81        ContentHandler contentHandler = new XMLFilterImpl();
82        parserAdapter.setContentHandler(contentHandler);
83        assertNotNull(parserAdapter.getContentHandler());
84    }
85
86    /**
87     * No exception is expected when set content handler as null.
88     */
89    @Test
90    public void contentHandler02() {
91        parserAdapter.setContentHandler(null);
92    }
93
94    /**
95     * Verifies parserAdapter.getEntityResolver()
96     */
97    @Test
98    public void entity01() {
99        XMLFilterImpl xmlFilter = new XMLFilterImpl();
100        parserAdapter.setEntityResolver(xmlFilter);
101        assertNotNull(parserAdapter.getEntityResolver());
102    }
103
104    /**
105     * No exception is expected when set entity resolver as null.
106     */
107    @Test
108    public void entity02() {
109        parserAdapter.setEntityResolver(null);
110    }
111
112    /**
113     * Verifies parserAdapter.getDTDHandler()
114     */
115    @Test
116    public void dtdHandler01() {
117        XMLFilterImpl xmlFilter = new XMLFilterImpl();
118        parserAdapter.setDTDHandler(xmlFilter);
119        assertNotNull(parserAdapter.getDTDHandler());
120    }
121
122    /**
123     * No exception is expected when set DTD handler as null.
124     */
125    @Test
126    public void dtdHandler02() {
127        parserAdapter.setDTDHandler(null);
128    }
129
130    /**
131     * Verifies parserAdapter.getErrorHandler()
132     */
133    @Test
134    public void errorHandler01() {
135        XMLFilterImpl eHandler = new XMLFilterImpl();
136        parserAdapter.setErrorHandler(eHandler);
137        assertNotNull(parserAdapter.getErrorHandler());
138    }
139
140    /**
141     * No exception is expected when set error handler as null.
142     */
143    @Test
144    public void errorHandler02() {
145        parserAdapter.setErrorHandler(null);
146    }
147
148    /**
149     * parserAdapter.getFeature(NAMESPACES) returns true be default.
150     *
151     * @exception Exception If any errors occur.
152     */
153    @Test
154    public void getFeature01() throws Exception {
155        assertTrue(parserAdapter.getFeature(NAMESPACES));
156    }
157
158    /**
159     * parserAdapter.getFeature(NAMESPACE_PREFIXES) returns true be default.
160     *
161     * @exception Exception If any errors occur.
162     */
163    @Test
164    public void getFeature02() throws Exception {
165        assertFalse(parserAdapter.getFeature(NAMESPACE_PREFIXES));
166    }
167
168    /**
169     * SAXNotRecognizedException thrown when feature name is not known one.
170     *
171     * @exception Exception If any errors occur.
172     */
173    @Test(expectedExceptions = SAXNotRecognizedException.class)
174    public void getFeature03() throws Exception {
175        parserAdapter.getFeature("no-meaning-feature");
176    }
177
178    /**
179     * Obtain getFeature after it's set returns set value.
180     *
181     * @exception Exception If any errors occur.
182     */
183    @Test
184    public void setFeature01() throws Exception {
185        parserAdapter.setFeature(NAMESPACES, false);
186        assertFalse(parserAdapter.getFeature(NAMESPACES));
187    }
188
189    /**
190     * Obtain getFeature after it's set returns set value.
191     *
192     * @exception Exception If any errors occur.
193     */
194    @Test
195    public void setFeature02() throws Exception {
196        parserAdapter.setFeature(NAMESPACE_PREFIXES, false);
197        assertFalse(parserAdapter.getFeature(NAMESPACE_PREFIXES));
198    }
199
200    /**
201     * Obtain getFeature after it's set returns set value.
202     *
203     * @exception Exception If any errors occur.
204     */
205    @Test
206    public void setFeature03() throws Exception {
207        parserAdapter.setFeature(NAMESPACES, true);
208        assertTrue(parserAdapter.getFeature(NAMESPACES));
209    }
210
211    /**
212     * Obtain getFeature after it's set returns set value.
213     *
214     * @exception Exception If any errors occur.
215     */
216    @Test
217    public void setFeature04() throws Exception {
218        parserAdapter.setFeature(NAMESPACE_PREFIXES, true);
219        assertTrue(parserAdapter.getFeature(NAMESPACE_PREFIXES));
220    }
221
222    /**
223     * NPE expected when parsing a null object by ParserAdapter.
224     *
225     * @throws Exception If any errors occur.
226     */
227    @Test(expectedExceptions = NullPointerException.class)
228    public void parse01() throws Exception {
229        parserAdapter.parse((InputSource)null);
230    }
231
232    /**
233     * SAXException expected when parsing a wrong-formatter XML with ParserAdapter.
234     *
235     * @throws Exception If any errors occur.
236     */
237    @Test(groups = {"readLocalFiles"}, expectedExceptions = SAXException.class)
238    public void parse02() throws Exception {
239        try(FileInputStream fis = new FileInputStream(XML_DIR + "invalid.xml")) {
240            InputSource is = new InputSource(fis);
241            parserAdapter.parse(is);
242        }
243    }
244
245    /**
246     * Parse a well-formatter XML with ParserAdapter.
247     *
248     * @throws Exception If any errors occur.
249     */
250    @Test(groups = {"readLocalFiles"})
251    public void parse03() throws Exception {
252        try(FileInputStream fis = new FileInputStream(XML_DIR + "correct.xml")) {
253            InputSource is = new InputSource(fis);
254            parserAdapter.parse(is);
255        }
256    }
257}
258