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.xml.sax.InputSource;
  38 import org.xml.sax.XMLReader;
  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 @Listeners({jaxp.library.FilePolicy.class})
  45 public class XMLReaderNSTableTest {
  46     /**
  47      * XML file that used to be parsed.
  48      */
  49     private static final String xmlFile = XML_DIR + "namespace1.xml";
  50 
  51     /**
  52      * XML namespaces prefixes.
  53      */
  54     private static final String NAMESPACE_PREFIXES =
  55                 "http://xml.org/sax/features/namespace-prefixes";
  56     /**
  57      * namespace processing is enabled. namespace-prefix is also is enabled.
  58      * So it is a True-True combination.
  59      * The test is to test XMLReader with these conditions.
  60      *
  61      * @throws Exception If any errors occur.
  62      */
  63     public void testWithTrueTrue() throws Exception {
  64         String outputFile = USER_DIR + "XRNSTableTT.out";
  65         String goldFile = GOLDEN_DIR + "NSTableTTGF.out";
  66 
  67         SAXParserFactory spf = SAXParserFactory.newInstance();
  68         spf.setNamespaceAware(true);
  69         XMLReader xmlReader = spf.newSAXParser().getXMLReader();
  70         xmlReader.setFeature(NAMESPACE_PREFIXES, true);
  71 
  72         try (FileInputStream fis = new FileInputStream(xmlFile);
  73             MyNSContentHandler handler = new MyNSContentHandler(outputFile);) {
  74             xmlReader.setContentHandler(handler);
  75             xmlReader.parse(new InputSource(fis));
  76         }
  77         assertTrue(compareWithGold(goldFile, outputFile));
  78     }
  79 
  80     /**
  81      * Namespace processing is enabled. Hence namespace-prefix is
  82      * expected to be automatically off. So it is a True-False combination.
  83      * The test is to test XMLReader with these conditions.
  84      *
  85      * @throws Exception If any errors occur.
  86      */
  87     public void testWithTrueFalse() throws Exception {
  88         String outputFile = USER_DIR + "XRNSTableTF.out";
  89         String goldFile = GOLDEN_DIR + "NSTableTFGF.out";
  90 
  91         SAXParserFactory spf = SAXParserFactory.newInstance();
  92         spf.setNamespaceAware(true);
  93         SAXParser saxParser = spf.newSAXParser();
  94         XMLReader xmlReader = saxParser.getXMLReader();
  95 
  96         try (FileInputStream fis = new FileInputStream(xmlFile);
  97             MyNSContentHandler handler = new MyNSContentHandler(outputFile)) {
  98             xmlReader.setContentHandler(handler);
  99             xmlReader.parse(new InputSource(fis));
 100         }
 101         assertTrue(compareWithGold(goldFile, outputFile));
 102     }
 103 
 104     /**
 105      * namespace processing is not enabled. Hence namespace-prefix is
 106      * expected to be automaically on. So it is a False-True combination.
 107      * The test is to test XMLReader with these conditions.
 108      *
 109      * @throws Exception If any errors occur.
 110      */
 111     public void testWithFalseTrue()throws Exception {
 112         String outputFile = USER_DIR + "XRNSTableFT.out";
 113         String goldFile = GOLDEN_DIR + "NSTableFTGF.out";
 114 
 115         SAXParserFactory spf = SAXParserFactory.newInstance();
 116         spf.setNamespaceAware(true);
 117         XMLReader xmlReader = spf.newSAXParser().getXMLReader();
 118         try (FileInputStream fis = new FileInputStream(xmlFile);
 119             MyNSContentHandler handler = new MyNSContentHandler(outputFile)) {
 120             xmlReader.setContentHandler(handler);
 121             xmlReader.parse(new InputSource(fis));
 122         }
 123         assertTrue(compareWithGold(goldFile, outputFile));
 124     }
 125 }