< prev index next >
test/javax/xml/jaxp/unittest/transform/TransformerTest.java
Print this page
rev 1063 : 8172974: [JAXP] XALAN: Wrong result when transforming namespace unaware StAX Input
@@ -1,7 +1,7 @@
/*
- * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
@@ -36,21 +36,28 @@
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.transform.Source;
+import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXSource;
+import javax.xml.transform.stax.StAXSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.testng.Assert;
import org.testng.AssertJUnit;
+import org.testng.annotations.DataProvider;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
@@ -73,11 +80,11 @@
* @test
* @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
* @run testng/othervm -DrunSecMngr=true transform.TransformerTest
* @run testng/othervm transform.TransformerTest
* @summary Transformer Tests
- * @bug 6272879 6305029 6505031 8150704 8162598 8169112 8169631 8169772
+ * @bug 6272879 6305029 6505031 8150704 8162598 8169112 8169631 8169772 8172974
*/
@Listeners({jaxp.library.FilePolicy.class})
public class TransformerTest {
// some global constants
@@ -89,11 +96,11 @@
private static final String NAMESPACE_PREFIXES =
"http://xml.org/sax/features/namespace-prefixes";
private static abstract class TestTemplate {
- protected void printSnippet(String title, String snippet) {
+ protected static void printSnippet(String title, String snippet) {
StringBuilder div = new StringBuilder();
for (int i = 0; i < title.length(); i++)
div.append("=");
System.out.println(title + "\n" + div + "\n" + snippet + "\n");
}
@@ -328,11 +335,11 @@
t.transform(new StreamSource(getClass().getResource("template.xml").toString()), new StreamResult(sw));
String s = sw.toString();
Assert.assertTrue(s.contains("map1key1value") && s.contains("map2key1value"));
}
- private static class Test8169631 extends TestTemplate {
+ public static class Test8169631And8172974 extends TestTemplate {
private final static String xsl =
"<?xml version=\"1.0\"?>" + LINE_SEPARATOR +
"<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">" + LINE_SEPARATOR +
" <xsl:template match=\"/\">" + LINE_SEPARATOR +
" <xsl:variable name=\"Counter\" select=\"count(//row)\"/>" + LINE_SEPARATOR +
@@ -375,76 +382,129 @@
result.contains("<AttribCounter>" + attribCount +
"</AttribCounter>"), true, "Result of transformation from " +
type + " should have count of "+ attribCount + " attributes.");
}
- public void run() throws IOException, TransformerException,
- SAXException, ParserConfigurationException
+ // utility to obtain Transformer from TransformerFactory
+ private static Transformer getTransformer(TransformerFactory tf)
+ throws TransformerConfigurationException
{
- printSnippet("Source:", sourceXml);
+ return tf.newTransformer(
+ new StreamSource(new ByteArrayInputStream(xsl.getBytes()))
+ );
+ }
- printSnippet("Stylesheet:", xsl);
+ // utility to obtain Templates from TransformerFactory
+ private static Templates getTemplates(TransformerFactory tf)
+ throws TransformerConfigurationException
+ {
+ return tf.newTemplates(
+ new StreamSource(new ByteArrayInputStream(xsl.getBytes()))
+ );
+ }
- // create default transformer (namespace aware)
- TransformerFactory tf1 = TransformerFactory.newInstance();
- ByteArrayInputStream bais = new ByteArrayInputStream(xsl.getBytes());
- Transformer t1 = tf1.newTransformer(new StreamSource(bais));
+ // utility to construct StreamSource
+ private static StreamSource getStreamSource() {
+ return new StreamSource(
+ new ByteArrayInputStream(sourceXml.getBytes())
+ );
+ }
- // test transformation from stream source with namespace support
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- bais = new ByteArrayInputStream(sourceXml.getBytes());
- t1.transform(new StreamSource(bais), new StreamResult(baos));
- verifyResult("StreamSource with namespace support", baos.toString(), 0, 1);
-
- // test transformation from DOM source with namespace support
- bais.reset();
- baos.reset();
+ // utility to construct DOMSource from DocumentBuilderFactory
+ private static DOMSource getDOMSource(DocumentBuilderFactory dbf)
+ throws SAXException, IOException, ParserConfigurationException
+ {
+ return new DOMSource(
+ dbf.newDocumentBuilder().parse(
+ new InputSource(
+ new ByteArrayInputStream(sourceXml.getBytes())
+ )
+ )
+ );
+ }
+
+ // utility to construct SAXSource from SAXParserFactory
+ private static SAXSource getSAXSource(SAXParserFactory spf)
+ throws SAXException, ParserConfigurationException
+ {
+ return new SAXSource(
+ spf.newSAXParser().getXMLReader(),
+ new InputSource(new ByteArrayInputStream(sourceXml.getBytes()))
+ );
+ }
+
+ // utility to construct StAXSource from XMLInputFactory
+ private static StAXSource getStAXSource(XMLInputFactory xif)
+ throws XMLStreamException
+ {
+ return new StAXSource(
+ xif.createXMLStreamReader(new StringReader(sourceXml))
+ );
+ }
+
+ @DataProvider(name = "testmatrix")
+ public static Object[][] documentTestData()
+ throws TransformerConfigurationException, SAXException, IOException,
+ ParserConfigurationException, XMLStreamException
+ {
+ // get Transformers
+ TransformerFactory tf = TransformerFactory.newInstance();
+ Transformer t = getTransformer(tf);
+ Transformer tFromTemplates = getTemplates(tf).newTransformer();
+
+ // get DOMSource objects
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+ DOMSource domSourceWithoutNS = getDOMSource(dbf);
dbf.setNamespaceAware(true);
- Document doc = dbf.newDocumentBuilder().parse(new InputSource(bais));
- t1.transform(new DOMSource(doc), new StreamResult(baos));
- verifyResult("DOMSource with namespace support", baos.toString(), 0, 1);
-
- // test transformation from DOM source without namespace support
- bais.reset();
- baos.reset();
- dbf.setNamespaceAware(false);
- doc = dbf.newDocumentBuilder().parse(new InputSource(bais));
- t1.transform(new DOMSource(doc), new StreamResult(baos));
- verifyResult("DOMSource without namespace support", baos.toString(), 3, 3);
-
- // test transformation from SAX source with namespace support
- bais.reset();
- baos.reset();
+ DOMSource domSourceWithNS = getDOMSource(dbf);
+
+ // get SAXSource objects
SAXParserFactory spf = SAXParserFactory.newInstance();
+ SAXSource saxSourceWithoutNS = getSAXSource(spf);
spf.setNamespaceAware(true);
- XMLReader xmlr = spf.newSAXParser().getXMLReader();
- SAXSource saxS = new SAXSource(xmlr, new InputSource(bais));
- t1.transform(saxS, new StreamResult(baos));
- verifyResult("SAXSource with namespace support", baos.toString(), 0, 1);
-
- // test transformation from SAX source without namespace support
- bais.reset();
- baos.reset();
- spf.setNamespaceAware(false);
- xmlr = spf.newSAXParser().getXMLReader();
- saxS = new SAXSource(xmlr, new InputSource(bais));
- t1.transform(saxS, new StreamResult(baos));
- verifyResult("SAXSource without namespace support", baos.toString(), 3, 3);
- }
- }
+ SAXSource saxSourceWithNS = getSAXSource(spf);
- /*
- * @bug 8169631
- * @summary Test combinations of namespace awareness settings on
- * XSL transformations
- */
- @Test
- public final void testBug8169631() throws IOException, SAXException,
- TransformerException, ParserConfigurationException
- {
- new Test8169631().run();
+ // get StAXSource objects
+ XMLInputFactory xif = XMLInputFactory.newInstance();
+ StAXSource staxSourceWithNS = getStAXSource(xif);
+ xif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, false);
+ StAXSource staxSourceWithoutNS = getStAXSource(xif);
+
+ // print XML/XSL snippets to ease understanding of result
+ printSnippet("Source:", sourceXml);
+ printSnippet("Stylesheet:", xsl);
+
+ return new Object[][] {
+ // test StreamSource input with all transformers
+ // namespace awareness is set by transformer
+ {t, getStreamSource(), "StreamSource with namespace support", 0, 1},
+ {tFromTemplates, getStreamSource(), "StreamSource with namespace support using templates", 0, 1},
+ // now test DOMSource, SAXSource and StAXSource
+ // with rotating use of created transformers
+ // namespace awareness is set by source objects
+ {t, domSourceWithNS, "DOMSource with namespace support", 0, 1},
+ {t, domSourceWithoutNS, "DOMSource without namespace support", 3, 3},
+ {tFromTemplates, saxSourceWithNS, "SAXSource with namespace support", 0, 1},
+ {tFromTemplates, saxSourceWithoutNS, "SAXSource without namespace support", 3, 3},
+ {t, staxSourceWithNS, "StAXSource with namespace support", 0, 1},
+ {t, staxSourceWithoutNS, "StAXSource without namespace support", 3, 3}
+ };
+ }
+
+ /*
+ * @bug 8169631 8172974
+ * @summary Test combinations of namespace awareness settings on
+ * XSL transformations
+ */
+ @Test(dataProvider = "testmatrix")
+ public void run(Transformer t, Source s, String label, int elementcount, int attributecount)
+ throws TransformerException
+ {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ t.transform(s, new StreamResult(baos));
+ verifyResult(label, baos.toString(), elementcount, attributecount);
+ }
}
/*
* @bug 8150704
* @summary Test that XSL transformation with lots of temporary result
< prev index next >