View Javadoc
1   package org.apache.maven.surefire.booter;
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  
23  import org.apache.maven.surefire.suite.RunResult;
24  
25  import static org.apache.maven.surefire.util.internal.StringUtils.isNotBlank;
26  
27  /**
28   * Encapsulates exceptions thrown during Surefire forking.
29   */
30  public class SurefireBooterForkException
31      extends Exception
32  {
33      public SurefireBooterForkException( String message, RunResult runResult )
34      {
35          this( message, null, null, runResult );
36      }
37  
38      public SurefireBooterForkException( String message, String rethrownMessage, Throwable rethrownCause,
39                                          RunResult runResult )
40      {
41          super( toString( message, rethrownMessage, rethrownCause, runResult ), rethrownCause );
42      }
43  
44      public SurefireBooterForkException( String message, Throwable cause )
45      {
46          super( message, cause );
47      }
48  
49      public SurefireBooterForkException( String msg )
50      {
51          super( msg );
52      }
53  
54      private static String toString( String message, String rethrownMessage, Throwable rethrownCause,
55                                      RunResult runResult )
56      {
57          return toNewLines( message,
58                                   rethrownMessage,
59                                   rethrownCause == null ? null : rethrownCause.getLocalizedMessage(),
60                                   runResult == null ? null : runResult.getFailure(),
61                                   runResult == null ? null : toString( runResult ) );
62      }
63  
64      private static String toString( RunResult runResult )
65      {
66          return "Fatal Tests run: " + runResult.getCompletedCount()
67                         + ", Failures: " + runResult.getFailures()
68                         + ", Errors: " + runResult.getErrors()
69                         + ", Skipped: " + runResult.getSkipped()
70                         + ", Flakes: " + runResult.getFlakes()
71                         + ", Elapsed timeout: " + runResult.isTimeout();
72      }
73  
74      private static String toNewLines( String... messages )
75      {
76          StringBuilder result = new StringBuilder();
77          for ( String message : messages )
78          {
79              if ( isNotBlank( message ) )
80              {
81                  if ( result.length() == 0 )
82                  {
83                      result.append( '\n' );
84                  }
85                  result.append( message );
86              }
87          }
88          return result.toString();
89      }
90  }