001: /*
002: * ========================================================================
003: *
004: * Copyright 2003 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.application;
021:
022: import java.io.ByteArrayInputStream;
023: import java.io.StringReader;
024: import java.util.Iterator;
025:
026: import javax.xml.parsers.DocumentBuilder;
027: import javax.xml.parsers.DocumentBuilderFactory;
028: import javax.xml.parsers.ParserConfigurationException;
029:
030: import junit.framework.TestCase;
031:
032: import org.w3c.dom.Document;
033: import org.xml.sax.EntityResolver;
034: import org.xml.sax.InputSource;
035: import org.xml.sax.SAXException;
036:
037: /**
038: * Unit tests for {@link ApplicationXml}.
039: *
040: * @version $Id: TestApplicationXml.java 239003 2004-05-31 20:05:27Z vmassol $
041: */
042: public final class TestApplicationXml extends TestCase {
043: /**
044: * The document builder factory.
045: */
046: private DocumentBuilderFactory factory;
047:
048: /**
049: * The JAXP document builder.
050: */
051: private DocumentBuilder builder;
052:
053: /**
054: * @see TestCase#setUp
055: */
056: public void setUp() throws ParserConfigurationException {
057: factory = DocumentBuilderFactory.newInstance();
058: factory.setValidating(false);
059: factory.setNamespaceAware(false);
060:
061: builder = factory.newDocumentBuilder();
062: builder.setEntityResolver(new EntityResolver() {
063: public InputSource resolveEntity(String thePublicId,
064: String theSystemId) throws SAXException {
065: return new InputSource(new StringReader(""));
066: }
067: });
068: }
069:
070: /**
071: * Tests whether the construction of a ApplicationXml object with a
072: * <code>null</code> parameter for the DOM document throws a
073: * <code>NullPointerException</code>.
074: *
075: * @throws Exception If an unexpected error occurs
076: */
077: public void testConstructionWithNullDocument() throws Exception {
078: try {
079: new DefaultApplicationXml(null);
080: fail("Expected NullPointerException");
081: } catch (NullPointerException npe) {
082: // expected
083: }
084:
085: }
086:
087: /**
088: * Verifies that the method <code>getWebModuleUris()</code> returns an empty
089: * iterator for a descriptor with no web module definitions.
090: *
091: * @throws Exception If an unexpected error occurs
092: */
093: public void testGetWebModuleUrisWithEmptyDocument()
094: throws Exception {
095: String xml = "<application>" + " <module>"
096: + " <java>javaclient.jar</java>" + " </module>"
097: + "</application>";
098: Document doc = builder.parse(new ByteArrayInputStream(xml
099: .getBytes()));
100: ApplicationXml applicationXml = new DefaultApplicationXml(doc);
101: Iterator webUris = applicationXml.getWebModuleUris();
102: assertTrue("No web modules defined", !webUris.hasNext());
103: }
104:
105: /**
106: * Verifies that the method <code>getWebModuleUris()</code> returns an
107: * iterator with the correct web-uri for a descriptor with a single web
108: * module definition.
109: *
110: * @throws Exception If an unexpected error occurs
111: */
112: public void testGetWebModuleUrisWithSingleWebModule()
113: throws Exception {
114: String xml = "<application>" + " <module>" + " <web>"
115: + " <web-uri>webmodule.jar</web-uri>"
116: + " <context-root>/webmodule</context-root>"
117: + " </web>" + " </module>" + "</application>";
118: Document doc = builder.parse(new ByteArrayInputStream(xml
119: .getBytes()));
120: ApplicationXml applicationXml = new DefaultApplicationXml(doc);
121: Iterator webUris = applicationXml.getWebModuleUris();
122: assertEquals("webmodule.jar", webUris.next());
123: assertTrue(!webUris.hasNext());
124: }
125:
126: /**
127: * Verifies that the method <code>getWebModuleUris()</code> returns an
128: * iterator with the correct web-uris for a descriptor with multiple web
129: * module definitions.
130: *
131: * @throws Exception If an unexpected error occurs
132: */
133: public void testGetWebModuleUrisWithMultipleWebModules()
134: throws Exception {
135: String xml = "<application>" + " <module>" + " <web>"
136: + " <web-uri>webmodule1.jar</web-uri>"
137: + " <context-root>/webmodule1</context-root>"
138: + " </web>" + " </module>" + " <module>"
139: + " <web>"
140: + " <web-uri>webmodule2.jar</web-uri>"
141: + " <context-root>/webmodule2</context-root>"
142: + " </web>" + " </module>" + " <module>"
143: + " <web>"
144: + " <web-uri>webmodule3.jar</web-uri>"
145: + " <context-root>/webmodule3</context-root>"
146: + " </web>" + " </module>" + "</application>";
147: Document doc = builder.parse(new ByteArrayInputStream(xml
148: .getBytes()));
149: ApplicationXml applicationXml = new DefaultApplicationXml(doc);
150: Iterator webUris = applicationXml.getWebModuleUris();
151: assertEquals("webmodule1.jar", webUris.next());
152: assertEquals("webmodule2.jar", webUris.next());
153: assertEquals("webmodule3.jar", webUris.next());
154: assertTrue(!webUris.hasNext());
155: }
156:
157: /**
158: * Verifies that the method <code>getWebModuleContextRoot()</code> throws an
159: * <code>IllegalARgumentException</code> when the specified web module is
160: * not defined.
161: *
162: * @throws Exception If an unexpected error occurs
163: */
164: public void testGetWebModuleContextRootUndefined() throws Exception {
165: String xml = "<application>" + " <module>"
166: + " <java>javaclient.jar</java>" + " </module>"
167: + "</application>";
168: Document doc = builder.parse(new ByteArrayInputStream(xml
169: .getBytes()));
170: ApplicationXml applicationXml = new DefaultApplicationXml(doc);
171: try {
172: applicationXml.getWebModuleContextRoot("webmodule.jar");
173: fail("IllegalArgumentException expected");
174: } catch (IllegalArgumentException expected) {
175: // expected
176: }
177: }
178:
179: /**
180: * Verifies that the method <code>getWebModuleContextRoot()</code> returns
181: * an the correct context root for a descriptor with a single web module.
182: *
183: * @throws Exception If an unexpected error occurs
184: */
185: public void testGetWebModuleContextRootSingleWebModule()
186: throws Exception {
187: String xml = "<application>" + " <module>" + " <web>"
188: + " <web-uri>webmodule.jar</web-uri>"
189: + " <context-root>/webmodule</context-root>"
190: + " </web>" + " </module>" + "</application>";
191: Document doc = builder.parse(new ByteArrayInputStream(xml
192: .getBytes()));
193: ApplicationXml applicationXml = new DefaultApplicationXml(doc);
194: assertEquals("/webmodule", applicationXml
195: .getWebModuleContextRoot("webmodule.jar"));
196: }
197:
198: /**
199: * Verifies that the method <code>getWebModuleContextRoot()</code> returns
200: * an the correct context roots for a descriptor with multiple web modules.
201: *
202: * @throws Exception If an unexpected error occurs
203: */
204: public void testGetWebModuleContextRootMultipleWebModules()
205: throws Exception {
206: String xml = "<application>" + " <module>" + " <web>"
207: + " <web-uri>webmodule1.jar</web-uri>"
208: + " <context-root>/webmodule1</context-root>"
209: + " </web>" + " </module>" + " <module>"
210: + " <web>"
211: + " <web-uri>webmodule2.jar</web-uri>"
212: + " <context-root>/webmodule2</context-root>"
213: + " </web>" + " </module>" + " <module>"
214: + " <web>"
215: + " <web-uri>webmodule3.jar</web-uri>"
216: + " <context-root>/webmodule3</context-root>"
217: + " </web>" + " </module>" + "</application>";
218: Document doc = builder.parse(new ByteArrayInputStream(xml
219: .getBytes()));
220: ApplicationXml applicationXml = new DefaultApplicationXml(doc);
221: assertEquals("/webmodule1", applicationXml
222: .getWebModuleContextRoot("webmodule1.jar"));
223: assertEquals("/webmodule2", applicationXml
224: .getWebModuleContextRoot("webmodule2.jar"));
225: assertEquals("/webmodule3", applicationXml
226: .getWebModuleContextRoot("webmodule3.jar"));
227: }
228:
229: }
|