KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > integration > ant > deployment > application > TestApplicationXml


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

20 package org.apache.cactus.integration.ant.deployment.application;
21
22 import java.io.ByteArrayInputStream JavaDoc;
23 import java.io.StringReader JavaDoc;
24 import java.util.Iterator JavaDoc;
25
26 import javax.xml.parsers.DocumentBuilder JavaDoc;
27 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
28 import javax.xml.parsers.ParserConfigurationException JavaDoc;
29
30 import junit.framework.TestCase;
31
32 import org.w3c.dom.Document JavaDoc;
33 import org.xml.sax.EntityResolver JavaDoc;
34 import org.xml.sax.InputSource JavaDoc;
35 import org.xml.sax.SAXException JavaDoc;
36
37 /**
38  * Unit tests for {@link ApplicationXml}.
39  *
40  * @version $Id: TestApplicationXml.java,v 1.1 2004/05/31 20:05:22 vmassol Exp $
41  */

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

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

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

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

81     public void testConstructionWithNullDocument() throws Exception JavaDoc
82     {
83         try
84         {
85             new DefaultApplicationXml(null);
86             fail("Expected NullPointerException");
87         }
88         catch (NullPointerException JavaDoc npe)
89         {
90             // expected
91
}
92         
93     }
94     
95     /**
96      * Verifies that the method <code>getWebModuleUris()</code> returns an empty
97      * iterator for a descriptor with no web module definitions.
98      *
99      * @throws Exception If an unexpected error occurs
100      */

101     public void testGetWebModuleUrisWithEmptyDocument() throws Exception JavaDoc
102     {
103         String JavaDoc xml = "<application>"
104             + " <module>"
105             + " <java>javaclient.jar</java>"
106             + " </module>"
107             + "</application>";
108         Document JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
109         ApplicationXml applicationXml = new DefaultApplicationXml(doc);
110         Iterator JavaDoc webUris = applicationXml.getWebModuleUris();
111         assertTrue("No web modules defined", !webUris.hasNext());
112     }
113     
114     /**
115      * Verifies that the method <code>getWebModuleUris()</code> returns an
116      * iterator with the correct web-uri for a descriptor with a single web
117      * module definition.
118      *
119      * @throws Exception If an unexpected error occurs
120      */

121     public void testGetWebModuleUrisWithSingleWebModule() throws Exception JavaDoc
122     {
123         String JavaDoc xml = "<application>"
124             + " <module>"
125             + " <web>"
126             + " <web-uri>webmodule.jar</web-uri>"
127             + " <context-root>/webmodule</context-root>"
128             + " </web>"
129             + " </module>"
130             + "</application>";
131         Document JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
132         ApplicationXml applicationXml = new DefaultApplicationXml(doc);
133         Iterator JavaDoc webUris = applicationXml.getWebModuleUris();
134         assertEquals("webmodule.jar", webUris.next());
135         assertTrue(!webUris.hasNext());
136     }
137     
138     /**
139      * Verifies that the method <code>getWebModuleUris()</code> returns an
140      * iterator with the correct web-uris for a descriptor with multiple web
141      * module definitions.
142      *
143      * @throws Exception If an unexpected error occurs
144      */

145     public void testGetWebModuleUrisWithMultipleWebModules() throws Exception JavaDoc
146     {
147         String JavaDoc xml = "<application>"
148             + " <module>"
149             + " <web>"
150             + " <web-uri>webmodule1.jar</web-uri>"
151             + " <context-root>/webmodule1</context-root>"
152             + " </web>"
153             + " </module>"
154             + " <module>"
155             + " <web>"
156             + " <web-uri>webmodule2.jar</web-uri>"
157             + " <context-root>/webmodule2</context-root>"
158             + " </web>"
159             + " </module>"
160             + " <module>"
161             + " <web>"
162             + " <web-uri>webmodule3.jar</web-uri>"
163             + " <context-root>/webmodule3</context-root>"
164             + " </web>"
165             + " </module>"
166             + "</application>";
167         Document JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
168         ApplicationXml applicationXml = new DefaultApplicationXml(doc);
169         Iterator JavaDoc webUris = applicationXml.getWebModuleUris();
170         assertEquals("webmodule1.jar", webUris.next());
171         assertEquals("webmodule2.jar", webUris.next());
172         assertEquals("webmodule3.jar", webUris.next());
173         assertTrue(!webUris.hasNext());
174     }
175     
176     /**
177      * Verifies that the method <code>getWebModuleContextRoot()</code> throws an
178      * <code>IllegalARgumentException</code> when the specified web module is
179      * not defined.
180      *
181      * @throws Exception If an unexpected error occurs
182      */

183     public void testGetWebModuleContextRootUndefined() throws Exception JavaDoc
184     {
185         String JavaDoc xml = "<application>"
186             + " <module>"
187             + " <java>javaclient.jar</java>"
188             + " </module>"
189             + "</application>";
190         Document JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
191         ApplicationXml applicationXml = new DefaultApplicationXml(doc);
192         try
193         {
194             applicationXml.getWebModuleContextRoot("webmodule.jar");
195             fail("IllegalArgumentException expected");
196         }
197         catch (IllegalArgumentException JavaDoc expected)
198         {
199             // expected
200
}
201     }
202
203     /**
204      * Verifies that the method <code>getWebModuleContextRoot()</code> returns
205      * an the correct context root for a descriptor with a single web module.
206      *
207      * @throws Exception If an unexpected error occurs
208      */

209     public void testGetWebModuleContextRootSingleWebModule() throws Exception JavaDoc
210     {
211         String JavaDoc xml = "<application>"
212             + " <module>"
213             + " <web>"
214             + " <web-uri>webmodule.jar</web-uri>"
215             + " <context-root>/webmodule</context-root>"
216             + " </web>"
217             + " </module>"
218             + "</application>";
219         Document JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
220         ApplicationXml applicationXml = new DefaultApplicationXml(doc);
221         assertEquals("/webmodule",
222             applicationXml.getWebModuleContextRoot("webmodule.jar"));
223     }
224
225     /**
226      * Verifies that the method <code>getWebModuleContextRoot()</code> returns
227      * an the correct context roots for a descriptor with multiple web modules.
228      *
229      * @throws Exception If an unexpected error occurs
230      */

231     public void testGetWebModuleContextRootMultipleWebModules() throws Exception JavaDoc
232     {
233         String JavaDoc xml = "<application>"
234             + " <module>"
235             + " <web>"
236             + " <web-uri>webmodule1.jar</web-uri>"
237             + " <context-root>/webmodule1</context-root>"
238             + " </web>"
239             + " </module>"
240             + " <module>"
241             + " <web>"
242             + " <web-uri>webmodule2.jar</web-uri>"
243             + " <context-root>/webmodule2</context-root>"
244             + " </web>"
245             + " </module>"
246             + " <module>"
247             + " <web>"
248             + " <web-uri>webmodule3.jar</web-uri>"
249             + " <context-root>/webmodule3</context-root>"
250             + " </web>"
251             + " </module>"
252             + "</application>";
253         Document JavaDoc doc = builder.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()));
254         ApplicationXml applicationXml = new DefaultApplicationXml(doc);
255         assertEquals("/webmodule1",
256             applicationXml.getWebModuleContextRoot("webmodule1.jar"));
257         assertEquals("/webmodule2",
258             applicationXml.getWebModuleContextRoot("webmodule2.jar"));
259         assertEquals("/webmodule3",
260             applicationXml.getWebModuleContextRoot("webmodule3.jar"));
261     }
262
263 }
264
Popular Tags