< prev index next >

test/javax/xml/jaxp/functional/org/xml/sax/ptests/XMLReaderAdapterTest.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.assertNotNull;
  31 import static org.testng.Assert.assertTrue;
  32 import org.testng.annotations.Test;
  33 import org.xml.sax.HandlerBase;
  34 import org.xml.sax.InputSource;
  35 import org.xml.sax.SAXException;
  36 import org.xml.sax.XMLReader;
  37 import org.xml.sax.helpers.XMLReaderAdapter;
  38 import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
  39 
  40 /**
  41  * Class containing the test cases for XMLReaderAdapter API
  42  */
  43 public class XMLReaderAdapterTest {
  44     /**
  45      * http://xml.org/sax/features/namespace-prefixes property name.
  46      */
  47     private final static String NM_PREFIXES_PROPERTY
  48             = "http://xml.org/sax/features/namespace-prefixes";
  49 
  50     /**
  51      * To test the constructor that uses "org.xml.sax.driver" property
  52      * @throws org.xml.sax.SAXException If the embedded driver cannot be
  53      * instantiated or if the org.xml.sax.driver property is not specified.
  54      */
  55     @Test
  56     public void constructor01() throws SAXException {
  57         assertNotNull(new XMLReaderAdapter());
  58     }
  59 
  60     /**
  61      * To test the constructor that uses XMLReader


  62      */
  63     @Test
  64     public void constructor02() {
  65         try {
  66             SAXParserFactory spf = SAXParserFactory.newInstance();
  67             XMLReader xmlReader = spf.newSAXParser().getXMLReader();
  68 
  69             assertNotNull(new XMLReaderAdapter(xmlReader));
  70         } catch (ParserConfigurationException | SAXException ex) {
  71             failUnexpected(ex);
  72         }
  73     }
  74 
  75     /**
  76      * To test the parse method. The specification says that this method
  77      * will throw an exception if the embedded XMLReader does not support
  78      * the http://xml.org/sax/features/namespace-prefixes property.


  79      */
  80     @Test
  81     public void nsfeature01() {
  82         try {
  83             SAXParserFactory spf = SAXParserFactory.newInstance();
  84             XMLReader xmlReader = spf.newSAXParser().getXMLReader();
  85             if (!xmlReader.getFeature(NM_PREFIXES_PROPERTY)) {
  86                 xmlReader.setFeature(NM_PREFIXES_PROPERTY, true);
  87             }
  88 
  89             assertTrue(xmlReader.getFeature(NM_PREFIXES_PROPERTY));
  90         } catch (SAXException | ParserConfigurationException ex) {
  91             failUnexpected(ex);
  92         }
  93     }
  94 
  95     /**
  96      * To test the parse method. The specification says that this method
  97      * will throw an exception if the embedded XMLReader does not support
  98      * the http://xml.org/sax/features/namespace-prefixes property.


  99      */
 100     @Test
 101     public void parse01() {

 102         try (FileInputStream fis = new FileInputStream(XML_DIR + "namespace1.xml")) {
 103             SAXParserFactory spf = SAXParserFactory.newInstance();
 104             XMLReader xmlReader = spf.newSAXParser().getXMLReader();
 105             if (!xmlReader.getFeature(NM_PREFIXES_PROPERTY)) {
 106                 xmlReader.setFeature(NM_PREFIXES_PROPERTY, true);
 107             }
 108             XMLReaderAdapter xmlRA = new XMLReaderAdapter(xmlReader);
 109 
 110             InputSource is = new InputSource(fis);
 111             xmlRA.setDocumentHandler(new HandlerBase());
 112             xmlRA.parse(is);
 113         } catch (IOException | SAXException | ParserConfigurationException ex) {
 114             failUnexpected(ex);
 115         }

 116     }
 117 }
   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 java.io.FilePermission;

  27 import javax.xml.parsers.SAXParserFactory;
  28 import jaxp.library.JAXPBaseTest;
  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.HandlerBase;
  33 import org.xml.sax.InputSource;
  34 import org.xml.sax.SAXException;
  35 import org.xml.sax.XMLReader;
  36 import org.xml.sax.helpers.XMLReaderAdapter;
  37 import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
  38 
  39 /**
  40  * Class containing the test cases for XMLReaderAdapter API
  41  */
  42 public class XMLReaderAdapterTest extends JAXPBaseTest {
  43     /**
  44      * http://xml.org/sax/features/namespace-prefixes property name.
  45      */
  46     private final static String NM_PREFIXES_PROPERTY
  47             = "http://xml.org/sax/features/namespace-prefixes";
  48 
  49     /**
  50      * To test the constructor that uses "org.xml.sax.driver" property
  51      * @throws org.xml.sax.SAXException If the embedded driver cannot be
  52      * instantiated or if the org.xml.sax.driver property is not specified.
  53      */
  54     @Test
  55     public void constructor01() throws SAXException {
  56         assertNotNull(new XMLReaderAdapter());
  57     }
  58 
  59     /**
  60      * To test the constructor that uses XMLReader.
  61      * 
  62      * @throws Exception If any errors occur.
  63      */
  64     @Test
  65     public void constructor02() throws Exception {
  66         XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();



  67         assertNotNull(new XMLReaderAdapter(xmlReader));



  68     }
  69 
  70     /**
  71      * To test the parse method. The specification says that this method
  72      * will throw an exception if the embedded XMLReader does not support
  73      * the http://xml.org/sax/features/namespace-prefixes property.
  74      * 
  75      * @throws Exception If any errors occur.
  76      */
  77     @Test
  78     public void nsfeature01() throws Exception {
  79         XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();


  80         if (!xmlReader.getFeature(NM_PREFIXES_PROPERTY)) {
  81             xmlReader.setFeature(NM_PREFIXES_PROPERTY, true);
  82         }

  83         assertTrue(xmlReader.getFeature(NM_PREFIXES_PROPERTY));



  84     }
  85 
  86     /**
  87      * To test the parse method. The specification says that this method
  88      * will throw an exception if the embedded XMLReader does not support
  89      * the http://xml.org/sax/features/namespace-prefixes property.
  90      * 
  91      * @throws Exception If any errors occur.
  92      */
  93     @Test
  94     public void parse01() throws Exception {
  95         setPermissions(new FilePermission(XML_DIR + "/-", "read"));
  96         try (FileInputStream fis = new FileInputStream(XML_DIR + "namespace1.xml")) {
  97             XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();

  98             if (!xmlReader.getFeature(NM_PREFIXES_PROPERTY)) {
  99                 xmlReader.setFeature(NM_PREFIXES_PROPERTY, true);
 100             }
 101             XMLReaderAdapter xmlRA = new XMLReaderAdapter(xmlReader);


 102             xmlRA.setDocumentHandler(new HandlerBase());
 103             xmlRA.parse(new InputSource(fis));


 104         }
 105         setPermissions();
 106     }
 107 }
< prev index next >