1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
package org.apache.maven.archetype.mojos; |
21 | |
|
22 | |
import org.apache.commons.collections.CollectionUtils; |
23 | |
import org.apache.maven.archetype.common.Constants; |
24 | |
import org.apache.maven.archetype.exception.ArchetypeGenerationFailure; |
25 | |
import org.apache.maven.archetype.exception.ArchetypeNotConfigured; |
26 | |
import org.apache.maven.archetype.exception.OutputFileExists; |
27 | |
import org.apache.maven.archetype.exception.PomFileExists; |
28 | |
import org.apache.maven.archetype.exception.ProjectDirectoryExists; |
29 | |
import org.apache.maven.archetype.exception.UnknownArchetype; |
30 | |
import org.apache.maven.archetype.generator.FilesetArchetypeGenerator; |
31 | |
import org.apache.maven.plugin.AbstractMojo; |
32 | |
import org.apache.maven.plugin.MojoExecutionException; |
33 | |
import org.apache.maven.plugin.MojoFailureException; |
34 | |
import org.apache.maven.project.MavenProject; |
35 | |
import org.codehaus.plexus.util.FileUtils; |
36 | |
import org.codehaus.plexus.util.StringUtils; |
37 | |
|
38 | |
import java.io.File; |
39 | |
import java.io.FileInputStream; |
40 | |
import java.io.FileNotFoundException; |
41 | |
import java.io.IOException; |
42 | |
import java.io.StringWriter; |
43 | |
import java.util.Iterator; |
44 | |
import java.util.List; |
45 | |
import java.util.Properties; |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | 0 | public class IntegrationTestMojo |
55 | |
extends AbstractMojo |
56 | |
{ |
57 | |
|
58 | |
FilesetArchetypeGenerator filesetGenerator; |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
private MavenProject project; |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | 0 | private boolean skip = false; |
76 | |
|
77 | |
public void execute() |
78 | |
throws |
79 | |
MojoExecutionException, |
80 | |
MojoFailureException |
81 | |
{ |
82 | 0 | if ( !skip ) |
83 | |
{ |
84 | |
try |
85 | |
{ |
86 | 0 | File projectsDirectory = |
87 | |
new File( project.getBasedir(), "target/test-classes/projects" ); |
88 | |
|
89 | 0 | if ( projectsDirectory.exists() ) |
90 | |
{ |
91 | 0 | File archetypeFile = project.getArtifact().getFile(); |
92 | |
|
93 | 0 | List projectsGoalFiles = |
94 | |
FileUtils.getFiles( projectsDirectory, "*/goal.txt", "" ); |
95 | |
|
96 | 0 | Iterator goalFiles = projectsGoalFiles.iterator(); |
97 | |
|
98 | 0 | StringWriter errorWriter = new StringWriter(); |
99 | 0 | while ( goalFiles.hasNext() ) |
100 | |
{ |
101 | 0 | File goalFile = (File) goalFiles.next(); |
102 | |
|
103 | |
try |
104 | |
{ |
105 | 0 | processIntegrationTest( goalFile, archetypeFile ); |
106 | |
} |
107 | 0 | catch ( IntegrationTestFailure ex ) |
108 | |
{ |
109 | 0 | errorWriter.write( |
110 | |
"Test " + goalFile.getParentFile().getName() + " failed\n" |
111 | |
); |
112 | |
|
113 | 0 | errorWriter.write( ex.getStackTrace() + "\n" ); |
114 | 0 | errorWriter.write( ex.getMessage() + "\n" ); |
115 | 0 | errorWriter.write( "\n" ); |
116 | 0 | } |
117 | 0 | } |
118 | |
|
119 | 0 | String errors = errorWriter.toString(); |
120 | 0 | if ( !StringUtils.isEmpty( errors ) ) |
121 | |
{ |
122 | 0 | throw new MojoExecutionException( errors ); |
123 | |
} |
124 | |
} |
125 | |
} |
126 | 0 | catch ( IOException ex ) |
127 | |
{ |
128 | 0 | throw new MojoFailureException( ex, ex.getMessage(), ex.getMessage() ); |
129 | 0 | } |
130 | |
} |
131 | 0 | } |
132 | |
|
133 | |
private void assertTest( File reference, |
134 | |
File basedir ) |
135 | |
throws |
136 | |
IntegrationTestFailure, |
137 | |
IOException |
138 | |
{ |
139 | 0 | List referenceFiles = FileUtils.getFileNames( reference, "**", null, false ); |
140 | 0 | List projectFiles = FileUtils.getFileNames( basedir, "**", null, false ); |
141 | |
|
142 | 0 | boolean fileNamesEquals = |
143 | |
CollectionUtils.isEqualCollection( referenceFiles, projectFiles ); |
144 | |
|
145 | |
{ |
146 | 0 | Iterator refs = referenceFiles.iterator(); |
147 | 0 | while ( refs.hasNext() ) |
148 | |
{ |
149 | 0 | String ref = (String) refs.next(); |
150 | |
|
151 | 0 | if ( projectFiles.contains( ref ) ) |
152 | |
{ |
153 | 0 | projectFiles.remove( ref ); |
154 | 0 | getLog().debug( "Contained " + ref ); |
155 | |
} |
156 | |
else |
157 | |
{ |
158 | 0 | getLog().debug( "Not contained " + ref ); |
159 | |
} |
160 | 0 | } |
161 | 0 | getLog().debug( "Remains " + projectFiles ); |
162 | |
} |
163 | |
|
164 | 0 | if ( !fileNamesEquals ) |
165 | |
{ |
166 | 0 | throw new IntegrationTestFailure( "Reference and generated project differs" ); |
167 | |
} |
168 | |
|
169 | 0 | boolean contentEquals = true; |
170 | 0 | Iterator files = referenceFiles.iterator(); |
171 | 0 | while ( files.hasNext() ) |
172 | |
{ |
173 | 0 | String file = (String) files.next(); |
174 | |
|
175 | 0 | if ( file.endsWith( "pom.xml" ) ) |
176 | |
{ |
177 | 0 | if ( !modelEquals( new File( reference, file ), new File( basedir, file ) ) ) |
178 | |
{ |
179 | 0 | getLog().warn( "Contents of file " + file + " are not equal" ); |
180 | 0 | contentEquals = false; |
181 | |
} |
182 | |
} |
183 | |
else |
184 | |
{ |
185 | 0 | if ( !FileUtils.contentEquals( |
186 | |
new File( reference, file ), |
187 | |
new File( basedir, file ) |
188 | |
) |
189 | |
) |
190 | |
{ |
191 | 0 | getLog().warn( "Contents of file " + file + " are not equal" ); |
192 | 0 | contentEquals = false; |
193 | |
} |
194 | |
} |
195 | 0 | } |
196 | 0 | if ( !contentEquals ) |
197 | |
{ |
198 | 0 | throw new IntegrationTestFailure( "Some content are not equals" ); |
199 | |
} |
200 | 0 | } |
201 | |
|
202 | |
private Properties loadProperties( final File propertiesFile ) |
203 | |
throws |
204 | |
IOException, |
205 | |
FileNotFoundException |
206 | |
{ |
207 | 0 | Properties properties = new Properties(); |
208 | |
|
209 | 0 | properties.load( new FileInputStream( propertiesFile ) ); |
210 | |
|
211 | 0 | return properties; |
212 | |
} |
213 | |
|
214 | |
private boolean modelEquals( File referencePom, |
215 | |
File generatedPom ) |
216 | |
throws |
217 | |
IOException |
218 | |
{ |
219 | 0 | return FileUtils.contentEquals( referencePom, generatedPom ); |
220 | |
} |
221 | |
|
222 | |
private void processIntegrationTest( File goalFile, |
223 | |
File archetypeFile ) |
224 | |
throws |
225 | |
IntegrationTestFailure |
226 | |
{ |
227 | |
try |
228 | |
{ |
229 | 0 | Properties testProperties = getTestProperties( goalFile ); |
230 | |
|
231 | 0 | Properties properties = getProperties( goalFile ); |
232 | |
|
233 | 0 | String basedir = goalFile.getParentFile().getPath() + "/project"; |
234 | |
|
235 | 0 | FileUtils.mkdir( basedir ); |
236 | |
|
237 | 0 | filesetGenerator.generateArchetype( null, archetypeFile, basedir ); |
238 | |
|
239 | 0 | File reference = new File( goalFile.getParentFile(), "reference" ); |
240 | |
|
241 | 0 | assertTest( |
242 | |
reference, |
243 | |
new File( basedir, properties.getProperty( Constants.ARTIFACT_ID ) ) |
244 | |
); |
245 | |
} |
246 | 0 | catch ( ArchetypeNotConfigured ex ) |
247 | |
{ |
248 | 0 | throw new IntegrationTestFailure( ex ); |
249 | |
} |
250 | 0 | catch ( UnknownArchetype ex ) |
251 | |
{ |
252 | 0 | throw new IntegrationTestFailure( ex ); |
253 | |
} |
254 | 0 | catch ( PomFileExists ex ) |
255 | |
{ |
256 | 0 | throw new IntegrationTestFailure( ex ); |
257 | |
} |
258 | 0 | catch ( ProjectDirectoryExists ex ) |
259 | |
{ |
260 | 0 | throw new IntegrationTestFailure( ex ); |
261 | |
} |
262 | 0 | catch ( ArchetypeGenerationFailure ex ) |
263 | |
{ |
264 | 0 | throw new IntegrationTestFailure( ex ); |
265 | |
} |
266 | 0 | catch ( IOException ex ) |
267 | |
{ |
268 | 0 | throw new IntegrationTestFailure( ex ); |
269 | |
} |
270 | 0 | catch ( OutputFileExists ex ) |
271 | |
{ |
272 | 0 | throw new IntegrationTestFailure( ex ); |
273 | 0 | } |
274 | 0 | } |
275 | |
|
276 | |
private Properties getProperties( File goalFile ) |
277 | |
throws |
278 | |
IOException |
279 | |
{ |
280 | 0 | File propertiesFile = new File( goalFile.getParentFile(), "archetype.properties" ); |
281 | |
|
282 | 0 | return loadProperties( propertiesFile ); |
283 | |
} |
284 | |
|
285 | |
private Properties getTestProperties( File goalFile ) |
286 | |
throws |
287 | |
IOException |
288 | |
{ |
289 | 0 | return loadProperties( goalFile ); |
290 | |
} |
291 | |
|
292 | 0 | class IntegrationTestFailure |
293 | |
extends Exception |
294 | |
{ |
295 | |
IntegrationTestFailure() |
296 | 0 | { |
297 | 0 | super(); |
298 | 0 | } |
299 | |
|
300 | |
IntegrationTestFailure( String message ) |
301 | 0 | { |
302 | 0 | super( message ); |
303 | 0 | } |
304 | |
|
305 | |
IntegrationTestFailure( Throwable cause ) |
306 | 0 | { |
307 | 0 | super( cause ); |
308 | 0 | } |
309 | |
|
310 | |
IntegrationTestFailure( String message, |
311 | |
Throwable cause ) |
312 | 0 | { |
313 | 0 | super( message, cause ); |
314 | 0 | } |
315 | |
} |
316 | |
} |