View Javadoc

1   package org.apache.maven.surefire.its;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.util.ArrayList;
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  import org.apache.maven.artifact.versioning.ArtifactVersion;
29  import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
30  import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
31  import org.apache.maven.artifact.versioning.VersionRange;
32  import org.apache.maven.it.VerificationException;
33  import org.apache.maven.it.Verifier;
34  import org.apache.maven.it.util.FileUtils;
35  import org.apache.maven.it.util.ResourceExtractor;
36  import org.apache.maven.surefire.its.misc.HelperAssertions;
37  
38  import junit.framework.Assert;
39  import junit.framework.TestCase;
40  
41  /**
42   * Contains commonly used featurtes for most tests, encapsulating
43   * common use cases.
44   * <p/>
45   * Also includes thread-safe access to the extracted resource
46   * files, which AbstractSurefireIntegrationTestClass does not.
47   * Thread safe only for running in "classes" mode.
48   *
49   * @author Kristian Rosenvold
50   */
51  public abstract class SurefireVerifierTestClass
52      extends TestCase
53  {
54      private final File testDir;
55  
56      private final List<String> cliOptions = new ArrayList<String>();
57  
58      private final List<String> goals;
59  
60      private final Verifier verifier;
61  
62      private final Map<String, String> envvars = new HashMap<String, String>(  );
63  
64      private final String testNgVersion = System.getProperty( "testng.version" );
65  
66      private final String surefireVersion = System.getProperty( "surefire.version" );
67  
68      protected SurefireVerifierTestClass( String testProject )
69      {
70          try
71          {
72              testDir = simpleExtractResources( getClass(), testProject );
73              this.goals = getInitialGoals();
74              this.verifier = new Verifier( testDir.getAbsolutePath() );
75          }
76          catch ( VerificationException e )
77          {
78              throw new RuntimeException( e );
79          }
80          catch ( IOException e )
81          {
82              throw new RuntimeException( e );
83          }
84      }
85  
86      private File simpleExtractResources( Class<?> cl, String resourcePath )
87          throws IOException
88      {
89          String tempDirPath = System.getProperty( "maven.test.tmpdir", System.getProperty( "java.io.tmpdir" ) );
90          File tempDir = new File( tempDirPath, this.getClass().getSimpleName() );
91          File testDir = new File( tempDir, resourcePath );
92          FileUtils.deleteDirectory( testDir );
93  
94          return ResourceExtractor.extractResourcePath( cl, resourcePath, tempDir, true );
95      }
96  
97      protected void reset()
98      {
99          goals.clear();
100         goals.addAll( getInitialGoals() );
101         cliOptions.clear();
102     }
103 
104 
105     protected void addEnvVar(String key, String value){
106         envvars.put(  key, value );
107     }
108 
109     private List<String> getInitialGoals()
110     {
111         List<String> goals1 = new ArrayList<String>();
112         goals1.add( "-Dsurefire.version=" + surefireVersion );
113 
114         if ( testNgVersion != null )
115         {
116             goals1.add( "-DtestNgVersion=" + testNgVersion );
117 
118             ArtifactVersion v = new DefaultArtifactVersion( testNgVersion );
119             try
120             {
121                 if ( VersionRange.createFromVersionSpec( "(,5.12.1)" ).containsVersion( v ) )
122                 {
123                     goals1.add( "-DtestNgClassifier=jdk15" );
124                 }
125             }
126             catch ( InvalidVersionSpecificationException e )
127             {
128                 throw new RuntimeException( e.getMessage(), e );
129             }
130         }
131 
132         return goals1;
133     }
134 
135     protected List<String> getInitialGoals( String testNgVersion )
136     {
137         List<String> goals = new ArrayList<String>();
138         goals.add( "-Dsurefire.version=" + surefireVersion );
139 
140         if ( testNgVersion != null )
141         {
142             goals.add( "-DtestNgVersion=" + testNgVersion );
143 
144             ArtifactVersion v = new DefaultArtifactVersion( testNgVersion );
145             try
146             {
147                 if ( VersionRange.createFromVersionSpec( "(,5.12.1)" ).containsVersion( v ) )
148                 {
149                     goals.add( "-DtestNgClassifier=jdk15" );
150                 }
151             }
152             catch ( InvalidVersionSpecificationException e )
153             {
154                 throw new RuntimeException( e.getMessage(), e );
155             }
156         }
157 
158         return goals;
159     }
160 
161     /**
162      * Returns a file, referenced from the extracted root (where pom.xml is located)
163      *
164      * @param path The subdirectory under basedir
165      * @return A file
166      */
167     protected File getSubFile( String path )
168     {
169         return new File( testDir, path );
170     }
171 
172     protected void assertPresent( File file )
173     {
174         verifier.assertFilePresent( file.getAbsolutePath() );
175     }
176 
177     protected void assertNotPresent( File file )
178     {
179         verifier.assertFileNotPresent( file.getAbsolutePath() );
180     }
181 
182     protected void showErrorStackTraces()
183     {
184         cliOptions.add( "-e" );
185     }
186 
187     protected void debugLogging()
188     {
189         cliOptions.add( "-X" );
190     }
191 
192     protected void failNever()
193     {
194         cliOptions.add( "-fn" );
195     }
196 
197     protected SurefireVerifierTestClass addGoal( String goal )
198     {
199         goals.add( goal );
200         return this;
201     }
202 
203     protected Verifier executeTest()
204         throws VerificationException
205     {
206         return execute( "test" );
207     }
208 
209     protected Verifier executeVerify()
210         throws VerificationException
211     {
212         return execute( "verify" );
213     }
214 
215     protected Verifier execute( String goal )
216         throws VerificationException
217     {
218         addGoal( goal );
219         verifier.setCliOptions( cliOptions );
220         try
221         {
222             verifier.executeGoals( goals, envvars );
223             return verifier;
224         }
225         finally
226         {
227             verifier.resetStreams();
228         }
229     }
230 
231     protected File getSurefireReportsFile( String fileName )
232     {
233         File targetDir = getSubFile( "target/surefire-reports" );
234         return new File( targetDir, fileName );
235     }
236 
237     protected File getSiteFile( String fileName )
238     {
239         File targetDir = getSubFile( "target/site" );
240         return new File( targetDir, fileName );
241     }
242 
243     protected File getTargetFile( String fileName )
244     {
245         File targetDir = getSubFile( "target" );
246         return new File( targetDir, fileName );
247     }
248 
249     protected File getSiteFile( String moduleName, String fileName )
250     {
251         File targetDir = getSubFile( moduleName + "/target/site" );
252         return new File( targetDir, fileName );
253     }
254 
255     protected void printSummary( boolean printsummary )
256     {
257         addGoal( "-DprintSummary=" + printsummary );
258     }
259 
260     protected void redirectToFileReally(boolean redirect){
261         addGoal( "-Dmaven.test.redirectTestOutputToFile=" + redirect);
262     }
263 
264     protected void redirectToFile( boolean redirect )
265     {
266         redirectToFileReally(redirect);
267         //addGoal( "-Dredirect.to.file=" + redirect );
268     }
269 
270     protected void forkOnce()
271     {
272         forkMode( "once" );
273     }
274 
275     protected void forkNever()
276     {
277         forkMode( "never" );
278     }
279 
280     protected void forkAlways()
281     {
282         forkMode( "always" );
283     }
284 
285     protected void forkMode( String forkMode )
286     {
287         addGoal( "-DforkMode=" + forkMode );
288     }
289 
290     protected void failIfNoTests( boolean fail)
291     {
292         addGoal( "-DfailIfNoTests=" + fail );
293     }
294 
295     protected void activateProfile( String profile )
296     {
297         addGoal( "-P" + profile );
298     }
299 
300     public void assertTestSuiteResults( int total, int errors, int failures, int skipped )
301     {
302         HelperAssertions.assertTestSuiteResults( total, errors, failures, skipped, testDir );
303     }
304 
305     public void verifyTextInLog( String text )
306         throws VerificationException
307     {
308         verifier.verifyTextInLog( text );
309     }
310 
311     protected void verifyErrorFreeLog()
312         throws VerificationException
313     {
314         verifier.verifyErrorFreeLog();
315     }
316 
317     protected Verifier getVerifier()
318     {
319         return verifier;
320     }
321 
322     public File getTestDir()
323     {
324         return testDir;
325     }
326 
327     protected boolean assertContainsText( File file, String text )
328         throws VerificationException
329     {
330         final List<String> list = getVerifier().loadFile( file, false );
331         for ( String line : list )
332         {
333 
334             if ( line.contains( text ) )
335             {
336                 return true;
337             }
338         }
339         Assert.fail( "Did not find expected message in log" );
340         return false; // doh
341     }
342 
343     protected boolean isMaven3X()
344     {
345         return matchesVersionRange( "[3.0,)" );
346     }
347 
348     private DefaultArtifactVersion getMavenVersion()
349     {
350         try
351         {
352             String v = verifier.getMavenVersion();
353             // NOTE: If the version looks like "${...}" it has been configured from an undefined expression
354             if ( v != null && v.length() > 0 && !v.startsWith( "${" ) )
355             {
356                 return new DefaultArtifactVersion( v );
357             }
358         }
359         catch ( VerificationException e )
360         {
361             throw new RuntimeException( e );
362         }
363 
364         return null;
365     }
366 
367     /**
368      * This allows fine-grained control over execution of individual test methods
369      * by allowing tests to adjust to the current maven version, or else simply avoid
370      * executing altogether if the wrong version is present.
371      */
372     private boolean matchesVersionRange( String versionRangeStr )
373     {
374         VersionRange versionRange;
375         try
376         {
377             versionRange = VersionRange.createFromVersionSpec( versionRangeStr );
378         }
379         catch ( InvalidVersionSpecificationException e )
380         {
381             throw (RuntimeException) new IllegalArgumentException(
382                 "Invalid version range: " + versionRangeStr ).initCause( e );
383         }
384 
385         ArtifactVersion version = getMavenVersion();
386         if ( version != null )
387         {
388             return versionRange.containsVersion( version );
389         }
390         else
391         {
392             throw new IllegalStateException( "Cannot determine maven version" );
393         }
394     }
395 
396     protected String getSurefireVersion()
397     {
398         return surefireVersion;
399     }
400 
401     protected String getSurefireReportGoal()
402     {
403         return "org.apache.maven.plugins:maven-surefire-report-plugin:" + getSurefireVersion() +  ":report";
404     }
405 
406     protected String getSurefireReportOnlyGoal()
407     {
408         return "org.apache.maven.plugins:maven-surefire-report-plugin:" + getSurefireVersion() + ":report-only";
409     }
410 
411     protected String getFailsafeReportOnlyGoal()
412     {
413         return "org.apache.maven.plugins:maven-surefire-report-plugin:" + getSurefireVersion()
414             + ":failsafe-report-only";
415     }
416 
417     protected void parallel( String parallel )
418     {
419         addD( "parallel", parallel );
420     }
421 
422     protected void addD( String variable, String value )
423     {
424         addGoal( "-D" + variable + "=" + value);
425     }
426 
427 
428 }