53 import org.xml.sax.SAXNotSupportedException; |
59 import org.xml.sax.SAXNotSupportedException; |
54 import org.xml.sax.XMLReader; |
60 import org.xml.sax.XMLReader; |
55 import org.xml.sax.helpers.AttributesImpl; |
61 import org.xml.sax.helpers.AttributesImpl; |
56 |
62 |
57 /* |
63 /* |
58 * @summary Test Transformer. |
64 * @summary Transformer Tests |
|
65 * @bug 6272879 6305029 6505031 8150704 |
59 */ |
66 */ |
60 public class TransformerTest { |
67 public class TransformerTest { |
61 |
68 private Transformer createTransformer() throws TransformerException { |
62 private static final String LINE_SEPARATOR = System.getProperty("line.separator"); |
69 return TransformerFactory.newInstance().newTransformer(); |
63 |
70 } |
64 private static final String XML_DOCUMENT = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<prefix:localName xmlns:prefix=\"namespaceUri\"/>"; |
71 |
65 |
72 private Transformer createTransformerFromInputstream(InputStream xslStream) throws TransformerException { |
66 //Test for JDK-6305029 |
73 return TransformerFactory.newInstance().newTransformer(new StreamSource(xslStream)); |
67 @Test |
74 } |
68 public final void testTransform() throws TransformerException { |
75 |
69 |
76 private Transformer createTransformerFromResource(String xslResource) throws TransformerException { |
70 // test SAXSource |
77 return TransformerFactory.newInstance().newTransformer(new StreamSource(getClass().getResource(xslResource).toString())); |
71 SAXSource saxSource = new SAXSource(new MyXMLReader(), new InputSource()); |
78 } |
72 |
79 |
73 StringWriter builder = new StringWriter(); |
80 private Document transformInputStreamToDocument(Transformer transformer, InputStream sourceStream) throws TransformerException { |
74 TransformerFactory.newInstance().newTransformer().transform(saxSource, new StreamResult(builder)); |
81 DOMResult response = new DOMResult(); |
75 |
82 transformer.transform(new StreamSource(sourceStream), response); |
76 AssertJUnit.assertEquals("Identity transform of SAXSource", XML_DOCUMENT, builder.toString()); |
83 return (Document)response.getNode(); |
77 |
84 } |
78 // test StreamSource |
85 |
79 StreamSource streamSource = new StreamSource(new StringReader(XML_DOCUMENT)); |
86 private StringWriter transformResourceToStringWriter(Transformer transformer, String xmlResource) throws TransformerException { |
80 |
87 StringWriter sw = new StringWriter(); |
81 StringWriter streamResult = new StringWriter(); |
88 transformer.transform(new StreamSource(getClass().getResource(xmlResource).toString()), new StreamResult(sw)); |
82 |
89 return sw; |
83 TransformerFactory.newInstance().newTransformer().transform(streamSource, new StreamResult(streamResult)); |
90 } |
84 |
91 |
85 AssertJUnit.assertEquals("Identity transform of StreamSource", XML_DOCUMENT, streamResult.toString()); |
92 /** |
86 } |
93 * Reads the contents of the given file into a string. |
87 |
94 * WARNING: this method adds a final line feed even if the last line of the file doesn't contain one. |
88 private static class MyXMLReader implements XMLReader { |
95 * |
89 |
96 * @param f |
|
97 * The file to read |
|
98 * @return The content of the file as a string, with line terminators as \"n" |
|
99 * for all platforms |
|
100 * @throws IOException |
|
101 * If there was an error reading |
|
102 */ |
|
103 private String getFileContentAsString(File f) throws IOException { |
|
104 try (BufferedReader reader = new BufferedReader(new FileReader(f))) { |
|
105 String line; |
|
106 StringBuilder sb = new StringBuilder(); |
|
107 while ((line = reader.readLine()) != null) { |
|
108 sb.append(line).append("\n"); |
|
109 } |
|
110 return sb.toString(); |
|
111 } |
|
112 } |
|
113 |
|
114 private class XMLReaderFor6305029 implements XMLReader { |
90 private static final String NAMESPACES = "http://xml.org/sax/features/namespaces"; |
115 private static final String NAMESPACES = "http://xml.org/sax/features/namespaces"; |
91 |
|
92 private static final String NAMESPACE_PREFIXES = "http://xml.org/sax/features/namespace-prefixes"; |
116 private static final String NAMESPACE_PREFIXES = "http://xml.org/sax/features/namespace-prefixes"; |
93 |
|
94 private boolean namespaces = true; |
117 private boolean namespaces = true; |
95 |
|
96 private boolean namespacePrefixes = false; |
118 private boolean namespacePrefixes = false; |
97 |
|
98 private EntityResolver resolver; |
119 private EntityResolver resolver; |
99 |
|
100 private DTDHandler dtdHandler; |
120 private DTDHandler dtdHandler; |
101 |
|
102 private ContentHandler contentHandler; |
121 private ContentHandler contentHandler; |
103 |
|
104 private ErrorHandler errorHandler; |
122 private ErrorHandler errorHandler; |
105 |
123 |
106 public boolean getFeature(final String name) throws SAXNotRecognizedException, SAXNotSupportedException { |
124 public boolean getFeature(final String name) throws SAXNotRecognizedException, SAXNotSupportedException { |
107 |
|
108 if (name.equals(NAMESPACES)) { |
125 if (name.equals(NAMESPACES)) { |
109 return namespaces; |
126 return namespaces; |
110 } else if (name.equals(NAMESPACE_PREFIXES)) { |
127 } else if (name.equals(NAMESPACE_PREFIXES)) { |
111 return namespacePrefixes; |
128 return namespacePrefixes; |
112 } else { |
129 } else { |
113 throw new SAXNotRecognizedException(); |
130 throw new SAXNotRecognizedException(); |
114 } |
131 } |
115 } |
132 } |
116 |
133 |
117 public void setFeature(final String name, final boolean value) throws SAXNotRecognizedException, SAXNotSupportedException { |
134 public void setFeature(final String name, final boolean value) throws SAXNotRecognizedException, SAXNotSupportedException { |
118 |
|
119 if (name.equals(NAMESPACES)) { |
135 if (name.equals(NAMESPACES)) { |
120 namespaces = value; |
136 namespaces = value; |
121 } else if (name.equals(NAMESPACE_PREFIXES)) { |
137 } else if (name.equals(NAMESPACE_PREFIXES)) { |
122 namespacePrefixes = value; |
138 namespacePrefixes = value; |
123 } else { |
139 } else { |
188 contentHandler.endPrefixMapping("prefix"); |
202 contentHandler.endPrefixMapping("prefix"); |
189 contentHandler.endDocument(); |
203 contentHandler.endDocument(); |
190 } |
204 } |
191 } |
205 } |
192 |
206 |
|
207 /* |
|
208 * @bug 6272879 |
|
209 * @summary Test for JDK-6272879 |
|
210 */ |
193 @Test |
211 @Test |
194 public final void testCR6272879() { |
212 public final void testBug6272879() throws IOException, TransformerException { |
195 |
213 final String LINE_SEPARATOR = System.getProperty("line.separator"); |
196 final String xsl = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" + LINE_SEPARATOR |
214 |
197 + "<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">" + LINE_SEPARATOR |
215 final String xsl = |
198 + "<xsl:output method=\"xml\" indent=\"no\" encoding=\"ISO-8859-1\"/>" + LINE_SEPARATOR + "<xsl:template match=\"/\">" + LINE_SEPARATOR |
216 "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" + LINE_SEPARATOR + |
199 + "<xsl:element name=\"TransformateurXML\">" + LINE_SEPARATOR + " <xsl:for-each select=\"XMLUtils/test\">" + LINE_SEPARATOR |
217 "<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">" + LINE_SEPARATOR + |
200 + " <xsl:element name=\"test2\">" + LINE_SEPARATOR + " <xsl:element name=\"valeur2\">" + LINE_SEPARATOR |
218 "<xsl:output method=\"xml\" indent=\"no\" encoding=\"ISO-8859-1\"/>" + LINE_SEPARATOR + |
201 + " <xsl:attribute name=\"attribut2\">" + LINE_SEPARATOR + " <xsl:value-of select=\"valeur/@attribut\"/>" + LINE_SEPARATOR |
219 "<xsl:template match=\"/\">" + LINE_SEPARATOR + |
202 + " </xsl:attribute>" + LINE_SEPARATOR + " <xsl:value-of select=\"valeur\"/>" + LINE_SEPARATOR + " </xsl:element>" |
220 "<xsl:element name=\"TransformateurXML\">" + LINE_SEPARATOR + |
203 + LINE_SEPARATOR + " </xsl:element>" + LINE_SEPARATOR + " </xsl:for-each>" + LINE_SEPARATOR + "</xsl:element>" + LINE_SEPARATOR |
221 " <xsl:for-each select=\"XMLUtils/test\">" + LINE_SEPARATOR + |
204 + "</xsl:template>" + LINE_SEPARATOR + "</xsl:stylesheet>"; |
222 " <xsl:element name=\"test2\">" + LINE_SEPARATOR + |
205 |
223 " <xsl:element name=\"valeur2\">" + LINE_SEPARATOR + |
206 final String sourceXml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" |
224 " <xsl:attribute name=\"attribut2\">" + LINE_SEPARATOR + |
207 + LINE_SEPARATOR |
225 " <xsl:value-of select=\"valeur/@attribut\"/>" + LINE_SEPARATOR + |
|
226 " </xsl:attribute>" + LINE_SEPARATOR + |
|
227 " <xsl:value-of select=\"valeur\"/>" + LINE_SEPARATOR + |
|
228 " </xsl:element>" + LINE_SEPARATOR + |
|
229 " </xsl:element>" + LINE_SEPARATOR + |
|
230 " </xsl:for-each>" + LINE_SEPARATOR + |
|
231 "</xsl:element>" + LINE_SEPARATOR + |
|
232 "</xsl:template>" + LINE_SEPARATOR + |
|
233 "</xsl:stylesheet>"; |
|
234 |
|
235 final String sourceXml = |
|
236 "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" + LINE_SEPARATOR + |
208 // "<!DOCTYPE XMLUtils [" + LINE_SEPARATOR + |
237 // "<!DOCTYPE XMLUtils [" + LINE_SEPARATOR + |
209 // "<!ELEMENT XMLUtils (test*)>" + LINE_SEPARATOR + |
238 // "<!ELEMENT XMLUtils (test*)>" + LINE_SEPARATOR + |
210 // "<!ELEMENT test (valeur*)>" + LINE_SEPARATOR + |
239 // "<!ELEMENT test (valeur*)>" + LINE_SEPARATOR + |
211 // "<!ELEMENT valeur (#PCDATA)>" + LINE_SEPARATOR + |
240 // "<!ELEMENT valeur (#PCDATA)>" + LINE_SEPARATOR + |
212 // "<!ATTLIST valeur attribut CDATA #REQUIRED>]>" + |
241 // "<!ATTLIST valeur attribut CDATA #REQUIRED>]>" + |
213 // LINE_SEPARATOR + |
242 // LINE_SEPARATOR + |
214 + "<XMLUtils>" + LINE_SEPARATOR + " <test>" + LINE_SEPARATOR + " <valeur attribut=\"Attribut 1\">Valeur 1</valeur>" + LINE_SEPARATOR |
243 "<XMLUtils>" + LINE_SEPARATOR + |
215 + " </test>" + LINE_SEPARATOR + " <test>" + LINE_SEPARATOR + " <valeur attribut=\"Attribut 2\">Valeur 2</valeur>" + LINE_SEPARATOR |
244 " <test>" + LINE_SEPARATOR + |
216 + " </test>" + LINE_SEPARATOR + "</XMLUtils>"; |
245 " <valeur attribut=\"Attribut 1\">Valeur 1</valeur>" + LINE_SEPARATOR + |
|
246 " </test>" + LINE_SEPARATOR + |
|
247 " <test>" + LINE_SEPARATOR + |
|
248 " <valeur attribut=\"Attribut 2\">Valeur 2</valeur>" + LINE_SEPARATOR + |
|
249 " </test>" + LINE_SEPARATOR + |
|
250 "</XMLUtils>"; |
217 |
251 |
218 Document document; |
252 Document document; |
219 Node node; |
253 Node node; |
220 |
254 |
221 System.out.println("Stylesheet:"); |
255 System.out.println("Stylesheet:"); |
228 System.out.println(sourceXml); |
262 System.out.println(sourceXml); |
229 System.out.println(); |
263 System.out.println(); |
230 |
264 |
231 System.out.println("Source file after transformation:"); |
265 System.out.println("Source file after transformation:"); |
232 System.out.println("================================="); |
266 System.out.println("================================="); |
233 document = getTransformation(xsl, sourceXml); |
267 document = transformInputStreamToDocument(createTransformerFromInputstream(new ByteArrayInputStream(xsl.getBytes())), |
234 |
268 new ByteArrayInputStream(sourceXml.getBytes())); |
235 System.out.println(document); |
269 OutputFormat format = new OutputFormat(); |
|
270 format.setIndenting(true); |
|
271 new XMLSerializer(System.out, format).serialize(document); |
|
272 System.out.println(); |
236 |
273 |
237 System.out.println("Node content for element valeur2:"); |
274 System.out.println("Node content for element valeur2:"); |
238 System.out.println("================================="); |
275 System.out.println("================================="); |
239 NodeList nodes = document.getElementsByTagName("valeur2"); |
276 NodeList nodes = document.getElementsByTagName("valeur2"); |
240 nodes = document.getElementsByTagName("valeur2"); |
277 nodes = document.getElementsByTagName("valeur2"); |
246 AssertJUnit.assertEquals("Node value mismatch", "Valeur " + (i + 1), node.getFirstChild().getNodeValue()); |
283 AssertJUnit.assertEquals("Node value mismatch", "Valeur " + (i + 1), node.getFirstChild().getNodeValue()); |
247 AssertJUnit.assertEquals("Node attribute mismatch", "Attribut " + (i + 1), node.getAttributes().item(0).getNodeValue()); |
284 AssertJUnit.assertEquals("Node attribute mismatch", "Attribut " + (i + 1), node.getAttributes().item(0).getNodeValue()); |
248 } |
285 } |
249 } |
286 } |
250 |
287 |
251 private static Document getTransformation(final String xsl, final String sourceXml) { |
288 /* |
252 |
289 * @bug 6305029 |
253 Transformer transformer; |
290 * @summary Test for JDK-6305029 |
254 DOMResult reponse; |
291 */ |
255 Document document = null; |
292 @Test |
256 |
293 public final void testBug6305029() throws TransformerException { |
257 try { |
294 final String XML_DOCUMENT = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<prefix:localName xmlns:prefix=\"namespaceUri\"/>"; |
258 InputStream in = new ByteArrayInputStream(xsl.getBytes()); |
295 |
259 transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(in)); |
296 // test SAXSource |
260 |
297 SAXSource saxSource = new SAXSource(new XMLReaderFor6305029(), new InputSource()); |
261 reponse = new DOMResult(); |
298 StringWriter resultWriter = new StringWriter(); |
262 transformer.transform(new StreamSource(new ByteArrayInputStream(sourceXml.getBytes())), reponse); |
299 createTransformer().transform(saxSource, new StreamResult(resultWriter)); |
263 document = (Document) reponse.getNode(); |
300 AssertJUnit.assertEquals("Identity transform of SAXSource", XML_DOCUMENT, resultWriter.toString()); |
264 } catch (Exception e) { |
301 |
265 String msg = "Exception in getTransformation: " + e; |
302 // test StreamSource |
266 System.err.println(msg); |
303 StreamSource streamSource = new StreamSource(new StringReader(XML_DOCUMENT)); |
267 Assert.fail(msg); |
304 resultWriter = new StringWriter(); |
268 } |
305 createTransformer().transform(streamSource, new StreamResult(resultWriter)); |
269 |
306 AssertJUnit.assertEquals("Identity transform of StreamSource", XML_DOCUMENT, resultWriter.toString()); |
270 return (document); |
307 } |
|
308 |
|
309 /* |
|
310 * @bug 6505031 |
|
311 * @summary Test transformer parses keys and their values coming from different xml documents. |
|
312 */ |
|
313 @Test |
|
314 public final void testBug6505031() throws TransformerException { |
|
315 Transformer transformer = createTransformerFromResource("transform.xsl"); |
|
316 transformer.setParameter("config", getClass().getResource("config.xml").toString()); |
|
317 transformer.setParameter("mapsFile", getClass().getResource("maps.xml").toString()); |
|
318 String s = transformResourceToStringWriter(transformer, "template.xml").toString(); |
|
319 Assert.assertTrue(s.contains("map1key1value") && s.contains("map2key1value")); |
|
320 } |
|
321 |
|
322 /* |
|
323 * @bug 8150704 |
|
324 * @summary Test that XSL transformation with lots of temporary result trees will not run out of DTM IDs. |
|
325 */ |
|
326 @Test |
|
327 public final void testBug8150704() throws TransformerException, IOException { |
|
328 System.out.println("Testing transformation of Bug8150704-1.xml..."); |
|
329 Transformer transformer = createTransformerFromResource("Bug8150704-1.xsl"); |
|
330 StringWriter result = transformResourceToStringWriter(transformer, "Bug8150704-1.xml"); |
|
331 String resultstring = result.toString().replaceAll("\\r\\n", "\n").replaceAll("\\r", "\n"); |
|
332 String reference = getFileContentAsString(new File(getClass().getResource("Bug8150704-1.ref").getPath())); |
|
333 Assert.assertEquals(resultstring, reference, "Output of transformation of Bug8150704-1.xml does not match reference"); |
|
334 System.out.println("Passed."); |
|
335 |
|
336 System.out.println("Testing transformation of Bug8150704-2.xml..."); |
|
337 transformer = createTransformerFromResource("Bug8150704-2.xsl"); |
|
338 result = transformResourceToStringWriter(transformer, "Bug8150704-2.xml"); |
|
339 resultstring = result.toString().replaceAll("\\r\\n", "\n").replaceAll("\\r", "\n"); |
|
340 reference = getFileContentAsString(new File(getClass().getResource("Bug8150704-2.ref").getPath())); |
|
341 Assert.assertEquals(resultstring, reference, "Output of transformation of Bug8150704-2.xml does not match reference"); |
|
342 System.out.println("Passed."); |
271 } |
343 } |
272 } |
344 } |