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