| 1 |
/* |
| 2 |
* Copyright 2015-2024 the original author or authors. |
| 3 |
* |
| 4 |
* All rights reserved. This program and the accompanying materials are |
| 5 |
* made available under the terms of the Eclipse Public License v2.0 which |
| 6 |
* accompanies this distribution and is available at |
| 7 |
* |
| 8 |
* https://www.eclipse.org/legal/epl-v20.html |
| 9 |
*/ |
| 10 |
|
| 11 |
package org.junit.platform.engine; |
| 12 |
|
| 13 |
import static org.apiguardian.api.API.Status.STABLE; |
| 14 |
import static org.junit.platform.engine.TestExecutionResult.Status.ABORTED; |
| 15 |
import static org.junit.platform.engine.TestExecutionResult.Status.FAILED; |
| 16 |
import static org.junit.platform.engine.TestExecutionResult.Status.SUCCESSFUL; |
| 17 |
|
| 18 |
import java.util.Optional; |
| 19 |
|
| 20 |
import org.apiguardian.api.API; |
| 21 |
import org.junit.platform.commons.util.Preconditions; |
| 22 |
import org.junit.platform.commons.util.ToStringBuilder; |
| 23 |
|
| 24 |
/** |
| 25 |
* {@code TestExecutionResult} encapsulates the result of executing a single test |
| 26 |
* or container. |
| 27 |
* |
| 28 |
* <p>A {@code TestExecutionResult} consists of a mandatory |
| 29 |
* {@link #getStatus() Status} and an optional {@link #getThrowable() Throwable}. |
| 30 |
* |
| 31 |
* @since 1.0 |
| 32 |
*/ |
| 33 |
@API(status = STABLE, since = "1.0") |
| 34 |
public class TestExecutionResult { |
| 35 |
|
| 36 |
/** |
| 37 |
* Status of executing a single test or container. |
| 38 |
*/ |
| 39 |
public enum Status { |
| 40 |
|
| 41 |
/** |
| 42 |
* Indicates that the execution of a test or container was |
| 43 |
* <em>successful</em>. |
| 44 |
*/ |
| 45 |
SUCCESSFUL, |
| 46 |
|
| 47 |
/** |
| 48 |
* Indicates that the execution of a test or container was |
| 49 |
* <em>aborted</em> (started but not finished). |
| 50 |
*/ |
| 51 |
ABORTED, |
| 52 |
|
| 53 |
/** |
| 54 |
* Indicates that the execution of a test or container <em>failed</em>. |
| 55 |
*/ |
| 56 |
FAILED; |
| 57 |
|
| 58 |
} |
| 59 |
|
| 60 |
private static final TestExecutionResult SUCCESSFUL_RESULT = new TestExecutionResult(SUCCESSFUL, null); |
| 61 |
|
| 62 |
/** |
| 63 |
* Create a {@code TestExecutionResult} for a <em>successful</em> execution |
| 64 |
* of a test or container. |
| 65 |
* @return the {@code TestExecutionResult}; never {@code null} |
| 66 |
*/ |
| 67 |
public static TestExecutionResult successful() { |
| 68 |
return SUCCESSFUL_RESULT; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Create a {@code TestExecutionResult} for an <em>aborted</em> execution |
| 73 |
* of a test or container with the supplied {@link Throwable throwable}. |
| 74 |
* |
| 75 |
* @param throwable the throwable that caused the aborted execution; may be |
| 76 |
* {@code null} |
| 77 |
* @return the {@code TestExecutionResult}; never {@code null} |
| 78 |
*/ |
| 79 |
public static TestExecutionResult aborted(Throwable throwable) { |
| 80 |
return new TestExecutionResult(ABORTED, throwable); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Create a {@code TestExecutionResult} for a <em>failed</em> execution |
| 85 |
* of a test or container with the supplied {@link Throwable throwable}. |
| 86 |
* |
| 87 |
* @param throwable the throwable that caused the failed execution; may be |
| 88 |
* {@code null} |
| 89 |
* @return the {@code TestExecutionResult}; never {@code null} |
| 90 |
*/ |
| 91 |
public static TestExecutionResult failed(Throwable throwable) { |
| 92 |
return new TestExecutionResult(FAILED, throwable); |
| 93 |
} |
| 94 |
|
| 95 |
private final Status status; |
| 96 |
private final Throwable throwable; |
| 97 |
|
| 98 |
private TestExecutionResult(Status status, Throwable throwable) { |
| 99 |
this.status = Preconditions.notNull(status, "Status must not be null"); |
| 100 |
this.throwable = throwable; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Get the {@linkplain Status status} of this result. |
| 105 |
* |
| 106 |
* @return the status; never {@code null} |
| 107 |
*/ |
| 108 |
public Status getStatus() { |
| 109 |
return status; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Get the throwable that caused this result, if available. |
| 114 |
* |
| 115 |
* @return an {@code Optional} containing the throwable; never {@code null} |
| 116 |
* but potentially empty |
| 117 |
*/ |
| 118 |
public Optional<Throwable> getThrowable() { |
| 119 |
return Optional.ofNullable(throwable); |
| 120 |
} |
| 121 |
|
| 122 |
@Override |
| 123 |
public String toString() { |
| 124 |
// @formatter:off |
| 125 |
return new ToStringBuilder(this) |
| 126 |
.append("status", status) |
| 127 |
.append("throwable", throwable) |
| 128 |
.toString(); |
| 129 |
// @formatter:on |
| 130 |
} |
| 131 |
|
| 132 |
} |