XMLFilterTest.java revision 653:c4cb73fc93db
1/*
2 * Copyright (c) 2014, 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 java.io.IOException;
27import javax.xml.parsers.ParserConfigurationException;
28import javax.xml.parsers.SAXParserFactory;
29import static jaxp.library.JAXPTestUtilities.failUnexpected;
30import static org.testng.Assert.assertFalse;
31import static org.testng.Assert.assertNotNull;
32import static org.testng.Assert.assertTrue;
33import org.testng.annotations.Test;
34import org.xml.sax.InputSource;
35import org.xml.sax.SAXException;
36import org.xml.sax.SAXNotRecognizedException;
37import org.xml.sax.SAXNotSupportedException;
38import org.xml.sax.XMLReader;
39import org.xml.sax.helpers.XMLFilterImpl;
40import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
41
42/**
43 * Unit test for XMLFilter.
44 */
45public class XMLFilterTest {
46    /**
47     * name spaces constant.
48     */
49    private static final String NAMESPACES =
50                "http://xml.org/sax/features/namespaces";
51
52    /**
53     * name spaces prefixes constant.
54     */
55    private static final String NAMESPACE_PREFIXES =
56                "http://xml.org/sax/features/namespace-prefixes";
57
58    /**
59     * No exception expected when set a correct content handler.
60     */
61    @Test
62    public void contentHandler01() {
63        XMLFilterImpl xmlFilter = new XMLFilterImpl();
64        xmlFilter.setContentHandler(xmlFilter);
65        assertNotNull(xmlFilter.getContentHandler());
66    }
67
68    /**
69     * No exception is expected when set content handler as null.
70     */
71    @Test
72    public void contentHandler02() {
73        new XMLFilterImpl().setContentHandler(null);
74    }
75
76    /**
77     * No exception expected when set a correct entity solver.
78     */
79    @Test
80    public void entity01() {
81        XMLFilterImpl xmlFilter = new XMLFilterImpl();
82        xmlFilter.setEntityResolver(xmlFilter);
83        assertNotNull(xmlFilter.getEntityResolver());
84    }
85
86    /**
87     * No exception is expected when set entity resolver as null.
88     */
89    @Test
90    public void entity02() {
91        new XMLFilterImpl().setEntityResolver(null);
92    }
93
94    /**
95     * No exception expected when set a correct DTD handler.
96     */
97    @Test
98    public void dtdHandler01() {
99        XMLFilterImpl xmlFilter = new XMLFilterImpl();
100        xmlFilter.setDTDHandler(xmlFilter);
101        assertNotNull(xmlFilter.getDTDHandler());
102    }
103
104    /**
105     * No exception is expected when set DTD handler as null.
106     */
107    @Test
108    public void dtdHandler02() {
109        new XMLFilterImpl().setDTDHandler(null);
110    }
111
112    /**
113     * No exception expected when set a correct error handler.
114     */
115    @Test
116    public void errorHandler01() {
117        XMLFilterImpl xmlFilter = new XMLFilterImpl();
118        xmlFilter.setErrorHandler(xmlFilter);
119        assertNotNull(xmlFilter.getErrorHandler());
120    }
121
122    /**
123     * No exception is expected when set error handler as null.
124     */
125    @Test
126    public void errorHandler02() {
127        new XMLFilterImpl().setErrorHandler(null);
128    }
129
130    /**
131     * By default true is expected get namespaces feature.
132     * @throws SAXException
133     */
134    @Test
135    public void getFeature01() throws SAXException {
136        try {
137            SAXParserFactory spf = SAXParserFactory.newInstance();
138            spf.setNamespaceAware(true);
139            XMLReader xmlReader = spf.newSAXParser().getXMLReader();
140
141            XMLFilterImpl xmlFilter = new XMLFilterImpl();
142            xmlFilter.setParent(xmlReader);
143            assertTrue(xmlFilter.getFeature(NAMESPACES));
144        } catch (SAXException | ParserConfigurationException ex) {
145            failUnexpected(ex);
146        }
147    }
148
149    /**
150     * By default false is expected get namespaces-prefix feature.
151     */
152    @Test
153    public void getFeature02() {
154        try {
155            SAXParserFactory spf = SAXParserFactory.newInstance();
156            spf.setNamespaceAware(true);
157            XMLReader xmlReader = spf.newSAXParser().getXMLReader();
158
159            XMLFilterImpl xmlFilter = new XMLFilterImpl();
160            xmlFilter.setParent(xmlReader);
161            assertFalse(xmlFilter.getFeature(NAMESPACE_PREFIXES));
162        } catch (SAXException | ParserConfigurationException ex) {
163            failUnexpected(ex);
164        }
165    }
166
167    /**
168     * SAXNotRecognizedException is expected when get a feature by an invalid
169     * feature name.
170     * @throws org.xml.sax.SAXNotRecognizedException If the feature
171     *            value can't be assigned or retrieved from the parent.
172     * @throws org.xml.sax.SAXNotSupportedException When the
173     *            parent recognizes the feature name but
174     *            cannot determine its value at this time.
175     */
176    @Test(expectedExceptions = SAXNotRecognizedException.class)
177    public void getFeature03() throws SAXNotRecognizedException,
178           SAXNotSupportedException {
179        new XMLFilterImpl().getFeature("no-meaning-feature");
180    }
181
182    /**
183     * Set namespaces feature to a value to XMLFilter. it's expected same when
184     * obtain it again.
185     */
186    @Test
187    public void setFeature01() {
188        try {
189            SAXParserFactory spf = SAXParserFactory.newInstance();
190            spf.setNamespaceAware(true);
191            XMLReader xmlReader = spf.newSAXParser().getXMLReader();
192
193            XMLFilterImpl xmlFilter = new XMLFilterImpl();
194            xmlFilter.setParent(xmlReader);
195            xmlFilter.setFeature(NAMESPACES, false);
196            assertFalse(xmlFilter.getFeature(NAMESPACES));
197            xmlFilter.setFeature(NAMESPACES, true);
198            assertTrue(xmlFilter.getFeature(NAMESPACES));
199        } catch (SAXException | ParserConfigurationException ex) {
200            failUnexpected(ex);
201        }
202    }
203
204    /**
205     * Set namespaces-prefix feature to a value to XMLFilter. it's expected same
206     * when obtain it again.
207     */
208    @Test
209    public void setFeature02() {
210        try {
211            SAXParserFactory spf = SAXParserFactory.newInstance();
212            spf.setNamespaceAware(true);
213            XMLReader xmlReader = spf.newSAXParser().getXMLReader();
214
215            XMLFilterImpl xmlFilter = new XMLFilterImpl();
216            xmlFilter.setParent(xmlReader);
217            xmlFilter.setFeature(NAMESPACE_PREFIXES, false);
218            assertFalse(xmlFilter.getFeature(NAMESPACE_PREFIXES));
219            xmlFilter.setFeature(NAMESPACE_PREFIXES, true);
220            assertTrue(xmlFilter.getFeature(NAMESPACE_PREFIXES));
221        } catch (SAXException | ParserConfigurationException ex) {
222            failUnexpected(ex);
223        }
224    }
225
226    /**
227     * NullPointerException is expected when parse a null InputSource.
228     */
229    @Test(expectedExceptions = NullPointerException.class)
230    public void parse01() {
231        try {
232            new XMLFilterImpl().parse((InputSource)null);
233        } catch (IOException | SAXException ex) {
234            failUnexpected(ex);
235        }
236    }
237
238    /**
239     * SAXException is expected when parsing a invalid formatted XML file.
240     * @throws org.xml.sax.SAXException when parse a incorrect formatted XML
241     * file.
242     */
243    @Test(expectedExceptions = NullPointerException.class)
244    public void parse02() throws SAXException {
245        XMLFilterImpl xmlFilter = new XMLFilterImpl();
246        try(FileInputStream fis = new FileInputStream(XML_DIR + "invalid.xml")) {
247            InputSource is = new InputSource(fis);
248            xmlFilter.parse(is);
249        } catch (IOException ex) {
250            failUnexpected(ex);
251        }
252    }
253
254    /**
255     * No exception when parse a normal XML file.
256     */
257    @Test(expectedExceptions = NullPointerException.class)
258    public void parse03() {
259        XMLFilterImpl xmlFilter = new XMLFilterImpl();
260        try(FileInputStream fis = new FileInputStream(XML_DIR + "correct2.xml")) {
261            InputSource is = new InputSource(fis);
262            xmlFilter.parse(is);
263        } catch (IOException | SAXException ex) {
264            failUnexpected(ex);
265        }
266    }
267}
268