diff --git a/cscore/src/test/java/edu/wpi/cscore/JNITest.java b/cscore/src/test/java/edu/wpi/cscore/JNITest.java index 1e8b023934..b3c1062e8c 100644 --- a/cscore/src/test/java/edu/wpi/cscore/JNITest.java +++ b/cscore/src/test/java/edu/wpi/cscore/JNITest.java @@ -7,12 +7,12 @@ package edu.wpi.cscore; -import org.junit.Test; +import org.junit.jupiter.api.Test; -public class JNITest { +class JNITest { @Test - public void jniLinkTest() { + void jniLinkTest() { // Test to verify that the JNI test link works correctly. - edu.wpi.cscore.CameraServerJNI.getHostname(); + CameraServerJNI.getHostname(); } } diff --git a/hal/src/test/java/edu/wpi/first/hal/sim/AccelerometerSimTest.java b/hal/src/test/java/edu/wpi/first/hal/sim/AccelerometerSimTest.java index e7e4634d38..67e7169cbf 100644 --- a/hal/src/test/java/edu/wpi/first/hal/sim/AccelerometerSimTest.java +++ b/hal/src/test/java/edu/wpi/first/hal/sim/AccelerometerSimTest.java @@ -7,25 +7,24 @@ package edu.wpi.first.hal.sim; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import org.junit.Test; +import org.junit.jupiter.api.Test; import edu.wpi.first.wpilibj.hal.AccelerometerJNI; import edu.wpi.first.wpilibj.hal.HAL; import edu.wpi.first.wpilibj.sim.AccelerometerSim; import edu.wpi.first.wpilibj.sim.CallbackStore; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; -public class AccelerometerSimTest { +class AccelerometerSimTest { static class TriggeredStore { - public boolean wasTriggered = false; + public boolean wasTriggered; public boolean setValue = true; } @Test - public void testCallbacks() { + void testCallbacks() { HAL.initialize(500, 0); AccelerometerSim sim = new AccelerometerSim(); sim.resetData(); diff --git a/ntcore/src/test/java/edu/wpi/first/networktables/ConnectionListenerTest.java b/ntcore/src/test/java/edu/wpi/first/networktables/ConnectionListenerTest.java index f51c7420b8..333948e204 100644 --- a/ntcore/src/test/java/edu/wpi/first/networktables/ConnectionListenerTest.java +++ b/ntcore/src/test/java/edu/wpi/first/networktables/ConnectionListenerTest.java @@ -44,6 +44,7 @@ class ConnectionListenerTest { /** * Connect to the server. */ + @SuppressWarnings("PMD.AvoidUsingHardCodedIP") private void connect() { m_serverInst.startServer("connectionlistenertest.ini", "127.0.0.1", 10000); m_clientInst.startClient("127.0.0.1", 10000); @@ -112,6 +113,7 @@ class ConnectionListenerTest { @Test @DisabledOnOs(OS.WINDOWS) + @SuppressWarnings("PMD.AvoidUsingHardCodedIP") void testThreaded() { m_serverInst.startServer("connectionlistenertest.ini", "127.0.0.1", 10000); List events = new ArrayList<>(); diff --git a/ntcore/src/test/java/edu/wpi/first/networktables/EntryListenerTest.java b/ntcore/src/test/java/edu/wpi/first/networktables/EntryListenerTest.java index 7c510246e8..80627a4a2f 100644 --- a/ntcore/src/test/java/edu/wpi/first/networktables/EntryListenerTest.java +++ b/ntcore/src/test/java/edu/wpi/first/networktables/EntryListenerTest.java @@ -10,14 +10,21 @@ package edu.wpi.first.networktables; import java.util.ArrayList; import java.util.List; -import junit.framework.TestCase; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -public class EntryListenerTest extends TestCase { - NetworkTableInstance m_serverInst; - NetworkTableInstance m_clientInst; +import static org.junit.jupiter.api.Assertions.assertAll; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; - @Override - protected void setUp() throws Exception { +class EntryListenerTest { + private NetworkTableInstance m_serverInst; + private NetworkTableInstance m_clientInst; + + @BeforeEach + void setUp() { m_serverInst = NetworkTableInstance.create(); m_serverInst.setNetworkIdentity("server"); @@ -25,12 +32,13 @@ public class EntryListenerTest extends TestCase { m_clientInst.setNetworkIdentity("client"); } - @Override - protected void tearDown() throws Exception { + @AfterEach + void tearDown() { m_clientInst.close(); m_serverInst.close(); } + @SuppressWarnings("PMD.AvoidUsingHardCodedIP") private void connect() { m_serverInst.startServer("connectionlistenertest.ini", "127.0.0.1", 10000); m_clientInst.startClient("127.0.0.1", 10000); @@ -51,7 +59,8 @@ public class EntryListenerTest extends TestCase { /** * Test prefix with a new remote. */ - public void testPrefixNewRemote() { + @Test + void testPrefixNewRemote() { connect(); List events = new ArrayList<>(); final int handle = m_serverInst.addEntryListener("/foo", events::add, @@ -70,11 +79,13 @@ public class EntryListenerTest extends TestCase { assertTrue(m_serverInst.waitForEntryListenerQueue(1.0)); // Check the event - assertEquals(events.size(), 1); - assertEquals(events.get(0).listener, handle); - assertEquals(events.get(0).getEntry(), m_serverInst.getEntry("/foo/bar")); - assertEquals(events.get(0).name, "/foo/bar"); - assertEquals(events.get(0).value, NetworkTableValue.makeDouble(1.0)); - assertEquals(events.get(0).flags, EntryListenerFlags.kNew); + assertAll("Event", + () -> assertEquals(1, events.size()), + () -> assertEquals(handle, events.get(0).listener), + () -> assertEquals(m_serverInst.getEntry("/foo/bar"), events.get(0).getEntry()), + () -> assertEquals("/foo/bar", events.get(0).name), + () -> assertEquals(NetworkTableValue.makeDouble(1.0), events.get(0).value), + () -> assertEquals(EntryListenerFlags.kNew, events.get(0).flags) + ); } } diff --git a/ntcore/src/test/java/edu/wpi/first/networktables/JNITest.java b/ntcore/src/test/java/edu/wpi/first/networktables/JNITest.java index d71d10434c..ef2b42b416 100644 --- a/ntcore/src/test/java/edu/wpi/first/networktables/JNITest.java +++ b/ntcore/src/test/java/edu/wpi/first/networktables/JNITest.java @@ -7,11 +7,11 @@ package edu.wpi.first.networktables; -import org.junit.Test; +import org.junit.jupiter.api.Test; -public class JNITest { +class JNITest { @Test - public void jniLinkTest() { + void jniLinkTest() { // Test to verify that the JNI test link works correctly. int inst = NetworkTablesJNI.getDefaultInstance(); NetworkTablesJNI.flush(inst); diff --git a/ntcore/src/test/java/edu/wpi/first/networktables/LoggerTest.java b/ntcore/src/test/java/edu/wpi/first/networktables/LoggerTest.java index a31d9233c0..420f9dc058 100644 --- a/ntcore/src/test/java/edu/wpi/first/networktables/LoggerTest.java +++ b/ntcore/src/test/java/edu/wpi/first/networktables/LoggerTest.java @@ -10,25 +10,29 @@ package edu.wpi.first.networktables; import java.util.ArrayList; import java.util.List; -import junit.framework.TestCase; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -public class LoggerTest extends TestCase { - NetworkTableInstance m_clientInst; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.fail; - @Override - protected void setUp() throws Exception { +class LoggerTest { + private NetworkTableInstance m_clientInst; + + @BeforeEach + protected void setUp() { m_clientInst = NetworkTableInstance.create(); } - @Override - protected void tearDown() throws Exception { + @AfterEach + protected void tearDown() { m_clientInst.close(); } - /** - * Test the logger. - */ - public void testLogger() { + @Test + @SuppressWarnings("PMD.AvoidUsingHardCodedIP") + void addMessageTest() { List msgs = new ArrayList<>(); m_clientInst.addLogger(msgs::add, LogMessage.kInfo, 100); diff --git a/ntcore/src/test/java/edu/wpi/first/networktables/NetworkTableTest.java b/ntcore/src/test/java/edu/wpi/first/networktables/NetworkTableTest.java index 91eeb8656c..e45f197cef 100644 --- a/ntcore/src/test/java/edu/wpi/first/networktables/NetworkTableTest.java +++ b/ntcore/src/test/java/edu/wpi/first/networktables/NetworkTableTest.java @@ -7,68 +7,74 @@ package edu.wpi.first.networktables; -import java.util.ArrayList; +import java.util.Arrays; import java.util.List; +import java.util.stream.Stream; -import junit.framework.TestCase; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; -public class NetworkTableTest extends TestCase { - public void testBasenameKey() { - assertEquals("simple", NetworkTable.basenameKey("simple")); - assertEquals("simple", NetworkTable.basenameKey("one/two/many/simple")); - assertEquals("simple", - NetworkTable.basenameKey("//////an/////awful/key////simple")); +import static org.junit.jupiter.api.Assertions.assertEquals; + +class NetworkTableTest { + private static Stream basenameKeyArguments() { + return Stream.of( + Arguments.of("simple", "simple"), + Arguments.of("simple", "one/two/many/simple"), + Arguments.of("simple", "//////an/////awful/key////simple") + ); } - public void testNormalizeKeySlash() { - assertEquals("/", NetworkTable.normalizeKey("///")); - assertEquals("/no/normal/req", NetworkTable.normalizeKey("/no/normal/req")); - assertEquals("/no/leading/slash", - NetworkTable.normalizeKey("no/leading/slash")); - assertEquals( - "/what/an/awful/key/", - NetworkTable.normalizeKey("//////what////an/awful/////key///")); + @ParameterizedTest + @MethodSource("basenameKeyArguments") + void basenameKeyTest(final String expected, final String testString) { + assertEquals(expected, NetworkTable.basenameKey(testString)); } - public void testNormalizeKeyNoSlash() { - assertEquals("a", NetworkTable.normalizeKey("a", false)); - assertEquals("a", NetworkTable.normalizeKey("///a", false)); - assertEquals("leading/slash", - NetworkTable.normalizeKey("/leading/slash", false)); - assertEquals("no/leading/slash", - NetworkTable.normalizeKey("no/leading/slash", false)); - assertEquals( - "what/an/awful/key/", - NetworkTable.normalizeKey("//////what////an/awful/////key///", false)); + private static Stream normalizeKeySlashArguments() { + return Stream.of( + Arguments.of("/", "///"), + Arguments.of("/no/normal/req", "/no/normal/req"), + Arguments.of("/no/leading/slash", "no/leading/slash"), + Arguments.of("/what/an/awful/key/", "//////what////an/awful/////key///") + ); } - public void testGetHierarchyEmpty() { - List expected = new ArrayList<>(); - expected.add("/"); - assertEquals(expected, NetworkTable.getHierarchy("")); + @ParameterizedTest + @MethodSource("normalizeKeySlashArguments") + void normalizeKeySlashTest(final String expected, final String testString) { + assertEquals(expected, NetworkTable.normalizeKey(testString)); } - public void testGetHierarchyRoot() { - List expected = new ArrayList<>(); - expected.add("/"); - assertEquals(expected, NetworkTable.getHierarchy("/")); + private static Stream normalizeKeyNoSlashArguments() { + return Stream.of( + Arguments.of("a", "a"), + Arguments.of("a", "///a"), + Arguments.of("leading/slash", "/leading/slash"), + Arguments.of("no/leading/slash", "no/leading/slash"), + Arguments.of("what/an/awful/key/", "//////what////an/awful/////key///") + ); } - public void testGetHierarchyNormal() { - List expected = new ArrayList<>(); - expected.add("/"); - expected.add("/foo"); - expected.add("/foo/bar"); - expected.add("/foo/bar/baz"); - assertEquals(expected, NetworkTable.getHierarchy("/foo/bar/baz")); + @ParameterizedTest + @MethodSource("normalizeKeyNoSlashArguments") + void normalizeKeyNoSlashTest(final String expected, final String testString) { + assertEquals(expected, NetworkTable.normalizeKey(testString, false)); } - public void testGetHierarchyTrailingSlash() { - List expected = new ArrayList<>(); - expected.add("/"); - expected.add("/foo"); - expected.add("/foo/bar"); - expected.add("/foo/bar/"); - assertEquals(expected, NetworkTable.getHierarchy("/foo/bar/")); + private static Stream getHierarchyArguments() { + return Stream.of( + Arguments.of(Arrays.asList("/"), ""), + Arguments.of(Arrays.asList("/"), "/"), + Arguments.of(Arrays.asList("/", "/foo", "/foo/bar", "/foo/bar/baz"), "/foo/bar/baz"), + Arguments.of(Arrays.asList("/", "/foo", "/foo/bar", "/foo/bar/"), "/foo/bar/") + ); + } + + @ParameterizedTest + @MethodSource("getHierarchyArguments") + void getHierarchyTest(final List expected, final String testString) { + assertEquals(expected, NetworkTable.getHierarchy(testString)); } } diff --git a/shared/java/javacommon.gradle b/shared/java/javacommon.gradle index e614892a69..0265d2e912 100644 --- a/shared/java/javacommon.gradle +++ b/shared/java/javacommon.gradle @@ -115,10 +115,9 @@ compileJava { } dependencies { - testCompileOnly 'junit:junit:4.12' testImplementation 'org.junit.jupiter:junit-jupiter-api:5.2.0' + testImplementation 'org.junit.jupiter:junit-jupiter-params:5.2.0' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.2.0' - testRuntimeOnly 'org.junit.vintage:junit-vintage-engine:5.2.0' devCompile sourceSets.main.output } diff --git a/styleguide/pmd-ruleset.xml b/styleguide/pmd-ruleset.xml index 1656e00fce..24cfd0dd11 100644 --- a/styleguide/pmd-ruleset.xml +++ b/styleguide/pmd-ruleset.xml @@ -7,7 +7,7 @@ PMD Ruleset for WPILib .*/*JNI.* - .*/*Test.* + .*/*IntegrationTests.* @@ -16,6 +16,8 @@ + + @@ -23,6 +25,12 @@ + + + + + @@ -61,33 +69,6 @@ class="net.sourceforge.pmd.lang.java.rule.migrating.UnnecessaryCastRule" externalInfoUrl="https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/migrating/UnnecessaryCastRule.java" /> - - - + diff --git a/wpilibj/build.gradle b/wpilibj/build.gradle index 3c61493f7b..c91795b92f 100644 --- a/wpilibj/build.gradle +++ b/wpilibj/build.gradle @@ -60,8 +60,6 @@ dependencies { compile project(':ntcore') compile project(':cscore') compile project(':cameraserver') - testCompile 'org.hamcrest:hamcrest-all:1.3' - testCompile 'junit:junit:4.12' testCompile 'com.google.guava:guava:19.0' devCompile project(':hal') devCompile project(':wpiutil') diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/CircularBufferTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/CircularBufferTest.java index 407a4056d6..3919fad995 100644 --- a/wpilibj/src/test/java/edu/wpi/first/wpilibj/CircularBufferTest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/CircularBufferTest.java @@ -7,26 +7,26 @@ package edu.wpi.first.wpilibj; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; -public class CircularBufferTest { - private double[] m_values = {751.848, 766.366, 342.657, 234.252, 716.126, +class CircularBufferTest { + private final 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_addFirstOut = {799.913, 421.125, 22.727, 445.697, 132.344, + private final double[] m_addFirstOut = {799.913, 421.125, 22.727, 445.697, 132.344, 716.126, 234.252, 342.657}; - private double[] m_addLastOut = {342.657, 234.252, 716.126, 132.344, 445.697, + private final double[] m_addLastOut = {342.657, 234.252, 716.126, 132.344, 445.697, 22.727, 421.125, 799.913}; - @BeforeClass - public static void before() { + @BeforeAll + static void before() { UnitTestUtility.setupMockBase(); } @Test - public void addFirstTest() { + void addFirstTest() { CircularBuffer queue = new CircularBuffer(8); for (double value : m_values) { @@ -39,7 +39,7 @@ public class CircularBufferTest { } @Test - public void addLastTest() { + void addLastTest() { CircularBuffer queue = new CircularBuffer(8); for (double value : m_values) { @@ -52,7 +52,7 @@ public class CircularBufferTest { } @Test - public void pushPopTest() { + void pushPopTest() { CircularBuffer queue = new CircularBuffer(3); // Insert three elements into the buffer @@ -96,7 +96,7 @@ public class CircularBufferTest { } @Test - public void resetTest() { + void resetTest() { CircularBuffer queue = new CircularBuffer(5); for (int i = 0; i < 6; i++) { @@ -111,7 +111,8 @@ public class CircularBufferTest { } @Test - public void resizeTest() { + @SuppressWarnings("PMD.ExcessiveMethodLength") + void resizeTest() { CircularBuffer queue = new CircularBuffer(5); /* Buffer contains {1, 2, 3, _, _} diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/PIDToleranceTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/PIDToleranceTest.java index fe79ffccf8..03684d90cc 100644 --- a/wpilibj/src/test/java/edu/wpi/first/wpilibj/PIDToleranceTest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/PIDToleranceTest.java @@ -7,22 +7,22 @@ package edu.wpi.first.wpilibj; -import org.junit.After; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; -public class PIDToleranceTest { +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; + private static final double m_setPoint = 50.0; + private static final double m_tolerance = 10.0; + private static final double m_range = 200; - @BeforeClass - public static void setupClass() { + @BeforeAll + static void setupClass() { UnitTestUtility.setupMockBase(); } @@ -49,7 +49,7 @@ public class PIDToleranceTest { } private FakeInput m_inp; - private PIDOutput m_out = new PIDOutput() { + private final PIDOutput m_out = new PIDOutput() { @Override public void pidWrite(double out) { } @@ -57,53 +57,53 @@ public class PIDToleranceTest { }; - @Before - public void setUp() throws Exception { + @BeforeEach + void setUp() { m_inp = new FakeInput(); m_pid = new PIDController(0.05, 0.0, 0.0, m_inp, m_out); m_pid.setInputRange(-m_range / 2, m_range / 2); } - @After - public void tearDown() throws Exception { + @AfterEach + void tearDown() { m_pid.close(); m_pid = null; } @Test - public void testAbsoluteTolerance() { + void absoluteToleranceTest() { m_pid.setAbsoluteTolerance(m_tolerance); m_pid.setSetpoint(m_setPoint); m_pid.enable(); Timer.delay(1); - assertFalse("Error was in tolerance when it should not have been. Error was " - + m_pid.getError(), m_pid.onTarget()); + assertFalse(m_pid.onTarget(), "Error was in tolerance when it should not have been. Error was " + + m_pid.getError()); m_inp.m_val = m_setPoint + m_tolerance / 2; Timer.delay(1.0); - assertTrue("Error was not in tolerance when it should have been. Error was " - + m_pid.getError(), m_pid.onTarget()); + assertTrue(m_pid.onTarget(), "Error was not in tolerance when it should have been. Error was " + + m_pid.getError()); m_inp.m_val = m_setPoint + 10 * m_tolerance; Timer.delay(1.0); - assertFalse("Error was in tolerance when it should not have been. Error was " - + m_pid.getError(), m_pid.onTarget()); + assertFalse(m_pid.onTarget(), "Error was in tolerance when it should not have been. Error was " + + m_pid.getError()); } @Test - public void testPercentTolerance() { + void percentToleranceTest() { m_pid.setPercentTolerance(m_tolerance); m_pid.setSetpoint(m_setPoint); m_pid.enable(); - assertFalse("Error was in tolerance when it should not have been. Error was " - + m_pid.getError(), m_pid.onTarget()); + assertFalse(m_pid.onTarget(), "Error was in tolerance when it should not have been. Error was " + + m_pid.getError()); //half of percent tolerance away from setPoint m_inp.m_val = m_setPoint + m_tolerance / 200 * m_range; Timer.delay(1.0); - assertTrue("Error was not in tolerance when it should have been. Error was " - + m_pid.getError(), m_pid.onTarget()); + assertTrue(m_pid.onTarget(), "Error was not in tolerance when it should have been. Error was " + + m_pid.getError()); //double percent tolerance away from setPoint m_inp.m_val = m_setPoint + m_tolerance / 50 * m_range; Timer.delay(1.0); - assertFalse("Error was in tolerance when it should not have been. Error was " - + m_pid.getError(), m_pid.onTarget()); + assertFalse(m_pid.onTarget(), "Error was in tolerance when it should not have been. Error was " + + m_pid.getError()); } } diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/RobotControllerTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/RobotControllerTest.java index 055488ad16..576eb8be88 100644 --- a/wpilibj/src/test/java/edu/wpi/first/wpilibj/RobotControllerTest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/RobotControllerTest.java @@ -7,8 +7,8 @@ package edu.wpi.first.wpilibj; -public class RobotControllerTest extends UtilityClassTest { - public RobotControllerTest() { +class RobotControllerTest extends UtilityClassTest { + RobotControllerTest() { super(RobotController.class); } } diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/SpeedControllerGroupTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/SpeedControllerGroupTest.java index 0c1cc739b3..62e2f46f14 100644 --- a/wpilibj/src/test/java/edu/wpi/first/wpilibj/SpeedControllerGroupTest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/SpeedControllerGroupTest.java @@ -8,107 +8,102 @@ package edu.wpi.first.wpilibj; import java.util.Arrays; -import java.util.Collection; +import java.util.stream.DoubleStream; +import java.util.stream.IntStream; +import java.util.stream.Stream; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; -@RunWith(Parameterized.class) -public class SpeedControllerGroupTest { - private final SpeedController[] m_speedControllers; - private final SpeedControllerGroup m_group; - - /** - * Returns a Collection of ArrayLists with various MockSpeedController configurations. - */ - @Parameterized.Parameters - public static Collection data() { - return Arrays.asList((Object[][][]) new SpeedController[][][] { - {{new MockSpeedController()}}, - {{new MockSpeedController(), - new MockSpeedController()}}, - {{new MockSpeedController(), - new MockSpeedController(), - new MockSpeedController()}} +class SpeedControllerGroupTest { + private static Stream speedControllerArguments() { + return IntStream.of(1, 2, 3).mapToObj(number -> { + SpeedController[] speedControllers = Stream.generate(MockSpeedController::new) + .limit(number) + .toArray(SpeedController[]::new); + SpeedControllerGroup group = new SpeedControllerGroup(speedControllers[0], + Arrays.copyOfRange(speedControllers, 1, speedControllers.length)); + return Arguments.of(group, speedControllers); }); } - /** - * Construct SpeedControllerGroupTest. - */ - public SpeedControllerGroupTest(SpeedController[] speedControllers) { - m_group = new SpeedControllerGroup(speedControllers[0], - Arrays.copyOfRange(speedControllers, 1, speedControllers.length)); - m_speedControllers = speedControllers; + @ParameterizedTest + @MethodSource("speedControllerArguments") + void setTest(final SpeedControllerGroup group, final SpeedController[] speedControllers) { + group.set(1.0); + + assertArrayEquals(DoubleStream.generate(() -> 1.0).limit(speedControllers.length).toArray(), + Arrays.stream(speedControllers).mapToDouble(SpeedController::get).toArray(), + 0.00005); } - @Test - public void testSet() { - m_group.set(1.0); + @ParameterizedTest + @MethodSource("speedControllerArguments") + void getInvertedTest(final SpeedControllerGroup group, + final SpeedController[] speedControllers) { + group.setInverted(true); - assertArrayEquals(Arrays.stream(m_speedControllers).mapToDouble(__ -> 1.0).toArray(), - Arrays.stream(m_speedControllers).mapToDouble(SpeedController::get).toArray(), - 0.0); + assertTrue(group.getInverted()); } - @Test - public void testGetInverted() { - m_group.setInverted(true); + @ParameterizedTest + @MethodSource("speedControllerArguments") + void setInvertedDoesNotModifySpeedControllersTest(final SpeedControllerGroup group, + final SpeedController[] speedControllers) { + group.setInverted(true); - assertTrue(m_group.getInverted()); + assertArrayEquals(Stream.generate(() -> false).limit(speedControllers.length).toArray(), + Arrays.stream(speedControllers).map(SpeedController::getInverted).toArray()); } - @Test - public void testSetInvertedDoesNotModifySpeedControllers() { - for (SpeedController speedController : m_speedControllers) { - speedController.setInverted(false); - } - m_group.setInverted(true); + @ParameterizedTest + @MethodSource("speedControllerArguments") + void setInvertedDoesInvertTest(final SpeedControllerGroup group, + final SpeedController[] speedControllers) { + group.setInverted(true); + group.set(1.0); - assertArrayEquals(Arrays.stream(m_speedControllers).map(__ -> false).toArray(), - Arrays.stream(m_speedControllers).map(SpeedController::getInverted).toArray()); + assertArrayEquals(DoubleStream.generate(() -> -1.0).limit(speedControllers.length).toArray(), + Arrays.stream(speedControllers).mapToDouble(SpeedController::get).toArray(), + 0.00005); } - @Test - public void testSetInvertedDoesInvert() { - m_group.setInverted(true); - m_group.set(1.0); + @ParameterizedTest + @MethodSource("speedControllerArguments") + void disableTest(final SpeedControllerGroup group, + final SpeedController[] speedControllers) { + group.set(1.0); + group.disable(); - assertArrayEquals(Arrays.stream(m_speedControllers).mapToDouble(__ -> -1.0).toArray(), - Arrays.stream(m_speedControllers).mapToDouble(SpeedController::get).toArray(), - 0.0); + assertArrayEquals(DoubleStream.generate(() -> 0.0).limit(speedControllers.length).toArray(), + Arrays.stream(speedControllers).mapToDouble(SpeedController::get).toArray(), + 0.00005); } - @Test - public void testDisable() { - m_group.set(1.0); - m_group.disable(); + @ParameterizedTest + @MethodSource("speedControllerArguments") + void stopMotorTest(final SpeedControllerGroup group, + final SpeedController[] speedControllers) { + group.set(1.0); + group.stopMotor(); - assertArrayEquals(Arrays.stream(m_speedControllers).mapToDouble(__ -> 0.0).toArray(), - Arrays.stream(m_speedControllers).mapToDouble(SpeedController::get).toArray(), - 0.0); + assertArrayEquals(DoubleStream.generate(() -> 0.0).limit(speedControllers.length).toArray(), + Arrays.stream(speedControllers).mapToDouble(SpeedController::get).toArray(), + 0.00005); } - @Test - public void testStopMotor() { - m_group.set(1.0); - m_group.stopMotor(); + @ParameterizedTest + @MethodSource("speedControllerArguments") + void pidWriteTest(final SpeedControllerGroup group, + final SpeedController[] speedControllers) { + group.pidWrite(1.0); - assertArrayEquals(Arrays.stream(m_speedControllers).mapToDouble(__ -> 0.0).toArray(), - Arrays.stream(m_speedControllers).mapToDouble(SpeedController::get).toArray(), - 0.0); - } - - @Test - public void testPidWrite() { - m_group.pidWrite(1.0); - - assertArrayEquals(Arrays.stream(m_speedControllers).mapToDouble(__ -> 1.0).toArray(), - Arrays.stream(m_speedControllers).mapToDouble(SpeedController::get).toArray(), - 0.0); + assertArrayEquals(DoubleStream.generate(() -> 1.0).limit(speedControllers.length).toArray(), + Arrays.stream(speedControllers).mapToDouble(SpeedController::get).toArray(), + 0.00005); } } diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/UnitTestUtility.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/UnitTestUtility.java index a601aa9685..1cb784ea2d 100644 --- a/wpilibj/src/test/java/edu/wpi/first/wpilibj/UnitTestUtility.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/UnitTestUtility.java @@ -52,7 +52,6 @@ public final class UnitTestUtility { Thread.sleep((long) (seconds * 1e3)); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); - throw new RuntimeException("Thread was interrupted", ex); } } diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/UtilityClassTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/UtilityClassTest.java index 0814a5ddad..cc94aac474 100644 --- a/wpilibj/src/test/java/edu/wpi/first/wpilibj/UtilityClassTest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/UtilityClassTest.java @@ -9,23 +9,25 @@ package edu.wpi.first.wpilibj; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import java.lang.reflect.Modifier; -import java.util.ArrayList; import java.util.Arrays; -import java.util.List; +import java.util.stream.Stream; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.DynamicTest; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestFactory; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.DynamicTest.dynamicTest; @SuppressWarnings("PMD.AbstractClassWithoutAbstractMethod") public abstract class UtilityClassTest { - @Before - public void setup() { + @BeforeAll + static void setup() { UnitTestUtility.setupMockBase(); } @@ -36,38 +38,30 @@ public abstract class UtilityClassTest { } @Test - public void testSingleConstructor() { - assertEquals("More than one constructor defined", 1, - m_clazz.getDeclaredConstructors().length); + public void singleConstructorTest() { + assertEquals(1, m_clazz.getDeclaredConstructors().length, + "More than one constructor defined"); } @Test - public void testConstructorPrivate() { + public void constructorPrivateTest() { Constructor constructor = m_clazz.getDeclaredConstructors()[0]; - assertFalse("Constructor is not private", constructor.isAccessible()); + assertFalse(constructor.isAccessible(), "Constructor is not private"); } - @Test(expected = InvocationTargetException.class) - public void testConstructorReflection() throws Throwable { + @Test + public void constructorReflectionTest() { Constructor constructor = m_clazz.getDeclaredConstructors()[0]; constructor.setAccessible(true); - constructor.newInstance(); + assertThrows(InvocationTargetException.class, () -> constructor.newInstance()); } - @Test - public void testPublicMethodsStatic() { - List failures = new ArrayList<>(); - for (Method method : m_clazz.getDeclaredMethods()) { - int modifiers = method.getModifiers(); - if (Modifier.isPublic(modifiers) && !Modifier.isStatic(modifiers)) { - failures.add(method.toString()); - } - } - - if (!failures.isEmpty()) { - fail("Found public methods that are not static: " - + Arrays.toString(failures.toArray())); - } + @TestFactory + Stream publicMethodsStaticTestFactory() { + return Arrays.stream(m_clazz.getDeclaredMethods()) + .filter(method -> Modifier.isPublic(method.getModifiers())) + .map(method -> dynamicTest(method.getName(), + () -> assertTrue(Modifier.isStatic(method.getModifiers())))); } } diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/can/CANStatusTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/can/CANStatusTest.java index 97b06a17c1..b1417c6dad 100644 --- a/wpilibj/src/test/java/edu/wpi/first/wpilibj/can/CANStatusTest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/can/CANStatusTest.java @@ -7,16 +7,17 @@ package edu.wpi.first.wpilibj.can; -import org.junit.Test; +import org.junit.jupiter.api.Test; import edu.wpi.first.wpilibj.hal.HAL; -public class CANStatusTest { +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; + +class CANStatusTest { @Test - public void canStatusGetDoesntThrow() { + void canStatusGetDoesntThrow() { HAL.initialize(500, 0); CANStatus status = new CANStatus(); - CANJNI.GetCANStatus(status); - // Nothing we can assert, so just make sure it didn't throw. + assertDoesNotThrow(() -> CANJNI.GetCANStatus(status)); } } diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/AbstractCommandTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/AbstractCommandTest.java index b6ff039589..57a546330f 100644 --- a/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/AbstractCommandTest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/AbstractCommandTest.java @@ -7,19 +7,20 @@ package edu.wpi.first.wpilibj.command; -import org.junit.Before; +import org.junit.jupiter.api.BeforeEach; import edu.wpi.first.wpilibj.UnitTestUtility; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertAll; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; /** * The basic test for all {@link Command} tests. */ public abstract class AbstractCommandTest { - @Before - public void commandSetup() { + @BeforeEach + void commandSetup() { UnitTestUtility.setupMockBase(); Scheduler.getInstance().removeAll(); Scheduler.getInstance().enable(); @@ -43,18 +44,16 @@ public abstract class AbstractCommandTest { protected void assertCommandState(MockCommand command, int initialize, int execute, int isFinished, int end, int interrupted) { - assertEquals(initialize, command.getInitializeCount()); - assertEquals(execute, command.getExecuteCount()); - assertEquals(isFinished, command.getIsFinishedCount()); - assertEquals(end, command.getEndCount()); - assertEquals(interrupted, command.getInterruptedCount()); + assertAll( + () -> assertEquals(initialize, command.getInitializeCount()), + () -> assertEquals(execute, command.getExecuteCount()), + () -> assertEquals(isFinished, command.getIsFinishedCount()), + () -> assertEquals(end, command.getEndCount()), + () -> assertEquals(interrupted, command.getInterruptedCount()) + ); } protected void sleep(int time) { - try { - Thread.sleep(time); - } catch (InterruptedException ex) { - fail("Sleep Interrupted!?!?!?!?"); - } + assertDoesNotThrow(() -> Thread.sleep(time)); } } diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/ButtonTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/ButtonTest.java index 64fb060390..3686c78076 100644 --- a/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/ButtonTest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/ButtonTest.java @@ -7,8 +7,8 @@ package edu.wpi.first.wpilibj.command; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import edu.wpi.first.wpilibj.buttons.InternalButton; @@ -17,12 +17,12 @@ import edu.wpi.first.wpilibj.buttons.InternalButton; * Test that covers the {@link edu.wpi.first.wpilibj.buttons.Button} with the {@link Command} * library. */ -public class ButtonTest extends AbstractCommandTest { +class ButtonTest extends AbstractCommandTest { private InternalButton m_button1; private InternalButton m_button2; - @Before - public void setUp() throws Exception { + @BeforeEach + void setUp() { m_button1 = new InternalButton(); m_button2 = new InternalButton(); } @@ -31,7 +31,7 @@ public class ButtonTest extends AbstractCommandTest { * Simple Button Test. */ @Test - public void test() { + void buttonTest() { final MockCommand command1 = new MockCommand(); final MockCommand command2 = new MockCommand(); final MockCommand command3 = new MockCommand(); diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/CommandParallelGroupTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/CommandParallelGroupTest.java index 8291f3ecfa..b3b7ad9739 100644 --- a/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/CommandParallelGroupTest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/CommandParallelGroupTest.java @@ -7,17 +7,17 @@ package edu.wpi.first.wpilibj.command; -import org.junit.Test; +import org.junit.jupiter.api.Test; /** * Ported from the old CrioTest Classes. */ -public class CommandParallelGroupTest extends AbstractCommandTest { +class CommandParallelGroupTest extends AbstractCommandTest { /** * Simple Parallel Command Group With 2 commands one command terminates first. */ @Test - public void testParallelCommandGroupWithTwoCommands() { + void parallelCommandGroupWithTwoCommandsTest() { final MockCommand command1 = new MockCommand(); final MockCommand command2 = new MockCommand(); diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/CommandScheduleTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/CommandScheduleTest.java index d324f31226..feb80bcbfa 100644 --- a/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/CommandScheduleTest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/CommandScheduleTest.java @@ -7,17 +7,17 @@ package edu.wpi.first.wpilibj.command; -import org.junit.Test; +import org.junit.jupiter.api.Test; /** * Ported from the old CrioTest Classes. */ -public class CommandScheduleTest extends AbstractCommandTest { +class CommandScheduleTest extends AbstractCommandTest { /** * Simple scheduling of a command and making sure the command is run and successfully terminates. */ @Test - public void testRunAndTerminate() { + void runAndTerminateTest() { final MockCommand command = new MockCommand(); command.start(); assertCommandState(command, 0, 0, 0, 0, 0); @@ -39,7 +39,7 @@ public class CommandScheduleTest extends AbstractCommandTest { * Simple scheduling of a command and making sure the command is run and cancels correctly. */ @Test - public void testRunAndCancel() { + void runAndCancelTest() { final MockCommand command = new MockCommand(); command.start(); assertCommandState(command, 0, 0, 0, 0, 0); diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/CommandSequentialGroupTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/CommandSequentialGroupTest.java index a7ecca45aa..cb24ab565b 100644 --- a/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/CommandSequentialGroupTest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/CommandSequentialGroupTest.java @@ -9,41 +9,29 @@ package edu.wpi.first.wpilibj.command; import java.util.logging.Logger; -import org.junit.Test; +import org.junit.jupiter.api.Test; /** * Ported from the old CrioTest Classes. */ -public class CommandSequentialGroupTest extends AbstractCommandTest { - private static Logger logger = Logger.getLogger(CommandSequentialGroupTest.class.getName()); +class CommandSequentialGroupTest extends AbstractCommandTest { + private static final Logger logger = Logger.getLogger(CommandSequentialGroupTest.class.getName()); /** * Simple Command Group With 3 commands that all depend on a subsystem. Some commands have a * timeout. */ - @Test(timeout = 20000) + @Test public void testThreeCommandOnSubSystem() { logger.fine("Begining Test"); final ASubsystem subsystem = new ASubsystem(); logger.finest("Creating Mock Command1"); - final MockCommand command1 = new MockCommand() { - { - requires(subsystem); - } - }; + final MockCommand command1 = new MockCommand(subsystem); logger.finest("Creating Mock Command2"); - final MockCommand command2 = new MockCommand() { - { - requires(subsystem); - } - }; + final MockCommand command2 = new MockCommand(subsystem); logger.finest("Creating Mock Command3"); - final MockCommand command3 = new MockCommand() { - { - requires(subsystem); - } - }; + final MockCommand command3 = new MockCommand(subsystem); logger.finest("Creating Command Group"); final CommandGroup commandGroup = new CommandGroup(); diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/CommandSupersedeTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/CommandSupersedeTest.java index 0373d58b18..4ba965f838 100644 --- a/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/CommandSupersedeTest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/CommandSupersedeTest.java @@ -7,35 +7,22 @@ package edu.wpi.first.wpilibj.command; -import java.util.logging.Logger; - -import org.junit.Test; +import org.junit.jupiter.api.Test; /** * Ported from the old CrioTest Classes. */ -public class CommandSupersedeTest extends AbstractCommandTest { - private static final Logger logger = Logger.getLogger(CommandSupersedeTest.class.getName()); - - +class CommandSupersedeTest extends AbstractCommandTest { /** * Testing one command superseding another because of dependencies. */ @Test - public void testOneCommandSupersedingAnotherBecauseOfDependencies() { + void oneCommandSupersedingAnotherBecauseOfDependenciesTest() { final ASubsystem subsystem = new ASubsystem(); - final MockCommand command1 = new MockCommand() { - { - requires(subsystem); - } - }; + final MockCommand command1 = new MockCommand(subsystem); - final MockCommand command2 = new MockCommand() { - { - requires(subsystem); - } - }; + final MockCommand command2 = new MockCommand(subsystem); assertCommandState(command1, 0, 0, 0, 0, 0); assertCommandState(command2, 0, 0, 0, 0, 0); @@ -76,21 +63,17 @@ public class CommandSupersedeTest extends AbstractCommandTest { * command cannot be interrupted. */ @Test - public void testCommandFailingSupersedingBecauseFirstCanNotBeInterrupted() { + @SuppressWarnings("PMD.NonStaticInitializer") + void commandFailingSupersedingBecauseFirstCanNotBeInterruptedTest() { final ASubsystem subsystem = new ASubsystem(); - final MockCommand command1 = new MockCommand() { + final MockCommand command1 = new MockCommand(subsystem) { { - requires(subsystem); setInterruptible(false); } }; - final MockCommand command2 = new MockCommand() { - { - requires(subsystem); - } - }; + final MockCommand command2 = new MockCommand(subsystem); assertCommandState(command1, 0, 0, 0, 0, 0); assertCommandState(command2, 0, 0, 0, 0, 0); @@ -116,5 +99,4 @@ public class CommandSupersedeTest extends AbstractCommandTest { assertCommandState(command1, 1, 4, 4, 0, 0); assertCommandState(command2, 0, 0, 0, 0, 0); } - } diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/CommandTimeoutTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/CommandTimeoutTest.java index fe191b1792..06674a2fa6 100644 --- a/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/CommandTimeoutTest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/CommandTimeoutTest.java @@ -7,26 +7,21 @@ package edu.wpi.first.wpilibj.command; -import org.junit.Test; +import org.junit.jupiter.api.Test; /** * Test a {@link Command} that times out. */ -public class CommandTimeoutTest extends AbstractCommandTest { +class CommandTimeoutTest extends AbstractCommandTest { /** * Command 2 second Timeout Test. */ @Test - public void testTwoSecondTimeout() { + void twoSecondTimeoutTest() { final ASubsystem subsystem = new ASubsystem(); - final MockCommand command = new MockCommand() { - { - requires(subsystem); - setTimeout(2); - } - + final MockCommand command = new MockCommand(subsystem, 2) { @Override public boolean isFinished() { return super.isFinished() || isTimedOut(); @@ -49,5 +44,4 @@ public class CommandTimeoutTest extends AbstractCommandTest { Scheduler.getInstance().run(); assertCommandState(command, 1, 4, 4, 1, 0); } - } diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/ConditionalCommandTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/ConditionalCommandTest.java index 8a1c720cc7..6147974eff 100644 --- a/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/ConditionalCommandTest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/ConditionalCommandTest.java @@ -7,15 +7,13 @@ package edu.wpi.first.wpilibj.command; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; -//import org.junit.Ignore; - -public class ConditionalCommandTest extends AbstractCommandTest { +class ConditionalCommandTest extends AbstractCommandTest { MockConditionalCommand m_command; MockConditionalCommand m_commandNull; MockCommand m_onTrue; @@ -23,8 +21,8 @@ public class ConditionalCommandTest extends AbstractCommandTest { MockSubsystem m_subsys; Boolean m_condition; - @Before - public void initCommands() { + @BeforeEach + void initCommands() { m_subsys = new MockSubsystem(); m_onTrue = new MockCommand(m_subsys); m_onFalse = new MockCommand(m_subsys); @@ -43,7 +41,7 @@ public class ConditionalCommandTest extends AbstractCommandTest { } @Test - public void testOnTrue() { + void onTrueTest() { m_command.setCondition(true); Scheduler.getInstance().add(m_command); @@ -76,12 +74,12 @@ public class ConditionalCommandTest extends AbstractCommandTest { assertCommandState(m_onFalse, 0, 0, 0, 0, 0); assertConditionalCommandState(m_command, 1, 5, 5, 1, 0); - assertTrue("Did not initialize the true command", m_onTrue.getInitializeCount() > 0); - assertTrue("Initialized the false command", m_onFalse.getInitializeCount() == 0); + assertTrue(m_onTrue.getInitializeCount() > 0, "Did not initialize the true command"); + assertTrue(m_onFalse.getInitializeCount() == 0, "Initialized the false command"); } @Test - public void testOnFalse() { + void onFalseTest() { m_command.setCondition(false); Scheduler.getInstance().add(m_command); @@ -114,12 +112,12 @@ public class ConditionalCommandTest extends AbstractCommandTest { assertCommandState(m_onTrue, 0, 0, 0, 0, 0); assertConditionalCommandState(m_command, 1, 5, 5, 1, 0); - assertTrue("Did not initialize the false command", m_onFalse.getInitializeCount() > 0); - assertTrue("Initialized the true command", m_onTrue.getInitializeCount() == 0); + assertTrue(m_onFalse.getInitializeCount() > 0, "Did not initialize the false command"); + assertTrue(m_onTrue.getInitializeCount() == 0, "Initialized the true command"); } @Test - public void testCancelSubCommand() { + void cancelSubCommandTest() { m_command.setCondition(true); Scheduler.getInstance().add(m_command); @@ -158,7 +156,7 @@ public class ConditionalCommandTest extends AbstractCommandTest { } @Test - public void testCancelRequires() { + void cancelRequiresTest() { m_command.setCondition(true); Scheduler.getInstance().add(m_command); @@ -193,7 +191,7 @@ public class ConditionalCommandTest extends AbstractCommandTest { } @Test - public void testCancelCondCommand() { + void cancelCondCommandTest() { m_command.setCondition(true); Scheduler.getInstance().add(m_command); @@ -228,7 +226,7 @@ public class ConditionalCommandTest extends AbstractCommandTest { } @Test - public void testOnTrueTwice() { + void onTrueTwiceTest() { m_command.setCondition(true); Scheduler.getInstance().add(m_command); @@ -296,7 +294,7 @@ public class ConditionalCommandTest extends AbstractCommandTest { } @Test - public void testOnTrueInstant() { + void onTrueInstantTest() { m_command.setCondition(true); m_onTrue.setHasFinished(true); @@ -327,7 +325,7 @@ public class ConditionalCommandTest extends AbstractCommandTest { } @Test - public void testOnFalseNull() { + void onFalseNullTest() { m_commandNull.setCondition(false); Scheduler.getInstance().add(m_commandNull); diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/DefaultCommandTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/DefaultCommandTest.java index 5c041db66c..1cfec7f074 100644 --- a/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/DefaultCommandTest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/DefaultCommandTest.java @@ -7,7 +7,7 @@ package edu.wpi.first.wpilibj.command; -import org.junit.Test; +import org.junit.jupiter.api.Test; /** * Tests the {@link Command} library. @@ -17,21 +17,13 @@ public class DefaultCommandTest extends AbstractCommandTest { * Testing of default commands where the interrupting command ends itself. */ @Test - public void testDefaultCommandWhereTheInteruptingCommandEndsItself() { + void defaultCommandWhereTheInteruptingCommandEndsItselfTest() { final ASubsystem subsystem = new ASubsystem(); - final MockCommand defaultCommand = new MockCommand() { - { - requires(subsystem); - } - }; + final MockCommand defaultCommand = new MockCommand(subsystem); - final MockCommand anotherCommand = new MockCommand() { - { - requires(subsystem); - } - }; + final MockCommand anotherCommand = new MockCommand(subsystem); assertCommandState(defaultCommand, 0, 0, 0, 0, 0); subsystem.init(defaultCommand); @@ -74,21 +66,11 @@ public class DefaultCommandTest extends AbstractCommandTest { * Testing of default commands where the interrupting command is canceled. */ @Test - public void testDefaultCommandsInterruptingCommandCanceled() { + void defaultCommandsInterruptingCommandCanceledTest() { final ASubsystem subsystem = new ASubsystem(); + final MockCommand defaultCommand = new MockCommand(subsystem); + final MockCommand anotherCommand = new MockCommand(subsystem); - - final MockCommand defaultCommand = new MockCommand() { - { - requires(subsystem); - } - }; - - final MockCommand anotherCommand = new MockCommand() { - { - requires(subsystem); - } - }; assertCommandState(defaultCommand, 0, 0, 0, 0, 0); subsystem.init(defaultCommand); subsystem.initDefaultCommand(); diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/MockCommand.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/MockCommand.java index 00503046c5..bee2f80999 100644 --- a/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/MockCommand.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/command/MockCommand.java @@ -24,6 +24,11 @@ public class MockCommand extends Command { requires(subsys); } + public MockCommand(Subsystem subsys, double timeout) { + this(subsys); + setTimeout(timeout); + } + public MockCommand() { super(); } diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/hal/JNITest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/hal/JNITest.java index 097a7080b5..f214a78727 100644 --- a/wpilibj/src/test/java/edu/wpi/first/wpilibj/hal/JNITest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/hal/JNITest.java @@ -7,19 +7,19 @@ package edu.wpi.first.wpilibj.hal; -import org.junit.Test; +import org.junit.jupiter.api.Test; import edu.wpi.first.networktables.NetworkTablesJNI; -public class JNITest { +class JNITest { @Test - public void jniNtcoreLinkTest() { + void jniNtcoreLinkTest() { // Test to verify that the JNI test link works correctly. NetworkTablesJNI.flush(NetworkTablesJNI.getDefaultInstance()); } @Test - public void jniHalLinkTest() { + void jniHalLinkTest() { HAL.initialize(500, 0); // Test to verify that the JNI test link works correctly. HALUtil.getHALRuntimeType(); diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/hal/MatchInfoDataTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/hal/MatchInfoDataTest.java index 4c06fb2629..04a1bd4c19 100644 --- a/wpilibj/src/test/java/edu/wpi/first/wpilibj/hal/MatchInfoDataTest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/hal/MatchInfoDataTest.java @@ -7,11 +7,12 @@ package edu.wpi.first.wpilibj.hal; -import org.junit.Test; +import org.junit.jupiter.api.Test; -public class MatchInfoDataTest { +class MatchInfoDataTest { @Test - public void matchInfoDataDoesNotThrow() { + @SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert") + void matchInfoDataDoesNotThrow() { HAL.initialize(500, 0); MatchInfoData data = new MatchInfoData(); HAL.getMatchInfo(data); diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/livewindow/LiveWindowTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/livewindow/LiveWindowTest.java index e7a90eaadd..da1757c529 100644 --- a/wpilibj/src/test/java/edu/wpi/first/wpilibj/livewindow/LiveWindowTest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/livewindow/LiveWindowTest.java @@ -9,8 +9,8 @@ package edu.wpi.first.wpilibj.livewindow; import edu.wpi.first.wpilibj.UtilityClassTest; -public class LiveWindowTest extends UtilityClassTest { - public LiveWindowTest() { +class LiveWindowTest extends UtilityClassTest { + LiveWindowTest() { super(LiveWindow.class); } } diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/sim/AnalogInputSimTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/sim/AnalogInputSimTest.java index 3292da8d67..7eb7c809a0 100644 --- a/wpilibj/src/test/java/edu/wpi/first/wpilibj/sim/AnalogInputSimTest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/sim/AnalogInputSimTest.java @@ -7,27 +7,25 @@ package edu.wpi.first.wpilibj.sim; -import org.junit.Test; +import org.junit.jupiter.api.Test; import edu.wpi.first.wpilibj.AnalogInput; import edu.wpi.first.wpilibj.hal.HAL; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; -public class AnalogInputSimTest { +class AnalogInputSimTest { static class DoubleStore { - public boolean m_wasTriggered = false; - public boolean m_wasCorrectType = false; - public double m_setValue = 0; + public boolean m_wasTriggered; + public boolean m_wasCorrectType; + public double m_setValue0; } @Test - public void testSetCallback() { + void setCallbackTest() { HAL.initialize(500, 0); - AnalogInput input = new AnalogInput(5); - AnalogInSim inputSim = input.getSimObject(); for (double i = 0; i < 5.0; i += 0.1) { diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/sim/AnalogOutputSimTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/sim/AnalogOutputSimTest.java index 3a537c9655..8d85ad93f6 100644 --- a/wpilibj/src/test/java/edu/wpi/first/wpilibj/sim/AnalogOutputSimTest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/sim/AnalogOutputSimTest.java @@ -7,19 +7,19 @@ package edu.wpi.first.wpilibj.sim; -import org.junit.Test; - import edu.wpi.first.wpilibj.AnalogOutput; import edu.wpi.first.wpilibj.hal.HAL; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; -public class AnalogOutputSimTest { +import org.junit.jupiter.api.Test; + +class AnalogOutputSimTest { static class DoubleStore { - public boolean m_wasTriggered = false; - public boolean m_wasCorrectType = false; + public boolean m_wasTriggered; + public boolean m_wasCorrectType; public double m_setValue = -1; public void reset() { @@ -30,7 +30,7 @@ public class AnalogOutputSimTest { } @Test - public void testSetCallback() { + void setCallbackTest() { HAL.initialize(500, 0); diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/smartdashboard/SmartDashboardTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/smartdashboard/SmartDashboardTest.java index 99d90ed848..00dd00221c 100644 --- a/wpilibj/src/test/java/edu/wpi/first/wpilibj/smartdashboard/SmartDashboardTest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/smartdashboard/SmartDashboardTest.java @@ -9,8 +9,8 @@ package edu.wpi.first.wpilibj.smartdashboard; import edu.wpi.first.wpilibj.UtilityClassTest; -public class SmartDashboardTest extends UtilityClassTest { - public SmartDashboardTest() { +class SmartDashboardTest extends UtilityClassTest { + SmartDashboardTest() { super(SmartDashboard.class); } } diff --git a/wpilibjIntegrationTests/build.gradle b/wpilibjIntegrationTests/build.gradle index 49ca7df124..f7910f9cc2 100644 --- a/wpilibjIntegrationTests/build.gradle +++ b/wpilibjIntegrationTests/build.gradle @@ -29,6 +29,7 @@ dependencies { compile project(':cscore') compile project(':cameraserver') compile 'junit:junit:4.11' + testCompile 'org.hamcrest:hamcrest-all:1.3' 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'