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