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 junit.framework.TestCase;
23  import org.apache.maven.artifact.versioning.ArtifactVersion;
24  import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
25  import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
26  import org.apache.maven.artifact.versioning.VersionRange;
27  import org.apache.maven.it.VerificationException;
28  import org.apache.maven.it.Verifier;
29  
30  import java.util.ArrayList;
31  import java.util.List;
32  
33  /**
34   * Base class of all integration test cases. Mainly used to pickup surefire version
35   * from system property
36   *
37   * @author Dan T. Tran
38   */
39  public abstract class AbstractSurefireIntegrationTestClass
40      extends TestCase
41  {
42      private String surefireVersion = System.getProperty( "surefire.version" );
43  
44      private String testNgVersion = System.getProperty( "testng.version" );
45  
46      protected List<String> getInitialGoals()
47      {
48          return getInitialGoals( testNgVersion );
49      }
50  
51      protected List<String> getInitialGoals( String testNgVersion )
52      {
53          List<String> goals = new ArrayList<String>();
54          goals.add( "-Dsurefire.version=" + surefireVersion );
55  
56          if ( testNgVersion != null )
57          {
58              goals.add( "-DtestNgVersion=" + testNgVersion );
59  
60              ArtifactVersion v = new DefaultArtifactVersion( testNgVersion );
61              try
62              {
63                  if ( VersionRange.createFromVersionSpec( "(,5.12.1)" ).containsVersion( v ) )
64                  {
65                      goals.add( "-DtestNgClassifier=jdk15" );
66                  }
67              }
68              catch ( InvalidVersionSpecificationException e )
69              {
70                  throw new RuntimeException( e.getMessage(), e );
71              }
72          }
73  
74          return goals;
75      }
76  
77      protected void executeGoal( Verifier verifier, String goal )
78          throws VerificationException
79      {
80          List<String> goals = getInitialGoals();
81          goals.add( goal );
82          executeGoals( verifier, goals );
83      }
84  
85      @SuppressWarnings( { "unchecked" } )
86      protected void executeGoals( Verifier verifier, List<String> goals )
87          throws VerificationException
88      {
89          if ( !verifier.getCliOptions().contains( "-s" ) )
90          {
91              String settingsPath = System.getProperty( "maven.settings.file" );
92              if ( settingsPath.indexOf( ' ' ) >= 0 )
93              {
94                  settingsPath = '"' + settingsPath + '"';
95              }
96              verifier.getCliOptions().add( "-s" );
97              verifier.getCliOptions().add( settingsPath );
98          }
99  
100         verifier.executeGoals( goals );
101     }
102 
103     protected String getSurefireVersion()
104     {
105         return surefireVersion;
106     }
107 }