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