1 20 package org.apache.cactus.integration.ant.deployment.webapp; 21 22 import java.io.ByteArrayInputStream ; 23 import java.util.Iterator ; 24 25 import javax.xml.parsers.DocumentBuilder ; 26 import javax.xml.parsers.DocumentBuilderFactory ; 27 import javax.xml.parsers.ParserConfigurationException ; 28 29 import junit.framework.TestCase; 30 31 import org.w3c.dom.Document ; 32 import org.w3c.dom.Element ; 33 34 41 public final class TestWebXmlMerger extends TestCase 42 { 43 46 private DocumentBuilderFactory factory; 47 48 51 private DocumentBuilder builder; 52 53 56 public void setUp() throws ParserConfigurationException 57 { 58 factory = DocumentBuilderFactory.newInstance(); 59 factory.setValidating(false); 60 factory.setNamespaceAware(false); 61 62 builder = factory.newDocumentBuilder(); 63 } 64 65 71 public void testMergeOneFilterIntoEmptyDocument() throws Exception 72 { 73 String srcXml = "<web-app></web-app>"; 74 Document srcDoc = 75 builder.parse(new ByteArrayInputStream (srcXml.getBytes())); 76 WebXml srcWebXml = new WebXml(srcDoc); 77 String mergeXml = "<web-app>" 78 + " <filter>" 79 + " <filter-name>f1</filter-name>" 80 + " <filter-class>fclass1</filter-class>" 81 + " </filter>" 82 + "</web-app>"; 83 Document mergeDoc = 84 builder.parse(new ByteArrayInputStream (mergeXml.getBytes())); 85 WebXml mergeWebXml = new WebXml(mergeDoc); 86 WebXmlMerger merger = new WebXmlMerger(srcWebXml); 87 merger.mergeFilters(mergeWebXml); 88 assertTrue(srcWebXml.hasFilter("f1")); 89 } 90 91 97 public void testMergeOneContextParamIntoEmptyDocument() throws Exception 98 { 99 String srcXml = "<web-app></web-app>"; 100 Document srcDoc = 101 builder.parse(new ByteArrayInputStream (srcXml.getBytes())); 102 WebXml srcWebXml = new WebXml(srcDoc); 103 String mergeXml = "<web-app>" 104 + " <context-param>" 105 + " <param-name>param</param-name>" 106 + " <param-value>value</param-value>" 107 + " </context-param>" 108 + "</web-app>"; 109 Document mergeDoc = 110 builder.parse(new ByteArrayInputStream (mergeXml.getBytes())); 111 WebXml mergeWebXml = new WebXml(mergeDoc); 112 WebXmlMerger merger = new WebXmlMerger(srcWebXml); 113 merger.mergeContextParams(mergeWebXml); 114 assertTrue(srcWebXml.hasContextParam("param")); 115 } 116 117 123 public void testMergeOneFilterIntoDocumentWithAnotherFilter() 124 throws Exception 125 { 126 String srcXml = "<web-app>" 127 + " <filter>" 128 + " <filter-name>f1</filter-name>" 129 + " <filter-class>fclass1</filter-class>" 130 + " </filter>" 131 + "</web-app>"; 132 Document srcDoc = 133 builder.parse(new ByteArrayInputStream (srcXml.getBytes())); 134 WebXml srcWebXml = new WebXml(srcDoc); 135 String mergeXml = "<web-app>" 136 + " <filter>" 137 + " <filter-name>f2</filter-name>" 138 + " <filter-class>fclass2</filter-class>" 139 + " </filter>" 140 + "</web-app>"; 141 Document mergeDoc = 142 builder.parse(new ByteArrayInputStream (mergeXml.getBytes())); 143 WebXml mergeWebXml = new WebXml(mergeDoc); 144 WebXmlMerger merger = new WebXmlMerger(srcWebXml); 145 merger.mergeFilters(mergeWebXml); 146 assertTrue(srcWebXml.hasFilter("f1")); 147 assertTrue(srcWebXml.hasFilter("f2")); 148 } 149 150 156 public void testMergeOneContextParamIntoDocumentWithAnotherContextParam() 157 throws Exception 158 { 159 String srcXml = "<web-app>" 160 + " <context-param>" 161 + " <param-name>param1</param-name>" 162 + " <param-value>value1</param-value>" 163 + " </context-param>" 164 + "</web-app>"; 165 Document srcDoc = 166 builder.parse(new ByteArrayInputStream (srcXml.getBytes())); 167 WebXml srcWebXml = new WebXml(srcDoc); 168 String mergeXml = "<web-app>" 169 + " <context-param>" 170 + " <param-name>param2</param-name>" 171 + " <param-value>value2</param-value>" 172 + " </context-param>" 173 + "</web-app>"; 174 Document mergeDoc = 175 builder.parse(new ByteArrayInputStream (mergeXml.getBytes())); 176 WebXml mergeWebXml = new WebXml(mergeDoc); 177 WebXmlMerger merger = new WebXmlMerger(srcWebXml); 178 merger.mergeContextParams(mergeWebXml); 179 assertTrue(srcWebXml.hasContextParam("param1")); 180 assertTrue(srcWebXml.hasContextParam("param2")); 181 } 182 183 189 public void testMergeOneFilterIntoDocumentWithSameFilter() 190 throws Exception 191 { 192 String srcXml = "<web-app>" 193 + " <filter>" 194 + " <filter-name>f1</filter-name>" 195 + " <filter-class>fclass1</filter-class>" 196 + " </filter>" 197 + "</web-app>"; 198 Document srcDoc = 199 builder.parse(new ByteArrayInputStream (srcXml.getBytes())); 200 WebXml srcWebXml = new WebXml(srcDoc); 201 String mergeXml = srcXml; 202 Document mergeDoc = 203 builder.parse(new ByteArrayInputStream (mergeXml.getBytes())); 204 WebXml mergeWebXml = new WebXml(mergeDoc); 205 WebXmlMerger merger = new WebXmlMerger(srcWebXml); 206 merger.mergeFilters(mergeWebXml); 207 assertTrue(srcWebXml.hasFilter("f1")); 208 } 209 210 217 public void testMergeOneContextParamIntoDocumentWithSameContextParam() 218 throws Exception 219 { 220 String srcXml = "<web-app>" 221 + " <context-param>" 222 + " <param-name>param</param-name>" 223 + " <param-value>value</param-value>" 224 + " </context-param>" 225 + "</web-app>"; 226 Document srcDoc = 227 builder.parse(new ByteArrayInputStream (srcXml.getBytes())); 228 WebXml srcWebXml = new WebXml(srcDoc); 229 String mergeXml = srcXml; 230 Document mergeDoc = 231 builder.parse(new ByteArrayInputStream (mergeXml.getBytes())); 232 WebXml mergeWebXml = new WebXml(mergeDoc); 233 WebXmlMerger merger = new WebXmlMerger(srcWebXml); 234 merger.mergeContextParams(mergeWebXml); 235 assertTrue(srcWebXml.hasContextParam("param")); 236 } 237 238 244 public void testMergeOneFilterIntoDocumentWithSameFilterAndParam() 245 throws Exception 246 { 247 String srcXml = "<web-app>" 248 + " <filter>" 249 + " <filter-name>f1</filter-name>" 250 + " <filter-class>fclass1</filter-class>" 251 + " </filter>" 252 + "</web-app>"; 253 Document srcDoc = 254 builder.parse(new ByteArrayInputStream (srcXml.getBytes())); 255 WebXml srcWebXml = new WebXml(srcDoc); 256 String mergeXml = "<web-app>" 257 + " <filter>" 258 + " <filter-name>f1</filter-name>" 259 + " <filter-class>fclass1</filter-class>" 260 + " <init-param>" 261 + " <param-name>f1param1</param-name>" 262 + " <param-value>f1param1value</param-value>" 263 + " </init-param>" 264 + " </filter>" 265 + "</web-app>"; 266 Document mergeDoc = 267 builder.parse(new ByteArrayInputStream (mergeXml.getBytes())); 268 WebXml mergeWebXml = new WebXml(mergeDoc); 269 WebXmlMerger merger = new WebXmlMerger(srcWebXml); 270 merger.mergeFilters(mergeWebXml); 271 assertTrue(srcWebXml.hasFilter("f1")); 272 Iterator initParams = srcWebXml.getFilterInitParamNames("f1"); 273 assertEquals("f1param1", initParams.next()); 274 assertTrue(!initParams.hasNext()); 275 } 276 277 283 public void testMergeOneFilterIntoDocumentWithMultipleFilters() 284 throws Exception 285 { 286 String srcXml = "<web-app>" 287 + " <filter>" 288 + " <filter-name>f1</filter-name>" 289 + " <filter-class>fclass1</filter-class>" 290 + " </filter>" 291 + " <filter>" 292 + " <filter-name>f2</filter-name>" 293 + " <filter-class>fclass2</filter-class>" 294 + " </filter>" 295 + " <filter>" 296 + " <filter-name>f3</filter-name>" 297 + " <filter-class>fclass3</filter-class>" 298 + " </filter>" 299 + "</web-app>"; 300 Document srcDoc = 301 builder.parse(new ByteArrayInputStream (srcXml.getBytes())); 302 WebXml srcWebXml = new WebXml(srcDoc); 303 String mergeXml = "<web-app>" 304 + " <filter>" 305 + " <filter-name>f4</filter-name>" 306 + " <filter-class>fclass4</filter-class>" 307 + " </filter>" 308 + "</web-app>"; 309 Document mergeDoc = 310 builder.parse(new ByteArrayInputStream (mergeXml.getBytes())); 311 WebXml mergeWebXml = new WebXml(mergeDoc); 312 WebXmlMerger merger = new WebXmlMerger(srcWebXml); 313 merger.mergeFilters(mergeWebXml); 314 Iterator filterNames = srcWebXml.getFilterNames(); 315 assertEquals("f1", filterNames.next()); 316 assertEquals("f2", filterNames.next()); 317 assertEquals("f3", filterNames.next()); 318 assertEquals("f4", filterNames.next()); 319 assertTrue(!filterNames.hasNext()); 320 } 321 322 328 public void testMergeMultipleFiltersIntoEmptyDocument() throws Exception 329 { 330 String srcXml = "<web-app></web-app>"; 331 Document srcDoc = 332 builder.parse(new ByteArrayInputStream (srcXml.getBytes())); 333 WebXml srcWebXml = new WebXml(srcDoc); 334 String mergeXml = "<web-app>" 335 + " <filter>" 336 + " <filter-name>f1</filter-name>" 337 + " <filter-class>fclass1</filter-class>" 338 + " </filter>" 339 + " <filter>" 340 + " <filter-name>f2</filter-name>" 341 + " <filter-class>fclass2</filter-class>" 342 + " </filter>" 343 + " <filter>" 344 + " <filter-name>f3</filter-name>" 345 + " <filter-class>fclass3</filter-class>" 346 + " </filter>" 347 + "</web-app>"; 348 Document mergeDoc = 349 builder.parse(new ByteArrayInputStream (mergeXml.getBytes())); 350 WebXml mergeWebXml = new WebXml(mergeDoc); 351 WebXmlMerger merger = new WebXmlMerger(srcWebXml); 352 merger.mergeFilters(mergeWebXml); 353 Iterator filterNames = srcWebXml.getFilterNames(); 354 assertEquals("f1", filterNames.next()); 355 assertEquals("f2", filterNames.next()); 356 assertEquals("f3", filterNames.next()); 357 assertTrue(!filterNames.hasNext()); 358 } 359 360 366 public void testMergeOneFilterWithOneMappingIntoEmptyDocument() 367 throws Exception 368 { 369 String srcXml = "<web-app></web-app>"; 370 Document srcDoc = 371 builder.parse(new ByteArrayInputStream (srcXml.getBytes())); 372 WebXml srcWebXml = new WebXml(srcDoc); 373 String mergeXml = "<web-app>" 374 + " <filter>" 375 + " <filter-name>f1</filter-name>" 376 + " <filter-class>fclass1</filter-class>" 377 + " </filter>" 378 + " <filter-mapping>" 379 + " <filter-name>f1</filter-name>" 380 + " <url-pattern>/f1mapping1</url-pattern>" 381 + " </filter-mapping>" 382 + "</web-app>"; 383 Document mergeDoc = 384 builder.parse(new ByteArrayInputStream (mergeXml.getBytes())); 385 WebXml mergeWebXml = new WebXml(mergeDoc); 386 WebXmlMerger merger = new WebXmlMerger(srcWebXml); 387 merger.mergeFilters(mergeWebXml); 388 assertTrue(srcWebXml.hasFilter("f1")); 389 Iterator filterMappings = srcWebXml.getFilterMappings("f1"); 390 assertEquals("/f1mapping1", filterMappings.next()); 391 assertTrue(!filterMappings.hasNext()); 392 } 393 394 400 public void testMergeOneFilterWithMultipleMappingsIntoEmptyDocument() 401 throws Exception 402 { 403 String srcXml = "<web-app></web-app>"; 404 Document srcDoc = 405 builder.parse(new ByteArrayInputStream (srcXml.getBytes())); 406 WebXml srcWebXml = new WebXml(srcDoc); 407 String mergeXml = "<web-app>" 408 + " <filter>" 409 + " <filter-name>f1</filter-name>" 410 + " <filter-class>fclass1</filter-class>" 411 + " </filter>" 412 + " <filter-mapping>" 413 + " <filter-name>f1</filter-name>" 414 + " <url-pattern>/f1mapping1</url-pattern>" 415 + " </filter-mapping>" 416 + " <filter-mapping>" 417 + " <filter-name>f1</filter-name>" 418 + " <url-pattern>/f1mapping2</url-pattern>" 419 + " </filter-mapping>" 420 + " <filter-mapping>" 421 + " <filter-name>f1</filter-name>" 422 + " <url-pattern>/f1mapping3</url-pattern>" 423 + " </filter-mapping>" 424 + "</web-app>"; 425 Document mergeDoc = 426 builder.parse(new ByteArrayInputStream (mergeXml.getBytes())); 427 WebXml mergeWebXml = new WebXml(mergeDoc); 428 WebXmlMerger merger = new WebXmlMerger(srcWebXml); 429 merger.mergeFilters(mergeWebXml); 430 assertTrue(srcWebXml.hasFilter("f1")); 431 Iterator filterMappings = srcWebXml.getFilterMappings("f1"); 432 assertEquals("/f1mapping1", filterMappings.next()); 433 assertEquals("/f1mapping2", filterMappings.next()); 434 assertEquals("/f1mapping3", filterMappings.next()); 435 assertTrue(!filterMappings.hasNext()); 436 } 437 438 444 public void testMergeOneServletIntoEmptyDocument() throws Exception 445 { 446 String srcXml = "<web-app></web-app>"; 447 Document srcDoc = 448 builder.parse(new ByteArrayInputStream (srcXml.getBytes())); 449 WebXml srcWebXml = new WebXml(srcDoc); 450 String mergeXml = "<web-app>" 451 + " <servlet>" 452 + " <servlet-name>s1</servlet-name>" 453 + " <servlet-class>sclass1</servlet-class>" 454 + " </servlet>" 455 + "</web-app>"; 456 Document mergeDoc = 457 builder.parse(new ByteArrayInputStream (mergeXml.getBytes())); 458 WebXml mergeWebXml = new WebXml(mergeDoc); 459 WebXmlMerger merger = new WebXmlMerger(srcWebXml); 460 merger.mergeServlets(mergeWebXml); 461 assertTrue(srcWebXml.hasServlet("s1")); 462 } 463 464 470 public void testMergeOneServletIntoDocumentWithAnotherServlet() 471 throws Exception 472 { 473 String srcXml = "<web-app>" 474 + " <servlet>" 475 + " <servlet-name>s1</servlet-name>" 476 + " <servlet-class>sclass1</servlet-class>" 477 + " </servlet>" 478 + "</web-app>"; 479 Document srcDoc = 480 builder.parse(new ByteArrayInputStream (srcXml.getBytes())); 481 WebXml srcWebXml = new WebXml(srcDoc); 482 String mergeXml = "<web-app>" 483 + " <servlet>" 484 + " <servlet-name>s2</servlet-name>" 485 + " <servlet-class>sclass2</servlet-class>" 486 + " </servlet>" 487 + "</web-app>"; 488 Document mergeDoc = 489 builder.parse(new ByteArrayInputStream (mergeXml.getBytes())); 490 WebXml mergeWebXml = new WebXml(mergeDoc); 491 WebXmlMerger merger = new WebXmlMerger(srcWebXml); 492 merger.mergeServlets(mergeWebXml); 493 assertTrue(srcWebXml.hasServlet("s1")); 494 assertTrue(srcWebXml.hasServlet("s2")); 495 } 496 497 503 public void testMergeOneServletIntoDocumentWithSameServlet() 504 throws Exception 505 { 506 String srcXml = "<web-app>" 507 + " <servlet>" 508 + " <servlet-name>s1</servlet-name>" 509 + " <servlet-class>sclass1</servlet-class>" 510 + " </servlet>" 511 + "</web-app>"; 512 Document srcDoc = 513 builder.parse(new ByteArrayInputStream (srcXml.getBytes())); 514 WebXml srcWebXml = new WebXml(srcDoc); 515 String mergeXml = "<web-app>" 516 + " <servlet>" 517 + " <servlet-name>s1</servlet-name>" 518 + " <servlet-class>sclass1</servlet-class>" 519 + " </servlet>" 520 + "</web-app>"; 521 Document mergeDoc = 522 builder.parse(new ByteArrayInputStream (mergeXml.getBytes())); 523 WebXml mergeWebXml = new WebXml(mergeDoc); 524 WebXmlMerger merger = new WebXmlMerger(srcWebXml); 525 merger.mergeServlets(mergeWebXml); 526 assertTrue(srcWebXml.hasServlet("s1")); 527 } 528 529 536 public void testMergeOneServletIntoDocumentWithSameServletAndParam() 537 throws Exception 538 { 539 String srcXml = "<web-app>" 540 + " <servlet>" 541 + " <servlet-name>s1</servlet-name>" 542 + " <servlet-class>sclass1</servlet-class>" 543 + " </servlet>" 544 + "</web-app>"; 545 Document srcDoc = 546 builder.parse(new ByteArrayInputStream (srcXml.getBytes())); 547 WebXml srcWebXml = new WebXml(srcDoc); 548 String mergeXml = "<web-app>" 549 + " <servlet>" 550 + " <servlet-name>s1</servlet-name>" 551 + " <servlet-class>sclass1</servlet-class>" 552 + " <init-param>" 553 + " <param-name>s1param1</param-name>" 554 + " <param-value>s1param1value</param-value>" 555 + " </init-param>" 556 + " </servlet>" 557 + "</web-app>"; 558 Document mergeDoc = 559 builder.parse(new ByteArrayInputStream (mergeXml.getBytes())); 560 WebXml mergeWebXml = new WebXml(mergeDoc); 561 WebXmlMerger merger = new WebXmlMerger(srcWebXml); 562 merger.mergeServlets(mergeWebXml); 563 assertTrue(srcWebXml.hasServlet("s1")); 564 Iterator initParams = srcWebXml.getServletInitParamNames("s1"); 565 assertEquals("s1param1", initParams.next()); 566 assertTrue(!initParams.hasNext()); 567 assertEquals("s1param1value", 568 srcWebXml.getServletInitParam("s1", "s1param1")); 569 } 570 571 577 public void testMergeOneServletIntoDocumentWithMultipleServlets() 578 throws Exception 579 { 580 String srcXml = "<web-app>" 581 + " <servlet>" 582 + " <servlet-name>s1</servlet-name>" 583 + " <servlet-class>sclass1</servlet-class>" 584 + " </servlet>" 585 + " <servlet>" 586 + " <servlet-name>s2</servlet-name>" 587 + " <servlet-class>sclass2</servlet-class>" 588 + " </servlet>" 589 + " <servlet>" 590 + " <servlet-name>s3</servlet-name>" 591 + " <servlet-class>sclass3</servlet-class>" 592 + " </servlet>" 593 + "</web-app>"; 594 Document srcDoc = 595 builder.parse(new ByteArrayInputStream (srcXml.getBytes())); 596 WebXml srcWebXml = new WebXml(srcDoc); 597 String mergeXml = "<web-app>" 598 + " <servlet>" 599 + " <servlet-name>s4</servlet-name>" 600 + " <servlet-class>sclass4</servlet-class>" 601 + " </servlet>" 602 + "</web-app>"; 603 Document mergeDoc = 604 builder.parse(new ByteArrayInputStream (mergeXml.getBytes())); 605 WebXml mergeWebXml = new WebXml(mergeDoc); 606 WebXmlMerger merger = new WebXmlMerger(srcWebXml); 607 merger.mergeServlets(mergeWebXml); 608 Iterator servletNames = srcWebXml.getServletNames(); 609 assertEquals("s1", servletNames.next()); 610 assertEquals("s2", servletNames.next()); 611 assertEquals("s3", servletNames.next()); 612 assertEquals("s4", servletNames.next()); 613 assertTrue(!servletNames.hasNext()); 614 } 615 616 622 public void testMergingServletWithInitParamsThatIsAlreadyDefined() 623 throws Exception 624 { 625 String srcXml = "<web-app>".trim() 626 + " <servlet>".trim() 627 + " <servlet-name>s1</servlet-name>".trim() 628 + " <servlet-class>sclass1</servlet-class>".trim() 629 + " <load-on-startup>1</load-on-startup>".trim() 630 + " </servlet>".trim() 631 + "</web-app>"; 632 Document srcDoc = 633 builder.parse(new ByteArrayInputStream (srcXml.getBytes())); 634 WebXml srcWebXml = new WebXml(srcDoc); 635 String mergeXml = "<web-app>" 636 + " <servlet>".trim() 637 + " <servlet-name>s1</servlet-name>".trim() 638 + " <servlet-class>sclass1</servlet-class>".trim() 639 + " <init-param>".trim() 640 + " <param-name>s1param1</param-name>".trim() 641 + " <param-value>s1param1value</param-value>".trim() 642 + " </init-param>".trim() 643 + " </servlet>".trim() 644 + "</web-app>"; 645 Document mergeDoc = 646 builder.parse(new ByteArrayInputStream (mergeXml.getBytes())); 647 WebXml mergeWebXml = new WebXml(mergeDoc); 648 WebXmlMerger merger = new WebXmlMerger(srcWebXml); 649 merger.mergeServlets(mergeWebXml); 650 Element servletElement = srcWebXml.getServlet("s1"); 651 assertEquals("load-on-startup", 652 ((Element ) servletElement.getLastChild()).getTagName()); 653 } 654 655 661 public void testMergeMultipleServletsIntoEmptyDocument() throws Exception 662 { 663 String srcXml = "<web-app></web-app>"; 664 Document srcDoc = 665 builder.parse(new ByteArrayInputStream (srcXml.getBytes())); 666 WebXml srcWebXml = new WebXml(srcDoc); 667 String mergeXml = "<web-app>" 668 + " <servlet>" 669 + " <servlet-name>s1</servlet-name>" 670 + " <servlet-class>sclass1</servlet-class>" 671 + " </servlet>" 672 + " <servlet>" 673 + " <servlet-name>s2</servlet-name>" 674 + " <servlet-class>sclass2</servlet-class>" 675 + " </servlet>" 676 + " <servlet>" 677 + " <servlet-name>s3</servlet-name>" 678 + " <servlet-class>sclass3</servlet-class>" 679 + " </servlet>" 680 + "</web-app>"; 681 Document mergeDoc = 682 builder.parse(new ByteArrayInputStream (mergeXml.getBytes())); 683 WebXml mergeWebXml = new WebXml(mergeDoc); 684 WebXmlMerger merger = new WebXmlMerger(srcWebXml); 685 merger.mergeServlets(mergeWebXml); 686 Iterator servletNames = srcWebXml.getServletNames(); 687 assertEquals("s1", servletNames.next()); 688 assertEquals("s2", servletNames.next()); 689 assertEquals("s3", servletNames.next()); 690 assertTrue(!servletNames.hasNext()); 691 } 692 693 699 public void testMergeOneServletWithOneMappingIntoEmptyDocument() 700 throws Exception 701 { 702 String srcXml = "<web-app></web-app>"; 703 Document srcDoc = 704 builder.parse(new ByteArrayInputStream (srcXml.getBytes())); 705 WebXml srcWebXml = new WebXml(srcDoc); 706 String mergeXml = "<web-app>" 707 + " <servlet>" 708 + " <servlet-name>s1</servlet-name>" 709 + " <servlet-class>sclass1</servlet-class>" 710 + " </servlet>" 711 + " <servlet-mapping>" 712 + " <servlet-name>s1</servlet-name>" 713 + " <url-pattern>/s1mapping1</url-pattern>" 714 + " </servlet-mapping>" 715 + "</web-app>"; 716 Document mergeDoc = 717 builder.parse(new ByteArrayInputStream (mergeXml.getBytes())); 718 WebXml mergeWebXml = new WebXml(mergeDoc); 719 WebXmlMerger merger = new WebXmlMerger(srcWebXml); 720 merger.mergeServlets(mergeWebXml); 721 assertTrue(srcWebXml.hasServlet("s1")); 722 Iterator servletMappings = srcWebXml.getServletMappings("s1"); 723 assertEquals("/s1mapping1", servletMappings.next()); 724 assertTrue(!servletMappings.hasNext()); 725 } 726 727 733 public void testMergeOneServletWithMultipleMappingsIntoEmptyDocument() 734 throws Exception 735 { 736 String srcXml = "<web-app></web-app>"; 737 Document srcDoc = 738 builder.parse(new ByteArrayInputStream (srcXml.getBytes())); 739 WebXml srcWebXml = new WebXml(srcDoc); 740 String mergeXml = "<web-app>" 741 + " <servlet>" 742 + " <servlet-name>s1</servlet-name>" 743 + " <servlet-class>sclass1</servlet-class>" 744 + " </servlet>" 745 + " <servlet-mapping>" 746 + " <servlet-name>s1</servlet-name>" 747 + " <url-pattern>/s1mapping1</url-pattern>" 748 + " </servlet-mapping>" 749 + " <servlet-mapping>" 750 + " <servlet-name>s1</servlet-name>" 751 + " <url-pattern>/s1mapping2</url-pattern>" 752 + " </servlet-mapping>" 753 + " <servlet-mapping>" 754 + " <servlet-name>s1</servlet-name>" 755 + " <url-pattern>/s1mapping3</url-pattern>" 756 + " </servlet-mapping>" 757 + "</web-app>"; 758 Document mergeDoc = 759 builder.parse(new ByteArrayInputStream (mergeXml.getBytes())); 760 WebXml mergeWebXml = new WebXml(mergeDoc); 761 WebXmlMerger merger = new WebXmlMerger(srcWebXml); 762 merger.mergeServlets(mergeWebXml); 763 assertTrue(srcWebXml.hasServlet("s1")); 764 Iterator servletMappings = srcWebXml.getServletMappings("s1"); 765 assertEquals("/s1mapping1", servletMappings.next()); 766 assertEquals("/s1mapping2", servletMappings.next()); 767 assertEquals("/s1mapping3", servletMappings.next()); 768 assertTrue(!servletMappings.hasNext()); 769 } 770 771 777 public void testMergeSecurityRoleIntoEmptyDocument() 778 throws Exception 779 { 780 String srcXml = "<web-app></web-app>"; 781 Document srcDoc = 782 builder.parse(new ByteArrayInputStream (srcXml.getBytes())); 783 WebXml srcWebXml = new WebXml(srcDoc); 784 String mergeXml = "<web-app>" 785 + " <security-role>" 786 + " <role-name>role1</role-name>" 787 + " </security-role>" 788 + "</web-app>"; 789 Document mergeDoc = 790 builder.parse(new ByteArrayInputStream (mergeXml.getBytes())); 791 WebXml mergeWebXml = new WebXml(mergeDoc); 792 WebXmlMerger merger = new WebXmlMerger(srcWebXml); 793 merger.mergeSecurityRoles(mergeWebXml); 794 Iterator securityRoleNames = srcWebXml.getSecurityRoleNames(); 795 assertTrue(securityRoleNames.hasNext()); 796 assertEquals("role1", securityRoleNames.next()); 797 assertTrue(!securityRoleNames.hasNext()); 798 } 799 800 806 public void testMergeSecurityRoleIntoDocumentWithSameRole() 807 throws Exception 808 { 809 String srcXml = "<web-app>" 810 + " <security-role>" 811 + " <description>A role</description>" 812 + " <role-name>role1</role-name>" 813 + " </security-role>" 814 + "</web-app>"; 815 Document srcDoc = 816 builder.parse(new ByteArrayInputStream (srcXml.getBytes())); 817 WebXml srcWebXml = new WebXml(srcDoc); 818 String mergeXml = "<web-app>" 819 + " <security-role>" 820 + " <role-name>role1</role-name>" 821 + " </security-role>" 822 + "</web-app>"; 823 Document mergeDoc = 824 builder.parse(new ByteArrayInputStream (mergeXml.getBytes())); 825 WebXml mergeWebXml = new WebXml(mergeDoc); 826 WebXmlMerger merger = new WebXmlMerger(srcWebXml); 827 merger.mergeSecurityRoles(mergeWebXml); 828 Iterator securityRoleNames = srcWebXml.getSecurityRoleNames(); 829 assertTrue(securityRoleNames.hasNext()); 830 assertEquals("role1", securityRoleNames.next()); 831 assertTrue(!securityRoleNames.hasNext()); 832 } 833 834 840 public void testMergeOneEjbRefIntoEmptyDocument() 841 throws Exception 842 { 843 String srcXml = "<web-app></web-app>"; 844 Document srcDoc = 845 builder.parse(new ByteArrayInputStream (srcXml.getBytes())); 846 WebXml srcWebXml = new WebXml(srcDoc); 847 String mergeXml = "<web-app>" 848 + " <ejb-ref>" 849 + " <ejb-ref-name>ejbref1</ejb-ref-name>" 850 + " <ejb-ref-type>ejbref1.type</ejb-ref-type>" 851 + " <home>ejbref1.homeInterface</home>" 852 + " <remote>ejbref1.remoteInterface</remote>" 853 + " </ejb-ref>" 854 + "</web-app>"; 855 Document mergeDoc = 856 builder.parse(new ByteArrayInputStream (mergeXml.getBytes())); 857 WebXml mergeWebXml = new WebXml(mergeDoc); 858 WebXmlMerger merger = new WebXmlMerger(srcWebXml); 859 merger.mergeEjbRefs(mergeWebXml); 860 Iterator ejbRefs = srcWebXml.getElements(WebXmlTag.EJB_REF); 861 assertTrue(ejbRefs.hasNext()); 862 } 863 864 } 865 | Popular Tags |