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