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