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 java.io.IOException; 27 import java.nio.file.Files; 28 import java.nio.file.Path; 29 import java.nio.file.Paths; 30 import javax.xml.parsers.ParserConfigurationException; 31 import javax.xml.parsers.SAXParser; 32 import javax.xml.parsers.SAXParserFactory; 33 import static jaxp.library.JAXPTestUtilities.compareWithGold; 34 import static jaxp.library.JAXPTestUtilities.failCleanup; 35 import static jaxp.library.JAXPTestUtilities.failUnexpected; 36 import static org.testng.Assert.assertTrue; 37 import org.testng.annotations.Test; 38 import org.xml.sax.SAXException; 39 import static org.xml.sax.ptests.SAXTestConst.CLASS_DIR; 40 import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR; 41 import static org.xml.sax.ptests.SAXTestConst.XML_DIR; 42 43 /** 44 * This class contains the testcases to test SAXParser with regard to 45 * Namespace Table defined at http://www.megginson.com/SAX/Java/namespaces.html 46 */ 47 public class SAXParserNSTableTest { 48 /** 49 * namespace processing is enabled. namespace-prefix is also is enabled. 50 * So it is a True-True combination. 51 * The test is to test SAXParser with these conditions 52 */ 53 @Test 54 public void testWithTrueTrue() { 55 String outputFile = CLASS_DIR + "SPNSTableTT.out"; 56 String goldFile = GOLDEN_DIR + "NSTableTTGF.out"; 57 String xmlFile = XML_DIR + "namespace1.xml"; 58 try { 59 SAXParserFactory spf = SAXParserFactory.newInstance(); 60 spf.setNamespaceAware(true); 61 spf.setFeature("http://xml.org/sax/features/namespace-prefixes", 62 true); 63 64 SAXParser saxParser = spf.newSAXParser(); 65 saxParser.parse(new File(xmlFile), new MyNSContentHandler(outputFile)); 66 assertTrue(compareWithGold(goldFile, outputFile)); 67 } catch (ParserConfigurationException | SAXException | IOException ex) { 68 failUnexpected(ex); 69 } finally { 70 try { 71 Path outputPath = Paths.get(outputFile); 72 if(Files.exists(outputPath)) 73 Files.delete(outputPath); 74 } catch (IOException ex) { 75 failCleanup(ex, outputFile); 76 } 77 } 78 } 79 /** 80 * namespace processing is enabled. Hence namespace-prefix is 81 * expected to be automaically off. So it is a True-False combination. 82 * The test is to test SAXParser with these conditions 83 */ 84 public void testWithTrueFalse() { 85 String outputFile = CLASS_DIR + "SPNSTableTF.out"; 86 String goldFile = GOLDEN_DIR + "NSTableTFGF.out"; 87 String xmlFile = XML_DIR + "namespace1.xml"; 88 try { 89 SAXParserFactory spf = SAXParserFactory.newInstance(); 90 spf.setNamespaceAware(true); 91 SAXParser saxParser = spf.newSAXParser(); 92 saxParser.parse(new File(xmlFile), new MyNSContentHandler(outputFile)); 93 assertTrue(compareWithGold(goldFile, outputFile)); 94 } catch (ParserConfigurationException | SAXException | IOException ex) { 95 failUnexpected(ex); 96 } finally { 97 try { 98 Path outputPath = Paths.get(outputFile); 99 if(Files.exists(outputPath)) 100 Files.delete(outputPath); 101 } catch (IOException ex) { 102 failCleanup(ex, outputFile); 103 } 104 } 105 } 106 107 /** 108 * namespace processing is not enabled. Hence namespace-prefix is 109 * expected to be automaically on. So it is a False-True combination. 110 * The test is to test SAXParser with these conditions 111 */ 112 public void testWithFalseTrue() { 113 String outputFile = CLASS_DIR + "SPNSTableFT.out"; 114 String goldFile = GOLDEN_DIR + "NSTableFTGF.out"; 115 String xmlFile = XML_DIR + "namespace1.xml"; 116 try { 117 SAXParserFactory spf = SAXParserFactory.newInstance(); 118 spf.setNamespaceAware(true); 119 SAXParser saxParser = spf.newSAXParser(); 120 saxParser.parse(new File(xmlFile), new MyNSContentHandler(outputFile)); 121 assertTrue(compareWithGold(goldFile, outputFile)); 122 } catch (ParserConfigurationException | SAXException | IOException ex) { 123 failUnexpected(ex); 124 } finally { 125 try { 126 Path outputPath = Paths.get(outputFile); 127 if(Files.exists(outputPath)) 128 Files.delete(outputPath); 129 } catch (IOException ex) { 130 failCleanup(ex, outputFile); 131 } 132 } 133 } 134 } | 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 java.io.IOException; 27 import javax.xml.parsers.ParserConfigurationException; 28 import javax.xml.parsers.SAXParserFactory; 29 import jaxp.library.JAXPFileBaseTest; 30 import static jaxp.library.JAXPTestUtilities.compareWithGold; 31 import static org.testng.Assert.assertTrue; 32 import org.testng.annotations.Test; 33 import org.xml.sax.SAXException; 34 import static org.xml.sax.ptests.SAXTestConst.CLASS_DIR; 35 import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR; 36 import static org.xml.sax.ptests.SAXTestConst.XML_DIR; 37 38 /** 39 * This class contains the testcases to test SAXParser with regard to 40 * Namespace Table defined at http://www.megginson.com/SAX/Java/namespaces.html 41 */ 42 public class SAXParserNSTableTest extends JAXPFileBaseTest { 43 /** 44 * namespace processing is enabled. namespace-prefix is also is enabled. 45 * So it is a True-True combination. 46 * The test is to test SAXParser with these conditions. 47 * 48 * @throws SAXException If there is a problem processing the document. 49 * @throws IOException if the file exists but is a directory rather than 50 * a regular file, does not exist but cannot be created, or cannot 51 * be opened for any other reason. 52 * @throws ParserConfigurationException if a DocumentBuilder cannot be 53 * created which satisfies the configuration requested. 54 */ 55 @Test 56 public void testWithTrueTrue() throws ParserConfigurationException, 57 SAXException, IOException { 58 String outputFile = CLASS_DIR + "SPNSTableTT.out"; 59 String goldFile = GOLDEN_DIR + "NSTableTTGF.out"; 60 String xmlFile = XML_DIR + "namespace1.xml"; 61 SAXParserFactory spf = SAXParserFactory.newInstance(); 62 spf.setNamespaceAware(true); 63 spf.setFeature("http://xml.org/sax/features/namespace-prefixes", 64 true); 65 try (MyNSContentHandler handler = new MyNSContentHandler(outputFile)) { 66 spf.newSAXParser().parse(new File(xmlFile), handler); 67 } 68 assertTrue(compareWithGold(goldFile, outputFile)); 69 70 } 71 /** 72 * namespace processing is enabled. Hence namespace-prefix is 73 * expected to be automatically off. So it is a True-False combination. 74 * The test is to test SAXParser with these conditions. 75 * 76 * @throws SAXException If there is a problem processing the document. 77 * @throws IOException if the file exists but is a directory rather than 78 * a regular file, does not exist but cannot be created, or cannot 79 * be opened for any other reason. 80 * @throws ParserConfigurationException if a DocumentBuilder cannot be 81 * created which satisfies the configuration requested. 82 */ 83 public void testWithTrueFalse() throws ParserConfigurationException, 84 SAXException, IOException { 85 String outputFile = CLASS_DIR + "SPNSTableTF.out"; 86 String goldFile = GOLDEN_DIR + "NSTableTFGF.out"; 87 String xmlFile = XML_DIR + "namespace1.xml"; 88 SAXParserFactory spf = SAXParserFactory.newInstance(); 89 spf.setNamespaceAware(true); 90 try (MyNSContentHandler handler = new MyNSContentHandler(outputFile)) { 91 spf.newSAXParser().parse(new File(xmlFile), handler); 92 } 93 assertTrue(compareWithGold(goldFile, outputFile)); 94 } 95 96 /** 97 * namespace processing is not enabled. Hence namespace-prefix is 98 * expected to be automatically on. So it is a False-True combination. 99 * The test is to test SAXParser with these conditions. 100 * 101 * @throws SAXException If there is a problem processing the document. 102 * @throws IOException if the file exists but is a directory rather than 103 * a regular file, does not exist but cannot be created, or cannot 104 * be opened for any other reason. 105 * @throws ParserConfigurationException if a DocumentBuilder cannot be 106 * created which satisfies the configuration requested. 107 */ 108 public void testWithFalseTrue() throws ParserConfigurationException, 109 SAXException, IOException { 110 String outputFile = CLASS_DIR + "SPNSTableFT.out"; 111 String goldFile = GOLDEN_DIR + "NSTableFTGF.out"; 112 String xmlFile = XML_DIR + "namespace1.xml"; 113 SAXParserFactory spf = SAXParserFactory.newInstance(); 114 spf.setNamespaceAware(true); 115 try (MyNSContentHandler handler = new MyNSContentHandler(outputFile)) { 116 spf.newSAXParser().parse(new File(xmlFile), handler); 117 } 118 assertTrue(compareWithGold(goldFile, outputFile)); 119 } 120 } |