1 /* 2 * Copyright (c) 2014, 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 24 package javax.xml.parsers.ptests; 25 26 import static jaxp.library.JAXPTestUtilities.CLASS_DIR; 27 import static jaxp.library.JAXPTestUtilities.compareWithGold; 28 import static org.testng.Assert.assertTrue; 29 import java.io.File; 30 import java.io.IOException; 31 import javax.xml.parsers.DocumentBuilderFactory; 32 import javax.xml.parsers.ParserConfigurationException; 33 import static javax.xml.parsers.ptests.TestUtils.GOLDEN_DIR; 34 import static javax.xml.parsers.ptests.TestUtils.XML_DIR; 35 import javax.xml.transform.TransformerException; 36 import javax.xml.transform.TransformerFactory; 37 import javax.xml.transform.dom.DOMSource; 38 import javax.xml.transform.sax.SAXResult; 39 import jaxp.library.JAXPFileBaseTest; 40 import org.testng.annotations.DataProvider; 41 import org.testng.annotations.Test; 42 import org.w3c.dom.Document; 43 import org.xml.sax.SAXException; 44 45 /** 46 * This tests DocumentBuilderFactory for namespace processing and no-namespace 47 * processing. 48 */ 49 public class DBFNamespaceTest extends JAXPFileBaseTest { 50 51 /** 52 * Provide input for the cases that supporting namespace or not. 53 * @return a two-dimensional array contains factory, output file name and 54 * golden validate file name. 55 */ 56 @DataProvider(name = "input-provider") 57 public Object[][] getInput() { 58 DocumentBuilderFactory dbf1 = DocumentBuilderFactory.newInstance(); 59 String outputfile1 = CLASS_DIR + "dbfnstest01.out"; 60 String goldfile1 = GOLDEN_DIR + "dbfnstest01GF.out"; 61 62 DocumentBuilderFactory dbf2 = DocumentBuilderFactory.newInstance(); 63 dbf2.setNamespaceAware(true); 64 String outputfile2 = CLASS_DIR + "dbfnstest02.out"; 65 String goldfile2 = GOLDEN_DIR + "dbfnstest02GF.out"; 66 return new Object[][] { { dbf1, outputfile1, goldfile1 }, { dbf2, outputfile2, goldfile2 } }; 67 } 68 69 /** 70 * Test to parse and transform a document without supporting namespace and 71 * with supporting namespace. 72 * @param dbf a Document Builder factory for creating document object. 73 * @param outputfile output file name. 74 * @param goldfile golden validate file name. 75 * @throws ParserConfigurationException if a DocumentBuilder cannot be 76 * created which satisfies the configuration requested. 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 TransformerException If an unrecoverable error occurs during the 81 * course of the transformation. 82 * @throws SAXException If any parse errors occur. 83 */ 84 @Test(dataProvider = "input-provider") 85 public void testNamespaceTest(DocumentBuilderFactory dbf, String outputfile, 86 String goldfile) throws ParserConfigurationException, IOException, 87 TransformerException, SAXException { 88 Document doc = dbf.newDocumentBuilder().parse( 89 new File(XML_DIR, "namespace1.xml")); 90 dummyTransform(doc, outputfile); 91 assertTrue(compareWithGold(goldfile, outputfile)); 92 } 93 94 /** 95 * This method transforms a Node without any xsl file and uses SAXResult to 96 * invoke the callbacks through a ContentHandler. If namespace processing is 97 * not chosen, namespaceURI in callbacks should be an empty string otherwise 98 * it should be namespaceURI. 99 * 100 * @throws TransformerException If an unrecoverable error occurs during the 101 * course of the transformation. 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 */ 106 private void dummyTransform(Document document, String fileName) throws 107 TransformerException, IOException { 108 DOMSource domSource = new DOMSource(document); 109 try(MyCHandler chandler = MyCHandler.newInstance(new File(fileName))) { 110 TransformerFactory.newInstance().newTransformer(). 111 transform(domSource, new SAXResult(chandler)); 112 } 113 } 114 }