1 /* 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 package stream.XMLStreamWriterTest; 25 26 import java.io.FileInputStream; 27 import java.io.FileOutputStream; 28 import java.io.IOException; 29 import java.io.InputStreamReader; 30 import java.io.LineNumberReader; 31 import java.io.Reader; 32 import java.net.URL; 33 import java.util.Iterator; 34 35 import javax.xml.namespace.NamespaceContext; 36 import javax.xml.stream.XMLInputFactory; 37 import javax.xml.stream.XMLOutputFactory; 38 import javax.xml.stream.XMLStreamWriter; 39 40 import org.testng.Assert; 41 import org.testng.annotations.AfterMethod; 42 import org.testng.annotations.BeforeMethod; 43 import org.testng.annotations.Test; 44 45 /* 46 * @summary Test XMLStreamWriter functionality. 47 */ 48 public class WriterTest { 49 50 final String ENCODING = "UTF-8"; 51 XMLOutputFactory outputFactory = null; 52 XMLInputFactory inputFactory = null; 53 XMLStreamWriter xtw = null; 54 String[] files = new String[] { "testOne.xml", "testTwo.xml", "testThree.xml", "testFour.xml", "testFive.xml", "testSix.xml", "testSeven.xml", 55 "testEight.xml", "testNine.xml", "testTen.xml", "testEleven.xml", "testTwelve.xml", "testDefaultNS.xml", null, "testFixAttr.xml" }; 56 57 String output = ""; 58 59 @BeforeMethod 60 protected void setUp() { 61 try { 62 outputFactory = XMLOutputFactory.newInstance(); 63 inputFactory = XMLInputFactory.newInstance(); 64 } catch (Exception ex) { 65 Assert.fail("Could not create XMLInputFactory"); 66 } 67 } 68 69 @AfterMethod 70 protected void tearDown() { 71 outputFactory = null; 72 inputFactory = null; 73 } 74 75 @Test 76 public void testOne() { 77 78 System.out.println("Test StreamWriter with out any namespace functionality"); 79 80 try { 81 String outputFile = files[0] + ".out"; 82 System.out.println("Writing output to " + outputFile); 83 84 xtw = outputFactory.createXMLStreamWriter(new FileOutputStream(outputFile), ENCODING); 85 xtw.writeStartDocument("utf-8", "1.0"); 86 xtw.writeStartElement("elmeOne"); 87 xtw.writeStartElement("elemTwo"); 88 xtw.writeStartElement("elemThree"); 89 xtw.writeStartElement("elemFour"); 90 xtw.writeStartElement("elemFive"); 91 xtw.writeEndDocument(); 92 xtw.flush(); 93 xtw.close(); 94 95 Assert.assertTrue(checkResults(files[0] + ".out", files[0] + ".org")); 96 97 } catch (Exception ex) { 98 Assert.fail("testOne Failed " + ex); 99 ex.printStackTrace(); 100 } 101 102 } 103 104 @Test 105 public void testTwo() { 106 107 System.out.println("Test StreamWriter's Namespace Context"); 108 109 try { 110 String outputFile = files[1] + ".out"; 111 System.out.println("Writing output to " + outputFile); 112 113 xtw = outputFactory.createXMLStreamWriter(System.out); 114 xtw.writeStartDocument(); 115 xtw.writeStartElement("elemTwo"); 116 xtw.setPrefix("html", "http://www.w3.org/TR/REC-html40"); 117 xtw.writeNamespace("html", "http://www.w3.org/TR/REC-html40"); 118 xtw.writeEndDocument(); 119 NamespaceContext nc = xtw.getNamespaceContext(); 120 // Got a Namespace Context.class 121 122 XMLStreamWriter xtw1 = outputFactory.createXMLStreamWriter(new FileOutputStream(outputFile), ENCODING); 123 124 xtw1.writeComment("all elements here are explicitly in the HTML namespace"); 125 xtw1.setNamespaceContext(nc); 126 xtw1.writeStartDocument("utf-8", "1.0"); 127 xtw1.setPrefix("htmlOne", "http://www.w3.org/TR/REC-html40"); 128 NamespaceContext nc1 = xtw1.getNamespaceContext(); 129 xtw1.close(); 130 Iterator it = nc1.getPrefixes("http://www.w3.org/TR/REC-html40"); 131 132 // FileWriter fw = new FileWriter(outputFile); 133 while (it.hasNext()) { 134 System.out.println("Prefixes :" + it.next()); 135 // fw.write((String)it.next()); 136 // fw.write(";"); 137 } 138 // fw.close(); 139 // assertTrue(checkResults(testTwo+".out", testTwo+".org")); 140 System.out.println("Done"); 141 } catch (Exception ex) { 142 Assert.fail("testTwo Failed " + ex); 143 ex.printStackTrace(); 144 } 145 146 } 147 148 @Test 149 public void testThree() { 150 151 System.out.println("Test StreamWriter for proper element sequence."); 152 153 try { 154 String outputFile = files[2] + ".out"; 155 System.out.println("Writing output to " + outputFile); 156 157 xtw = outputFactory.createXMLStreamWriter(new FileOutputStream(outputFile), ENCODING); 158 xtw.writeStartDocument("utf-8", "1.0"); 159 xtw.writeStartElement("elmeOne"); 160 xtw.writeStartElement("elemTwo"); 161 xtw.writeEmptyElement("emptyElem"); 162 xtw.writeStartElement("elemThree"); 163 xtw.writeStartElement("elemFour"); 164 xtw.writeStartElement("elemFive"); 165 xtw.writeEndDocument(); 166 xtw.flush(); 167 xtw.close(); 168 169 Assert.assertTrue(checkResults(files[2] + ".out", files[2] + ".org")); 170 171 } catch (Exception ex) { 172 Assert.fail("testThree Failed " + ex); 173 ex.printStackTrace(); 174 } 175 176 } 177 178 @Test 179 public void testFour() { 180 181 System.out.println("Test StreamWriter with elements,attribute and element content."); 182 183 try { 184 185 String outputFile = files[3] + ".out"; 186 System.out.println("Writing output to " + outputFile); 187 188 xtw = outputFactory.createXMLStreamWriter(new FileOutputStream(outputFile), ENCODING); 189 xtw.writeStartDocument("utf-8", "1.0"); 190 xtw.writeStartElement("elmeOne"); 191 xtw.writeStartElement("elemTwo"); 192 xtw.writeEmptyElement("emptyElem"); 193 xtw.writeAttribute("testAttr", "testValue"); 194 xtw.writeStartElement("elemThree"); 195 xtw.writeStartElement("elemFour"); 196 xtw.writeCharacters("TestCharacterData"); 197 xtw.writeStartElement("elemFive"); 198 xtw.writeEndDocument(); 199 xtw.flush(); 200 xtw.close(); 201 202 Assert.assertTrue(checkResults(files[3] + ".out", files[3] + ".org")); 203 204 } catch (Exception ex) { 205 Assert.fail("testFour Failed " + ex); 206 ex.printStackTrace(); 207 } 208 209 } 210 211 @Test 212 public void testFive() { 213 214 System.out.println("Test StreamWriter's Namespace Context."); 215 216 try { 217 218 String outputFile = files[4] + ".out"; 219 System.out.println("Writing output to " + outputFile); 220 221 xtw = outputFactory.createXMLStreamWriter(System.out); 222 xtw.writeStartDocument(); 223 xtw.writeStartElement("elemTwo"); 224 xtw.setPrefix("html", "http://www.w3.org/TR/REC-html40"); 225 xtw.writeNamespace("html", "http://www.w3.org/TR/REC-html40"); 226 // xtw.writeEndDocument(); 227 NamespaceContext nc = xtw.getNamespaceContext(); 228 // Got a Namespace Context.class 229 230 xtw = outputFactory.createXMLStreamWriter(new FileOutputStream(outputFile), ENCODING); 231 232 xtw.writeComment("all elements here are explicitly in the HTML namespace"); 233 xtw.setNamespaceContext(nc); 234 xtw.writeStartDocument("utf-8", "1.0"); 235 // xtw.setPrefix("html", "http://www.w3.org/TR/REC-html40"); 236 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "html"); 237 // xtw.writeNamespace("html", "http://www.w3.org/TR/REC-html40"); 238 239 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "head"); 240 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "title"); 241 242 xtw.writeCharacters("Frobnostication"); 243 xtw.writeEndElement(); 244 xtw.writeEndElement(); 245 246 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "body"); 247 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "p"); 248 xtw.writeCharacters("Moved to"); 249 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "a"); 250 xtw.writeAttribute("href", "http://frob.com"); 251 252 xtw.writeCharacters("here"); 253 xtw.writeEndElement(); 254 xtw.writeEndElement(); 255 xtw.writeEndElement(); 256 257 xtw.writeEndElement(); 258 259 xtw.writeEndDocument(); 260 xtw.flush(); 261 xtw.close(); 262 Assert.assertTrue(checkResults(files[4] + ".out", files[4] + ".org")); 263 System.out.println("Done"); 264 } catch (Exception ex) { 265 Assert.fail("testFive Failed " + ex); 266 ex.printStackTrace(); 267 } 268 269 } 270 271 @Test 272 public void testSix() { 273 274 System.out.println("Test StreamWriter, uses the Namespace Context set by the user to resolve namespaces."); 275 276 try { 277 278 String outputFile = files[5] + ".out"; 279 System.out.println("Writing output to " + outputFile); 280 281 xtw = outputFactory.createXMLStreamWriter(System.out); 282 xtw.writeStartDocument(); 283 xtw.writeStartElement("elemTwo"); 284 xtw.setPrefix("html", "http://www.w3.org/TR/REC-html40"); 285 xtw.writeNamespace("html", "http://www.w3.org/TR/REC-html40"); 286 xtw.writeEndDocument(); 287 NamespaceContext nc = xtw.getNamespaceContext(); 288 // Got a Namespace Context information. 289 290 xtw = outputFactory.createXMLStreamWriter(new FileOutputStream(outputFile), ENCODING); 291 292 xtw.writeComment("all elements here are explicitly in the HTML namespace"); 293 xtw.setNamespaceContext(nc); 294 xtw.writeStartDocument("utf-8", "1.0"); 295 xtw.setPrefix("htmlNewPrefix", "http://www.w3.org/TR/REC-html40"); 296 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "html"); 297 // xtw.writeNamespace("html", "http://www.w3.org/TR/REC-html40"); 298 299 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "head"); 300 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "title"); 301 302 xtw.writeCharacters("Frobnostication"); 303 xtw.writeEndElement(); 304 xtw.writeEndElement(); 305 306 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "body"); 307 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "p"); 308 xtw.writeCharacters("Moved to"); 309 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "a"); 310 xtw.writeAttribute("href", "http://frob.com"); 311 312 xtw.writeCharacters("here"); 313 xtw.writeEndElement(); 314 xtw.writeEndElement(); 315 xtw.writeEndElement(); 316 317 xtw.writeEndElement(); 318 319 xtw.writeEndDocument(); 320 xtw.flush(); 321 xtw.close(); 322 Assert.assertTrue(checkResults(files[5] + ".out", files[5] + ".org")); 323 System.out.println("Done"); 324 } catch (Exception ex) { 325 Assert.fail("testSix Failed " + ex); 326 ex.printStackTrace(); 327 } 328 329 } 330 331 @Test 332 public void testSeven() { 333 334 System.out.println("Test StreamWriter supplied with correct namespace information"); 335 336 try { 337 338 String outputFile = files[6] + ".out"; 339 System.out.println("Writing output to " + outputFile); 340 341 xtw = outputFactory.createXMLStreamWriter(new FileOutputStream(outputFile), ENCODING); 342 xtw.writeComment("all elements here are explicitly in the HTML namespace"); 343 xtw.writeStartDocument("utf-8", "1.0"); 344 xtw.setPrefix("html", "http://www.w3.org/TR/REC-html40"); 345 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "html"); 346 xtw.writeNamespace("html", "http://www.w3.org/TR/REC-html40"); 347 348 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "head"); 349 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "title"); 350 351 xtw.writeCharacters("Frobnostication"); 352 xtw.writeEndElement(); 353 xtw.writeEndElement(); 354 355 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "body"); 356 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "p"); 357 xtw.writeCharacters("Moved to"); 358 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "a"); 359 xtw.writeAttribute("href", "http://frob.com"); 360 361 xtw.writeCharacters("here"); 362 xtw.writeEndElement(); 363 xtw.writeEndElement(); 364 xtw.writeEndElement(); 365 366 xtw.writeEndElement(); 367 368 xtw.writeEndDocument(); 369 xtw.flush(); 370 xtw.close(); 371 Assert.assertTrue(checkResults(files[6] + ".out", files[6] + ".org")); 372 System.out.println("Done"); 373 } catch (Exception ex) { 374 Assert.fail("testSeven Failed " + ex); 375 ex.printStackTrace(); 376 } 377 378 } 379 380 @Test 381 public void testEight() { 382 383 System.out.println("Test StreamWriter supplied with correct namespace information and" + "isRepairingNamespace is set to true."); 384 385 try { 386 387 String outputFile = files[7] + ".out"; 388 System.out.println("Writing output to " + outputFile); 389 outputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, new Boolean(true)); 390 xtw = outputFactory.createXMLStreamWriter(new FileOutputStream(outputFile), ENCODING); 391 xtw.writeComment("all elements here are explicitly in the HTML namespace"); 392 xtw.writeStartDocument("utf-8", "1.0"); 393 xtw.setPrefix("html", "http://www.w3.org/TR/REC-html40"); 394 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "html"); 395 xtw.writeNamespace("html", "http://www.w3.org/TR/REC-html40"); 396 397 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "head"); 398 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "title"); 399 400 xtw.writeCharacters("Frobnostication"); 401 xtw.writeEndElement(); 402 xtw.writeEndElement(); 403 404 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "body"); 405 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "p"); 406 xtw.writeCharacters("Moved to"); 407 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "a"); 408 xtw.writeAttribute("href", "http://frob.com"); 409 410 xtw.writeCharacters("here"); 411 xtw.writeEndElement(); 412 xtw.writeEndElement(); 413 xtw.writeEndElement(); 414 415 xtw.writeEndElement(); 416 417 xtw.writeEndDocument(); 418 xtw.flush(); 419 xtw.close(); 420 // check against testSeven.xml.org 421 Assert.assertTrue(checkResults(files[7] + ".out", files[7] + ".org")); 422 System.out.println("Done"); 423 } catch (Exception ex) { 424 ex.printStackTrace(); 425 Assert.fail("testEight Failed " + ex); 426 427 } 428 429 } 430 431 @Test 432 public void testNine() { 433 434 System.out.println("Test StreamWriter supplied with correct namespace information and" + "isRepairingNamespace is set to true." 435 + "pass namespace information using" + "writenamespace function"); 436 437 try { 438 439 String outputFile = files[8] + ".out"; 440 System.out.println("Writing output to " + outputFile); 441 outputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, new Boolean(true)); 442 xtw = outputFactory.createXMLStreamWriter(new FileOutputStream(outputFile), ENCODING); 443 xtw.writeComment("all elements here are explicitly in the HTML namespace"); 444 xtw.writeStartDocument("utf-8", "1.0"); 445 // xtw.setPrefix("html", "http://www.w3.org/TR/REC-html40"); 446 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "html"); 447 xtw.writeNamespace("html", "http://www.w3.org/TR/REC-html40"); 448 449 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "head"); 450 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "title"); 451 452 xtw.writeCharacters("Frobnostication"); 453 xtw.writeEndElement(); 454 xtw.writeEndElement(); 455 456 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "body"); 457 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "p"); 458 xtw.writeCharacters("Moved to"); 459 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "a"); 460 xtw.writeAttribute("href", "http://frob.com"); 461 462 xtw.writeCharacters("here"); 463 xtw.writeEndElement(); 464 xtw.writeEndElement(); 465 xtw.writeEndElement(); 466 467 xtw.writeEndElement(); 468 469 xtw.writeEndDocument(); 470 xtw.flush(); 471 xtw.close(); 472 // check against testSeven.xml.org 473 Assert.assertTrue(checkResults(files[8] + ".out", files[7] + ".org")); 474 System.out.println("Done"); 475 } catch (Exception ex) { 476 Assert.fail("testNine Failed " + ex); 477 ex.printStackTrace(); 478 } 479 480 } 481 482 @Test 483 public void testTen() { 484 485 System.out.println("Test StreamWriter supplied with no namespace information and" + "isRepairingNamespace is set to true."); 486 try { 487 488 String outputFile = files[9] + ".out"; 489 System.out.println("Writing output to " + outputFile); 490 outputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, new Boolean(true)); 491 xtw = outputFactory.createXMLStreamWriter(new FileOutputStream(outputFile), ENCODING); 492 xtw.writeComment("all elements here are explicitly in the HTML namespace"); 493 xtw.writeStartDocument("utf-8", "1.0"); 494 // xtw.setPrefix("html", "http://www.w3.org/TR/REC-html40"); 495 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "html"); 496 // xtw.writeNamespace("html", "http://www.w3.org/TR/REC-html40"); 497 498 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "head"); 499 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "title"); 500 501 xtw.writeCharacters("Frobnostication"); 502 xtw.writeEndElement(); 503 xtw.writeEndElement(); 504 505 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "body"); 506 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "p"); 507 xtw.writeCharacters("Moved to"); 508 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "a"); 509 xtw.writeAttribute("href", "http://frob.com"); 510 511 xtw.writeCharacters("here"); 512 xtw.writeEndElement(); 513 xtw.writeEndElement(); 514 xtw.writeEndElement(); 515 516 xtw.writeEndElement(); 517 518 xtw.writeEndDocument(); 519 xtw.flush(); 520 xtw.close(); 521 // check against testSeven.xml.org 522 // prefix is generated while it was defined in the 'org' file, the 523 // following comparison method needs a rewrite. 524 // assertTrue(checkResults(files[9]+".out",files[7]+".org")); 525 System.out.println("Done"); 526 } catch (Exception ex) { 527 Assert.fail("testTen Failed " + ex); 528 ex.printStackTrace(); 529 } 530 531 } 532 533 @Test 534 public void testEleven() { 535 536 System.out.println("Test StreamWriter supplied with namespace information passed through startElement and" + "isRepairingNamespace is set to true."); 537 try { 538 539 String outputFile = files[10] + ".out"; 540 System.out.println("Writing output to " + outputFile); 541 outputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, new Boolean(true)); 542 xtw = outputFactory.createXMLStreamWriter(new FileOutputStream(outputFile), ENCODING); 543 xtw.writeComment("all elements here are explicitly in the HTML namespace"); 544 xtw.writeStartDocument("utf-8", "1.0"); 545 // xtw.setPrefix("html", "http://www.w3.org/TR/REC-html40"); 546 xtw.writeStartElement("html", "html", "http://www.w3.org/TR/REC-html40"); 547 // xtw.writeNamespace("html", "http://www.w3.org/TR/REC-html40"); 548 549 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "head"); 550 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "title"); 551 552 xtw.writeCharacters("Frobnostication"); 553 xtw.writeEndElement(); 554 xtw.writeEndElement(); 555 556 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "body"); 557 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "p"); 558 xtw.writeCharacters("Moved to"); 559 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "a"); 560 xtw.writeAttribute("href", "http://frob.com"); 561 562 xtw.writeCharacters("here"); 563 xtw.writeEndElement(); 564 xtw.writeEndElement(); 565 xtw.writeEndElement(); 566 567 xtw.writeEndElement(); 568 569 xtw.writeEndDocument(); 570 xtw.flush(); 571 xtw.close(); 572 // check against testSeven.xml.org 573 Assert.assertTrue(checkResults(files[10] + ".out", files[7] + ".org")); 574 System.out.println("Done"); 575 } catch (Exception ex) { 576 Assert.fail("testEleven Failed " + ex); 577 ex.printStackTrace(); 578 } 579 580 } 581 582 @Test 583 public void testTwelve() { 584 585 System.out.println("Test StreamWriter supplied with namespace information set at few places"); 586 587 try { 588 589 String outputFile = files[11] + ".out"; 590 System.out.println("Writing output to " + outputFile); 591 outputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, new Boolean(true)); 592 xtw = outputFactory.createXMLStreamWriter(new FileOutputStream(outputFile), ENCODING); 593 xtw.writeComment("all elements here are explicitly in the HTML namespace"); 594 xtw.writeStartDocument("utf-8", "1.0"); 595 596 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "html"); 597 // xtw.writeNamespace("html", "http://www.w3.org/TR/REC-html40"); 598 599 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "head"); 600 xtw.setPrefix("html", "http://www.w3.org/TR/REC-html40"); 601 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "title"); 602 603 xtw.writeCharacters("Frobnostication"); 604 xtw.writeEndElement(); 605 xtw.writeEndElement(); 606 607 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "body"); 608 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "p"); 609 xtw.writeCharacters("Moved to"); 610 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "a"); 611 xtw.writeAttribute("href", "http://frob.com"); 612 613 xtw.writeCharacters("here"); 614 xtw.writeEndElement(); 615 xtw.writeEndElement(); 616 xtw.writeEndElement(); 617 618 xtw.writeEndElement(); 619 620 xtw.writeEndDocument(); 621 xtw.flush(); 622 xtw.close(); 623 // check against testSeven.xml.org 624 // assertTrue(checkResults(files[10]+".out",files[7]+".org")); 625 System.out.println("Done"); 626 } catch (Exception ex) { 627 Assert.fail("testtwelve Failed " + ex); 628 ex.printStackTrace(); 629 } 630 631 } 632 633 @Test 634 public void testDefaultNamespace() { 635 636 System.out.println("Test StreamWriter supplied with namespace information set at few places"); 637 638 try { 639 640 String outputFile = files[12] + ".out"; 641 System.out.println("Writing output to " + outputFile); 642 outputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, new Boolean(true)); 643 xtw = outputFactory.createXMLStreamWriter(new FileOutputStream(outputFile), ENCODING); 644 xtw.writeComment("all elements here are explicitly in the HTML namespace"); 645 xtw.writeStartDocument("utf-8", "1.0"); 646 647 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "html"); 648 xtw.writeDefaultNamespace("http://www.w3.org/TR/REC-html40"); 649 650 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "head"); 651 // xtw.setPrefix("html", "http://www.w3.org/TR/REC-html40"); 652 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "title"); 653 654 xtw.writeCharacters("Frobnostication"); 655 xtw.writeEndElement(); 656 xtw.writeEndElement(); 657 658 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "body"); 659 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "p"); 660 xtw.writeCharacters("Moved to"); 661 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "a"); 662 xtw.writeAttribute("href", "http://frob.com"); 663 664 xtw.writeCharacters("here"); 665 xtw.writeEndElement(); 666 xtw.writeEndElement(); 667 xtw.writeEndElement(); 668 669 xtw.writeEndElement(); 670 671 xtw.writeEndDocument(); 672 xtw.flush(); 673 xtw.close(); 674 // check against testSeven.xml.org 675 // assertTrue(checkResults(files[10]+".out",files[7]+".org")); 676 System.out.println("Done"); 677 } catch (Exception ex) { 678 ex.printStackTrace(); 679 Assert.fail("testDefaultNamespace Failed " + ex); 680 681 } 682 683 } 684 685 @Test 686 public void testRepairNamespace() { 687 688 System.out.println("Test StreamWriter supplied with namespace information set at few places"); 689 690 try { 691 692 String outputFile = files[14] + ".out"; 693 System.out.println("Writing output to " + outputFile); 694 outputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, new Boolean(true)); 695 xtw = outputFactory.createXMLStreamWriter(new FileOutputStream(outputFile), ENCODING); 696 xtw.writeComment("all elements here are explicitly in the HTML namespace"); 697 xtw.writeStartDocument("utf-8", "1.0"); 698 xtw.writeStartElement("html", "html", "http://www.w3.org/TR/REC-html40"); 699 // xtw.writeStartElement("http://www.w3.org/TR/REC-html40","html"); 700 // xtw.writeDefaultNamespace("http://www.w3.org/TR/REC-html40"); 701 xtw.writeAttribute("html", "testPrefix", "attr1", "http://frob.com"); 702 xtw.writeAttribute("html", "testPrefix", "attr2", "http://frob2.com"); 703 xtw.writeAttribute("html", "http://www.w3.org/TR/REC-html40", "attr4", "http://frob4.com"); 704 705 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "head"); 706 xtw.setPrefix("html", "http://www.w3.org/TR/REC-html40"); 707 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "title"); 708 709 xtw.writeCharacters("Frobnostication"); 710 xtw.writeEndElement(); 711 xtw.writeEndElement(); 712 713 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "body"); 714 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "p"); 715 xtw.writeCharacters("Moved to"); 716 xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "a"); 717 xtw.writeAttribute("href", "http://frob.com"); 718 719 xtw.writeCharacters("here"); 720 xtw.writeEndElement(); 721 xtw.writeEndElement(); 722 xtw.writeEndElement(); 723 724 xtw.writeEndElement(); 725 726 xtw.writeEndDocument(); 727 xtw.flush(); 728 xtw.close(); 729 // check against testSeven.xml.org 730 // assertTrue(checkResults(files[10]+".out",files[7]+".org")); 731 System.out.println("Done"); 732 } catch (Exception ex) { 733 ex.printStackTrace(); 734 Assert.fail("testDefaultNamespace Failed " + ex); 735 736 } 737 738 } 739 740 protected boolean checkResults(String checkFile, String orgFile) { 741 try { 742 URL fileName = WriterTest.class.getResource(orgFile); 743 // URL outputFileName = WriterTest.class.getResource(checkFile); 744 return compareOutput(new InputStreamReader(fileName.openStream()), new InputStreamReader(new FileInputStream(checkFile))); 745 746 } catch (Exception ex) { 747 ex.printStackTrace(); 748 Assert.fail(ex.getMessage()); 749 } 750 return false; 751 } 752 753 protected boolean compareOutput(Reader expected, Reader actual) throws IOException { 754 LineNumberReader expectedOutput = null; 755 LineNumberReader actualOutput = null; 756 try { 757 expectedOutput = new LineNumberReader(expected); 758 actualOutput = new LineNumberReader(actual); 759 760 while (expectedOutput.ready() && actualOutput.ready()) { 761 String expectedLine = expectedOutput.readLine(); 762 String actualLine = actualOutput.readLine(); 763 if (!expectedLine.equals(actualLine)) { 764 System.out.println("Entityreference expansion failed, line no: " + expectedOutput.getLineNumber()); 765 System.out.println("Expected: " + expectedLine); 766 System.out.println("Actual : " + actualLine); 767 return false; 768 } 769 } 770 return true; 771 } catch (IOException ex) { 772 System.err.println("Error occured while comparing results."); 773 throw ex; 774 } finally { 775 expectedOutput.close(); 776 actualOutput.close(); 777 778 } 779 } 780 }