1 /*
   2  * Copyright (c) 2003, 2016, 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 static jaxp.library.JAXPTestUtilities.USER_DIR;
  26 import static jaxp.library.JAXPTestUtilities.compareWithGold;
  27 import static org.testng.Assert.assertTrue;
  28 import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR;
  29 import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
  30 
  31 import java.io.FileInputStream;
  32 
  33 import javax.xml.parsers.SAXParser;
  34 import javax.xml.parsers.SAXParserFactory;
  35 
  36 import org.testng.annotations.Listeners;
  37 import org.testng.annotations.Test;
  38 import org.xml.sax.InputSource;
  39 import org.xml.sax.XMLReader;
  40 
  41 /** This class contains the testcases to test XMLReader with regard to
  42   * Namespace Table defined at
  43   * http://www.megginson.com/SAX/Java/namespaces.html
  44   */
  45 @Test
  46 @Listeners({jaxp.library.FilePolicy.class})
  47 public class XMLReaderNSTableTest {
  48     /**
  49      * XML file that used to be parsed.
  50      */
  51     private static final String xmlFile = XML_DIR + "namespace1.xml";
  52 
  53     /**
  54      * XML namespaces prefixes.
  55      */
  56     private static final String NAMESPACE_PREFIXES =
  57                 "http://xml.org/sax/features/namespace-prefixes";
  58     /**
  59      * namespace processing is enabled. namespace-prefix is also is enabled.
  60      * So it is a True-True combination.
  61      * The test is to test XMLReader with these conditions.
  62      *
  63      * @throws Exception If any errors occur.
  64      */
  65     public void testWithTrueTrue() throws Exception {
  66         String outputFile = USER_DIR + "XRNSTableTT.out";
  67         String goldFile = GOLDEN_DIR + "NSTableTTGF.out";
  68 
  69         SAXParserFactory spf = SAXParserFactory.newInstance();
  70         spf.setNamespaceAware(true);
  71         XMLReader xmlReader = spf.newSAXParser().getXMLReader();
  72         xmlReader.setFeature(NAMESPACE_PREFIXES, true);
  73 
  74         try (FileInputStream fis = new FileInputStream(xmlFile);
  75             MyNSContentHandler handler = new MyNSContentHandler(outputFile);) {
  76             xmlReader.setContentHandler(handler);
  77             xmlReader.parse(new InputSource(fis));
  78         }
  79         assertTrue(compareWithGold(goldFile, outputFile));
  80     }
  81 
  82     /**
  83      * Namespace processing is enabled. Hence namespace-prefix is
  84      * expected to be automatically off. So it is a True-False combination.
  85      * The test is to test XMLReader with these conditions.
  86      *
  87      * @throws Exception If any errors occur.
  88      */
  89     public void testWithTrueFalse() throws Exception {
  90         String outputFile = USER_DIR + "XRNSTableTF.out";
  91         String goldFile = GOLDEN_DIR + "NSTableTFGF.out";
  92 
  93         SAXParserFactory spf = SAXParserFactory.newInstance();
  94         spf.setNamespaceAware(true);
  95         SAXParser saxParser = spf.newSAXParser();
  96         XMLReader xmlReader = saxParser.getXMLReader();
  97 
  98         try (FileInputStream fis = new FileInputStream(xmlFile);
  99             MyNSContentHandler handler = new MyNSContentHandler(outputFile)) {
 100             xmlReader.setContentHandler(handler);
 101             xmlReader.parse(new InputSource(fis));
 102         }
 103         assertTrue(compareWithGold(goldFile, outputFile));
 104     }
 105 
 106     /**
 107      * namespace processing is not enabled. Hence namespace-prefix is
 108      * expected to be automaically on. So it is a False-True combination.
 109      * The test is to test XMLReader with these conditions.
 110      *
 111      * @throws Exception If any errors occur.
 112      */
 113     public void testWithFalseTrue()throws Exception {
 114         String outputFile = USER_DIR + "XRNSTableFT.out";
 115         String goldFile = GOLDEN_DIR + "NSTableFTGF.out";
 116 
 117         SAXParserFactory spf = SAXParserFactory.newInstance();
 118         spf.setNamespaceAware(false);
 119         XMLReader xmlReader = spf.newSAXParser().getXMLReader();
 120         try (FileInputStream fis = new FileInputStream(xmlFile);
 121             MyNSContentHandler handler = new MyNSContentHandler(outputFile)) {
 122             xmlReader.setContentHandler(handler);
 123             xmlReader.parse(new InputSource(fis));
 124         }
 125         assertTrue(compareWithGold(goldFile, outputFile));
 126     }
 127 }