< prev index next >

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

Print this page




  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.SAXParser;
  29 import javax.xml.parsers.SAXParserFactory;

  30 import static jaxp.library.JAXPTestUtilities.compareWithGold;
  31 import static jaxp.library.JAXPTestUtilities.failUnexpected;
  32 import static org.testng.Assert.assertTrue;
  33 import org.xml.sax.InputSource;
  34 import org.xml.sax.SAXException;
  35 import org.xml.sax.XMLReader;
  36 import static org.xml.sax.ptests.SAXTestConst.CLASS_DIR;
  37 import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR;
  38 import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
  39 
  40 /** This class contains the testcases to test XMLReader with regard to
  41   * Namespace Table defined at
  42   * http://www.megginson.com/SAX/Java/namespaces.html
  43   */
  44 public class XMLReaderNSTableTest {
  45     /**
  46      * XML file that used to be parsed.
  47      */
  48     private static final String xmlFile = XML_DIR + "namespace1.xml";
  49 
  50     /**
  51      * XML namespaces prefixes.
  52      */
  53     private static final String NAMESPACE_PREFIXES =
  54                 "http://xml.org/sax/features/namespace-prefixes";
  55     /**
  56      * namespace processing is enabled. namespace-prefix is also is enabled.
  57      * So it is a True-True combination.
  58      * The test is to test XMLReader with these conditions







  59      */
  60     public void testWithTrueTrue() {

  61         String outputFile = CLASS_DIR + "XRNSTableTT.out";
  62         String goldFile = GOLDEN_DIR + "NSTableTTGF.out";
  63 
  64         try {
  65             SAXParserFactory spf = SAXParserFactory.newInstance();
  66             spf.setNamespaceAware(true);
  67             SAXParser saxParser = spf.newSAXParser();
  68 
  69             XMLReader xmlReader = saxParser.getXMLReader();
  70             xmlReader.setFeature(NAMESPACE_PREFIXES, true);
  71 
  72             xmlReader.setContentHandler(new MyNSContentHandler(outputFile));
  73             xmlReader.parse(new InputSource(new FileInputStream(xmlFile)));
  74             assertTrue(compareWithGold(goldFile, outputFile));
  75         } catch (ParserConfigurationException | SAXException | IOException ex) {
  76             failUnexpected(ex);
  77         }

  78     }
  79 
  80     /**
  81      * Namespace processing is enabled. Hence namespace-prefix is
  82      * expected to be automaically off. So it is a True-False combination.
  83      * The test is to test XMLReader with these conditions







  84      */
  85     public void testWithTrueFalse() {

  86         String outputFile = CLASS_DIR + "XRNSTableTF.out";
  87         String goldFile = GOLDEN_DIR + "NSTableTFGF.out";
  88 
  89         try {
  90             SAXParserFactory spf = SAXParserFactory.newInstance();
  91             spf.setNamespaceAware(true);
  92             SAXParser saxParser = spf.newSAXParser();
  93             XMLReader xmlReader = saxParser.getXMLReader();
  94 
  95             xmlReader.setContentHandler(new MyNSContentHandler(outputFile));
  96             xmlReader.parse(new InputSource(new FileInputStream(xmlFile)));
  97             assertTrue(compareWithGold(goldFile, outputFile));
  98         } catch (ParserConfigurationException | SAXException | IOException ex) {
  99             failUnexpected(ex);
 100         }

 101     }
 102 
 103     /**
 104      * namespace processing is not enabled. Hence namespace-prefix is
 105      * expected to be automaically on. So it is a False-True combination.
 106      * The test is to test XMLReader with these conditions







 107      */
 108     public void testWithFalseTrue() {

 109         String outputFile = CLASS_DIR + "XRNSTableFT.out";
 110         String goldFile = GOLDEN_DIR + "NSTableFTGF.out";
 111 
 112         try {
 113             SAXParserFactory spf = SAXParserFactory.newInstance();
 114             spf.setNamespaceAware(true);
 115             SAXParser saxParser = spf.newSAXParser();
 116             XMLReader xmlReader = saxParser.getXMLReader();
 117 
 118             xmlReader.setContentHandler(new MyNSContentHandler(outputFile));
 119             xmlReader.parse(new InputSource(new FileInputStream(xmlFile)));
 120             assertTrue(compareWithGold(goldFile, outputFile));
 121         } catch (ParserConfigurationException | SAXException | IOException ex) {
 122             failUnexpected(ex);
 123         }

 124     }
 125 }


  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.SAXParser;
  29 import javax.xml.parsers.SAXParserFactory;
  30 import jaxp.library.JAXPFileBaseTest;
  31 import static jaxp.library.JAXPTestUtilities.compareWithGold;

  32 import static org.testng.Assert.assertTrue;
  33 import org.xml.sax.InputSource;
  34 import org.xml.sax.SAXException;
  35 import org.xml.sax.XMLReader;
  36 import static org.xml.sax.ptests.SAXTestConst.CLASS_DIR;
  37 import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR;
  38 import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
  39 
  40 /** This class contains the testcases to test XMLReader with regard to
  41   * Namespace Table defined at
  42   * http://www.megginson.com/SAX/Java/namespaces.html
  43   */
  44 public class XMLReaderNSTableTest extends JAXPFileBaseTest {
  45     /**
  46      * XML file that used to be parsed.
  47      */
  48     private static final String xmlFile = XML_DIR + "namespace1.xml";
  49 
  50     /**
  51      * XML namespaces prefixes.
  52      */
  53     private static final String NAMESPACE_PREFIXES =
  54                 "http://xml.org/sax/features/namespace-prefixes";
  55     /**
  56      * namespace processing is enabled. namespace-prefix is also is enabled.
  57      * So it is a True-True combination.
  58      * The test is to test XMLReader with these conditions.
  59      * 
  60      * @throws SAXException If there is a problem processing the document.
  61      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
  62      *         created which satisfies the configuration requested.
  63      * @throws IOException if the file exists but is a directory rather than
  64      *         a regular file, does not exist but cannot be created, or cannot 
  65      *         be opened for any other reason.
  66      */
  67     public void testWithTrueTrue() throws IOException, SAXException, 
  68             ParserConfigurationException {
  69         String outputFile = CLASS_DIR + "XRNSTableTT.out";
  70         String goldFile = GOLDEN_DIR + "NSTableTTGF.out";
  71         

  72         SAXParserFactory spf = SAXParserFactory.newInstance();
  73         spf.setNamespaceAware(true);
  74         XMLReader xmlReader = spf.newSAXParser().getXMLReader();


  75         xmlReader.setFeature(NAMESPACE_PREFIXES, true);
  76 
  77         try (FileInputStream fis = new FileInputStream(xmlFile);
  78             MyNSContentHandler handler = new MyNSContentHandler(outputFile);) {
  79             xmlReader.setContentHandler(handler);
  80             xmlReader.parse(new InputSource(fis));

  81         }
  82         assertTrue(compareWithGold(goldFile, outputFile));
  83     }
  84 
  85     /**
  86      * Namespace processing is enabled. Hence namespace-prefix is
  87      * expected to be automatically off. So it is a True-False combination.
  88      * The test is to test XMLReader with these conditions.
  89      * 
  90      * @throws SAXException If there is a problem processing the document.
  91      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
  92      *         created which satisfies the configuration requested.
  93      * @throws IOException if the file exists but is a directory rather than
  94      *         a regular file, does not exist but cannot be created, or cannot 
  95      *         be opened for any other reason.
  96      */
  97     public void testWithTrueFalse() throws IOException, SAXException, 
  98             ParserConfigurationException {
  99         String outputFile = CLASS_DIR + "XRNSTableTF.out";
 100         String goldFile = GOLDEN_DIR + "NSTableTFGF.out";
 101 

 102         SAXParserFactory spf = SAXParserFactory.newInstance();
 103         spf.setNamespaceAware(true);
 104         SAXParser saxParser = spf.newSAXParser();
 105         XMLReader xmlReader = saxParser.getXMLReader();
 106 
 107         try (FileInputStream fis = new FileInputStream(xmlFile);
 108             MyNSContentHandler handler = new MyNSContentHandler(outputFile)) {
 109             xmlReader.setContentHandler(handler);
 110             xmlReader.parse(new InputSource(fis));   

 111         }
 112         assertTrue(compareWithGold(goldFile, outputFile));
 113     }
 114 
 115     /**
 116      * namespace processing is not enabled. Hence namespace-prefix is
 117      * expected to be automaically on. So it is a False-True combination.
 118      * The test is to test XMLReader with these conditions.
 119      * 
 120      * @throws SAXException If there is a problem processing the document.
 121      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
 122      *         created which satisfies the configuration requested.
 123      * @throws IOException if the file exists but is a directory rather than
 124      *         a regular file, does not exist but cannot be created, or cannot 
 125      *         be opened for any other reason.
 126      */
 127     public void testWithFalseTrue()throws IOException, SAXException, 
 128             ParserConfigurationException {
 129         String outputFile = CLASS_DIR + "XRNSTableFT.out";
 130         String goldFile = GOLDEN_DIR + "NSTableFTGF.out";
 131         

 132         SAXParserFactory spf = SAXParserFactory.newInstance();
 133         spf.setNamespaceAware(true);
 134         XMLReader xmlReader = spf.newSAXParser().getXMLReader();
 135         try (FileInputStream fis = new FileInputStream(xmlFile);
 136             MyNSContentHandler handler = new MyNSContentHandler(outputFile)) {
 137             xmlReader.setContentHandler(handler);
 138             xmlReader.parse(new InputSource(fis));



 139         } 
 140         assertTrue(compareWithGold(goldFile, outputFile));
 141     }
 142 }
< prev index next >