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.plugin.testing.stubs.ArtifactStub;
23 import org.apache.maven.plugin.war.stub.MavenProjectArtifactsStub;
24 import org.codehaus.plexus.util.FileUtils;
25
26 import java.io.File;
27 import java.io.FileFilter;
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.Collections;
32 import java.util.LinkedList;
33 import java.util.List;
34
35
36
37
38 public abstract class AbstractWarExplodedMojoTest
39 extends AbstractWarMojoTest
40 {
41
42 protected WarExplodedMojo mojo;
43
44 public void setUp()
45 throws Exception
46 {
47 super.setUp();
48 mojo = (WarExplodedMojo) lookupMojo( "exploded", getPomFile() );
49 }
50
51
52
53
54
55
56
57 protected abstract File getPomFile();
58
59
60
61
62
63
64 protected abstract File getTestDirectory();
65
66
67
68
69
70
71
72
73
74
75
76
77
78 protected File setUpMojo( final String testId, ArtifactStub[] artifactStubs, String[] sourceFiles )
79 throws Exception
80 {
81 final MavenProjectArtifactsStub project = new MavenProjectArtifactsStub();
82 final File webAppDirectory = new File( getTestDirectory(), testId );
83
84
85 File webAppSource;
86 if ( sourceFiles == null )
87 {
88 webAppSource = createWebAppSource( testId );
89 }
90 else
91 {
92 webAppSource = createWebAppSource( testId, false );
93 for ( int i = 0; i < sourceFiles.length; i++ )
94 {
95 String sourceFile = sourceFiles[i];
96 File sample = new File( webAppSource, sourceFile );
97 createFile( sample );
98
99 }
100
101 }
102
103 final File classesDir = createClassesDir( testId, true );
104 final File workDirectory = new File( getTestDirectory(), "/war/work-" + testId );
105 createDir( workDirectory );
106
107 if ( artifactStubs != null )
108 {
109 for ( int i = 0; i < artifactStubs.length; i++ )
110 {
111 ArtifactStub artifactStub = artifactStubs[i];
112 project.addArtifact( artifactStub );
113 }
114 }
115
116 configureMojo( mojo, new LinkedList(), classesDir, webAppSource, webAppDirectory, project );
117 setVariableValueToObject( mojo, "workDirectory", workDirectory );
118
119 return webAppDirectory;
120 }
121
122
123
124
125
126
127
128
129
130
131 protected File setUpMojo( final String testId, ArtifactStub[] artifactStubs )
132 throws Exception
133 {
134 return setUpMojo( testId, artifactStubs, null );
135 }
136
137
138
139
140
141
142
143 protected void cleanDirectory( File directory )
144 throws IOException
145 {
146 if ( directory != null && directory.isDirectory() && directory.exists() )
147 {
148 FileUtils.deleteDirectory( directory );
149 }
150 }
151
152
153
154
155
156
157
158
159 protected List assertDefaultContent( File webAppDirectory )
160 {
161
162 File expectedWebSourceFile = new File( webAppDirectory, "pansit.jsp" );
163 File expectedWebSource2File = new File( webAppDirectory, "org/web/app/last-exile.jsp" );
164
165 assertTrue( "source file not found: " + expectedWebSourceFile.toString(), expectedWebSourceFile.exists() );
166 assertTrue( "source file not found: " + expectedWebSource2File.toString(), expectedWebSource2File.exists() );
167
168 final List content = new ArrayList();
169 content.add( expectedWebSourceFile );
170 content.add( expectedWebSource2File );
171
172 return content;
173 }
174
175
176
177
178
179
180
181
182
183 protected List assertWebXml( File webAppDirectory )
184 {
185 File expectedWEBXMLFile = new File( webAppDirectory, "WEB-INF/web.xml" );
186 assertTrue( "web xml not found: " + expectedWEBXMLFile.toString(), expectedWEBXMLFile.exists() );
187
188 final List content = new ArrayList();
189 content.add( expectedWEBXMLFile );
190
191 return content;
192 }
193
194
195
196
197
198
199
200
201
202
203 protected List assertCustomContent( File webAppDirectory, String[] filePaths, String customMessage )
204 {
205 final List content = new ArrayList();
206 for ( int i = 0; i < filePaths.length; i++ )
207 {
208 String filePath = filePaths[i];
209 final File expectedFile = new File( webAppDirectory, filePath );
210 if ( customMessage != null )
211 {
212 assertTrue( customMessage + " - " + expectedFile.toString(), expectedFile.exists() );
213 }
214 else
215 {
216 assertTrue( "source file not found: " + expectedFile.toString(), expectedFile.exists() );
217 }
218 content.add( expectedFile );
219 }
220 return content;
221 }
222
223
224
225
226
227
228
229
230 protected void assertWebAppContent( File webAppDirectory, List expectedFiles, FileFilter filter )
231 {
232 final List webAppContent = new ArrayList();
233 if ( filter != null )
234 {
235 buildFilesList( webAppDirectory, filter, webAppContent );
236 }
237 else
238 {
239 buildFilesList( webAppDirectory, new FileFilterImpl( webAppDirectory, null ), webAppContent );
240 }
241
242
243 Collections.sort( expectedFiles );
244 Collections.sort( webAppContent );
245 assertEquals( "Invalid webapp content, expected " + expectedFiles.size() + "file(s) " + expectedFiles +
246 " but got " + webAppContent.size() + " file(s) " + webAppContent, expectedFiles, webAppContent );
247 }
248
249
250
251
252
253
254
255
256
257
258
259
260 private void buildFilesList( final File dir, FileFilter filter, final List content )
261 {
262 final File[] files = dir.listFiles();
263
264 for ( int i = 0; i < files.length; i++ )
265 {
266 File file = files[i];
267
268
269 if ( filter.accept( file ) )
270 {
271 content.add( file );
272 }
273
274
275 if ( file.isDirectory() )
276 {
277 buildFilesList( file, filter, content );
278 }
279
280 }
281 }
282
283 class FileFilterImpl
284 implements FileFilter
285 {
286
287 private final List rejectedFilePaths;
288
289 private final int webAppDirIndex;
290
291
292 public FileFilterImpl( File webAppDirectory, String[] rejectedFilePaths )
293 {
294 if ( rejectedFilePaths != null )
295 {
296 this.rejectedFilePaths = Arrays.asList( rejectedFilePaths );
297 }
298 else
299 {
300 this.rejectedFilePaths = new ArrayList();
301 }
302 this.webAppDirIndex = webAppDirectory.getAbsolutePath().length() + 1;
303 }
304
305 public boolean accept( File file )
306 {
307 String effectiveRelativePath = buildRelativePath( file );
308 return !( rejectedFilePaths.contains( effectiveRelativePath ) || file.isDirectory() );
309 }
310
311 private String buildRelativePath( File f )
312 {
313 return f.getAbsolutePath().substring( webAppDirIndex );
314 }
315 }
316
317 }