View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.surefire.booter;
20  
21  import org.apache.maven.surefire.api.suite.RunResult;
22  
23  import static org.apache.maven.surefire.shared.utils.StringUtils.isNotBlank;
24  
25  /**
26   * Encapsulates exceptions thrown during Surefire forking.
27   */
28  public class SurefireBooterForkException extends Exception {
29      public SurefireBooterForkException(String message, RunResult runResult) {
30          this(message, null, null, runResult);
31      }
32  
33      public SurefireBooterForkException(
34              String message, String rethrownMessage, Throwable rethrownCause, RunResult runResult) {
35          super(toString(message, rethrownMessage, rethrownCause, runResult), rethrownCause);
36      }
37  
38      public SurefireBooterForkException(String message, Throwable cause) {
39          super(message, cause);
40      }
41  
42      public SurefireBooterForkException(String msg) {
43          super(msg);
44      }
45  
46      private static String toString(
47              String message, String rethrownMessage, Throwable rethrownCause, RunResult runResult) {
48          return toNewLines(
49                  message,
50                  rethrownMessage,
51                  rethrownCause == null ? null : rethrownCause.getLocalizedMessage(),
52                  runResult == null ? null : runResult.getFailure(),
53                  runResult == null ? null : toString(runResult));
54      }
55  
56      private static String toString(RunResult runResult) {
57          return "Fatal Tests run: " + runResult.getCompletedCount()
58                  + ", Failures: " + runResult.getFailures()
59                  + ", Errors: " + runResult.getErrors()
60                  + ", Skipped: " + runResult.getSkipped()
61                  + ", Flakes: " + runResult.getFlakes()
62                  + ", Elapsed timeout: " + runResult.isTimeout();
63      }
64  
65      private static String toNewLines(String... messages) {
66          StringBuilder result = new StringBuilder();
67          for (String message : messages) {
68              if (isNotBlank(message)) {
69                  if (result.length() == 0) {
70                      result.append('\n');
71                  }
72                  result.append(message);
73              }
74          }
75          return result.toString();
76      }
77  }