diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index e16d9ddd525d..41370dae20be 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -16,7 +16,7 @@ jobs: strategy: fail-fast: false matrix: - java: ['6', '8', '11', '17'] + java: ['6', '8', '11', '17', '18'] steps: - uses: actions/checkout@v2 - name: Download Maven # Download with default JDK because OpenJDK 6 does not support TLS 1.2 diff --git a/pom.xml b/pom.xml index c62a0ed8119a..2e0606e5e758 100644 --- a/pom.xml +++ b/pom.xml @@ -262,7 +262,9 @@ maven-surefire-plugin ${surefireVersion} - org/junit/tests/AllTests.java + + org/junit/tests/AllTests.java + true false diff --git a/src/main/java/org/junit/runner/JUnitCore.java b/src/main/java/org/junit/runner/JUnitCore.java index 31977513994f..c17fbde11c8f 100644 --- a/src/main/java/org/junit/runner/JUnitCore.java +++ b/src/main/java/org/junit/runner/JUnitCore.java @@ -33,8 +33,12 @@ public class JUnitCore { * @param args names of classes in which to find tests to run */ public static void main(String... args) { + System.exit(runMain(args)); + } + + static int runMain(String[] args) { Result result = new JUnitCore().runMain(new RealSystem(), args); - System.exit(result.wasSuccessful() ? 0 : 1); + return result.wasSuccessful() ? 0 : 1; } /** diff --git a/src/main/java/org/junit/runner/Result.java b/src/main/java/org/junit/runner/Result.java index 4b5f4a406238..ff53b084375f 100644 --- a/src/main/java/org/junit/runner/Result.java +++ b/src/main/java/org/junit/runner/Result.java @@ -188,7 +188,7 @@ public SerializedForm(Result result) { } @SuppressWarnings("unchecked") - private SerializedForm(ObjectInputStream.GetField fields) throws IOException { + private SerializedForm(ObjectInputStream.GetField fields) throws IOException, ClassNotFoundException { fCount = (AtomicInteger) fields.get("fCount", null); fIgnoreCount = (AtomicInteger) fields.get("fIgnoreCount", null); assumptionFailureCount = (AtomicInteger) fields.get("assumptionFailureCount", null); diff --git a/src/test/java/org/junit/runner/MainRunner.java b/src/test/java/org/junit/runner/MainRunner.java new file mode 100644 index 000000000000..4ed1be730209 --- /dev/null +++ b/src/test/java/org/junit/runner/MainRunner.java @@ -0,0 +1,23 @@ +package org.junit.runner; + +import java.io.OutputStream; +import java.io.PrintStream; + +public class MainRunner { + + public static int runMain(String... args) { + PrintStream oldOut = System.out; + System.setOut(new PrintStream(new NullOutputStream())); + try { + return JUnitCore.runMain(args); + } finally { + System.setOut(oldOut); + } + } + + static class NullOutputStream extends OutputStream { + public void write(int b) { + // do nothing + } + } +} diff --git a/src/test/java/org/junit/tests/running/core/CommandLineTest.java b/src/test/java/org/junit/tests/running/core/CommandLineTest.java index 37f065902884..29cd4bcd851c 100644 --- a/src/test/java/org/junit/tests/running/core/CommandLineTest.java +++ b/src/test/java/org/junit/tests/running/core/CommandLineTest.java @@ -10,6 +10,7 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.JUnitCore; +import org.junit.runner.MainRunner; public class CommandLineTest { private ByteArrayOutputStream results; @@ -38,11 +39,7 @@ public void test() { @Test public void runATest() { testWasRun = false; - new MainRunner().runWithCheckForSystemExit(new Runnable() { - public void run() { - JUnitCore.main("org.junit.tests.running.core.CommandLineTest$Example"); - } - }); + MainRunner.runMain(Example.class.getName()); assertTrue(testWasRun); } diff --git a/src/test/java/org/junit/tests/running/core/JUnitCoreReturnsCorrectExitCodeTest.java b/src/test/java/org/junit/tests/running/core/JUnitCoreReturnsCorrectExitCodeTest.java index 64af301ae93e..7d35416a92a3 100644 --- a/src/test/java/org/junit/tests/running/core/JUnitCoreReturnsCorrectExitCodeTest.java +++ b/src/test/java/org/junit/tests/running/core/JUnitCoreReturnsCorrectExitCodeTest.java @@ -4,7 +4,7 @@ import static org.junit.Assert.fail; import org.junit.Test; -import org.junit.runner.JUnitCore; +import org.junit.runner.MainRunner; public class JUnitCoreReturnsCorrectExitCodeTest { @@ -37,11 +37,7 @@ public void successCausesExitCodeOf0() throws Exception { } private void runClass(final String className, int returnCode) { - Integer exitValue = new MainRunner().runWithCheckForSystemExit(new Runnable() { - public void run() { - JUnitCore.main(className); - } - }); - assertEquals(Integer.valueOf(returnCode), exitValue); + int exitValue = MainRunner.runMain(className); + assertEquals(returnCode, exitValue); } } diff --git a/src/test/java/org/junit/tests/running/core/MainRunner.java b/src/test/java/org/junit/tests/running/core/MainRunner.java deleted file mode 100644 index ea9088684af8..000000000000 --- a/src/test/java/org/junit/tests/running/core/MainRunner.java +++ /dev/null @@ -1,67 +0,0 @@ -package org.junit.tests.running.core; - -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import java.security.Permission; - -public class MainRunner { - private static class ExitException extends SecurityException { - private static final long serialVersionUID = -9104651568237766642L; - - private final int status; - - public ExitException(int status) { - super(""); - this.status = status; - } - - public int getStatus() { - return status; - } - } - - /** - * A {@code NoExitSecurityManager} throws a {@link ExitException} exception - * whenever {@link #checkExit(int)} is called; all other permissions are allowed. - */ - public class NoExitSecurityManager extends SecurityManager { - - @Override - public void checkExit(int status) { - throw new ExitException(status); - } - - @Override - public void checkPermission(Permission perm) { - if (perm.getName().startsWith("exitVM")) { - super.checkPermission(perm); - } - } - } - - /** - * Execute runnable.run(), preventing System.exit(). If System.exit() is called - * in runnable.run(), the value is returned. If System.exit() - * is not called, null is returned. - * - * @return null if System.exit() is not called, Integer.valueof(status) if not - */ - public Integer runWithCheckForSystemExit(Runnable runnable) { - SecurityManager oldSecurityManager = System.getSecurityManager(); - System.setSecurityManager(new NoExitSecurityManager()); - PrintStream oldOut = System.out; - - System.setOut(new PrintStream(new ByteArrayOutputStream())); - try { - runnable.run(); - System.out.println("System.exit() not called, return null"); - return null; - } catch (ExitException e) { - System.out.println("System.exit() called, value=" + e.getStatus()); - return e.getStatus(); - } finally { - System.setSecurityManager(oldSecurityManager); - System.setOut(oldOut); - } - } -}