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