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