View Javadoc
1   package org.apache.maven.plugin.surefire;
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 org.apache.maven.execution.MavenExecutionRequest;
23  import org.apache.maven.execution.MavenSession;
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugin.MojoFailureException;
26  import org.apache.maven.plugin.logging.Log;
27  import org.apache.maven.surefire.cli.CommandLineOption;
28  import org.apache.maven.surefire.suite.RunResult;
29  
30  import java.lang.reflect.InvocationTargetException;
31  import java.lang.reflect.Method;
32  import java.util.ArrayList;
33  import java.util.Collection;
34  import java.util.Collections;
35  import java.util.List;
36  
37  /**
38   * Helper class for surefire plugins
39   */
40  public final class SurefireHelper
41  {
42  
43      /**
44       * Do not instantiate.
45       */
46      private SurefireHelper()
47      {
48          throw new IllegalAccessError( "Utility class" );
49      }
50  
51      public static void reportExecution( SurefireReportParameters reportParameters, RunResult result, Log log )
52          throws MojoFailureException, MojoExecutionException
53      {
54          boolean timeoutOrOtherFailure = result.isFailureOrTimeout();
55  
56          if ( !timeoutOrOtherFailure )
57          {
58              if ( result.getCompletedCount() == 0 )
59              {
60                  if ( ( reportParameters.getFailIfNoTests() == null ) || !reportParameters.getFailIfNoTests() )
61                  {
62                      return;
63                  }
64                  throw new MojoFailureException(
65                      "No tests were executed!  (Set -DfailIfNoTests=false to ignore this error.)" );
66              }
67  
68              if ( result.isErrorFree() )
69              {
70                  return;
71              }
72          }
73  
74          String msg = timeoutOrOtherFailure
75              ? "There was a timeout or other error in the fork"
76              : "There are test failures.\n\nPlease refer to " + reportParameters.getReportsDirectory()
77                  + " for the individual test results.";
78  
79          if ( reportParameters.isTestFailureIgnore() )
80          {
81              log.error( msg );
82          }
83          else
84          {
85              if ( result.isFailure() )
86              {
87                  throw new MojoExecutionException( msg );
88              }
89              else
90              {
91                  throw new MojoFailureException( msg );
92              }
93          }
94      }
95  
96      public static List<CommandLineOption> commandLineOptions( MavenSession session, Log log )
97      {
98          List<CommandLineOption> cli = new ArrayList<CommandLineOption>();
99          if ( log.isErrorEnabled() )
100         {
101             cli.add( CommandLineOption.LOGGING_LEVEL_ERROR );
102         }
103 
104         if ( log.isWarnEnabled() )
105         {
106             cli.add( CommandLineOption.LOGGING_LEVEL_WARN );
107         }
108 
109         if ( log.isInfoEnabled() )
110         {
111             cli.add( CommandLineOption.LOGGING_LEVEL_INFO );
112         }
113 
114         if ( log.isDebugEnabled() )
115         {
116             cli.add( CommandLineOption.LOGGING_LEVEL_DEBUG );
117         }
118 
119         try
120         {
121             Method getRequestMethod = session.getClass().getMethod( "getRequest" );
122             MavenExecutionRequest request = (MavenExecutionRequest) getRequestMethod.invoke( session );
123 
124             String f = getFailureBehavior( request );
125             if ( f != null )
126             {
127                 // compatible with enums Maven 3.0
128                 cli.add( CommandLineOption.valueOf( f.startsWith( "REACTOR_" ) ? f : "REACTOR_" + f ) );
129             }
130 
131             if ( request.isShowErrors() )
132             {
133                 cli.add( CommandLineOption.SHOW_ERRORS );
134             }
135         }
136         catch ( Exception e )
137         {
138             // don't need to log the exception that Maven 2 does not have getRequest() method in Maven Session
139         }
140         return Collections.unmodifiableList( cli );
141     }
142 
143     public static void logDebugOrCliShowErrors( CharSequence s, Log log, Collection<CommandLineOption> cli )
144     {
145         if ( cli.contains( CommandLineOption.LOGGING_LEVEL_DEBUG ) )
146         {
147             log.debug( s );
148         }
149         else if ( cli.contains( CommandLineOption.SHOW_ERRORS ) )
150         {
151             if ( log.isDebugEnabled() )
152             {
153                 log.debug( s );
154             }
155             else
156             {
157                 log.info( s );
158             }
159         }
160     }
161 
162     private static String getFailureBehavior( MavenExecutionRequest request )
163         throws NoSuchMethodException, InvocationTargetException, IllegalAccessException
164     {
165         try
166         {
167             return request.getFailureBehavior();
168         }
169         catch ( NoSuchMethodError e )
170         {
171             return (String) request.getClass()
172                 .getMethod( "getReactorFailureBehavior" )
173                 .invoke( request );
174         }
175     }
176 
177 }