From cbc9f0d5c9dfe4e76b78111defd2a118118f46cb Mon Sep 17 00:00:00 2001 From: Ryan Cahoon Date: Thu, 18 Apr 2024 21:50:10 -0700 Subject: [PATCH] Remove the last remnants of JUnit4 (#123) --- build.gradle | 1 - src/main/java/com/team766/logging/Logger.java | 9 ++++-- src/test/java/com/team766/TestCase.java | 29 ++++++++++++------- .../team766/config/ConfigFileReaderTest.java | 23 +++------------ .../team766/config/ConfigFileTestUtils.java | 7 +++++ .../com/team766/framework/ContextTest.java | 2 ++ .../team766/framework/YieldWithValueTest.java | 2 ++ 7 files changed, 40 insertions(+), 33 deletions(-) create mode 100644 src/test/java/com/team766/config/ConfigFileTestUtils.java diff --git a/build.gradle b/build.gradle index 1bb874e63..71faf41d4 100644 --- a/build.gradle +++ b/build.gradle @@ -115,7 +115,6 @@ dependencies { nativeRelease wpi.java.deps.wpilibJniRelease(wpi.platforms.desktop) nativeRelease wpi.java.vendor.jniRelease(wpi.platforms.desktop) simulationRelease wpi.sim.enableRelease() - testImplementation 'junit:junit:4.12' testImplementation 'org.junit.jupiter:junit-jupiter:5.10.1' testImplementation 'org.junit.jupiter:junit-jupiter-params:5.8.2' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' diff --git a/src/main/java/com/team766/logging/Logger.java b/src/main/java/com/team766/logging/Logger.java index 007c72359..46f43835d 100644 --- a/src/main/java/com/team766/logging/Logger.java +++ b/src/main/java/com/team766/logging/Logger.java @@ -36,6 +36,7 @@ public void uncaughtException(final Thread t, final Throwable e) { } private static final int MAX_NUM_RECENT_ENTRIES = 100; + private static final String LOG_FILE_PATH_KEY = "logFilePath"; private static EnumMap m_loggers = new EnumMap(Category.class); @@ -53,8 +54,8 @@ public void uncaughtException(final Thread t, final Throwable e) { } try { ConfigFileReader config_file = ConfigFileReader.getInstance(); - if (config_file != null) { - logFilePathBase = config_file.getString("logFilePath").get(); + if (config_file != null && config_file.containsKey(LOG_FILE_PATH_KEY)) { + logFilePathBase = config_file.getString(LOG_FILE_PATH_KEY).get(); new File(logFilePathBase).mkdirs(); final String timestamp = new SimpleDateFormat("yyyyMMdd'T'HHmmss").format(new Date()); @@ -65,7 +66,9 @@ public void uncaughtException(final Thread t, final Throwable e) { get(Category.CONFIGURATION) .logRaw( Severity.ERROR, - "Config file is not available. Logs will only be in-memory and will be lost when the robot is turned off."); + "Config file does not specify " + + LOG_FILE_PATH_KEY + + ". Logs will only be in-memory and will be lost when the robot is turned off."); } } catch (Exception e) { e.printStackTrace(); diff --git a/src/test/java/com/team766/TestCase.java b/src/test/java/com/team766/TestCase.java index 71dec67cb..535a31d27 100644 --- a/src/test/java/com/team766/TestCase.java +++ b/src/test/java/com/team766/TestCase.java @@ -1,22 +1,31 @@ package com.team766; import com.team766.config.ConfigFileReader; +import com.team766.config.ConfigFileTestUtils; import com.team766.framework.Scheduler; import com.team766.hal.RobotProvider; import com.team766.hal.mock.TestRobotProvider; +import java.io.IOException; +import java.nio.file.Files; +import org.junit.jupiter.api.BeforeEach; -public abstract class TestCase extends junit.framework.TestCase { +public abstract class TestCase { - @Override - protected void setUp() throws Exception { - ConfigFileReader.instance = new ConfigFileReader(this.getClass().getClassLoader().getResource("testConfig.txt").getPath()); - RobotProvider.instance = new TestRobotProvider(); + @BeforeEach + public void setUp() { + ConfigFileTestUtils.resetStatics(); + Scheduler.getInstance().reset(); - Scheduler.getInstance().reset(); - } + RobotProvider.instance = new TestRobotProvider(); + } - protected void step() { - Scheduler.getInstance().run(); - } + protected void loadConfig(String configJson) throws IOException { + var configFilePath = Files.createTempFile("testConfig", ".txt"); + Files.writeString(configFilePath, configJson); + ConfigFileReader.instance = new ConfigFileReader(configFilePath.toString()); + } + protected void step() { + Scheduler.getInstance().run(); + } } diff --git a/src/test/java/com/team766/config/ConfigFileReaderTest.java b/src/test/java/com/team766/config/ConfigFileReaderTest.java index 75d3973ee..da7bb1bc8 100644 --- a/src/test/java/com/team766/config/ConfigFileReaderTest.java +++ b/src/test/java/com/team766/config/ConfigFileReaderTest.java @@ -2,26 +2,15 @@ import static org.junit.jupiter.api.Assertions.*; -import java.io.File; -import java.io.FileWriter; +import com.team766.TestCase; import java.io.IOException; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -public class ConfigFileReaderTest { - @BeforeEach - public void setup() { - AbstractConfigValue.resetStatics(); - } - +public class ConfigFileReaderTest extends TestCase { @Test public void getJsonStringFromEmptyConfigFile() throws IOException { - File testConfigFile = File.createTempFile("config_file_test", ".json"); - try (FileWriter fos = new FileWriter(testConfigFile)) { - fos.append("{}"); - } + loadConfig("{}"); - ConfigFileReader.instance = new ConfigFileReader(testConfigFile.getPath()); ConfigFileReader.getInstance().getString("test.sub.key"); assertEquals( "{\"test\": {\"sub\": {\"key\": null}}}", @@ -30,12 +19,8 @@ public void getJsonStringFromEmptyConfigFile() throws IOException { @Test public void getJsonStringFromPartialConfigFile() throws IOException { - File testConfigFile = File.createTempFile("config_file_test", ".json"); - try (FileWriter fos = new FileWriter(testConfigFile)) { - fos.append("{\"test\": {\"sub\": {\"key\": \"pi\", \"value\": 3.14159}}}"); - } + loadConfig("{\"test\": {\"sub\": {\"key\": \"pi\", \"value\": 3.14159}}}"); - ConfigFileReader.instance = new ConfigFileReader(testConfigFile.getPath()); assertEquals("pi", ConfigFileReader.getInstance().getString("test.sub.key").get()); assertEquals( 3.14159, diff --git a/src/test/java/com/team766/config/ConfigFileTestUtils.java b/src/test/java/com/team766/config/ConfigFileTestUtils.java new file mode 100644 index 000000000..688768810 --- /dev/null +++ b/src/test/java/com/team766/config/ConfigFileTestUtils.java @@ -0,0 +1,7 @@ +package com.team766.config; + +public class ConfigFileTestUtils { + public static void resetStatics() { + AbstractConfigValue.resetStatics(); + } +} diff --git a/src/test/java/com/team766/framework/ContextTest.java b/src/test/java/com/team766/framework/ContextTest.java index f79ea26c4..147107f1d 100644 --- a/src/test/java/com/team766/framework/ContextTest.java +++ b/src/test/java/com/team766/framework/ContextTest.java @@ -1,5 +1,7 @@ package com.team766.framework; +import static org.junit.jupiter.api.Assertions.*; + import com.team766.TestCase; import org.junit.jupiter.api.Test; diff --git a/src/test/java/com/team766/framework/YieldWithValueTest.java b/src/test/java/com/team766/framework/YieldWithValueTest.java index d7a0a41fd..528649029 100644 --- a/src/test/java/com/team766/framework/YieldWithValueTest.java +++ b/src/test/java/com/team766/framework/YieldWithValueTest.java @@ -1,5 +1,7 @@ package com.team766.framework; +import static org.junit.jupiter.api.Assertions.*; + import com.team766.TestCase; import java.util.ArrayList; import java.util.List;