1 20 package org.apache.cactus.integration.ant.deployment.webapp; 21 22 import java.io.ByteArrayInputStream ; 23 import java.io.StringReader ; 24 import java.util.ArrayList ; 25 import java.util.Collections ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 29 import javax.xml.parsers.DocumentBuilder ; 30 import javax.xml.parsers.DocumentBuilderFactory ; 31 import javax.xml.parsers.ParserConfigurationException ; 32 33 import junit.framework.TestCase; 34 35 import org.w3c.dom.Document ; 36 import org.w3c.dom.Element ; 37 import org.w3c.dom.NodeList ; 38 import org.xml.sax.EntityResolver ; 39 import org.xml.sax.InputSource ; 40 import org.xml.sax.SAXException ; 41 42 47 public final class TestWebXml extends TestCase 48 { 49 52 private DocumentBuilderFactory factory; 53 54 57 private DocumentBuilder builder; 58 59 62 public void setUp() throws ParserConfigurationException 63 { 64 factory = DocumentBuilderFactory.newInstance(); 65 factory.setValidating(false); 66 factory.setNamespaceAware(false); 67 68 builder = factory.newDocumentBuilder(); 69 builder.setEntityResolver(new EntityResolver () 70 { 71 public InputSource resolveEntity(String thePublicId, 72 String theSystemId) throws SAXException 73 { 74 return new InputSource (new StringReader ("")); 75 } 76 }); 77 } 78 79 86 public void testConstructionWithNullDocument() throws Exception 87 { 88 try 89 { 90 new WebXml(null); 91 fail("Expected NullPointerException"); 92 } 93 catch (NullPointerException npe) 94 { 95 } 97 98 } 99 100 105 public void testGetVersion22() throws Exception 106 { 107 String xml = "<!DOCTYPE web-app " 108 + "PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN' " 109 + "'http://java.sun.com/j2ee/dtds/web-app_2.2.dtd'>" 110 + "<web-app></web-app>"; 111 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 112 WebXml webXml = new WebXml(doc); 113 assertEquals(WebXmlVersion.V2_2, webXml.getVersion()); 114 } 115 116 121 public void testGetVersion23() throws Exception 122 { 123 String xml = "<!DOCTYPE web-app " 124 + "PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN' " 125 + "'http://java.sun.com/dtd/web-app_2_3.dtd'>" 126 + "<web-app></web-app>"; 127 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 128 WebXml webXml = new WebXml(doc); 129 assertEquals(WebXmlVersion.V2_3, webXml.getVersion()); 130 } 131 132 138 public void testGetVersionUnknown() throws Exception 139 { 140 String xml = "<!DOCTYPE web-app " 141 + "PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 1.9//EN' " 142 + "'http://java.sun.com/dtd/web-app_1_9.dtd'>" 143 + "<web-app></web-app>"; 144 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 145 WebXml webXml = new WebXml(doc); 146 assertNull(webXml.getVersion()); 147 } 148 149 155 public void testGetVersionWithoutDoctype() throws Exception 156 { 157 String xml = "<web-app></web-app>"; 158 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 159 WebXml webXml = new WebXml(doc); 160 assertNull(webXml.getVersion()); 161 } 162 163 170 public void testHasFilterWithNullName() throws Exception 171 { 172 String xml = "<web-app></web-app>"; 173 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 174 WebXml webXml = new WebXml(doc); 175 try 176 { 177 webXml.hasFilter(null); 178 fail("Expected NullPointerException"); 179 } 180 catch (NullPointerException npe) 181 { 182 } 184 185 } 186 187 193 public void testHasFilterWithOneFilter() throws Exception 194 { 195 String xml = "<web-app>" 196 + " <filter>" 197 + " <filter-name>f1</filter-name>" 198 + " <filter-class>fclass1</filter-class>" 199 + " </filter>" 200 + "</web-app>"; 201 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 202 WebXml webXml = new WebXml(doc); 203 assertTrue(webXml.hasFilter("f1")); 204 assertTrue(!webXml.hasFilter("f2")); 205 } 206 207 213 public void testHasFilterWithMultipleFilters() throws Exception 214 { 215 String xml = "<web-app>" 216 + " <filter>" 217 + " <filter-name>f1</filter-name>" 218 + " <filter-class>fclass1</filter-class>" 219 + " </filter>" 220 + " <filter>" 221 + " <filter-name>f2</filter-name>" 222 + " <filter-class>fclass2</filter-class>" 223 + " </filter>" 224 + " <filter>" 225 + " <filter-name>f3</filter-name>" 226 + " <filter-class>fclass3</filter-class>" 227 + " </filter>" 228 + "</web-app>"; 229 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 230 WebXml webXml = new WebXml(doc); 231 assertTrue(webXml.hasFilter("f1")); 232 assertTrue(webXml.hasFilter("f2")); 233 assertTrue(webXml.hasFilter("f3")); 234 assertTrue(!webXml.hasFilter("f4")); 235 } 236 237 243 public void testGetFilterElementWithOneFilter() throws Exception 244 { 245 String xml = "<web-app>" 246 + " <filter>".trim() 247 + " <filter-name>f1</filter-name>".trim() 248 + " <filter-class>fclass1</filter-class>".trim() 249 + " </filter>".trim() 250 + "</web-app>"; 251 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 252 WebXml webXml = new WebXml(doc); 253 Element servletElement = webXml.getFilter("f1"); 254 assertNotNull(servletElement); 255 assertEquals("filter", servletElement.getNodeName()); 256 assertEquals("filter-name", 257 servletElement.getFirstChild().getNodeName()); 258 assertEquals("f1", 259 servletElement.getFirstChild().getFirstChild().getNodeValue()); 260 assertEquals("filter-class", 261 servletElement.getLastChild().getNodeName()); 262 assertEquals("fclass1", 263 servletElement.getLastChild().getFirstChild().getNodeValue()); 264 } 265 266 271 public void testGetFilterNames() throws Exception 272 { 273 String xml = "<web-app>" 274 + " <filter>" 275 + " <filter-name>f1</filter-name>" 276 + " <filter-class>fclass1</filter-class>" 277 + " </filter>" 278 + " <filter>" 279 + " <filter-name>f2</filter-name>" 280 + " <filter-class>fclass2</filter-class>" 281 + " </filter>" 282 + " <filter>" 283 + " <filter-name>f3</filter-name>" 284 + " <filter-class>fclass3</filter-class>" 285 + " </filter>" 286 + "</web-app>"; 287 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 288 WebXml webXml = new WebXml(doc); 289 Iterator filterNames = webXml.getFilterNames(); 290 assertEquals("f1", filterNames.next()); 291 assertEquals("f2", filterNames.next()); 292 assertEquals("f3", filterNames.next()); 293 assertTrue(!filterNames.hasNext()); 294 } 295 296 303 public void testGetFilterNamesForClassWithSingleFilter() throws Exception 304 { 305 String xml = "<web-app>" 306 + " <filter>" 307 + " <filter-name>f1</filter-name>" 308 + " <filter-class>f1class</filter-class>" 309 + " </filter>" 310 + "</web-app>"; 311 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 312 WebXml webXml = new WebXml(doc); 313 Iterator filterNames = webXml.getFilterNamesForClass("f1class"); 314 assertEquals("f1", filterNames.next()); 315 assertTrue(!filterNames.hasNext()); 316 } 317 318 325 public void testGetFilterNamesForClassWithMultipleFilters() throws Exception 326 { 327 String xml = "<web-app>" 328 + " <filter>" 329 + " <filter-name>f1</filter-name>" 330 + " <filter-class>f1class</filter-class>" 331 + " </filter>" 332 + " <filter>" 333 + " <filter-name>f2</filter-name>" 334 + " <filter-class>f2class</filter-class>" 335 + " </filter>" 336 + " <filter>" 337 + " <filter-name>f3</filter-name>" 338 + " <filter-class>f1class</filter-class>" 339 + " </filter>" 340 + "</web-app>"; 341 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 342 WebXml webXml = new WebXml(doc); 343 Iterator filterNames = webXml.getFilterNamesForClass("f1class"); 344 assertEquals("f1", filterNames.next()); 345 assertEquals("f3", filterNames.next()); 346 assertTrue(!filterNames.hasNext()); 347 } 348 349 354 public void testGetFilterMappingsWithOneMapping() throws Exception 355 { 356 String xml = "<web-app>" 357 + " <filter-mapping>" 358 + " <filter-name>f1</filter-name>" 359 + " <url-pattern>/f1mapping</url-pattern>" 360 + " </filter-mapping>" 361 + "</web-app>"; 362 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 363 WebXml webXml = new WebXml(doc); 364 Iterator filterMappings = webXml.getFilterMappings("f1"); 365 assertEquals("/f1mapping", filterMappings.next()); 366 assertTrue(!filterMappings.hasNext()); 367 } 368 369 375 public void testGetFilterMappingsWithMultipleMappings() throws Exception 376 { 377 String xml = "<web-app>" 378 + " <filter-mapping>" 379 + " <filter-name>f1</filter-name>" 380 + " <url-pattern>/f1mapping1</url-pattern>" 381 + " </filter-mapping>" 382 + " <filter-mapping>" 383 + " <filter-name>f1</filter-name>" 384 + " <url-pattern>/f1mapping2</url-pattern>" 385 + " </filter-mapping>" 386 + " <filter-mapping>" 387 + " <filter-name>f1</filter-name>" 388 + " <url-pattern>/f1mapping3</url-pattern>" 389 + " </filter-mapping>" 390 + "</web-app>"; 391 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 392 WebXml webXml = new WebXml(doc); 393 Iterator filterMappings = webXml.getFilterMappings("f1"); 394 assertEquals("/f1mapping1", filterMappings.next()); 395 assertEquals("/f1mapping2", filterMappings.next()); 396 assertEquals("/f1mapping3", filterMappings.next()); 397 assertTrue(!filterMappings.hasNext()); 398 } 399 400 405 public void testGetFilterMappingsWithFilter() throws Exception 406 { 407 String xml = "<web-app>" 408 + " <filter>" 409 + " <filter-name>f1</filter-name>" 410 + " <filter-class>f1class</filter-class>" 411 + " </filter>" 412 + " <filter-mapping>" 413 + " <filter-name>f1</filter-name>" 414 + " <url-pattern>/f1mapping</url-pattern>" 415 + " </filter-mapping>" 416 + "</web-app>"; 417 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 418 WebXml webXml = new WebXml(doc); 419 Iterator filterMappings = webXml.getFilterMappings("f1"); 420 assertEquals("/f1mapping", filterMappings.next()); 421 assertTrue(!filterMappings.hasNext()); 422 } 423 424 430 public void testAddContextParamToEmptyDocument() throws Exception 431 { 432 String xml = "<web-app></web-app>"; 433 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 434 WebXml webXml = new WebXml(doc); 435 Element contextParamElement = 436 createContextParamElement(doc, "param", "value"); 437 webXml.addContextParam(contextParamElement); 438 assertTrue(webXml.hasContextParam("param")); 439 } 440 441 447 public void testAddFilterToEmptyDocument() throws Exception 448 { 449 String xml = "<web-app></web-app>"; 450 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 451 WebXml webXml = new WebXml(doc); 452 Element filterElement = createFilterElement(doc, "f1", "f1class"); 453 webXml.addFilter(filterElement); 454 assertTrue(webXml.hasFilter("f1")); 455 } 456 457 463 public void testAddContextParamToDocumentWithAnotherContextParam() 464 throws Exception 465 { 466 String xml = "<web-app>" 467 + " <context-param>" 468 + " <param-name>param1</param-name>" 469 + " <param-value>value1</param-value>" 470 + " </context-param>" 471 + "</web-app>"; 472 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 473 WebXml webXml = new WebXml(doc); 474 Element contextParamElement = 475 createContextParamElement(doc, "param2", "value2"); 476 webXml.addContextParam(contextParamElement); 477 assertTrue(webXml.hasContextParam("param1")); 478 assertTrue(webXml.hasContextParam("param2")); 479 } 480 481 487 public void testAddFilterToDocumentWithAnotherFilter() throws Exception 488 { 489 String xml = "<web-app>" 490 + " <filter>" 491 + " <filter-name>f1</filter-name>" 492 + " <filter-class>fclass1</filter-class>" 493 + " </filter>" 494 + "</web-app>"; 495 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 496 WebXml webXml = new WebXml(doc); 497 Element filterElement = createFilterElement(doc, "f2", "f2class"); 498 webXml.addFilter(filterElement); 499 assertTrue(webXml.hasFilter("f1")); 500 assertTrue(webXml.hasFilter("f2")); 501 } 502 503 510 public void testAddContextParamToDocumentWithTheSameContextParam() 511 throws Exception 512 { 513 String xml = "<web-app>" 514 + " <context-param>" 515 + " <param-name>param</param-name>" 516 + " <param-value>value</param-value>" 517 + " </context-param>" 518 + "</web-app>"; 519 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 520 WebXml webXml = new WebXml(doc); 521 Element contextParamElement = 522 createContextParamElement(doc, "param", "value"); 523 try 524 { 525 webXml.addContextParam(contextParamElement); 526 fail("Expected IllegalStateException"); 527 } 528 catch (IllegalStateException ise) 529 { 530 } 532 } 533 534 540 public void testAddFilterToDocumentWithTheSameFilter() throws Exception 541 { 542 String xml = "<web-app>" 543 + " <filter>" 544 + " <filter-name>f1</filter-name>" 545 + " <filter-class>fclass1</filter-class>" 546 + " </filter>" 547 + "</web-app>"; 548 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 549 WebXml webXml = new WebXml(doc); 550 Element filterElement = createFilterElement(doc, "f1", "f1class"); 551 try 552 { 553 webXml.addFilter(filterElement); 554 fail("Expected IllegalStateException"); 555 } 556 catch (IllegalStateException ise) 557 { 558 } 560 } 561 562 568 public void testAddOneFilterInitParam() throws Exception 569 { 570 String xml = "<web-app>" 571 + " <filter>" 572 + " <filter-name>f1</filter-name>" 573 + " <filter-class>fclass1</filter-class>" 574 + " </filter>" 575 + "</web-app>"; 576 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 577 WebXml webXml = new WebXml(doc); 578 webXml.addFilterInitParam("f1", "f1param1", "f1param1value"); 579 Iterator initParams = webXml.getFilterInitParamNames("f1"); 580 assertEquals("f1param1", initParams.next()); 581 assertTrue(!initParams.hasNext()); 582 } 583 584 590 public void testAddMultipleFilterInitParams() throws Exception 591 { 592 String xml = "<web-app>" 593 + " <filter>" 594 + " <filter-name>f1</filter-name>" 595 + " <filter-class>fclass1</filter-class>" 596 + " </filter>" 597 + "</web-app>"; 598 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 599 WebXml webXml = new WebXml(doc); 600 webXml.addFilterInitParam("f1", "f1param1", "f1param1value"); 601 webXml.addFilterInitParam("f1", "f1param2", "f1param2value"); 602 webXml.addFilterInitParam("f1", "f1param3", "f1param3value"); 603 Iterator initParams = webXml.getFilterInitParamNames("f1"); 604 assertEquals("f1param1", initParams.next()); 605 assertEquals("f1param2", initParams.next()); 606 assertEquals("f1param3", initParams.next()); 607 assertTrue(!initParams.hasNext()); 608 } 609 610 616 public void testAddFilterWithNameAndClass() throws Exception 617 { 618 String xml = "<web-app>" 619 + "</web-app>"; 620 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 621 WebXml webXml = new WebXml(doc); 622 webXml.addServlet("f1", "f1class"); 623 assertTrue(webXml.hasServlet("f1")); 624 } 625 626 632 public void testHasServletWithNullName() throws Exception 633 { 634 String xml = "<web-app></web-app>"; 635 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 636 WebXml webXml = new WebXml(doc); 637 try 638 { 639 webXml.hasServlet(null); 640 fail("Expected NullPointerException"); 641 } 642 catch (NullPointerException npe) 643 { 644 } 646 647 } 648 649 655 public void testHasServletWithOneServlet() throws Exception 656 { 657 String xml = "<web-app>" 658 + " <servlet>" 659 + " <servlet-name>s1</servlet-name>" 660 + " <servlet-class>sclass1</servlet-class>" 661 + " </servlet>" 662 + "</web-app>"; 663 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 664 WebXml webXml = new WebXml(doc); 665 assertTrue(webXml.hasServlet("s1")); 666 assertTrue(!webXml.hasServlet("s2")); 667 } 668 669 675 public void testHasServletWithMultipleServlets() throws Exception 676 { 677 String xml = "<web-app>" 678 + " <servlet>" 679 + " <servlet-name>s1</servlet-name>" 680 + " <servlet-class>sclass1</servlet-class>" 681 + " </servlet>" 682 + " <servlet>" 683 + " <servlet-name>s2</servlet-name>" 684 + " <servlet-class>sclass2</servlet-class>" 685 + " </servlet>" 686 + " <servlet>" 687 + " <servlet-name>s3</servlet-name>" 688 + " <servlet-class>sclass3</servlet-class>" 689 + " </servlet>" 690 + "</web-app>"; 691 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 692 WebXml webXml = new WebXml(doc); 693 assertTrue(webXml.hasServlet("s1")); 694 assertTrue(webXml.hasServlet("s2")); 695 assertTrue(webXml.hasServlet("s3")); 696 assertTrue(!webXml.hasServlet("s4")); 697 } 698 699 705 public void testGetServletElementWithOneServlet() throws Exception 706 { 707 String xml = "<web-app>" 708 + " <servlet>".trim() 709 + " <servlet-name>s1</servlet-name>".trim() 710 + " <servlet-class>sclass1</servlet-class>".trim() 711 + " </servlet>".trim() 712 + "</web-app>"; 713 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 714 WebXml webXml = new WebXml(doc); 715 Element servletElement = webXml.getServlet("s1"); 716 assertNotNull(servletElement); 717 assertEquals("servlet", servletElement.getNodeName()); 718 assertEquals("servlet-name", 719 servletElement.getFirstChild().getNodeName()); 720 assertEquals("s1", 721 servletElement.getFirstChild().getFirstChild().getNodeValue()); 722 assertEquals("servlet-class", 723 servletElement.getLastChild().getNodeName()); 724 assertEquals("sclass1", 725 servletElement.getLastChild().getFirstChild().getNodeValue()); 726 } 727 728 734 public void testGetServletNames() throws Exception 735 { 736 String xml = "<web-app>" 737 + " <servlet>" 738 + " <servlet-name>s1</servlet-name>" 739 + " <servlet-class>sclass1</servlet-class>" 740 + " </servlet>" 741 + " <servlet>" 742 + " <servlet-name>s2</servlet-name>" 743 + " <servlet-class>sclass2</servlet-class>" 744 + " </servlet>" 745 + " <servlet>" 746 + " <servlet-name>s3</servlet-name>" 747 + " <servlet-class>sclass3</servlet-class>" 748 + " </servlet>" 749 + "</web-app>"; 750 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 751 WebXml webXml = new WebXml(doc); 752 Iterator servletNames = webXml.getServletNames(); 753 assertEquals("s1", servletNames.next()); 754 assertEquals("s2", servletNames.next()); 755 assertEquals("s3", servletNames.next()); 756 assertTrue(!servletNames.hasNext()); 757 } 758 759 766 public void testGetServletNamesForClassWithSingleServlet() throws Exception 767 { 768 String xml = "<web-app>" 769 + " <servlet>" 770 + " <servlet-name>s1</servlet-name>" 771 + " <servlet-class>s1class</servlet-class>" 772 + " </servlet>" 773 + "</web-app>"; 774 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 775 WebXml webXml = new WebXml(doc); 776 Iterator servletNames = webXml.getServletNamesForClass("s1class"); 777 assertEquals("s1", servletNames.next()); 778 assertTrue(!servletNames.hasNext()); 779 } 780 781 788 public void testGetServletNamesForClassWithMultipleServlets() 789 throws Exception 790 { 791 String xml = "<web-app>" 792 + " <servlet>" 793 + " <servlet-name>s1</servlet-name>" 794 + " <servlet-class>sclass1</servlet-class>" 795 + " </servlet>" 796 + " <servlet>" 797 + " <servlet-name>s2</servlet-name>" 798 + " <servlet-class>sclass2</servlet-class>" 799 + " </servlet>" 800 + " <servlet>" 801 + " <servlet-name>s3</servlet-name>" 802 + " <servlet-class>sclass1</servlet-class>" 803 + " </servlet>" 804 + "</web-app>"; 805 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 806 WebXml webXml = new WebXml(doc); 807 Iterator servletNames = webXml.getServletNamesForClass("sclass1"); 808 assertEquals("s1", servletNames.next()); 809 assertEquals("s3", servletNames.next()); 810 assertTrue(!servletNames.hasNext()); 811 } 812 813 820 public void testGetServletNamesForJspFileWithSingleServlet() 821 throws Exception 822 { 823 String xml = "<web-app>" 824 + " <servlet>" 825 + " <servlet-name>s1</servlet-name>" 826 + " <jsp-file>/s1.jsp</jsp-file>" 827 + " </servlet>" 828 + "</web-app>"; 829 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 830 WebXml webXml = new WebXml(doc); 831 Iterator servletNames = webXml.getServletNamesForJspFile("/s1.jsp"); 832 assertEquals("s1", servletNames.next()); 833 assertTrue(!servletNames.hasNext()); 834 } 835 836 843 public void testGetServletNamesForJspFileWithMultipleServlets() 844 throws Exception 845 { 846 String xml = "<web-app>" 847 + " <servlet>" 848 + " <servlet-name>s1</servlet-name>" 849 + " <jsp-file>/s1.jsp</jsp-file>" 850 + " </servlet>" 851 + " <servlet>" 852 + " <servlet-name>s2</servlet-name>" 853 + " <servlet-class>sclass2</servlet-class>" 854 + " </servlet>" 855 + " <servlet>" 856 + " <servlet-name>s3</servlet-name>" 857 + " <jsp-file>/s3.jsp</jsp-file>" 858 + " </servlet>" 859 + "</web-app>"; 860 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 861 WebXml webXml = new WebXml(doc); 862 Iterator servletNames = webXml.getServletNamesForJspFile("/s3.jsp"); 863 assertEquals("s3", servletNames.next()); 864 assertTrue(!servletNames.hasNext()); 865 } 866 867 873 public void testGetServletMappingsWithOneMapping() throws Exception 874 { 875 String xml = "<web-app>" 876 + " <servlet-mapping>" 877 + " <servlet-name>s1</servlet-name>" 878 + " <url-pattern>/s1mapping</url-pattern>" 879 + " </servlet-mapping>" 880 + "</web-app>"; 881 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 882 WebXml webXml = new WebXml(doc); 883 Iterator servletMappings = webXml.getServletMappings("s1"); 884 assertEquals("/s1mapping", servletMappings.next()); 885 assertTrue(!servletMappings.hasNext()); 886 } 887 888 894 public void testGetServletMappingsWithMultipleMappings() throws Exception 895 { 896 String xml = "<web-app>" 897 + " <servlet-mapping>" 898 + " <servlet-name>s1</servlet-name>" 899 + " <url-pattern>/s1mapping1</url-pattern>" 900 + " </servlet-mapping>" 901 + " <servlet-mapping>" 902 + " <servlet-name>s1</servlet-name>" 903 + " <url-pattern>/s1mapping2</url-pattern>" 904 + " </servlet-mapping>" 905 + " <servlet-mapping>" 906 + " <servlet-name>s1</servlet-name>" 907 + " <url-pattern>/s1mapping3</url-pattern>" 908 + " </servlet-mapping>" 909 + "</web-app>"; 910 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 911 WebXml webXml = new WebXml(doc); 912 Iterator servletMappings = webXml.getServletMappings("s1"); 913 assertEquals("/s1mapping1", servletMappings.next()); 914 assertEquals("/s1mapping2", servletMappings.next()); 915 assertEquals("/s1mapping3", servletMappings.next()); 916 assertTrue(!servletMappings.hasNext()); 917 } 918 919 924 public void testAddServletToEmptyDocument() throws Exception 925 { 926 String xml = "<web-app></web-app>"; 927 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 928 WebXml webXml = new WebXml(doc); 929 webXml.addServlet(createServletElement(doc, "s1", "s1class")); 930 assertTrue(webXml.hasServlet("s1")); 931 } 932 933 939 public void testAddServletToDocumentWithAnotherServlet() throws Exception 940 { 941 String xml = "<web-app>" 942 + " <servlet>" 943 + " <servlet-name>s1</servlet-name>" 944 + " <servlet-class>sclass1</servlet-class>" 945 + " </servlet>" 946 + "</web-app>"; 947 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 948 WebXml webXml = new WebXml(doc); 949 webXml.addServlet(createServletElement(doc, "s2", "s2class")); 950 assertTrue(webXml.hasServlet("s1")); 951 assertTrue(webXml.hasServlet("s2")); 952 } 953 954 960 public void testAddServletToDocumentWithTheSameServlet() throws Exception 961 { 962 String xml = "<web-app>" 963 + " <servlet>" 964 + " <servlet-name>s1</servlet-name>" 965 + " <servlet-class>sclass1</servlet-class>" 966 + " </servlet>" 967 + "</web-app>"; 968 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 969 WebXml webXml = new WebXml(doc); 970 try 971 { 972 webXml.addServlet(createServletElement(doc, "s1", "s1class")); 973 fail("Expected IllegalStateException"); 974 } 975 catch (IllegalStateException ise) 976 { 977 } 979 } 980 981 987 public void testAddOneServletInitParam() throws Exception 988 { 989 String xml = "<web-app>" 990 + " <servlet>" 991 + " <servlet-name>s1</servlet-name>" 992 + " <servlet-class>sclass1</servlet-class>" 993 + " </servlet>" 994 + "</web-app>"; 995 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 996 WebXml webXml = new WebXml(doc); 997 webXml.addServletInitParam("s1", "s1param1", "s1param1value"); 998 Iterator initParams = webXml.getServletInitParamNames("s1"); 999 assertEquals("s1param1", initParams.next()); 1000 assertTrue(!initParams.hasNext()); 1001 } 1002 1003 1009 public void testAddMultipleServletInitParams() throws Exception 1010 { 1011 String xml = "<web-app>" 1012 + " <servlet>" 1013 + " <servlet-name>s1</servlet-name>" 1014 + " <servlet-class>sclass1</servlet-class>" 1015 + " </servlet>" 1016 + "</web-app>"; 1017 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 1018 WebXml webXml = new WebXml(doc); 1019 webXml.addServletInitParam("s1", "s1param1", "s1param1value"); 1020 webXml.addServletInitParam("s1", "s1param2", "s1param2value"); 1021 webXml.addServletInitParam("s1", "s1param3", "s1param3value"); 1022 Iterator initParams = webXml.getServletInitParamNames("s1"); 1023 assertEquals("s1param1", initParams.next()); 1024 assertEquals("s1param2", initParams.next()); 1025 assertEquals("s1param3", initParams.next()); 1026 assertTrue(!initParams.hasNext()); 1027 } 1028 1029 1035 public void testAddServletWithNameAndClass() throws Exception 1036 { 1037 String xml = "<web-app>" 1038 + "</web-app>"; 1039 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 1040 WebXml webXml = new WebXml(doc); 1041 webXml.addServlet("s1", "s1class"); 1042 assertTrue(webXml.hasServlet("s1")); 1043 } 1044 1045 1051 public void testAddServletWithNameAndJspFile() throws Exception 1052 { 1053 String xml = "<web-app>" 1054 + "</web-app>"; 1055 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 1056 WebXml webXml = new WebXml(doc); 1057 webXml.addJspFile("s1", "s1.jsp"); 1058 assertTrue(webXml.hasServlet("s1")); 1059 } 1060 1061 1067 public void testAddSecurityConstraint() 1068 throws Exception 1069 { 1070 String xml = "<web-app></web-app>"; 1071 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 1072 WebXml webXml = new WebXml(doc); 1073 webXml.addSecurityConstraint("wrn", "/url", Collections.EMPTY_LIST); 1074 assertTrue(webXml.hasSecurityConstraint("/url")); 1075 } 1076 1077 1083 public void testAddSecurityConstraintWithRoles() 1084 throws Exception 1085 { 1086 String xml = "<web-app></web-app>"; 1087 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 1088 WebXml webXml = new WebXml(doc); 1089 List roles = new ArrayList (); 1090 roles.add("role1"); 1091 roles.add("role2"); 1092 webXml.addSecurityConstraint("wrn", "/url", roles); 1093 assertTrue(webXml.hasSecurityConstraint("/url")); 1094 Element securityConstraintElement = 1095 webXml.getSecurityConstraint("/url"); 1096 assertNotNull(securityConstraintElement); 1097 Element authConstraintElement = (Element ) 1098 securityConstraintElement.getElementsByTagName( 1099 "auth-constraint").item(0); 1100 assertNotNull(authConstraintElement); 1101 NodeList roleNameElements = 1102 authConstraintElement.getElementsByTagName("role-name"); 1103 assertEquals(2, roleNameElements.getLength()); 1104 assertEquals("role1", 1105 roleNameElements.item(0).getChildNodes().item(0).getNodeValue()); 1106 assertEquals("role2", 1107 roleNameElements.item(1).getChildNodes().item(0).getNodeValue()); 1108 } 1109 1110 1116 public void testHasLoginConfigEmpty() 1117 throws Exception 1118 { 1119 String xml = "<web-app></web-app>"; 1120 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 1121 WebXml webXml = new WebXml(doc); 1122 assertTrue(!webXml.hasLoginConfig()); 1123 } 1124 1125 1131 public void testHasLoginConfig() 1132 throws Exception 1133 { 1134 String xml = "<web-app>" 1135 + " <login-config>" 1136 + " <auth-method>BASIC</auth-method>" 1137 + " </login-config>" 1138 + "</web-app>"; 1139 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 1140 WebXml webXml = new WebXml(doc); 1141 assertTrue(webXml.hasLoginConfig()); 1142 } 1143 1144 1149 public void testGetLoginConfigAuthMethod() 1150 throws Exception 1151 { 1152 String xml = "<web-app>" 1153 + " <login-config>" 1154 + " <auth-method>BASIC</auth-method>" 1155 + " </login-config>" 1156 + "</web-app>"; 1157 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 1158 WebXml webXml = new WebXml(doc); 1159 assertEquals("BASIC", webXml.getLoginConfigAuthMethod()); 1160 } 1161 1162 1167 public void testSetLoginConfigAdding() 1168 throws Exception 1169 { 1170 String xml = "<web-app></web-app>"; 1171 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 1172 WebXml webXml = new WebXml(doc); 1173 webXml.setLoginConfig("BASIC", "Test Realm"); 1174 assertTrue(webXml.hasLoginConfig()); 1175 assertEquals("BASIC", webXml.getLoginConfigAuthMethod()); 1176 } 1177 1178 1183 public void testSetLoginConfigReplacing() 1184 throws Exception 1185 { 1186 String xml = "<web-app>" 1187 + " <login-config>" 1188 + " <auth-method>DIGEST</auth-method>" 1189 + " </login-config>" 1190 + "</web-app>"; 1191 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 1192 WebXml webXml = new WebXml(doc); 1193 webXml.setLoginConfig("BASIC", "Test Realm"); 1194 assertTrue(webXml.hasLoginConfig()); 1195 assertEquals("BASIC", webXml.getLoginConfigAuthMethod()); 1196 } 1197 1198 1204 public void testHasSecurityConstraintEmpty() 1205 throws Exception 1206 { 1207 String xml = "<web-app></web-app>"; 1208 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 1209 WebXml webXml = new WebXml(doc); 1210 assertTrue(!webXml.hasSecurityConstraint("/TestUrl")); 1211 } 1212 1213 1219 public void testGetSingleSecurityConstraint() 1220 throws Exception 1221 { 1222 String xml = "<web-app>" 1223 + " <security-constraint>" 1224 + " <web-resource-collection>" 1225 + " <web-resource-name>wr1</web-resource-name>" 1226 + " <url-pattern>/url1</url-pattern>" 1227 + " </web-resource-collection>" 1228 + " </security-constraint>" 1229 + "</web-app>"; 1230 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 1231 WebXml webXml = new WebXml(doc); 1232 assertTrue(webXml.hasSecurityConstraint("/url1")); 1233 Element securityConstraintElement = 1234 webXml.getSecurityConstraint("/url1"); 1235 assertNotNull(securityConstraintElement); 1236 } 1237 1238 1244 public void testGetMutlipleSecurityConstraints() 1245 throws Exception 1246 { 1247 String xml = "<web-app>" 1248 + " <security-constraint>" 1249 + " <web-resource-collection>" 1250 + " <web-resource-name>wr1</web-resource-name>" 1251 + " <url-pattern>/url1</url-pattern>" 1252 + " </web-resource-collection>" 1253 + " </security-constraint>" 1254 + " <security-constraint>" 1255 + " <web-resource-collection>" 1256 + " <web-resource-name>wr2</web-resource-name>" 1257 + " <url-pattern>/url2</url-pattern>" 1258 + " </web-resource-collection>" 1259 + " </security-constraint>" 1260 + " <security-constraint>" 1261 + " <web-resource-collection>" 1262 + " <web-resource-name>wr3</web-resource-name>" 1263 + " <url-pattern>/url3</url-pattern>" 1264 + " </web-resource-collection>" 1265 + " </security-constraint>" 1266 + "</web-app>"; 1267 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 1268 WebXml webXml = new WebXml(doc); 1269 assertTrue(webXml.hasSecurityConstraint("/url1")); 1270 assertTrue(webXml.hasSecurityConstraint("/url2")); 1271 assertTrue(webXml.hasSecurityConstraint("/url3")); 1272 Iterator securityConstraints = 1273 webXml.getElements(WebXmlTag.SECURITY_CONSTRAINT); 1274 assertNotNull(securityConstraints.next()); 1275 assertNotNull(securityConstraints.next()); 1276 assertNotNull(securityConstraints.next()); 1277 assertTrue(!securityConstraints.hasNext()); 1278 } 1279 1280 1286 public void testGetLoginConfigEmpty() 1287 throws Exception 1288 { 1289 String xml = "<web-app></web-app>"; 1290 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 1291 WebXml webXml = new WebXml(doc); 1292 assertTrue(!webXml.getElements(WebXmlTag.LOGIN_CONFIG).hasNext()); 1293 } 1294 1295 1300 public void testGetLoginConfig() 1301 throws Exception 1302 { 1303 String xml = "<web-app><login-config/></web-app>"; 1304 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 1305 WebXml webXml = new WebXml(doc); 1306 assertTrue(webXml.getElements(WebXmlTag.LOGIN_CONFIG).hasNext()); 1307 } 1308 1309 1315 public void testHasSecurityRoleEmpty() 1316 throws Exception 1317 { 1318 String xml = "<web-app></web-app>"; 1319 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 1320 WebXml webXml = new WebXml(doc); 1321 assertTrue(!webXml.hasSecurityRole("someRole")); 1322 assertTrue(!webXml.getSecurityRoleNames().hasNext()); 1323 } 1324 1325 1330 public void testGetSingleSecurityRole() 1331 throws Exception 1332 { 1333 String xml = "<web-app>" 1334 + " <security-role>".trim() 1335 + " <role-name>r1</role-name>".trim() 1336 + " </security-role>".trim() 1337 + "</web-app>"; 1338 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 1339 WebXml webXml = new WebXml(doc); 1340 assertTrue(webXml.hasSecurityRole("r1")); 1341 Element securityRoleElement = webXml.getSecurityRole("r1"); 1342 assertNotNull(securityRoleElement); 1343 assertEquals("security-role", securityRoleElement.getNodeName()); 1344 assertEquals("role-name", 1345 securityRoleElement.getFirstChild().getNodeName()); 1346 assertEquals("r1", 1347 securityRoleElement.getFirstChild().getFirstChild().getNodeValue()); 1348 Iterator securityRoleNames = webXml.getSecurityRoleNames(); 1349 assertTrue(securityRoleNames.hasNext()); 1350 assertEquals("r1", securityRoleNames.next()); 1351 assertTrue(!securityRoleNames.hasNext()); 1352 } 1353 1354 1360 public void testGetMutlipleSecurityRoles() 1361 throws Exception 1362 { 1363 String xml = "<web-app>" 1364 + " <security-role>".trim() 1365 + " <role-name>r1</role-name>".trim() 1366 + " </security-role>".trim() 1367 + " <security-role>".trim() 1368 + " <role-name>r2</role-name>".trim() 1369 + " </security-role>".trim() 1370 + " <security-role>".trim() 1371 + " <role-name>r3</role-name>".trim() 1372 + " </security-role>".trim() 1373 + "</web-app>"; 1374 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 1375 WebXml webXml = new WebXml(doc); 1376 assertTrue(webXml.hasSecurityRole("r1")); 1377 Element securityRoleElement1 = webXml.getSecurityRole("r1"); 1378 assertNotNull(securityRoleElement1); 1379 assertEquals("security-role", securityRoleElement1.getNodeName()); 1380 assertEquals("role-name", 1381 securityRoleElement1.getFirstChild().getNodeName()); 1382 assertEquals("r1", 1383 securityRoleElement1.getFirstChild().getFirstChild(). 1384 getNodeValue()); 1385 assertTrue(webXml.hasSecurityRole("r2")); 1386 Element securityRoleElement2 = webXml.getSecurityRole("r2"); 1387 assertNotNull(securityRoleElement2); 1388 assertEquals("security-role", securityRoleElement2.getNodeName()); 1389 assertEquals("role-name", 1390 securityRoleElement2.getFirstChild().getNodeName()); 1391 assertEquals("r2", 1392 securityRoleElement2.getFirstChild().getFirstChild(). 1393 getNodeValue()); 1394 assertTrue(webXml.hasSecurityRole("r3")); 1395 Element securityRoleElement3 = webXml.getSecurityRole("r3"); 1396 assertNotNull(securityRoleElement3); 1397 assertEquals("security-role", securityRoleElement3.getNodeName()); 1398 assertEquals("role-name", 1399 securityRoleElement3.getFirstChild().getNodeName()); 1400 assertEquals("r3", 1401 securityRoleElement3.getFirstChild().getFirstChild(). 1402 getNodeValue()); 1403 Iterator securityRoleNames = webXml.getSecurityRoleNames(); 1404 assertTrue(securityRoleNames.hasNext()); 1405 assertEquals("r1", securityRoleNames.next()); 1406 assertTrue(securityRoleNames.hasNext()); 1407 assertEquals("r2", securityRoleNames.next()); 1408 assertTrue(securityRoleNames.hasNext()); 1409 assertEquals("r3", securityRoleNames.next()); 1410 assertTrue(!securityRoleNames.hasNext()); 1411 } 1412 1413 1418 public void testElementOrderFilterBeforeServlet() throws Exception 1419 { 1420 String xml = "<web-app>" 1421 + " <servlet>".trim() 1422 + " <servlet-name>s1</servlet-name>".trim() 1423 + " <servlet-class>s1class</servlet-class>".trim() 1424 + " </servlet>".trim() 1425 + "</web-app>"; 1426 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 1427 WebXml webXml = new WebXml(doc); 1428 webXml.addFilter(createFilterElement(doc, "f1", "f1class")); 1429 NodeList order = doc.getDocumentElement().getChildNodes(); 1430 assertEquals("filter", order.item(0).getNodeName()); 1431 assertEquals("servlet", order.item(1).getNodeName()); 1432 } 1433 1434 1440 public void testElementOrderFilterBeforeServletWithComment() 1441 throws Exception 1442 { 1443 String xml = "<web-app>" 1444 + " <!-- My servlets -->".trim() 1445 + " <servlet>".trim() 1446 + " <servlet-name>s1</servlet-name>".trim() 1447 + " <servlet-class>s1class</servlet-class>".trim() 1448 + " </servlet>".trim() 1449 + "</web-app>"; 1450 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 1451 WebXml webXml = new WebXml(doc); 1452 webXml.addFilter(createFilterElement(doc, "f1", "f1class")); 1453 NodeList order = doc.getDocumentElement().getChildNodes(); 1454 assertEquals("filter", order.item(0).getNodeName()); 1455 assertEquals("#comment", order.item(1).getNodeName()); 1456 assertEquals("servlet", order.item(2).getNodeName()); 1457 } 1458 1459 1464 public void testElementOrderServletAfterFilter() throws Exception 1465 { 1466 String xml = "<web-app>" 1467 + " <filter>".trim() 1468 + " <filter-name>f1</filter-name>".trim() 1469 + " <filter-class>f1class</filter-class>".trim() 1470 + " </filter>".trim() 1471 + "</web-app>"; 1472 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 1473 WebXml webXml = new WebXml(doc); 1474 webXml.addServlet(createServletElement(doc, "s1", "s1class")); 1475 NodeList order = doc.getDocumentElement().getChildNodes(); 1476 assertEquals("filter", order.item(0).getNodeName()); 1477 assertEquals("servlet", order.item(1).getNodeName()); 1478 } 1479 1480 1486 public void testElementOrderServletAfterFilterWithComment() 1487 throws Exception 1488 { 1489 String xml = "<web-app>" 1490 + " <!-- My filters -->".trim() 1491 + " <filter>".trim() 1492 + " <filter-name>f1</filter-name>".trim() 1493 + " <filter-class>f1class</filter-class>".trim() 1494 + " </filter>".trim() 1495 + "</web-app>"; 1496 Document doc = builder.parse(new ByteArrayInputStream (xml.getBytes())); 1497 WebXml webXml = new WebXml(doc); 1498 webXml.addServlet(createServletElement(doc, "s1", "s1class")); 1499 NodeList order = doc.getDocumentElement().getChildNodes(); 1500 assertEquals("#comment", order.item(0).getNodeName()); 1501 assertEquals("filter", order.item(1).getNodeName()); 1502 assertEquals("servlet", order.item(2).getNodeName()); 1503 } 1504 1505 1507 1516 public Element createContextParamElement(Document theDocument, 1517 String theParamName, String theParamValue) 1518 { 1519 Element contextParamElement = 1520 theDocument.createElement("context-param"); 1521 Element paramNameElement = theDocument.createElement("param-name"); 1522 paramNameElement.appendChild(theDocument.createTextNode(theParamName)); 1523 contextParamElement.appendChild(paramNameElement); 1524 Element paramValueElement = theDocument.createElement("param-value"); 1525 paramValueElement.appendChild( 1526 theDocument.createTextNode(theParamValue)); 1527 contextParamElement.appendChild(paramValueElement); 1528 return contextParamElement; 1529 } 1530 1531 1540 public Element createFilterElement(Document theDocument, 1541 String theFilterName, String theFilterClass) 1542 { 1543 Element filterElement = theDocument.createElement("filter"); 1544 Element filterNameElement = theDocument.createElement("filter-name"); 1545 filterNameElement.appendChild( 1546 theDocument.createTextNode(theFilterName)); 1547 filterElement.appendChild(filterNameElement); 1548 Element filterClassElement = theDocument.createElement("filter-class"); 1549 filterClassElement.appendChild( 1550 theDocument.createTextNode(theFilterClass)); 1551 filterElement.appendChild(filterClassElement); 1552 return filterElement; 1553 } 1554 1555 1564 public Element createServletElement(Document theDocument, 1565 String theServletName, String theServletClass) 1566 { 1567 Element filterElement = theDocument.createElement("servlet"); 1568 Element filterNameElement = theDocument.createElement("servlet-name"); 1569 filterNameElement.appendChild( 1570 theDocument.createTextNode(theServletName)); 1571 filterElement.appendChild(filterNameElement); 1572 Element filterClassElement = theDocument.createElement("servlet-class"); 1573 filterClassElement.appendChild( 1574 theDocument.createTextNode(theServletClass)); 1575 filterElement.appendChild(filterClassElement); 1576 return filterElement; 1577 } 1578 1579} 1580 | Popular Tags |