Debian Codegraph @

src/main/java/com/ginsberg/junit/exit/assertions/SystemExitAssertion.java

raw
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.assertions;
26
27 import com.ginsberg.junit.exit.ExitPreventerStrategy;
28 import com.ginsberg.junit.exit.SystemExitPreventedException;
29 import com.ginsberg.junit.exit.agent.AgentSystemExitHandlerStrategy;
30
31 import static org.junit.jupiter.api.Assertions.assertEquals;
32 import static org.junit.jupiter.api.Assertions.assertTrue;
33 import static org.junit.jupiter.api.Assertions.fail;
34
35
36 public class SystemExitAssertion {
37 private final SystemExitPreventedException theException;
38
39 public SystemExitAssertion(SystemExitPreventedException theException) {
40 this.theException = theException;
41 }
42
43 public static SystemExitAssertion assertThatCallsSystemExit(final Runnable function) {
44 return new SystemExitAssertion(catchSystemExitFrom(function)).calledSystemExit();
45 }
46
47 public static void assertThatDoesNotCallSystemExit(final Runnable function) {
48 new SystemExitAssertion(catchSystemExitFrom(function)).didNotCallSystemExit();
49 }
50
51 private SystemExitAssertion calledSystemExit() {
52 if (theException == null) {
53 fail("Expected call to System.exit() did not happen");
54 }
55 return this;
56 }
57
58 private SystemExitAssertion didNotCallSystemExit() {
59 if (theException != null) {
60 fail("Unexpected call to System.exit() with exit code " + theException.getStatusCode(), theException);
61 }
62 return this;
63 }
64
65 private static SystemExitPreventedException catchSystemExitFrom(final Runnable function) {
66 final ExitPreventerStrategy exitPreventerStrategy = new AgentSystemExitHandlerStrategy();
67 try {
68 exitPreventerStrategy.resetBetweenTests();
69 exitPreventerStrategy.beforeTest();
70 function.run();
71 } catch (SystemExitPreventedException e) {
72 return e;
73 } catch (Exception e) {
74 throw new RuntimeException(e);
75 } finally {
76 exitPreventerStrategy.afterTest();
77 }
78 return null;
79 }
80
81 public SystemExitAssertion withExitCode(final int code) {
82 assertEquals(code, theException.getStatusCode(), "Wrong exit code found");
83 return this;
84 }
85
86 public SystemExitAssertion withExitCodeInRange(final int startInclusive, final int endInclusive) {
87 assertTrue(startInclusive < endInclusive, "Start must come before end");
88 final int code = theException.getStatusCode();
89 assertTrue(
90 startInclusive <= code && code <= endInclusive,
91 "Exit code expected in range (" + startInclusive + " .. " + endInclusive + ") but was " + code
92 );
93 return this;
94 }
95
96 }