1 package org.apache.maven.j2ee;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 import java.io.File;
58 import java.io.IOException;
59 import java.util.ArrayList;
60 import java.util.Collections;
61 import java.util.Iterator;
62 import java.util.List;
63 import java.util.Set;
64 import java.util.jar.JarEntry;
65
66 import junit.framework.TestCase;
67
68 import org.apache.maven.TestConstants;
69
70 /**
71 * Unit test for {@link WarFile}.
72 *
73 * @author <a href="mailto:dion@multitask.com.au">dIon Gillard</a>
74 * @version $Id: WarFileTest.java 170200 2005-05-15 06:24:19Z brett $
75 */
76 public class WarFileTest extends TestCase
77 {
78 /** instance used for testing */
79 private WarFile instance;
80 /** file name for the empty war used during testing */
81 private String emptyWarFile;
82 /** file name for manifest-only war used during testing */
83 private String manifestOnlyWarFile;
84 /** file name for simple war (manifest & web.xml) used during testing */
85 private String simpleWarFile;
86 /** file name for dummy war (two dummy servlets) */
87 private String dummyWarFile;
88 /** file name for dummy jsp war (dummy war + a jsp-file entry) */
89 private String dummyJspWarFile;
90 /** file name for dummy taglib war (dummy war + 2 taglibs) */
91 private String dummyTaglibWarFile;
92 /** file name for error pages war (dummy war + 2 error pages) */
93 private String errorPagesWarFile;
94 /** file name for form login config war (dummy war + login config) */
95 private String loginPagesWarFile;
96
97 /** Creates a new instance of WarFileTest
98 * @param testName the name of the test
99 */
100 public WarFileTest(String testName)
101 {
102 super(testName);
103 }
104
105 /**
106 * Initialize per test data
107 * @throws Exception when there is an unexpected problem
108 */
109 public void setUp() throws Exception
110 {
111 String baseDir = System.getProperty("basedir");
112 assertNotNull("The system property basedir was not defined.", baseDir);
113 final String j2eeDir = baseDir + TestConstants.J2EE_DIR ;
114 emptyWarFile = j2eeDir + "empty.war";
115 manifestOnlyWarFile = j2eeDir + "manifest-only.war";
116 simpleWarFile = j2eeDir + "simple.war";
117 dummyWarFile = j2eeDir + "dummy-servlet.war";
118 dummyJspWarFile = j2eeDir + "dummy-jsp.war";
119 dummyTaglibWarFile = j2eeDir + "dummy-taglib.war";
120 errorPagesWarFile = j2eeDir + "error-pages.war";
121 loginPagesWarFile = j2eeDir + "login-pages.war";
122 }
123
124 /**
125 * test that an empty war file produces an error
126 * @throws Exception when there is an unexpected problem
127 */
128 public void testEmptyWar() throws Exception
129 {
130 try
131 {
132 instance = new WarFile(emptyWarFile);
133 fail("Empty war file (with no META-INF, or WEB-INF/web.xml) has"
134 + " succeeded");
135 }
136 catch (IOException ioe)
137 {
138
139 }
140 }
141
142 /** test that a manifest only war (no web.xml) returns null for the web.xml!
143 * @throws Exception when there is an unexpected problem
144 */
145 public void testManifestOnlyWar() throws Exception
146 {
147 instance = new WarFile(manifestOnlyWarFile);
148 assertNull("web.xml for manifest only war is not null",
149 instance.getWebXmlEntry());
150 }
151
152 /** test that a simple war (web.xml + manifest) returns an entry for web.xml
153 * test that a simple war returns zero servlets
154 * @throws Exception when there is an unexpected problem
155 */
156 public void testSimpleWar() throws Exception
157 {
158 instance = new WarFile(simpleWarFile);
159 assertNotNull("web.xml for simple war is null",
160 instance.getWebXmlEntry());
161 assertEquals("number of servlets for simple war is not zero", 0,
162 instance.getServlets().size());
163 }
164
165 /** test that the dummy war returns two servlets
166 * @throws Exception when there is an unexpected problem
167 */
168 public void testDummyWarServlets() throws Exception
169 {
170 instance = new WarFile(dummyWarFile);
171 assertEquals("number of servlets for dummy war is not two", 2,
172 instance.getServlets().size());
173 assertTrue("servlet list doesn't contain 'dummy servlet'",
174 instance.getServlets().keySet().contains("dummy servlet"));
175 assertTrue("servlet list doesn't contain 'dummy number 2'",
176 instance.getServlets().keySet().contains("dummy number 2"));
177 assertTrue("servlet list doesn't contain HttpServlet",
178 instance.getServlets().containsValue(
179 "javax.servlet.http.HttpServlet"));
180 assertTrue("servlet list doesn't contain Servlet",
181 instance.getServlets().containsValue(
182 "java.servlet.Servlet"));
183 }
184
185 /** test the getLibEntries method
186 * @throws Exception when there is an unexpected problem
187 */
188 public void testDummyWarLib() throws Exception
189 {
190 instance = new WarFile(dummyWarFile);
191 assertEquals("number of lib entries for dummy war is not two", 2,
192 instance.getLibEntries().size());
193 JarEntry entry = null;
194 List list = new ArrayList();
195 for (Iterator entries = instance.getLibEntries().iterator();
196 entries.hasNext();)
197 {
198 entry = (JarEntry) entries.next();
199 list.add(entry.getName());
200 }
201 Collections.sort(list);
202 assertEquals("First entry is a.jar", "WEB-INF/lib/a.jar",
203 list.get(0));
204 assertEquals("Second entry is b.jar", "WEB-INF/lib/b.jar",
205 list.get(1));
206 }
207
208 /** test the file extraction
209 * @throws Exception when there is an unexpected problem
210 */
211 public void testDummyWarExtract() throws Exception
212 {
213 instance = new WarFile(dummyWarFile);
214 Set libs = instance.getLibEntries();
215 File[] files = new File[libs.size()];
216 JarEntry entry = null;
217 int i = 0;
218 for (Iterator entries = instance.getLibEntries().iterator();
219 entries.hasNext();)
220 {
221 entry = (JarEntry) entries.next();
222 files[i] = instance.extract(entry);
223 assertTrue("Extracted file " + files[i].getAbsolutePath()
224 + " doesn't exist", files[i].exists());
225 i++;
226 }
227 }
228
229 /** test that the dummy jsp war returns two servlets
230 * @throws Exception when there is an unexpected problem
231 */
232 public void testDummyJspWar() throws Exception
233 {
234 instance = new WarFile(dummyJspWarFile);
235 assertEquals("Number of jsps is wrong", 1,
236 instance.getJSPs().size());
237 assertTrue("List of jsps doesn't contain /a.jsp",
238 instance.getJSPs().values().contains("/a.jsp"));
239 assertEquals("Number of taglibs is wrong", 0,
240 instance.getTaglibs().size());
241 }
242
243 /** test that the dummy taglib war returns two taglibs
244 * @throws Exception when there is an unexpected problem
245 */
246 public void testDummyTaglibWar() throws Exception
247 {
248 instance = new WarFile(dummyTaglibWarFile);
249 assertEquals("Number of taglibs is wrong", 2,
250 instance.getTaglibs().size());
251 assertEquals("Number of error pages is wrong", 0,
252 instance.getErrorPages().size());
253 assertNull("Form Login Config isn't null",
254 instance.getFormLoginConfig());
255 assertTrue("List of taglibs doesn't contain /WEB-INF/a.tld",
256 instance.getTaglibs().values().contains("/WEB-INF/a.tld"));
257 assertTrue("List of taglibs doesn't contain /WEB-INF/b.tld",
258 instance.getTaglibs().values().contains("/WEB-INF/b.tld"));
259 }
260
261 /** test the error pages war contains 2 error pages
262 * @throws Exception when there is an unexpected problem
263 */
264 public void testErrorPagesWar() throws Exception
265 {
266 instance = new WarFile(errorPagesWarFile);
267 assertEquals("Number of error pages is wrong", 2,
268 instance.getErrorPages().size());
269 assertTrue("Error page list doesn't contain error.jsp",
270 instance.getErrorPages().values().contains("/error.jsp"));
271 assertTrue("Error page list doesn't contain error404.jsp",
272 instance.getErrorPages().values().contains("/error404.jsp"));
273 }
274
275 /** test the login pages war contains a form login config object with the
276 * login and error pages set
277 * @throws Exception when there is an unexpected problem
278 */
279 public void testLoginPagesWar() throws Exception
280 {
281 instance = new WarFile(loginPagesWarFile);
282 assertNotNull("Form login config is null",
283 instance.getFormLoginConfig());
284 assertEquals("Login page is not /login.html",
285 "/login.html", instance.getFormLoginConfig().getLoginPage());
286 assertEquals("Error page is not /error.jsp",
287 "/error.jsp", instance.getFormLoginConfig().getErrorPage());
288 }
289
290 /** test the servlet mappings comes back with the correct entries
291 * @throws Exception when there is an unexpected problem
292 */
293 public void testServletMappings() throws Exception
294 {
295 instance = new WarFile(dummyWarFile);
296 assertEquals("# of servlet mappings for dummy war file is wrong", 2,
297 instance.getServletMappings().size());
298 instance = new WarFile(loginPagesWarFile);
299 assertEquals("# of servlet mappings in login pages war is wrong", 0,
300 instance.getServletMappings().size());
301 }
302 }