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