-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added MainRunner. This removes the need for public methods in JUnitCore
MainRunner installs a SecurityManager which traps the System.exit(), thereby removing the need to have runMainAndExit and runMain public in JUnitCore. MainRunner is only used in the tests of course.
- Loading branch information
1 parent
45eaab7
commit 917a88f
Showing
7 changed files
with
89 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,5 @@ | |
import java.io.PrintStream; | ||
|
||
public interface JUnitSystem { | ||
void exit(int i); | ||
PrintStream out(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/test/java/org/junit/tests/running/core/MainRunner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
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; | ||
} | ||
} | ||
|
||
private static class NoExitSecurityManager extends SecurityManager { | ||
@Override | ||
public void checkPermission(Permission perm) { | ||
// allow anything. | ||
} | ||
|
||
@Override | ||
public void checkPermission(Permission perm, Object context) { | ||
// allow anything. | ||
} | ||
|
||
@Override | ||
public void checkExit(int status) { | ||
super.checkExit(status); | ||
throw new ExitException(status); | ||
} | ||
} | ||
|
||
/** | ||
* 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()); | ||
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); | ||
} | ||
} | ||
|
||
} |