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.BufferedWriter; 26 import java.io.FileInputStream; 27 import java.io.FileWriter; 28 import java.io.IOException; 29 import javax.xml.parsers.SAXParserFactory; 30 import jaxp.library.JAXPFileBaseTest; 31 import static jaxp.library.JAXPTestUtilities.USER_DIR; 32 import static jaxp.library.JAXPTestUtilities.compareWithGold; 33 import static org.testng.Assert.assertTrue; 34 import org.xml.sax.Attributes; 35 import org.xml.sax.InputSource; 36 import org.xml.sax.Locator; 37 import org.xml.sax.SAXException; 38 import org.xml.sax.SAXParseException; 39 import org.xml.sax.XMLReader; 40 import org.xml.sax.helpers.XMLFilterImpl; 41 import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR; 42 import static org.xml.sax.ptests.SAXTestConst.XML_DIR; 43 44 /** 45 * Set parent of XMLFilter to XMLReader. Parsing on XML file will invoke XMLFilter 46 * to write to output file. Test verifies output is same as the golden file. 47 */ 48 public class XMLFilterCBTest extends JAXPFileBaseTest { 49 /** 50 * Test XMLFilter working with XML reader. 51 * 52 * @throws Exception If any errors occur. 53 */ 54 public void testXMLFilterCB() throws Exception { 55 String outputFile = USER_DIR + "XMLFilter.out"; 56 String goldFile = GOLDEN_DIR + "XMLFilterGF.out"; 57 String xmlFile = XML_DIR + "namespace1.xml"; 58 59 try (FileInputStream fis = new FileInputStream(xmlFile); 60 MyXMLFilter myXmlFilter = new MyXMLFilter(outputFile)){ 61 SAXParserFactory spf = SAXParserFactory.newInstance(); 62 spf.setNamespaceAware(true); 63 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 64 myXmlFilter.setParent(xmlReader); 65 myXmlFilter.parse(new InputSource(fis)); 66 } 67 // Need close the output file before we compare it with golden file. 68 assertTrue(compareWithGold(goldFile, outputFile)); | 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.BufferedWriter; 32 import java.io.FileInputStream; 33 import java.io.FileWriter; 34 import java.io.IOException; 35 36 import javax.xml.parsers.SAXParserFactory; 37 38 import org.testng.annotations.Listeners; 39 import org.testng.annotations.Test; 40 import org.xml.sax.Attributes; 41 import org.xml.sax.InputSource; 42 import org.xml.sax.Locator; 43 import org.xml.sax.SAXException; 44 import org.xml.sax.SAXParseException; 45 import org.xml.sax.XMLReader; 46 import org.xml.sax.helpers.XMLFilterImpl; 47 48 /** 49 * Set parent of XMLFilter to XMLReader. Parsing on XML file will invoke XMLFilter 50 * to write to output file. Test verifies output is same as the golden file. 51 */ 52 @Test 53 @Listeners({jaxp.library.FilePolicy.class}) 54 public class XMLFilterCBTest { 55 /** 56 * Test XMLFilter working with XML reader. 57 * 58 * @throws Exception If any errors occur. 59 */ 60 public void testXMLFilterCB() throws Exception { 61 String outputFile = USER_DIR + "XMLFilter.out"; 62 String goldFile = GOLDEN_DIR + "XMLFilterGF.out"; 63 String xmlFile = XML_DIR + "namespace1.xml"; 64 65 try (FileInputStream fis = new FileInputStream(xmlFile); 66 MyXMLFilter myXmlFilter = new MyXMLFilter(outputFile)){ 67 SAXParserFactory spf = SAXParserFactory.newInstance(); 68 spf.setNamespaceAware(true); 69 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 70 myXmlFilter.setParent(xmlReader); 71 myXmlFilter.parse(new InputSource(fis)); 72 } 73 // Need close the output file before we compare it with golden file. 74 assertTrue(compareWithGold(goldFile, outputFile)); |