1 package org.apache.maven.surefire.its;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import junit.framework.Assert;
23 import junit.framework.TestCase;
24 import org.apache.maven.artifact.versioning.ArtifactVersion;
25 import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
26 import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
27 import org.apache.maven.artifact.versioning.VersionRange;
28 import org.apache.maven.it.VerificationException;
29 import org.apache.maven.it.Verifier;
30 import org.apache.maven.it.util.FileUtils;
31 import org.apache.maven.it.util.ResourceExtractor;
32 import org.apache.maven.surefire.its.misc.HelperAssertions;
33
34 import java.io.File;
35 import java.io.IOException;
36 import java.util.ArrayList;
37 import java.util.List;
38
39
40
41
42
43
44
45
46
47
48
49 public abstract class SurefireVerifierTestClass
50 extends TestCase
51 {
52 private final File testDir;
53
54 private final List<String> cliOptions = new ArrayList<String>();
55
56 private final List<String> goals;
57
58 private final Verifier verifier;
59
60 private final String testNgVersion = System.getProperty( "testng.version" );
61
62 private final String surefireVersion = System.getProperty( "surefire.version" );
63
64 protected SurefireVerifierTestClass( String testProject )
65 {
66 try
67 {
68 testDir = simpleExtractResources( getClass(), testProject );
69 this.goals = getInitialGoals();
70 this.verifier = new Verifier( testDir.getAbsolutePath() );
71 }
72 catch ( VerificationException e )
73 {
74 throw new RuntimeException( e );
75 }
76 catch ( IOException e )
77 {
78 throw new RuntimeException( e );
79 }
80 }
81
82 private File simpleExtractResources( Class<?> cl, String resourcePath )
83 throws IOException
84 {
85 String tempDirPath = System.getProperty( "maven.test.tmpdir", System.getProperty( "java.io.tmpdir" ) );
86 File tempDir = new File( tempDirPath, this.getClass().getSimpleName() );
87 System.out.println( "tempDir = " + tempDir );
88
89 File testDir = new File( tempDir, resourcePath );
90 FileUtils.deleteDirectory( testDir );
91
92 return ResourceExtractor.extractResourcePath( cl, resourcePath, tempDir, true );
93 }
94
95 protected void reset()
96 {
97 goals.clear();
98 goals.addAll( getInitialGoals() );
99 cliOptions.clear();
100 }
101
102 private List<String> getInitialGoals()
103 {
104 List<String> goals1 = new ArrayList<String>();
105 goals1.add( "-Dsurefire.version=" + surefireVersion );
106
107 if ( testNgVersion != null )
108 {
109 goals1.add( "-DtestNgVersion=" + testNgVersion );
110
111 ArtifactVersion v = new DefaultArtifactVersion( testNgVersion );
112 try
113 {
114 if ( VersionRange.createFromVersionSpec( "(,5.12.1)" ).containsVersion( v ) )
115 {
116 goals1.add( "-DtestNgClassifier=jdk15" );
117 }
118 }
119 catch ( InvalidVersionSpecificationException e )
120 {
121 throw new RuntimeException( e.getMessage(), e );
122 }
123 }
124
125 return goals1;
126 }
127
128 protected List<String> getInitialGoals( String testNgVersion )
129 {
130 List<String> goals = new ArrayList<String>();
131 goals.add( "-Dsurefire.version=" + surefireVersion );
132
133 if ( testNgVersion != null )
134 {
135 goals.add( "-DtestNgVersion=" + testNgVersion );
136
137 ArtifactVersion v = new DefaultArtifactVersion( testNgVersion );
138 try
139 {
140 if ( VersionRange.createFromVersionSpec( "(,5.12.1)" ).containsVersion( v ) )
141 {
142 goals.add( "-DtestNgClassifier=jdk15" );
143 }
144 }
145 catch ( InvalidVersionSpecificationException e )
146 {
147 throw new RuntimeException( e.getMessage(), e );
148 }
149 }
150
151 return goals;
152 }
153
154
155
156
157
158
159
160 protected File getSubFile( String path )
161 {
162 return new File( testDir, path );
163 }
164
165 protected void assertPresent( File file )
166 {
167 verifier.assertFilePresent( file.getAbsolutePath() );
168 }
169
170 protected void assertNotPresent( File file )
171 {
172 verifier.assertFileNotPresent( file.getAbsolutePath() );
173 }
174
175 protected void showErrorStackTraces()
176 {
177 cliOptions.add( "-e" );
178 }
179
180 protected void debugLogging()
181 {
182 cliOptions.add( "-X" );
183 }
184
185 protected void failNever()
186 {
187 cliOptions.add( "-fn" );
188 }
189
190 protected SurefireVerifierTestClass addGoal( String goal )
191 {
192 goals.add( goal );
193 return this;
194 }
195
196 protected Verifier executeTest()
197 throws VerificationException
198 {
199 return execute( "test" );
200 }
201
202 protected Verifier execute( String goal )
203 throws VerificationException
204 {
205 addGoal( goal );
206 verifier.setCliOptions( cliOptions );
207 try
208 {
209 verifier.executeGoals( goals );
210 return verifier;
211 }
212 finally
213 {
214 verifier.resetStreams();
215 }
216 }
217
218 protected File getSurefireReportsFile( String fileName )
219 {
220 File targetDir = getSubFile( "target/surefire-reports" );
221 return new File( targetDir, fileName );
222 }
223
224 protected File getSiteFile( String fileName )
225 {
226 File targetDir = getSubFile( "target/site" );
227 return new File( targetDir, fileName );
228 }
229
230 protected File getSiteFile( String moduleName, String fileName )
231 {
232 File targetDir = getSubFile(moduleName + "/target/site" );
233 return new File( targetDir, fileName );
234 }
235
236 protected void printSummary( boolean printsummary )
237 {
238 addGoal( "-DprintSummary=" + printsummary );
239 }
240
241 protected void redirectToFile( boolean redirect )
242 {
243 addGoal( "-Dredirect.to.file=" + redirect );
244 }
245
246 protected void forkOnce()
247 {
248 forkMode( "once" );
249 }
250
251 protected void forkNever()
252 {
253 forkMode( "never" );
254 }
255
256 protected void forkAlways()
257 {
258 forkMode( "always" );
259 }
260
261 protected void forkMode( String forkMode )
262 {
263 addGoal( "-DforkMode=" + forkMode );
264 }
265
266 protected void activateProfile( String profile )
267 {
268 addGoal( "-P" + profile );
269 }
270
271 public void assertTestSuiteResults( int total, int errors, int failures, int skipped )
272 {
273 HelperAssertions.assertTestSuiteResults( total, errors, failures, skipped, testDir );
274 }
275
276 public void verifyTextInLog( String text )
277 throws VerificationException
278 {
279 verifier.verifyTextInLog( text );
280 }
281
282 protected void verifyErrorFreeLog()
283 throws VerificationException
284 {
285 verifier.verifyErrorFreeLog();
286 }
287
288 protected Verifier getVerifier()
289 {
290 return verifier;
291 }
292
293 public File getTestDir()
294 {
295 return testDir;
296 }
297
298 protected boolean assertContainsText( File file, String text )
299 throws VerificationException
300 {
301 final List<String> list = getVerifier().loadFile( file, false );
302 for ( String line : list )
303 {
304
305 if ( line.contains( text ) )
306 {
307 return true;
308 }
309 }
310 Assert.fail( "Did not find expected message in log" );
311 return false;
312 }
313
314 protected boolean isMaven3X()
315 {
316 return matchesVersionRange( "[3.0,)" );
317 }
318
319 private DefaultArtifactVersion getMavenVersion()
320 {
321 try
322 {
323 String v = verifier.getMavenVersion();
324
325 if ( v != null && v.length() > 0 && !v.startsWith( "${" ) )
326 {
327 return new DefaultArtifactVersion( v );
328 }
329 }
330 catch ( VerificationException e )
331 {
332 throw new RuntimeException( e );
333 }
334
335 return null;
336 }
337
338
339
340
341
342
343 private boolean matchesVersionRange( String versionRangeStr )
344 {
345 VersionRange versionRange;
346 try
347 {
348 versionRange = VersionRange.createFromVersionSpec( versionRangeStr );
349 }
350 catch ( InvalidVersionSpecificationException e )
351 {
352 throw (RuntimeException) new IllegalArgumentException( "Invalid version range: " + versionRangeStr ).initCause( e );
353 }
354
355 ArtifactVersion version = getMavenVersion();
356 if ( version != null )
357 {
358 return versionRange.containsVersion( version );
359 }
360 else
361 {
362 throw new IllegalStateException( "Cannot determine maven version" );
363 }
364 }
365
366 protected String getSurefireVersion()
367 {
368 return surefireVersion;
369 }
370
371 }