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