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