Skip to content
Browse files

Merge pull request #370 from matthewfarwell/junit-core-run-fixes

Added MainRunner. This removes the need for public methods in JUnitCore
  • Loading branch information...
2 parents b1068dc + 4596630 commit d5bce273008bc4fbc7a0805a21b9e2991313bd3b David Saff committed Apr 3, 2012
View
1 src/main/java/org/junit/internal/JUnitSystem.java
@@ -3,6 +3,5 @@
import java.io.PrintStream;
public interface JUnitSystem {
- void exit(int i);
PrintStream out();
}
View
5 src/main/java/org/junit/internal/RealSystem.java
@@ -3,11 +3,6 @@
import java.io.PrintStream;
public class RealSystem implements JUnitSystem {
-
- public void exit(int code) {
- System.exit(code);
- }
-
public PrintStream out() {
return System.out;
}
View
13 src/main/java/org/junit/runner/JUnitCore.java
@@ -39,12 +39,13 @@ public static void main(String... args) {
}
/**
- * Do not use. Testing purposes only.
- * @param system
+ * Runs main and exits
+ * @param system
+ * @args args from main()
*/
- public static void runMainAndExit(JUnitSystem system, String... args) {
+ private static void runMainAndExit(JUnitSystem system, String... args) {
Result result= new JUnitCore().runMain(system, args);
- system.exit(result.wasSuccessful() ? 0 : 1);
+ System.exit(result.wasSuccessful() ? 0 : 1);
}
/**
@@ -70,10 +71,10 @@ public static Result runClasses(Class<?>... classes) {
}
/**
- * Do not use. Testing purposes only.
* @param system
+ * @args args from main()
*/
- public Result runMain(JUnitSystem system, String... args) {
+ private Result runMain(JUnitSystem system, String... args) {
system.out().println("JUnit version " + Version.id());
List<Class<?>> classes= new ArrayList<Class<?>>();
List<Failure> missingClasses= new ArrayList<Failure>();
View
5 src/test/java/org/junit/tests/TestSystem.java
@@ -8,18 +8,13 @@
public class TestSystem implements JUnitSystem {
private PrintStream out;
- public int fCode;
private ByteArrayOutputStream fOutContents;
public TestSystem() {
fOutContents= new ByteArrayOutputStream();
out= new PrintStream(fOutContents);
}
- public void exit(int code) {
- fCode= code;
- }
-
public PrintStream out() {
return out;
}
View
9 src/test/java/org/junit/tests/running/core/CommandLineTest.java
@@ -9,7 +9,6 @@
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
-import org.junit.internal.RealSystem;
import org.junit.runner.JUnitCore;
public class CommandLineTest {
@@ -34,8 +33,12 @@
}
@Test public void runATest() {
- testWasRun= false; // todo create a TestSystem instead
- new JUnitCore().runMain(new RealSystem(), new String[]{"org.junit.tests.running.core.CommandLineTest$Example"});
+ testWasRun= false;
+ new MainRunner().runWithCheckForSystemExit(new Runnable() {
+ public void run() {
+ JUnitCore.main("org.junit.tests.running.core.CommandLineTest$Example");
+ }
+ });
assertTrue(testWasRun);
}
View
12 src/test/java/org/junit/tests/running/core/JUnitCoreReturnsCorrectExitCodeTest.java
@@ -4,7 +4,6 @@
import static org.junit.Assert.fail;
import org.junit.Test;
import org.junit.runner.JUnitCore;
-import org.junit.tests.TestSystem;
public class JUnitCoreReturnsCorrectExitCodeTest {
@@ -31,9 +30,12 @@
runClass(getClass().getName() + "$Succeed", 0);
}
- private void runClass(String className, int returnCode) {
- TestSystem system= new TestSystem();
- JUnitCore.runMainAndExit(system, className);
- assertEquals(returnCode, system.fCode);
+ 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);
}
}
View
263 src/test/java/org/junit/tests/running/core/MainRunner.java
@@ -0,0 +1,263 @@
+package org.junit.tests.running.core;
+
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+import java.security.Permission;
+import java.io.FileDescriptor;
+import java.net.InetAddress;
+
+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 method calls are
+ * delegated to the original security manager.
+ */
+ public class NoExitSecurityManager extends SecurityManager {
+ private final SecurityManager originalSecurityManager;
+
+ public NoExitSecurityManager(SecurityManager originalSecurityManager) {
+ this.originalSecurityManager= originalSecurityManager;
+ }
+
+ @Override
+ public void checkExit(int status) {
+ throw new ExitException(status);
+ }
+
+ @Override
+ public boolean getInCheck() {
+ return (originalSecurityManager != null) && originalSecurityManager.getInCheck();
+ }
+
+ @Override
+ public Object getSecurityContext() {
+ return (originalSecurityManager == null) ? super.getSecurityContext() : originalSecurityManager.getSecurityContext();
+ }
+
+ @Override
+ public void checkPermission(Permission perm) {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkPermission(perm);
+ }
+
+ @Override
+ public void checkPermission(Permission perm, Object context) {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkPermission(perm, context);
+ }
+
+ @Override
+ public void checkCreateClassLoader() {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkCreateClassLoader();
+ }
+
+ @Override
+ public void checkAccess(Thread t) {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkAccess(t);
+ }
+
+ @Override
+ public void checkAccess(ThreadGroup g) {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkAccess(g);
+ }
+
+ @Override
+ public void checkExec(String cmd) {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkExec(cmd);
+ }
+
+ @Override
+ public void checkLink(String lib) {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkLink(lib);
+ }
+
+ @Override
+ public void checkRead(FileDescriptor fd) {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkRead(fd);
+ }
+
+ @Override
+ public void checkRead(String file) {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkRead(file);
+ }
+
+ @Override
+ public void checkRead(String file, Object context) {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkRead(file, context);
+ }
+
+ @Override
+ public void checkWrite(FileDescriptor fd) {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkWrite(fd);
+ }
+
+ @Override
+ public void checkWrite(String file) {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkWrite(file);
+ }
+
+ @Override
+ public void checkDelete(String file) {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkDelete(file);
+ }
+
+ @Override
+ public void checkConnect(String host, int port) {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkConnect(host, port);
+ }
+
+ @Override
+ public void checkConnect(String host, int port, Object context) {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkConnect(host, port, context);
+ }
+
+ @Override
+ public void checkListen(int port) {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkListen(port);
+ }
+
+ @Override
+ public void checkAccept(String host, int port) {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkAccept(host, port);
+ }
+
+ @Override
+ public void checkMulticast(InetAddress maddr) {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkMulticast(maddr);
+ }
+
+ @Override
+ public void checkMulticast(InetAddress maddr, byte ttl) {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkMulticast(maddr, ttl);
+ }
+
+ @Override
+ public void checkPropertiesAccess() {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkPropertiesAccess();
+ }
+
+ @Override
+ public void checkPropertyAccess(String key) {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkPropertyAccess(key);
+ }
+
+ @Override
+ public boolean checkTopLevelWindow(Object window) {
+ return (originalSecurityManager == null) ? super.checkTopLevelWindow(window) : originalSecurityManager.checkTopLevelWindow(window);
+ }
+
+ @Override
+ public void checkPrintJobAccess() {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkPrintJobAccess();
+ }
+
+ @Override
+ public void checkSystemClipboardAccess() {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkSystemClipboardAccess();
+ }
+
+ @Override
+ public void checkAwtEventQueueAccess() {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkAwtEventQueueAccess();
+ }
+
+ @Override
+ public void checkPackageAccess(String pkg) {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkPackageAccess(pkg);
+ }
+
+ @Override
+ public void checkPackageDefinition(String pkg) {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkPackageDefinition(pkg);
+ }
+
+ @Override
+ public void checkSetFactory() {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkSetFactory();
+ }
+
+ @Override
+ public void checkMemberAccess(Class<?> clazz, int which) {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkMemberAccess(clazz, which);
+ }
+
+ @Override
+ public void checkSecurityAccess(String target) {
+ if (originalSecurityManager != null)
+ originalSecurityManager.checkSecurityAccess(target);
+ }
+
+ @Override
+ public ThreadGroup getThreadGroup() {
+ return (originalSecurityManager == null) ? super.getThreadGroup() : originalSecurityManager.getThreadGroup();
+ }
+ }
+
+ /**
+ * 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.
+ *
+ * @param runnable
+ * @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(oldSecurityManager));
+ PrintStream oldPrintStream= 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(oldPrintStream);
+ }
+ }
+}

0 comments on commit d5bce27

Please sign in to comment.
Something went wrong with that request. Please try again.