| 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 org.junit.jupiter.api.DisplayName; |
| 29 |
import org.junit.jupiter.api.Nested; |
| 30 |
import org.junit.jupiter.api.Test; |
| 31 |
import org.junit.jupiter.api.condition.EnabledIfSystemProperty; |
| 32 |
|
| 33 |
import java.lang.reflect.Field; |
| 34 |
|
| 35 |
import static com.ginsberg.junit.exit.TestUtils.assertTestFails; |
| 36 |
import static org.junit.jupiter.api.Assertions.assertThrows; |
| 37 |
|
| 38 |
class ExpectSystemExitTest { |
| 39 |
|
| 40 |
@Nested |
| 41 |
@DisplayName("Success Cases") |
| 42 |
class HappyPath { |
| 43 |
@Test |
| 44 |
@DisplayName("System.exit() is caught and detected") |
| 45 |
@ExpectSystemExit |
| 46 |
void detectSystemExit() { |
| 47 |
System.exit(1234); |
| 48 |
} |
| 49 |
|
| 50 |
@Test |
| 51 |
@DisplayName("System.exit() is caught and detected within a thread") |
| 52 |
@ExpectSystemExit |
| 53 |
void detectSystemExitInThread() throws InterruptedException { |
| 54 |
final Thread t = new Thread(() -> System.exit(1234)); |
| 55 |
t.start(); |
| 56 |
t.join(); |
| 57 |
} |
| 58 |
|
| 59 |
@Nested |
| 60 |
@DisplayName("Class Success") |
| 61 |
@ExpectSystemExit |
| 62 |
class ExpectedSuccessClassLevel { |
| 63 |
@Test |
| 64 |
@DisplayName("Method in class annotated with ExpectSystemExit succeeds") |
| 65 |
void classLevelExpect() { |
| 66 |
System.exit(123456); |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
@Nested |
| 72 |
@DisplayName("Failure Cases") |
| 73 |
class FailurePath { |
| 74 |
@Test |
| 75 |
@DisplayName("System.exit() is expected for method but not called") |
| 76 |
void expectSystemExitThatDoesNotHappenMethod() { |
| 77 |
assertTestFails(ExpectedFailuresAtTestLevelTest.class, "doNotCallSystemExit"); |
| 78 |
} |
| 79 |
|
| 80 |
@Test |
| 81 |
@DisplayName("System.exit() is expected for class but not called") |
| 82 |
void expectSystemExitThatDoesNotHappenClass() { |
| 83 |
assertTestFails(ExpectedFailuresAtClassLevelTest.class); |
| 84 |
} |
| 85 |
|
| 86 |
@Test |
| 87 |
@DisplayName("Extension fails when agent instrumentation has not run") |
| 88 |
void expectAgentInstrumentation() throws NoSuchFieldException, IllegalAccessException { |
| 89 |
// Arrange |
| 90 |
final Field field = AgentSystemExitHandlerStrategy.class.getDeclaredField("loadedFromAgent"); |
| 91 |
field.setAccessible(true); |
| 92 |
field.setBoolean(AgentSystemExitHandlerStrategy.class, false); |
| 93 |
|
| 94 |
// Act |
| 95 |
try { |
| 96 |
assertThrows(IllegalStateException.class, SystemExitExtension::new); |
| 97 |
} finally { |
| 98 |
field.setBoolean(AgentSystemExitHandlerStrategy.class, true); |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
@SuppressWarnings("JUnitMalformedDeclaration") |
| 104 |
@EnabledIfSystemProperty(named = "running_within_test", matches = "true") |
| 105 |
static class ExpectedFailuresAtTestLevelTest { |
| 106 |
@Test |
| 107 |
@ExpectSystemExit |
| 108 |
void doNotCallSystemExit() { |
| 109 |
// Done! :) |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
@SuppressWarnings("JUnitMalformedDeclaration") |
| 114 |
@EnabledIfSystemProperty(named = "running_within_test", matches = "true") |
| 115 |
@ExpectSystemExit |
| 116 |
static class ExpectedFailuresAtClassLevelTest { |
| 117 |
@Test |
| 118 |
void doNotCallSystemExit() { |
| 119 |
// Done! :) |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
} |