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