001: /*
002: * ========================================================================
003: *
004: * Copyright 2003 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * ========================================================================
019: */
020: package org.apache.cactus.integration.ant;
021:
022: import java.io.File;
023: import java.util.Iterator;
024:
025: import org.apache.tools.ant.BuildException;
026: import org.apache.tools.ant.Project;
027: import org.codehaus.cargo.module.webapp.DefaultWarArchive;
028: import org.codehaus.cargo.module.webapp.WarArchive;
029: import org.codehaus.cargo.module.webapp.WebXml;
030: import org.codehaus.cargo.module.webapp.WebXmlTag;
031: import org.codehaus.cargo.module.webapp.WebXmlVersion;
032: import org.codehaus.cargo.module.webapp.weblogic.WeblogicXml;
033: import org.codehaus.cargo.module.webapp.weblogic.WeblogicXmlTag;
034: import org.w3c.dom.Element;
035: import org.w3c.dom.NodeList;
036:
037: /**
038: * Unit tests for {@link CactifyWarTask}.
039: *
040: * TODO: test whether all files contained by the source WAR are also added to
041: * the cactified WAR
042: * TODO: test whether the mergewebxml is actually merged into the cactified
043: * web.xml
044: *
045: * @version $Id: TestCactifyWarTask.java 239162 2005-04-26 09:57:59Z grimsell $
046: */
047: public final class TestCactifyWarTask extends AntTestCase {
048:
049: // Constructors ------------------------------------------------------------
050:
051: /**
052: * @see AntTestCase#AntTestCase
053: */
054: public TestCactifyWarTask() {
055: super ("org/apache/cactus/integration/ant/test-cactifywar.xml");
056: }
057:
058: // TestCase Implementation -------------------------------------------------
059:
060: /**
061: * @see junit.framework.TestCase#setUp()
062: */
063: protected void setUp() throws Exception {
064: super .setUp();
065:
066: getProject().addTaskDefinition("cactifywar",
067: CactifyWarTask.class);
068: }
069:
070: // Test Methods ------------------------------------------------------------
071:
072: /**
073: * Verifies that the task throws an exception when neither the srcfile
074: * nor the version attribute has been set.
075: *
076: * @throws Exception If an unexpected error occurs
077: */
078: public void testNeitherSrcFileNorVersionSet() throws Exception {
079: try {
080: executeTestTarget();
081: fail("Expected BuildException");
082: } catch (BuildException expected) {
083: assertEquals(
084: "You need to specify either the [srcfile] or the "
085: + "[version] attribute", expected
086: .getMessage());
087: }
088: }
089:
090: /**
091: * Verifies that the task throws an exception when the destfile attribute
092: * has not been set.
093: *
094: * @throws Exception If an unexpected error occurs
095: */
096: public void testDestFileNotSet() throws Exception {
097: try {
098: executeTestTarget();
099: fail("Expected BuildException");
100: } catch (BuildException expected) {
101: assertEquals("You must specify the war file to create!",
102: expected.getMessage());
103: }
104: }
105:
106: /**
107: * Verifies the error raised when the source archive does not contain a web
108: * deployment descriptor.
109: *
110: * @throws Exception If an unexpected error occurs
111: */
112: public void testSrcFileWithoutWebXml() throws Exception {
113: try {
114: executeTestTarget();
115: fail("Expected BuildException");
116: } catch (BuildException expected) {
117: assertEquals(
118: "You need to specify either the [srcfile] or the "
119: + "[version] attribute", expected
120: .getMessage());
121: }
122: }
123:
124: /**
125: * Verifies an empty web was created when the source archive does not
126: * contain a web deployment descriptor but specifies the version.
127: *
128: * @throws Exception If an unexpected error occurs
129: */
130: public void testSrcFileWithoutWebXmlNewWebXml22() throws Exception {
131: try {
132: executeTestTarget();
133: } catch (BuildException e) {
134: fail("The WAR source file does not contain a "
135: + "WEB-INF/web.xml deployment descriptor, but Cactus "
136: + "should have created an empty one");
137: }
138: }
139:
140: /**
141: * Verifies an empty web was created when the source archive does not
142: * contain a web deployment descriptor but specifies the version.
143: *
144: * @throws Exception If an unexpected error occurs
145: */
146: public void testSrcFileWithoutWebXmlNewWebXml23() throws Exception {
147: try {
148: executeTestTarget();
149: } catch (BuildException e) {
150: fail("The WAR source file does not contain a "
151: + "WEB-INF/web.xml deployment descriptor, but Cactus "
152: + "should have created an empty one");
153: }
154: }
155:
156: /**
157: * Tests whether the Cactus test redirectors are correctly added to the
158: * descriptor of the cactified WAR.
159: *
160: * @throws Exception If an unexpected error occurs
161: */
162: public void testDefaultRedirectorsNoDoctype() throws Exception {
163: executeTestTarget();
164:
165: File destFile = getProject().resolveFile("work/destfile.war");
166: WarArchive destWar = new DefaultWarArchive(destFile);
167: WebXml webXml = destWar.getWebXml();
168: assertNull("The web.xml should not have a version specified",
169: webXml.getVersion());
170: assertServletMapping(webXml,
171: "org.apache.cactus.server.ServletTestRedirector",
172: "ServletRedirector", "/ServletRedirector");
173: assertJspMapping(webXml, "/jspRedirector.jsp", "JspRedirector",
174: "/JspRedirector");
175: // As the deployment descriptor in the source WAR doesn't contain a
176: // DOCTYPE, it is assumed to be a version 2.2 descriptor. Thus it
177: // should not contain a definition of the filter test redirector.
178: // Assert that.
179: assertTrue(
180: "Filter test redirector should not have been defined",
181: !webXml.getFilterNames().hasNext());
182: }
183:
184: /**
185: * Tests whether the Cactus test redirectors are correctly added to the
186: * descriptor of the cactified WAR.
187: *
188: * @throws Exception If an unexpected error occurs
189: */
190: public void testDefaultRedirectors22Doctype() throws Exception {
191: executeTestTarget();
192:
193: File destFile = getProject().resolveFile("work/destfile.war");
194: WarArchive destWar = new DefaultWarArchive(destFile);
195: WebXml webXml = destWar.getWebXml();
196: assertEquals(WebXmlVersion.V2_2, webXml.getVersion());
197: assertServletMapping(webXml,
198: "org.apache.cactus.server.ServletTestRedirector",
199: "ServletRedirector", "/ServletRedirector");
200: assertJspMapping(webXml, "/jspRedirector.jsp", "JspRedirector",
201: "/JspRedirector");
202: assertTrue(
203: "Filter test redirector should not have been defined",
204: !webXml.getFilterNames().hasNext());
205: }
206:
207: /**
208: * Tests whether the Cactus test redirectors are correctly added to the
209: * descriptor of the cactified WAR.
210: *
211: * @throws Exception If an unexpected error occurs
212: */
213: public void testDefaultRedirectors23Doctype() throws Exception {
214: executeTestTarget();
215:
216: File destFile = getProject().resolveFile("work/destfile.war");
217: WarArchive destWar = new DefaultWarArchive(destFile);
218: WebXml webXml = destWar.getWebXml();
219: assertEquals(WebXmlVersion.V2_3, webXml.getVersion());
220: assertServletMapping(webXml,
221: "org.apache.cactus.server.ServletTestRedirector",
222: "ServletRedirector", "/ServletRedirector");
223: assertJspMapping(webXml, "/jspRedirector.jsp", "JspRedirector",
224: "/JspRedirector");
225: assertFilterMapping(webXml,
226: "org.apache.cactus.server.FilterTestRedirector",
227: "FilterRedirector", "/FilterRedirector");
228: }
229:
230: /**
231: * Tests whether the Cactus test redirectors are correctly added to the
232: * descriptor of a WAR when no srcfile attribute had been set, and the
233: * version has been set to 2.2.
234: *
235: * @throws Exception If an unexpected error occurs
236: */
237: public void testDefaultRedirectorsNewWar22() throws Exception {
238: executeTestTarget();
239:
240: File destFile = getProject().resolveFile("work/destfile.war");
241: WarArchive destWar = new DefaultWarArchive(destFile);
242: WebXml webXml = destWar.getWebXml();
243: assertEquals(WebXmlVersion.V2_2, webXml.getVersion());
244: assertServletMapping(webXml,
245: "org.apache.cactus.server.ServletTestRedirector",
246: "ServletRedirector", "/ServletRedirector");
247: assertJspMapping(webXml, "/jspRedirector.jsp", "JspRedirector",
248: "/JspRedirector");
249: assertTrue(
250: "Filter test redirector should not have been defined",
251: !webXml.getFilterNames().hasNext());
252: }
253:
254: /**
255: * Tests whether the Cactus test redirectors are correctly added to the
256: * descriptor of a WAR when no srcfile attribute had been set, and the
257: * version has been set to 2.3.
258: *
259: * @throws Exception If an unexpected error occurs
260: */
261: public void testDefaultRedirectorsNewWar23() throws Exception {
262: executeTestTarget();
263:
264: File destFile = getProject().resolveFile("work/destfile.war");
265: WarArchive destWar = new DefaultWarArchive(destFile);
266: WebXml webXml = destWar.getWebXml();
267: assertEquals(WebXmlVersion.V2_3, webXml.getVersion());
268: assertServletMapping(webXml,
269: "org.apache.cactus.server.ServletTestRedirector",
270: "ServletRedirector", "/ServletRedirector");
271: assertJspMapping(webXml, "/jspRedirector.jsp", "JspRedirector",
272: "/JspRedirector");
273: assertFilterMapping(webXml,
274: "org.apache.cactus.server.FilterTestRedirector",
275: "FilterRedirector", "/FilterRedirector");
276: }
277:
278: /**
279: * Verifies that the mapping of the servlet redirector is correctly
280: * overridden by a nested 'servletredirector' element.
281: *
282: * @throws Exception If an unexpected error occurs
283: */
284: public void testCustomServletRedirectorMapping() throws Exception {
285: executeTestTarget();
286:
287: File destFile = getProject().resolveFile("work/destfile.war");
288: WarArchive destWar = new DefaultWarArchive(destFile);
289: WebXml webXml = destWar.getWebXml();
290: assertServletMapping(webXml,
291: "org.apache.cactus.server.ServletTestRedirector",
292: "ServletRedirector", "/test/servletRedirector");
293: }
294:
295: /**
296: * Verifies that the mapping of the JSP redirector is correctly overridden
297: * by a nested 'jspredirector' element.
298: *
299: * @throws Exception If an unexpected error occurs
300: */
301: public void testCustomJspRedirectorMapping() throws Exception {
302: executeTestTarget();
303:
304: File destFile = getProject().resolveFile("work/destfile.war");
305: WarArchive destWar = new DefaultWarArchive(destFile);
306: WebXml webXml = destWar.getWebXml();
307: assertJspMapping(webXml, "/jspRedirector.jsp", "JspRedirector",
308: "/test/jspRedirector");
309: }
310:
311: /**
312: * Verifies that the mapping of the filter redirector is correctly
313: * overridden by a nested 'filterredirector' element.
314: *
315: * @throws Exception If an unexpected error occurs
316: */
317: public void testCustomFilterRedirectorMapping() throws Exception {
318: executeTestTarget();
319:
320: File destFile = getProject().resolveFile("work/destfile.war");
321: WarArchive destWar = new DefaultWarArchive(destFile);
322: WebXml webXml = destWar.getWebXml();
323: assertFilterMapping(webXml,
324: "org.apache.cactus.server.FilterTestRedirector",
325: "FilterRedirector", "/test/filterRedirector");
326: }
327:
328: /**
329: * Verifies that no definition of the filter redirector is added to a
330: * Servlet 2.2 descriptor, even when explicitly requested by a nested
331: * 'filterredirector' element.
332: *
333: * @throws Exception If an unexpected error occurs
334: */
335: public void testCustomFilterRedirectorMappingIgnored()
336: throws Exception {
337: executeTestTarget();
338:
339: File destFile = getProject().resolveFile("work/destfile.war");
340: WarArchive destWar = new DefaultWarArchive(destFile);
341: WebXml webXml = destWar.getWebXml();
342: assertTrue(
343: "The filter redirector should not have been defined",
344: !webXml
345: .getFilterNamesForClass(
346: "org.apache.cactus.server.FilterTestRedirector")
347: .hasNext());
348: }
349:
350: /**
351: * Verifies that two servlet redirectors with different names and mappings
352: * are added as expected.
353: *
354: * @throws Exception If an unexpected error occurs
355: */
356: public void testMultipleNamedServletRedirectors() throws Exception {
357: executeTestTarget();
358:
359: File destFile = getProject().resolveFile("work/destfile.war");
360: WarArchive destWar = new DefaultWarArchive(destFile);
361: WebXml webXml = destWar.getWebXml();
362: assertTrue(webXml.hasServlet("ServletRedirector"));
363: assertEquals("/test/ServletRedirector", webXml
364: .getServletMappings("ServletRedirector").next());
365: assertTrue(webXml.hasServlet("ServletRedirectorSecure"));
366: assertEquals("/test/ServletRedirectorSecure", webXml
367: .getServletMappings("ServletRedirectorSecure").next());
368: }
369:
370: /**
371: * Verifies that two JSP redirectors with different names and mappings
372: * are added as expected.
373: *
374: * @throws Exception If an unexpected error occurs
375: */
376: public void testMultipleNamedJspRedirectors() throws Exception {
377: executeTestTarget();
378:
379: File destFile = getProject().resolveFile("work/destfile.war");
380: WarArchive destWar = new DefaultWarArchive(destFile);
381: WebXml webXml = destWar.getWebXml();
382: assertTrue(webXml.hasServlet("JspRedirector"));
383: assertEquals("/test/JspRedirector", webXml.getServletMappings(
384: "JspRedirector").next());
385: assertTrue(webXml.hasServlet("JspRedirectorSecure"));
386: assertEquals("/test/JspRedirectorSecure", webXml
387: .getServletMappings("JspRedirectorSecure").next());
388: }
389:
390: /**
391: * Verifies that two filter redirectors with different names and mappings
392: * are added as expected.
393: *
394: * @throws Exception If an unexpected error occurs
395: */
396: public void testMultipleNamedFilterRedirectors() throws Exception {
397: executeTestTarget();
398:
399: File destFile = getProject().resolveFile("work/destfile.war");
400: WarArchive destWar = new DefaultWarArchive(destFile);
401: WebXml webXml = destWar.getWebXml();
402: assertTrue(webXml.hasFilter("FilterRedirector"));
403: assertEquals("/test/FilterRedirector", webXml
404: .getFilterMappings("FilterRedirector").next());
405: assertTrue(webXml.hasFilter("FilterRedirectorSecure"));
406: assertEquals("/test/FilterRedirectorSecure", webXml
407: .getFilterMappings("FilterRedirectorSecure").next());
408: }
409:
410: /**
411: * Verifies that a secured servlet redirector gets added alongside the role
412: * names.
413: *
414: * @throws Exception If an unexpected error occurs
415: */
416: public void testSecuredServletRedirector() throws Exception {
417: executeTestTarget();
418:
419: File destFile = getProject().resolveFile("work/destfile.war");
420: WarArchive destWar = new DefaultWarArchive(destFile);
421: WebXml webXml = destWar.getWebXml();
422: assertTrue(webXml.hasServlet("ServletRedirectorSecure"));
423: assertEquals("/ServletRedirectorSecure", webXml
424: .getServletMappings("ServletRedirectorSecure").next());
425: assertTrue(webXml.hasSecurityRole("test"));
426: assertTrue(webXml.hasSecurityRole("cactus"));
427: assertTrue(webXml
428: .hasSecurityConstraint("/ServletRedirectorSecure"));
429: Element securityConstraintElement = webXml
430: .getSecurityConstraint("/ServletRedirectorSecure");
431: assertNotNull(securityConstraintElement);
432: Element authConstraintElement = (Element) securityConstraintElement
433: .getElementsByTagName("auth-constraint").item(0);
434: assertNotNull(authConstraintElement);
435: NodeList roleNameElements = authConstraintElement
436: .getElementsByTagName("role-name");
437: assertEquals(2, roleNameElements.getLength());
438: assertEquals("test", roleNameElements.item(0).getChildNodes()
439: .item(0).getNodeValue());
440: assertEquals("cactus", roleNameElements.item(1).getChildNodes()
441: .item(0).getNodeValue());
442: Iterator loginConfigElements = webXml
443: .getElements(WebXmlTag.LOGIN_CONFIG);
444: assertTrue(loginConfigElements.hasNext());
445: Element loginConfigElement = (Element) loginConfigElements
446: .next();
447: Element authMethodElement = (Element) loginConfigElement
448: .getElementsByTagName("auth-method").item(0);
449: assertEquals("BASIC", authMethodElement.getChildNodes().item(0)
450: .getNodeValue());
451: }
452:
453: /**
454: * Verifies that a already existent login configuration does not get
455: * replaced by the default BASIC login configuration when secured
456: * redirectors are defined.
457: *
458: * @throws Exception If an unexpected error occurs
459: */
460: public void testLoginConfigNotOverwritten() throws Exception {
461: executeTestTarget();
462:
463: File destFile = getProject().resolveFile("work/destfile.war");
464: WarArchive destWar = new DefaultWarArchive(destFile);
465: WebXml webXml = destWar.getWebXml();
466: assertEquals("FORM", webXml.getLoginConfigAuthMethod());
467: }
468:
469: /**
470: * Verifies that JARs already contained by the source archive are not added
471: * again.
472: *
473: * @throws Exception If an unexpected error occurs
474: */
475: public void testNoDuplicateJars() throws Exception {
476: executeTestTarget();
477:
478: assertMessageLogged(
479: "The AspectJ Runtime JAR is already present in the "
480: + "WAR", Project.MSG_VERBOSE);
481: assertMessageLogged(
482: "The Cactus Framework JAR is already present in "
483: + "the WAR", Project.MSG_VERBOSE);
484: assertMessageLogged(
485: "The Commons-Logging JAR is already present in the "
486: + "WAR", Project.MSG_VERBOSE);
487: assertMessageLogged(
488: "The Commons-HttpClient JAR is already present in "
489: + "the WAR", Project.MSG_VERBOSE);
490: assertMessageLogged(
491: "The JUnit JAR is already present in the WAR",
492: Project.MSG_VERBOSE);
493: }
494:
495: /**
496: * Tests that ejb refs can be added for weblogic
497: *
498: * @throws Exception iIf an unexpected error occurs
499: */
500: public void testAddWeblogicEjbRefs() throws Exception {
501: executeTestTarget();
502:
503: File destFile = getProject().resolveFile("work/destfile.war");
504: WarArchive destWar = new DefaultWarArchive(destFile);
505:
506: // test web.xml
507: WebXml webXml = destWar.getWebXml();
508: Iterator i = webXml.getElements(WebXmlTag.EJB_LOCAL_REF);
509: Element e = (Element) i.next();
510: NodeList nl = e.getElementsByTagName("ejb-ref-name");
511: Element f = (Element) nl.item(0);
512: assertEquals("MyEjb", f.getFirstChild().getNodeValue());
513: nl = e.getElementsByTagName("ejb-ref-type");
514: f = (Element) nl.item(0);
515: assertEquals("Session", f.getFirstChild().getNodeValue());
516: nl = e.getElementsByTagName("local-home");
517: f = (Element) nl.item(0);
518: assertEquals("com.wombat.MyEjbHome", f.getFirstChild()
519: .getNodeValue());
520: nl = e.getElementsByTagName("local");
521: f = (Element) nl.item(0);
522: assertEquals("com.wombat.MyEjb", f.getFirstChild()
523: .getNodeValue());
524:
525: // test weblogic.xml
526: WeblogicXml weblogicXml = (WeblogicXml) webXml
527: .getVendorDescriptor();
528: i = weblogicXml
529: .getElements(WeblogicXmlTag.EJB_REFERENCE_DESCRIPTION);
530: e = (Element) i.next();
531: nl = e.getElementsByTagName("ejb-ref-name");
532: f = (Element) nl.item(0);
533: assertEquals("MyEjb", f.getFirstChild().getNodeValue());
534: nl = e.getElementsByTagName("jndi-name");
535: f = (Element) nl.item(0);
536: assertEquals("/wombat/MyEjb", f.getFirstChild().getNodeValue());
537: }
538:
539: // Private Methods ---------------------------------------------------------
540:
541: /**
542: * Asserts that a filter of the specified class is defined in the given
543: * deployment descriptor and mapped to a specific URL-pattern.
544: *
545: * @param theWebXml The deployment descriptor
546: * @param theFilterClass The name of the filter class
547: * @param theFilterName The name of the filter
548: * @param theMapping The URL-pattern that the filter is expected to be
549: * mapped to
550: */
551: private void assertFilterMapping(WebXml theWebXml,
552: String theFilterClass, String theFilterName,
553: String theMapping) {
554: Iterator names = theWebXml
555: .getFilterNamesForClass(theFilterClass);
556:
557: // Look for the definition that matches the JSP servlet name
558: boolean found = false;
559: String name = null;
560: while (names.hasNext()) {
561: name = (String) names.next();
562: if (name.equals(theFilterName)) {
563: found = true;
564: break;
565: }
566: }
567:
568: if (!found) {
569: fail("Definition of [" + theFilterClass + "("
570: + theFilterName + ")] not found");
571: }
572:
573: Iterator mappings = theWebXml.getFilterMappings(name);
574: assertTrue("Mapping for [" + theFilterClass + "("
575: + theFilterName + ")] not found", mappings.hasNext());
576: assertEquals(theMapping, mappings.next());
577: }
578:
579: /**
580: * Asserts that the specified JSP file is defined in the given deployment
581: * descriptor and mapped to a specific URL-pattern.
582: *
583: * @param theWebXml The deployment descriptor
584: * @param theJspFile The JSP file name
585: * @param theJspName The JSP servlet name
586: * @param theMapping The URL-pattern that the JSP file is expected to be
587: * mapped to
588: */
589: private void assertJspMapping(WebXml theWebXml, String theJspFile,
590: String theJspName, String theMapping) {
591: Iterator names = theWebXml
592: .getServletNamesForJspFile(theJspFile);
593:
594: // Look for the definition that matches the JSP servlet name
595: boolean found = false;
596: String name = null;
597: while (names.hasNext()) {
598: name = (String) names.next();
599: if (name.equals(theJspName)) {
600: found = true;
601: break;
602: }
603: }
604:
605: if (!found) {
606: fail("Definition of [" + theJspFile + "(" + theJspName
607: + ")] not found");
608: }
609:
610: Iterator mappings = theWebXml.getServletMappings(name);
611: assertTrue("Mapping for [" + theJspFile + "(" + theJspName
612: + ")] not found", mappings.hasNext());
613: assertEquals(theMapping, mappings.next());
614: }
615:
616: /**
617: * Asserts that a servlet of the specified name is defined in the given
618: * deployment descriptor and mapped to a specific URL-pattern.
619: *
620: * @param theWebXml The deployment descriptor
621: * @param theServletClass The name of servlet class
622: * @param theServletName The name of the servlet
623: * @param theMapping The URL-pattern that the servlet is expected to be
624: * mapped to
625: */
626: private void assertServletMapping(WebXml theWebXml,
627: String theServletClass, String theServletName,
628: String theMapping) {
629: Iterator names = theWebXml
630: .getServletNamesForClass(theServletClass);
631:
632: // Look for the definition that matches the servlet name
633: boolean found = false;
634: String name = null;
635: while (names.hasNext()) {
636: name = (String) names.next();
637: if (name.equals(theServletName)) {
638: found = true;
639: break;
640: }
641: }
642:
643: if (!found) {
644: fail("Definition of [" + theServletClass + "("
645: + theServletName + ")] not found");
646: }
647:
648: Iterator mappings = theWebXml.getServletMappings(name);
649: assertTrue("Mapping for [" + theServletClass + "("
650: + theServletName + ")] not found", mappings.hasNext());
651: assertEquals(theMapping, mappings.next());
652: }
653:
654: }
|