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