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.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 }