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 tests the Attributes interface. Here the startElement() callback of 45 * ContentHandler has Attributes as one of its arguments. Attributes 46 * pertaining to an element are taken into this argument and various methods 47 * of Attributes interfaces are tested. 48 * This program uses Namespace processing without any namepsaces in xml file. 49 * This program uses Validation 50 */ 51 public class AttributesTest { 52 /** 53 * Unit test for Attributes interface. Prints all attributes into output 54 * file. Check it with golden file. 55 */ 56 @Test 57 public void testcase01() { 58 String outputFile = CLASS_DIR + "Attributes.out"; 59 String goldFile = GOLDEN_DIR + "AttributesGF.out"; 60 String xmlFile = XML_DIR + "family.xml"; 61 try { 62 SAXParserFactory spf = SAXParserFactory.newInstance(); 63 spf.setNamespaceAware(true); 64 spf.setFeature("http://xml.org/sax/features/namespace-prefixes", 65 true); 66 spf.setValidating(true); 67 SAXParser saxParser = spf.newSAXParser(); 68 MyAttrCHandler myAttrCHandler = new MyAttrCHandler(outputFile); 69 saxParser.parse(new File(xmlFile), myAttrCHandler); 70 myAttrCHandler.flushAndClose(); 71 assertTrue(compareWithGold(goldFile, outputFile)); 72 } catch (IOException | ParserConfigurationException | SAXException ex) { 73 failUnexpected(ex); 74 } finally { 75 try { 76 Path outputPath = Paths.get(outputFile); 77 if(Files.exists(outputPath)) 78 Files.delete(outputPath); 79 } catch (IOException ex) { 80 failCleanup(ex, outputFile); 81 } 82 } 83 } 84 } | 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.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.testng.annotations.Test; 34 import org.xml.sax.SAXException; 35 import static org.xml.sax.ptests.SAXTestConst.CLASS_DIR; 36 import static org.xml.sax.ptests.SAXTestConst.GOLDEN_DIR; 37 import static org.xml.sax.ptests.SAXTestConst.XML_DIR; 38 39 /** 40 * This tests the Attributes interface. Here the startElement() callback of 41 * ContentHandler has Attributes as one of its arguments. Attributes 42 * pertaining to an element are taken into this argument and various methods 43 * of Attributes interfaces are tested. 44 * This program uses Namespace processing without any namepsaces in xml file. 45 * This program uses Validation 46 */ 47 public class AttributesTest extends JAXPFileBaseTest { 48 /** 49 * Unit test for Attributes interface. Prints all attributes into output 50 * file. Check it with golden file. 51 * 52 * @throws SAXException If any parse errors occur. 53 * @throws IOException if the file exists but is a directory rather than 54 * a regular file, does not exist but cannot be created, or cannot 55 * be opened for any other reason. 56 * @throws ParserConfigurationException if a DocumentBuilder cannot be 57 * created which satisfies the configuration requested. 58 */ 59 @Test 60 public void testcase01() throws ParserConfigurationException, SAXException, 61 IOException { 62 String outputFile = CLASS_DIR + "Attributes.out"; 63 String goldFile = GOLDEN_DIR + "AttributesGF.out"; 64 String xmlFile = XML_DIR + "family.xml"; 65 66 SAXParserFactory spf = SAXParserFactory.newInstance(); 67 spf.setNamespaceAware(true); 68 spf.setFeature("http://xml.org/sax/features/namespace-prefixes", 69 true); 70 spf.setValidating(true); 71 SAXParser saxParser = spf.newSAXParser(); 72 MyAttrCHandler myAttrCHandler = new MyAttrCHandler(outputFile); 73 saxParser.parse(new File(xmlFile), myAttrCHandler); 74 myAttrCHandler.flushAndClose(); 75 assertTrue(compareWithGold(goldFile, outputFile)); 76 } 77 } |