-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from Team488/test-only-exceptions
Add ability to write test-only assertions in common code
- Loading branch information
Showing
9 changed files
with
124 additions
and
1 deletion.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package xbot.common.logging; | ||
|
||
public class LoudRobotAssertionManager extends RobotAssertionManager { | ||
|
||
@Override | ||
protected void handlePlatformException(RuntimeException e) { | ||
throw e; | ||
} | ||
|
||
@Override | ||
public boolean isExceptionsEnabled() { | ||
return true; | ||
} | ||
|
||
} |
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,7 @@ | ||
package xbot.common.logging; | ||
|
||
public class RobotAssertionException extends RuntimeException { | ||
public RobotAssertionException(String failureCauseCause) { | ||
super("Assertion error: " + failureCauseCause); | ||
} | ||
} |
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,35 @@ | ||
package xbot.common.logging; | ||
|
||
import java.util.Arrays; | ||
import java.util.stream.Collectors; | ||
|
||
import org.apache.log4j.Logger; | ||
|
||
public abstract class RobotAssertionManager { | ||
static Logger log = Logger.getLogger(RobotAssertionManager.class); | ||
|
||
public final void throwException(RuntimeException e) { | ||
log.error("Safe exception encountered (exception throw " + (this.isExceptionsEnabled() ? "enabled" : "disabled") + "): " | ||
+ e.getMessage()); | ||
log.error("Stack trace: \n " | ||
+ Arrays.stream(e.getStackTrace()) | ||
.map(elem -> elem.toString()) | ||
.collect(Collectors.joining("\n "))); | ||
|
||
handlePlatformException(e); | ||
} | ||
|
||
public final void throwException(String message, Throwable cause) { | ||
throwException(new RuntimeException(message, cause)); | ||
} | ||
|
||
protected abstract void handlePlatformException(RuntimeException e); | ||
|
||
public abstract boolean isExceptionsEnabled(); | ||
|
||
public final void assertTrue(boolean value, String assertionFaliureCause) { | ||
if(!value) { | ||
throwException(new RobotAssertionException(assertionFaliureCause)); | ||
} | ||
} | ||
} |
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,15 @@ | ||
package xbot.common.logging; | ||
|
||
public class SilentRobotAssertionManager extends RobotAssertionManager { | ||
|
||
@Override | ||
protected void handlePlatformException(RuntimeException e) { | ||
// Don't do anything: we don't need to throw | ||
} | ||
|
||
@Override | ||
public boolean isExceptionsEnabled() { | ||
return false; | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package xbot.common.logging; | ||
|
||
import static org.junit.Assert.*; | ||
import org.junit.Test; | ||
|
||
import xbot.common.injection.BaseWPITest; | ||
|
||
public class SafeRobotAssertionTests extends BaseWPITest { | ||
@Test | ||
public void testNoExceptionOnRobot() { | ||
RobotAssertionManager assertMan = new SilentRobotAssertionManager(); | ||
|
||
assertMan.throwException(new RuntimeException("Something really bad happened (...but robots never die)")); | ||
} | ||
|
||
@Test(expected=RuntimeException.class) | ||
public void testExceptionThrownInTests() { | ||
RobotAssertionManager assertMan = new LoudRobotAssertionManager(); | ||
|
||
assertMan.throwException(new RuntimeException("Something really bad happened (tests are free to die as necessary)")); | ||
} | ||
|
||
@Test | ||
public void testAssertionContinuesOnRobot() { | ||
RobotAssertionManager assertMan = new SilentRobotAssertionManager(); | ||
|
||
assertMan.assertTrue(true, "The world is ending"); | ||
assertMan.assertTrue(false, "false != true"); | ||
} | ||
|
||
@Test(expected=RobotAssertionException.class) | ||
public void testAssertionFailedInTests() { | ||
RobotAssertionManager assertMan = new LoudRobotAssertionManager(); | ||
|
||
assertMan.assertTrue(false, "false != true"); | ||
} | ||
|
||
@Test() | ||
public void testAssertionPassedInTests() { | ||
RobotAssertionManager assertMan = new LoudRobotAssertionManager(); | ||
|
||
assertMan.assertTrue(true, "The world is ending"); | ||
} | ||
} |