author | joehw |
Mon, 08 Jun 2015 21:59:07 -0700 | |
changeset 31104 | 79a0696bbd7c |
parent 30707 | 47b636cd6456 |
child 31284 | e9b8469adb9a |
permissions | -rw-r--r-- |
27830 | 1 |
/* |
30707
47b636cd6456
8078596: jaxp tests failed in modular jdk due to internal class access
joehw
parents:
27830
diff
changeset
|
2 |
* Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved. |
27830 | 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 org.w3c.dom.ls; |
|
25 |
||
26 |
import java.io.IOException; |
|
27 |
import java.io.OutputStream; |
|
28 |
import java.io.StringReader; |
|
29 |
import java.io.Writer; |
|
30 |
||
31 |
import javax.xml.parsers.DocumentBuilder; |
|
32 |
import javax.xml.parsers.DocumentBuilderFactory; |
|
33 |
import javax.xml.parsers.ParserConfigurationException; |
|
34 |
||
35 |
import org.testng.Assert; |
|
36 |
import org.testng.annotations.Test; |
|
37 |
import org.w3c.dom.DOMConfiguration; |
|
38 |
import org.w3c.dom.DOMError; |
|
39 |
import org.w3c.dom.DOMErrorHandler; |
|
40 |
import org.w3c.dom.DOMImplementation; |
|
41 |
import org.w3c.dom.Document; |
|
42 |
import org.xml.sax.InputSource; |
|
43 |
import org.xml.sax.SAXException; |
|
44 |
||
45 |
||
46 |
/* |
|
31104
79a0696bbd7c
8080906: Develop test for Xerces Update: DOM L3 Serializer
joehw
parents:
30707
diff
changeset
|
47 |
* @bug 6439439 8080906 |
27830 | 48 |
* @summary Test LSSerializer. |
49 |
*/ |
|
50 |
public class LSSerializerTest { |
|
30707
47b636cd6456
8078596: jaxp tests failed in modular jdk due to internal class access
joehw
parents:
27830
diff
changeset
|
51 |
private static final String DOM_FORMAT_PRETTY_PRINT = "format-pretty-print"; |
27830 | 52 |
|
53 |
class DOMErrorHandlerImpl implements DOMErrorHandler { |
|
54 |
||
55 |
boolean NoOutputSpecifiedErrorReceived = false; |
|
56 |
||
57 |
public boolean handleError(final DOMError error) { |
|
58 |
// consume "no-output-specified" errors |
|
59 |
if ("no-output-specified".equalsIgnoreCase(error.getType())) { |
|
60 |
NoOutputSpecifiedErrorReceived = true; |
|
61 |
return true; |
|
62 |
} |
|
63 |
||
64 |
// unexpected error |
|
65 |
Assert.fail("Unexpected Error Type: " + error.getType() + " @ (" + error.getLocation().getLineNumber() + ", " |
|
66 |
+ error.getLocation().getColumnNumber() + ")" + ", " + error.getMessage()); |
|
67 |
||
68 |
return false; |
|
69 |
} |
|
70 |
} |
|
71 |
||
72 |
class Output implements LSOutput { |
|
73 |
public OutputStream getByteStream() { |
|
74 |
return null; |
|
75 |
} |
|
76 |
||
77 |
public void setByteStream(final OutputStream byteStream) { |
|
78 |
} |
|
79 |
||
80 |
public Writer getCharacterStream() { |
|
81 |
return null; |
|
82 |
} |
|
83 |
||
84 |
public void setCharacterStream(final Writer characterStream) { |
|
85 |
} |
|
86 |
||
87 |
public String getSystemId() { |
|
88 |
return null; |
|
89 |
} |
|
90 |
||
91 |
public void setSystemId(final String systemId) { |
|
92 |
} |
|
93 |
||
94 |
public String getEncoding() { |
|
95 |
return "UTF8"; |
|
96 |
} |
|
97 |
||
98 |
public void setEncoding(final String encoding) { |
|
99 |
} |
|
100 |
} |
|
101 |
||
31104
79a0696bbd7c
8080906: Develop test for Xerces Update: DOM L3 Serializer
joehw
parents:
30707
diff
changeset
|
102 |
/* |
79a0696bbd7c
8080906: Develop test for Xerces Update: DOM L3 Serializer
joehw
parents:
30707
diff
changeset
|
103 |
* @bug 8080906 |
79a0696bbd7c
8080906: Develop test for Xerces Update: DOM L3 Serializer
joehw
parents:
30707
diff
changeset
|
104 |
* It will fail in a Jigsaw build until JDK-8080266 is fixed. |
79a0696bbd7c
8080906: Develop test for Xerces Update: DOM L3 Serializer
joehw
parents:
30707
diff
changeset
|
105 |
*/ |
79a0696bbd7c
8080906: Develop test for Xerces Update: DOM L3 Serializer
joehw
parents:
30707
diff
changeset
|
106 |
@Test |
79a0696bbd7c
8080906: Develop test for Xerces Update: DOM L3 Serializer
joehw
parents:
30707
diff
changeset
|
107 |
public void testDefaultLSSerializer() throws Exception { |
79a0696bbd7c
8080906: Develop test for Xerces Update: DOM L3 Serializer
joehw
parents:
30707
diff
changeset
|
108 |
DOMImplementationLS domImpl = (DOMImplementationLS) DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation(); |
79a0696bbd7c
8080906: Develop test for Xerces Update: DOM L3 Serializer
joehw
parents:
30707
diff
changeset
|
109 |
LSSerializer lsSerializer = domImpl.createLSSerializer(); |
79a0696bbd7c
8080906: Develop test for Xerces Update: DOM L3 Serializer
joehw
parents:
30707
diff
changeset
|
110 |
Assert.assertTrue(lsSerializer.getClass().getName().endsWith("dom3.LSSerializerImpl")); |
79a0696bbd7c
8080906: Develop test for Xerces Update: DOM L3 Serializer
joehw
parents:
30707
diff
changeset
|
111 |
} |
79a0696bbd7c
8080906: Develop test for Xerces Update: DOM L3 Serializer
joehw
parents:
30707
diff
changeset
|
112 |
|
27830 | 113 |
@Test |
114 |
public void testDOMErrorHandler() { |
|
115 |
||
116 |
final String XML_DOCUMENT = "<?xml version=\"1.0\"?>" + "<hello>" + "world" + "</hello>"; |
|
117 |
||
118 |
StringReader stringReader = new StringReader(XML_DOCUMENT); |
|
119 |
InputSource inputSource = new InputSource(stringReader); |
|
120 |
Document doc = null; |
|
121 |
try { |
|
122 |
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); |
|
123 |
// LSSerializer defaults to Namespace processing |
|
124 |
// so parsing must also |
|
125 |
documentBuilderFactory.setNamespaceAware(true); |
|
126 |
DocumentBuilder parser = documentBuilderFactory.newDocumentBuilder(); |
|
127 |
doc = parser.parse(inputSource); |
|
128 |
||
129 |
} catch (Throwable e) { |
|
130 |
e.printStackTrace(); |
|
131 |
Assert.fail(e.toString()); |
|
132 |
} |
|
133 |
||
134 |
DOMImplementation impl = doc.getImplementation(); |
|
135 |
DOMImplementationLS implLS = (DOMImplementationLS) impl.getFeature("LS", "3.0"); |
|
136 |
LSSerializer writer = implLS.createLSSerializer(); |
|
137 |
DOMErrorHandlerImpl eh = new DOMErrorHandlerImpl(); |
|
138 |
writer.getDomConfig().setParameter("error-handler", eh); |
|
139 |
||
140 |
boolean serialized = false; |
|
141 |
try { |
|
142 |
serialized = writer.write(doc, new Output()); |
|
143 |
||
144 |
// unexpected success |
|
145 |
Assert.fail("Serialized without raising an LSException due to " + "'no-output-specified'."); |
|
146 |
} catch (LSException lsException) { |
|
147 |
// expected exception |
|
148 |
System.out.println("Expected LSException: " + lsException.toString()); |
|
149 |
// continue processing |
|
150 |
} |
|
151 |
||
152 |
Assert.assertFalse(serialized, "Expected writer.write(doc, new Output()) == false"); |
|
153 |
||
154 |
Assert.assertTrue(eh.NoOutputSpecifiedErrorReceived, "'no-output-specified' error was expected"); |
|
155 |
} |
|
156 |
||
157 |
@Test |
|
158 |
public void testFormatPrettyPrint() { |
|
159 |
||
160 |
final String XML_DOCUMENT = "<?xml version=\"1.0\" encoding=\"UTF-16\"?>\n" + "<hello>" + "world" + "<child><children/><children/></child>" |
|
161 |
+ "</hello>"; |
|
162 |
/**JDK-8035467 |
|
163 |
* no newline in default output |
|
164 |
*/ |
|
165 |
final String XML_DOCUMENT_DEFAULT_PRINT = |
|
166 |
"<?xml version=\"1.0\" encoding=\"UTF-16\"?>" |
|
167 |
+ "<hello>" |
|
168 |
+ "world" |
|
169 |
+ "<child><children/><children/></child>" |
|
170 |
+ "</hello>"; |
|
171 |
||
172 |
final String XML_DOCUMENT_PRETTY_PRINT = "<?xml version=\"1.0\" encoding=\"UTF-16\"?>" + "<hello>" + "world" + "<child>" + "\n" + " " |
|
173 |
+ "<children/>" + "\n" + " " + "<children/>" + "\n" + " " + "</child>" + "\n" + "</hello>" + "\n"; |
|
174 |
||
175 |
// it all begins with a Document |
|
176 |
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); |
|
177 |
DocumentBuilder documentBuilder = null; |
|
178 |
try { |
|
179 |
documentBuilder = documentBuilderFactory.newDocumentBuilder(); |
|
180 |
} catch (ParserConfigurationException parserConfigurationException) { |
|
181 |
parserConfigurationException.printStackTrace(); |
|
182 |
Assert.fail(parserConfigurationException.toString()); |
|
183 |
} |
|
184 |
Document document = null; |
|
185 |
||
186 |
StringReader stringReader = new StringReader(XML_DOCUMENT); |
|
187 |
InputSource inputSource = new InputSource(stringReader); |
|
188 |
try { |
|
189 |
document = documentBuilder.parse(inputSource); |
|
190 |
} catch (SAXException saxException) { |
|
191 |
saxException.printStackTrace(); |
|
192 |
Assert.fail(saxException.toString()); |
|
193 |
} catch (IOException ioException) { |
|
194 |
ioException.printStackTrace(); |
|
195 |
Assert.fail(ioException.toString()); |
|
196 |
} |
|
197 |
||
198 |
// query DOM Interfaces to get to a LSSerializer |
|
199 |
DOMImplementation domImplementation = documentBuilder.getDOMImplementation(); |
|
200 |
DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementation; |
|
201 |
LSSerializer lsSerializer = domImplementationLS.createLSSerializer(); |
|
202 |
||
203 |
// get configuration |
|
204 |
DOMConfiguration domConfiguration = lsSerializer.getDomConfig(); |
|
205 |
||
206 |
// query current configuration |
|
30707
47b636cd6456
8078596: jaxp tests failed in modular jdk due to internal class access
joehw
parents:
27830
diff
changeset
|
207 |
Boolean defaultFormatPrettyPrint = (Boolean) domConfiguration.getParameter(DOM_FORMAT_PRETTY_PRINT); |
47b636cd6456
8078596: jaxp tests failed in modular jdk due to internal class access
joehw
parents:
27830
diff
changeset
|
208 |
Boolean canSetFormatPrettyPrintFalse = (Boolean) domConfiguration.canSetParameter(DOM_FORMAT_PRETTY_PRINT, Boolean.FALSE); |
47b636cd6456
8078596: jaxp tests failed in modular jdk due to internal class access
joehw
parents:
27830
diff
changeset
|
209 |
Boolean canSetFormatPrettyPrintTrue = (Boolean) domConfiguration.canSetParameter(DOM_FORMAT_PRETTY_PRINT, Boolean.TRUE); |
27830 | 210 |
|
30707
47b636cd6456
8078596: jaxp tests failed in modular jdk due to internal class access
joehw
parents:
27830
diff
changeset
|
211 |
System.out.println(DOM_FORMAT_PRETTY_PRINT + " default/can set false/can set true = " + defaultFormatPrettyPrint + "/" |
27830 | 212 |
+ canSetFormatPrettyPrintFalse + "/" + canSetFormatPrettyPrintTrue); |
213 |
||
214 |
// test values |
|
30707
47b636cd6456
8078596: jaxp tests failed in modular jdk due to internal class access
joehw
parents:
27830
diff
changeset
|
215 |
Assert.assertEquals(defaultFormatPrettyPrint, Boolean.FALSE, "Default value of " + DOM_FORMAT_PRETTY_PRINT + " should be " + Boolean.FALSE); |
27830 | 216 |
|
30707
47b636cd6456
8078596: jaxp tests failed in modular jdk due to internal class access
joehw
parents:
27830
diff
changeset
|
217 |
Assert.assertEquals(canSetFormatPrettyPrintFalse, Boolean.TRUE, "Can set " + DOM_FORMAT_PRETTY_PRINT + " to " + Boolean.FALSE + " should be " |
27830 | 218 |
+ Boolean.TRUE); |
219 |
||
30707
47b636cd6456
8078596: jaxp tests failed in modular jdk due to internal class access
joehw
parents:
27830
diff
changeset
|
220 |
Assert.assertEquals(canSetFormatPrettyPrintTrue, Boolean.TRUE, "Can set " + DOM_FORMAT_PRETTY_PRINT + " to " + Boolean.TRUE + " should be " |
27830 | 221 |
+ Boolean.TRUE); |
222 |
||
223 |
// get default serialization |
|
224 |
String prettyPrintDefault = lsSerializer.writeToString(document); |
|
30707
47b636cd6456
8078596: jaxp tests failed in modular jdk due to internal class access
joehw
parents:
27830
diff
changeset
|
225 |
System.out.println("(default) " + DOM_FORMAT_PRETTY_PRINT + "==" + (Boolean) domConfiguration.getParameter(DOM_FORMAT_PRETTY_PRINT) |
27830 | 226 |
+ ": \n\"" + prettyPrintDefault + "\""); |
227 |
||
30707
47b636cd6456
8078596: jaxp tests failed in modular jdk due to internal class access
joehw
parents:
27830
diff
changeset
|
228 |
Assert.assertEquals(XML_DOCUMENT_DEFAULT_PRINT, prettyPrintDefault, "Invalid serialization with default value, " + DOM_FORMAT_PRETTY_PRINT + "==" |
47b636cd6456
8078596: jaxp tests failed in modular jdk due to internal class access
joehw
parents:
27830
diff
changeset
|
229 |
+ (Boolean) domConfiguration.getParameter(DOM_FORMAT_PRETTY_PRINT)); |
27830 | 230 |
|
231 |
// configure LSSerializer to not format-pretty-print |
|
30707
47b636cd6456
8078596: jaxp tests failed in modular jdk due to internal class access
joehw
parents:
27830
diff
changeset
|
232 |
domConfiguration.setParameter(DOM_FORMAT_PRETTY_PRINT, Boolean.FALSE); |
27830 | 233 |
String prettyPrintFalse = lsSerializer.writeToString(document); |
30707
47b636cd6456
8078596: jaxp tests failed in modular jdk due to internal class access
joehw
parents:
27830
diff
changeset
|
234 |
System.out.println("(FALSE) " + DOM_FORMAT_PRETTY_PRINT + "==" + (Boolean) domConfiguration.getParameter(DOM_FORMAT_PRETTY_PRINT) |
27830 | 235 |
+ ": \n\"" + prettyPrintFalse + "\""); |
236 |
||
30707
47b636cd6456
8078596: jaxp tests failed in modular jdk due to internal class access
joehw
parents:
27830
diff
changeset
|
237 |
Assert.assertEquals(XML_DOCUMENT_DEFAULT_PRINT, prettyPrintFalse, "Invalid serialization with FALSE value, " + DOM_FORMAT_PRETTY_PRINT + "==" |
47b636cd6456
8078596: jaxp tests failed in modular jdk due to internal class access
joehw
parents:
27830
diff
changeset
|
238 |
+ (Boolean) domConfiguration.getParameter(DOM_FORMAT_PRETTY_PRINT)); |
27830 | 239 |
|
240 |
// configure LSSerializer to format-pretty-print |
|
30707
47b636cd6456
8078596: jaxp tests failed in modular jdk due to internal class access
joehw
parents:
27830
diff
changeset
|
241 |
domConfiguration.setParameter(DOM_FORMAT_PRETTY_PRINT, Boolean.TRUE); |
27830 | 242 |
String prettyPrintTrue = lsSerializer.writeToString(document); |
30707
47b636cd6456
8078596: jaxp tests failed in modular jdk due to internal class access
joehw
parents:
27830
diff
changeset
|
243 |
System.out.println("(TRUE) " + DOM_FORMAT_PRETTY_PRINT + "==" + (Boolean) domConfiguration.getParameter(DOM_FORMAT_PRETTY_PRINT) |
27830 | 244 |
+ ": \n\"" + prettyPrintTrue + "\""); |
245 |
||
30707
47b636cd6456
8078596: jaxp tests failed in modular jdk due to internal class access
joehw
parents:
27830
diff
changeset
|
246 |
Assert.assertEquals(XML_DOCUMENT_PRETTY_PRINT, prettyPrintTrue, "Invalid serialization with TRUE value, " + DOM_FORMAT_PRETTY_PRINT + "==" |
47b636cd6456
8078596: jaxp tests failed in modular jdk due to internal class access
joehw
parents:
27830
diff
changeset
|
247 |
+ (Boolean) domConfiguration.getParameter(DOM_FORMAT_PRETTY_PRINT)); |
27830 | 248 |
} |
249 |
||
250 |
@Test |
|
251 |
public void testXML11() { |
|
252 |
||
253 |
/** |
|
254 |
* XML 1.1 document to parse. |
|
255 |
*/ |
|
256 |
final String XML11_DOCUMENT = "<?xml version=\"1.1\" encoding=\"UTF-16\"?>\n" + "<hello>" + "world" + "<child><children/><children/></child>" |
|
257 |
+ "</hello>"; |
|
258 |
||
259 |
/**JDK-8035467 |
|
260 |
* no newline in default output |
|
261 |
*/ |
|
262 |
final String XML11_DOCUMENT_OUTPUT = |
|
263 |
"<?xml version=\"1.1\" encoding=\"UTF-16\"?>" |
|
264 |
+ "<hello>" |
|
265 |
+ "world" |
|
266 |
+ "<child><children/><children/></child>" |
|
267 |
+ "</hello>"; |
|
268 |
||
269 |
// it all begins with a Document |
|
270 |
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); |
|
271 |
DocumentBuilder documentBuilder = null; |
|
272 |
try { |
|
273 |
documentBuilder = documentBuilderFactory.newDocumentBuilder(); |
|
274 |
} catch (ParserConfigurationException parserConfigurationException) { |
|
275 |
parserConfigurationException.printStackTrace(); |
|
276 |
Assert.fail(parserConfigurationException.toString()); |
|
277 |
} |
|
278 |
Document document = null; |
|
279 |
||
280 |
StringReader stringReader = new StringReader(XML11_DOCUMENT); |
|
281 |
InputSource inputSource = new InputSource(stringReader); |
|
282 |
try { |
|
283 |
document = documentBuilder.parse(inputSource); |
|
284 |
} catch (SAXException saxException) { |
|
285 |
saxException.printStackTrace(); |
|
286 |
Assert.fail(saxException.toString()); |
|
287 |
} catch (IOException ioException) { |
|
288 |
ioException.printStackTrace(); |
|
289 |
Assert.fail(ioException.toString()); |
|
290 |
} |
|
291 |
||
292 |
// query DOM Interfaces to get to a LSSerializer |
|
293 |
DOMImplementation domImplementation = documentBuilder.getDOMImplementation(); |
|
294 |
DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementation; |
|
295 |
LSSerializer lsSerializer = domImplementationLS.createLSSerializer(); |
|
296 |
||
297 |
// get default serialization |
|
298 |
String defaultSerialization = lsSerializer.writeToString(document); |
|
299 |
||
300 |
System.out.println("XML 1.1 serialization = \"" + defaultSerialization + "\""); |
|
301 |
||
302 |
// output should == input |
|
303 |
Assert.assertEquals(XML11_DOCUMENT_OUTPUT, defaultSerialization, "Invalid serialization of XML 1.1 document: "); |
|
304 |
} |
|
305 |
} |