1 package org.apache.maven.plugin.surefire;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
39
40 public final class SurefireHelper
41 {
42
43
44
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
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
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 }