author | joehw |
Thu, 16 Oct 2014 15:51:12 -0700 | |
changeset 27216 | e63176413817 |
permissions | -rw-r--r-- |
27216
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
1 |
/* |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
2 |
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
4 |
* |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
8 |
* |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
13 |
* accompanied this code). |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
14 |
* |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
18 |
* |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
21 |
* questions. |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
22 |
*/ |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
23 |
|
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
24 |
package javax.xml.transform.ptests; |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
25 |
|
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
26 |
import java.io.BufferedWriter; |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
27 |
import java.io.FileWriter; |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
28 |
import java.io.IOException; |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
29 |
import java.nio.file.Files; |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
30 |
import java.nio.file.Path; |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
31 |
import java.nio.file.Paths; |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
32 |
import javax.xml.transform.TransformerConfigurationException; |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
33 |
import javax.xml.transform.TransformerFactory; |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
34 |
import javax.xml.transform.dom.DOMResult; |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
35 |
import static javax.xml.transform.ptests.TransformerTestConst.CLASS_DIR; |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
36 |
import static javax.xml.transform.ptests.TransformerTestConst.GOLDEN_DIR; |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
37 |
import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR; |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
38 |
import javax.xml.transform.sax.SAXSource; |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
39 |
import javax.xml.transform.sax.SAXTransformerFactory; |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
40 |
import javax.xml.transform.sax.TransformerHandler; |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
41 |
import static jaxp.library.JAXPTestUtilities.compareWithGold; |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
42 |
import static jaxp.library.JAXPTestUtilities.failCleanup; |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
43 |
import static jaxp.library.JAXPTestUtilities.failUnexpected; |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
44 |
import static org.testng.Assert.assertTrue; |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
45 |
import org.testng.annotations.Test; |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
46 |
import org.w3c.dom.Attr; |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
47 |
import org.w3c.dom.NamedNodeMap; |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
48 |
import org.w3c.dom.Node; |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
49 |
import org.w3c.dom.NodeList; |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
50 |
import org.xml.sax.InputSource; |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
51 |
import org.xml.sax.SAXException; |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
52 |
import org.xml.sax.XMLReader; |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
53 |
import org.xml.sax.helpers.XMLReaderFactory; |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
54 |
|
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
55 |
/** |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
56 |
* DOM parse on test file to be compared with golden output file. No Exception |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
57 |
* is expected. |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
58 |
*/ |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
59 |
public class DOMResultTest01 { |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
60 |
/** |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
61 |
* Unit test for simple DOM parsing. |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
62 |
*/ |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
63 |
@Test |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
64 |
public void testcase01() { |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
65 |
String resultFile = CLASS_DIR + "domresult01.out"; |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
66 |
String goldFile = GOLDEN_DIR + "domresult01GF.out"; |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
67 |
String xsltFile = XML_DIR + "cities.xsl"; |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
68 |
String xmlFile = XML_DIR + "cities.xml"; |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
69 |
|
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
70 |
try { |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
71 |
XMLReader reader = XMLReaderFactory.createXMLReader(); |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
72 |
SAXTransformerFactory saxTFactory |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
73 |
= (SAXTransformerFactory) TransformerFactory.newInstance(); |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
74 |
SAXSource saxSource = new SAXSource(new InputSource(xsltFile)); |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
75 |
TransformerHandler handler |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
76 |
= saxTFactory.newTransformerHandler(saxSource); |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
77 |
|
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
78 |
DOMResult result = new DOMResult(); |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
79 |
|
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
80 |
handler.setResult(result); |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
81 |
reader.setContentHandler(handler); |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
82 |
reader.parse(xmlFile); |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
83 |
|
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
84 |
Node node = result.getNode(); |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
85 |
try (BufferedWriter writer = new BufferedWriter(new FileWriter(resultFile))) { |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
86 |
writeNodes(node, writer); |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
87 |
} |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
88 |
assertTrue(compareWithGold(goldFile, resultFile)); |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
89 |
} catch (SAXException | TransformerConfigurationException |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
90 |
| IllegalArgumentException | IOException ex) { |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
91 |
failUnexpected(ex); |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
92 |
} finally { |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
93 |
try { |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
94 |
Path resultPath = Paths.get(resultFile); |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
95 |
if(Files.exists(resultPath)) |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
96 |
Files.delete(resultPath); |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
97 |
} catch (IOException ex) { |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
98 |
failCleanup(ex, resultFile); |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
99 |
} |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
100 |
} |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
101 |
} |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
102 |
|
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
103 |
/** |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
104 |
* Prints all node names, attributes to file |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
105 |
* @param node a node that need to be recursively access. |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
106 |
* @param bWriter file writer. |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
107 |
* @throws IOException if writing file failed. |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
108 |
*/ |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
109 |
private void writeNodes(Node node, BufferedWriter bWriter) throws IOException { |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
110 |
String str = "Node: " + node.getNodeName(); |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
111 |
bWriter.write( str, 0,str.length()); |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
112 |
bWriter.newLine(); |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
113 |
|
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
114 |
NamedNodeMap nnm = node.getAttributes(); |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
115 |
if (nnm != null && nnm.getLength() > 0) |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
116 |
for (int i=0; i<nnm.getLength(); i++) { |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
117 |
str = "AttributeName:" + ((Attr) nnm.item(i)).getName() + |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
118 |
", AttributeValue:" +((Attr) nnm.item(i)).getValue(); |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
119 |
bWriter.write( str, 0,str.length()); |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
120 |
bWriter.newLine(); |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
121 |
} |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
122 |
|
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
123 |
NodeList kids = node.getChildNodes(); |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
124 |
if (kids != null) |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
125 |
for (int i=0; i<kids.getLength(); i++) |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
126 |
writeNodes(kids.item(i), bWriter); |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
127 |
} |
e63176413817
8051540: Convert JAXP functional tests to jtreg(TestNG): SAX and Transform
joehw
parents:
diff
changeset
|
128 |
} |