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