| 1 |
/* |
| 2 |
* MIT License |
| 3 |
* |
| 4 |
* Copyright (c) 2024 Todd Ginsberg |
| 5 |
* |
| 6 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 |
* of this software and associated documentation files (the "Software"), to deal |
| 8 |
* in the Software without restriction, including without limitation the rights |
| 9 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 |
* copies of the Software, and to permit persons to whom the Software is |
| 11 |
* furnished to do so, subject to the following conditions: |
| 12 |
* |
| 13 |
* The above copyright notice and this permission notice shall be included in all |
| 14 |
* copies or substantial portions of the Software. |
| 15 |
* |
| 16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 |
* SOFTWARE. |
| 23 |
*/ |
| 24 |
|
| 25 |
package com.ginsberg.junit.exit; |
| 26 |
|
| 27 |
import com.ginsberg.junit.exit.agent.AgentSystemExitHandlerStrategy; |
| 28 |
import com.ginsberg.junit.exit.agent.DoNotRewriteExitCalls; |
| 29 |
import org.junit.jupiter.api.extension.AfterEachCallback; |
| 30 |
import org.junit.jupiter.api.extension.BeforeEachCallback; |
| 31 |
import org.junit.jupiter.api.extension.ExtensionContext; |
| 32 |
import org.junit.jupiter.api.extension.TestExecutionExceptionHandler; |
| 33 |
|
| 34 |
import java.lang.annotation.Annotation; |
| 35 |
import java.util.Optional; |
| 36 |
|
| 37 |
import static org.junit.jupiter.api.Assertions.assertEquals; |
| 38 |
import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 39 |
import static org.junit.jupiter.api.Assertions.assertNull; |
| 40 |
import static org.junit.platform.commons.support.AnnotationSupport.findAnnotation; |
| 41 |
|
| 42 |
/** |
| 43 |
* Entry point for JUnit tests. This class is responsible for installing the preferred `ExitPreventerStrategy` |
| 44 |
* and interpreting the results. |
| 45 |
*/ |
| 46 |
@DoNotRewriteExitCalls |
| 47 |
public class SystemExitExtension implements BeforeEachCallback, AfterEachCallback, TestExecutionExceptionHandler { |
| 48 |
private Integer expectedStatusCode; |
| 49 |
private boolean failOnSystemExit; |
| 50 |
private final ExitPreventerStrategy exitPreventerStrategy; |
| 51 |
|
| 52 |
public SystemExitExtension() { |
| 53 |
if(AgentSystemExitHandlerStrategy.isLoadedFromAgent()) { |
| 54 |
exitPreventerStrategy = new AgentSystemExitHandlerStrategy(); |
| 55 |
} else { |
| 56 |
throw new IllegalStateException("SystemExitExtension Agent not loaded, please see documentation"); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
@Override |
| 61 |
public void afterEach(ExtensionContext context) { |
| 62 |
exitPreventerStrategy.afterTest(); |
| 63 |
|
| 64 |
try { |
| 65 |
if (failOnSystemExit) { |
| 66 |
assertNull( |
| 67 |
exitPreventerStrategy.firstExitStatusCode(), |
| 68 |
"Unexpected System.exit(" + exitPreventerStrategy.firstExitStatusCode() + ") caught" |
| 69 |
); |
| 70 |
} else if (expectedStatusCode == null) { |
| 71 |
assertNotNull( |
| 72 |
exitPreventerStrategy.firstExitStatusCode(), |
| 73 |
"Expected System.exit() to be called, but it was not" |
| 74 |
); |
| 75 |
} else { |
| 76 |
assertEquals( |
| 77 |
expectedStatusCode, |
| 78 |
exitPreventerStrategy.firstExitStatusCode(), |
| 79 |
"Expected System.exit(" + expectedStatusCode + ") to be called, but it was not." |
| 80 |
); |
| 81 |
} |
| 82 |
} finally { |
| 83 |
// Clear state so if this is run as part of a @ParameterizedTest, the next time through we'll have the |
| 84 |
// correct state |
| 85 |
expectedStatusCode = null; |
| 86 |
failOnSystemExit = false; |
| 87 |
exitPreventerStrategy.resetBetweenTests(); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
@Override |
| 92 |
public void beforeEach(final ExtensionContext context) { |
| 93 |
// Should we fail on a System.exit() rather than letting it bubble out? |
| 94 |
failOnSystemExit = getAnnotation(context, FailOnSystemExit.class).isPresent(); |
| 95 |
|
| 96 |
// Get the expected exit status code, if any |
| 97 |
getAnnotation(context, ExpectSystemExitWithStatus.class).ifPresent(code -> expectedStatusCode = code.value()); |
| 98 |
|
| 99 |
// Allow the strategy to do pre-test housekeeping |
| 100 |
exitPreventerStrategy.beforeTest(); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* This is here so we can catch exceptions thrown by the `ExitPreventerStrategy` and prevent them from |
| 105 |
* stopping the annotated test. If anything other than our own exception comes through, throw it because |
| 106 |
* the `ExitPreventerStrategy` has encountered some other test-failing exception. |
| 107 |
* |
| 108 |
* @param context the current extension context; never {@code null} |
| 109 |
* @param throwable the {@code Throwable} to handle; never {@code null} |
| 110 |
* @throws Throwable if the throwable argument is not a SystemExitPreventedException |
| 111 |
*/ |
| 112 |
@Override |
| 113 |
public void handleTestExecutionException( |
| 114 |
final ExtensionContext context, |
| 115 |
final Throwable throwable |
| 116 |
) throws Throwable { |
| 117 |
if (!(throwable instanceof SystemExitPreventedException)) { |
| 118 |
throw throwable; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
// Find the annotation on a method, or failing that, a class. |
| 123 |
private <T extends Annotation> Optional<T> getAnnotation( |
| 124 |
final ExtensionContext context, |
| 125 |
final Class<T> annotationClass |
| 126 |
) { |
| 127 |
final Optional<T> method = findAnnotation(context.getTestMethod(), annotationClass); |
| 128 |
return method.isPresent() ? method : findAnnotation(context.getTestClass(), annotationClass); |
| 129 |
} |
| 130 |
} |