1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.war;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.Date;
24 import java.util.List;
25
26 import org.apache.maven.execution.DefaultMavenExecutionRequest;
27 import org.apache.maven.execution.MavenExecutionRequest;
28 import org.apache.maven.execution.MavenSession;
29 import org.apache.maven.plugin.testing.AbstractMojoTestCase;
30 import org.apache.maven.plugin.testing.stubs.ArtifactStub;
31 import org.apache.maven.plugins.war.stub.MavenProjectBasicStub;
32 import org.apache.maven.plugins.war.stub.WarOverlayStub;
33 import org.apache.maven.shared.filtering.MavenFileFilter;
34 import org.codehaus.plexus.PlexusContainer;
35 import org.codehaus.plexus.archiver.ArchiverException;
36 import org.codehaus.plexus.archiver.jar.JarArchiver;
37 import org.codehaus.plexus.util.FileUtils;
38 import org.eclipse.aether.RepositorySystemSession;
39
40 public abstract class AbstractWarMojoTest extends AbstractMojoTestCase {
41
42 protected static final File OVERLAYS_TEMP_DIR = new File(getBasedir(), "target/test-overlays/");
43
44 protected static final File OVERLAYS_ROOT_DIR = new File(getBasedir(), "target/test-classes/overlays/");
45
46 protected static final String MANIFEST_PATH = "META-INF" + File.separator + "MANIFEST.MF";
47
48 protected abstract File getTestDirectory() throws Exception;
49
50
51
52
53
54
55
56
57
58
59
60
61 protected void configureMojo(
62 AbstractWarMojo mojo,
63 List<String> filters,
64 File classesDir,
65 File webAppSource,
66 File webAppDir,
67 MavenProjectBasicStub project)
68 throws Exception {
69 setVariableValueToObject(mojo, "filters", filters);
70 setVariableValueToObject(mojo, "mavenFileFilter", lookup(MavenFileFilter.class.getName()));
71 setVariableValueToObject(mojo, "useJvmChmod", Boolean.TRUE);
72
73 MavenExecutionRequest request = new DefaultMavenExecutionRequest()
74 .setSystemProperties(System.getProperties())
75 .setStartTime(new Date());
76
77 MavenSession mavenSession =
78 new MavenSession((PlexusContainer) null, (RepositorySystemSession) null, request, null);
79 setVariableValueToObject(mojo, "session", mavenSession);
80 setVariableValueToObject(mojo, "outdatedCheckPath", "WEB-INF/lib/");
81 mojo.setClassesDirectory(classesDir);
82 mojo.setWarSourceDirectory(webAppSource);
83 mojo.setWebappDirectory(webAppDir);
84 mojo.setProject(project);
85 }
86
87
88
89
90
91
92
93
94
95 protected File createXMLConfigDir(String id, String[] xmlFiles) throws Exception {
96 File xmlConfigDir = new File(getTestDirectory(), "/" + id + "-test-data/xml-config");
97 File XMLFile;
98
99 createDir(xmlConfigDir);
100
101 if (xmlFiles != null) {
102 for (String o : xmlFiles) {
103 XMLFile = new File(xmlConfigDir, o);
104 createFile(XMLFile);
105 }
106 }
107
108 return xmlConfigDir;
109 }
110
111
112
113
114
115
116
117
118 protected File getWebAppSource(String id) throws Exception {
119 return new File(getTestDirectory(), "/" + id + "-test-data/source");
120 }
121
122
123
124
125
126
127
128
129
130 protected File createWebAppSource(String id, boolean createSamples) throws Exception {
131 File webAppSource = getWebAppSource(id);
132 if (createSamples) {
133 File simpleJSP = new File(webAppSource, "pansit.jsp");
134 File jspFile = new File(webAppSource, "org/web/app/last-exile.jsp");
135
136 createFile(simpleJSP);
137 createFile(jspFile);
138 }
139 return webAppSource;
140 }
141
142 protected File createWebAppSource(String id) throws Exception {
143 return createWebAppSource(id, true);
144 }
145
146
147
148
149
150
151
152
153
154 protected File createClassesDir(String id, boolean empty) throws Exception {
155 File classesDir = new File(getTestDirectory() + "/" + id + "-test-data/classes/");
156
157 createDir(classesDir);
158
159 if (!empty) {
160 createFile(new File(classesDir + "/sample-servlet.clazz"));
161 }
162
163 return classesDir;
164 }
165
166 protected void createDir(File dir) {
167 if (!dir.exists()) {
168 assertTrue("can not create test dir: " + dir.toString(), dir.mkdirs());
169 }
170 }
171
172 protected void createFile(File testFile, String body) throws Exception {
173 createDir(testFile.getParentFile());
174 FileUtils.fileWrite(testFile.toString(), body);
175
176 assertTrue("could not create file: " + testFile, testFile.exists());
177 }
178
179 protected void createFile(File testFile) throws Exception {
180 createFile(testFile, testFile.toString());
181 }
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227 protected File generateFullOverlayWar(String id) throws Exception {
228 final File destFile = new File(OVERLAYS_TEMP_DIR, id + ".war");
229 if (destFile.exists()) {
230 return destFile;
231 }
232
233
234 final File rootDir = new File(OVERLAYS_ROOT_DIR, id);
235 rootDir.mkdirs();
236 String[] filePaths = new String[] {
237 "jsp/d/a.jsp",
238 "jsp/d/b.jsp",
239 "jsp/d/c.jsp",
240 "jsp/a.jsp",
241 "jsp/b.jsp",
242 "jsp/c.jsp",
243 "WEB-INF/classes/a.clazz",
244 "WEB-INF/classes/b.clazz",
245 "WEB-INF/classes/c.clazz",
246 "WEB-INF/lib/a.jar",
247 "WEB-INF/lib/b.jar",
248 "WEB-INF/lib/c.jar",
249 "WEB-INF/web.xml"
250 };
251
252 for (String filePath : filePaths) {
253 createFile(new File(rootDir, filePath), id + "-" + filePath);
254 }
255
256 createArchive(rootDir, destFile);
257 return destFile;
258 }
259
260
261
262
263
264
265
266
267
268 protected ArtifactStub buildWarOverlayStub(String id) {
269
270 final File destFile = new File(OVERLAYS_TEMP_DIR, id + ".war");
271 if (!destFile.exists()) {
272 createArchive(new File(OVERLAYS_ROOT_DIR, id), destFile);
273 }
274
275 return new WarOverlayStub(getBasedir(), id, destFile);
276 }
277
278 protected File getOverlayFile(String id, String filePath) {
279 final File overlayDir = new File(OVERLAYS_ROOT_DIR, id);
280 final File file = new File(overlayDir, filePath);
281
282
283 assertTrue(
284 "Overlay file " + filePath + " does not exist for overlay " + id + " at " + file.getAbsolutePath(),
285 file.exists());
286 return file;
287 }
288
289 protected void createArchive(final File directory, final File destinationFile) {
290 try {
291 JarArchiver archiver = new JarArchiver();
292
293 archiver.setDestFile(destinationFile);
294 archiver.addDirectory(directory);
295
296 archiver.createArchive();
297
298 } catch (ArchiverException e) {
299 e.printStackTrace();
300 fail("Failed to create overlay archive " + e.getMessage());
301 } catch (IOException e) {
302 e.printStackTrace();
303 fail("Unexpected exception " + e.getMessage());
304 }
305 }
306 }