1 /*
   2  * Copyright (c) 2003, 2015, 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.File;
  26 import javax.xml.parsers.SAXParserFactory;
  27 import jaxp.library.JAXPFileBaseTest;
  28 import static jaxp.library.JAXPTestUtilities.USER_DIR;
  29 import static jaxp.library.JAXPTestUtilities.compareWithGold;
  30 import static org.testng.Assert.assertTrue;
  31 import org.testng.annotations.Test;
  32 import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR;
  33 import static org.xml.sax.ptests.SAXTestConst.XML_DIR;
  34 
  35 /**
  36  * This class contains the testcases to test SAXParser with regard to
  37  * Namespace Table defined at http://www.megginson.com/SAX/Java/namespaces.html
  38  */
  39 public class SAXParserNSTableTest extends JAXPFileBaseTest {
  40     /**
  41      * namespace processing is enabled. namespace-prefix is also is enabled.
  42      * So it is a True-True combination.
  43      * The test is to test SAXParser with these conditions.
  44      *
  45      * @throws Exception If any errors occur.
  46      */
  47     @Test
  48     public void testWithTrueTrue() throws Exception {
  49         String outputFile = USER_DIR + "SPNSTableTT.out";
  50         String goldFile = GOLDEN_DIR + "NSTableTTGF.out";
  51         String xmlFile = XML_DIR + "namespace1.xml";
  52         SAXParserFactory spf = SAXParserFactory.newInstance();
  53         spf.setNamespaceAware(true);
  54         spf.setFeature("http://xml.org/sax/features/namespace-prefixes",
  55                                     true);
  56         try (MyNSContentHandler handler = new MyNSContentHandler(outputFile)) {
  57             spf.newSAXParser().parse(new File(xmlFile), handler);
  58         }
  59         assertTrue(compareWithGold(goldFile, outputFile));
  60 
  61     }
  62     /**
  63      * namespace processing is enabled. Hence namespace-prefix is
  64      * expected to be automatically off. So it is a True-False combination.
  65      * The test is to test SAXParser with these conditions.
  66      *
  67      * @throws Exception If any errors occur.
  68      */
  69     public void testWithTrueFalse() throws Exception {
  70         String outputFile = USER_DIR + "SPNSTableTF.out";
  71         String goldFile = GOLDEN_DIR + "NSTableTFGF.out";
  72         String xmlFile = XML_DIR + "namespace1.xml";
  73         SAXParserFactory spf = SAXParserFactory.newInstance();
  74         spf.setNamespaceAware(true);
  75         try (MyNSContentHandler handler = new MyNSContentHandler(outputFile)) {
  76             spf.newSAXParser().parse(new File(xmlFile), handler);
  77         }
  78         assertTrue(compareWithGold(goldFile, outputFile));
  79     }
  80 
  81     /**
  82      * namespace processing is not enabled. Hence namespace-prefix is
  83      * expected to be automatically on. So it is a False-True combination.
  84      * The test is to test SAXParser with these conditions.
  85      *
  86      * @throws Exception If any errors occur.
  87      */
  88     public void testWithFalseTrue() throws Exception {
  89         String outputFile = USER_DIR + "SPNSTableFT.out";
  90         String goldFile = GOLDEN_DIR + "NSTableFTGF.out";
  91         String xmlFile = XML_DIR + "namespace1.xml";
  92         SAXParserFactory spf = SAXParserFactory.newInstance();
  93         spf.setNamespaceAware(true);
  94         try (MyNSContentHandler handler = new MyNSContentHandler(outputFile)) {
  95             spf.newSAXParser().parse(new File(xmlFile), handler);
  96         }
  97         assertTrue(compareWithGold(goldFile, outputFile));
  98     }
  99 }