001: /*
002: * ========================================================================
003: *
004: * Copyright 2003-2004 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * ========================================================================
019: */
020: package org.apache.cactus.integration.ant.deployment.webapp;
021:
022: import java.io.ByteArrayInputStream;
023: import java.util.Iterator;
024:
025: import javax.xml.parsers.DocumentBuilder;
026: import javax.xml.parsers.DocumentBuilderFactory;
027: import javax.xml.parsers.ParserConfigurationException;
028:
029: import junit.framework.TestCase;
030:
031: import org.w3c.dom.Document;
032: import org.w3c.dom.Element;
033:
034: /**
035: * Unit tests for {@link WebXmlMerger}.
036: *
037: * TODO: we need more tests for the security sections and the various references
038: *
039: * @version $Id: TestWebXmlMerger.java 239003 2004-05-31 20:05:27Z vmassol $
040: */
041: public final class TestWebXmlMerger extends TestCase {
042: /**
043: * The document builder factory.
044: */
045: private DocumentBuilderFactory factory;
046:
047: /**
048: * The JAXP document builder.
049: */
050: private DocumentBuilder builder;
051:
052: /**
053: * @see TestCase#setUp
054: */
055: public void setUp() throws ParserConfigurationException {
056: factory = DocumentBuilderFactory.newInstance();
057: factory.setValidating(false);
058: factory.setNamespaceAware(false);
059:
060: builder = factory.newDocumentBuilder();
061: }
062:
063: /**
064: * Tests whether a single filter is correctly merged into an empty
065: * descriptor.
066: *
067: * @throws Exception If an unexpected error occurs
068: */
069: public void testMergeOneFilterIntoEmptyDocument() throws Exception {
070: String srcXml = "<web-app></web-app>";
071: Document srcDoc = builder.parse(new ByteArrayInputStream(srcXml
072: .getBytes()));
073: WebXml srcWebXml = new WebXml(srcDoc);
074: String mergeXml = "<web-app>" + " <filter>"
075: + " <filter-name>f1</filter-name>"
076: + " <filter-class>fclass1</filter-class>"
077: + " </filter>" + "</web-app>";
078: Document mergeDoc = builder.parse(new ByteArrayInputStream(
079: mergeXml.getBytes()));
080: WebXml mergeWebXml = new WebXml(mergeDoc);
081: WebXmlMerger merger = new WebXmlMerger(srcWebXml);
082: merger.mergeFilters(mergeWebXml);
083: assertTrue(srcWebXml.hasFilter("f1"));
084: }
085:
086: /**
087: * Tests whether a single context param is correctly merged into an empty
088: * descriptor.
089: *
090: * @throws Exception If an unexpected error occurs
091: */
092: public void testMergeOneContextParamIntoEmptyDocument()
093: throws Exception {
094: String srcXml = "<web-app></web-app>";
095: Document srcDoc = builder.parse(new ByteArrayInputStream(srcXml
096: .getBytes()));
097: WebXml srcWebXml = new WebXml(srcDoc);
098: String mergeXml = "<web-app>" + " <context-param>"
099: + " <param-name>param</param-name>"
100: + " <param-value>value</param-value>"
101: + " </context-param>" + "</web-app>";
102: Document mergeDoc = builder.parse(new ByteArrayInputStream(
103: mergeXml.getBytes()));
104: WebXml mergeWebXml = new WebXml(mergeDoc);
105: WebXmlMerger merger = new WebXmlMerger(srcWebXml);
106: merger.mergeContextParams(mergeWebXml);
107: assertTrue(srcWebXml.hasContextParam("param"));
108: }
109:
110: /**
111: * Tests whether a single filter is correctly merged into a descriptor that
112: * already contains another filter.
113: *
114: * @throws Exception If an unexpected error occurs
115: */
116: public void testMergeOneFilterIntoDocumentWithAnotherFilter()
117: throws Exception {
118: String srcXml = "<web-app>" + " <filter>"
119: + " <filter-name>f1</filter-name>"
120: + " <filter-class>fclass1</filter-class>"
121: + " </filter>" + "</web-app>";
122: Document srcDoc = builder.parse(new ByteArrayInputStream(srcXml
123: .getBytes()));
124: WebXml srcWebXml = new WebXml(srcDoc);
125: String mergeXml = "<web-app>" + " <filter>"
126: + " <filter-name>f2</filter-name>"
127: + " <filter-class>fclass2</filter-class>"
128: + " </filter>" + "</web-app>";
129: Document mergeDoc = builder.parse(new ByteArrayInputStream(
130: mergeXml.getBytes()));
131: WebXml mergeWebXml = new WebXml(mergeDoc);
132: WebXmlMerger merger = new WebXmlMerger(srcWebXml);
133: merger.mergeFilters(mergeWebXml);
134: assertTrue(srcWebXml.hasFilter("f1"));
135: assertTrue(srcWebXml.hasFilter("f2"));
136: }
137:
138: /**
139: * Tests whether a single context param is correctly merged into a
140: * descriptor that already contains another context param.
141: *
142: * @throws Exception If an unexpected error occurs
143: */
144: public void testMergeOneContextParamIntoDocumentWithAnotherContextParam()
145: throws Exception {
146: String srcXml = "<web-app>" + " <context-param>"
147: + " <param-name>param1</param-name>"
148: + " <param-value>value1</param-value>"
149: + " </context-param>" + "</web-app>";
150: Document srcDoc = builder.parse(new ByteArrayInputStream(srcXml
151: .getBytes()));
152: WebXml srcWebXml = new WebXml(srcDoc);
153: String mergeXml = "<web-app>" + " <context-param>"
154: + " <param-name>param2</param-name>"
155: + " <param-value>value2</param-value>"
156: + " </context-param>" + "</web-app>";
157: Document mergeDoc = builder.parse(new ByteArrayInputStream(
158: mergeXml.getBytes()));
159: WebXml mergeWebXml = new WebXml(mergeDoc);
160: WebXmlMerger merger = new WebXmlMerger(srcWebXml);
161: merger.mergeContextParams(mergeWebXml);
162: assertTrue(srcWebXml.hasContextParam("param1"));
163: assertTrue(srcWebXml.hasContextParam("param2"));
164: }
165:
166: /**
167: * Tests whether a single filter in the merge descriptor is ignored because
168: * a filter with the same name already exists in the source descriptor.
169: *
170: * @throws Exception If an unexpected error occurs
171: */
172: public void testMergeOneFilterIntoDocumentWithSameFilter()
173: throws Exception {
174: String srcXml = "<web-app>" + " <filter>"
175: + " <filter-name>f1</filter-name>"
176: + " <filter-class>fclass1</filter-class>"
177: + " </filter>" + "</web-app>";
178: Document srcDoc = builder.parse(new ByteArrayInputStream(srcXml
179: .getBytes()));
180: WebXml srcWebXml = new WebXml(srcDoc);
181: String mergeXml = srcXml;
182: Document mergeDoc = builder.parse(new ByteArrayInputStream(
183: mergeXml.getBytes()));
184: WebXml mergeWebXml = new WebXml(mergeDoc);
185: WebXmlMerger merger = new WebXmlMerger(srcWebXml);
186: merger.mergeFilters(mergeWebXml);
187: assertTrue(srcWebXml.hasFilter("f1"));
188: }
189:
190: /**
191: * Tests whether a single context param in the merge descriptor is ignored
192: * because a context param with the same name already exists in the source
193: * descriptor.
194: *
195: * @throws Exception If an unexpected error occurs
196: */
197: public void testMergeOneContextParamIntoDocumentWithSameContextParam()
198: throws Exception {
199: String srcXml = "<web-app>" + " <context-param>"
200: + " <param-name>param</param-name>"
201: + " <param-value>value</param-value>"
202: + " </context-param>" + "</web-app>";
203: Document srcDoc = builder.parse(new ByteArrayInputStream(srcXml
204: .getBytes()));
205: WebXml srcWebXml = new WebXml(srcDoc);
206: String mergeXml = srcXml;
207: Document mergeDoc = builder.parse(new ByteArrayInputStream(
208: mergeXml.getBytes()));
209: WebXml mergeWebXml = new WebXml(mergeDoc);
210: WebXmlMerger merger = new WebXmlMerger(srcWebXml);
211: merger.mergeContextParams(mergeWebXml);
212: assertTrue(srcWebXml.hasContextParam("param"));
213: }
214:
215: /**
216: * Tests whether a filter initialization parameter is merged into the
217: * descriptor.
218: *
219: * @throws Exception If an unexpected error occurs
220: */
221: public void testMergeOneFilterIntoDocumentWithSameFilterAndParam()
222: throws Exception {
223: String srcXml = "<web-app>" + " <filter>"
224: + " <filter-name>f1</filter-name>"
225: + " <filter-class>fclass1</filter-class>"
226: + " </filter>" + "</web-app>";
227: Document srcDoc = builder.parse(new ByteArrayInputStream(srcXml
228: .getBytes()));
229: WebXml srcWebXml = new WebXml(srcDoc);
230: String mergeXml = "<web-app>" + " <filter>"
231: + " <filter-name>f1</filter-name>"
232: + " <filter-class>fclass1</filter-class>"
233: + " <init-param>"
234: + " <param-name>f1param1</param-name>"
235: + " <param-value>f1param1value</param-value>"
236: + " </init-param>" + " </filter>" + "</web-app>";
237: Document mergeDoc = builder.parse(new ByteArrayInputStream(
238: mergeXml.getBytes()));
239: WebXml mergeWebXml = new WebXml(mergeDoc);
240: WebXmlMerger merger = new WebXmlMerger(srcWebXml);
241: merger.mergeFilters(mergeWebXml);
242: assertTrue(srcWebXml.hasFilter("f1"));
243: Iterator initParams = srcWebXml.getFilterInitParamNames("f1");
244: assertEquals("f1param1", initParams.next());
245: assertTrue(!initParams.hasNext());
246: }
247:
248: /**
249: * Tests whether a single filter is correctly merged into a descriptor that
250: * already contains multiple other filter definitions.
251: *
252: * @throws Exception If an unexpected error occurs
253: */
254: public void testMergeOneFilterIntoDocumentWithMultipleFilters()
255: throws Exception {
256: String srcXml = "<web-app>" + " <filter>"
257: + " <filter-name>f1</filter-name>"
258: + " <filter-class>fclass1</filter-class>"
259: + " </filter>" + " <filter>"
260: + " <filter-name>f2</filter-name>"
261: + " <filter-class>fclass2</filter-class>"
262: + " </filter>" + " <filter>"
263: + " <filter-name>f3</filter-name>"
264: + " <filter-class>fclass3</filter-class>"
265: + " </filter>" + "</web-app>";
266: Document srcDoc = builder.parse(new ByteArrayInputStream(srcXml
267: .getBytes()));
268: WebXml srcWebXml = new WebXml(srcDoc);
269: String mergeXml = "<web-app>" + " <filter>"
270: + " <filter-name>f4</filter-name>"
271: + " <filter-class>fclass4</filter-class>"
272: + " </filter>" + "</web-app>";
273: Document mergeDoc = builder.parse(new ByteArrayInputStream(
274: mergeXml.getBytes()));
275: WebXml mergeWebXml = new WebXml(mergeDoc);
276: WebXmlMerger merger = new WebXmlMerger(srcWebXml);
277: merger.mergeFilters(mergeWebXml);
278: Iterator filterNames = srcWebXml.getFilterNames();
279: assertEquals("f1", filterNames.next());
280: assertEquals("f2", filterNames.next());
281: assertEquals("f3", filterNames.next());
282: assertEquals("f4", filterNames.next());
283: assertTrue(!filterNames.hasNext());
284: }
285:
286: /**
287: * Tests whether multiple filters are correctly merged into an empty
288: * descriptor.
289: *
290: * @throws Exception If an unexpected error occurs
291: */
292: public void testMergeMultipleFiltersIntoEmptyDocument()
293: throws Exception {
294: String srcXml = "<web-app></web-app>";
295: Document srcDoc = builder.parse(new ByteArrayInputStream(srcXml
296: .getBytes()));
297: WebXml srcWebXml = new WebXml(srcDoc);
298: String mergeXml = "<web-app>" + " <filter>"
299: + " <filter-name>f1</filter-name>"
300: + " <filter-class>fclass1</filter-class>"
301: + " </filter>" + " <filter>"
302: + " <filter-name>f2</filter-name>"
303: + " <filter-class>fclass2</filter-class>"
304: + " </filter>" + " <filter>"
305: + " <filter-name>f3</filter-name>"
306: + " <filter-class>fclass3</filter-class>"
307: + " </filter>" + "</web-app>";
308: Document mergeDoc = builder.parse(new ByteArrayInputStream(
309: mergeXml.getBytes()));
310: WebXml mergeWebXml = new WebXml(mergeDoc);
311: WebXmlMerger merger = new WebXmlMerger(srcWebXml);
312: merger.mergeFilters(mergeWebXml);
313: Iterator filterNames = srcWebXml.getFilterNames();
314: assertEquals("f1", filterNames.next());
315: assertEquals("f2", filterNames.next());
316: assertEquals("f3", filterNames.next());
317: assertTrue(!filterNames.hasNext());
318: }
319:
320: /**
321: * Tests whether a filter with one mapping is correctly merged into an empty
322: * descriptor.
323: *
324: * @throws Exception If an unexpected error occurs
325: */
326: public void testMergeOneFilterWithOneMappingIntoEmptyDocument()
327: throws Exception {
328: String srcXml = "<web-app></web-app>";
329: Document srcDoc = builder.parse(new ByteArrayInputStream(srcXml
330: .getBytes()));
331: WebXml srcWebXml = new WebXml(srcDoc);
332: String mergeXml = "<web-app>" + " <filter>"
333: + " <filter-name>f1</filter-name>"
334: + " <filter-class>fclass1</filter-class>"
335: + " </filter>" + " <filter-mapping>"
336: + " <filter-name>f1</filter-name>"
337: + " <url-pattern>/f1mapping1</url-pattern>"
338: + " </filter-mapping>" + "</web-app>";
339: Document mergeDoc = builder.parse(new ByteArrayInputStream(
340: mergeXml.getBytes()));
341: WebXml mergeWebXml = new WebXml(mergeDoc);
342: WebXmlMerger merger = new WebXmlMerger(srcWebXml);
343: merger.mergeFilters(mergeWebXml);
344: assertTrue(srcWebXml.hasFilter("f1"));
345: Iterator filterMappings = srcWebXml.getFilterMappings("f1");
346: assertEquals("/f1mapping1", filterMappings.next());
347: assertTrue(!filterMappings.hasNext());
348: }
349:
350: /**
351: * Tests wether a single filter with multiple mappings is correctly merged
352: * into an empty descriptor.
353: *
354: * @throws Exception If an unexpected error occurs
355: */
356: public void testMergeOneFilterWithMultipleMappingsIntoEmptyDocument()
357: throws Exception {
358: String srcXml = "<web-app></web-app>";
359: Document srcDoc = builder.parse(new ByteArrayInputStream(srcXml
360: .getBytes()));
361: WebXml srcWebXml = new WebXml(srcDoc);
362: String mergeXml = "<web-app>" + " <filter>"
363: + " <filter-name>f1</filter-name>"
364: + " <filter-class>fclass1</filter-class>"
365: + " </filter>" + " <filter-mapping>"
366: + " <filter-name>f1</filter-name>"
367: + " <url-pattern>/f1mapping1</url-pattern>"
368: + " </filter-mapping>" + " <filter-mapping>"
369: + " <filter-name>f1</filter-name>"
370: + " <url-pattern>/f1mapping2</url-pattern>"
371: + " </filter-mapping>" + " <filter-mapping>"
372: + " <filter-name>f1</filter-name>"
373: + " <url-pattern>/f1mapping3</url-pattern>"
374: + " </filter-mapping>" + "</web-app>";
375: Document mergeDoc = builder.parse(new ByteArrayInputStream(
376: mergeXml.getBytes()));
377: WebXml mergeWebXml = new WebXml(mergeDoc);
378: WebXmlMerger merger = new WebXmlMerger(srcWebXml);
379: merger.mergeFilters(mergeWebXml);
380: assertTrue(srcWebXml.hasFilter("f1"));
381: Iterator filterMappings = srcWebXml.getFilterMappings("f1");
382: assertEquals("/f1mapping1", filterMappings.next());
383: assertEquals("/f1mapping2", filterMappings.next());
384: assertEquals("/f1mapping3", filterMappings.next());
385: assertTrue(!filterMappings.hasNext());
386: }
387:
388: /**
389: * Tests whether a single servlet is correctly merged into an empty
390: * descriptor.
391: *
392: * @throws Exception If an unexpected error occurs
393: */
394: public void testMergeOneServletIntoEmptyDocument() throws Exception {
395: String srcXml = "<web-app></web-app>";
396: Document srcDoc = builder.parse(new ByteArrayInputStream(srcXml
397: .getBytes()));
398: WebXml srcWebXml = new WebXml(srcDoc);
399: String mergeXml = "<web-app>" + " <servlet>"
400: + " <servlet-name>s1</servlet-name>"
401: + " <servlet-class>sclass1</servlet-class>"
402: + " </servlet>" + "</web-app>";
403: Document mergeDoc = builder.parse(new ByteArrayInputStream(
404: mergeXml.getBytes()));
405: WebXml mergeWebXml = new WebXml(mergeDoc);
406: WebXmlMerger merger = new WebXmlMerger(srcWebXml);
407: merger.mergeServlets(mergeWebXml);
408: assertTrue(srcWebXml.hasServlet("s1"));
409: }
410:
411: /**
412: * Tests whether a single servlet is correctly merged into a descriptor that
413: * already contains the definition of an other servlet.
414: *
415: * @throws Exception If an unexpected error occurs
416: */
417: public void testMergeOneServletIntoDocumentWithAnotherServlet()
418: throws Exception {
419: String srcXml = "<web-app>" + " <servlet>"
420: + " <servlet-name>s1</servlet-name>"
421: + " <servlet-class>sclass1</servlet-class>"
422: + " </servlet>" + "</web-app>";
423: Document srcDoc = builder.parse(new ByteArrayInputStream(srcXml
424: .getBytes()));
425: WebXml srcWebXml = new WebXml(srcDoc);
426: String mergeXml = "<web-app>" + " <servlet>"
427: + " <servlet-name>s2</servlet-name>"
428: + " <servlet-class>sclass2</servlet-class>"
429: + " </servlet>" + "</web-app>";
430: Document mergeDoc = builder.parse(new ByteArrayInputStream(
431: mergeXml.getBytes()));
432: WebXml mergeWebXml = new WebXml(mergeDoc);
433: WebXmlMerger merger = new WebXmlMerger(srcWebXml);
434: merger.mergeServlets(mergeWebXml);
435: assertTrue(srcWebXml.hasServlet("s1"));
436: assertTrue(srcWebXml.hasServlet("s2"));
437: }
438:
439: /**
440: * Tests whether a single servlet is correctly merged into a descriptor that
441: * already contains the definition of a servlet with the same name.
442: *
443: * @throws Exception If an unexpected error occurs
444: */
445: public void testMergeOneServletIntoDocumentWithSameServlet()
446: throws Exception {
447: String srcXml = "<web-app>" + " <servlet>"
448: + " <servlet-name>s1</servlet-name>"
449: + " <servlet-class>sclass1</servlet-class>"
450: + " </servlet>" + "</web-app>";
451: Document srcDoc = builder.parse(new ByteArrayInputStream(srcXml
452: .getBytes()));
453: WebXml srcWebXml = new WebXml(srcDoc);
454: String mergeXml = "<web-app>" + " <servlet>"
455: + " <servlet-name>s1</servlet-name>"
456: + " <servlet-class>sclass1</servlet-class>"
457: + " </servlet>" + "</web-app>";
458: Document mergeDoc = builder.parse(new ByteArrayInputStream(
459: mergeXml.getBytes()));
460: WebXml mergeWebXml = new WebXml(mergeDoc);
461: WebXmlMerger merger = new WebXmlMerger(srcWebXml);
462: merger.mergeServlets(mergeWebXml);
463: assertTrue(srcWebXml.hasServlet("s1"));
464: }
465:
466: /**
467: * Tets whether a servlet with an initialization parameter is correctly
468: * merged into a descriptor that contains the definition of a servlet with
469: * the same name.
470: *
471: * @throws Exception If an unexpected error occurs
472: */
473: public void testMergeOneServletIntoDocumentWithSameServletAndParam()
474: throws Exception {
475: String srcXml = "<web-app>" + " <servlet>"
476: + " <servlet-name>s1</servlet-name>"
477: + " <servlet-class>sclass1</servlet-class>"
478: + " </servlet>" + "</web-app>";
479: Document srcDoc = builder.parse(new ByteArrayInputStream(srcXml
480: .getBytes()));
481: WebXml srcWebXml = new WebXml(srcDoc);
482: String mergeXml = "<web-app>" + " <servlet>"
483: + " <servlet-name>s1</servlet-name>"
484: + " <servlet-class>sclass1</servlet-class>"
485: + " <init-param>"
486: + " <param-name>s1param1</param-name>"
487: + " <param-value>s1param1value</param-value>"
488: + " </init-param>" + " </servlet>" + "</web-app>";
489: Document mergeDoc = builder.parse(new ByteArrayInputStream(
490: mergeXml.getBytes()));
491: WebXml mergeWebXml = new WebXml(mergeDoc);
492: WebXmlMerger merger = new WebXmlMerger(srcWebXml);
493: merger.mergeServlets(mergeWebXml);
494: assertTrue(srcWebXml.hasServlet("s1"));
495: Iterator initParams = srcWebXml.getServletInitParamNames("s1");
496: assertEquals("s1param1", initParams.next());
497: assertTrue(!initParams.hasNext());
498: assertEquals("s1param1value", srcWebXml.getServletInitParam(
499: "s1", "s1param1"));
500: }
501:
502: /**
503: * Tests whether a single servlet is correctly merged into a descriptor with
504: * multiple servlets.
505: *
506: * @throws Exception If an unexpected error occurs
507: */
508: public void testMergeOneServletIntoDocumentWithMultipleServlets()
509: throws Exception {
510: String srcXml = "<web-app>" + " <servlet>"
511: + " <servlet-name>s1</servlet-name>"
512: + " <servlet-class>sclass1</servlet-class>"
513: + " </servlet>" + " <servlet>"
514: + " <servlet-name>s2</servlet-name>"
515: + " <servlet-class>sclass2</servlet-class>"
516: + " </servlet>" + " <servlet>"
517: + " <servlet-name>s3</servlet-name>"
518: + " <servlet-class>sclass3</servlet-class>"
519: + " </servlet>" + "</web-app>";
520: Document srcDoc = builder.parse(new ByteArrayInputStream(srcXml
521: .getBytes()));
522: WebXml srcWebXml = new WebXml(srcDoc);
523: String mergeXml = "<web-app>" + " <servlet>"
524: + " <servlet-name>s4</servlet-name>"
525: + " <servlet-class>sclass4</servlet-class>"
526: + " </servlet>" + "</web-app>";
527: Document mergeDoc = builder.parse(new ByteArrayInputStream(
528: mergeXml.getBytes()));
529: WebXml mergeWebXml = new WebXml(mergeDoc);
530: WebXmlMerger merger = new WebXmlMerger(srcWebXml);
531: merger.mergeServlets(mergeWebXml);
532: Iterator servletNames = srcWebXml.getServletNames();
533: assertEquals("s1", servletNames.next());
534: assertEquals("s2", servletNames.next());
535: assertEquals("s3", servletNames.next());
536: assertEquals("s4", servletNames.next());
537: assertTrue(!servletNames.hasNext());
538: }
539:
540: /**
541: * Verifies that servlet init parameters are added after the load-on-startup
542: * element of an already existing servlet definition.
543: *
544: * @throws Exception If an unexpected error occurs
545: */
546: public void testMergingServletWithInitParamsThatIsAlreadyDefined()
547: throws Exception {
548: String srcXml = "<web-app>".trim() + " <servlet>".trim()
549: + " <servlet-name>s1</servlet-name>".trim()
550: + " <servlet-class>sclass1</servlet-class>".trim()
551: + " <load-on-startup>1</load-on-startup>".trim()
552: + " </servlet>".trim() + "</web-app>";
553: Document srcDoc = builder.parse(new ByteArrayInputStream(srcXml
554: .getBytes()));
555: WebXml srcWebXml = new WebXml(srcDoc);
556: String mergeXml = "<web-app>"
557: + " <servlet>".trim()
558: + " <servlet-name>s1</servlet-name>".trim()
559: + " <servlet-class>sclass1</servlet-class>".trim()
560: + " <init-param>".trim()
561: + " <param-name>s1param1</param-name>".trim()
562: + " <param-value>s1param1value</param-value>"
563: .trim() + " </init-param>".trim()
564: + " </servlet>".trim() + "</web-app>";
565: Document mergeDoc = builder.parse(new ByteArrayInputStream(
566: mergeXml.getBytes()));
567: WebXml mergeWebXml = new WebXml(mergeDoc);
568: WebXmlMerger merger = new WebXmlMerger(srcWebXml);
569: merger.mergeServlets(mergeWebXml);
570: Element servletElement = srcWebXml.getServlet("s1");
571: assertEquals("load-on-startup", ((Element) servletElement
572: .getLastChild()).getTagName());
573: }
574:
575: /**
576: * Tests whether multiple servlet in the merge file are correctly inserted
577: * into an empty descriptor.
578: *
579: * @throws Exception If an unexpected error occurs
580: */
581: public void testMergeMultipleServletsIntoEmptyDocument()
582: throws Exception {
583: String srcXml = "<web-app></web-app>";
584: Document srcDoc = builder.parse(new ByteArrayInputStream(srcXml
585: .getBytes()));
586: WebXml srcWebXml = new WebXml(srcDoc);
587: String mergeXml = "<web-app>" + " <servlet>"
588: + " <servlet-name>s1</servlet-name>"
589: + " <servlet-class>sclass1</servlet-class>"
590: + " </servlet>" + " <servlet>"
591: + " <servlet-name>s2</servlet-name>"
592: + " <servlet-class>sclass2</servlet-class>"
593: + " </servlet>" + " <servlet>"
594: + " <servlet-name>s3</servlet-name>"
595: + " <servlet-class>sclass3</servlet-class>"
596: + " </servlet>" + "</web-app>";
597: Document mergeDoc = builder.parse(new ByteArrayInputStream(
598: mergeXml.getBytes()));
599: WebXml mergeWebXml = new WebXml(mergeDoc);
600: WebXmlMerger merger = new WebXmlMerger(srcWebXml);
601: merger.mergeServlets(mergeWebXml);
602: Iterator servletNames = srcWebXml.getServletNames();
603: assertEquals("s1", servletNames.next());
604: assertEquals("s2", servletNames.next());
605: assertEquals("s3", servletNames.next());
606: assertTrue(!servletNames.hasNext());
607: }
608:
609: /**
610: * Tests whether a single servlet with one mapping is correctly inserted
611: * into an empty descriptor.
612: *
613: * @throws Exception If an unexpected error occurs
614: */
615: public void testMergeOneServletWithOneMappingIntoEmptyDocument()
616: throws Exception {
617: String srcXml = "<web-app></web-app>";
618: Document srcDoc = builder.parse(new ByteArrayInputStream(srcXml
619: .getBytes()));
620: WebXml srcWebXml = new WebXml(srcDoc);
621: String mergeXml = "<web-app>" + " <servlet>"
622: + " <servlet-name>s1</servlet-name>"
623: + " <servlet-class>sclass1</servlet-class>"
624: + " </servlet>" + " <servlet-mapping>"
625: + " <servlet-name>s1</servlet-name>"
626: + " <url-pattern>/s1mapping1</url-pattern>"
627: + " </servlet-mapping>" + "</web-app>";
628: Document mergeDoc = builder.parse(new ByteArrayInputStream(
629: mergeXml.getBytes()));
630: WebXml mergeWebXml = new WebXml(mergeDoc);
631: WebXmlMerger merger = new WebXmlMerger(srcWebXml);
632: merger.mergeServlets(mergeWebXml);
633: assertTrue(srcWebXml.hasServlet("s1"));
634: Iterator servletMappings = srcWebXml.getServletMappings("s1");
635: assertEquals("/s1mapping1", servletMappings.next());
636: assertTrue(!servletMappings.hasNext());
637: }
638:
639: /**
640: * Tests whether a single servlet with multiple mappings is correctly
641: * inserted into an empty descriptor.
642: *
643: * @throws Exception If an unexpected error occurs
644: */
645: public void testMergeOneServletWithMultipleMappingsIntoEmptyDocument()
646: throws Exception {
647: String srcXml = "<web-app></web-app>";
648: Document srcDoc = builder.parse(new ByteArrayInputStream(srcXml
649: .getBytes()));
650: WebXml srcWebXml = new WebXml(srcDoc);
651: String mergeXml = "<web-app>" + " <servlet>"
652: + " <servlet-name>s1</servlet-name>"
653: + " <servlet-class>sclass1</servlet-class>"
654: + " </servlet>" + " <servlet-mapping>"
655: + " <servlet-name>s1</servlet-name>"
656: + " <url-pattern>/s1mapping1</url-pattern>"
657: + " </servlet-mapping>" + " <servlet-mapping>"
658: + " <servlet-name>s1</servlet-name>"
659: + " <url-pattern>/s1mapping2</url-pattern>"
660: + " </servlet-mapping>" + " <servlet-mapping>"
661: + " <servlet-name>s1</servlet-name>"
662: + " <url-pattern>/s1mapping3</url-pattern>"
663: + " </servlet-mapping>" + "</web-app>";
664: Document mergeDoc = builder.parse(new ByteArrayInputStream(
665: mergeXml.getBytes()));
666: WebXml mergeWebXml = new WebXml(mergeDoc);
667: WebXmlMerger merger = new WebXmlMerger(srcWebXml);
668: merger.mergeServlets(mergeWebXml);
669: assertTrue(srcWebXml.hasServlet("s1"));
670: Iterator servletMappings = srcWebXml.getServletMappings("s1");
671: assertEquals("/s1mapping1", servletMappings.next());
672: assertEquals("/s1mapping2", servletMappings.next());
673: assertEquals("/s1mapping3", servletMappings.next());
674: assertTrue(!servletMappings.hasNext());
675: }
676:
677: /**
678: * Tests whether a single security role is correctly inserted into an empty
679: * descriptor.
680: *
681: * @throws Exception If an unexpected error occurs
682: */
683: public void testMergeSecurityRoleIntoEmptyDocument()
684: throws Exception {
685: String srcXml = "<web-app></web-app>";
686: Document srcDoc = builder.parse(new ByteArrayInputStream(srcXml
687: .getBytes()));
688: WebXml srcWebXml = new WebXml(srcDoc);
689: String mergeXml = "<web-app>" + " <security-role>"
690: + " <role-name>role1</role-name>"
691: + " </security-role>" + "</web-app>";
692: Document mergeDoc = builder.parse(new ByteArrayInputStream(
693: mergeXml.getBytes()));
694: WebXml mergeWebXml = new WebXml(mergeDoc);
695: WebXmlMerger merger = new WebXmlMerger(srcWebXml);
696: merger.mergeSecurityRoles(mergeWebXml);
697: Iterator securityRoleNames = srcWebXml.getSecurityRoleNames();
698: assertTrue(securityRoleNames.hasNext());
699: assertEquals("role1", securityRoleNames.next());
700: assertTrue(!securityRoleNames.hasNext());
701: }
702:
703: /**
704: * Tests whether a single security role is ignored when the source
705: * descriptor already contains a role with the same name.
706: *
707: * @throws Exception If an unexpected error occurs
708: */
709: public void testMergeSecurityRoleIntoDocumentWithSameRole()
710: throws Exception {
711: String srcXml = "<web-app>" + " <security-role>"
712: + " <description>A role</description>"
713: + " <role-name>role1</role-name>"
714: + " </security-role>" + "</web-app>";
715: Document srcDoc = builder.parse(new ByteArrayInputStream(srcXml
716: .getBytes()));
717: WebXml srcWebXml = new WebXml(srcDoc);
718: String mergeXml = "<web-app>" + " <security-role>"
719: + " <role-name>role1</role-name>"
720: + " </security-role>" + "</web-app>";
721: Document mergeDoc = builder.parse(new ByteArrayInputStream(
722: mergeXml.getBytes()));
723: WebXml mergeWebXml = new WebXml(mergeDoc);
724: WebXmlMerger merger = new WebXmlMerger(srcWebXml);
725: merger.mergeSecurityRoles(mergeWebXml);
726: Iterator securityRoleNames = srcWebXml.getSecurityRoleNames();
727: assertTrue(securityRoleNames.hasNext());
728: assertEquals("role1", securityRoleNames.next());
729: assertTrue(!securityRoleNames.hasNext());
730: }
731:
732: /**
733: * Tests whether a single EJB reference is correctly inserted into an empty
734: * descriptor.
735: *
736: * @throws Exception If an unexpected error occurs
737: */
738: public void testMergeOneEjbRefIntoEmptyDocument() throws Exception {
739: String srcXml = "<web-app></web-app>";
740: Document srcDoc = builder.parse(new ByteArrayInputStream(srcXml
741: .getBytes()));
742: WebXml srcWebXml = new WebXml(srcDoc);
743: String mergeXml = "<web-app>" + " <ejb-ref>"
744: + " <ejb-ref-name>ejbref1</ejb-ref-name>"
745: + " <ejb-ref-type>ejbref1.type</ejb-ref-type>"
746: + " <home>ejbref1.homeInterface</home>"
747: + " <remote>ejbref1.remoteInterface</remote>"
748: + " </ejb-ref>" + "</web-app>";
749: Document mergeDoc = builder.parse(new ByteArrayInputStream(
750: mergeXml.getBytes()));
751: WebXml mergeWebXml = new WebXml(mergeDoc);
752: WebXmlMerger merger = new WebXmlMerger(srcWebXml);
753: merger.mergeEjbRefs(mergeWebXml);
754: Iterator ejbRefs = srcWebXml.getElements(WebXmlTag.EJB_REF);
755: assertTrue(ejbRefs.hasNext());
756: }
757:
758: }
|