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 MavenSession mavenSession = 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 Iterator iterator = Arrays.asList( xmlFiles ).iterator();
97 while ( iterator.hasNext() )
98 {
99 XMLFile = new File( xmlConfigDir, (String) iterator.next() );
100 createFile( XMLFile );
101 }
102 }
103
104 return xmlConfigDir;
105 }
106
107
108
109
110
111
112
113
114 protected File getWebAppSource( String id )
115 throws Exception
116 {
117 return new File( getTestDirectory(), "/" + id + "-test-data/source" );
118 }
119
120
121
122
123
124
125
126
127 protected File createWebAppSource( String id, boolean createSamples )
128 throws Exception
129 {
130 File webAppSource = getWebAppSource( id );
131 if ( createSamples )
132 {
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 )
143 throws Exception
144 {
145 return createWebAppSource( id, true );
146 }
147
148
149
150
151
152
153
154
155
156
157 protected File createClassesDir( String id, boolean empty )
158 throws Exception
159 {
160 File classesDir = new File( getTestDirectory() + "/" + id + "-test-data/classes/" );
161
162 createDir( classesDir );
163
164 if ( !empty )
165 {
166 createFile( new File( classesDir + "/sample-servlet.class" ) );
167 }
168
169 return classesDir;
170 }
171
172 protected void createDir( File dir )
173 {
174 if ( !dir.exists() )
175 {
176 assertTrue( "can not create test dir: " + dir.toString(), dir.mkdirs() );
177 }
178 }
179
180 protected void createFile( File testFile, String body )
181 throws Exception
182 {
183 createDir( testFile.getParentFile() );
184 FileUtils.fileWrite( testFile.toString(), body );
185
186 assertTrue( "could not create file: " + testFile, testFile.exists() );
187 }
188
189 protected void createFile( File testFile )
190 throws Exception
191 {
192 createFile( testFile, testFile.toString() );
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
240
241 protected File generateFullOverlayWar( String id )
242 throws Exception
243 {
244 final File destFile = new File( OVERLAYS_TEMP_DIR, id + ".war" );
245 if ( destFile.exists() )
246 {
247 return destFile;
248 }
249
250
251 final File rootDir = new File( OVERLAYS_ROOT_DIR, id );
252 rootDir.mkdirs();
253 String[] filePaths = new String[]{"jsp/d/a.jsp", "jsp/d/b.jsp", "jsp/d/c.jsp", "jsp/a.jsp", "jsp/b.jsp",
254 "jsp/c.jsp", "WEB-INF/classes/a.class", "WEB-INF/classes/b.class", "WEB-INF/classes/c.class",
255 "WEB-INF/lib/a.jar", "WEB-INF/lib/b.jar", "WEB-INF/lib/c.jar", "WEB-INF/web.xml"};
256
257 for ( int i = 0; i < filePaths.length; i++ )
258 {
259 createFile( new File( rootDir, filePaths[i] ), id + "-" + filePaths[i] );
260 }
261
262 createArchive( rootDir, destFile );
263 return destFile;
264 }
265
266
267
268
269
270
271
272
273
274
275 protected ArtifactStub buildWarOverlayStub( String id )
276 {
277
278 final File destFile = new File( OVERLAYS_TEMP_DIR, id + ".war" );
279 if ( !destFile.exists() )
280 {
281 createArchive( new File( OVERLAYS_ROOT_DIR, id ), destFile );
282 }
283
284 return new WarOverlayStub( getBasedir(), id, destFile );
285 }
286
287 protected File getOverlayFile( String id, String filePath )
288 {
289 final File overlayDir = new File( OVERLAYS_ROOT_DIR, id );
290 final File file = new File( overlayDir, filePath );
291
292
293 assertTrue( "Overlay file " + filePath + " does not exist for overlay " + id + " at " + file.getAbsolutePath(),
294 file.exists() );
295 return file;
296
297 }
298
299 protected void createArchive( final File directory, final File destinationFile )
300 {
301 try
302 {
303
304
305 Archiver archiver = new JarArchiver();
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
329 }