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 junit.framework.TestCase;
58
59 import org.apache.maven.TestConstants;
60
61 /**
62 * Unit test for <code>WarValidator</code>.
63 *
64 * @author <a href="mailto:dion@multitask.com.au">dIon Gillard</a>
65 * @version $Id: WarValidatorTest.java 170200 2005-05-15 06:24:19Z brett $
66 */
67 public class WarValidatorTest extends TestCase
68 {
69
70 /** the validator being tested */
71 private WarValidator validator = null;
72 /** file name for the empty war*/
73 private String emptyWarFile = null;
74 /** file name for manifest-only war */
75 private String manifestOnlyWarFile = null;
76 /** file name for simple war (manifest & web.xml)*/
77 private String simpleWarFile = null;
78 /** file name for dummy war */
79 private String dummyWarFile = null;
80 /** file name for dummy jsp war (dummy war + a jsp-file entry) */
81 private String dummyJspWarFile;
82 /** file name for dummy taglib war (dummy war + 2 taglibs) */
83 private String dummyTaglibWarFile;
84 /** file name for error pages war (dummy war + 2 error pages) */
85 private String errorPagesWarFile;
86 /** file name for form login config war (dummy war + login config) */
87 private String loginPagesWarFile;
88
89 /**
90 * Creates a new instance of WarValidatorTest
91 * @param testName the name of the test
92 */
93 public WarValidatorTest(String testName)
94 {
95 super(testName);
96 }
97
98 /**
99 * Initialize per test data
100 * @throws Exception when there is an unexpected problem
101 */
102 public void setUp() throws Exception
103 {
104 validator = new WarValidator();
105 String baseDir = System.getProperty("basedir");
106 assertNotNull("The system property basedir was not defined.", baseDir);
107 String j2eeDir = baseDir + TestConstants.J2EE_DIR;
108 emptyWarFile = j2eeDir + "empty.war";
109 manifestOnlyWarFile = j2eeDir + "manifest-only.war";
110 simpleWarFile = j2eeDir + "simple.war";
111 dummyWarFile = j2eeDir + "dummy-servlet.war";
112 dummyJspWarFile = j2eeDir + "dummy-jsp.war";
113 dummyTaglibWarFile = j2eeDir + "dummy-taglib.war";
114 errorPagesWarFile = j2eeDir + "error-pages.war";
115 loginPagesWarFile = j2eeDir + "login-pages.war";
116 }
117
118 /**
119 * test that the warFileName property is mandatory for validation
120 * @throws Exception when there is an unexpected problem
121 */
122 public void testWarFileMandatory() throws Exception
123 {
124 try
125 {
126 validator.execute();
127 fail("war validator executed without a war file");
128 }
129 catch (NullPointerException e)
130 {
131
132 }
133 }
134
135 /**
136 * test that an empty war file produces an error
137 * @throws Exception when there is an unexpected problem
138 */
139 public void testEmptyWar() throws Exception
140 {
141 try
142 {
143 validator.setFailOnError(true);
144 validator.setWarFileName(emptyWarFile);
145 validator.addFormatter(new ValidationFormatter());
146 validator.execute();
147 fail("Empty war file (with no META-INF, or WEB-INF/web.xml) has"
148 + " succeeded");
149 }
150 catch (IllegalStateException e)
151 {
152
153 }
154 }
155
156 /**
157 * test that the failOnError property when set to false doesn't throw
158 * an exception.
159 * @throws Exception when there is an unexpected problem
160 */
161 public void testFailOnError() throws Exception
162 {
163 try
164 {
165 validator.setFailOnError(false);
166 validator.setWarFileName(emptyWarFile);
167 validator.addFormatter(new ValidationFormatter());
168 validator.execute();
169 assertTrue("Error not fired for an empty war file "
170 + "(with no META-INF, or WEB-INF/web.xml)",
171 validator.getStatus().isError());
172 }
173 catch (IllegalStateException e)
174 {
175 fail("Empty war file (with no META-INF, or WEB-INF/web.xml) failed"
176 + " the build process with failOnError=false");
177 }
178 }
179
180 /**
181 * test that a manifest only war file produces a warning
182 * @throws Exception when there is an unexpected problem
183 */
184 public void testManifestOnlyWar() throws Exception
185 {
186 validator.setWarFileName(manifestOnlyWarFile);
187 validator.addFormatter(new ValidationFormatter());
188 validator.execute();
189 assertTrue("Manifest only war file (with no WEB-INF/web.xml) has"
190 + " succeeded without a warning",
191 validator.getStatus().isWarning());
192 }
193
194 /**
195 * test that a simple war (MANIFEST and web.xml) file produces no warning
196 * @throws Exception when there is an unexpected problem
197 */
198 public void testSimpleWar() throws Exception
199 {
200 validator.setWarFileName(simpleWarFile);
201 validator.addFormatter(new ValidationFormatter());
202 validator.execute();
203 assertTrue("Simple war file has generated a warning",
204 !validator.getStatus().isWarning());
205 assertTrue("Simple war file has generated an error",
206 !validator.getStatus().isError());
207 }
208
209 /**
210 * test that a dummy war file produces no warning and an error
211 * (as there is an intentionally bad class (java.servlet.Servlet) in
212 * web.xml)
213 * @throws Exception when there is an unexpected problem
214 */
215 public void testDummyWar() throws Exception
216 {
217 validator.setWarFileName(dummyWarFile);
218 validator.addFormatter(new ValidationFormatter());
219 validator.setFailOnError(false);
220 validator.execute();
221 assertTrue("Dummy war file has generated a warning",
222 !validator.getStatus().isWarning());
223 assertTrue("Dummy war file has not generated an error",
224 validator.getStatus().isError());
225 }
226
227 /**
228 * FIXME: Need to finish coding
229 *
230 * @throws Exception when there is an unexpected problem
231 */
232 public void testDummyJspWar() throws Exception
233 {
234 validator.setWarFileName(dummyJspWarFile);
235 validator.addFormatter(new ValidationFormatter());
236 validator.setFailOnError(false);
237 validator.execute();
238 assertTrue("Dummy jsp war file has not generated an error",
239 validator.getStatus().isError());
240 }
241 }