KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > integration > ant > deployment > webapp > TestWebXml


1 /*
2  * ========================================================================
3  *
4  * Copyright 2003-2004 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.deployment.webapp;
21
22 import java.io.ByteArrayInputStream JavaDoc;
23 import java.io.StringReader JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28
29 import javax.xml.parsers.DocumentBuilder JavaDoc;
30 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
31 import javax.xml.parsers.ParserConfigurationException JavaDoc;
32
33 import junit.framework.TestCase;
34
35 import org.w3c.dom.Document JavaDoc;
36 import org.w3c.dom.Element JavaDoc;
37 import org.w3c.dom.NodeList JavaDoc;
38 import org.xml.sax.EntityResolver JavaDoc;
39 import org.xml.sax.InputSource JavaDoc;
40 import org.xml.sax.SAXException JavaDoc;
41
42 /**
43  * Unit tests for {@link WebXml}.
44  *
45  * @version $Id: TestWebXml.java,v 1.1 2004/05/31 20:05:21 vmassol Exp $
46  */

47 public final class TestWebXml extends TestCase
48 {
49     /**
50      * The document builder factory.
51      */

52     private DocumentBuilderFactory JavaDoc factory;
53
54     /**
55      * The JAXP document builder.
56      */

57     private DocumentBuilder JavaDoc builder;
58
59     /**
60      * @see TestCase#setUp
61      */

62     public void setUp() throws ParserConfigurationException JavaDoc
63     {
64         factory = DocumentBuilderFactory.newInstance();
65         factory.setValidating(false);
66         factory.setNamespaceAware(false);
67
68         builder = factory.newDocumentBuilder();
69         builder.setEntityResolver(new EntityResolver JavaDoc()
70         {
71             public InputSource JavaDoc resolveEntity(String JavaDoc thePublicId,
72                 String JavaDoc theSystemId) throws SAXException JavaDoc
73             {
74                 return new InputSource JavaDoc(new StringReader JavaDoc(""));
75             }
76         });
77     }
78     
79     /**
80      * Tests whether the construction of a WebXml object with a
81      * <code>null</code> parameter for the DOM document throws a
82      * <code>NullPointerException</code>.
83      *
84      * @throws Exception If an unexpected error occurs
85      */

86     public void testConstructionWithNullDocument() throws Exception JavaDoc
87     {
88         try
89         {
90             new WebXml(null);
91             fail("Expected NullPointerException");
92         }
93         catch (NullPointerException JavaDoc npe)
94         {
95             // expected
96
}
97         
98     }
99     
100     /**
101      * Tests whether a servlet API version 2.2 descriptor is correctly detected.
102      *
103      * @throws Exception If an unexpected error occurs
104      */

105     public void testGetVersion22() throws Exception JavaDoc
106     {
107         String JavaDoc 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 JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
112         WebXml webXml = new WebXml(doc);
113         assertEquals(WebXmlVersion.V2_2, webXml.getVersion());
114     }
115     
116     /**
117      * Tests whether a servlet API version 2.3 descriptor is correctly detected.
118      *
119      * @throws Exception If an unexpected error occurs
120      */

121     public void testGetVersion23() throws Exception JavaDoc
122     {
123         String JavaDoc 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 JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
128         WebXml webXml = new WebXml(doc);
129         assertEquals(WebXmlVersion.V2_3, webXml.getVersion());
130     }
131     
132     /**
133      * Tests whether WebXml#getVersion returns <code>null</code> when the public
134      * ID of the <code>DOCTYPE</code> is not recognized.
135      *
136      * @throws Exception If an unexpected error occurs
137      */

138     public void testGetVersionUnknown() throws Exception JavaDoc
139     {
140         String JavaDoc 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 JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
145         WebXml webXml = new WebXml(doc);
146         assertNull(webXml.getVersion());
147     }
148     
149     /**
150      * Tests whether WebXml#getVersion returns <code>null</code> when the
151      * <code>DOCTYPE</code> is missing.
152      *
153      * @throws Exception If an unexpected error occurs
154      */

155     public void testGetVersionWithoutDoctype() throws Exception JavaDoc
156     {
157         String JavaDoc xml = "<web-app></web-app>";
158         Document JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
159         WebXml webXml = new WebXml(doc);
160         assertNull(webXml.getVersion());
161     }
162     
163     /**
164      * Tests whether calling {@link WebXml.hasFilter} with <code>null</code> as
165      * filter name parameter results in a <code>NullPointerException</code>
166      * being thrown.
167      *
168      * @throws Exception If an unexpected error occurs
169      */

170     public void testHasFilterWithNullName() throws Exception JavaDoc
171     {
172         String JavaDoc xml = "<web-app></web-app>";
173         Document JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
174         WebXml webXml = new WebXml(doc);
175         try
176         {
177             webXml.hasFilter(null);
178             fail("Expected NullPointerException");
179         }
180         catch (NullPointerException JavaDoc npe)
181         {
182             // expected
183
}
184         
185     }
186     
187     /**
188      * Tests whether {@link WebXml.hasFilter} returns the correct value for
189      * a descriptor containing one filter definition.
190      *
191      * @throws Exception If an unexpected error occurs
192      */

193     public void testHasFilterWithOneFilter() throws Exception JavaDoc
194     {
195         String JavaDoc xml = "<web-app>"
196             + " <filter>"
197             + " <filter-name>f1</filter-name>"
198             + " <filter-class>fclass1</filter-class>"
199             + " </filter>"
200             + "</web-app>";
201         Document JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
202         WebXml webXml = new WebXml(doc);
203         assertTrue(webXml.hasFilter("f1"));
204         assertTrue(!webXml.hasFilter("f2"));
205     }
206
207     /**
208      * Tests whether {@link WebXml.hasFilter} returns the correct values for
209      * a descriptor containing multiple filter definitions.
210      *
211      * @throws Exception If an unexpected error occurs
212      */

213     public void testHasFilterWithMultipleFilters() throws Exception JavaDoc
214     {
215         String JavaDoc 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 JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(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     /**
238      * Tests whether a DOM element representing a single filter definition can
239      * be correctly retrieved from a descriptor containing only that filter.
240      *
241      * @throws Exception If an unexpected error occurs
242      */

243     public void testGetFilterElementWithOneFilter() throws Exception JavaDoc
244     {
245         String JavaDoc 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 JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
252         WebXml webXml = new WebXml(doc);
253         Element JavaDoc 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     /**
267      * Tests whether the filter names are retrieved in the expected order.
268      *
269      * @throws Exception If an unexpected error occurs
270      */

271     public void testGetFilterNames() throws Exception JavaDoc
272     {
273         String JavaDoc 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 JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
288         WebXml webXml = new WebXml(doc);
289         Iterator JavaDoc 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     /**
297      * Tests whether a retrieving a filter name by the name of the class
298      * implementing the filter works correctly for a descriptor with a single
299      * filter definition.
300      *
301      * @throws Exception If an unexpected error occurs
302      */

303     public void testGetFilterNamesForClassWithSingleFilter() throws Exception JavaDoc
304     {
305         String JavaDoc xml = "<web-app>"
306             + " <filter>"
307             + " <filter-name>f1</filter-name>"
308             + " <filter-class>f1class</filter-class>"
309             + " </filter>"
310             + "</web-app>";
311         Document JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
312         WebXml webXml = new WebXml(doc);
313         Iterator JavaDoc filterNames = webXml.getFilterNamesForClass("f1class");
314         assertEquals("f1", filterNames.next());
315         assertTrue(!filterNames.hasNext());
316     }
317     
318     /**
319      * Tests whether a retrieving the filter names by the name of the class
320      * implementing the filter works correctly for a descriptor with multiple
321      * filter definitions.
322      *
323      * @throws Exception If an unexpected error occurs
324      */

325     public void testGetFilterNamesForClassWithMultipleFilters() throws Exception JavaDoc
326     {
327         String JavaDoc 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 JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
342         WebXml webXml = new WebXml(doc);
343         Iterator JavaDoc filterNames = webXml.getFilterNamesForClass("f1class");
344         assertEquals("f1", filterNames.next());
345         assertEquals("f3", filterNames.next());
346         assertTrue(!filterNames.hasNext());
347     }
348     
349     /**
350      * Tests whether a filter-mapping is correctly retrieved from a descriptor.
351      *
352      * @throws Exception If an unexpected error occurs
353      */

354     public void testGetFilterMappingsWithOneMapping() throws Exception JavaDoc
355     {
356         String JavaDoc 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 JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
363         WebXml webXml = new WebXml(doc);
364         Iterator JavaDoc filterMappings = webXml.getFilterMappings("f1");
365         assertEquals("/f1mapping", filterMappings.next());
366         assertTrue(!filterMappings.hasNext());
367     }
368     
369     /**
370      * Tests whether multiple filter-mappings are correctly retrieved from a
371      * descriptor.
372      *
373      * @throws Exception If an unexpected error occurs
374      */

375     public void testGetFilterMappingsWithMultipleMappings() throws Exception JavaDoc
376     {
377         String JavaDoc 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 JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
392         WebXml webXml = new WebXml(doc);
393         Iterator JavaDoc 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     /**
401      * Tests whether a filter-mapping is correctly retrieved from a descriptor.
402      *
403      * @throws Exception If an unexpected error occurs
404      */

405     public void testGetFilterMappingsWithFilter() throws Exception JavaDoc
406     {
407         String JavaDoc 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 JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
418         WebXml webXml = new WebXml(doc);
419         Iterator JavaDoc filterMappings = webXml.getFilterMappings("f1");
420         assertEquals("/f1mapping", filterMappings.next());
421         assertTrue(!filterMappings.hasNext());
422     }
423
424     /**
425      * Tests whether a single context-param is correctly inserted into an empty
426      * descriptor.
427      *
428      * @throws Exception If an unexpected error occurs
429      */

430     public void testAddContextParamToEmptyDocument() throws Exception JavaDoc
431     {
432         String JavaDoc xml = "<web-app></web-app>";
433         Document JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
434         WebXml webXml = new WebXml(doc);
435         Element JavaDoc contextParamElement =
436             createContextParamElement(doc, "param", "value");
437         webXml.addContextParam(contextParamElement);
438         assertTrue(webXml.hasContextParam("param"));
439     }
440     
441     /**
442      * Tests whether a single filter is correctly inserted into an empty
443      * descriptor.
444      *
445      * @throws Exception If an unexpected error occurs
446      */

447     public void testAddFilterToEmptyDocument() throws Exception JavaDoc
448     {
449         String JavaDoc xml = "<web-app></web-app>";
450         Document JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
451         WebXml webXml = new WebXml(doc);
452         Element JavaDoc filterElement = createFilterElement(doc, "f1", "f1class");
453         webXml.addFilter(filterElement);
454         assertTrue(webXml.hasFilter("f1"));
455     }
456
457     /**
458      * Tests whether a single context param is correctly inserted into a
459      * descriptor that already contains an other context param definition.
460      *
461      * @throws Exception If an unexpected error occurs
462      */

463     public void testAddContextParamToDocumentWithAnotherContextParam()
464         throws Exception JavaDoc
465     {
466         String JavaDoc 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 JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
473         WebXml webXml = new WebXml(doc);
474         Element JavaDoc contextParamElement =
475             createContextParamElement(doc, "param2", "value2");
476         webXml.addContextParam(contextParamElement);
477         assertTrue(webXml.hasContextParam("param1"));
478         assertTrue(webXml.hasContextParam("param2"));
479     }
480     
481     /**
482      * Tests whether a single filter is correctly inserted into a descriptor
483      * that already contains an other filter definition.
484      *
485      * @throws Exception If an unexpected error occurs
486      */

487     public void testAddFilterToDocumentWithAnotherFilter() throws Exception JavaDoc
488     {
489         String JavaDoc xml = "<web-app>"
490             + " <filter>"
491             + " <filter-name>f1</filter-name>"
492             + " <filter-class>fclass1</filter-class>"
493             + " </filter>"
494             + "</web-app>";
495         Document JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
496         WebXml webXml = new WebXml(doc);
497         Element JavaDoc filterElement = createFilterElement(doc, "f2", "f2class");
498         webXml.addFilter(filterElement);
499         assertTrue(webXml.hasFilter("f1"));
500         assertTrue(webXml.hasFilter("f2"));
501     }
502
503     /**
504      * Tests whether trying to add a context param to a descriptor that already
505      * contains a context param definition with the same name results in an
506      * exception.
507      *
508      * @throws Exception If an unexpected error occurs
509      */

510     public void testAddContextParamToDocumentWithTheSameContextParam()
511         throws Exception JavaDoc
512     {
513         String JavaDoc 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 JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
520         WebXml webXml = new WebXml(doc);
521         Element JavaDoc contextParamElement =
522             createContextParamElement(doc, "param", "value");
523         try
524         {
525             webXml.addContextParam(contextParamElement);
526             fail("Expected IllegalStateException");
527         }
528         catch (IllegalStateException JavaDoc ise)
529         {
530             // expected
531
}
532     }
533
534     /**
535      * Tests whether trying to add a filter to a descriptor that already
536      * contains a filter definition with the same name results in a exception.
537      *
538      * @throws Exception If an unexpected error occurs
539      */

540     public void testAddFilterToDocumentWithTheSameFilter() throws Exception JavaDoc
541     {
542         String JavaDoc xml = "<web-app>"
543             + " <filter>"
544             + " <filter-name>f1</filter-name>"
545             + " <filter-class>fclass1</filter-class>"
546             + " </filter>"
547             + "</web-app>";
548         Document JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
549         WebXml webXml = new WebXml(doc);
550         Element JavaDoc filterElement = createFilterElement(doc, "f1", "f1class");
551         try
552         {
553             webXml.addFilter(filterElement);
554             fail("Expected IllegalStateException");
555         }
556         catch (IllegalStateException JavaDoc ise)
557         {
558             // expected
559
}
560     }
561     
562     /**
563      * Tests whether a single initialization parameter can be added to a filter
564      * definition.
565      *
566      * @throws Exception If an unexpected error occurs
567      */

568     public void testAddOneFilterInitParam() throws Exception JavaDoc
569     {
570         String JavaDoc xml = "<web-app>"
571             + " <filter>"
572             + " <filter-name>f1</filter-name>"
573             + " <filter-class>fclass1</filter-class>"
574             + " </filter>"
575             + "</web-app>";
576         Document JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
577         WebXml webXml = new WebXml(doc);
578         webXml.addFilterInitParam("f1", "f1param1", "f1param1value");
579         Iterator JavaDoc initParams = webXml.getFilterInitParamNames("f1");
580         assertEquals("f1param1", initParams.next());
581         assertTrue(!initParams.hasNext());
582     }
583
584     /**
585      * Tests whether multiple initialization parameter can be added to a filter
586      * definition.
587      *
588      * @throws Exception If an unexpected error occurs
589      */

590     public void testAddMultipleFilterInitParams() throws Exception JavaDoc
591     {
592         String JavaDoc xml = "<web-app>"
593             + " <filter>"
594             + " <filter-name>f1</filter-name>"
595             + " <filter-class>fclass1</filter-class>"
596             + " </filter>"
597             + "</web-app>";
598         Document JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(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 JavaDoc 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     /**
611      * Tests whether a single filter can be added using the method that takes
612      * a string for the filter name and a string for the filter class.
613      *
614      * @throws Exception If an unexpected error occurs
615      */

616     public void testAddFilterWithNameAndClass() throws Exception JavaDoc
617     {
618         String JavaDoc xml = "<web-app>"
619             + "</web-app>";
620         Document JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
621         WebXml webXml = new WebXml(doc);
622         webXml.addServlet("f1", "f1class");
623         assertTrue(webXml.hasServlet("f1"));
624     }
625
626     /**
627      * Tests whether calling {@link WebXml#hasServlet} with a <code>null</code>
628      * parameter as servlet name throws a <code>NullPointerException</code>.
629      *
630      * @throws Exception If an unexpected error occurs
631      */

632     public void testHasServletWithNullName() throws Exception JavaDoc
633     {
634         String JavaDoc xml = "<web-app></web-app>";
635         Document JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
636         WebXml webXml = new WebXml(doc);
637         try
638         {
639             webXml.hasServlet(null);
640             fail("Expected NullPointerException");
641         }
642         catch (NullPointerException JavaDoc npe)
643         {
644             // expected
645
}
646         
647     }
648     
649     /**
650      * Tests whether {@link WebXml#hasServlet} reports the correct values for a
651      * descriptor containing a single servlet definition.
652      *
653      * @throws Exception If an unexpected error occurs
654      */

655     public void testHasServletWithOneServlet() throws Exception JavaDoc
656     {
657         String JavaDoc xml = "<web-app>"
658             + " <servlet>"
659             + " <servlet-name>s1</servlet-name>"
660             + " <servlet-class>sclass1</servlet-class>"
661             + " </servlet>"
662             + "</web-app>";
663         Document JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
664         WebXml webXml = new WebXml(doc);
665         assertTrue(webXml.hasServlet("s1"));
666         assertTrue(!webXml.hasServlet("s2"));
667     }
668
669     /**
670      * Tests whether {@link WebXml#hasServlet} reports the correct values for a
671      * descriptor containing multiple servlet definitions.
672      *
673      * @throws Exception If an unexpected error occurs
674      */

675     public void testHasServletWithMultipleServlets() throws Exception JavaDoc
676     {
677         String JavaDoc 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 JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(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     /**
700      * Tests whether a servlet element is correctly retrieved from a descriptor
701      * containing only one servlet definition.
702      *
703      * @throws Exception If an unexpected error occurs
704      */

705     public void testGetServletElementWithOneServlet() throws Exception JavaDoc
706     {
707         String JavaDoc 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 JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
714         WebXml webXml = new WebXml(doc);
715         Element JavaDoc 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     /**
729      * Tests whether the names of the servlets defined in a descriptor are
730      * correctly returned in the expected order.
731      *
732      * @throws Exception If an unexpected error occurs
733      */

734     public void testGetServletNames() throws Exception JavaDoc
735     {
736         String JavaDoc 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 JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
751         WebXml webXml = new WebXml(doc);
752         Iterator JavaDoc 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     /**
760      * Tests whether a retrieving a servlet name by the name of the class
761      * implementing the servlet works correctly for a descriptor with a single
762      * servlet definition.
763      *
764      * @throws Exception If an unexpected error occurs
765      */

766     public void testGetServletNamesForClassWithSingleServlet() throws Exception JavaDoc
767     {
768         String JavaDoc xml = "<web-app>"
769             + " <servlet>"
770             + " <servlet-name>s1</servlet-name>"
771             + " <servlet-class>s1class</servlet-class>"
772             + " </servlet>"
773             + "</web-app>";
774         Document JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
775         WebXml webXml = new WebXml(doc);
776         Iterator JavaDoc servletNames = webXml.getServletNamesForClass("s1class");
777         assertEquals("s1", servletNames.next());
778         assertTrue(!servletNames.hasNext());
779     }
780     
781     /**
782      * Tests whether a retrieving the servlet names by the name of the class
783      * implementing the servlet works correctly for a descriptor with multiple
784      * servlet definitions.
785      *
786      * @throws Exception If an unexpected error occurs
787      */

788     public void testGetServletNamesForClassWithMultipleServlets()
789         throws Exception JavaDoc
790     {
791         String JavaDoc 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 JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
806         WebXml webXml = new WebXml(doc);
807         Iterator JavaDoc servletNames = webXml.getServletNamesForClass("sclass1");
808         assertEquals("s1", servletNames.next());
809         assertEquals("s3", servletNames.next());
810         assertTrue(!servletNames.hasNext());
811     }
812     
813     /**
814      * Tests whether a retrieving a servlet name by the path of the JSP file
815      * implementing the servlet works correctly for a descriptor with a single
816      * servlet definition.
817      *
818      * @throws Exception If an unexpected error occurs
819      */

820     public void testGetServletNamesForJspFileWithSingleServlet()
821         throws Exception JavaDoc
822     {
823         String JavaDoc 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 JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
830         WebXml webXml = new WebXml(doc);
831         Iterator JavaDoc servletNames = webXml.getServletNamesForJspFile("/s1.jsp");
832         assertEquals("s1", servletNames.next());
833         assertTrue(!servletNames.hasNext());
834     }
835     
836     /**
837      * Tests whether a retrieving the servlet names by the path of the JSP file
838      * implementing the servlet works correctly for a descriptor with multiple
839      * servlet definitions.
840      *
841      * @throws Exception If an unexpected error occurs
842      */

843     public void testGetServletNamesForJspFileWithMultipleServlets()
844         throws Exception JavaDoc
845     {
846         String JavaDoc 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 JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
861         WebXml webXml = new WebXml(doc);
862         Iterator JavaDoc servletNames = webXml.getServletNamesForJspFile("/s3.jsp");
863         assertEquals("s3", servletNames.next());
864         assertTrue(!servletNames.hasNext());
865     }
866     
867     /**
868      * Tests whether a single serrvlet-mapping is correctly retrieved from a
869      * descriptor.
870      *
871      * @throws Exception If an unexpected error occurs
872      */

873     public void testGetServletMappingsWithOneMapping() throws Exception JavaDoc
874     {
875         String JavaDoc 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 JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
882         WebXml webXml = new WebXml(doc);
883         Iterator JavaDoc servletMappings = webXml.getServletMappings("s1");
884         assertEquals("/s1mapping", servletMappings.next());
885         assertTrue(!servletMappings.hasNext());
886     }
887
888     /**
889      * Tests whether multiple servlet mappings are correctly retrieved from a
890      * descriptor.
891      *
892      * @throws Exception If an unexpected error occurs
893      */

894     public void testGetServletMappingsWithMultipleMappings() throws Exception JavaDoc
895     {
896         String JavaDoc 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 JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
911         WebXml webXml = new WebXml(doc);
912         Iterator JavaDoc 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     /**
920      * Tests whether a single servlet can be added to an empty descriptor.
921      *
922      * @throws Exception If an unexpected error occurs
923      */

924     public void testAddServletToEmptyDocument() throws Exception JavaDoc
925     {
926         String JavaDoc xml = "<web-app></web-app>";
927         Document JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
928         WebXml webXml = new WebXml(doc);
929         webXml.addServlet(createServletElement(doc, "s1", "s1class"));
930         assertTrue(webXml.hasServlet("s1"));
931     }
932
933     /**
934      * Tests whether a single servlet can be added to a descriptor already
935      * containing an other servlet.
936      *
937      * @throws Exception If an unexpected error occurs
938      */

939     public void testAddServletToDocumentWithAnotherServlet() throws Exception JavaDoc
940     {
941         String JavaDoc xml = "<web-app>"
942             + " <servlet>"
943             + " <servlet-name>s1</servlet-name>"
944             + " <servlet-class>sclass1</servlet-class>"
945             + " </servlet>"
946             + "</web-app>";
947         Document JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(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     /**
955      * Tests whether trying to add a servlet to a descriptor that already
956      * contains a servlet with the same name results in an exception.
957      *
958      * @throws Exception If an unexpected error occurs
959      */

960     public void testAddServletToDocumentWithTheSameServlet() throws Exception JavaDoc
961     {
962         String JavaDoc xml = "<web-app>"
963             + " <servlet>"
964             + " <servlet-name>s1</servlet-name>"
965             + " <servlet-class>sclass1</servlet-class>"
966             + " </servlet>"
967             + "</web-app>";
968         Document JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(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 JavaDoc ise)
976         {
977             // expected
978
}
979     }
980
981     /**
982      * Tests whether a single initialization parameter is correctly added to an
983      * existing servlet definition.
984      *
985      * @throws Exception If an unexpected error occurs
986      */

987     public void testAddOneServletInitParam() throws Exception JavaDoc
988     {
989         String JavaDoc xml = "<web-app>"
990             + " <servlet>"
991             + " <servlet-name>s1</servlet-name>"
992             + " <servlet-class>sclass1</servlet-class>"
993             + " </servlet>"
994             + "</web-app>";
995         Document JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
996         WebXml webXml = new WebXml(doc);
997         webXml.addServletInitParam("s1", "s1param1", "s1param1value");
998         Iterator JavaDoc initParams = webXml.getServletInitParamNames("s1");
999         assertEquals("s1param1", initParams.next());
1000        assertTrue(!initParams.hasNext());
1001    }
1002
1003    /**
1004     * Tests whether multiple initialization parameters are correctly added to
1005     * an existing servlet definition.
1006     *
1007     * @throws Exception If an unexpected error occurs
1008     */

1009    public void testAddMultipleServletInitParams() throws Exception JavaDoc
1010    {
1011        String JavaDoc xml = "<web-app>"
1012            + " <servlet>"
1013            + " <servlet-name>s1</servlet-name>"
1014            + " <servlet-class>sclass1</servlet-class>"
1015            + " </servlet>"
1016            + "</web-app>";
1017        Document JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(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 JavaDoc 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    /**
1030     * Tests whether a single servlet can be added using the method that takes
1031     * a string for the servlet name and a string for the servlet class.
1032     *
1033     * @throws Exception If an unexpected error occurs
1034     */

1035    public void testAddServletWithNameAndClass() throws Exception JavaDoc
1036    {
1037        String JavaDoc xml = "<web-app>"
1038            + "</web-app>";
1039        Document JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
1040        WebXml webXml = new WebXml(doc);
1041        webXml.addServlet("s1", "s1class");
1042        assertTrue(webXml.hasServlet("s1"));
1043    }
1044
1045    /**
1046     * Tests whether a single servlet can be added using the method that takes
1047     * a string for the servlet name and a string for the JSP file.
1048     *
1049     * @throws Exception If an unexpected error occurs
1050     */

1051    public void testAddServletWithNameAndJspFile() throws Exception JavaDoc
1052    {
1053        String JavaDoc xml = "<web-app>"
1054            + "</web-app>";
1055        Document JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
1056        WebXml webXml = new WebXml(doc);
1057        webXml.addJspFile("s1", "s1.jsp");
1058        assertTrue(webXml.hasServlet("s1"));
1059    }
1060
1061    /**
1062     * Tests whether a security-constraint with no roles is successfully added
1063     * to an empty descriptor.
1064     *
1065     * @throws Exception If an unexpected error occurs
1066     */

1067    public void testAddSecurityConstraint()
1068        throws Exception JavaDoc
1069    {
1070        String JavaDoc xml = "<web-app></web-app>";
1071        Document JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
1072        WebXml webXml = new WebXml(doc);
1073        webXml.addSecurityConstraint("wrn", "/url", Collections.EMPTY_LIST);
1074        assertTrue(webXml.hasSecurityConstraint("/url"));
1075    }
1076
1077    /**
1078     * Tests whether a security-constraint with two roles is successfully added
1079     * to an empty descriptor.
1080     *
1081     * @throws Exception If an unexpected error occurs
1082     */

1083    public void testAddSecurityConstraintWithRoles()
1084        throws Exception JavaDoc
1085    {
1086        String JavaDoc xml = "<web-app></web-app>";
1087        Document JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
1088        WebXml webXml = new WebXml(doc);
1089        List JavaDoc roles = new ArrayList JavaDoc();
1090        roles.add("role1");
1091        roles.add("role2");
1092        webXml.addSecurityConstraint("wrn", "/url", roles);
1093        assertTrue(webXml.hasSecurityConstraint("/url"));
1094        Element JavaDoc securityConstraintElement =
1095            webXml.getSecurityConstraint("/url");
1096        assertNotNull(securityConstraintElement);
1097        Element JavaDoc authConstraintElement = (Element JavaDoc)
1098            securityConstraintElement.getElementsByTagName(
1099                "auth-constraint").item(0);
1100        assertNotNull(authConstraintElement);
1101        NodeList JavaDoc 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    /**
1111     * Tests whether checking an empty descriptor for a login configuration
1112     * results in <code>false</code>.
1113     *
1114     * @throws Exception If an unexpected error occurs
1115     */

1116    public void testHasLoginConfigEmpty()
1117        throws Exception JavaDoc
1118    {
1119        String JavaDoc xml = "<web-app></web-app>";
1120        Document JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
1121        WebXml webXml = new WebXml(doc);
1122        assertTrue(!webXml.hasLoginConfig());
1123    }
1124
1125    /**
1126     * Tests whether checking a descriptor with a login configuration for a
1127     * login configuration results in <code>true</code>.
1128     *
1129     * @throws Exception If an unexpected error occurs
1130     */

1131    public void testHasLoginConfig()
1132        throws Exception JavaDoc
1133    {
1134        String JavaDoc xml = "<web-app>"
1135            + " <login-config>"
1136            + " <auth-method>BASIC</auth-method>"
1137            + " </login-config>"
1138            + "</web-app>";
1139        Document JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
1140        WebXml webXml = new WebXml(doc);
1141        assertTrue(webXml.hasLoginConfig());
1142    }
1143
1144    /**
1145     * Tests retrieving the authentication method from a descriptor.
1146     *
1147     * @throws Exception If an unexpected error occurs
1148     */

1149    public void testGetLoginConfigAuthMethod()
1150        throws Exception JavaDoc
1151    {
1152        String JavaDoc xml = "<web-app>"
1153            + " <login-config>"
1154            + " <auth-method>BASIC</auth-method>"
1155            + " </login-config>"
1156            + "</web-app>";
1157        Document JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
1158        WebXml webXml = new WebXml(doc);
1159        assertEquals("BASIC", webXml.getLoginConfigAuthMethod());
1160    }
1161
1162    /**
1163     * Tests retrieving the authentication method from a descriptor.
1164     *
1165     * @throws Exception If an unexpected error occurs
1166     */

1167    public void testSetLoginConfigAdding()
1168        throws Exception JavaDoc
1169    {
1170        String JavaDoc xml = "<web-app></web-app>";
1171        Document JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(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    /**
1179     * Tests retrieving the authentication method from a descriptor.
1180     *
1181     * @throws Exception If an unexpected error occurs
1182     */

1183    public void testSetLoginConfigReplacing()
1184        throws Exception JavaDoc
1185    {
1186        String JavaDoc xml = "<web-app>"
1187        + " <login-config>"
1188        + " <auth-method>DIGEST</auth-method>"
1189        + " </login-config>"
1190        + "</web-app>";
1191        Document JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(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    /**
1199     * Tests whether checking an empty descriptor for some security constraint
1200     * results in <code>false</code>.
1201     *
1202     * @throws Exception If an unexpected error occurs
1203     */

1204    public void testHasSecurityConstraintEmpty()
1205        throws Exception JavaDoc
1206    {
1207        String JavaDoc xml = "<web-app></web-app>";
1208        Document JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
1209        WebXml webXml = new WebXml(doc);
1210        assertTrue(!webXml.hasSecurityConstraint("/TestUrl"));
1211    }
1212
1213    /**
1214     * Tests whether a single security-constraint element in the descriptor is
1215     * correctly retrieved.
1216     *
1217     * @throws Exception If an unexpected error occurs
1218     */

1219    public void testGetSingleSecurityConstraint()
1220        throws Exception JavaDoc
1221    {
1222        String JavaDoc 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 JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
1231        WebXml webXml = new WebXml(doc);
1232        assertTrue(webXml.hasSecurityConstraint("/url1"));
1233        Element JavaDoc securityConstraintElement =
1234            webXml.getSecurityConstraint("/url1");
1235        assertNotNull(securityConstraintElement);
1236    }
1237
1238    /**
1239     * Tests whether multiple security-constraint elements are returned in
1240     * the expected order.
1241     *
1242     * @throws Exception If an unexpected error occurs
1243     */

1244    public void testGetMutlipleSecurityConstraints()
1245        throws Exception JavaDoc
1246    {
1247        String JavaDoc 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 JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(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 JavaDoc 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    /**
1281     * Tests whether retrieving the login-config from an empty descriptor
1282     * returns <code>null</code>.
1283     *
1284     * @throws Exception If an unexpected error occurs
1285     */

1286    public void testGetLoginConfigEmpty()
1287        throws Exception JavaDoc
1288    {
1289        String JavaDoc xml = "<web-app></web-app>";
1290        Document JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
1291        WebXml webXml = new WebXml(doc);
1292        assertTrue(!webXml.getElements(WebXmlTag.LOGIN_CONFIG).hasNext());
1293    }
1294
1295    /**
1296     * Tests whether the login-config element can be correctly retrieved.
1297     *
1298     * @throws Exception If an unexpected error occurs
1299     */

1300    public void testGetLoginConfig()
1301        throws Exception JavaDoc
1302    {
1303        String JavaDoc xml = "<web-app><login-config/></web-app>";
1304        Document JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
1305        WebXml webXml = new WebXml(doc);
1306        assertTrue(webXml.getElements(WebXmlTag.LOGIN_CONFIG).hasNext());
1307    }
1308
1309    /**
1310     * Tests whether checking an empty descriptor for some security roles
1311     * results in <code>false</code>.
1312     *
1313     * @throws Exception If an unexpected error occurs
1314     */

1315    public void testHasSecurityRoleEmpty()
1316        throws Exception JavaDoc
1317    {
1318        String JavaDoc xml = "<web-app></web-app>";
1319        Document JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
1320        WebXml webXml = new WebXml(doc);
1321        assertTrue(!webXml.hasSecurityRole("someRole"));
1322        assertTrue(!webXml.getSecurityRoleNames().hasNext());
1323    }
1324
1325    /**
1326     * Tests whether a single security-role element is correctly retrieved.
1327     *
1328     * @throws Exception If an unexpected error occurs
1329     */

1330    public void testGetSingleSecurityRole()
1331        throws Exception JavaDoc
1332    {
1333        String JavaDoc xml = "<web-app>"
1334            + " <security-role>".trim()
1335            + " <role-name>r1</role-name>".trim()
1336            + " </security-role>".trim()
1337            + "</web-app>";
1338        Document JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
1339        WebXml webXml = new WebXml(doc);
1340        assertTrue(webXml.hasSecurityRole("r1"));
1341        Element JavaDoc 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 JavaDoc securityRoleNames = webXml.getSecurityRoleNames();
1349        assertTrue(securityRoleNames.hasNext());
1350        assertEquals("r1", securityRoleNames.next());
1351        assertTrue(!securityRoleNames.hasNext());
1352    }
1353
1354    /**
1355     * Tests whether multiple security-role elements are correctly retrieved
1356     * in the expected order.
1357     *
1358     * @throws Exception If an unexpected error occurs
1359     */

1360    public void testGetMutlipleSecurityRoles()
1361        throws Exception JavaDoc
1362    {
1363        String JavaDoc 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 JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
1375        WebXml webXml = new WebXml(doc);
1376        assertTrue(webXml.hasSecurityRole("r1"));
1377        Element JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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    /**
1414     * Tests whether a filter is inserted before a servlet element.
1415     *
1416     * @throws Exception If an unexpected error occurs
1417     */

1418    public void testElementOrderFilterBeforeServlet() throws Exception JavaDoc
1419    {
1420        String JavaDoc 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 JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
1427        WebXml webXml = new WebXml(doc);
1428        webXml.addFilter(createFilterElement(doc, "f1", "f1class"));
1429        NodeList JavaDoc order = doc.getDocumentElement().getChildNodes();
1430        assertEquals("filter", order.item(0).getNodeName());
1431        assertEquals("servlet", order.item(1).getNodeName());
1432    }
1433
1434    /**
1435     * Tests whether a filter is inserted before the comment node preceding a
1436     * servlet definition.
1437     *
1438     * @throws Exception If an unexpected error occurs
1439     */

1440    public void testElementOrderFilterBeforeServletWithComment()
1441        throws Exception JavaDoc
1442    {
1443        String JavaDoc 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 JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
1451        WebXml webXml = new WebXml(doc);
1452        webXml.addFilter(createFilterElement(doc, "f1", "f1class"));
1453        NodeList JavaDoc 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    /**
1460     * Tests whether a servlet is inserted after a filter.
1461     *
1462     * @throws Exception If an unexpected error occurs
1463     */

1464    public void testElementOrderServletAfterFilter() throws Exception JavaDoc
1465    {
1466        String JavaDoc 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 JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
1473        WebXml webXml = new WebXml(doc);
1474        webXml.addServlet(createServletElement(doc, "s1", "s1class"));
1475        NodeList JavaDoc order = doc.getDocumentElement().getChildNodes();
1476        assertEquals("filter", order.item(0).getNodeName());
1477        assertEquals("servlet", order.item(1).getNodeName());
1478    }
1479
1480    /**
1481     * Tests whether a servlet is inserted after a filter that is preceded by
1482     * a comment node.
1483     *
1484     * @throws Exception If an unexpected error occurs
1485     */

1486    public void testElementOrderServletAfterFilterWithComment()
1487        throws Exception JavaDoc
1488    {
1489        String JavaDoc 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 JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
1497        WebXml webXml = new WebXml(doc);
1498        webXml.addServlet(createServletElement(doc, "s1", "s1class"));
1499        NodeList JavaDoc 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    // Private Methods ---------------------------------------------------------
1506

1507    /**
1508     * Create a <code>context-param</code> element containing the specified
1509     * text in the child elements.
1510     *
1511     * @param theDocument The DOM document
1512     * @param theParamName The parameter name
1513     * @param theParamValue The parameter value
1514     * @return The created element
1515     */

1516    public Element JavaDoc createContextParamElement(Document JavaDoc theDocument,
1517        String JavaDoc theParamName, String JavaDoc theParamValue)
1518    {
1519        Element JavaDoc contextParamElement =
1520            theDocument.createElement("context-param");
1521        Element JavaDoc paramNameElement = theDocument.createElement("param-name");
1522        paramNameElement.appendChild(theDocument.createTextNode(theParamName));
1523        contextParamElement.appendChild(paramNameElement);
1524        Element JavaDoc paramValueElement = theDocument.createElement("param-value");
1525        paramValueElement.appendChild(
1526            theDocument.createTextNode(theParamValue));
1527        contextParamElement.appendChild(paramValueElement);
1528        return contextParamElement;
1529    }
1530    
1531    /**
1532     * Create a <code>filter</code> element containing the specified text in
1533     * the child elements.
1534     *
1535     * @param theDocument The DOM document
1536     * @param theFilterName The name of the filter
1537     * @param theFilterClass The name of the filter implementation class
1538     * @return The created element
1539     */

1540    public Element JavaDoc createFilterElement(Document JavaDoc theDocument,
1541            String JavaDoc theFilterName, String JavaDoc theFilterClass)
1542    {
1543        Element JavaDoc filterElement = theDocument.createElement("filter");
1544        Element JavaDoc filterNameElement = theDocument.createElement("filter-name");
1545        filterNameElement.appendChild(
1546            theDocument.createTextNode(theFilterName));
1547        filterElement.appendChild(filterNameElement);
1548        Element JavaDoc filterClassElement = theDocument.createElement("filter-class");
1549        filterClassElement.appendChild(
1550            theDocument.createTextNode(theFilterClass));
1551        filterElement.appendChild(filterClassElement);
1552        return filterElement;
1553    }
1554    
1555    /**
1556     * Create a <code>servlet</code> element containing the specified text in
1557     * the child elements.
1558     *
1559     * @param theDocument The DOM document
1560     * @param theServletName The name of the servlet
1561     * @param theServletClass The name of the servlet implementation class
1562     * @return The created element
1563     */

1564    public Element JavaDoc createServletElement(Document JavaDoc theDocument,
1565            String JavaDoc theServletName, String JavaDoc theServletClass)
1566    {
1567        Element JavaDoc filterElement = theDocument.createElement("servlet");
1568        Element JavaDoc filterNameElement = theDocument.createElement("servlet-name");
1569        filterNameElement.appendChild(
1570            theDocument.createTextNode(theServletName));
1571        filterElement.appendChild(filterNameElement);
1572        Element JavaDoc 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