1 20 package org.apache.cactus.integration.ant; 21 22 import java.io.File ; 23 import java.util.Iterator ; 24 25 import org.apache.cactus.integration.ant.deployment.webapp.DefaultWarArchive; 26 import org.apache.cactus.integration.ant.deployment.webapp.WarArchive; 27 import org.apache.cactus.integration.ant.deployment.webapp.WebXml; 28 import org.apache.cactus.integration.ant.deployment.webapp.WebXmlTag; 29 import org.apache.cactus.integration.ant.deployment.webapp.WebXmlVersion; 30 import org.apache.tools.ant.BuildException; 31 import org.apache.tools.ant.Project; 32 import org.w3c.dom.Element ; 33 import org.w3c.dom.NodeList ; 34 35 45 public final class TestCactifyWarTask extends AntTestCase 46 { 47 48 50 53 public TestCactifyWarTask() 54 { 55 super("org/apache/cactus/integration/ant/test-cactifywar.xml"); 56 } 57 58 60 63 protected void setUp() throws Exception 64 { 65 super.setUp(); 66 67 getProject().addTaskDefinition("cactifywar", CactifyWarTask.class); 68 } 69 70 72 78 public void testNeitherSrcFileNorVersionSet() throws Exception 79 { 80 try 81 { 82 executeTestTarget(); 83 fail("Expected BuildException"); 84 } 85 catch (BuildException expected) 86 { 87 assertEquals("You need to specify either the [srcfile] or the " 88 + "[version] attribute", expected.getMessage()); 89 } 90 } 91 92 98 public void testDestFileNotSet() throws Exception 99 { 100 try 101 { 102 executeTestTarget(); 103 fail("Expected BuildException"); 104 } 105 catch (BuildException expected) 106 { 107 assertEquals("You must specify the war file to create!", 108 expected.getMessage()); 109 } 110 } 111 112 118 public void testSrcFileWithoutWebXml() throws Exception 119 { 120 try 121 { 122 executeTestTarget(); 123 fail("Expected BuildException"); 124 } 125 catch (BuildException expected) 126 { 127 assertEquals("The WAR source file does not contain a " 128 + "WEB-INF/web.xml deployment descriptor", 129 expected.getMessage()); 130 } 131 } 132 133 139 public void testDefaultRedirectorsNoDoctype() throws Exception 140 { 141 executeTestTarget(); 142 143 File destFile = getProject().resolveFile("work/destfile.war"); 144 WarArchive destWar = new DefaultWarArchive(destFile); 145 WebXml webXml = destWar.getWebXml(); 146 assertNull("The web.xml should not have a version specified", 147 webXml.getVersion()); 148 assertServletMapping(webXml, 149 "org.apache.cactus.server.ServletTestRedirector", 150 "ServletRedirector", 151 "/ServletRedirector"); 152 assertJspMapping(webXml, "/jspRedirector.jsp", "JspRedirector", 153 "/JspRedirector"); 154 assertTrue("Filter test redirector should not have been defined", 159 !webXml.getFilterNames().hasNext()); 160 } 161 162 168 public void testDefaultRedirectors22Doctype() throws Exception 169 { 170 executeTestTarget(); 171 172 File destFile = getProject().resolveFile("work/destfile.war"); 173 WarArchive destWar = new DefaultWarArchive(destFile); 174 WebXml webXml = destWar.getWebXml(); 175 assertEquals(WebXmlVersion.V2_2, webXml.getVersion()); 176 assertServletMapping(webXml, 177 "org.apache.cactus.server.ServletTestRedirector", 178 "ServletRedirector", 179 "/ServletRedirector"); 180 assertJspMapping(webXml, "/jspRedirector.jsp", "JspRedirector", 181 "/JspRedirector"); 182 assertTrue("Filter test redirector should not have been defined", 183 !webXml.getFilterNames().hasNext()); 184 } 185 186 192 public void testDefaultRedirectors23Doctype() throws Exception 193 { 194 executeTestTarget(); 195 196 File destFile = getProject().resolveFile("work/destfile.war"); 197 WarArchive destWar = new DefaultWarArchive(destFile); 198 WebXml webXml = destWar.getWebXml(); 199 assertEquals(WebXmlVersion.V2_3, webXml.getVersion()); 200 assertServletMapping(webXml, 201 "org.apache.cactus.server.ServletTestRedirector", 202 "ServletRedirector", 203 "/ServletRedirector"); 204 assertJspMapping(webXml, "/jspRedirector.jsp", "JspRedirector", 205 "/JspRedirector"); 206 assertFilterMapping(webXml, 207 "org.apache.cactus.server.FilterTestRedirector", 208 "FilterRedirector", 209 "/FilterRedirector"); 210 } 211 212 219 public void testDefaultRedirectorsNewWar22() throws Exception 220 { 221 executeTestTarget(); 222 223 File destFile = getProject().resolveFile("work/destfile.war"); 224 WarArchive destWar = new DefaultWarArchive(destFile); 225 WebXml webXml = destWar.getWebXml(); 226 assertEquals(WebXmlVersion.V2_2, webXml.getVersion()); 227 assertServletMapping(webXml, 228 "org.apache.cactus.server.ServletTestRedirector", 229 "ServletRedirector", 230 "/ServletRedirector"); 231 assertJspMapping(webXml, "/jspRedirector.jsp", "JspRedirector", 232 "/JspRedirector"); 233 assertTrue("Filter test redirector should not have been defined", 234 !webXml.getFilterNames().hasNext()); 235 } 236 237 244 public void testDefaultRedirectorsNewWar23() throws Exception 245 { 246 executeTestTarget(); 247 248 File destFile = getProject().resolveFile("work/destfile.war"); 249 WarArchive destWar = new DefaultWarArchive(destFile); 250 WebXml webXml = destWar.getWebXml(); 251 assertEquals(WebXmlVersion.V2_3, webXml.getVersion()); 252 assertServletMapping(webXml, 253 "org.apache.cactus.server.ServletTestRedirector", 254 "ServletRedirector", 255 "/ServletRedirector"); 256 assertJspMapping(webXml, "/jspRedirector.jsp", "JspRedirector", 257 "/JspRedirector"); 258 assertFilterMapping(webXml, 259 "org.apache.cactus.server.FilterTestRedirector", 260 "FilterRedirector", 261 "/FilterRedirector"); 262 } 263 264 270 public void testCustomServletRedirectorMapping() throws Exception 271 { 272 executeTestTarget(); 273 274 File destFile = getProject().resolveFile("work/destfile.war"); 275 WarArchive destWar = new DefaultWarArchive(destFile); 276 WebXml webXml = destWar.getWebXml(); 277 assertServletMapping(webXml, 278 "org.apache.cactus.server.ServletTestRedirector", 279 "ServletRedirector", 280 "/test/servletRedirector"); 281 } 282 283 289 public void testCustomJspRedirectorMapping() throws Exception 290 { 291 executeTestTarget(); 292 293 File destFile = getProject().resolveFile("work/destfile.war"); 294 WarArchive destWar = new DefaultWarArchive(destFile); 295 WebXml webXml = destWar.getWebXml(); 296 assertJspMapping(webXml, "/jspRedirector.jsp", "JspRedirector", 297 "/test/jspRedirector"); 298 } 299 300 306 public void testCustomFilterRedirectorMapping() throws Exception 307 { 308 executeTestTarget(); 309 310 File destFile = getProject().resolveFile("work/destfile.war"); 311 WarArchive destWar = new DefaultWarArchive(destFile); 312 WebXml webXml = destWar.getWebXml(); 313 assertFilterMapping(webXml, 314 "org.apache.cactus.server.FilterTestRedirector", 315 "FilterRedirector", 316 "/test/filterRedirector"); 317 } 318 319 326 public void testCustomFilterRedirectorMappingIgnored() throws Exception 327 { 328 executeTestTarget(); 329 330 File destFile = getProject().resolveFile("work/destfile.war"); 331 WarArchive destWar = new DefaultWarArchive(destFile); 332 WebXml webXml = destWar.getWebXml(); 333 assertTrue("The filter redirector should not have been defined", 334 !webXml.getFilterNamesForClass( 335 "org.apache.cactus.server.FilterTestRedirector").hasNext()); 336 } 337 338 344 public void testMultipleNamedServletRedirectors() throws Exception 345 { 346 executeTestTarget(); 347 348 File destFile = getProject().resolveFile("work/destfile.war"); 349 WarArchive destWar = new DefaultWarArchive(destFile); 350 WebXml webXml = destWar.getWebXml(); 351 assertTrue(webXml.hasServlet("ServletRedirector")); 352 assertEquals("/test/ServletRedirector", 353 webXml.getServletMappings("ServletRedirector").next()); 354 assertTrue(webXml.hasServlet("ServletRedirectorSecure")); 355 assertEquals("/test/ServletRedirectorSecure", 356 webXml.getServletMappings("ServletRedirectorSecure").next()); 357 } 358 359 365 public void testMultipleNamedJspRedirectors() throws Exception 366 { 367 executeTestTarget(); 368 369 File destFile = getProject().resolveFile("work/destfile.war"); 370 WarArchive destWar = new DefaultWarArchive(destFile); 371 WebXml webXml = destWar.getWebXml(); 372 assertTrue(webXml.hasServlet("JspRedirector")); 373 assertEquals("/test/JspRedirector", 374 webXml.getServletMappings("JspRedirector").next()); 375 assertTrue(webXml.hasServlet("JspRedirectorSecure")); 376 assertEquals("/test/JspRedirectorSecure", 377 webXml.getServletMappings("JspRedirectorSecure").next()); 378 } 379 380 386 public void testMultipleNamedFilterRedirectors() throws Exception 387 { 388 executeTestTarget(); 389 390 File destFile = getProject().resolveFile("work/destfile.war"); 391 WarArchive destWar = new DefaultWarArchive(destFile); 392 WebXml webXml = destWar.getWebXml(); 393 assertTrue(webXml.hasFilter("FilterRedirector")); 394 assertEquals("/test/FilterRedirector", 395 webXml.getFilterMappings("FilterRedirector").next()); 396 assertTrue(webXml.hasFilter("FilterRedirectorSecure")); 397 assertEquals("/test/FilterRedirectorSecure", 398 webXml.getFilterMappings("FilterRedirectorSecure").next()); 399 } 400 401 407 public void testSecuredServletRedirector() throws Exception 408 { 409 executeTestTarget(); 410 411 File destFile = getProject().resolveFile("work/destfile.war"); 412 WarArchive destWar = new DefaultWarArchive(destFile); 413 WebXml webXml = destWar.getWebXml(); 414 assertTrue(webXml.hasServlet("ServletRedirectorSecure")); 415 assertEquals("/ServletRedirectorSecure", 416 webXml.getServletMappings("ServletRedirectorSecure").next()); 417 assertTrue(webXml.hasSecurityRole("test")); 418 assertTrue(webXml.hasSecurityRole("cactus")); 419 assertTrue(webXml.hasSecurityConstraint("/ServletRedirectorSecure")); 420 Element securityConstraintElement = 421 webXml.getSecurityConstraint("/ServletRedirectorSecure"); 422 assertNotNull(securityConstraintElement); 423 Element authConstraintElement = (Element ) 424 securityConstraintElement.getElementsByTagName( 425 "auth-constraint").item(0); 426 assertNotNull(authConstraintElement); 427 NodeList roleNameElements = 428 authConstraintElement.getElementsByTagName("role-name"); 429 assertEquals(2, roleNameElements.getLength()); 430 assertEquals("test", 431 roleNameElements.item(0).getChildNodes().item(0).getNodeValue()); 432 assertEquals("cactus", 433 roleNameElements.item(1).getChildNodes().item(0).getNodeValue()); 434 Iterator loginConfigElements = 435 webXml.getElements(WebXmlTag.LOGIN_CONFIG); 436 assertTrue(loginConfigElements.hasNext()); 437 Element loginConfigElement = (Element ) loginConfigElements.next(); 438 Element authMethodElement = (Element ) 439 loginConfigElement.getElementsByTagName("auth-method").item(0); 440 assertEquals("BASIC", 441 authMethodElement.getChildNodes().item(0).getNodeValue()); 442 } 443 444 451 public void testLoginConfigNotOverwritten() throws Exception 452 { 453 executeTestTarget(); 454 455 File destFile = getProject().resolveFile("work/destfile.war"); 456 WarArchive destWar = new DefaultWarArchive(destFile); 457 WebXml webXml = destWar.getWebXml(); 458 assertEquals("FORM", webXml.getLoginConfigAuthMethod()); 459 } 460 461 467 public void testNoDuplicateJars() throws Exception 468 { 469 executeTestTarget(); 470 471 assertMessageLogged("The AspectJ Runtime JAR is already present in the " 472 + "WAR", Project.MSG_VERBOSE); 473 assertMessageLogged("The Cactus Framework JAR is already present in " 474 + "the WAR", Project.MSG_VERBOSE); 475 assertMessageLogged("The Commons-Logging JAR is already present in the " 476 + "WAR", Project.MSG_VERBOSE); 477 assertMessageLogged("The Commons-HttpClient JAR is already present in " 478 + "the WAR", Project.MSG_VERBOSE); 479 assertMessageLogged("The JUnit JAR is already present in the WAR", 480 Project.MSG_VERBOSE); 481 } 482 483 485 495 private void assertFilterMapping(WebXml theWebXml, String theFilterClass, 496 String theFilterName, String theMapping) 497 { 498 Iterator names = theWebXml.getFilterNamesForClass(theFilterClass); 499 500 boolean found = false; 502 String name = null; 503 while (names.hasNext()) 504 { 505 name = (String ) names.next(); 506 if (name.equals(theFilterName)) 507 { 508 found = true; 509 break; 510 } 511 } 512 513 if (!found) 514 { 515 fail("Definition of [" + theFilterClass + "(" + theFilterName 516 + ")] not found"); 517 } 518 519 Iterator mappings = theWebXml.getFilterMappings(name); 520 assertTrue("Mapping for [" + theFilterClass + "(" + theFilterName 521 + ")] not found", mappings.hasNext()); 522 assertEquals(theMapping, mappings.next()); 523 } 524 525 535 private void assertJspMapping(WebXml theWebXml, String theJspFile, 536 String theJspName, String theMapping) 537 { 538 Iterator names = theWebXml.getServletNamesForJspFile(theJspFile); 539 540 boolean found = false; 542 String name = null; 543 while (names.hasNext()) 544 { 545 name = (String ) names.next(); 546 if (name.equals(theJspName)) 547 { 548 found = true; 549 break; 550 } 551 } 552 553 if (!found) 554 { 555 fail("Definition of [" + theJspFile + "(" + theJspName 556 + ")] not found"); 557 } 558 559 Iterator mappings = theWebXml.getServletMappings(name); 560 assertTrue("Mapping for [" + theJspFile + "(" + theJspName 561 + ")] not found", mappings.hasNext()); 562 assertEquals(theMapping, mappings.next()); 563 } 564 565 575 private void assertServletMapping(WebXml theWebXml, String theServletClass, 576 String theServletName, String theMapping) 577 { 578 Iterator names = theWebXml.getServletNamesForClass(theServletClass); 579 580 boolean found = false; 582 String name = null; 583 while (names.hasNext()) 584 { 585 name = (String ) names.next(); 586 if (name.equals(theServletName)) 587 { 588 found = true; 589 break; 590 } 591 } 592 593 if (!found) 594 { 595 fail("Definition of [" + theServletClass + "(" + theServletName 596 + ")] not found"); 597 } 598 599 Iterator mappings = theWebXml.getServletMappings(name); 600 assertTrue("Mapping for [" + theServletClass + "(" + theServletName 601 + ")] not found", mappings.hasNext()); 602 assertEquals(theMapping, mappings.next()); 603 } 604 605 } 606 | Popular Tags |