KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Iterator JavaDoc;
24
25 import javax.xml.parsers.DocumentBuilder JavaDoc;
26 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
27 import javax.xml.parsers.ParserConfigurationException JavaDoc;
28
29 import junit.framework.TestCase;
30
31 import org.w3c.dom.Document JavaDoc;
32 import org.w3c.dom.Element JavaDoc;
33
34 /**
35  * Unit tests for {@link WebXmlMerger}.
36  *
37  * TODO: we need more tests for the security sections and the various references
38  *
39  * @version $Id: TestWebXmlMerger.java,v 1.1 2004/05/31 20:05:21 vmassol Exp $
40  */

41 public final class TestWebXmlMerger extends TestCase
42 {
43     /**
44      * The document builder factory.
45      */

46     private DocumentBuilderFactory JavaDoc factory;
47
48     /**
49      * The JAXP document builder.
50      */

51     private DocumentBuilder JavaDoc builder;
52
53     /**
54      * @see TestCase#setUp
55      */

56     public void setUp() throws ParserConfigurationException JavaDoc
57     {
58         factory = DocumentBuilderFactory.newInstance();
59         factory.setValidating(false);
60         factory.setNamespaceAware(false);
61
62         builder = factory.newDocumentBuilder();
63     }
64
65     /**
66      * Tests whether a single filter is correctly merged into an empty
67      * descriptor.
68      *
69      * @throws Exception If an unexpected error occurs
70      */

71     public void testMergeOneFilterIntoEmptyDocument() throws Exception JavaDoc
72     {
73         String JavaDoc srcXml = "<web-app></web-app>";
74         Document JavaDoc srcDoc =
75             builder.parse(new ByteArrayInputStream JavaDoc(srcXml.getBytes()));
76         WebXml srcWebXml = new WebXml(srcDoc);
77         String JavaDoc mergeXml = "<web-app>"
78             + " <filter>"
79             + " <filter-name>f1</filter-name>"
80             + " <filter-class>fclass1</filter-class>"
81             + " </filter>"
82             + "</web-app>";
83         Document JavaDoc mergeDoc =
84             builder.parse(new ByteArrayInputStream JavaDoc(mergeXml.getBytes()));
85         WebXml mergeWebXml = new WebXml(mergeDoc);
86         WebXmlMerger merger = new WebXmlMerger(srcWebXml);
87         merger.mergeFilters(mergeWebXml);
88         assertTrue(srcWebXml.hasFilter("f1"));
89     }
90
91     /**
92      * Tests whether a single context param is correctly merged into an empty
93      * descriptor.
94      *
95      * @throws Exception If an unexpected error occurs
96      */

97     public void testMergeOneContextParamIntoEmptyDocument() throws Exception JavaDoc
98     {
99         String JavaDoc srcXml = "<web-app></web-app>";
100         Document JavaDoc srcDoc =
101             builder.parse(new ByteArrayInputStream JavaDoc(srcXml.getBytes()));
102         WebXml srcWebXml = new WebXml(srcDoc);
103         String JavaDoc mergeXml = "<web-app>"
104             + " <context-param>"
105             + " <param-name>param</param-name>"
106             + " <param-value>value</param-value>"
107             + " </context-param>"
108             + "</web-app>";
109         Document JavaDoc mergeDoc =
110             builder.parse(new ByteArrayInputStream JavaDoc(mergeXml.getBytes()));
111         WebXml mergeWebXml = new WebXml(mergeDoc);
112         WebXmlMerger merger = new WebXmlMerger(srcWebXml);
113         merger.mergeContextParams(mergeWebXml);
114         assertTrue(srcWebXml.hasContextParam("param"));
115     }
116     
117     /**
118      * Tests whether a single filter is correctly merged into a descriptor that
119      * already contains another filter.
120      *
121      * @throws Exception If an unexpected error occurs
122      */

123     public void testMergeOneFilterIntoDocumentWithAnotherFilter()
124         throws Exception JavaDoc
125     {
126         String JavaDoc srcXml = "<web-app>"
127             + " <filter>"
128             + " <filter-name>f1</filter-name>"
129             + " <filter-class>fclass1</filter-class>"
130             + " </filter>"
131             + "</web-app>";
132         Document JavaDoc srcDoc =
133             builder.parse(new ByteArrayInputStream JavaDoc(srcXml.getBytes()));
134         WebXml srcWebXml = new WebXml(srcDoc);
135         String JavaDoc mergeXml = "<web-app>"
136             + " <filter>"
137             + " <filter-name>f2</filter-name>"
138             + " <filter-class>fclass2</filter-class>"
139             + " </filter>"
140             + "</web-app>";
141         Document JavaDoc mergeDoc =
142             builder.parse(new ByteArrayInputStream JavaDoc(mergeXml.getBytes()));
143         WebXml mergeWebXml = new WebXml(mergeDoc);
144         WebXmlMerger merger = new WebXmlMerger(srcWebXml);
145         merger.mergeFilters(mergeWebXml);
146         assertTrue(srcWebXml.hasFilter("f1"));
147         assertTrue(srcWebXml.hasFilter("f2"));
148     }
149
150     /**
151      * Tests whether a single context param is correctly merged into a
152      * descriptor that already contains another context param.
153      *
154      * @throws Exception If an unexpected error occurs
155      */

156     public void testMergeOneContextParamIntoDocumentWithAnotherContextParam()
157         throws Exception JavaDoc
158     {
159         String JavaDoc srcXml = "<web-app>"
160             + " <context-param>"
161             + " <param-name>param1</param-name>"
162             + " <param-value>value1</param-value>"
163             + " </context-param>"
164             + "</web-app>";
165         Document JavaDoc srcDoc =
166             builder.parse(new ByteArrayInputStream JavaDoc(srcXml.getBytes()));
167         WebXml srcWebXml = new WebXml(srcDoc);
168         String JavaDoc mergeXml = "<web-app>"
169             + " <context-param>"
170             + " <param-name>param2</param-name>"
171             + " <param-value>value2</param-value>"
172             + " </context-param>"
173             + "</web-app>";
174         Document JavaDoc mergeDoc =
175             builder.parse(new ByteArrayInputStream JavaDoc(mergeXml.getBytes()));
176         WebXml mergeWebXml = new WebXml(mergeDoc);
177         WebXmlMerger merger = new WebXmlMerger(srcWebXml);
178         merger.mergeContextParams(mergeWebXml);
179         assertTrue(srcWebXml.hasContextParam("param1"));
180         assertTrue(srcWebXml.hasContextParam("param2"));
181     }
182
183     /**
184      * Tests whether a single filter in the merge descriptor is ignored because
185      * a filter with the same name already exists in the source descriptor.
186      *
187      * @throws Exception If an unexpected error occurs
188      */

189     public void testMergeOneFilterIntoDocumentWithSameFilter()
190         throws Exception JavaDoc
191     {
192         String JavaDoc srcXml = "<web-app>"
193             + " <filter>"
194             + " <filter-name>f1</filter-name>"
195             + " <filter-class>fclass1</filter-class>"
196             + " </filter>"
197             + "</web-app>";
198         Document JavaDoc srcDoc =
199             builder.parse(new ByteArrayInputStream JavaDoc(srcXml.getBytes()));
200         WebXml srcWebXml = new WebXml(srcDoc);
201         String JavaDoc mergeXml = srcXml;
202         Document JavaDoc mergeDoc =
203             builder.parse(new ByteArrayInputStream JavaDoc(mergeXml.getBytes()));
204         WebXml mergeWebXml = new WebXml(mergeDoc);
205         WebXmlMerger merger = new WebXmlMerger(srcWebXml);
206         merger.mergeFilters(mergeWebXml);
207         assertTrue(srcWebXml.hasFilter("f1"));
208     }
209
210     /**
211      * Tests whether a single context param in the merge descriptor is ignored
212      * because a context param with the same name already exists in the source
213      * descriptor.
214      *
215      * @throws Exception If an unexpected error occurs
216      */

217     public void testMergeOneContextParamIntoDocumentWithSameContextParam()
218         throws Exception JavaDoc
219     {
220         String JavaDoc srcXml = "<web-app>"
221             + " <context-param>"
222             + " <param-name>param</param-name>"
223             + " <param-value>value</param-value>"
224             + " </context-param>"
225             + "</web-app>";
226         Document JavaDoc srcDoc =
227             builder.parse(new ByteArrayInputStream JavaDoc(srcXml.getBytes()));
228         WebXml srcWebXml = new WebXml(srcDoc);
229         String JavaDoc mergeXml = srcXml;
230         Document JavaDoc mergeDoc =
231             builder.parse(new ByteArrayInputStream JavaDoc(mergeXml.getBytes()));
232         WebXml mergeWebXml = new WebXml(mergeDoc);
233         WebXmlMerger merger = new WebXmlMerger(srcWebXml);
234         merger.mergeContextParams(mergeWebXml);
235         assertTrue(srcWebXml.hasContextParam("param"));
236     }
237     
238     /**
239      * Tests whether a filter initialization parameter is merged into the
240      * descriptor.
241      *
242      * @throws Exception If an unexpected error occurs
243      */

244     public void testMergeOneFilterIntoDocumentWithSameFilterAndParam()
245         throws Exception JavaDoc
246     {
247         String JavaDoc srcXml = "<web-app>"
248             + " <filter>"
249             + " <filter-name>f1</filter-name>"
250             + " <filter-class>fclass1</filter-class>"
251             + " </filter>"
252             + "</web-app>";
253         Document JavaDoc srcDoc =
254             builder.parse(new ByteArrayInputStream JavaDoc(srcXml.getBytes()));
255         WebXml srcWebXml = new WebXml(srcDoc);
256         String JavaDoc mergeXml = "<web-app>"
257             + " <filter>"
258             + " <filter-name>f1</filter-name>"
259             + " <filter-class>fclass1</filter-class>"
260             + " <init-param>"
261             + " <param-name>f1param1</param-name>"
262             + " <param-value>f1param1value</param-value>"
263             + " </init-param>"
264             + " </filter>"
265             + "</web-app>";
266         Document JavaDoc mergeDoc =
267             builder.parse(new ByteArrayInputStream JavaDoc(mergeXml.getBytes()));
268         WebXml mergeWebXml = new WebXml(mergeDoc);
269         WebXmlMerger merger = new WebXmlMerger(srcWebXml);
270         merger.mergeFilters(mergeWebXml);
271         assertTrue(srcWebXml.hasFilter("f1"));
272         Iterator JavaDoc initParams = srcWebXml.getFilterInitParamNames("f1");
273         assertEquals("f1param1", initParams.next());
274         assertTrue(!initParams.hasNext());
275     }
276
277     /**
278      * Tests whether a single filter is correctly merged into a descriptor that
279      * already contains multiple other filter definitions.
280      *
281      * @throws Exception If an unexpected error occurs
282      */

283     public void testMergeOneFilterIntoDocumentWithMultipleFilters()
284         throws Exception JavaDoc
285     {
286         String JavaDoc srcXml = "<web-app>"
287             + " <filter>"
288             + " <filter-name>f1</filter-name>"
289             + " <filter-class>fclass1</filter-class>"
290             + " </filter>"
291             + " <filter>"
292             + " <filter-name>f2</filter-name>"
293             + " <filter-class>fclass2</filter-class>"
294             + " </filter>"
295             + " <filter>"
296             + " <filter-name>f3</filter-name>"
297             + " <filter-class>fclass3</filter-class>"
298             + " </filter>"
299             + "</web-app>";
300         Document JavaDoc srcDoc =
301             builder.parse(new ByteArrayInputStream JavaDoc(srcXml.getBytes()));
302         WebXml srcWebXml = new WebXml(srcDoc);
303         String JavaDoc mergeXml = "<web-app>"
304             + " <filter>"
305             + " <filter-name>f4</filter-name>"
306             + " <filter-class>fclass4</filter-class>"
307             + " </filter>"
308             + "</web-app>";
309         Document JavaDoc mergeDoc =
310             builder.parse(new ByteArrayInputStream JavaDoc(mergeXml.getBytes()));
311         WebXml mergeWebXml = new WebXml(mergeDoc);
312         WebXmlMerger merger = new WebXmlMerger(srcWebXml);
313         merger.mergeFilters(mergeWebXml);
314         Iterator JavaDoc filterNames = srcWebXml.getFilterNames();
315         assertEquals("f1", filterNames.next());
316         assertEquals("f2", filterNames.next());
317         assertEquals("f3", filterNames.next());
318         assertEquals("f4", filterNames.next());
319         assertTrue(!filterNames.hasNext());
320     }
321
322     /**
323      * Tests whether multiple filters are correctly merged into an empty
324      * descriptor.
325      *
326      * @throws Exception If an unexpected error occurs
327      */

328     public void testMergeMultipleFiltersIntoEmptyDocument() throws Exception JavaDoc
329     {
330         String JavaDoc srcXml = "<web-app></web-app>";
331         Document JavaDoc srcDoc =
332             builder.parse(new ByteArrayInputStream JavaDoc(srcXml.getBytes()));
333         WebXml srcWebXml = new WebXml(srcDoc);
334         String JavaDoc mergeXml = "<web-app>"
335             + " <filter>"
336             + " <filter-name>f1</filter-name>"
337             + " <filter-class>fclass1</filter-class>"
338             + " </filter>"
339             + " <filter>"
340             + " <filter-name>f2</filter-name>"
341             + " <filter-class>fclass2</filter-class>"
342             + " </filter>"
343             + " <filter>"
344             + " <filter-name>f3</filter-name>"
345             + " <filter-class>fclass3</filter-class>"
346             + " </filter>"
347             + "</web-app>";
348         Document JavaDoc mergeDoc =
349             builder.parse(new ByteArrayInputStream JavaDoc(mergeXml.getBytes()));
350         WebXml mergeWebXml = new WebXml(mergeDoc);
351         WebXmlMerger merger = new WebXmlMerger(srcWebXml);
352         merger.mergeFilters(mergeWebXml);
353         Iterator JavaDoc filterNames = srcWebXml.getFilterNames();
354         assertEquals("f1", filterNames.next());
355         assertEquals("f2", filterNames.next());
356         assertEquals("f3", filterNames.next());
357         assertTrue(!filterNames.hasNext());
358     }
359
360     /**
361      * Tests whether a filter with one mapping is correctly merged into an empty
362      * descriptor.
363      *
364      * @throws Exception If an unexpected error occurs
365      */

366     public void testMergeOneFilterWithOneMappingIntoEmptyDocument()
367         throws Exception JavaDoc
368     {
369         String JavaDoc srcXml = "<web-app></web-app>";
370         Document JavaDoc srcDoc =
371             builder.parse(new ByteArrayInputStream JavaDoc(srcXml.getBytes()));
372         WebXml srcWebXml = new WebXml(srcDoc);
373         String JavaDoc mergeXml = "<web-app>"
374             + " <filter>"
375             + " <filter-name>f1</filter-name>"
376             + " <filter-class>fclass1</filter-class>"
377             + " </filter>"
378             + " <filter-mapping>"
379             + " <filter-name>f1</filter-name>"
380             + " <url-pattern>/f1mapping1</url-pattern>"
381             + " </filter-mapping>"
382             + "</web-app>";
383         Document JavaDoc mergeDoc =
384             builder.parse(new ByteArrayInputStream JavaDoc(mergeXml.getBytes()));
385         WebXml mergeWebXml = new WebXml(mergeDoc);
386         WebXmlMerger merger = new WebXmlMerger(srcWebXml);
387         merger.mergeFilters(mergeWebXml);
388         assertTrue(srcWebXml.hasFilter("f1"));
389         Iterator JavaDoc filterMappings = srcWebXml.getFilterMappings("f1");
390         assertEquals("/f1mapping1", filterMappings.next());
391         assertTrue(!filterMappings.hasNext());
392     }
393
394     /**
395      * Tests wether a single filter with multiple mappings is correctly merged
396      * into an empty descriptor.
397      *
398      * @throws Exception If an unexpected error occurs
399      */

400     public void testMergeOneFilterWithMultipleMappingsIntoEmptyDocument()
401         throws Exception JavaDoc
402     {
403         String JavaDoc srcXml = "<web-app></web-app>";
404         Document JavaDoc srcDoc =
405             builder.parse(new ByteArrayInputStream JavaDoc(srcXml.getBytes()));
406         WebXml srcWebXml = new WebXml(srcDoc);
407         String JavaDoc mergeXml = "<web-app>"
408             + " <filter>"
409             + " <filter-name>f1</filter-name>"
410             + " <filter-class>fclass1</filter-class>"
411             + " </filter>"
412             + " <filter-mapping>"
413             + " <filter-name>f1</filter-name>"
414             + " <url-pattern>/f1mapping1</url-pattern>"
415             + " </filter-mapping>"
416             + " <filter-mapping>"
417             + " <filter-name>f1</filter-name>"
418             + " <url-pattern>/f1mapping2</url-pattern>"
419             + " </filter-mapping>"
420             + " <filter-mapping>"
421             + " <filter-name>f1</filter-name>"
422             + " <url-pattern>/f1mapping3</url-pattern>"
423             + " </filter-mapping>"
424             + "</web-app>";
425         Document JavaDoc mergeDoc =
426             builder.parse(new ByteArrayInputStream JavaDoc(mergeXml.getBytes()));
427         WebXml mergeWebXml = new WebXml(mergeDoc);
428         WebXmlMerger merger = new WebXmlMerger(srcWebXml);
429         merger.mergeFilters(mergeWebXml);
430         assertTrue(srcWebXml.hasFilter("f1"));
431         Iterator JavaDoc filterMappings = srcWebXml.getFilterMappings("f1");
432         assertEquals("/f1mapping1", filterMappings.next());
433         assertEquals("/f1mapping2", filterMappings.next());
434         assertEquals("/f1mapping3", filterMappings.next());
435         assertTrue(!filterMappings.hasNext());
436     }
437
438     /**
439      * Tests whether a single servlet is correctly merged into an empty
440      * descriptor.
441      *
442      * @throws Exception If an unexpected error occurs
443      */

444     public void testMergeOneServletIntoEmptyDocument() throws Exception JavaDoc
445     {
446         String JavaDoc srcXml = "<web-app></web-app>";
447         Document JavaDoc srcDoc =
448             builder.parse(new ByteArrayInputStream JavaDoc(srcXml.getBytes()));
449         WebXml srcWebXml = new WebXml(srcDoc);
450         String JavaDoc mergeXml = "<web-app>"
451             + " <servlet>"
452             + " <servlet-name>s1</servlet-name>"
453             + " <servlet-class>sclass1</servlet-class>"
454             + " </servlet>"
455             + "</web-app>";
456         Document JavaDoc mergeDoc =
457             builder.parse(new ByteArrayInputStream JavaDoc(mergeXml.getBytes()));
458         WebXml mergeWebXml = new WebXml(mergeDoc);
459         WebXmlMerger merger = new WebXmlMerger(srcWebXml);
460         merger.mergeServlets(mergeWebXml);
461         assertTrue(srcWebXml.hasServlet("s1"));
462     }
463
464     /**
465      * Tests whether a single servlet is correctly merged into a descriptor that
466      * already contains the definition of an other servlet.
467      *
468      * @throws Exception If an unexpected error occurs
469      */

470     public void testMergeOneServletIntoDocumentWithAnotherServlet()
471         throws Exception JavaDoc
472     {
473         String JavaDoc srcXml = "<web-app>"
474             + " <servlet>"
475             + " <servlet-name>s1</servlet-name>"
476             + " <servlet-class>sclass1</servlet-class>"
477             + " </servlet>"
478             + "</web-app>";
479         Document JavaDoc srcDoc =
480             builder.parse(new ByteArrayInputStream JavaDoc(srcXml.getBytes()));
481         WebXml srcWebXml = new WebXml(srcDoc);
482         String JavaDoc mergeXml = "<web-app>"
483             + " <servlet>"
484             + " <servlet-name>s2</servlet-name>"
485             + " <servlet-class>sclass2</servlet-class>"
486             + " </servlet>"
487             + "</web-app>";
488         Document JavaDoc mergeDoc =
489             builder.parse(new ByteArrayInputStream JavaDoc(mergeXml.getBytes()));
490         WebXml mergeWebXml = new WebXml(mergeDoc);
491         WebXmlMerger merger = new WebXmlMerger(srcWebXml);
492         merger.mergeServlets(mergeWebXml);
493         assertTrue(srcWebXml.hasServlet("s1"));
494         assertTrue(srcWebXml.hasServlet("s2"));
495     }
496
497     /**
498      * Tests whether a single servlet is correctly merged into a descriptor that
499      * already contains the definition of a servlet with the same name.
500      *
501      * @throws Exception If an unexpected error occurs
502      */

503     public void testMergeOneServletIntoDocumentWithSameServlet()
504         throws Exception JavaDoc
505     {
506         String JavaDoc srcXml = "<web-app>"
507             + " <servlet>"
508             + " <servlet-name>s1</servlet-name>"
509             + " <servlet-class>sclass1</servlet-class>"
510             + " </servlet>"
511             + "</web-app>";
512         Document JavaDoc srcDoc =
513             builder.parse(new ByteArrayInputStream JavaDoc(srcXml.getBytes()));
514         WebXml srcWebXml = new WebXml(srcDoc);
515         String JavaDoc mergeXml = "<web-app>"
516             + " <servlet>"
517             + " <servlet-name>s1</servlet-name>"
518             + " <servlet-class>sclass1</servlet-class>"
519             + " </servlet>"
520             + "</web-app>";
521         Document JavaDoc mergeDoc =
522             builder.parse(new ByteArrayInputStream JavaDoc(mergeXml.getBytes()));
523         WebXml mergeWebXml = new WebXml(mergeDoc);
524         WebXmlMerger merger = new WebXmlMerger(srcWebXml);
525         merger.mergeServlets(mergeWebXml);
526         assertTrue(srcWebXml.hasServlet("s1"));
527     }
528
529     /**
530      * Tets whether a servlet with an initialization parameter is correctly
531      * merged into a descriptor that contains the definition of a servlet with
532      * the same name.
533      *
534      * @throws Exception If an unexpected error occurs
535      */

536     public void testMergeOneServletIntoDocumentWithSameServletAndParam()
537         throws Exception JavaDoc
538     {
539         String JavaDoc srcXml = "<web-app>"
540             + " <servlet>"
541             + " <servlet-name>s1</servlet-name>"
542             + " <servlet-class>sclass1</servlet-class>"
543             + " </servlet>"
544             + "</web-app>";
545         Document JavaDoc srcDoc =
546             builder.parse(new ByteArrayInputStream JavaDoc(srcXml.getBytes()));
547         WebXml srcWebXml = new WebXml(srcDoc);
548         String JavaDoc mergeXml = "<web-app>"
549             + " <servlet>"
550             + " <servlet-name>s1</servlet-name>"
551             + " <servlet-class>sclass1</servlet-class>"
552             + " <init-param>"
553             + " <param-name>s1param1</param-name>"
554             + " <param-value>s1param1value</param-value>"
555             + " </init-param>"
556             + " </servlet>"
557             + "</web-app>";
558         Document JavaDoc mergeDoc =
559             builder.parse(new ByteArrayInputStream JavaDoc(mergeXml.getBytes()));
560         WebXml mergeWebXml = new WebXml(mergeDoc);
561         WebXmlMerger merger = new WebXmlMerger(srcWebXml);
562         merger.mergeServlets(mergeWebXml);
563         assertTrue(srcWebXml.hasServlet("s1"));
564         Iterator JavaDoc initParams = srcWebXml.getServletInitParamNames("s1");
565         assertEquals("s1param1", initParams.next());
566         assertTrue(!initParams.hasNext());
567         assertEquals("s1param1value",
568             srcWebXml.getServletInitParam("s1", "s1param1"));
569     }
570
571     /**
572      * Tests whether a single servlet is correctly merged into a descriptor with
573      * multiple servlets.
574      *
575      * @throws Exception If an unexpected error occurs
576      */

577     public void testMergeOneServletIntoDocumentWithMultipleServlets()
578         throws Exception JavaDoc
579     {
580         String JavaDoc srcXml = "<web-app>"
581             + " <servlet>"
582             + " <servlet-name>s1</servlet-name>"
583             + " <servlet-class>sclass1</servlet-class>"
584             + " </servlet>"
585             + " <servlet>"
586             + " <servlet-name>s2</servlet-name>"
587             + " <servlet-class>sclass2</servlet-class>"
588             + " </servlet>"
589             + " <servlet>"
590             + " <servlet-name>s3</servlet-name>"
591             + " <servlet-class>sclass3</servlet-class>"
592             + " </servlet>"
593             + "</web-app>";
594         Document JavaDoc srcDoc =
595             builder.parse(new ByteArrayInputStream JavaDoc(srcXml.getBytes()));
596         WebXml srcWebXml = new WebXml(srcDoc);
597         String JavaDoc mergeXml = "<web-app>"
598             + " <servlet>"
599             + " <servlet-name>s4</servlet-name>"
600             + " <servlet-class>sclass4</servlet-class>"
601             + " </servlet>"
602             + "</web-app>";
603         Document JavaDoc mergeDoc =
604             builder.parse(new ByteArrayInputStream JavaDoc(mergeXml.getBytes()));
605         WebXml mergeWebXml = new WebXml(mergeDoc);
606         WebXmlMerger merger = new WebXmlMerger(srcWebXml);
607         merger.mergeServlets(mergeWebXml);
608         Iterator JavaDoc servletNames = srcWebXml.getServletNames();
609         assertEquals("s1", servletNames.next());
610         assertEquals("s2", servletNames.next());
611         assertEquals("s3", servletNames.next());
612         assertEquals("s4", servletNames.next());
613         assertTrue(!servletNames.hasNext());
614     }
615
616     /**
617      * Verifies that servlet init parameters are added after the load-on-startup
618      * element of an already existing servlet definition.
619      *
620      * @throws Exception If an unexpected error occurs
621      */

622     public void testMergingServletWithInitParamsThatIsAlreadyDefined()
623         throws Exception JavaDoc
624     {
625         String JavaDoc srcXml = "<web-app>".trim()
626             + " <servlet>".trim()
627             + " <servlet-name>s1</servlet-name>".trim()
628             + " <servlet-class>sclass1</servlet-class>".trim()
629             + " <load-on-startup>1</load-on-startup>".trim()
630             + " </servlet>".trim()
631             + "</web-app>";
632         Document JavaDoc srcDoc =
633             builder.parse(new ByteArrayInputStream JavaDoc(srcXml.getBytes()));
634         WebXml srcWebXml = new WebXml(srcDoc);
635         String JavaDoc mergeXml = "<web-app>"
636             + " <servlet>".trim()
637             + " <servlet-name>s1</servlet-name>".trim()
638             + " <servlet-class>sclass1</servlet-class>".trim()
639             + " <init-param>".trim()
640             + " <param-name>s1param1</param-name>".trim()
641             + " <param-value>s1param1value</param-value>".trim()
642             + " </init-param>".trim()
643             + " </servlet>".trim()
644             + "</web-app>";
645         Document JavaDoc mergeDoc =
646             builder.parse(new ByteArrayInputStream JavaDoc(mergeXml.getBytes()));
647         WebXml mergeWebXml = new WebXml(mergeDoc);
648         WebXmlMerger merger = new WebXmlMerger(srcWebXml);
649         merger.mergeServlets(mergeWebXml);
650         Element JavaDoc servletElement = srcWebXml.getServlet("s1");
651         assertEquals("load-on-startup",
652             ((Element JavaDoc) servletElement.getLastChild()).getTagName());
653     }
654
655     /**
656      * Tests whether multiple servlet in the merge file are correctly inserted
657      * into an empty descriptor.
658      *
659      * @throws Exception If an unexpected error occurs
660      */

661     public void testMergeMultipleServletsIntoEmptyDocument() throws Exception JavaDoc
662     {
663         String JavaDoc srcXml = "<web-app></web-app>";
664         Document JavaDoc srcDoc =
665             builder.parse(new ByteArrayInputStream JavaDoc(srcXml.getBytes()));
666         WebXml srcWebXml = new WebXml(srcDoc);
667         String JavaDoc mergeXml = "<web-app>"
668             + " <servlet>"
669             + " <servlet-name>s1</servlet-name>"
670             + " <servlet-class>sclass1</servlet-class>"
671             + " </servlet>"
672             + " <servlet>"
673             + " <servlet-name>s2</servlet-name>"
674             + " <servlet-class>sclass2</servlet-class>"
675             + " </servlet>"
676             + " <servlet>"
677             + " <servlet-name>s3</servlet-name>"
678             + " <servlet-class>sclass3</servlet-class>"
679             + " </servlet>"
680             + "</web-app>";
681         Document JavaDoc mergeDoc =
682             builder.parse(new ByteArrayInputStream JavaDoc(mergeXml.getBytes()));
683         WebXml mergeWebXml = new WebXml(mergeDoc);
684         WebXmlMerger merger = new WebXmlMerger(srcWebXml);
685         merger.mergeServlets(mergeWebXml);
686         Iterator JavaDoc servletNames = srcWebXml.getServletNames();
687         assertEquals("s1", servletNames.next());
688         assertEquals("s2", servletNames.next());
689         assertEquals("s3", servletNames.next());
690         assertTrue(!servletNames.hasNext());
691     }
692
693     /**
694      * Tests whether a single servlet with one mapping is correctly inserted
695      * into an empty descriptor.
696      *
697      * @throws Exception If an unexpected error occurs
698      */

699     public void testMergeOneServletWithOneMappingIntoEmptyDocument()
700         throws Exception JavaDoc
701     {
702         String JavaDoc srcXml = "<web-app></web-app>";
703         Document JavaDoc srcDoc =
704             builder.parse(new ByteArrayInputStream JavaDoc(srcXml.getBytes()));
705         WebXml srcWebXml = new WebXml(srcDoc);
706         String JavaDoc mergeXml = "<web-app>"
707             + " <servlet>"
708             + " <servlet-name>s1</servlet-name>"
709             + " <servlet-class>sclass1</servlet-class>"
710             + " </servlet>"
711             + " <servlet-mapping>"
712             + " <servlet-name>s1</servlet-name>"
713             + " <url-pattern>/s1mapping1</url-pattern>"
714             + " </servlet-mapping>"
715             + "</web-app>";
716         Document JavaDoc mergeDoc =
717             builder.parse(new ByteArrayInputStream JavaDoc(mergeXml.getBytes()));
718         WebXml mergeWebXml = new WebXml(mergeDoc);
719         WebXmlMerger merger = new WebXmlMerger(srcWebXml);
720         merger.mergeServlets(mergeWebXml);
721         assertTrue(srcWebXml.hasServlet("s1"));
722         Iterator JavaDoc servletMappings = srcWebXml.getServletMappings("s1");
723         assertEquals("/s1mapping1", servletMappings.next());
724         assertTrue(!servletMappings.hasNext());
725     }
726
727     /**
728      * Tests whether a single servlet with multiple mappings is correctly
729      * inserted into an empty descriptor.
730      *
731      * @throws Exception If an unexpected error occurs
732      */

733     public void testMergeOneServletWithMultipleMappingsIntoEmptyDocument()
734         throws Exception JavaDoc
735     {
736         String JavaDoc srcXml = "<web-app></web-app>";
737         Document JavaDoc srcDoc =
738             builder.parse(new ByteArrayInputStream JavaDoc(srcXml.getBytes()));
739         WebXml srcWebXml = new WebXml(srcDoc);
740         String JavaDoc mergeXml = "<web-app>"
741             + " <servlet>"
742             + " <servlet-name>s1</servlet-name>"
743             + " <servlet-class>sclass1</servlet-class>"
744             + " </servlet>"
745             + " <servlet-mapping>"
746             + " <servlet-name>s1</servlet-name>"
747             + " <url-pattern>/s1mapping1</url-pattern>"
748             + " </servlet-mapping>"
749             + " <servlet-mapping>"
750             + " <servlet-name>s1</servlet-name>"
751             + " <url-pattern>/s1mapping2</url-pattern>"
752             + " </servlet-mapping>"
753             + " <servlet-mapping>"
754             + " <servlet-name>s1</servlet-name>"
755             + " <url-pattern>/s1mapping3</url-pattern>"
756             + " </servlet-mapping>"
757             + "</web-app>";
758         Document JavaDoc mergeDoc =
759             builder.parse(new ByteArrayInputStream JavaDoc(mergeXml.getBytes()));
760         WebXml mergeWebXml = new WebXml(mergeDoc);
761         WebXmlMerger merger = new WebXmlMerger(srcWebXml);
762         merger.mergeServlets(mergeWebXml);
763         assertTrue(srcWebXml.hasServlet("s1"));
764         Iterator JavaDoc servletMappings = srcWebXml.getServletMappings("s1");
765         assertEquals("/s1mapping1", servletMappings.next());
766         assertEquals("/s1mapping2", servletMappings.next());
767         assertEquals("/s1mapping3", servletMappings.next());
768         assertTrue(!servletMappings.hasNext());
769     }
770
771     /**
772      * Tests whether a single security role is correctly inserted into an empty
773      * descriptor.
774      *
775      * @throws Exception If an unexpected error occurs
776      */

777     public void testMergeSecurityRoleIntoEmptyDocument()
778         throws Exception JavaDoc
779     {
780         String JavaDoc srcXml = "<web-app></web-app>";
781         Document JavaDoc srcDoc =
782             builder.parse(new ByteArrayInputStream JavaDoc(srcXml.getBytes()));
783         WebXml srcWebXml = new WebXml(srcDoc);
784         String JavaDoc mergeXml = "<web-app>"
785             + " <security-role>"
786             + " <role-name>role1</role-name>"
787             + " </security-role>"
788             + "</web-app>";
789         Document JavaDoc mergeDoc =
790             builder.parse(new ByteArrayInputStream JavaDoc(mergeXml.getBytes()));
791         WebXml mergeWebXml = new WebXml(mergeDoc);
792         WebXmlMerger merger = new WebXmlMerger(srcWebXml);
793         merger.mergeSecurityRoles(mergeWebXml);
794         Iterator JavaDoc securityRoleNames = srcWebXml.getSecurityRoleNames();
795         assertTrue(securityRoleNames.hasNext());
796         assertEquals("role1", securityRoleNames.next());
797         assertTrue(!securityRoleNames.hasNext());
798     }
799
800     /**
801      * Tests whether a single security role is ignored when the source
802      * descriptor already contains a role with the same name.
803      *
804      * @throws Exception If an unexpected error occurs
805      */

806     public void testMergeSecurityRoleIntoDocumentWithSameRole()
807         throws Exception JavaDoc
808     {
809         String JavaDoc srcXml = "<web-app>"
810             + " <security-role>"
811             + " <description>A role</description>"
812             + " <role-name>role1</role-name>"
813             + " </security-role>"
814             + "</web-app>";
815         Document JavaDoc srcDoc =
816             builder.parse(new ByteArrayInputStream JavaDoc(srcXml.getBytes()));
817         WebXml srcWebXml = new WebXml(srcDoc);
818         String JavaDoc mergeXml = "<web-app>"
819             + " <security-role>"
820             + " <role-name>role1</role-name>"
821             + " </security-role>"
822             + "</web-app>";
823         Document JavaDoc mergeDoc =
824             builder.parse(new ByteArrayInputStream JavaDoc(mergeXml.getBytes()));
825         WebXml mergeWebXml = new WebXml(mergeDoc);
826         WebXmlMerger merger = new WebXmlMerger(srcWebXml);
827         merger.mergeSecurityRoles(mergeWebXml);
828         Iterator JavaDoc securityRoleNames = srcWebXml.getSecurityRoleNames();
829         assertTrue(securityRoleNames.hasNext());
830         assertEquals("role1", securityRoleNames.next());
831         assertTrue(!securityRoleNames.hasNext());
832     }
833
834     /**
835      * Tests whether a single EJB reference is correctly inserted into an empty
836      * descriptor.
837      *
838      * @throws Exception If an unexpected error occurs
839      */

840     public void testMergeOneEjbRefIntoEmptyDocument()
841         throws Exception JavaDoc
842     {
843         String JavaDoc srcXml = "<web-app></web-app>";
844         Document JavaDoc srcDoc =
845             builder.parse(new ByteArrayInputStream JavaDoc(srcXml.getBytes()));
846         WebXml srcWebXml = new WebXml(srcDoc);
847         String JavaDoc mergeXml = "<web-app>"
848             + " <ejb-ref>"
849             + " <ejb-ref-name>ejbref1</ejb-ref-name>"
850             + " <ejb-ref-type>ejbref1.type</ejb-ref-type>"
851             + " <home>ejbref1.homeInterface</home>"
852             + " <remote>ejbref1.remoteInterface</remote>"
853             + " </ejb-ref>"
854             + "</web-app>";
855         Document JavaDoc mergeDoc =
856             builder.parse(new ByteArrayInputStream JavaDoc(mergeXml.getBytes()));
857         WebXml mergeWebXml = new WebXml(mergeDoc);
858         WebXmlMerger merger = new WebXmlMerger(srcWebXml);
859         merger.mergeEjbRefs(mergeWebXml);
860         Iterator JavaDoc ejbRefs = srcWebXml.getElements(WebXmlTag.EJB_REF);
861         assertTrue(ejbRefs.hasNext());
862     }
863
864 }
865
Popular Tags