Skip to content

Commit

Permalink
Change structure of assertion manager to rely on guice instead of flags
Browse files Browse the repository at this point in the history
  • Loading branch information
WasabiFan committed Dec 19, 2015
1 parent c38f592 commit 6dcc437
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 58 deletions.
1 change: 0 additions & 1 deletion src/xbot/common/command/BaseSubsystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public void setDefaultCommand(Command command) {

@Override
protected void initDefaultCommand() {
// TODO Auto-generated method stub

}

Expand Down
3 changes: 3 additions & 0 deletions src/xbot/common/injection/RobotModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import xbot.common.command.SmartDashboardCommandPutter;
import xbot.common.injection.wpi_factories.RealWPIFactory;
import xbot.common.injection.wpi_factories.WPIFactory;
import xbot.common.logging.RobotAssertionManager;
import xbot.common.logging.SilentRobotAssertionManager;
import xbot.common.properties.DatabaseStorageBase;
import xbot.common.properties.ITableProxy;
import xbot.common.properties.RobotDatabaseStorage;
Expand All @@ -19,6 +21,7 @@ protected void configure() {
this.bind(ITableProxy.class).to(SmartDashboardTableWrapper.class);
this.bind(DatabaseStorageBase.class).to(RobotDatabaseStorage.class);
this.bind(SmartDashboardCommandPutter.class).to(RealSmartDashboardCommandPutter.class);
this.bind(RobotAssertionManager.class).to(SilentRobotAssertionManager.class);
}

}
15 changes: 15 additions & 0 deletions src/xbot/common/logging/LoudRobotAssertionManager.java
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;
}

}
7 changes: 7 additions & 0 deletions src/xbot/common/logging/RobotAssertionException.java
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);
}
}
35 changes: 35 additions & 0 deletions src/xbot/common/logging/RobotAssertionManager.java
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);
}

protected abstract void handlePlatformException(RuntimeException e);

public abstract boolean isExceptionsEnabled();

public final void throwException(String message, Throwable cause) {
throwException(new RuntimeException(message, cause));
}

public final void assertTrue(boolean value, String assertionFaliureCause) {
if(!value) {
throwException(new RobotAssertionException(assertionFaliureCause));
}
}
}
7 changes: 0 additions & 7 deletions src/xbot/common/logging/SafeRobotAssertionException.java

This file was deleted.

38 changes: 0 additions & 38 deletions src/xbot/common/logging/SafeRobotAssertionManager.java

This file was deleted.

15 changes: 15 additions & 0 deletions src/xbot/common/logging/SilentRobotAssertionManager.java
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;
}

}
5 changes: 1 addition & 4 deletions tests/xbot/common/injection/BaseWPITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import com.google.inject.Injector;

import xbot.common.controls.MockRobotIO;
import xbot.common.logging.SafeRobotAssertionManager;
import xbot.common.logging.RobotAssertionManager;
import xbot.common.properties.PropertyManager;
import edu.wpi.first.wpilibj.HLUsageReporting;
import edu.wpi.first.wpilibj.MockHLUsageReporting;
Expand Down Expand Up @@ -39,9 +39,6 @@ public void setUp() {
RobotState.SetImplementation(mockRobotState);

propertyManager = injector.getInstance(PropertyManager.class);

SafeRobotAssertionManager assertionMan = injector.getInstance(SafeRobotAssertionManager.class);
assertionMan.setExceptionsEnabled(true);

DOMConfigurator.configure("lib/log4jConfig/log4j4unitTesting.xml");
}
Expand Down
4 changes: 4 additions & 0 deletions tests/xbot/common/injection/UnitTestModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import xbot.common.command.SmartDashboardCommandPutter;
import xbot.common.injection.wpi_factories.MockWPIFactory;
import xbot.common.injection.wpi_factories.WPIFactory;
import xbot.common.logging.LoudRobotAssertionManager;
import xbot.common.logging.RobotAssertionManager;
import xbot.common.properties.DatabaseStorageBase;
import xbot.common.properties.ITableProxy;
import xbot.common.properties.TableProxy;
Expand All @@ -28,5 +30,7 @@ protected void configure() {
this.bind(DatabaseStorageBase.class).to(OffRobotDatabaseStorage.class).in(Singleton.class);

this.bind(SmartDashboardCommandPutter.class).to(MockSmartDashboardCommandPutter.class);

this.bind(RobotAssertionManager.class).to(LoudRobotAssertionManager.class);
}
}
14 changes: 6 additions & 8 deletions tests/xbot/common/logging/SafeRobotAssertionTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,36 @@
public class SafeRobotAssertionTests extends BaseWPITest {
@Test
public void testNoExceptionOnRobot() {
SafeRobotAssertionManager assertMan = injector.getInstance(SafeRobotAssertionManager.class);
assertMan.setExceptionsEnabled(false);
RobotAssertionManager assertMan = new SilentRobotAssertionManager();

assertMan.throwException(new RuntimeException("Something really bad happened (...but robots never die)"));
}

@Test(expected=RuntimeException.class)
public void testExceptionThrownInTests() {
SafeRobotAssertionManager assertMan = injector.getInstance(SafeRobotAssertionManager.class);
RobotAssertionManager assertMan = new LoudRobotAssertionManager();

assertMan.throwException(new RuntimeException("Something really bad happened (tests are free to die as necessary)"));
}

@Test
public void testAssertionContinuesOnRobot() {
SafeRobotAssertionManager assertMan = injector.getInstance(SafeRobotAssertionManager.class);
assertMan.setExceptionsEnabled(false);
RobotAssertionManager assertMan = new SilentRobotAssertionManager();

assertMan.assertTrue(true, "The world is ending");
assertMan.assertTrue(false, "false != true");
}

@Test(expected=SafeRobotAssertionException.class)
@Test(expected=RobotAssertionException.class)
public void testAssertionFailedInTests() {
SafeRobotAssertionManager assertMan = injector.getInstance(SafeRobotAssertionManager.class);
RobotAssertionManager assertMan = new LoudRobotAssertionManager();

assertMan.assertTrue(false, "false != true");
}

@Test()
public void testAssertionPassedInTests() {
SafeRobotAssertionManager assertMan = injector.getInstance(SafeRobotAssertionManager.class);
RobotAssertionManager assertMan = new LoudRobotAssertionManager();

assertMan.assertTrue(true, "The world is ending");
}
Expand Down

0 comments on commit 6dcc437

Please sign in to comment.