diff --git a/wpilibj/build.gradle b/wpilibj/build.gradle index b15d6d8d59..1b3854a9fb 100644 --- a/wpilibj/build.gradle +++ b/wpilibj/build.gradle @@ -13,6 +13,11 @@ sourceSets { dependencies { sharedCompile 'edu.wpi.first.wpilib.networktables.java:NetworkTables:3.0.0-SNAPSHOT:arm' sharedRuntime 'edu.wpi.first.wpilib.networktables.java:NetworkTables:3.0.0-SNAPSHOT:arm' + testCompile 'org.hamcrest:hamcrest-all:1.3' + testCompile 'junit:junit:4.12' + testCompile 'edu.wpi.first.wpilib.networktables.java:NetworkTables:3.0.0-SNAPSHOT:desktop' + testCompile 'com.google.guava:guava:19.0' + testCompile sourceSets.shared.output } apply from: 'athena.gradle' @@ -20,3 +25,10 @@ apply from: 'athena.gradle' if (project.hasProperty('makeSim')) { apply from: 'simulation.gradle' } + +test { + testLogging { + events "failed" + exceptionFormat "full" + } +} diff --git a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/CircularBufferTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/CircularBufferTest.java similarity index 90% rename from wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/CircularBufferTest.java rename to wpilibj/src/test/java/edu/wpi/first/wpilibj/CircularBufferTest.java index e44c14e9fe..ecd95b436c 100644 --- a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/CircularBufferTest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/CircularBufferTest.java @@ -7,16 +7,12 @@ package edu.wpi.first.wpilibj; +import org.junit.BeforeClass; import org.junit.Test; -import java.util.logging.Logger; - -import edu.wpi.first.wpilibj.test.AbstractComsSetup; - import static org.junit.Assert.assertEquals; -public class CircularBufferTest extends AbstractComsSetup { - private static final Logger logger = Logger.getLogger(CircularBufferTest.class.getName()); +public class CircularBufferTest { private double[] m_values = {751.848, 766.366, 342.657, 234.252, 716.126, 132.344, 445.697, 22.727, 421.125, 799.913}; private double[] m_pushFrontOut = {799.913, 421.125, 22.727, 445.697, 132.344, @@ -24,6 +20,11 @@ public class CircularBufferTest extends AbstractComsSetup { private double[] m_pushBackOut = {342.657, 234.252, 716.126, 132.344, 445.697, 22.727, 421.125, 799.913}; + @BeforeClass + public static void before() { + UnitTestUtility.setupMockBase(); + } + @Test public void pushFrontTest() { CircularBuffer queue = new CircularBuffer(8); @@ -94,8 +95,4 @@ public class CircularBufferTest extends AbstractComsSetup { assertEquals(4.0, queue.get(0), 0.00005); } - @Override - protected Logger getClassLogger() { - return logger; - } } diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/MockRobotStateInterface.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/MockRobotStateInterface.java new file mode 100644 index 0000000000..fbace75fe4 --- /dev/null +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/MockRobotStateInterface.java @@ -0,0 +1,31 @@ +package edu.wpi.first.wpilibj; + +/** + * A concrete implementation of the robot state interface that can be used in UnitTests. + */ +public class MockRobotStateInterface implements RobotState.Interface { + @Override + public boolean isDisabled() { + return false; + } + + @Override + public boolean isEnabled() { + return true; + } + + @Override + public boolean isOperatorControl() { + return false; + } + + @Override + public boolean isAutonomous() { + return false; + } + + @Override + public boolean isTest() { + return true; + } +} diff --git a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/PIDToleranceTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/PIDToleranceTest.java similarity index 89% rename from wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/PIDToleranceTest.java rename to wpilibj/src/test/java/edu/wpi/first/wpilibj/PIDToleranceTest.java index b373b38b7c..effb5751d5 100644 --- a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/PIDToleranceTest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/PIDToleranceTest.java @@ -2,22 +2,23 @@ package edu.wpi.first.wpilibj; import org.junit.After; import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; -import java.util.logging.Logger; - -import edu.wpi.first.wpilibj.test.AbstractComsSetup; - import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -public class PIDToleranceTest extends AbstractComsSetup { - private static final Logger logger = Logger.getLogger(PIDToleranceTest.class.getName()); +public class PIDToleranceTest { private PIDController m_pid; private final double m_setPoint = 50.0; private final double m_tolerance = 10.0; private final double m_range = 200; + @BeforeClass + public static void setupClass() { + UnitTestUtility.setupMockBase(); + } + private class FakeInput implements PIDSource { public double m_val; @@ -48,10 +49,6 @@ public class PIDToleranceTest extends AbstractComsSetup { }; - @Override - protected Logger getClassLogger() { - return logger; - } @Before public void setUp() throws Exception { diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/UnitTestUtility.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/UnitTestUtility.java new file mode 100644 index 0000000000..8c55ad136a --- /dev/null +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/UnitTestUtility.java @@ -0,0 +1,91 @@ +package edu.wpi.first.wpilibj; + +import com.google.common.base.Stopwatch; + +import java.util.concurrent.TimeUnit; + +import edu.wpi.first.wpilibj.util.BaseSystemNotInitializedException; + +/** + * Utility class for configuring unit tests. + */ +public final class UnitTestUtility { + private UnitTestUtility() { + /* no-op */ + } + + /** + * Sets up the base system WPILib so that it does not rely on hardware. + */ + public static void setupMockBase() { + try { + // Check to see if this has been setup + Timer.getFPGATimestamp(); + } catch (BaseSystemNotInitializedException ex) { + // If it hasn't been then do this setup + + HLUsageReporting.SetImplementation(new HLUsageReporting.Null()); + RobotState.SetImplementation(new MockRobotStateInterface()); + Timer.SetImplementation(new Timer.StaticInterface() { + + @Override + public double getFPGATimestamp() { + return System.currentTimeMillis() / 1000.0; + } + + @Override + public double getMatchTime() { + return 0; + } + + @Override + public void delay(double seconds) { + try { + Thread.sleep((long) (seconds * 1e3)); + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + throw new RuntimeException("Thread was interrupted", ex); + } + } + + @Override + public Timer.Interface newTimer() { + return new Timer.Interface() { + private final Stopwatch m_stopwatch = Stopwatch.createUnstarted(); + + @Override + public double get() { + return m_stopwatch.elapsed(TimeUnit.SECONDS); + } + + @Override + public void reset() { + m_stopwatch.reset(); + } + + @Override + public void start() { + m_stopwatch.start(); + } + + @Override + public void stop() { + m_stopwatch.stop(); + } + + @Override + public boolean hasPeriodPassed(double period) { + if (get() > period) { + // Advance the start time by the period. + // Don't set it to the current time... we want to avoid drift. + m_stopwatch.reset().start(); + return true; + } + return false; + } + }; + } + }); + } + } +} diff --git a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/command/AbstractCommandTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/AbstractCommandTest.java similarity index 91% rename from wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/command/AbstractCommandTest.java rename to wpilibj/src/test/java/edu/wpi/first/wpilibj/command/AbstractCommandTest.java index 6d584bd470..28925a4261 100644 --- a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/command/AbstractCommandTest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/AbstractCommandTest.java @@ -9,8 +9,7 @@ package edu.wpi.first.wpilibj.command; import org.junit.Before; -import edu.wpi.first.wpilibj.mocks.MockCommand; -import edu.wpi.first.wpilibj.test.AbstractComsSetup; +import edu.wpi.first.wpilibj.UnitTestUtility; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; @@ -20,10 +19,11 @@ import static org.junit.Assert.fail; * * @author jonathanleitschuh */ -public abstract class AbstractCommandTest extends AbstractComsSetup { +public abstract class AbstractCommandTest { @Before public void commandSetup() { + UnitTestUtility.setupMockBase(); Scheduler.getInstance().removeAll(); Scheduler.getInstance().enable(); } diff --git a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/command/ButtonTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/ButtonTest.java similarity index 95% rename from wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/command/ButtonTest.java rename to wpilibj/src/test/java/edu/wpi/first/wpilibj/command/ButtonTest.java index 6f7cbb8fbd..bb315fbdc1 100644 --- a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/command/ButtonTest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/ButtonTest.java @@ -10,10 +10,7 @@ package edu.wpi.first.wpilibj.command; import org.junit.Before; import org.junit.Test; -import java.util.logging.Logger; - import edu.wpi.first.wpilibj.buttons.InternalButton; -import edu.wpi.first.wpilibj.mocks.MockCommand; /** @@ -24,15 +21,9 @@ import edu.wpi.first.wpilibj.mocks.MockCommand; * @author jonathanleitschuh */ public class ButtonTest extends AbstractCommandTest { - private static final Logger logger = Logger.getLogger(ButtonTest.class.getName()); - private InternalButton m_button1; private InternalButton m_button2; - protected Logger getClassLogger() { - return logger; - } - @Before public void setUp() throws Exception { m_button1 = new InternalButton(); diff --git a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/command/CommandParallelGroupTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/CommandParallelGroupTest.java similarity index 90% rename from wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/command/CommandParallelGroupTest.java rename to wpilibj/src/test/java/edu/wpi/first/wpilibj/command/CommandParallelGroupTest.java index 14c549eb35..77fa06495e 100644 --- a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/command/CommandParallelGroupTest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/CommandParallelGroupTest.java @@ -9,10 +9,6 @@ package edu.wpi.first.wpilibj.command; import org.junit.Test; -import java.util.logging.Logger; - -import edu.wpi.first.wpilibj.mocks.MockCommand; - /** * Ported from the old CrioTest Classes. * @@ -20,11 +16,6 @@ import edu.wpi.first.wpilibj.mocks.MockCommand; * @author Jonathan Leitschuh */ public class CommandParallelGroupTest extends AbstractCommandTest { - private static final Logger logger = Logger.getLogger(CommandParallelGroupTest.class.getName()); - - protected Logger getClassLogger() { - return logger; - } /** * Simple Parallel Command Group With 2 commands one command terminates first. diff --git a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/command/CommandScheduleTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/CommandScheduleTest.java similarity index 90% rename from wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/command/CommandScheduleTest.java rename to wpilibj/src/test/java/edu/wpi/first/wpilibj/command/CommandScheduleTest.java index bacd551e20..153dd32118 100644 --- a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/command/CommandScheduleTest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/CommandScheduleTest.java @@ -9,10 +9,6 @@ package edu.wpi.first.wpilibj.command; import org.junit.Test; -import java.util.logging.Logger; - -import edu.wpi.first.wpilibj.mocks.MockCommand; - /** * Ported from the old CrioTest Classes. * @@ -20,11 +16,6 @@ import edu.wpi.first.wpilibj.mocks.MockCommand; * @author Jonathan Leitschuh */ public class CommandScheduleTest extends AbstractCommandTest { - private static final Logger logger = Logger.getLogger(CommandScheduleTest.class.getName()); - - protected Logger getClassLogger() { - return logger; - } /** * Simple scheduling of a command and making sure the command is run and successfully terminates. diff --git a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/command/CommandSequentialGroupTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/CommandSequentialGroupTest.java similarity index 94% rename from wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/command/CommandSequentialGroupTest.java rename to wpilibj/src/test/java/edu/wpi/first/wpilibj/command/CommandSequentialGroupTest.java index 141dae38a4..bad64f3a9f 100644 --- a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/command/CommandSequentialGroupTest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/CommandSequentialGroupTest.java @@ -11,8 +11,6 @@ import org.junit.Test; import java.util.logging.Logger; -import edu.wpi.first.wpilibj.mocks.MockCommand; - /** * Ported from the old CrioTest Classes. * @@ -20,11 +18,7 @@ import edu.wpi.first.wpilibj.mocks.MockCommand; * @author Jonathan Leitschuh */ public class CommandSequentialGroupTest extends AbstractCommandTest { - private static final Logger logger = Logger.getLogger(CommandSequentialGroupTest.class.getName()); - - protected Logger getClassLogger() { - return logger; - } + private static Logger logger = Logger.getLogger(CommandSequentialGroupTest.class.getName()); /** * Simple Command Group With 3 commands that all depend on a subsystem. Some commands have a diff --git a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/command/CommandSupersedeTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/CommandSupersedeTest.java similarity index 97% rename from wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/command/CommandSupersedeTest.java rename to wpilibj/src/test/java/edu/wpi/first/wpilibj/command/CommandSupersedeTest.java index 3eba9cf1f3..a2e2437313 100644 --- a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/command/CommandSupersedeTest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/CommandSupersedeTest.java @@ -11,8 +11,6 @@ import org.junit.Test; import java.util.logging.Logger; -import edu.wpi.first.wpilibj.mocks.MockCommand; - /** * Ported from the old CrioTest Classes. * @@ -22,9 +20,6 @@ import edu.wpi.first.wpilibj.mocks.MockCommand; public class CommandSupersedeTest extends AbstractCommandTest { private static final Logger logger = Logger.getLogger(CommandSupersedeTest.class.getName()); - protected Logger getClassLogger() { - return logger; - } /** * Testing one command superseding another because of dependencies. diff --git a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/command/CommandTimeoutTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/CommandTimeoutTest.java similarity index 87% rename from wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/command/CommandTimeoutTest.java rename to wpilibj/src/test/java/edu/wpi/first/wpilibj/command/CommandTimeoutTest.java index 2e0a56a14b..fd88fc2fb4 100644 --- a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/command/CommandTimeoutTest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/CommandTimeoutTest.java @@ -9,21 +9,13 @@ package edu.wpi.first.wpilibj.command; import org.junit.Test; -import java.util.logging.Logger; - -import edu.wpi.first.wpilibj.mocks.MockCommand; - /** * Test a {@link Command} that times out. * * @author jonathanleitschuh + * */ public class CommandTimeoutTest extends AbstractCommandTest { - private static final Logger logger = Logger.getLogger(CommandTimeoutTest.class.getName()); - - protected Logger getClassLogger() { - return logger; - } /** * Command 2 second Timeout Test. diff --git a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/command/DefaultCommandTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/DefaultCommandTest.java similarity index 95% rename from wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/command/DefaultCommandTest.java rename to wpilibj/src/test/java/edu/wpi/first/wpilibj/command/DefaultCommandTest.java index 7c7f15ab8c..f2a078590c 100644 --- a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/command/DefaultCommandTest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/DefaultCommandTest.java @@ -9,21 +9,12 @@ package edu.wpi.first.wpilibj.command; import org.junit.Test; -import java.util.logging.Logger; - -import edu.wpi.first.wpilibj.mocks.MockCommand; - /** * Tests the {@link Command} library. * * @author jonathanleitschuh */ public class DefaultCommandTest extends AbstractCommandTest { - private static final Logger logger = Logger.getLogger(DefaultCommandTest.class.getName()); - - protected Logger getClassLogger() { - return logger; - } /** diff --git a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/mocks/MockCommand.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/MockCommand.java similarity index 96% rename from wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/mocks/MockCommand.java rename to wpilibj/src/test/java/edu/wpi/first/wpilibj/command/MockCommand.java index bddd2e6878..39d096c629 100644 --- a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/mocks/MockCommand.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/MockCommand.java @@ -5,9 +5,7 @@ /* the project. */ /*----------------------------------------------------------------------------*/ -package edu.wpi.first.wpilibj.mocks; - -import edu.wpi.first.wpilibj.command.Command; +package edu.wpi.first.wpilibj.command; /** * A class to simulate a simple command The command keeps track of how many times each method was diff --git a/wpilibjIntegrationTests/build.gradle b/wpilibjIntegrationTests/build.gradle index a4b532d351..8d309a503c 100644 --- a/wpilibjIntegrationTests/build.gradle +++ b/wpilibjIntegrationTests/build.gradle @@ -29,8 +29,11 @@ dependencies { compile 'com.googlecode.junit-toolbox:junit-toolbox:2.0' compile 'org.apache.ant:ant:1.9.4' compile 'org.apache.ant:ant-junit:1.9.4' + compile files(wpilibj.sourceSets.test.output.classesDir) } +compileJava.dependsOn tasks.getByPath(':wpilibj:testClasses') + build.dependsOn shadowJar jar {