< prev index next >

test/javax/xml/jaxp/functional/org/xml/sax/ptests/XMLFilterTest.java

Print this page


   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  */
  23 package org.xml.sax.ptests;
  24 
  25 import java.io.FileInputStream;
  26 import java.io.IOException;
  27 import javax.xml.parsers.ParserConfigurationException;
  28 import javax.xml.parsers.SAXParserFactory;
  29 import static jaxp.library.JAXPTestUtilities.failUnexpected;
  30 import static org.testng.Assert.assertFalse;
  31 import static org.testng.Assert.assertNotNull;
  32 import static org.testng.Assert.assertTrue;
  33 import org.testng.annotations.Test;
  34 import org.xml.sax.InputSource;
  35 import org.xml.sax.SAXException;
  36 import org.xml.sax.SAXNotRecognizedException;
  37 import org.xml.sax.SAXNotSupportedException;
  38 import org.xml.sax.XMLReader;
  39 import org.xml.sax.helpers.XMLFilterImpl;
  40 import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
  41 
  42 /**
  43  * Unit test for XMLFilter.
  44  */
  45 public 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());


 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 }
   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  */
  23 package org.xml.sax.ptests;
  24 
  25 import java.io.FileInputStream;


  26 import javax.xml.parsers.SAXParserFactory;
  27 import jaxp.library.JAXPFileReadOnlyBaseTest;
  28 import static org.testng.Assert.assertFalse;
  29 import static org.testng.Assert.assertNotNull;
  30 import static org.testng.Assert.assertTrue;
  31 import org.testng.annotations.Test;
  32 import org.xml.sax.InputSource;

  33 import org.xml.sax.SAXNotRecognizedException;

  34 import org.xml.sax.XMLReader;
  35 import org.xml.sax.helpers.XMLFilterImpl;
  36 import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
  37 
  38 /**
  39  * Unit test for XMLFilter.
  40  */
  41 public class XMLFilterTest extends JAXPFileReadOnlyBaseTest {
  42     /**
  43      * name spaces constant.
  44      */
  45     private static final String NAMESPACES =
  46                 "http://xml.org/sax/features/namespaces";
  47 
  48     /**
  49      * name spaces prefixes constant.
  50      */
  51     private static final String NAMESPACE_PREFIXES =
  52                 "http://xml.org/sax/features/namespace-prefixes";
  53 
  54     /**
  55      * No exception expected when set a correct content handler.
  56      */
  57     @Test
  58     public void contentHandler01() {
  59         XMLFilterImpl xmlFilter = new XMLFilterImpl();
  60         xmlFilter.setContentHandler(xmlFilter);
  61         assertNotNull(xmlFilter.getContentHandler());


 108     /**
 109      * No exception expected when set a correct error handler.
 110      */
 111     @Test
 112     public void errorHandler01() {
 113         XMLFilterImpl xmlFilter = new XMLFilterImpl();
 114         xmlFilter.setErrorHandler(xmlFilter);
 115         assertNotNull(xmlFilter.getErrorHandler());
 116     }
 117 
 118     /**
 119      * No exception is expected when set error handler as null.
 120      */
 121     @Test
 122     public void errorHandler02() {
 123         new XMLFilterImpl().setErrorHandler(null);
 124     }
 125 
 126     /**
 127      * By default true is expected get namespaces feature.
 128      * 
 129      * @throws Exception If any errors occur.
 130      */
 131     @Test
 132     public void getFeature01() throws Exception {

 133         SAXParserFactory spf = SAXParserFactory.newInstance();
 134         spf.setNamespaceAware(true);
 135         XMLReader xmlReader = spf.newSAXParser().getXMLReader();
 136 
 137         XMLFilterImpl xmlFilter = new XMLFilterImpl();
 138         xmlFilter.setParent(xmlReader);
 139         assertTrue(xmlFilter.getFeature(NAMESPACES));



 140     }
 141 
 142     /**
 143      * By default false is expected get namespaces-prefix feature.
 144      * 
 145      * @throws Exception If any errors occur.
 146      */
 147     @Test
 148     public void getFeature02() throws Exception {

 149         SAXParserFactory spf = SAXParserFactory.newInstance();
 150         spf.setNamespaceAware(true);


 151         XMLFilterImpl xmlFilter = new XMLFilterImpl();
 152         xmlFilter.setParent(spf.newSAXParser().getXMLReader());
 153         assertFalse(xmlFilter.getFeature(NAMESPACE_PREFIXES));



 154     }
 155 
 156     /**
 157      * SAXNotRecognizedException is expected when get a feature by an invalid
 158      * feature name.
 159      * 
 160      * @throws Exception If any errors occur.



 161      */
 162     @Test(expectedExceptions = SAXNotRecognizedException.class)
 163     public void getFeature03() throws Exception {

 164         new XMLFilterImpl().getFeature("no-meaning-feature");
 165     }
 166 
 167     /**
 168      * Set namespaces feature to a value to XMLFilter. it's expected same when
 169      * obtain it again.
 170      * 
 171      * @throws Exception If any errors occur.
 172      */
 173     @Test
 174     public void setFeature01() throws Exception {

 175         SAXParserFactory spf = SAXParserFactory.newInstance();
 176         spf.setNamespaceAware(true);

 177 
 178         XMLFilterImpl xmlFilter = new XMLFilterImpl();
 179         xmlFilter.setParent(spf.newSAXParser().getXMLReader());
 180         xmlFilter.setFeature(NAMESPACES, false);
 181         assertFalse(xmlFilter.getFeature(NAMESPACES));
 182         xmlFilter.setFeature(NAMESPACES, true);
 183         assertTrue(xmlFilter.getFeature(NAMESPACES));



 184     }
 185 
 186     /**
 187      * Set namespaces-prefix feature to a value to XMLFilter. it's expected same
 188      * when obtain it again.
 189      * 
 190      * @throws Exception If any errors occur.
 191      */
 192     @Test
 193     public void setFeature02() throws Exception {

 194         SAXParserFactory spf = SAXParserFactory.newInstance();
 195         spf.setNamespaceAware(true);

 196 
 197         XMLFilterImpl xmlFilter = new XMLFilterImpl();
 198         xmlFilter.setParent(spf.newSAXParser().getXMLReader());
 199         xmlFilter.setFeature(NAMESPACE_PREFIXES, false);
 200         assertFalse(xmlFilter.getFeature(NAMESPACE_PREFIXES));
 201         xmlFilter.setFeature(NAMESPACE_PREFIXES, true);
 202         assertTrue(xmlFilter.getFeature(NAMESPACE_PREFIXES));



 203     }
 204 
 205     /**
 206      * NullPointerException is expected when parse a null InputSource.
 207      * 
 208      * @throws Exception If any errors occur.
 209      */
 210     @Test(expectedExceptions = NullPointerException.class)
 211     public void parse01() throws Exception {

 212         new XMLFilterImpl().parse((InputSource)null);



 213     }
 214 
 215     /**
 216      * SAXException is expected when parsing a invalid formatted XML file.
 217      * 
 218      * @throws Exception If any errors occur.
 219      */
 220     @Test(groups = {"readLocalFiles"}, expectedExceptions = NullPointerException.class)
 221     public void parse02() throws Exception {

 222         try(FileInputStream fis = new FileInputStream(XML_DIR + "invalid.xml")) {
 223             new XMLFilterImpl().parse(new InputSource(fis));



 224         } 
 225     }
 226 
 227     /**
 228      * No exception when parse a normal XML file.
 229      * 
 230      * @throws Exception If any errors occur.
 231      */
 232     @Test(groups = {"readLocalFiles"}, expectedExceptions = NullPointerException.class)
 233     public void parse03() throws Exception {

 234         try(FileInputStream fis = new FileInputStream(XML_DIR + "correct2.xml")) {
 235             new XMLFilterImpl().parse(new InputSource(fis));



 236         }
 237     }
 238 }
< prev index next >