Skip to content

Commit

Permalink
Remove the last remnants of JUnit4 (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
rcahoon authored Apr 19, 2024
1 parent 5aaaabc commit cbc9f0d
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 33 deletions.
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/com/team766/logging/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Category, Logger> m_loggers =
new EnumMap<Category, Logger>(Category.class);
Expand All @@ -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());
Expand All @@ -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();
Expand Down
29 changes: 19 additions & 10 deletions src/test/java/com/team766/TestCase.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
23 changes: 4 additions & 19 deletions src/test/java/com/team766/config/ConfigFileReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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}}}",
Expand All @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/com/team766/config/ConfigFileTestUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.team766.config;

public class ConfigFileTestUtils {
public static void resetStatics() {
AbstractConfigValue.resetStatics();
}
}
2 changes: 2 additions & 0 deletions src/test/java/com/team766/framework/ContextTest.java
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
2 changes: 2 additions & 0 deletions src/test/java/com/team766/framework/YieldWithValueTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit cbc9f0d

Please sign in to comment.