< prev index next >

test/javax/xml/jaxp/unittest/transform/TransformerTest.java

Print this page
rev 1071 : 8173602: JAXP: TESTBUG: javax/xml/jaxp/unittest/transform/TransformerTest.java needs refactoring


   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 transform;
  25 
  26 import static jaxp.library.JAXPTestUtilities.getSystemProperty;
  27 import static jaxp.library.JAXPTestUtilities.tryRunWithTmpPermission;
  28 
  29 import java.io.BufferedReader;
  30 import java.io.ByteArrayInputStream;
  31 import java.io.ByteArrayOutputStream;
  32 import java.io.File;
  33 import java.io.FileNotFoundException;
  34 import java.io.FileReader;
  35 import java.io.IOException;
  36 import java.io.StringReader;
  37 import java.io.StringWriter;
  38 
  39 import javax.xml.parsers.DocumentBuilderFactory;
  40 import javax.xml.parsers.ParserConfigurationException;
  41 import javax.xml.parsers.SAXParserFactory;



  42 import javax.xml.transform.Transformer;

  43 import javax.xml.transform.TransformerException;
  44 import javax.xml.transform.TransformerFactory;

  45 import javax.xml.transform.dom.DOMResult;
  46 import javax.xml.transform.dom.DOMSource;
  47 import javax.xml.transform.sax.SAXSource;

  48 import javax.xml.transform.stream.StreamResult;
  49 import javax.xml.transform.stream.StreamSource;
  50 
  51 import org.testng.Assert;
  52 import org.testng.AssertJUnit;

  53 import org.testng.annotations.Listeners;
  54 import org.testng.annotations.Test;
  55 import org.w3c.dom.Document;
  56 import org.w3c.dom.Element;
  57 import org.w3c.dom.Node;
  58 import org.w3c.dom.NodeList;
  59 import org.xml.sax.ContentHandler;
  60 import org.xml.sax.DTDHandler;
  61 import org.xml.sax.EntityResolver;
  62 import org.xml.sax.ErrorHandler;
  63 import org.xml.sax.InputSource;
  64 import org.xml.sax.SAXException;
  65 import org.xml.sax.SAXNotRecognizedException;
  66 import org.xml.sax.SAXNotSupportedException;
  67 import org.xml.sax.XMLReader;
  68 import org.xml.sax.helpers.AttributesImpl;
  69 
  70 import com.sun.org.apache.xml.internal.serialize.OutputFormat;
  71 import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
  72 
  73 /*
  74  * @test
  75  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
  76  * @run testng/othervm -DrunSecMngr=true transform.TransformerTest
  77  * @run testng/othervm transform.TransformerTest
  78  * @summary Transformer Tests
  79  * @bug 6272879 6305029 6505031 8150704 8162598 8169112 8169631 8169772
  80  */
  81 @Listeners({jaxp.library.FilePolicy.class})
  82 public class TransformerTest {
  83 
  84     // some global constants
  85     private static final String LINE_SEPARATOR =
  86         getSystemProperty("line.separator");
  87 
  88     private static final String NAMESPACES =
  89         "http://xml.org/sax/features/namespaces";
  90 
  91     private static final String NAMESPACE_PREFIXES =
  92         "http://xml.org/sax/features/namespace-prefixes";
  93 
  94     private static abstract class TestTemplate {
  95         protected void printSnippet(String title, String snippet) {
  96             StringBuilder div = new StringBuilder();
  97             for (int i = 0; i < title.length(); i++)
  98                 div.append("=");
  99             System.out.println(title + "\n" + div + "\n" + snippet + "\n");
 100         }
 101     }















































 102 
 103     /**
 104      * Reads the contents of the given file into a string.
 105      * WARNING: this method adds a final line feed even if the last line of the file doesn't contain one.
 106      *
 107      * @param f
 108      * The file to read
 109      * @return The content of the file as a string, with line terminators as \"n"
 110      * for all platforms
 111      * @throws IOException
 112      * If there was an error reading
 113      */
 114     private String getFileContentAsString(File f) throws IOException {
 115         try (BufferedReader reader = new BufferedReader(new FileReader(f))) {
 116             String line;
 117             StringBuilder sb = new StringBuilder();
 118             while ((line = reader.readLine()) != null) {
 119                 sb.append(line).append("\n");


 120             }
 121             return sb.toString();
 122         }
 123     }
 124 
 125     private class XMLReaderFor6305029 implements XMLReader {
 126         private boolean namespaces = true;
 127         private boolean namespacePrefixes = false;
 128         private EntityResolver resolver;
 129         private DTDHandler dtdHandler;
 130         private ContentHandler contentHandler;
 131         private ErrorHandler errorHandler;
 132 
 133         public boolean getFeature(final String name) throws SAXNotRecognizedException, SAXNotSupportedException {
 134             if (name.equals(NAMESPACES)) {
 135                 return namespaces;
 136             } else if (name.equals(NAMESPACE_PREFIXES)) {
 137                 return namespacePrefixes;
 138             } else {
 139                 throw new SAXNotRecognizedException();
 140             }
 141         }
 142 
 143         public void setFeature(final String name, final boolean value) throws SAXNotRecognizedException, SAXNotSupportedException {
 144             if (name.equals(NAMESPACES)) {
 145                 namespaces = value;
 146             } else if (name.equals(NAMESPACE_PREFIXES)) {
 147                 namespacePrefixes = value;
 148             } else {
 149                 throw new SAXNotRecognizedException();
 150             }
 151         }
 152 
 153         public Object getProperty(final String name) throws SAXNotRecognizedException, SAXNotSupportedException {
 154             return null;
 155         }















 156 
 157         public void setProperty(final String name, final Object value) throws SAXNotRecognizedException, SAXNotSupportedException {
 158         }







 159 
 160         public void setEntityResolver(final EntityResolver theResolver) {
 161             this.resolver = theResolver;
 162         }
 163 
 164         public EntityResolver getEntityResolver() {
 165             return resolver;
 166         }
 167 
 168         public void setDTDHandler(final DTDHandler theHandler) {
 169             dtdHandler = theHandler;
 170         }
 171 
 172         public DTDHandler getDTDHandler() {
 173             return dtdHandler;
 174         }
 175 
 176         public void setContentHandler(final ContentHandler handler) {
 177             contentHandler = handler;
 178         }
 179 
 180         public ContentHandler getContentHandler() {
 181             return contentHandler;
 182         }
 183 
 184         public void setErrorHandler(final ErrorHandler handler) {
 185             errorHandler = handler;
 186         }
 187 
 188         public ErrorHandler getErrorHandler() {
 189             return errorHandler;
 190         }
 191 
 192         public void parse(final InputSource input) throws IOException, SAXException {
 193             parse();
 194         }
 195 
 196         public void parse(final String systemId) throws IOException, SAXException {
 197             parse();
 198         }
 199 
 200         private void parse() throws SAXException {
 201             contentHandler.startDocument();
 202             contentHandler.startPrefixMapping("prefix", "namespaceUri");
 203 
 204             AttributesImpl atts = new AttributesImpl();
 205             if (namespacePrefixes) {
 206                 atts.addAttribute("", "xmlns:prefix", "xmlns:prefix", "CDATA", "namespaceUri");
 207             }
 208 
 209             contentHandler.startElement("namespaceUri", "localName", namespacePrefixes ? "prefix:localName" : "", atts);
 210             contentHandler.endElement("namespaceUri", "localName", namespacePrefixes ? "prefix:localName" : "");
 211             contentHandler.endPrefixMapping("prefix");
 212             contentHandler.endDocument();










 213         }
 214     }
 215 
 216     /*
 217      * @bug 6272879
 218      * @summary Test for JDK-6272879
 219      */
 220     @Test
 221     public final void testBug6272879() throws Exception {
 222         final String xsl =
 223                 "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" + LINE_SEPARATOR +
 224                 "<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">" + LINE_SEPARATOR +
 225                 "<xsl:output method=\"xml\" indent=\"no\" encoding=\"ISO-8859-1\"/>" + LINE_SEPARATOR +
 226                 "<xsl:template match=\"/\">" + LINE_SEPARATOR +
 227                 "<xsl:element name=\"TransformateurXML\">" + LINE_SEPARATOR +
 228                 "  <xsl:for-each select=\"XMLUtils/test\">" + LINE_SEPARATOR +
 229                 "  <xsl:element name=\"test2\">" + LINE_SEPARATOR +
 230                 "    <xsl:element name=\"valeur2\">" + LINE_SEPARATOR +
 231                 "      <xsl:attribute name=\"attribut2\">" + LINE_SEPARATOR +
 232                 "        <xsl:value-of select=\"valeur/@attribut\"/>" + LINE_SEPARATOR +
 233                 "      </xsl:attribute>" + LINE_SEPARATOR +
 234                 "      <xsl:value-of select=\"valeur\"/>" + LINE_SEPARATOR +
 235                 "    </xsl:element>" + LINE_SEPARATOR +
 236                 "  </xsl:element>" + LINE_SEPARATOR +
 237                 "  </xsl:for-each>" + LINE_SEPARATOR +
 238                 "</xsl:element>" + LINE_SEPARATOR +
 239                 "</xsl:template>" + LINE_SEPARATOR +
 240                 "</xsl:stylesheet>";
 241 
 242         final String sourceXml =
 243                 "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" + LINE_SEPARATOR +
 244                 // "<!DOCTYPE XMLUtils [" + LINE_SEPARATOR +
 245                 // "<!ELEMENT XMLUtils (test*)>" + LINE_SEPARATOR +
 246                 // "<!ELEMENT test (valeur*)>" + LINE_SEPARATOR +
 247                 // "<!ELEMENT valeur (#PCDATA)>" + LINE_SEPARATOR +
 248                 // "<!ATTLIST valeur attribut CDATA #REQUIRED>]>" +
 249                 // LINE_SEPARATOR +
 250                 "<XMLUtils>" + LINE_SEPARATOR +
 251                 "  <test>" + LINE_SEPARATOR +
 252                 "    <valeur attribut=\"Attribut 1\">Valeur 1</valeur>" + LINE_SEPARATOR +
 253                 "  </test>" + LINE_SEPARATOR +
 254                 "  <test>" + LINE_SEPARATOR +
 255                 "    <valeur attribut=\"Attribut 2\">Valeur 2</valeur>" + LINE_SEPARATOR +
 256                 "  </test>" + LINE_SEPARATOR +
 257                 "</XMLUtils>";
 258 
 259         System.out.println("Stylesheet:");
 260         System.out.println("=============================");
 261         System.out.println(xsl);
 262         System.out.println();
 263 
 264         System.out.println("Source before transformation:");
 265         System.out.println("=============================");
 266         System.out.println(sourceXml);
 267         System.out.println();
 268 
 269         // transform to DOM result
 270         TransformerFactory tf = TransformerFactory.newInstance();
 271         Transformer t = tf.newTransformer(new StreamSource(new ByteArrayInputStream(xsl.getBytes())));
 272         DOMResult result = new DOMResult();
 273         t.transform(new StreamSource(new ByteArrayInputStream(sourceXml.getBytes())), result);
 274         Document document = (Document)result.getNode();
 275 
 276         System.out.println("Result after transformation:");
 277         System.out.println("============================");
 278         tryRunWithTmpPermission(() -> {
 279             OutputFormat format = new OutputFormat();
 280             format.setIndenting(true);
 281             new XMLSerializer(System.out, format).serialize(document);
 282         }, new RuntimePermission("accessClassInPackage.com.sun.org.apache.xml.internal.serialize"));
 283         System.out.println();
 284 
 285         System.out.println("Node content for element valeur2:");
 286         System.out.println("=================================");
 287         NodeList nodes = document.getElementsByTagName("valeur2");
 288         for (int i = 0; i < nodes.getLength(); i++) {
 289             Node node = nodes.item(i);
 290             System.out.println("  Node value: " + node.getFirstChild().getNodeValue());
 291             System.out.println("  Node attribute: " + node.getAttributes().item(0).getNodeValue());
 292 
 293             AssertJUnit.assertEquals("Node value mismatch", "Valeur " + (i + 1), node.getFirstChild().getNodeValue());
 294             AssertJUnit.assertEquals("Node attribute mismatch", "Attribut " + (i + 1), node.getAttributes().item(0).getNodeValue());























 295         }
 296     }
 297 
 298     /*
 299      * @bug 6305029
 300      * @summary Test for JDK-6305029
 301      */
 302     @Test
 303     public final void testBug6305029() throws TransformerException {
 304         final String XML_DOCUMENT = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<prefix:localName xmlns:prefix=\"namespaceUri\"/>";
 305 
 306         // test SAXSource
 307         SAXSource saxSource = new SAXSource(new XMLReaderFor6305029(), new InputSource());
 308         StringWriter resultWriter = new StringWriter();
 309         TransformerFactory tf = TransformerFactory.newInstance();
 310         tf.newTransformer().transform(saxSource, new StreamResult(resultWriter));
 311         AssertJUnit.assertEquals("Identity transform of SAXSource", XML_DOCUMENT, resultWriter.toString());
 312 
 313         // test StreamSource
 314         StreamSource streamSource = new StreamSource(new StringReader(XML_DOCUMENT));
 315         resultWriter = new StringWriter();
 316         tf.newTransformer().transform(streamSource, new StreamResult(resultWriter));
 317         AssertJUnit.assertEquals("Identity transform of StreamSource", XML_DOCUMENT, resultWriter.toString());


 318     }
 319 
 320     /*
 321      * @bug 6505031
 322      * @summary Test transformer parses keys and their values coming from different xml documents.
 323      */
 324     @Test
 325     public final void testBug6505031() throws TransformerException {
 326         TransformerFactory tf = TransformerFactory.newInstance();
 327         Transformer t = tf.newTransformer(new StreamSource(getClass().getResource("transform.xsl").toString()));
 328         t.setParameter("config", getClass().getResource("config.xml").toString());
 329         t.setParameter("mapsFile", getClass().getResource("maps.xml").toString());
 330         StringWriter sw = new StringWriter();
 331         t.transform(new StreamSource(getClass().getResource("template.xml").toString()), new StreamResult(sw));
 332         String s = sw.toString();
 333         Assert.assertTrue(s.contains("map1key1value") && s.contains("map2key1value"));
 334     }
 335 
 336     private static class Test8169631 extends TestTemplate {
 337         private final static String xsl =
 338             "<?xml version=\"1.0\"?>" + LINE_SEPARATOR +
 339             "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">" + LINE_SEPARATOR +
 340             "  <xsl:template match=\"/\">" + LINE_SEPARATOR +
 341             "    <xsl:variable name=\"Counter\" select=\"count(//row)\"/>" + LINE_SEPARATOR +
 342             "    <xsl:variable name=\"AttribCounter\" select=\"count(//@attrib)\"/>" + LINE_SEPARATOR +
 343             "    <Counter><xsl:value-of select=\"$Counter\"/></Counter>" + LINE_SEPARATOR +
 344             "    <AttribCounter><xsl:value-of select=\"$AttribCounter\"/></AttribCounter>" + LINE_SEPARATOR +
 345             "  </xsl:template>" + LINE_SEPARATOR +
 346             "</xsl:stylesheet>" + LINE_SEPARATOR;
 347 
 348         private final static String sourceXml =
 349             "<?xml version=\"1.0\"?>" + LINE_SEPARATOR +
 350             "<envelope xmlns=\"http://www.sap.com/myns\" xmlns:sap=\"http://www.sap.com/myns\">" + LINE_SEPARATOR +
 351             "  <sap:row sap:attrib=\"a\">1</sap:row>" + LINE_SEPARATOR +
 352             "  <row attrib=\"b\">2</row>" + LINE_SEPARATOR +
 353             "  <row sap:attrib=\"c\">3</row>" + LINE_SEPARATOR +
 354             "</envelope>" + LINE_SEPARATOR;
 355 




 356         /**
 357          * Utility method to print out transformation result and check values.
 358          *
 359          * @param type
 360          * Text describing type of transformation
 361          * @param result
 362          * Resulting output of transformation
 363          * @param elementCount
 364          * Counter of elements to check
 365          * @param attribCount
 366          * Counter of attributes to check
 367          */
 368         private void verifyResult(String type, String result, int elementCount,
 369                                   int attribCount)
 370         {
 371             printSnippet("Result of transformation from " + type + ":",
 372                          result);
 373             Assert.assertEquals(
 374                 result.contains("<Counter>" + elementCount + "</Counter>"),
 375                 true, "Result of transformation from " + type +
 376                 " should have count of " + elementCount + " elements.");
 377             Assert.assertEquals(
 378                 result.contains("<AttribCounter>" + attribCount +
 379                 "</AttribCounter>"), true, "Result of transformation from " +
 380                 type + " should have count of "+ attribCount + " attributes.");
 381         }
 382 
 383         public void run() throws IOException, TransformerException,
 384             SAXException, ParserConfigurationException


 385         {
 386             printSnippet("Source:", sourceXml);
 387 
 388             printSnippet("Stylesheet:", xsl);
 389 
 390             // create default transformer (namespace aware)
 391             TransformerFactory tf1 = TransformerFactory.newInstance();
 392             ByteArrayInputStream bais = new ByteArrayInputStream(xsl.getBytes());
 393             Transformer t1 = tf1.newTransformer(new StreamSource(bais));
 394 
 395             // test transformation from stream source with namespace support
 396             ByteArrayOutputStream baos = new ByteArrayOutputStream();
 397             bais = new ByteArrayInputStream(sourceXml.getBytes());
 398             t1.transform(new StreamSource(bais), new StreamResult(baos));
 399             verifyResult("StreamSource with namespace support", baos.toString(), 0, 1);
 400 
 401             // test transformation from DOM source with namespace support
 402             bais.reset();
 403             baos.reset();
 404             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

 405             dbf.setNamespaceAware(true);
 406             Document doc = dbf.newDocumentBuilder().parse(new InputSource(bais));
 407             t1.transform(new DOMSource(doc), new StreamResult(baos));
 408             verifyResult("DOMSource with namespace support", baos.toString(), 0, 1);
 409 
 410             // test transformation from DOM source without namespace support
 411             bais.reset();
 412             baos.reset();
 413             dbf.setNamespaceAware(false);
 414             doc = dbf.newDocumentBuilder().parse(new InputSource(bais));
 415             t1.transform(new DOMSource(doc), new StreamResult(baos));
 416             verifyResult("DOMSource without namespace support", baos.toString(), 3, 3);
 417 
 418             // test transformation from SAX source with namespace support
 419             bais.reset();
 420             baos.reset();
 421             SAXParserFactory spf = SAXParserFactory.newInstance();

 422             spf.setNamespaceAware(true);
 423             XMLReader xmlr = spf.newSAXParser().getXMLReader();
 424             SAXSource saxS = new SAXSource(xmlr, new InputSource(bais));
 425             t1.transform(saxS, new StreamResult(baos));
 426             verifyResult("SAXSource with namespace support", baos.toString(), 0, 1);
 427 
 428             // test transformation from SAX source without namespace support
 429             bais.reset();
 430             baos.reset();
 431             spf.setNamespaceAware(false);
 432             xmlr = spf.newSAXParser().getXMLReader();
 433             saxS = new SAXSource(xmlr, new InputSource(bais));
 434             t1.transform(saxS, new StreamResult(baos));
 435             verifyResult("SAXSource without namespace support", baos.toString(), 3, 3);

























 436         }
 437     }
 438 
 439     /*
 440      * @bug 8169631
 441      * @summary Test combinations of namespace awareness settings on
 442      *          XSL transformations
 443      */
 444     @Test
 445     public final void testBug8169631() throws IOException, SAXException,
 446         TransformerException, ParserConfigurationException
 447     {
 448         new Test8169631().run();
 449     }
 450 
 451     /*
 452      * @bug 8150704
 453      * @summary Test that XSL transformation with lots of temporary result
 454      *          trees will not run out of DTM IDs.
 455      */
 456     @Test
 457     public final void testBug8150704() throws TransformerException, IOException {
 458         System.out.println("Testing transformation of Bug8150704-1.xml...");
 459         TransformerFactory tf = TransformerFactory.newInstance();
 460         Transformer t = tf.newTransformer(new StreamSource(getClass().getResource("Bug8150704-1.xsl").toString()));
 461         StringWriter sw = new StringWriter();
 462         t.transform(new StreamSource(getClass().getResource("Bug8150704-1.xml").toString()), new StreamResult(sw));
 463         String resultstring = sw.toString().replaceAll("\\r\\n", "\n").replaceAll("\\r", "\n");
 464         String reference = getFileContentAsString(new File(getClass().getResource("Bug8150704-1.ref").getPath()));
 465         Assert.assertEquals(resultstring, reference, "Output of transformation of Bug8150704-1.xml does not match reference");
 466         System.out.println("Passed.");
 467 
 468         System.out.println("Testing transformation of Bug8150704-2.xml...");
 469         t = tf.newTransformer(new StreamSource(getClass().getResource("Bug8150704-2.xsl").toString()));
 470         sw = new StringWriter();
 471         t.transform(new StreamSource(getClass().getResource("Bug8150704-2.xml").toString()), new StreamResult(sw));
 472         resultstring = sw.toString().replaceAll("\\r\\n", "\n").replaceAll("\\r", "\n");
 473         reference = getFileContentAsString(new File(getClass().getResource("Bug8150704-2.ref").getPath()));
 474         Assert.assertEquals(resultstring, reference, "Output of transformation of Bug8150704-2.xml does not match reference");
 475         System.out.println("Passed.");

 476     }
 477 
 478     private static class Test8162598 extends TestTemplate {
 479         private static final String xsl =

 480             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + LINE_SEPARATOR +
 481             "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">" + LINE_SEPARATOR +
 482             "    <xsl:template match=\"/\">" + LINE_SEPARATOR +
 483             "        <root xmlns=\"ns1\">" + LINE_SEPARATOR +
 484             "            <xsl:call-template name=\"transform\"/>" + LINE_SEPARATOR +
 485             "        </root>" + LINE_SEPARATOR +
 486             "    </xsl:template>" + LINE_SEPARATOR +
 487             "    <xsl:template name=\"transform\">" + LINE_SEPARATOR +
 488             "        <test1 xmlns=\"ns2\"><b xmlns=\"ns2\"><c xmlns=\"\"></c></b></test1>" + LINE_SEPARATOR +
 489             "        <test2 xmlns=\"ns1\"><b xmlns=\"ns2\"><c xmlns=\"\"></c></b></test2>" + LINE_SEPARATOR +
 490             "        <test3><b><c xmlns=\"\"></c></b></test3>" + LINE_SEPARATOR +
 491             "        <test4 xmlns=\"\"><b><c xmlns=\"\"></c></b></test4>" + LINE_SEPARATOR +
 492             "        <test5 xmlns=\"ns1\"><b><c xmlns=\"\"></c></b></test5>" + LINE_SEPARATOR +
 493             "        <test6 xmlns=\"\"/>" + LINE_SEPARATOR +
 494             "    </xsl:template>" + LINE_SEPARATOR +
 495             "</xsl:stylesheet>";
 496 
 497         private static final String sourceXml =
 498             "<?xml version=\"1.0\" encoding=\"UTF-8\"?><aaa></aaa>" + LINE_SEPARATOR;





 499         /**
 500          * Utility method for testBug8162598().
 501          * Provides a convenient way to check/assert the expected namespaces
 502          * of a Node and its siblings.
 503          *
 504          * @param test
 505          * The node to check
 506          * @param nstest
 507          * Expected namespace of the node
 508          * @param nsb
 509          * Expected namespace of the first sibling
 510          * @param nsc
 511          * Expected namespace of the first sibling of the first sibling
 512          */
 513 
 514         private void checkNodeNS(Node test, String nstest, String nsb, String nsc) {
 515             String testNodeName = test.getNodeName();
 516             if (nstest == null) {
 517                 Assert.assertNull(test.getNamespaceURI(), "unexpected namespace for " + testNodeName);
 518             } else {
 519                 Assert.assertEquals(test.getNamespaceURI(), nstest, "unexpected namespace for " + testNodeName);
 520             }
 521             Node b = test.getChildNodes().item(0);
 522             if (nsb == null) {
 523                 Assert.assertNull(b.getNamespaceURI(), "unexpected namespace for " + testNodeName + "->b");
 524             } else {
 525                 Assert.assertEquals(b.getNamespaceURI(), nsb, "unexpected namespace for " + testNodeName + "->b");
 526             }
 527             Node c = b.getChildNodes().item(0);
 528             if (nsc == null) {
 529                 Assert.assertNull(c.getNamespaceURI(), "unexpected namespace for " + testNodeName + "->b->c");
 530             } else {
 531                 Assert.assertEquals(c.getNamespaceURI(), nsc, "unexpected namespace for " + testNodeName + "->b->c");
 532             }
 533         }
 534 






 535         public void run()  throws Exception {
 536             printSnippet("Source:", sourceXml);
 537 
 538             printSnippet("Stylesheet:", xsl);
 539 
 540             // transform to DOM result
 541             TransformerFactory tf = TransformerFactory.newInstance();
 542             ByteArrayInputStream bais = new ByteArrayInputStream(xsl.getBytes());
 543             Transformer t = tf.newTransformer(new StreamSource(bais));
 544             DOMResult result = new DOMResult();
 545             bais = new ByteArrayInputStream(sourceXml.getBytes());
 546             t.transform(new StreamSource(bais), result);
 547             Document document = (Document)result.getNode();
 548 
 549             System.out.println("Result after transformation:");
 550             System.out.println("============================");
 551             tryRunWithTmpPermission(() -> {
 552                 OutputFormat format = new OutputFormat();
 553                 format.setIndenting(true);
 554                 new XMLSerializer(System.out, format).serialize(document);
 555             }, new RuntimePermission("accessClassInPackage.com.sun.org.apache.xml.internal.serialize"));
 556             System.out.println();
 557 


 558             checkNodeNS(document.getElementsByTagName("test1").item(0), "ns2", "ns2", null);
 559             checkNodeNS(document.getElementsByTagName("test2").item(0), "ns1", "ns2", null);
 560             checkNodeNS(document.getElementsByTagName("test3").item(0), null, null, null);
 561             checkNodeNS(document.getElementsByTagName("test4").item(0), null, null, null);
 562             checkNodeNS(document.getElementsByTagName("test5").item(0), "ns1", "ns1", null);
 563             Assert.assertNull(document.getElementsByTagName("test6").item(0).getNamespaceURI(),
 564                 "unexpected namespace for test6");
 565         }
 566     }
 567 
 568     /*
 569      * @bug 8162598
 570      * @summary Test XSLTC handling of namespaces, especially empty namespace
 571      *          definitions to reset the default namespace
 572      */
 573     @Test
 574     public final void testBug8162598() throws Exception {
 575         new Test8162598().run();
 576     }
 577 
 578     /**
 579      * @bug 8169112
 580      * @summary Test compilation of large xsl file with outlining.
 581      *
 582      * This test merely compiles a large xsl file and tests if its bytecode
 583      * passes verification by invoking the transform() method for
 584      * dummy content. The test succeeds if no Exception is thrown
 585      */
 586     @Test
 587     public final void testBug8169112() throws FileNotFoundException,
 588         TransformerException
 589     {
 590         TransformerFactory tf = TransformerFactory.newInstance();
 591         String xslFile = getClass().getResource("Bug8169112.xsl").toString();
 592         Transformer t = tf.newTransformer(new StreamSource(xslFile));
 593         String xmlIn = "<?xml version=\"1.0\"?><DOCROOT/>";
 594         ByteArrayInputStream bis = new ByteArrayInputStream(xmlIn.getBytes());
 595         ByteArrayOutputStream bos = new ByteArrayOutputStream();
 596         t.transform(new StreamSource(bis), new StreamResult(bos));




 597     }
 598 
 599     /**
 600      * @bug 8169772
 601      * @summary Test transformation of DOM with null valued text node
 602      *
 603      * This test would throw a NullPointerException during transform when the
 604      * fix was not present.
 605      */
 606     @Test
 607     public final void testBug8169772() throws ParserConfigurationException,
 608         SAXException, IOException, TransformerException
 609     {
 610         // create a small DOM
 611         Document doc = DocumentBuilderFactory.newInstance().
 612             newDocumentBuilder().parse(
 613                 new ByteArrayInputStream(
 614                     "<?xml version=\"1.0\"?><DOCROOT/>".getBytes()
 615                 )
 616             );
 617 
 618         // insert a bad element
 619         Element e = doc.createElement("ERROR");
 620         e.appendChild(doc.createTextNode(null));
 621         doc.getDocumentElement().appendChild(e);
 622 
 623         // transform
 624         ByteArrayOutputStream bos = new ByteArrayOutputStream();
 625         TransformerFactory.newInstance().newTransformer().transform(
 626             new DOMSource(doc.getDocumentElement()), new StreamResult(bos)
 627         );
 628         System.out.println("Transformation result (DOM with null text node):");
 629         System.out.println("================================================");
 630         System.out.println(bos);










 631     }
 632 }


   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 transform;
  25 
  26 import static jaxp.library.JAXPTestUtilities.getSystemProperty;

  27 

  28 import java.io.ByteArrayInputStream;
  29 import java.io.ByteArrayOutputStream;



  30 import java.io.IOException;

  31 import java.io.StringWriter;
  32 
  33 import javax.xml.parsers.DocumentBuilderFactory;
  34 import javax.xml.parsers.ParserConfigurationException;
  35 import javax.xml.parsers.SAXParserFactory;
  36 import javax.xml.stream.XMLInputFactory;
  37 import javax.xml.stream.XMLStreamException;
  38 import javax.xml.transform.Source;
  39 import javax.xml.transform.Transformer;
  40 import javax.xml.transform.TransformerConfigurationException;
  41 import javax.xml.transform.TransformerException;
  42 import javax.xml.transform.TransformerFactory;
  43 import javax.xml.transform.TransformerFactoryConfigurationError;
  44 import javax.xml.transform.dom.DOMResult;
  45 import javax.xml.transform.dom.DOMSource;
  46 import javax.xml.transform.sax.SAXSource;
  47 import javax.xml.transform.stax.StAXSource;
  48 import javax.xml.transform.stream.StreamResult;

  49 
  50 import org.testng.Assert;
  51 import org.testng.AssertJUnit;
  52 import org.testng.annotations.DataProvider;
  53 import org.testng.annotations.Listeners;
  54 import org.testng.annotations.Test;
  55 import org.w3c.dom.Document;
  56 import org.w3c.dom.Element;
  57 import org.w3c.dom.Node;
  58 import org.w3c.dom.NodeList;
  59 import org.xml.sax.ContentHandler;
  60 import org.xml.sax.DTDHandler;
  61 import org.xml.sax.EntityResolver;
  62 import org.xml.sax.ErrorHandler;
  63 import org.xml.sax.InputSource;
  64 import org.xml.sax.SAXException;
  65 import org.xml.sax.SAXNotRecognizedException;
  66 import org.xml.sax.SAXNotSupportedException;
  67 import org.xml.sax.XMLReader;
  68 import org.xml.sax.helpers.AttributesImpl;
  69 
  70 import transform.util.TransformerTestTemplate;

  71 
  72 /*
  73  * @test
  74  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
  75  * @run testng/othervm -DrunSecMngr=true transform.TransformerTest
  76  * @run testng/othervm transform.TransformerTest
  77  * @summary Transformer Tests
  78  * @bug 6272879 6305029 6505031 8150704 8162598 8169112 8169631 8169772
  79  */
  80 @Listeners({jaxp.library.FilePolicy.class})
  81 public class TransformerTest {
  82 
  83     // some global constants
  84     private static final String LINE_SEPARATOR =
  85         getSystemProperty("line.separator");
  86 
  87     private static final String NAMESPACES =
  88         "http://xml.org/sax/features/namespaces";
  89 
  90     private static final String NAMESPACE_PREFIXES =
  91         "http://xml.org/sax/features/namespace-prefixes";
  92 
  93     public static class Test6272879 extends TransformerTestTemplate {
  94 
  95         private static String XSL_INPUT =
  96             "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" + LINE_SEPARATOR +
  97             "<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">" + LINE_SEPARATOR +
  98             "<xsl:output method=\"xml\" indent=\"no\" encoding=\"ISO-8859-1\"/>" + LINE_SEPARATOR +
  99             "<xsl:template match=\"/\">" + LINE_SEPARATOR +
 100             "<xsl:element name=\"TransformateurXML\">" + LINE_SEPARATOR +
 101             "  <xsl:for-each select=\"XMLUtils/test\">" + LINE_SEPARATOR +
 102             "  <xsl:element name=\"test2\">" + LINE_SEPARATOR +
 103             "    <xsl:element name=\"valeur2\">" + LINE_SEPARATOR +
 104             "      <xsl:attribute name=\"attribut2\">" + LINE_SEPARATOR +
 105             "        <xsl:value-of select=\"valeur/@attribut\"/>" + LINE_SEPARATOR +
 106             "      </xsl:attribute>" + LINE_SEPARATOR +
 107             "      <xsl:value-of select=\"valeur\"/>" + LINE_SEPARATOR +
 108             "    </xsl:element>" + LINE_SEPARATOR +
 109             "  </xsl:element>" + LINE_SEPARATOR +
 110             "  </xsl:for-each>" + LINE_SEPARATOR +
 111             "</xsl:element>" + LINE_SEPARATOR +
 112             "</xsl:template>" + LINE_SEPARATOR +
 113             "</xsl:stylesheet>";
 114 
 115         private static String XML_INPUT =
 116             "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" + LINE_SEPARATOR +
 117             // "<!DOCTYPE XMLUtils [" + LINE_SEPARATOR +
 118             // "<!ELEMENT XMLUtils (test*)>" + LINE_SEPARATOR +
 119             // "<!ELEMENT test (valeur*)>" + LINE_SEPARATOR +
 120             // "<!ELEMENT valeur (#PCDATA)>" + LINE_SEPARATOR +
 121             // "<!ATTLIST valeur attribut CDATA #REQUIRED>]>" +
 122             // LINE_SEPARATOR +
 123             "<XMLUtils>" + LINE_SEPARATOR +
 124             "  <test>" + LINE_SEPARATOR +
 125             "    <valeur attribut=\"Attribut 1\">Valeur 1</valeur>" + LINE_SEPARATOR +
 126             "  </test>" + LINE_SEPARATOR +
 127             "  <test>" + LINE_SEPARATOR +
 128             "    <valeur attribut=\"Attribut 2\">Valeur 2</valeur>" + LINE_SEPARATOR +
 129             "  </test>" + LINE_SEPARATOR +
 130             "</XMLUtils>";
 131 
 132         public Test6272879() {
 133             super(XSL_INPUT, XML_INPUT);
 134         }
 135 
 136         /*
 137          * @bug 6272879
 138          * @summary Test for JDK-6272879
 139          *          DomResult had truncated Strings in some places
 140          */
 141         @Test
 142         public void run() throws TransformerException, ClassNotFoundException, InstantiationException,
 143             IllegalAccessException, ClassCastException
 144         {
 145             // print input
 146             printSnippet("Stylesheet:", getXsl());
 147             printSnippet("Source before transformation:", getSourceXml());
 148 
 149             // transform to DOM result
 150             Transformer t = getTransformer();
 151             DOMResult result = new DOMResult();
 152             t.transform(getStreamSource(), result);
 153 
 154             // print output
 155             printSnippet("Result after transformation:", prettyPrintDOMResult(result));
 156 
 157             // do some assertions
 158             Document document = (Document)result.getNode();
 159             NodeList nodes = document.getElementsByTagName("valeur2");
 160             for (int i = 0; i < nodes.getLength(); i++) {
 161                 Node node = nodes.item(i);
 162                 AssertJUnit.assertEquals("Node value mismatch",
 163                                          "Valeur " + (i + 1),
 164                                          node.getFirstChild().getNodeValue());
 165                 AssertJUnit.assertEquals("Node attribute mismatch",
 166                                          "Attribut " + (i + 1),
 167                                          node.getAttributes().item(0).getNodeValue());
 168             }

 169         }
 170     }
 171 
 172     public static class Test6305029 extends TransformerTestTemplate {
















 173 
 174         private static String XML_INPUT =
 175             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<prefix:localName xmlns:prefix=\"namespaceUri\"/>";







 176 
 177         // custom XMLReader representing XML_INPUT
 178         private class MyXMLReader implements XMLReader {
 179             private boolean namespaces = true;
 180             private boolean namespacePrefixes = false;
 181             private EntityResolver resolver;
 182             private DTDHandler dtdHandler;
 183             private ContentHandler contentHandler;
 184             private ErrorHandler errorHandler;
 185 
 186             public boolean getFeature(final String name) throws SAXNotRecognizedException, SAXNotSupportedException {
 187                 if (name.equals(NAMESPACES)) {
 188                     return namespaces;
 189                 } else if (name.equals(NAMESPACE_PREFIXES)) {
 190                     return namespacePrefixes;
 191                 } else {
 192                     throw new SAXNotRecognizedException();
 193                 }
 194             }
 195 
 196             public void setFeature(final String name, final boolean value) throws SAXNotRecognizedException, SAXNotSupportedException {
 197                 if (name.equals(NAMESPACES)) {
 198                     namespaces = value;
 199                 } else if (name.equals(NAMESPACE_PREFIXES)) {
 200                     namespacePrefixes = value;
 201                 } else {
 202                     throw new SAXNotRecognizedException();
 203                 }
 204             }
 205 
 206             public Object getProperty(final String name) throws SAXNotRecognizedException, SAXNotSupportedException {
 207                 return null;
 208             }
 209 
 210             public void setProperty(final String name, final Object value) throws SAXNotRecognizedException, SAXNotSupportedException {
 211             }

 212 
 213             public void setEntityResolver(final EntityResolver theResolver) {
 214                 this.resolver = theResolver;
 215             }
 216 
 217             public EntityResolver getEntityResolver() {
 218                 return resolver;
 219             }
 220 
 221             public void setDTDHandler(final DTDHandler theHandler) {
 222                 dtdHandler = theHandler;
 223             }
 224 
 225             public DTDHandler getDTDHandler() {
 226                 return dtdHandler;
 227             }
 228 
 229             public void setContentHandler(final ContentHandler handler) {
 230                 contentHandler = handler;
 231             }
 232 
 233             public ContentHandler getContentHandler() {
 234                 return contentHandler;
 235             }
 236 
 237             public void setErrorHandler(final ErrorHandler handler) {
 238                 errorHandler = handler;
 239             }
 240 
 241             public ErrorHandler getErrorHandler() {
 242                 return errorHandler;
 243             }
 244 
 245             public void parse(final InputSource input) throws IOException, SAXException {
 246                 parse();
 247             }
 248 
 249             public void parse(final String systemId) throws IOException, SAXException {
 250                 parse();

 251             }
 252 
 253             private void parse() throws SAXException {
 254                 contentHandler.startDocument();
 255                 contentHandler.startPrefixMapping("prefix", "namespaceUri");
 256 
 257                 AttributesImpl atts = new AttributesImpl();
 258                 if (namespacePrefixes) {
 259                     atts.addAttribute("", "xmlns:prefix", "xmlns:prefix", "CDATA", "namespaceUri");
 260                 }
 261 
 262                 contentHandler.startElement("namespaceUri", "localName", namespacePrefixes ? "prefix:localName" : "", atts);
 263                 contentHandler.endElement("namespaceUri", "localName", namespacePrefixes ? "prefix:localName" : "");
 264                 contentHandler.endPrefixMapping("prefix");
 265                 contentHandler.endDocument();
 266             }
 267         }

 268 
 269         public Test6305029() {
 270             super(null, XML_INPUT);
 271         }









































































 272 
 273         /*
 274          * @bug 6305029
 275          * @summary Test for JDK-6305029
 276          *          Test identity transformation
 277          */
 278         @Test
 279         public void run() throws TransformerFactoryConfigurationError, TransformerException {
 280             // get Identity transformer
 281             Transformer t = getTransformer();
 282 
 283             // test SAXSource from custom XMLReader
 284             SAXSource saxSource = new SAXSource(new MyXMLReader(), new InputSource());
 285             StringWriter resultWriter = new StringWriter();
 286             t.transform(saxSource, new StreamResult(resultWriter));
 287             String resultString = resultWriter.toString();
 288             printSnippet("Result after transformation from custom SAXSource:", resultString);
 289             AssertJUnit.assertEquals("Identity transform of SAXSource", getSourceXml(), resultString);
 290 
 291             // test StreamSource
 292             printSnippet("Source before transformation of StreamSource:", getSourceXml());
 293             resultWriter = new StringWriter();
 294             t.transform(getStreamSource(), new StreamResult(resultWriter));
 295             resultString = resultWriter.toString();
 296             printSnippet("Result after transformation of StreamSource:", resultString);
 297             AssertJUnit.assertEquals("Identity transform of StreamSource", getSourceXml(), resultString);
 298         }
 299     }
 300 
 301     public static class Test6505031 extends TransformerTestTemplate {
 302 
 303         public Test6505031() throws IOException {
 304             super();
 305             setXsl(fromInputStream(getClass().getResourceAsStream("transform.xsl")));
 306             setSourceXml(fromInputStream(getClass().getResourceAsStream("template.xml")));
 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 void run() throws TransformerFactoryConfigurationError, TransformerException {
 315             Transformer t = getTransformer();
 316             t.setParameter("config", getClass().getResource("config.xml").toString());
 317             t.setParameter("mapsFile", getClass().getResource("maps.xml").toString());
 318             StringWriter resultWriter = new StringWriter();
 319             t.transform(getStreamSource(), new StreamResult(resultWriter));
 320             String resultString = resultWriter.toString();
 321             Assert.assertTrue(resultString.contains("map1key1value") && resultString.contains("map2key1value"));
 322         }
 323     }
 324 
 325     public static class Test8169631 extends TransformerTestTemplate {














 326 
 327         private static String XSL_INPUT =

 328             "<?xml version=\"1.0\"?>" + LINE_SEPARATOR +
 329             "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">" + LINE_SEPARATOR +
 330             "  <xsl:template match=\"/\">" + LINE_SEPARATOR +
 331             "    <xsl:variable name=\"Counter\" select=\"count(//row)\"/>" + LINE_SEPARATOR +
 332             "    <xsl:variable name=\"AttribCounter\" select=\"count(//@attrib)\"/>" + LINE_SEPARATOR +
 333             "    <Counter><xsl:value-of select=\"$Counter\"/></Counter>" + LINE_SEPARATOR +
 334             "    <AttribCounter><xsl:value-of select=\"$AttribCounter\"/></AttribCounter>" + LINE_SEPARATOR +
 335             "  </xsl:template>" + LINE_SEPARATOR +
 336             "</xsl:stylesheet>" + LINE_SEPARATOR;
 337 
 338         private static String XML_INPUT =
 339             "<?xml version=\"1.0\"?>" + LINE_SEPARATOR +
 340             "<envelope xmlns=\"http://www.sap.com/myns\" xmlns:sap=\"http://www.sap.com/myns\">" + LINE_SEPARATOR +
 341             "  <sap:row sap:attrib=\"a\">1</sap:row>" + LINE_SEPARATOR +
 342             "  <row attrib=\"b\">2</row>" + LINE_SEPARATOR +
 343             "  <row sap:attrib=\"c\">3</row>" + LINE_SEPARATOR +
 344             "</envelope>" + LINE_SEPARATOR;
 345 
 346         public Test8169631() {
 347             super(XSL_INPUT, XML_INPUT);
 348         }
 349 
 350         /**
 351          * Utility method to print out transformation result and check values.
 352          *
 353          * @param type
 354          * Text describing type of transformation
 355          * @param result
 356          * Resulting output of transformation
 357          * @param elementCount
 358          * Counter of elements to check
 359          * @param attribCount
 360          * Counter of attributes to check
 361          */
 362         private void verifyResult(String type, String result, int elementCount,
 363                                   int attribCount)
 364         {
 365             printSnippet("Result of transformation from " + type + ":",
 366                          result);
 367             Assert.assertEquals(
 368                 result.contains("<Counter>" + elementCount + "</Counter>"),
 369                 true, "Result of transformation from " + type +
 370                 " should have count of " + elementCount + " elements.");
 371             Assert.assertEquals(
 372                 result.contains("<AttribCounter>" + attribCount +
 373                 "</AttribCounter>"), true, "Result of transformation from " +
 374                 type + " should have count of "+ attribCount + " attributes.");
 375         }
 376 
 377         @DataProvider(name = "testdata8169631")
 378         public Object[][] testData()
 379             throws TransformerConfigurationException, SAXException, IOException,
 380             ParserConfigurationException, XMLStreamException
 381         {
 382             // get Transformers
 383             TransformerFactory tf = TransformerFactory.newInstance();
 384             Transformer t = getTransformer(tf);
 385             Transformer tFromTemplates = getTemplates(tf).newTransformer();




 386 
 387             // get DOMSource objects








 388             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 389             DOMSource domSourceWithoutNS = getDOMSource(dbf);
 390             dbf.setNamespaceAware(true);
 391             DOMSource domSourceWithNS = getDOMSource(dbf);
 392 
 393             // get SAXSource objects












 394             SAXParserFactory spf = SAXParserFactory.newInstance();
 395             SAXSource saxSourceWithoutNS = getSAXSource(spf);
 396             spf.setNamespaceAware(true);
 397             SAXSource saxSourceWithNS = getSAXSource(spf);
 398 
 399             // get StAXSource objects
 400             XMLInputFactory xif = XMLInputFactory.newInstance();
 401             StAXSource staxSourceWithNS = getStAXSource(xif);
 402 
 403             // print XML/XSL snippets to ease understanding of result
 404             printSnippet("Source:", getSourceXml());
 405             printSnippet("Stylesheet:", getXsl());
 406 
 407             return new Object[][] {
 408                 // test StreamSource input with all transformers
 409                 // namespace awareness is set by transformer
 410                 {t, getStreamSource(), "StreamSource with namespace support", 0, 1},
 411                 {tFromTemplates, getStreamSource(), "StreamSource with namespace support using templates", 0, 1},
 412                 // now test DOMSource, SAXSource and StAXSource
 413                 // with rotating use of created transformers
 414                 // namespace awareness is set by source objects
 415                 {t, domSourceWithNS, "DOMSource with namespace support", 0, 1},
 416                 {t, domSourceWithoutNS, "DOMSource without namespace support", 3, 3},
 417                 {tFromTemplates, saxSourceWithNS, "SAXSource with namespace support", 0, 1},
 418                 {tFromTemplates, saxSourceWithoutNS, "SAXSource without namespace support", 3, 3},
 419                 {t, staxSourceWithNS, "StAXSource with namespace support", 0, 1}
 420             };
 421         }
 422 
 423         /*
 424          * @bug 8169631
 425          * @summary Test combinations of namespace awareness settings on
 426          *          XSL transformations
 427          */
 428         @Test(dataProvider = "testdata8169631")
 429         public void run(Transformer t, Source s, String label, int elementcount, int attributecount)
 430             throws TransformerException
 431         {
 432             ByteArrayOutputStream baos = new ByteArrayOutputStream();
 433             t.transform(s, new StreamResult(baos));
 434             verifyResult(label, baos.toString(), elementcount, attributecount);
 435         }
 436     }
 437 
 438     public static class Test8150704 extends TransformerTestTemplate {
 439 
 440         public Test8150704() {
 441             super();
 442         }






 443 
 444         @DataProvider(name = "testdata8150704")
 445         public Object[][] testData() {
 446             return new Object[][] {
 447                 {"Bug8150704-1.xsl", "Bug8150704-1.xml", "Bug8150704-1.ref"},
 448                 {"Bug8150704-2.xsl", "Bug8150704-2.xml", "Bug8150704-2.ref"}
 449             };
 450         }
 451 
 452         /*
 453          * @bug 8150704
 454          * @summary Test that XSL transformation with lots of temporary result
 455          *          trees will not run out of DTM IDs.
 456          */
 457         @Test(dataProvider = "testdata8150704")
 458         public void run(String xsl, String xml, String ref) throws IOException, TransformerException {
 459             System.out.println("Testing transformation of " + xml + "...");
 460             setXsl(fromInputStream(getClass().getResourceAsStream(xsl)));
 461             setSourceXml(fromInputStream(getClass().getResourceAsStream(xml)));
 462             Transformer t = getTransformer();
 463             StringWriter resultWriter = new StringWriter();
 464             t.transform(getStreamSource(), new StreamResult(resultWriter));
 465             String resultString = resultWriter.toString().replaceAll("\\r\\n", "\n").replaceAll("\\r", "\n").trim();
 466             String reference = fromInputStream(getClass().getResourceAsStream(ref)).trim();
 467             Assert.assertEquals(resultString, reference, "Output of transformation of " + xml + " does not match reference");
 468             System.out.println("Passed.");
 469         }
 470     }
 471 
 472     public static class Test8162598 extends TransformerTestTemplate {
 473 
 474         private static String XSL_INPUT =
 475             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + LINE_SEPARATOR +
 476             "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">" + LINE_SEPARATOR +
 477             "    <xsl:template match=\"/\">" + LINE_SEPARATOR +
 478             "        <root xmlns=\"ns1\">" + LINE_SEPARATOR +
 479             "            <xsl:call-template name=\"transform\"/>" + LINE_SEPARATOR +
 480             "        </root>" + LINE_SEPARATOR +
 481             "    </xsl:template>" + LINE_SEPARATOR +
 482             "    <xsl:template name=\"transform\">" + LINE_SEPARATOR +
 483             "        <test1 xmlns=\"ns2\"><b xmlns=\"ns2\"><c xmlns=\"\"></c></b></test1>" + LINE_SEPARATOR +
 484             "        <test2 xmlns=\"ns1\"><b xmlns=\"ns2\"><c xmlns=\"\"></c></b></test2>" + LINE_SEPARATOR +
 485             "        <test3><b><c xmlns=\"\"></c></b></test3>" + LINE_SEPARATOR +
 486             "        <test4 xmlns=\"\"><b><c xmlns=\"\"></c></b></test4>" + LINE_SEPARATOR +
 487             "        <test5 xmlns=\"ns1\"><b><c xmlns=\"\"></c></b></test5>" + LINE_SEPARATOR +
 488             "        <test6 xmlns=\"\"/>" + LINE_SEPARATOR +
 489             "    </xsl:template>" + LINE_SEPARATOR +
 490             "</xsl:stylesheet>";
 491 
 492         private static String XML_INPUT =
 493             "<?xml version=\"1.0\" encoding=\"UTF-8\"?><aaa></aaa>" + LINE_SEPARATOR;
 494 
 495         public Test8162598() {
 496             super(XSL_INPUT, XML_INPUT);
 497         }
 498 
 499         /**
 500          * Utility method for testBug8162598().
 501          * Provides a convenient way to check/assert the expected namespaces
 502          * of a Node and its siblings.
 503          *
 504          * @param test
 505          * The node to check
 506          * @param nstest
 507          * Expected namespace of the node
 508          * @param nsb
 509          * Expected namespace of the first sibling
 510          * @param nsc
 511          * Expected namespace of the first sibling of the first sibling
 512          */

 513         private void checkNodeNS(Node test, String nstest, String nsb, String nsc) {
 514             String testNodeName = test.getNodeName();
 515             if (nstest == null) {
 516                 Assert.assertNull(test.getNamespaceURI(), "unexpected namespace for " + testNodeName);
 517             } else {
 518                 Assert.assertEquals(test.getNamespaceURI(), nstest, "unexpected namespace for " + testNodeName);
 519             }
 520             Node b = test.getChildNodes().item(0);
 521             if (nsb == null) {
 522                 Assert.assertNull(b.getNamespaceURI(), "unexpected namespace for " + testNodeName + "->b");
 523             } else {
 524                 Assert.assertEquals(b.getNamespaceURI(), nsb, "unexpected namespace for " + testNodeName + "->b");
 525             }
 526             Node c = b.getChildNodes().item(0);
 527             if (nsc == null) {
 528                 Assert.assertNull(c.getNamespaceURI(), "unexpected namespace for " + testNodeName + "->b->c");
 529             } else {
 530                 Assert.assertEquals(c.getNamespaceURI(), nsc, "unexpected namespace for " + testNodeName + "->b->c");
 531             }
 532         }
 533 
 534         /*
 535          * @bug 8162598
 536          * @summary Test XSLTC handling of namespaces, especially empty namespace
 537          *          definitions to reset the default namespace
 538          */
 539         @Test
 540         public void run()  throws Exception {
 541             // print input
 542             printSnippet("Source:", getSourceXml());
 543             printSnippet("Stylesheet:", getXsl());
 544 
 545             // transform to DOM result
 546             Transformer t = getTransformer();


 547             DOMResult result = new DOMResult();
 548             t.transform(getStreamSource(), result);


 549 
 550             // print output
 551             printSnippet("Result after transformation:", prettyPrintDOMResult(result));






 552 
 553             // do some verifications
 554             Document document = (Document)result.getNode();
 555             checkNodeNS(document.getElementsByTagName("test1").item(0), "ns2", "ns2", null);
 556             checkNodeNS(document.getElementsByTagName("test2").item(0), "ns1", "ns2", null);
 557             checkNodeNS(document.getElementsByTagName("test3").item(0), null, null, null);
 558             checkNodeNS(document.getElementsByTagName("test4").item(0), null, null, null);
 559             checkNodeNS(document.getElementsByTagName("test5").item(0), "ns1", "ns1", null);
 560             Assert.assertNull(document.getElementsByTagName("test6").item(0).getNamespaceURI(),
 561                 "unexpected namespace for test6");
 562         }
 563     }
 564 
 565     public static class Test8169112 extends TransformerTestTemplate{








 566 
 567         public static String XML_INPUT =
 568             "<?xml version=\"1.0\"?><DOCROOT/>";
 569 
 570         public Test8169112() throws IOException {
 571             super();
 572             setXsl(fromInputStream(getClass().getResourceAsStream("Bug8169112.xsl")));
 573             setSourceXml(XML_INPUT);
 574         }
 575 
 576         /**
 577          * @throws TransformerException
 578          * @bug 8169112
 579          * @summary Test compilation of large xsl file with outlining.
 580          *
 581          * This test merely compiles a large xsl file and tests if its bytecode
 582          * passes verification by invoking the transform() method for
 583          * dummy content. The test succeeds if no Exception is thrown
 584          */
 585         @Test
 586         public void run() throws TransformerException {
 587             Transformer t = getTransformer();
 588             t.transform(getStreamSource(), new StreamResult(new ByteArrayOutputStream()));
 589         }
 590     }
 591 
 592     public static class Test8169772 extends TransformerTestTemplate {
 593 
 594         public Test8169772() {
 595             super();
 596         }
 597 
 598         private Document getDOMWithBadElement() throws SAXException, IOException, ParserConfigurationException {
 599             // create a small DOM
 600             Document doc = DocumentBuilderFactory.newInstance().
 601                 newDocumentBuilder().parse(
 602                     new ByteArrayInputStream(
 603                         "<?xml version=\"1.0\"?><DOCROOT/>".getBytes()
 604                     )
 605                 );
 606 
 607             // insert a bad element
 608             Element e = doc.createElement("ERROR");
 609             e.appendChild(doc.createTextNode(null));
 610             doc.getDocumentElement().appendChild(e);
 611 
 612             return doc;
 613         }
 614 
 615         /**
 616          * @throws ParserConfigurationException
 617          * @throws IOException
 618          * @throws SAXException
 619          * @throws TransformerException
 620          * @bug 8169772
 621          * @summary Test transformation of DOM with null valued text node
 622          *
 623          * This test would throw a NullPointerException during transform when the
 624          * fix was not present.
 625          */
 626         @Test
 627         public void run() throws SAXException, IOException, ParserConfigurationException, TransformerException {
 628             Transformer t = getTransformer();
 629             StringWriter resultWriter = new StringWriter();
 630             DOMSource d = new DOMSource(getDOMWithBadElement().getDocumentElement());
 631             t.transform(d, new StreamResult(resultWriter));
 632             printSnippet("Transformation result (DOM with null text node):", resultWriter.toString());
 633         }
 634     }
 635 }
< prev index next >