1 /* 2 * Copyright (c) 1999, 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 24 package javax.xml.parsers.ptests; 25 26 import static javax.xml.parsers.ptests.ParserTestConst.GOLDEN_DIR; 27 import static javax.xml.parsers.ptests.ParserTestConst.XML_DIR; 28 import static jaxp.library.JAXPTestUtilities.USER_DIR; 29 import static jaxp.library.JAXPTestUtilities.compareWithGold; 30 import static org.testng.Assert.assertTrue; 31 32 import java.io.File; 33 34 import javax.xml.parsers.DocumentBuilderFactory; 35 import javax.xml.transform.TransformerFactory; 36 import javax.xml.transform.dom.DOMSource; 37 import javax.xml.transform.sax.SAXResult; 38 39 import org.testng.annotations.DataProvider; 40 import org.testng.annotations.Listeners; 41 import org.testng.annotations.Test; 42 import org.w3c.dom.Document; 43 44 /** 45 * This tests DocumentBuilderFactory for namespace processing and no-namespace 46 * processing. 47 */ 48 @Listeners({jaxp.library.FilePolicy.class}) 49 public class DBFNamespaceTest { 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 = USER_DIR + "dbfnstest01.out"; 60 String goldfile1 = GOLDEN_DIR + "dbfnstest01GF.out"; 61 62 DocumentBuilderFactory dbf2 = DocumentBuilderFactory.newInstance(); 63 dbf2.setNamespaceAware(true); 64 String outputfile2 = USER_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 Exception If any errors occur. 76 */ 77 @Test(dataProvider = "input-provider") 78 public void testNamespaceTest(DocumentBuilderFactory dbf, String outputfile, 79 String goldfile) throws Exception { 80 Document doc = dbf.newDocumentBuilder().parse(new File(XML_DIR, "namespace1.xml")); 81 dummyTransform(doc, outputfile); 82 assertTrue(compareWithGold(goldfile, outputfile)); 83 } 84 85 /** 86 * This method transforms a Node without any xsl file and uses SAXResult to 87 * invoke the callbacks through a ContentHandler. If namespace processing is 88 * not chosen, namespaceURI in callbacks should be an empty string otherwise 89 * it should be namespaceURI. 90 * 91 * @throws Exception If any errors occur. 92 */ 93 private void dummyTransform(Document document, String fileName) 94 throws Exception { 95 DOMSource domSource = new DOMSource(document); 96 try(MyCHandler chandler = MyCHandler.newInstance(new File(fileName))) { 97 TransformerFactory.newInstance().newTransformer(). 98 transform(domSource, new SAXResult(chandler)); 99 } 100 } 101 }