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