KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > integration > ant > TestCactifyWarTask


1 /*
2  * ========================================================================
3  *
4  * Copyright 2003 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * ========================================================================
19  */

20 package org.apache.cactus.integration.ant;
21
22 import java.io.File JavaDoc;
23 import java.util.Iterator JavaDoc;
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 JavaDoc;
33 import org.w3c.dom.NodeList JavaDoc;
34
35 /**
36  * Unit tests for {@link CactifyWarTask}.
37  *
38  * TODO: test whether all files contained by the source WAR are also added to
39  * the cactified WAR
40  * TODO: test whether the mergewebxml is actually merged into the cactified
41  * web.xml
42  *
43  * @version $Id: TestCactifyWarTask.java,v 1.21 2004/05/31 20:05:23 vmassol Exp $
44  */

45 public final class TestCactifyWarTask extends AntTestCase
46 {
47
48     // Constructors ------------------------------------------------------------
49

50     /**
51      * @see AntTestCase#AntTestCase
52      */

53     public TestCactifyWarTask()
54     {
55         super("org/apache/cactus/integration/ant/test-cactifywar.xml");
56     }
57
58     // TestCase Implementation -------------------------------------------------
59

60     /**
61      * @see junit.framework.TestCase#setUp()
62      */

63     protected void setUp() throws Exception JavaDoc
64     {
65         super.setUp();
66
67         getProject().addTaskDefinition("cactifywar", CactifyWarTask.class);
68     }
69
70     // Test Methods ------------------------------------------------------------
71

72     /**
73      * Verifies that the task throws an exception when neither the srcfile
74      * nor the version attribute has been set.
75      *
76      * @throws Exception If an unexpected error occurs
77      */

78     public void testNeitherSrcFileNorVersionSet() throws Exception JavaDoc
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     /**
93      * Verifies that the task throws an exception when the destfile attribute
94      * has not been set.
95      *
96      * @throws Exception If an unexpected error occurs
97      */

98     public void testDestFileNotSet() throws Exception JavaDoc
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     /**
113      * Verifies the error raised when the source archive does not contain a web
114      * deployment descriptor.
115      *
116      * @throws Exception If an unexpected error occurs
117      */

118     public void testSrcFileWithoutWebXml() throws Exception JavaDoc
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     /**
134      * Tests whether the Cactus test redirectors are correctly added to the
135      * descriptor of the cactified WAR.
136      *
137      * @throws Exception If an unexpected error occurs
138      */

139     public void testDefaultRedirectorsNoDoctype() throws Exception JavaDoc
140     {
141         executeTestTarget();
142
143         File JavaDoc 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         // As the deployment descriptor in the source WAR doesn't contain a
155
// DOCTYPE, it is assumed to be a version 2.2 descriptor. Thus it
156
// should not contain a definition of the filter test redirector.
157
// Assert that.
158
assertTrue("Filter test redirector should not have been defined",
159             !webXml.getFilterNames().hasNext());
160     }
161
162     /**
163      * Tests whether the Cactus test redirectors are correctly added to the
164      * descriptor of the cactified WAR.
165      *
166      * @throws Exception If an unexpected error occurs
167      */

168     public void testDefaultRedirectors22Doctype() throws Exception JavaDoc
169     {
170         executeTestTarget();
171
172         File JavaDoc 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     /**
187      * Tests whether the Cactus test redirectors are correctly added to the
188      * descriptor of the cactified WAR.
189      *
190      * @throws Exception If an unexpected error occurs
191      */

192     public void testDefaultRedirectors23Doctype() throws Exception JavaDoc
193     {
194         executeTestTarget();
195
196         File JavaDoc 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     /**
213      * Tests whether the Cactus test redirectors are correctly added to the
214      * descriptor of a WAR when no srcfile attribute had been set, and the
215      * version has been set to 2.2.
216      *
217      * @throws Exception If an unexpected error occurs
218      */

219     public void testDefaultRedirectorsNewWar22() throws Exception JavaDoc
220     {
221         executeTestTarget();
222
223         File JavaDoc 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     /**
238      * Tests whether the Cactus test redirectors are correctly added to the
239      * descriptor of a WAR when no srcfile attribute had been set, and the
240      * version has been set to 2.3.
241      *
242      * @throws Exception If an unexpected error occurs
243      */

244     public void testDefaultRedirectorsNewWar23() throws Exception JavaDoc
245     {
246         executeTestTarget();
247
248         File JavaDoc 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     /**
265      * Verifies that the mapping of the servlet redirector is correctly
266      * overridden by a nested 'servletredirector' element.
267      *
268      * @throws Exception If an unexpected error occurs
269      */

270     public void testCustomServletRedirectorMapping() throws Exception JavaDoc
271     {
272         executeTestTarget();
273
274         File JavaDoc 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     /**
284      * Verifies that the mapping of the JSP redirector is correctly overridden
285      * by a nested 'jspredirector' element.
286      *
287      * @throws Exception If an unexpected error occurs
288      */

289     public void testCustomJspRedirectorMapping() throws Exception JavaDoc
290     {
291         executeTestTarget();
292
293         File JavaDoc 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     /**
301      * Verifies that the mapping of the filter redirector is correctly
302      * overridden by a nested 'filterredirector' element.
303      *
304      * @throws Exception If an unexpected error occurs
305      */

306     public void testCustomFilterRedirectorMapping() throws Exception JavaDoc
307     {
308         executeTestTarget();
309
310         File JavaDoc 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     /**
320      * Verifies that no definition of the filter redirector is added to a
321      * Servlet 2.2 descriptor, even when explicitly requested by a nested
322      * 'filterredirector' element.
323      *
324      * @throws Exception If an unexpected error occurs
325      */

326     public void testCustomFilterRedirectorMappingIgnored() throws Exception JavaDoc
327     {
328         executeTestTarget();
329
330         File JavaDoc 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     /**
339      * Verifies that two servlet redirectors with different names and mappings
340      * are added as expected.
341      *
342      * @throws Exception If an unexpected error occurs
343      */

344     public void testMultipleNamedServletRedirectors() throws Exception JavaDoc
345     {
346         executeTestTarget();
347
348         File JavaDoc 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     /**
360      * Verifies that two JSP redirectors with different names and mappings
361      * are added as expected.
362      *
363      * @throws Exception If an unexpected error occurs
364      */

365     public void testMultipleNamedJspRedirectors() throws Exception JavaDoc
366     {
367         executeTestTarget();
368
369         File JavaDoc 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     /**
381      * Verifies that two filter redirectors with different names and mappings
382      * are added as expected.
383      *
384      * @throws Exception If an unexpected error occurs
385      */

386     public void testMultipleNamedFilterRedirectors() throws Exception JavaDoc
387     {
388         executeTestTarget();
389
390         File JavaDoc 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     /**
402      * Verifies that a secured servlet redirector gets added alongside the role
403      * names.
404      *
405      * @throws Exception If an unexpected error occurs
406      */

407     public void testSecuredServletRedirector() throws Exception JavaDoc
408     {
409         executeTestTarget();
410
411         File JavaDoc 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 JavaDoc securityConstraintElement =
421             webXml.getSecurityConstraint("/ServletRedirectorSecure");
422         assertNotNull(securityConstraintElement);
423         Element JavaDoc authConstraintElement = (Element JavaDoc)
424             securityConstraintElement.getElementsByTagName(
425                 "auth-constraint").item(0);
426         assertNotNull(authConstraintElement);
427         NodeList JavaDoc 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 JavaDoc loginConfigElements =
435             webXml.getElements(WebXmlTag.LOGIN_CONFIG);
436         assertTrue(loginConfigElements.hasNext());
437         Element JavaDoc loginConfigElement = (Element JavaDoc) loginConfigElements.next();
438         Element JavaDoc authMethodElement = (Element JavaDoc)
439             loginConfigElement.getElementsByTagName("auth-method").item(0);
440         assertEquals("BASIC",
441             authMethodElement.getChildNodes().item(0).getNodeValue());
442     }
443
444     /**
445      * Verifies that a already existent login configuration does not get
446      * replaced by the default BASIC login configuration when secured
447      * redirectors are defined.
448      *
449      * @throws Exception If an unexpected error occurs
450      */

451     public void testLoginConfigNotOverwritten() throws Exception JavaDoc
452     {
453         executeTestTarget();
454
455         File JavaDoc 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     /**
462      * Verifies that JARs already contained by the source archive are not added
463      * again.
464      *
465      * @throws Exception If an unexpected error occurs
466      */

467     public void testNoDuplicateJars() throws Exception JavaDoc
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     // Private Methods ---------------------------------------------------------
484

485     /**
486      * Asserts that a filter of the specified class is defined in the given
487      * deployment descriptor and mapped to a specific URL-pattern.
488      *
489      * @param theWebXml The deployment descriptor
490      * @param theFilterClass The name of the filter class
491      * @param theFilterName The name of the filter
492      * @param theMapping The URL-pattern that the filter is expected to be
493      * mapped to
494      */

495     private void assertFilterMapping(WebXml theWebXml, String JavaDoc theFilterClass,
496         String JavaDoc theFilterName, String JavaDoc theMapping)
497     {
498         Iterator JavaDoc names = theWebXml.getFilterNamesForClass(theFilterClass);
499
500         // Look for the definition that matches the JSP servlet name
501
boolean found = false;
502         String JavaDoc name = null;
503         while (names.hasNext())
504         {
505             name = (String JavaDoc) 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 JavaDoc mappings = theWebXml.getFilterMappings(name);
520         assertTrue("Mapping for [" + theFilterClass + "(" + theFilterName
521             + ")] not found", mappings.hasNext());
522         assertEquals(theMapping, mappings.next());
523     }
524
525     /**
526      * Asserts that the specified JSP file is defined in the given deployment
527      * descriptor and mapped to a specific URL-pattern.
528      *
529      * @param theWebXml The deployment descriptor
530      * @param theJspFile The JSP file name
531      * @param theJspName The JSP servlet name
532      * @param theMapping The URL-pattern that the JSP file is expected to be
533      * mapped to
534      */

535     private void assertJspMapping(WebXml theWebXml, String JavaDoc theJspFile,
536         String JavaDoc theJspName, String JavaDoc theMapping)
537     {
538         Iterator JavaDoc names = theWebXml.getServletNamesForJspFile(theJspFile);
539
540         // Look for the definition that matches the JSP servlet name
541
boolean found = false;
542         String JavaDoc name = null;
543         while (names.hasNext())
544         {
545             name = (String JavaDoc) 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 JavaDoc mappings = theWebXml.getServletMappings(name);
560         assertTrue("Mapping for [" + theJspFile + "(" + theJspName
561             + ")] not found", mappings.hasNext());
562         assertEquals(theMapping, mappings.next());
563     }
564
565     /**
566      * Asserts that a servlet of the specified name is defined in the given
567      * deployment descriptor and mapped to a specific URL-pattern.
568      *
569      * @param theWebXml The deployment descriptor
570      * @param theServletClass The name of servlet class
571      * @param theServletName The name of the servlet
572      * @param theMapping The URL-pattern that the servlet is expected to be
573      * mapped to
574      */

575     private void assertServletMapping(WebXml theWebXml, String JavaDoc theServletClass,
576         String JavaDoc theServletName, String JavaDoc theMapping)
577     {
578         Iterator JavaDoc names = theWebXml.getServletNamesForClass(theServletClass);
579         
580         // Look for the definition that matches the servlet name
581
boolean found = false;
582         String JavaDoc name = null;
583         while (names.hasNext())
584         {
585             name = (String JavaDoc) 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 JavaDoc 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