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