1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   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 javax.xml.stream.XMLStreamWriterTest;
  25 
  26 import java.io.ByteArrayOutputStream;
  27 import java.io.IOException;
  28 
  29 import javax.xml.XMLConstants;
  30 import javax.xml.stream.XMLOutputFactory;
  31 import javax.xml.stream.XMLStreamException;
  32 import javax.xml.stream.XMLStreamWriter;
  33 import javax.xml.transform.stream.StreamResult;
  34 
  35 import org.testng.Assert;
  36 import org.testng.annotations.BeforeMethod;
  37 import org.testng.annotations.Test;
  38 
  39 import com.sun.xml.internal.stream.writers.XMLStreamWriterImpl;
  40 
  41 /*
  42  * @summary Test the writing of Namespaces.
  43  */
  44 public class NamespaceTest {
  45 
  46     /** debug output? */
  47     private static final boolean DEBUG = true;
  48 
  49     /** Factory to reuse. */
  50     XMLOutputFactory xmlOutputFactory = null;
  51 
  52     /** Writer to reuse. */
  53     XMLStreamWriter xmlStreamWriter = null;
  54 
  55     /** OutputStream to reuse. */
  56     ByteArrayOutputStream byteArrayOutputStream = null;
  57 
  58     @BeforeMethod
  59     public void setUp() {
  60 
  61         // want a Factory that repairs Namespaces
  62         xmlOutputFactory = XMLOutputFactory.newInstance();
  63         xmlOutputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE);
  64 
  65         // new OutputStream
  66         byteArrayOutputStream = new ByteArrayOutputStream();
  67 
  68         // new Writer
  69         try {
  70             xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(byteArrayOutputStream, "utf-8");
  71 
  72         } catch (XMLStreamException xmlStreamException) {
  73             Assert.fail(xmlStreamException.toString());
  74         }
  75     }
  76 
  77     /**
  78      * Reset Writer for reuse.
  79      */
  80     private void resetWriter() {
  81         // reset the Writer
  82         try {
  83             xmlStreamWriter.flush();
  84             xmlStreamWriter.close();
  85             ((XMLStreamWriterImpl) xmlStreamWriter).reset();
  86             byteArrayOutputStream.reset();
  87             ((XMLStreamWriterImpl) xmlStreamWriter).setOutput(new StreamResult(byteArrayOutputStream), "utf-8");
  88         } catch (XMLStreamException xmlStreamException) {
  89             Assert.fail(xmlStreamException.toString());
  90         } catch (IOException ioException) {
  91             Assert.fail(ioException.toString());
  92         }
  93     }
  94 
  95     @Test
  96     public void testDoubleXmlNs() {
  97         try {
  98 
  99             xmlStreamWriter.writeStartDocument();
 100             xmlStreamWriter.writeStartElement("foo");
 101             xmlStreamWriter.writeNamespace("xml", XMLConstants.XML_NS_URI);
 102             xmlStreamWriter.writeAttribute("xml", XMLConstants.XML_NS_URI, "lang", "ja_JP");
 103             xmlStreamWriter.writeCharacters("Hello");
 104             xmlStreamWriter.writeEndElement();
 105             xmlStreamWriter.writeEndDocument();
 106 
 107             xmlStreamWriter.flush();
 108             String actualOutput = byteArrayOutputStream.toString();
 109 
 110             if (DEBUG) {
 111                 System.out.println("testDoubleXmlNs(): actualOutput: " + actualOutput);
 112             }
 113 
 114             // there should be no xmlns:xml
 115             Assert.assertTrue(actualOutput.split("xmlns:xml").length == 1, "Expected 0 xmlns:xml, actual output: " + actualOutput);
 116         } catch (Exception e) {
 117             e.printStackTrace();
 118             Assert.fail(e.getMessage());
 119         }
 120     }
 121 
 122     @Test
 123     public void testDuplicateNamespaceURI() throws Exception {
 124 
 125         xmlStreamWriter.writeStartDocument();
 126         xmlStreamWriter.writeStartElement(new String(""), "localName", new String("nsUri"));
 127         xmlStreamWriter.writeNamespace(new String(""), new String("nsUri"));
 128         xmlStreamWriter.writeEndElement();
 129         xmlStreamWriter.writeEndDocument();
 130 
 131         xmlStreamWriter.flush();
 132         String actualOutput = byteArrayOutputStream.toString();
 133 
 134         if (DEBUG) {
 135             System.out.println("testDuplicateNamespaceURI(): actualOutput: " + actualOutput);
 136         }
 137 
 138         // there must be only 1 xmlns=...
 139         Assert.assertTrue(actualOutput.split("xmlns").length == 2, "Expected 1 xmlns=, actual output: " + actualOutput);
 140     }
 141 
 142     // TODO: test with both "" & null
 143     // NDW: There's no distinction in XML between a "null" namespace URI and one
 144     // with a URI of "" (the empty string) so I haven't tried to call out any
 145     // such distinctions.
 146 
 147     // ---------------- Current default namespace is "" ----------------
 148 
 149     private void startDocumentEmptyDefaultNamespace(XMLStreamWriter xmlStreamWriter) throws XMLStreamException {
 150 
 151         xmlStreamWriter.writeStartDocument();
 152         xmlStreamWriter.writeStartElement("root");
 153         xmlStreamWriter.writeDefaultNamespace("");
 154     }
 155 
 156     private String endDocumentEmptyDefaultNamespace(XMLStreamWriter xmlStreamWriter) throws XMLStreamException {
 157 
 158         xmlStreamWriter.writeEndDocument();
 159 
 160         xmlStreamWriter.flush();
 161 
 162         return byteArrayOutputStream.toString();
 163     }
 164 
 165     /**
 166      * Current default namespace is "".
 167      * writeStartElement("", "localName"", "")
 168      * requires no fixup
 169      */
 170     @Test
 171     public void testEmptyDefaultEmptyPrefix() throws Exception {
 172 
 173         final String EXPECTED_OUTPUT = "<?xml version=\"1.0\" ?>" + "<root xmlns=\"\">" + "<localName>" + "requires no fixup" + "</localName>" + "</root>";
 174 
 175         startDocumentEmptyDefaultNamespace(xmlStreamWriter);
 176 
 177         xmlStreamWriter.writeStartElement("", "localName", "");
 178         xmlStreamWriter.writeCharacters("requires no fixup");
 179 
 180         String actualOutput = endDocumentEmptyDefaultNamespace(xmlStreamWriter);
 181 
 182         if (DEBUG) {
 183             System.out.println("testEmptyDefaultEmptyPrefix(): actualOutput: " + actualOutput);
 184         }
 185 
 186         Assert.assertEquals(EXPECTED_OUTPUT, actualOutput);
 187     }
 188 
 189     /**
 190      * Current default namespace is "".
 191      *
 192      * writeStartElement("prefix", "localName", "http://example.org/myURI")
 193      *
 194      * requires no fixup, but should generate a declaration for "prefix":
 195      * xmlns:prefix="http://example.org/myURI" if necessary
 196      *
 197      * necessary to generate a declaration in this test case.
 198      */
 199     @Test
 200     public void testEmptyDefaultSpecifiedPrefix() throws Exception {
 201 
 202         final String EXPECTED_OUTPUT = "<?xml version=\"1.0\" ?>" + "<root xmlns=\"\">" + "<prefix:localName xmlns:prefix=\"http://example.org/myURI\">"
 203                 + "generate xmlns:prefix" + "</prefix:localName>" + "</root>";
 204 
 205         startDocumentEmptyDefaultNamespace(xmlStreamWriter);
 206 
 207         xmlStreamWriter.writeStartElement("prefix", "localName", "http://example.org/myURI");
 208         xmlStreamWriter.writeCharacters("generate xmlns:prefix");
 209 
 210         String actualOutput = endDocumentEmptyDefaultNamespace(xmlStreamWriter);
 211 
 212         if (DEBUG) {
 213             System.out.println("testEmptyDefaultSpecifiedPrefix(): actualOutput: " + actualOutput);
 214         }
 215 
 216         Assert.assertEquals(EXPECTED_OUTPUT, actualOutput);
 217     }
 218 
 219     /**
 220      * Current default namespace is "".
 221      *
 222      * writeStartElement("prefix", "localName", "http://example.org/myURI")
 223      *
 224      * requires no fixup, but should generate a declaration for "prefix":
 225      * xmlns:prefix="http://example.org/myURI" if necessary
 226      *
 227      * not necessary to generate a declaration in this test case.
 228      */
 229     @Test
 230     public void testEmptyDefaultSpecifiedPrefixNoDeclarationGeneration() throws Exception {
 231 
 232         final String EXPECTED_OUTPUT = "<?xml version=\"1.0\" ?>" + "<root xmlns=\"\"" + " xmlns:prefix=\"http://example.org/myURI\">" + "<prefix:localName>"
 233                 + "not necessary to generate a declaration" + "</prefix:localName>" + "</root>";
 234 
 235         startDocumentEmptyDefaultNamespace(xmlStreamWriter);
 236 
 237         xmlStreamWriter.writeNamespace("prefix", "http://example.org/myURI");
 238 
 239         xmlStreamWriter.writeStartElement("prefix", "localName", "http://example.org/myURI");
 240         xmlStreamWriter.writeCharacters("not necessary to generate a declaration");
 241 
 242         String actualOutput = endDocumentEmptyDefaultNamespace(xmlStreamWriter);
 243 
 244         if (DEBUG) {
 245             System.out.println("testEmptyDefaultSpecifiedPrefixNoDeclarationGeneration(): expectedOutput: " + EXPECTED_OUTPUT);
 246             System.out.println("testEmptyDefaultSpecifiedPrefixNoDeclarationGeneration():   actualOutput: " + actualOutput);
 247         }
 248 
 249         Assert.assertEquals(EXPECTED_OUTPUT, actualOutput);
 250     }
 251 
 252     /**
 253      * Current default namespace is "".
 254      *
 255      * writeStartElement("", "localName", "http://example.org/myURI")
 256      *
 257      * should "fixup" the declaration for the default namespace:
 258      * xmlns="http://example.org/myURI"
 259      */
 260     @Test
 261     public void testEmptyDefaultSpecifiedDefault() throws Exception {
 262 
 263         final String EXPECTED_OUTPUT = "<?xml version=\"1.0\" ?>" + "<root xmlns=\"\">" + "<localName xmlns=\"http://example.org/myURI\">" + "generate xmlns"
 264                 + "</localName>" + "</root>";
 265 
 266         startDocumentEmptyDefaultNamespace(xmlStreamWriter);
 267 
 268         xmlStreamWriter.writeStartElement("", "localName", "http://example.org/myURI");
 269         xmlStreamWriter.writeCharacters("generate xmlns");
 270 
 271         String actualOutput = endDocumentEmptyDefaultNamespace(xmlStreamWriter);
 272 
 273         if (DEBUG) {
 274             System.out.println("testEmptyDefaultSpecifiedDefault(): expectedOutput: " + EXPECTED_OUTPUT);
 275             System.out.println("testEmptyDefaultSpecifiedDefault():   actualOutput: " + actualOutput);
 276         }
 277 
 278         Assert.assertEquals(EXPECTED_OUTPUT, actualOutput);
 279     }
 280 
 281     /**
 282      * Current default namespace is "".
 283      *
 284      * writeAttribute("", "", "attrName", "value")
 285      *
 286      * requires no fixup
 287      */
 288     @Test
 289     public void testEmptyDefaultEmptyPrefixWriteAttribute() throws Exception {
 290 
 291         final String EXPECTED_OUTPUT = "<?xml version=\"1.0\" ?>" + "<root xmlns=\"\" attrName=\"value\">" + "requires no fixup" + "</root>";
 292 
 293         startDocumentEmptyDefaultNamespace(xmlStreamWriter);
 294 
 295         xmlStreamWriter.writeAttribute("", "", "attrName", "value");
 296         xmlStreamWriter.writeCharacters("requires no fixup");
 297 
 298         String actualOutput = endDocumentEmptyDefaultNamespace(xmlStreamWriter);
 299 
 300         if (DEBUG) {
 301             System.out.println("testEmptyDefaultEmptyPrefixWriteAttribute(): expectedOutput: " + EXPECTED_OUTPUT);
 302             System.out.println("testEmptyDefaultEmptyPrefixWriteAttribute():   actualOutput: " + actualOutput);
 303         }
 304 
 305         Assert.assertEquals(EXPECTED_OUTPUT, actualOutput);
 306     }
 307 
 308     /**
 309      * Current default namespace is "".
 310      *
 311      * writeAttribute("p", "http://example.org/myURI", "attrName", "value")
 312      *
 313      * requires no fixup, but should generate a declaration for "p":
 314      * xmlns:p="http://example.org/myURI" if necessary
 315      *
 316      * necessary to generate a declaration in this test case.
 317      */
 318     @Test
 319     public void testEmptyDefaultSpecifiedPrefixWriteAttribute() throws Exception {
 320 
 321         final String EXPECTED_OUTPUT = "<?xml version=\"1.0\" ?>" + "<root xmlns=\"\" xmlns:p=\"http://example.org/myURI\" p:attrName=\"value\">"
 322                 + "generate xmlns:p=\"http://example.org/myURI\"" + "</root>";
 323 
 324         startDocumentEmptyDefaultNamespace(xmlStreamWriter);
 325 
 326         xmlStreamWriter.writeAttribute("p", "http://example.org/myURI", "attrName", "value");
 327         xmlStreamWriter.writeCharacters("generate xmlns:p=\"http://example.org/myURI\"");
 328 
 329         String actualOutput = endDocumentEmptyDefaultNamespace(xmlStreamWriter);
 330 
 331         if (DEBUG) {
 332             System.out.println("testEmptyDefaultSpecifiedPrefixWriteAttribute(): expectedOutput: " + EXPECTED_OUTPUT);
 333             System.out.println("testEmptyDefaultSpecifiedPrefixWriteAttribute():   actualOutput: " + actualOutput);
 334         }
 335 
 336         Assert.assertEquals(EXPECTED_OUTPUT, actualOutput);
 337     }
 338 
 339     /**
 340      * Current default namespace is "".
 341      *
 342      * writeAttribute("p", "http://example.org/myURI", "attrName", "value")
 343      *
 344      * requires no fixup, but should generate a declaration for "p":
 345      * xmlns:p="http://example.org/myURI" if necessary
 346      *
 347      * not necessary to generate a declaration in this test case.
 348      */
 349     @Test
 350     public void testEmptyDefaultSpecifiedPrefixWriteAttributeNoDeclarationGeneration() throws Exception {
 351 
 352         final String EXPECTED_OUTPUT = "<?xml version=\"1.0\" ?>" + "<root xmlns=\"\" xmlns:p=\"http://example.org/myURI\" p:attrName=\"value\">"
 353                 + "not necessary to generate a declaration" + "</root>";
 354 
 355         startDocumentEmptyDefaultNamespace(xmlStreamWriter);
 356 
 357         xmlStreamWriter.writeNamespace("p", "http://example.org/myURI");
 358 
 359         xmlStreamWriter.writeAttribute("p", "http://example.org/myURI", "attrName", "value");
 360         xmlStreamWriter.writeCharacters("not necessary to generate a declaration");
 361 
 362         String actualOutput = endDocumentEmptyDefaultNamespace(xmlStreamWriter);
 363 
 364         if (DEBUG) {
 365             System.out.println("testEmptyDefaultSpecifiedPrefixWriteAttributeNoDeclarationGeneration(): expectedOutput: " + EXPECTED_OUTPUT);
 366             System.out.println("testEmptyDefaultSpecifiedPrefixWriteAttributeNoDeclarationGeneration():   actualOutput: " + actualOutput);
 367         }
 368 
 369         Assert.assertEquals(EXPECTED_OUTPUT, actualOutput);
 370     }
 371 
 372     /**
 373      * Current default namespace is "".
 374      *
 375      * writeAttribute("", "http://example.org/myURI", "attrName", "value")
 376      *
 377      * XMLOutputFactory (Javadoc) : "If a writer isRepairingNamespaces it will
 378      * create a namespace declaration on the current StartElement for any
 379      * attribute that does not currently have a namespace declaration in scope.
 380      * If the StartElement has a uri but no prefix specified a prefix will be
 381      * assigned, if the prefix has not been declared in a parent of the current
 382      * StartElement it will be declared on the current StartElement. If the
 383      * defaultNamespace is bound and in scope and the default namespace matches
 384      * the URI of the attribute or StartElement QName no prefix will be
 385      * assigned."
 386      *
 387      * prefix needs to be assigned for this test case.
 388      */
 389     @Test
 390     public void testEmptyDefaultEmptyPrefixSpecifiedNamespaceURIWriteAttribute() throws Exception {
 391 
 392         final String EXPECTED_OUTPUT = "<?xml version=\"1.0\" ?>"
 393                 + "<root xmlns=\"\" xmlns:{generated prefix}=\"http://example.org/myURI\" {generated prefix}:attrName=\"value\">"
 394                 + "generate xmlns declaration {generated prefix}=\"http://example.org/myURI\"" + "</root>";
 395 
 396         startDocumentEmptyDefaultNamespace(xmlStreamWriter);
 397 
 398         xmlStreamWriter.writeAttribute("", "http://example.org/myURI", "attrName", "value");
 399         xmlStreamWriter.writeCharacters("generate xmlns declaration {generated prefix}=\"http://example.org/myURI\"");
 400 
 401         String actualOutput = endDocumentEmptyDefaultNamespace(xmlStreamWriter);
 402 
 403         if (DEBUG) {
 404             System.out.println("testEmptyDefaultUnspecifiedPrefixWriteAttribute(): expectedOutput: " + EXPECTED_OUTPUT);
 405             System.out.println("testEmptyDefaultUnspecifiedPrefixWriteAttribute():   actualOutput: " + actualOutput);
 406         }
 407 
 408         // there must be one xmlns=
 409         Assert.assertTrue(actualOutput.split("xmlns=").length == 2, "Expected 1 xmlns=, actual output: " + actualOutput);
 410 
 411         // there must be one xmlns:{generated prefix}="..."
 412         Assert.assertTrue(actualOutput.split("xmlns:").length == 2, "Expected 1 xmlns:{generated prefix}=\"\", actual output: " + actualOutput);
 413 
 414         // there must be one {generated prefix}:attrName="value"
 415         Assert.assertTrue(actualOutput.split(":attrName=\"value\"").length == 2, "Expected 1 {generated prefix}:attrName=\"value\", actual output: "
 416                 + actualOutput);
 417     }
 418 
 419     /**
 420      * Current default namespace is "".
 421      *
 422      * writeAttribute("", "http://example.org/myURI", "attrName", "value")
 423      *
 424      * XMLOutputFactory (Javadoc) : "If a writer isRepairingNamespaces it will
 425      * create a namespace declaration on the current StartElement for any
 426      * attribute that does not currently have a namespace declaration in scope.
 427      * If the StartElement has a uri but no prefix specified a prefix will be
 428      * assigned, if the prefix has not been declared in a parent of the current
 429      * StartElement it will be declared on the current StartElement. If the
 430      * defaultNamespace is bound and in scope and the default namespace matches
 431      * the URI of the attribute or StartElement QName no prefix will be
 432      * assigned."
 433      *
 434      * no prefix needs to be assigned for this test case
 435      */
 436     @Test
 437     public void testEmptyDefaultEmptyPrefixSpecifiedNamespaceURIWriteAttributeNoPrefixGeneration() throws Exception {
 438 
 439         final String EXPECTED_OUTPUT = "<?xml version=\"1.0\" ?>" + "<root xmlns=\"\" xmlns:p=\"http://example.org/myURI\" p:attrName=\"value\">"
 440                 + "no prefix generation" + "</root>";
 441 
 442         startDocumentEmptyDefaultNamespace(xmlStreamWriter);
 443 
 444         xmlStreamWriter.writeNamespace("p", "http://example.org/myURI");
 445 
 446         xmlStreamWriter.writeAttribute("", "http://example.org/myURI", "attrName", "value");
 447         xmlStreamWriter.writeCharacters("no prefix generation");
 448 
 449         String actualOutput = endDocumentEmptyDefaultNamespace(xmlStreamWriter);
 450 
 451         if (DEBUG) {
 452             System.out.println("testEmptyDefaultEmptyPrefixSpecifiedNamespaceURIWriteAttributeNoPrefixGeneration(): expectedOutput: " + EXPECTED_OUTPUT);
 453             System.out.println("testEmptyDefaultEmptyPrefixSpecifiedNamespaceURIWriteAttributeNoPrefixGeneration():   actualOutput: " + actualOutput);
 454         }
 455 
 456         Assert.assertEquals(EXPECTED_OUTPUT, actualOutput);
 457     }
 458 
 459     // ---------------- Current default namespace is
 460     // "http://example.org/uniqueURI" ----------------
 461 
 462     private void startDocumentSpecifiedDefaultNamespace(XMLStreamWriter xmlStreamWriter) throws XMLStreamException {
 463 
 464         xmlStreamWriter.writeStartDocument();
 465         xmlStreamWriter.writeStartElement("root");
 466         xmlStreamWriter.writeDefaultNamespace("http://example.org/uniqueURI");
 467     }
 468 
 469     private String endDocumentSpecifiedDefaultNamespace(XMLStreamWriter xmlStreamWriter) throws XMLStreamException {
 470 
 471         xmlStreamWriter.writeEndDocument();
 472 
 473         xmlStreamWriter.flush();
 474 
 475         return byteArrayOutputStream.toString();
 476     }
 477 
 478     /**
 479      * Current default namespace is "http://example.org/uniqueURI".
 480      *
 481      * writeElement("", "localName", "")
 482      *
 483      * should "fixup" the declaration for the default namespace: xmlns=""
 484      */
 485     @Test
 486     public void testSpecifiedDefaultEmptyPrefix() throws Exception {
 487 
 488         final String EXPECTED_OUTPUT = "<?xml version=\"1.0\" ?>" + "<root xmlns=\"http://example.org/uniqueURI\">" + "<localName xmlns=\"\">"
 489                 + "generate xmlns=\"\"" + "</localName>" + "</root>";
 490 
 491         startDocumentSpecifiedDefaultNamespace(xmlStreamWriter);
 492 
 493         xmlStreamWriter.writeStartElement("", "localName", "");
 494         xmlStreamWriter.writeCharacters("generate xmlns=\"\"");
 495 
 496         String actualOutput = endDocumentSpecifiedDefaultNamespace(xmlStreamWriter);
 497 
 498         if (DEBUG) {
 499             System.out.println("testSpecifiedDefaultEmptyPrefix(): expectedOutput: " + EXPECTED_OUTPUT);
 500             System.out.println("testSpecifiedDefaultEmptyPrefix():   actualOutput: " + actualOutput);
 501         }
 502 
 503         Assert.assertEquals(EXPECTED_OUTPUT, actualOutput);
 504     }
 505 
 506     /**
 507      * Current default namespace is "http://example.org/uniqueURI".
 508      *
 509      * writeStartElement("p", "localName", "http://example.org/myURI")
 510      *
 511      * requires no fixup, but should generate a declaration for "p":
 512      * xmlns:p="http://example.org/myURI" if necessary
 513      *
 514      * test case where it is necessary to generate a declaration.
 515      */
 516     @Test
 517     public void testSpecifiedDefaultSpecifiedPrefix() throws Exception {
 518 
 519         final String EXPECTED_OUTPUT = "<?xml version=\"1.0\" ?>" + "<root xmlns=\"http://example.org/uniqueURI\">"
 520                 + "<p:localName xmlns:p=\"http://example.org/myURI\">" + "generate xmlns:p=\"http://example.org/myURI\"" + "</p:localName>" + "</root>";
 521 
 522         startDocumentSpecifiedDefaultNamespace(xmlStreamWriter);
 523 
 524         xmlStreamWriter.writeStartElement("p", "localName", "http://example.org/myURI");
 525         xmlStreamWriter.writeCharacters("generate xmlns:p=\"http://example.org/myURI\"");
 526 
 527         String actualOutput = endDocumentSpecifiedDefaultNamespace(xmlStreamWriter);
 528 
 529         if (DEBUG) {
 530             System.out.println("testSpecifiedDefaultSpecifiedPrefix(): expectedOutput: " + EXPECTED_OUTPUT);
 531             System.out.println("testSpecifiedDefaultSpecifiedPrefix():   actualOutput: " + actualOutput);
 532         }
 533 
 534         Assert.assertEquals(EXPECTED_OUTPUT, actualOutput);
 535     }
 536 
 537     /**
 538      * Current default namespace is "http://example.org/uniqueURI".
 539      *
 540      * writeStartElement("p", "localName", "http://example.org/myURI")
 541      *
 542      * requires no fixup, but should generate a declaration for "p":
 543      * xmlns:p="http://example.org/myURI" if necessary
 544      *
 545      * test case where it is not necessary to generate a declaration.
 546      */
 547     @Test
 548     public void testSpecifiedDefaultSpecifiedPrefixNoPrefixGeneration() throws Exception {
 549 
 550         final String EXPECTED_OUTPUT = "<?xml version=\"1.0\" ?>" + "<root" + " xmlns=\"http://example.org/uniqueURI\""
 551                 + " xmlns:p=\"http://example.org/myURI\">" + "<p:localName>" + "not necessary to generate a declaration" + "</p:localName>" + "</root>";
 552 
 553         startDocumentSpecifiedDefaultNamespace(xmlStreamWriter);
 554 
 555         xmlStreamWriter.writeNamespace("p", "http://example.org/myURI");
 556 
 557         xmlStreamWriter.writeStartElement("p", "localName", "http://example.org/myURI");
 558         xmlStreamWriter.writeCharacters("not necessary to generate a declaration");
 559 
 560         String actualOutput = endDocumentSpecifiedDefaultNamespace(xmlStreamWriter);
 561 
 562         if (DEBUG) {
 563             System.out.println("testSpecifiedDefaultSpecifiedPrefixNoPrefixGeneration(): expectedOutput: " + EXPECTED_OUTPUT);
 564             System.out.println("testSpecifiedDefaultSpecifiedPrefixNoPrefixGeneration():   actualOutput: " + actualOutput);
 565         }
 566 
 567         Assert.assertEquals(EXPECTED_OUTPUT, actualOutput);
 568     }
 569 
 570     /**
 571      * Current default namespace is "http://example.org/uniqueURI".
 572      *
 573      * writeStartElement("", "localName", "http://example.org/myURI")
 574      *
 575      * should "fixup" the declaration for the default namespace:
 576      * xmlns="http://example.org/myURI"
 577      */
 578     @Test
 579     public void testSpecifiedDefaultEmptyPrefixSpecifiedNamespaceURI() throws Exception {
 580 
 581         final String EXPECTED_OUTPUT = "<?xml version=\"1.0\" ?>" + "<root xmlns=\"http://example.org/uniqueURI\">"
 582                 + "<localName xmlns=\"http://example.org/myURI\">" + "generate xmlns=\"http://example.org/myURI\"" + "</localName>" + "</root>";
 583 
 584         startDocumentSpecifiedDefaultNamespace(xmlStreamWriter);
 585 
 586         xmlStreamWriter.writeStartElement("", "localName", "http://example.org/myURI");
 587         xmlStreamWriter.writeCharacters("generate xmlns=\"http://example.org/myURI\"");
 588 
 589         String actualOutput = endDocumentSpecifiedDefaultNamespace(xmlStreamWriter);
 590 
 591         if (DEBUG) {
 592             System.out.println("testSpecifiedDefaultEmptyPrefixSpecifiedNamespaceURI(): expectedOutput: " + EXPECTED_OUTPUT);
 593             System.out.println("testSpecifiedDefaultEmptyPrefixSpecifiedNamespaceURI():   actualOutput: " + actualOutput);
 594         }
 595 
 596         Assert.assertEquals(EXPECTED_OUTPUT, actualOutput);
 597     }
 598 
 599     /**
 600      * Current default namespace is "http://example.org/uniqueURI".
 601      *
 602      * writeAttribute("", "", "attrName", "value")
 603      *
 604      * requires no fixup
 605      */
 606     @Test
 607     public void testSpecifiedDefaultEmptyPrefixWriteAttribute() throws Exception {
 608 
 609         final String EXPECTED_OUTPUT = "<?xml version=\"1.0\" ?>" + "<root xmlns=\"http://example.org/uniqueURI\" attrName=\"value\">" + "requires no fixup"
 610                 + "</root>";
 611 
 612         startDocumentSpecifiedDefaultNamespace(xmlStreamWriter);
 613 
 614         xmlStreamWriter.writeAttribute("", "", "attrName", "value");
 615         xmlStreamWriter.writeCharacters("requires no fixup");
 616 
 617         String actualOutput = endDocumentEmptyDefaultNamespace(xmlStreamWriter);
 618 
 619         if (DEBUG) {
 620             System.out.println("testSpecifiedDefaultEmptyPrefixWriteAttribute(): expectedOutput: " + EXPECTED_OUTPUT);
 621             System.out.println("testSpecifiedDefaultEmptyPrefixWriteAttribute():   actualOutput: " + actualOutput);
 622         }
 623 
 624         Assert.assertEquals(EXPECTED_OUTPUT, actualOutput);
 625     }
 626 
 627     /**
 628      * Current default namespace is "http://example.org/uniqueURI".
 629      *
 630      * writeAttribute("p", "http://example.org/myURI", "attrName", "value")
 631      *
 632      * requires no fixup, but should generate a declaration for "p":
 633      * xmlns:p="http://example.org/myURI" if necessary
 634      *
 635      * test case where it is necessary to generate a declaration.
 636      */
 637     @Test
 638     public void testSpecifiedDefaultSpecifiedPrefixWriteAttribute() throws Exception { // want
 639                                                                                        // to
 640                                                                                        // test
 641 
 642         final String EXPECTED_OUTPUT = "<?xml version=\"1.0\" ?>"
 643                 + "<root xmlns=\"http://example.org/uniqueURI\" xmlns:p=\"http://example.org/myURI\" p:attrName=\"value\">"
 644                 + "generate xmlns:p=\"http://example.org/myURI\"" + "</root>";
 645 
 646         startDocumentSpecifiedDefaultNamespace(xmlStreamWriter);
 647 
 648         xmlStreamWriter.writeAttribute("p", "http://example.org/myURI", "attrName", "value");
 649         xmlStreamWriter.writeCharacters("generate xmlns:p=\"http://example.org/myURI\"");
 650 
 651         String actualOutput = endDocumentEmptyDefaultNamespace(xmlStreamWriter);
 652 
 653         if (DEBUG) {
 654             System.out.println("testSpecifiedDefaultSpecifiedPrefixWriteAttribute(): expectedOutput: " + EXPECTED_OUTPUT);
 655             System.out.println("testSpecifiedDefaultSpecifiedPrefixWriteAttribute():   actualOutput: " + actualOutput);
 656         }
 657 
 658         Assert.assertEquals(EXPECTED_OUTPUT, actualOutput);
 659     }
 660 
 661     /**
 662      * Current default namespace is "http://example.org/uniqueURI".
 663      *
 664      * writeAttribute("p", "http://example.org/myURI", "attrName", "value")
 665      *
 666      * requires no fixup, but should generate a declaration for "p":
 667      * xmlns:p="http://example.org/myURI" if necessary
 668      *
 669      * test case where it is not necessary to generate a declaration.
 670      */
 671     @Test
 672     public void testSpecifiedDefaultSpecifiedPrefixWriteAttributeNoDeclarationGeneration() throws Exception {
 673 
 674         final String EXPECTED_OUTPUT = "<?xml version=\"1.0\" ?>"
 675                 + "<root xmlns=\"http://example.org/uniqueURI\" xmlns:p=\"http://example.org/myURI\" p:attrName=\"value\">"
 676                 + "not necessary to generate a declaration" + "</root>";
 677 
 678         startDocumentSpecifiedDefaultNamespace(xmlStreamWriter);
 679 
 680         xmlStreamWriter.writeNamespace("p", "http://example.org/myURI");
 681 
 682         xmlStreamWriter.writeAttribute("p", "http://example.org/myURI", "attrName", "value");
 683         xmlStreamWriter.writeCharacters("not necessary to generate a declaration");
 684 
 685         String actualOutput = endDocumentEmptyDefaultNamespace(xmlStreamWriter);
 686 
 687         if (DEBUG) {
 688             System.out.println("testSpecifiedDefaultSpecifiedPrefixWriteAttributeNoDeclarationGeneration(): expectedOutput: " + EXPECTED_OUTPUT);
 689             System.out.println("testSpecifiedDefaultSpecifiedPrefixWriteAttributeNoDeclarationGeneration():   actualOutput: " + actualOutput);
 690         }
 691 
 692         Assert.assertEquals(EXPECTED_OUTPUT, actualOutput);
 693     }
 694 
 695     /**
 696      * Current default namespace is "http://example.org/uniqueURI".
 697      *
 698      * writeAttribute("p", "http://example.org/uniqueURI", "attrName", "value")
 699      *
 700      * requires no fixup, but should generate a declaration for "p":
 701      * xmlns:p="http://example.org/uniqueURI" if necessary. (Note that this will
 702      * potentially produce two namespace bindings with the same URI, xmlns="xxx"
 703      * and xmlns:p="xxx", but that's perfectly legal.)
 704      */
 705     @Test
 706     public void testSpecifiedDefaultSpecifiedPrefixSpecifiedNamespaceURIWriteAttribute() throws Exception {
 707 
 708         final String EXPECTED_OUTPUT = "<?xml version=\"1.0\" ?>" + "<root xmlns=\"http://example.org/uniqueURI\" attrName=\"value\">" + "requires no fixup"
 709                 + "</root>";
 710         final String EXPECTED_OUTPUT_2 = "<?xml version=\"1.0\" ?>"
 711                 + "<root xmlns=\"http://example.org/uniqueURI\" xmlns:p=\"http://example.org/uniqueURI\" p:attrName=\"value\">" + "requires no fixup"
 712                 + "</root>";
 713 
 714         startDocumentSpecifiedDefaultNamespace(xmlStreamWriter);
 715 
 716         xmlStreamWriter.writeAttribute("p", "http://example.org/uniqueURI", "attrName", "value");
 717         xmlStreamWriter.writeCharacters("requires no fixup");
 718 
 719         String actualOutput = endDocumentEmptyDefaultNamespace(xmlStreamWriter);
 720 
 721         if (DEBUG) {
 722             System.out.println("testSpecifiedDefaultSpecifiedPrefixSpecifiedNamespaceURIWriteAttribute: expectedOutput: " + EXPECTED_OUTPUT);
 723             System.out.println("testSpecifiedDefaultSpecifiedPrefixSpecifiedNamespaceURIWriteAttribute: expectedOutput: " + EXPECTED_OUTPUT_2);
 724             System.out.println("testSpecifiedDefaultSpecifiedPrefixSpecifiedNamespaceURIWriteAttribute:   actualOutput: " + actualOutput);
 725         }
 726 
 727         Assert.assertTrue(actualOutput.equals(EXPECTED_OUTPUT) || actualOutput.equals(EXPECTED_OUTPUT_2), "Expected: " + EXPECTED_OUTPUT + "\n" + "Actual: "
 728                 + actualOutput);
 729     }
 730 
 731     /**
 732      * Current default namespace is "http://example.org/uniqueURI".
 733      *
 734      * writeAttribute("", "http://example.org/myURI", "attrName", "value")
 735      *
 736      * XMLOutputFactory (Javadoc) : "If a writer isRepairingNamespaces it will
 737      * create a namespace declaration on the current StartElement for any
 738      * attribute that does not currently have a namespace declaration in scope.
 739      * If the StartElement has a uri but no prefix specified a prefix will be
 740      * assigned, if the prefix has not been declared in a parent of the current
 741      * StartElement it will be declared on the current StartElement. If the
 742      * defaultNamespace is bound and in scope and the default namespace matches
 743      * the URI of the attribute or StartElement QName no prefix will be
 744      * assigned."
 745      *
 746      * test case where prefix needs to be assigned.
 747      */
 748     @Test
 749     public void testSpecifiedDefaultEmptyPrefixSpecifiedNamespaceURIWriteAttribute() throws Exception {
 750 
 751         final String EXPECTED_OUTPUT = "<?xml version=\"1.0\" ?>" + "<root" + " xmlns=\"http://example.org/uniqueURI\""
 752                 + " xmlns:{generated prefix}=\"http://example.org/myURI\"" + " {generated prefix}:attrName=\"value\">"
 753                 + "generate xmlns declaration {generated prefix}=\"http://example.org/myURI\"" + "</root>";
 754 
 755         startDocumentSpecifiedDefaultNamespace(xmlStreamWriter);
 756 
 757         xmlStreamWriter.writeAttribute("", "http://example.org/myURI", "attrName", "value");
 758         xmlStreamWriter.writeCharacters("generate xmlns declaration {generated prefix}=\"http://example.org/myURI\"");
 759 
 760         String actualOutput = endDocumentSpecifiedDefaultNamespace(xmlStreamWriter);
 761 
 762         if (DEBUG) {
 763             System.out.println("testSpecifiedDefaultEmptyPrefixSpecifiedNamespaceURIWriteAttribute(): expectedOutput: " + EXPECTED_OUTPUT);
 764             System.out.println("testSpecifiedDefaultEmptyPrefixSpecifiedNamespaceURIWriteAttribute():   actualOutput: " + actualOutput);
 765         }
 766 
 767         // there must be one xmlns=
 768         Assert.assertTrue(actualOutput.split("xmlns=").length == 2, "Expected 1 xmlns=, actual output: " + actualOutput);
 769 
 770         // there must be one xmlns:{generated prefix}="..."
 771         Assert.assertTrue(actualOutput.split("xmlns:").length == 2, "Expected 1 xmlns:{generated prefix}=\"\", actual output: " + actualOutput);
 772 
 773         // there must be one {generated prefix}:attrName="value"
 774         Assert.assertTrue(actualOutput.split(":attrName=\"value\"").length == 2, "Expected 1 {generated prefix}:attrName=\"value\", actual output: "
 775                 + actualOutput);
 776     }
 777 
 778     /**
 779      * Current default namespace is "http://example.org/uniqueURI".
 780      *
 781      * writeAttribute("", "http://example.org/myURI", "attrName", "value")
 782      *
 783      * XMLOutputFactory (Javadoc) : "If a writer isRepairingNamespaces it will
 784      * create a namespace declaration on the current StartElement for any
 785      * attribute that does not currently have a namespace declaration in scope.
 786      * If the StartElement has a uri but no prefix specified a prefix will be
 787      * assigned, if the prefix has not been declared in a parent of the current
 788      * StartElement it will be declared on the current StartElement. If the
 789      * defaultNamespace is bound and in scope and the default namespace matches
 790      * the URI of the attribute or StartElement QName no prefix will be
 791      * assigned."
 792      *
 793      * test case where no prefix needs to be assigned.
 794      */
 795     @Test
 796     public void testSpecifiedDefaultEmptyPrefixSpecifiedNamespaceURIWriteAttributeNoPrefixGeneration() throws Exception {
 797 
 798         final String EXPECTED_OUTPUT = "<?xml version=\"1.0\" ?>" + "<root" + " xmlns=\"http://example.org/uniqueURI\""
 799                 + " xmlns:p=\"http://example.org/myURI\"" + " p:attrName=\"value\">" + "no prefix needs to be assigned" + "</root>";
 800 
 801         startDocumentSpecifiedDefaultNamespace(xmlStreamWriter);
 802 
 803         xmlStreamWriter.writeNamespace("p", "http://example.org/myURI");
 804 
 805         xmlStreamWriter.writeAttribute("", "http://example.org/myURI", "attrName", "value");
 806         xmlStreamWriter.writeCharacters("no prefix needs to be assigned");
 807 
 808         String actualOutput = endDocumentSpecifiedDefaultNamespace(xmlStreamWriter);
 809 
 810         if (DEBUG) {
 811             System.out.println("testSpecifiedDefaultEmptyPrefixSpecifiedNamespaceURIWriteAttributeNoPrefixGeneration(): expectedOutput: " + EXPECTED_OUTPUT);
 812             System.out.println("testSpecifiedDefaultEmptyPrefixSpecifiedNamespaceURIWriteAttributeNoPrefixGeneration():   actualOutput: " + actualOutput);
 813         }
 814 
 815         Assert.assertEquals(EXPECTED_OUTPUT, actualOutput);
 816     }
 817 
 818     // --------------- Serializations, sequences ---------------
 819 
 820     // Unfortunately, the nature of the StAX API makes it possible for the
 821     // programmer to generate events that cannot be serialized in XML.
 822 
 823     /**
 824      * Current default namespace is "".
 825      *
 826      * write*("p", "myuri", ...); write*("p", "otheruri", ...);
 827      *
 828      * XMLOutputFactory (Javadoc) (If repairing of namespaces is enabled): "If
 829      * element and/or attribute names in the same start or empty-element tag are
 830      * bound to different namespace URIs and are using the same prefix then the
 831      * element or the first occurring attribute retains the original prefix and
 832      * the following attributes have their prefixes replaced with a new prefix
 833      * that is bound to the namespace URIs of those attributes."
 834      */
 835     @Test
 836     public void testSamePrefixDifferentURI() throws Exception {
 837 
 838         /**
 839          * writeAttribute("p", "http://example.org/URI-ONE", "attr1", "value");
 840          * writeAttribute("p", "http://example.org/URI-TWO", "attr2", "value");
 841          */
 842         final String EXPECTED_OUTPUT = "<?xml version=\"1.0\" ?>" + "<root" + " xmlns=\"\"" + " xmlns:p=\"http://example.org/URI-ONE\"" + " p:attr1=\"value\">"
 843                 + " xmlns:{generated prefix}=\"http://example.org/URI-TWO\"" + " {generated prefix}:attr2=\"value\">"
 844                 + "remap xmlns declaration {generated prefix}=\"http://example.org/URI-TWO\"" + "</root>";
 845 
 846         startDocumentEmptyDefaultNamespace(xmlStreamWriter);
 847 
 848         xmlStreamWriter.writeAttribute("p", "http://example.org/URI-ONE", "attr1", "value");
 849         xmlStreamWriter.writeAttribute("p", "http://example.org/URI-TWO", "attr2", "value");
 850         xmlStreamWriter.writeCharacters("remap xmlns declaration {generated prefix}=\"http://example.org/URI-TWO\"");
 851 
 852         String actualOutput = endDocumentEmptyDefaultNamespace(xmlStreamWriter);
 853 
 854         if (DEBUG) {
 855             System.out.println("testSamePrefixDifferentURI(): expectedOutput: " + EXPECTED_OUTPUT);
 856             System.out.println("testSamePrefixDifferentURI():   actualOutput: " + actualOutput);
 857         }
 858 
 859         // there must be 1 xmlns=
 860         Assert.assertTrue(actualOutput.split("xmlns=").length == 2, "Expected 1 xmlns=, actual output: " + actualOutput);
 861 
 862         // there must be 2 xmlns:
 863         Assert.assertTrue(actualOutput.split("xmlns:").length == 3, "Expected 2 xmlns:, actual output: " + actualOutput);
 864 
 865         // there must be 2 :attr
 866         Assert.assertTrue(actualOutput.split(":attr").length == 3, "Expected 2 :attr, actual output: " + actualOutput);
 867 
 868         /**
 869          * writeStartElement("p", "localName", "http://example.org/URI-ONE");
 870          * writeAttribute("p", "http://example.org/URI-TWO", "attrName",
 871          * "value");
 872          */
 873         final String EXPECTED_OUTPUT_2 = "<?xml version=\"1.0\" ?>" + "<root" + " xmlns=\"\">" + "<p:localName" + " xmlns:p=\"http://example.org/URI-ONE\""
 874                 + " xmlns:{generated prefix}=\"http://example.org/URI-TWO\"" + " {generated prefix}:attrName=\"value\">" + "</p:localName>" + "</root>";
 875 
 876         // reset to known state
 877         resetWriter();
 878         startDocumentEmptyDefaultNamespace(xmlStreamWriter);
 879 
 880         xmlStreamWriter.writeStartElement("p", "localName", "http://example.org/URI-ONE");
 881         xmlStreamWriter.writeAttribute("p", "http://example.org/URI-TWO", "attrName", "value");
 882 
 883         actualOutput = endDocumentEmptyDefaultNamespace(xmlStreamWriter);
 884 
 885         if (DEBUG) {
 886             System.out.println("testSamePrefixDifferentURI(): expectedOutput: " + EXPECTED_OUTPUT_2);
 887             System.out.println("testSamePrefixDifferentURI():   actualOutput: " + actualOutput);
 888         }
 889 
 890         // there must be 1 xmlns=
 891         Assert.assertTrue(actualOutput.split("xmlns=").length == 2, "Expected 1 xmlns=, actual output: " + actualOutput);
 892 
 893         // there must be 2 xmlns:
 894         Assert.assertTrue(actualOutput.split("xmlns:").length == 3, "Expected 2 xmlns:, actual output: " + actualOutput);
 895 
 896         // there must be 2 p:localName
 897         Assert.assertTrue(actualOutput.split("p:localName").length == 3, "Expected 2 p:localName, actual output: " + actualOutput);
 898 
 899         // there must be 1 :attrName
 900         Assert.assertTrue(actualOutput.split(":attrName").length == 2, "Expected 1 :attrName, actual output: " + actualOutput);
 901 
 902         /**
 903          * writeNamespace("p", "http://example.org/URI-ONE");
 904          * writeAttribute("p", "http://example.org/URI-TWO", "attrName",
 905          * "value");
 906          */
 907         final String EXPECTED_OUTPUT_3 = "<?xml version=\"1.0\" ?>" + "<root" + " xmlns=\"\"" + " xmlns:p=\"http://example.org/URI-ONE\""
 908                 + " xmlns:{generated prefix}=\"http://example.org/URI-TWO\"" + " {generated prefix}:attrName=\"value\">" + "</root>";
 909 
 910         // reset to known state
 911         resetWriter();
 912         startDocumentEmptyDefaultNamespace(xmlStreamWriter);
 913 
 914         xmlStreamWriter.writeNamespace("p", "http://example.org/URI-ONE");
 915         xmlStreamWriter.writeAttribute("p", "http://example.org/URI-TWO", "attrName", "value");
 916 
 917         actualOutput = endDocumentEmptyDefaultNamespace(xmlStreamWriter);
 918 
 919         if (DEBUG) {
 920             System.out.println("testSamePrefixDifferentURI(): expectedOutput: " + EXPECTED_OUTPUT_3);
 921             System.out.println("testSamePrefixDifferentURI():   actualOutput: " + actualOutput);
 922         }
 923 
 924         // there must be 1 xmlns=
 925         Assert.assertTrue(actualOutput.split("xmlns=").length == 2, "Expected 1 xmlns=, actual output: " + actualOutput);
 926 
 927         // there must be 2 xmlns:
 928         Assert.assertTrue(actualOutput.split("xmlns:").length == 3, "Expected 2 xmlns:, actual output: " + actualOutput);
 929 
 930         // there must be 1 :attrName
 931         Assert.assertTrue(actualOutput.split(":attrName").length == 2, "Expected a :attrName, actual output: " + actualOutput);
 932 
 933         /**
 934          * writeNamespace("xmlns", ""); writeStartElement("", "localName",
 935          * "http://example.org/URI-TWO");
 936          */
 937         final String EXPECTED_OUTPUT_4 = "<?xml version=\"1.0\" ?>" + "<root xmlns=\"\">" + "<localName xmlns=\"http://example.org/URI-TWO\">"
 938                 + "xmlns declaration =\"http://example.org/URI-TWO\"" + "</localName" + "</root>";
 939 
 940         // reset to known state
 941         resetWriter();
 942         startDocumentEmptyDefaultNamespace(xmlStreamWriter);
 943 
 944         // writeNamespace("xmlns", ""); already done by
 945         // startDocumentEmptyDefaultNamespace above
 946         xmlStreamWriter.writeStartElement("", "localName", "http://example.org/URI-TWO");
 947         xmlStreamWriter.writeCharacters("remap xmlns declaration {generated prefix}=\"http://example.org/URI-TWO\"");
 948 
 949         actualOutput = endDocumentEmptyDefaultNamespace(xmlStreamWriter);
 950 
 951         if (DEBUG) {
 952             System.out.println("testSamePrefixDifferentURI(): expectedOutput: " + EXPECTED_OUTPUT_4);
 953             System.out.println("testSamePrefixDifferentURI():   actualOutput: " + actualOutput);
 954         }
 955 
 956         // there must be 2 xmlns=
 957         Assert.assertTrue(actualOutput.split("xmlns=").length == 3, "Expected 2 xmlns=, actual output: " + actualOutput);
 958 
 959         // there must be 0 xmlns:
 960         Assert.assertTrue(actualOutput.split("xmlns:").length == 1, "Expected 0 xmlns:, actual output: " + actualOutput);
 961 
 962         // there must be 0 :localName
 963         Assert.assertTrue(actualOutput.split(":localName").length == 1, "Expected 0 :localName, actual output: " + actualOutput);
 964     }
 965 
 966     // ---------------- Misc ----------------
 967 
 968     /**
 969      * The one case where you don't have to worry about fixup is on attributes
 970      * that do not have a prefix. Irrespective of the current namespace
 971      * bindings,
 972      *
 973      * writeAttribute("", "", "attrName", "value")
 974      *
 975      * is always correct and never requires fixup.
 976      */
 977     @Test
 978     public void testEmptyDefaultEmptyPrefixEmptyNamespaceURIWriteAttribute() throws Exception {
 979 
 980         final String EXPECTED_OUTPUT = "<?xml version=\"1.0\" ?>" + "<root xmlns=\"\" attrName=\"value\">" + "never requires fixup" + "</root>";
 981 
 982         startDocumentEmptyDefaultNamespace(xmlStreamWriter);
 983 
 984         xmlStreamWriter.writeAttribute("", "", "attrName", "value");
 985         xmlStreamWriter.writeCharacters("never requires fixup");
 986 
 987         String actualOutput = endDocumentEmptyDefaultNamespace(xmlStreamWriter);
 988 
 989         if (DEBUG) {
 990             System.out.println("testEmptyDefaultEmptyPrefixEmptyNamespaceURIWriteAttribute(): expectedOutput: " + EXPECTED_OUTPUT);
 991             System.out.println("testEmptyDefaultEmptyPrefixEmptyNamespaceURIWriteAttribute():   actualOutput: " + actualOutput);
 992         }
 993 
 994         Assert.assertEquals(EXPECTED_OUTPUT, actualOutput);
 995     }
 996 
 997     @Test
 998     public void testSpecifiedDefaultEmptyPrefixEmptyNamespaceURIWriteAttribute() throws Exception {
 999 
1000         final String EXPECTED_OUTPUT = "<?xml version=\"1.0\" ?>" + "<root xmlns=\"http://example.org/uniqueURI\" attrName=\"value\">" + "never requires fixup"
1001                 + "</root>";
1002 
1003         startDocumentSpecifiedDefaultNamespace(xmlStreamWriter);
1004 
1005         xmlStreamWriter.writeAttribute("", "", "attrName", "value");
1006         xmlStreamWriter.writeCharacters("never requires fixup");
1007 
1008         String actualOutput = endDocumentSpecifiedDefaultNamespace(xmlStreamWriter);
1009 
1010         if (DEBUG) {
1011             System.out.println("testSpecifiedDefaultEmptyPrefixEmptyNamespaceURIWriteAttribute(): expectedOutput: " + EXPECTED_OUTPUT);
1012             System.out.println("testSpecifiedDefaultEmptyPrefixEmptyNamespaceURIWriteAttribute():   actualOutput: " + actualOutput);
1013         }
1014 
1015         Assert.assertEquals(EXPECTED_OUTPUT, actualOutput);
1016     }
1017 
1018     /*--------------- Negative tests with isRepairingNamespaces as FALSE ---------------------- */
1019 
1020     private void setUpForNoRepair() {
1021 
1022         xmlOutputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.FALSE);
1023 
1024         // new Writer
1025         try {
1026             xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(byteArrayOutputStream);
1027 
1028         } catch (XMLStreamException xmlStreamException) {
1029             xmlStreamException.printStackTrace();
1030             Assert.fail(xmlStreamException.toString());
1031         }
1032     }
1033 
1034     /*
1035      * Tries to assign default namespace to empty URI and again to a different
1036      * uri in element and attribute. Expects XMLStreamException .
1037      * writeNamespace("",""); writeAttribute("", "http://example.org/myURI",
1038      * "attrName", "value");
1039      */
1040     @Test
1041     public void testEmptyDefaultEmptyPrefixSpecifiedURIWriteAttributeNoRepair() {
1042         try {
1043             setUpForNoRepair();
1044             startDocumentEmptyDefaultNamespace(xmlStreamWriter);
1045             xmlStreamWriter.writeAttribute("", "http://example.org/myURI", "attrName", "value");
1046             String actualOutput = endDocumentEmptyDefaultNamespace(xmlStreamWriter);
1047             Assert.fail("XMLStreamException is expected, actualOutput: " + actualOutput);
1048         } catch (Exception e) {
1049             System.out.println("PASS: caught an expected exception" + e.getMessage());
1050             e.printStackTrace();
1051         }
1052     }
1053 
1054     /*
1055      * Tries to assign default namespace to different uris in element and
1056      * attribute and expects XMLStreamException.
1057      * writeNamespace("","http://example.org/uniqueURI"); writeAttribute("",
1058      * "http://example.org/myURI", "attrName", "value");
1059      */
1060     @Test
1061     public void testSpecifiedDefaultEmptyPrefixSpecifiedURIWriteAttributeNoRepair() {
1062         try {
1063             setUpForNoRepair();
1064             startDocumentSpecifiedDefaultNamespace(xmlStreamWriter);
1065             xmlStreamWriter.writeAttribute("", "http://example.org/uniqueURI", "attrName", "value");
1066             String actualOutput = endDocumentEmptyDefaultNamespace(xmlStreamWriter);
1067             Assert.fail("XMLStreamException is expected, actualOutput: " + actualOutput);
1068         } catch (Exception e) {
1069             System.out.println("PASS: caught an expected exception" + e.getMessage());
1070             e.printStackTrace();
1071         }
1072     }
1073 
1074     /*
1075      * Tries to assign default namespace to same uri twice in element and
1076      * attribute and expects XMLStreamException.
1077      * writeNamespace("","http://example.org/uniqueURI"); writeAttribute("",
1078      * "http://example.org/uniqueURI", "attrName", "value");
1079      */
1080     @Test
1081     public void testSpecifiedDefaultEmptyPrefixSpecifiedDifferentURIWriteAttributeNoRepair() {
1082         try {
1083             setUpForNoRepair();
1084             startDocumentSpecifiedDefaultNamespace(xmlStreamWriter);
1085             xmlStreamWriter.writeAttribute("", "http://example.org/myURI", "attrName", "value");
1086             String actualOutput = endDocumentEmptyDefaultNamespace(xmlStreamWriter);
1087             Assert.fail("XMLStreamException is expected, actualOutput: " + actualOutput);
1088         } catch (Exception e) {
1089             System.out.println("PASS: caught an expected exception" + e.getMessage());
1090             e.printStackTrace();
1091         }
1092     }
1093 
1094     /*
1095      * Tries to assign prefix 'p' to different uris to attributes of the same
1096      * element and expects XMLStreamException. writeAttribute("p",
1097      * "http://example.org/URI-ONE", "attr1", "value"); writeAttribute("p",
1098      * "http://example.org/URI-TWO", "attr2", "value");
1099      */
1100     @Test
1101     public void testSamePrefixDiffrentURIWriteAttributeNoRepair() {
1102         try {
1103             setUpForNoRepair();
1104             startDocumentEmptyDefaultNamespace(xmlStreamWriter);
1105             xmlStreamWriter.writeAttribute("p", "http://example.org/URI-ONE", "attr1", "value");
1106             xmlStreamWriter.writeAttribute("p", "http://example.org/URI-TWO", "attr2", "value");
1107             String actualOutput = endDocumentEmptyDefaultNamespace(xmlStreamWriter);
1108             Assert.fail("XMLStreamException is expected, actualOutput: " + actualOutput);
1109         } catch (Exception e) {
1110             System.out.println("PASS: caught an expected exception" + e.getMessage());
1111             e.printStackTrace();
1112         }
1113     }
1114 
1115     /*
1116      * Tries to assign prefix 'p' to different uris in element and attribute and
1117      * expects XMLStreamException.
1118      * writeStartElement("p","localName","http://example.org/URI-ONE")
1119      * writeAttribute("p", "http://example.org/URI-TWO", "attrName", "value")
1120      */
1121     @Test
1122     public void testSamePrefixDiffrentURIWriteElemAndWriteAttributeNoRepair() {
1123         try {
1124             setUpForNoRepair();
1125             startDocumentEmptyDefaultNamespace(xmlStreamWriter);
1126             xmlStreamWriter.writeStartElement("p", "localName", "http://example.org/URI-ONE");
1127             xmlStreamWriter.writeAttribute("p", "http://example.org/URI-TWO", "attrName", "value");
1128             xmlStreamWriter.writeEndElement();
1129             String actualOutput = endDocumentEmptyDefaultNamespace(xmlStreamWriter);
1130             Assert.fail("XMLStreamException is expected, actualOutput: " + actualOutput);
1131         } catch (Exception e) {
1132             System.out.println("PASS: caught an expected exception" + e.getMessage());
1133             e.printStackTrace();
1134         }
1135     }
1136 
1137     /*
1138      * Tries to write following and expects a StreamException. <root
1139      * xmlns=""http://example.org/uniqueURI"" xmlns=""http://example.org/myURI""
1140      * />
1141      */
1142     @Test
1143     public void testDefaultNamespaceDiffrentURIWriteElementNoRepair() {
1144         try {
1145             System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
1146             setUpForNoRepair();
1147             startDocumentSpecifiedDefaultNamespace(xmlStreamWriter);
1148             xmlStreamWriter.writeNamespace("", "http://example.org/myURI");
1149             xmlStreamWriter.writeEndElement();
1150             String actualOutput = endDocumentEmptyDefaultNamespace(xmlStreamWriter);
1151             System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
1152             Assert.fail("XMLStreamException is expected, actualOutput: " + actualOutput);
1153         } catch (Exception e) {
1154             System.out.println("PASS: caught an expected exception" + e.getMessage());
1155             e.printStackTrace();
1156         }
1157     }
1158 
1159     /*--------------------------------------------------------------------------
1160      Miscelleneous tests for writeStartElement() & writeAttribute() methods
1161      in case of NOREPAIR
1162      --------------------------------------------------------------------------*/
1163 
1164     private void startDocument(XMLStreamWriter xmlStreamWriter) throws XMLStreamException {
1165         xmlStreamWriter.writeStartDocument();
1166         xmlStreamWriter.writeStartElement("root");
1167     }
1168 
1169     @Test
1170     public void testSpecifiedPrefixSpecifiedURIWriteElementNoRepair() {
1171 
1172         final String EXPECTED_OUTPUT = "<?xml version=\"1.0\" ?>" + "<root>" + "<p:localName></p:localName>" + "</root>";
1173         try {
1174             setUpForNoRepair();
1175             startDocument(xmlStreamWriter);
1176             xmlStreamWriter.writeStartElement("p", "localName", "http://example.org/myURI");
1177             xmlStreamWriter.writeEndElement();
1178             String actualOutput = endDocumentEmptyDefaultNamespace(xmlStreamWriter);
1179             System.out.println("actualOutput: " + actualOutput);
1180             Assert.assertEquals(EXPECTED_OUTPUT, actualOutput);
1181         } catch (Exception e) {
1182             e.printStackTrace();
1183             Assert.fail("Caught an unexpected exception" + e.getMessage());
1184         }
1185     }
1186 
1187     @Test
1188     public void testSpecifiedPrefixSpecifiedURIWriteAttributeNoRepair() {
1189 
1190         final String EXPECTED_OUTPUT = "<?xml version=\"1.0\" ?>" + "<root p:attrName=\"value\">" + "</root>";
1191         try {
1192             setUpForNoRepair();
1193             startDocument(xmlStreamWriter);
1194             xmlStreamWriter.writeAttribute("p", "http://example.org/myURI", "attrName", "value");
1195             xmlStreamWriter.writeEndElement();
1196             String actualOutput = endDocumentEmptyDefaultNamespace(xmlStreamWriter);
1197             System.out.println("actualOutput: " + actualOutput);
1198             Assert.assertEquals(EXPECTED_OUTPUT, actualOutput);
1199         } catch (Exception e) {
1200             e.printStackTrace();
1201             Assert.fail("Caught an unexpected exception" + e.getMessage());
1202         }
1203     }
1204 
1205     @Test
1206     public void testSpecifiedPrefixSpecifiedURISpecifiedNamespcaeWriteElementNoRepair() {
1207 
1208         final String EXPECTED_OUTPUT = "<?xml version=\"1.0\" ?>" + "<root>" + "<p:localName xmlns:p=\"http://example.org/myURI\"></p:localName>" + "</root>";
1209         try {
1210             setUpForNoRepair();
1211             startDocument(xmlStreamWriter);
1212 
1213             xmlStreamWriter.writeStartElement("p", "localName", "http://example.org/myURI");
1214             xmlStreamWriter.writeNamespace("p", "http://example.org/myURI");
1215             xmlStreamWriter.writeEndElement();
1216             String actualOutput = endDocumentEmptyDefaultNamespace(xmlStreamWriter);
1217             System.out.println("actualOutput: " + actualOutput);
1218             Assert.assertEquals(EXPECTED_OUTPUT, actualOutput);
1219         } catch (Exception e) {
1220             e.printStackTrace();
1221             Assert.fail("Caught an unexpected exception" + e.getMessage());
1222         }
1223     }
1224 
1225     /*
1226      * writeStartElement("p","localName", "http://example.org/myURI")
1227      * writeNamespace("p","http://example.org/uniqueURI") This sequence of calls
1228      * should generate an error as prefix 'p' is binded to different namespace
1229      * URIs in same namespace context and repairing is disabled.
1230      */
1231 
1232     @Test
1233     public void testSpecifiedPrefixSpecifiedURISpecifiedDifferentNamespcaeWriteElementNoRepair() {
1234 
1235         try {
1236             setUpForNoRepair();
1237             startDocument(xmlStreamWriter);
1238             xmlStreamWriter.writeStartElement("p", "localName", "http://example.org/myURI");
1239             xmlStreamWriter.writeNamespace("p", "http://example.org/uniqueURI");
1240             xmlStreamWriter.writeEndElement();
1241             String actualOutput = endDocumentEmptyDefaultNamespace(xmlStreamWriter);
1242             System.out.println("actualOutput: " + actualOutput);
1243             Assert.fail("XMLStreamException is expected as 'p' is rebinded to a different URI in same namespace context");
1244         } catch (Exception e) {
1245             System.out.println("Caught an expected exception" + e.getMessage());
1246         }
1247     }
1248 
1249     @Test
1250     public void testEmptyPrefixEmptyURIWriteAttributeNoRepair() {
1251         final String EXPECTED_OUTPUT = "<?xml version=\"1.0\" ?>" + "<root>" + "<localName attrName=\"value\"></localName>" + "</root>";
1252         try {
1253             setUpForNoRepair();
1254             startDocument(xmlStreamWriter);
1255             xmlStreamWriter.writeStartElement("localName");
1256             xmlStreamWriter.writeAttribute("", "", "attrName", "value");
1257             xmlStreamWriter.writeEndElement();
1258             String actualOutput = endDocumentEmptyDefaultNamespace(xmlStreamWriter);
1259             System.out.println("actualOutput: " + actualOutput);
1260             Assert.assertEquals(EXPECTED_OUTPUT, actualOutput);
1261         } catch (Exception e) {
1262             e.printStackTrace();
1263             Assert.fail("Caught an unexpected exception" + e.getMessage());
1264         }
1265     }
1266 
1267     @Test
1268     public void testEmptyPrefixNullURIWriteAttributeNoRepair() {
1269         final String EXPECTED_OUTPUT = "<?xml version=\"1.0\" ?>" + "<root>" + "<localName attrName=\"value\"></localName>" + "</root>";
1270         try {
1271             setUpForNoRepair();
1272             startDocument(xmlStreamWriter);
1273             xmlStreamWriter.writeStartElement("localName");
1274             xmlStreamWriter.writeAttribute(null, null, "attrName", "value");
1275             xmlStreamWriter.writeEndElement();
1276             String actualOutput = endDocumentEmptyDefaultNamespace(xmlStreamWriter);
1277             System.out.println("actualOutput: " + actualOutput);
1278             Assert.fail("XMLStreamException is expected, actualOutput: " + actualOutput);
1279         } catch (Exception e) {
1280             System.out.println("PASS: caught an expected exception" + e.getMessage());
1281             e.printStackTrace();
1282         }
1283     }
1284 
1285     @Test
1286     public void testDoubleXmlNsNoRepair() {
1287         try {
1288             // reset to known state
1289             setUpForNoRepair();
1290 
1291             xmlStreamWriter.writeStartDocument();
1292             xmlStreamWriter.writeStartElement("foo");
1293             xmlStreamWriter.writeNamespace("xml", XMLConstants.XML_NS_URI);
1294             xmlStreamWriter.writeAttribute("xml", XMLConstants.XML_NS_URI, "lang", "ja_JP");
1295             xmlStreamWriter.writeCharacters("Hello");
1296             xmlStreamWriter.writeEndElement();
1297             xmlStreamWriter.writeEndDocument();
1298 
1299             xmlStreamWriter.flush();
1300             String actualOutput = byteArrayOutputStream.toString();
1301 
1302             if (DEBUG) {
1303                 System.out.println("testDoubleXmlNsNoRepair(): actualOutput: " + actualOutput);
1304             }
1305 
1306             // there should be no xmlns:xml
1307             Assert.assertTrue(actualOutput.split("xmlns:xml").length == 1, "Expected 0 xmlns:xml, actual output: " + actualOutput);
1308         } catch (Exception e) {
1309             e.printStackTrace();
1310             Assert.fail(e.getMessage());
1311         }
1312     }
1313 
1314     @Test
1315     public void testSpecifiedURIWriteAttributeNoRepair() {
1316         final String EXPECTED_OUTPUT = "<?xml version=\"1.0\" ?>" + "<root>" + "<p:localName p:attrName=\"value\"></p:localName>" + "</root>";
1317         try {
1318             setUpForNoRepair();
1319             startDocument(xmlStreamWriter);
1320             xmlStreamWriter.writeStartElement("p", "localName", "http://example.org/myURI");
1321             xmlStreamWriter.writeAttribute("http://example.org/myURI", "attrName", "value");
1322             xmlStreamWriter.writeEndElement();
1323             String actualOutput = endDocumentEmptyDefaultNamespace(xmlStreamWriter);
1324             System.out.println("actualOutput: " + actualOutput);
1325             Assert.assertEquals(EXPECTED_OUTPUT, actualOutput);
1326         } catch (Exception e) {
1327             System.out.println("Caught an expected exception" + e.getMessage());
1328         }
1329     }
1330 
1331     @Test
1332     public void testSpecifiedURIWriteAttributeWithRepair() {
1333         final String EXPECTED_OUTPUT = "<?xml version=\"1.0\" ?>" + "<root>"
1334                 + "<p:localName xmlns:p=\"http://example.org/myURI\" p:attrName=\"value\"></p:localName>" + "</root>";
1335         try {
1336             startDocument(xmlStreamWriter);
1337             xmlStreamWriter.writeStartElement("p", "localName", "http://example.org/myURI");
1338             xmlStreamWriter.writeNamespace("p", "http://example.org/myURI");
1339             xmlStreamWriter.writeAttribute("http://example.org/myURI", "attrName", "value");
1340             xmlStreamWriter.writeEndElement();
1341             String actualOutput = endDocumentEmptyDefaultNamespace(xmlStreamWriter);
1342             System.out.println("actualOutput: " + actualOutput);
1343             Assert.assertEquals(EXPECTED_OUTPUT, actualOutput);
1344         } catch (Exception e) {
1345             e.printStackTrace();
1346             Assert.fail("Exception occured: " + e.getMessage());
1347         }
1348     }
1349 
1350     @Test
1351     public void testSpecifiedDefaultInDifferentElementsNoRepair() {
1352         final String EXPECTED_OUTPUT = "<?xml version=\"1.0\" ?>" + "<root>" + "<localName xmlns=\"http://example.org/myURI\">"
1353                 + "<child xmlns=\"http://example.org/uniqueURI\"></child>" + "</localName>" + "</root>";
1354         try {
1355             setUpForNoRepair();
1356             startDocument(xmlStreamWriter);
1357             xmlStreamWriter.writeStartElement("localName");
1358             xmlStreamWriter.writeDefaultNamespace("http://example.org/myURI");
1359             xmlStreamWriter.writeStartElement("child");
1360             xmlStreamWriter.writeDefaultNamespace("http://example.org/uniqueURI");
1361             xmlStreamWriter.writeEndElement();
1362             xmlStreamWriter.writeEndElement();
1363             String actualOutput = endDocumentEmptyDefaultNamespace(xmlStreamWriter);
1364             System.out.println("actualOutput: " + actualOutput);
1365             Assert.assertEquals(EXPECTED_OUTPUT, actualOutput);
1366         } catch (Exception e) {
1367             e.printStackTrace();
1368             Assert.fail("Exception occured: " + e.getMessage());
1369         }
1370     }
1371 
1372     /*------------- Tests for setPrefix() and setDefaultNamespace() methods --------------------*/
1373 
1374     @Test
1375     public void testSetPrefixWriteNamespaceNoRepair() {
1376         final String EXPECTED_OUTPUT = "<?xml version=\"1.0\" ?>" + "<root xmlns:p=\"http://example.org/myURI\">" + "</root>";
1377         try {
1378             setUpForNoRepair();
1379             startDocument(xmlStreamWriter);
1380             xmlStreamWriter.setPrefix("p", "http://example.org/myURI");
1381             xmlStreamWriter.writeNamespace("p", "http://example.org/myURI");
1382             xmlStreamWriter.writeEndElement();
1383             String actualOutput = endDocumentEmptyDefaultNamespace(xmlStreamWriter);
1384             System.out.println("actualOutput: " + actualOutput);
1385             Assert.assertEquals(EXPECTED_OUTPUT, actualOutput);
1386         } catch (Exception e) {
1387             System.out.println("Caught an expected exception" + e.getMessage());
1388         }
1389     }
1390 
1391     @Test
1392     public void testSetPrefixWriteNamespaceWithRepair() {
1393         final String EXPECTED_OUTPUT = "<?xml version=\"1.0\" ?>" + "<root xmlns:p=\"http://example.org/myURI\">" + "</root>";
1394         try {
1395             startDocument(xmlStreamWriter);
1396             xmlStreamWriter.setPrefix("p", "http://example.org/myURI");
1397             xmlStreamWriter.writeNamespace("p", "http://example.org/myURI");
1398             xmlStreamWriter.writeEndElement();
1399             String actualOutput = endDocumentEmptyDefaultNamespace(xmlStreamWriter);
1400             System.out.println("actualOutput: " + actualOutput);
1401             Assert.assertEquals(EXPECTED_OUTPUT, actualOutput);
1402         } catch (Exception e) {
1403             System.out.println("Caught an expected exception" + e.getMessage());
1404         }
1405     }
1406 
1407     @Test
1408     public void testSetDefaultNamespaceWriteNamespaceNoRepair() {
1409         final String EXPECTED_OUTPUT = "<?xml version=\"1.0\" ?>" + "<root xmlns=\"http://example.org/myURI\">" + "</root>";
1410         try {
1411             setUpForNoRepair();
1412             startDocument(xmlStreamWriter);
1413             xmlStreamWriter.setDefaultNamespace("http://example.org/myURI");
1414             xmlStreamWriter.writeNamespace("", "http://example.org/myURI");
1415             xmlStreamWriter.writeEndElement();
1416             String actualOutput = endDocumentEmptyDefaultNamespace(xmlStreamWriter);
1417             System.out.println("actualOutput: " + actualOutput);
1418             Assert.assertEquals(EXPECTED_OUTPUT, actualOutput);
1419         } catch (Exception e) {
1420             System.out.println("Caught an expected exception" + e.getMessage());
1421         }
1422     }
1423 
1424     @Test
1425     public void testSetDefaultNamespaceWriteNamespaceWithRepair() {
1426         final String EXPECTED_OUTPUT = "<?xml version=\"1.0\" ?>" + "<root xmlns=\"http://example.org/myURI\">" + "</root>";
1427         try {
1428             startDocument(xmlStreamWriter);
1429             xmlStreamWriter.setDefaultNamespace("http://example.org/myURI");
1430             xmlStreamWriter.writeNamespace("", "http://example.org/myURI");
1431             xmlStreamWriter.writeEndElement();
1432             String actualOutput = endDocumentEmptyDefaultNamespace(xmlStreamWriter);
1433             System.out.println("actualOutput: " + actualOutput);
1434             Assert.assertEquals(EXPECTED_OUTPUT, actualOutput);
1435         } catch (Exception e) {
1436             System.out.println("Caught an expected exception" + e.getMessage());
1437         }
1438     }
1439 }