2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2019-07-16 01:02:52 -04:00
|
|
|
|
|
|
|
|
package edu.wpi.first.wpilibj;
|
|
|
|
|
|
2020-12-29 22:45:16 -08:00
|
|
|
import static org.junit.jupiter.api.Assertions.assertAll;
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.fail;
|
2022-10-08 10:01:31 -07:00
|
|
|
import static org.junit.jupiter.api.parallel.ExecutionMode.SAME_THREAD;
|
2020-12-29 22:45:16 -08:00
|
|
|
|
|
|
|
|
import edu.wpi.first.networktables.NetworkTable;
|
|
|
|
|
import edu.wpi.first.networktables.NetworkTableInstance;
|
2022-10-08 10:01:31 -07:00
|
|
|
import edu.wpi.first.networktables.Topic;
|
2019-07-16 01:02:52 -04:00
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.nio.file.Files;
|
|
|
|
|
import java.nio.file.Path;
|
2022-10-08 10:01:31 -07:00
|
|
|
import java.util.ArrayList;
|
2019-07-16 01:02:52 -04:00
|
|
|
import java.util.Arrays;
|
2022-10-08 10:01:31 -07:00
|
|
|
import java.util.List;
|
2019-07-16 01:02:52 -04:00
|
|
|
import java.util.stream.Stream;
|
|
|
|
|
import org.junit.jupiter.api.AfterEach;
|
|
|
|
|
import org.junit.jupiter.api.BeforeEach;
|
2023-08-29 17:46:50 -07:00
|
|
|
import org.junit.jupiter.api.Disabled;
|
2019-07-16 01:02:52 -04:00
|
|
|
import org.junit.jupiter.api.Nested;
|
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
import org.junit.jupiter.api.io.TempDir;
|
2022-10-08 10:01:31 -07:00
|
|
|
import org.junit.jupiter.api.parallel.Execution;
|
2019-07-16 01:02:52 -04:00
|
|
|
import org.junit.jupiter.params.ParameterizedTest;
|
|
|
|
|
import org.junit.jupiter.params.provider.MethodSource;
|
|
|
|
|
|
2022-10-08 10:01:31 -07:00
|
|
|
@Execution(SAME_THREAD)
|
2019-07-16 01:02:52 -04:00
|
|
|
class PreferencesTest {
|
2022-10-08 10:01:31 -07:00
|
|
|
private NetworkTableInstance m_inst;
|
|
|
|
|
private NetworkTable m_table;
|
2019-07-16 01:02:52 -04:00
|
|
|
|
2022-10-08 10:01:31 -07:00
|
|
|
private static final String kFilename = "networktables.json";
|
2019-07-16 01:02:52 -04:00
|
|
|
|
|
|
|
|
@BeforeEach
|
|
|
|
|
void setup(@TempDir Path tempDir) {
|
2022-10-08 10:01:31 -07:00
|
|
|
m_inst = NetworkTableInstance.create();
|
|
|
|
|
m_table = m_inst.getTable("Preferences");
|
|
|
|
|
Preferences.setNetworkTableInstance(m_inst);
|
2019-07-16 01:02:52 -04:00
|
|
|
|
|
|
|
|
Path filepath = tempDir.resolve(kFilename);
|
2022-10-08 10:01:31 -07:00
|
|
|
try (InputStream is = getClass().getResource("PreferencesTestDefault.json").openStream()) {
|
2019-07-16 01:02:52 -04:00
|
|
|
Files.copy(is, filepath);
|
|
|
|
|
} catch (IOException ex) {
|
|
|
|
|
fail(ex);
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-02 23:05:13 -08:00
|
|
|
m_inst.startServer(filepath.toString(), "", 0);
|
2022-10-08 10:01:31 -07:00
|
|
|
try {
|
|
|
|
|
int count = 0;
|
2022-11-04 20:25:37 -07:00
|
|
|
while (m_inst.getNetworkMode().contains(NetworkTableInstance.NetworkMode.kStarting)) {
|
2022-10-08 10:01:31 -07:00
|
|
|
Thread.sleep(100);
|
|
|
|
|
count++;
|
|
|
|
|
if (count > 30) {
|
|
|
|
|
throw new InterruptedException();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (InterruptedException ex) {
|
|
|
|
|
fail("interrupted while waiting for server to start");
|
|
|
|
|
}
|
2019-07-16 01:02:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@AfterEach
|
|
|
|
|
void cleanup() {
|
2022-10-08 10:01:31 -07:00
|
|
|
m_inst.close();
|
2019-07-16 01:02:52 -04:00
|
|
|
}
|
|
|
|
|
|
2023-08-29 17:46:50 -07:00
|
|
|
@Disabled("Fails often with 'Preferences was not empty!'")
|
2019-07-16 01:02:52 -04:00
|
|
|
@Test
|
|
|
|
|
void removeAllTest() {
|
2022-05-04 20:37:27 -07:00
|
|
|
Preferences.removeAll();
|
2019-07-16 01:02:52 -04:00
|
|
|
|
2022-10-08 10:01:31 -07:00
|
|
|
List<String> keys = new ArrayList<>();
|
|
|
|
|
boolean anyPersistent = false;
|
|
|
|
|
for (Topic topic : m_table.getTopics()) {
|
|
|
|
|
if (topic.isPersistent()) {
|
|
|
|
|
anyPersistent = true;
|
|
|
|
|
keys.add(topic.getName());
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-16 01:02:52 -04:00
|
|
|
|
2022-10-08 10:01:31 -07:00
|
|
|
assertFalse(
|
|
|
|
|
anyPersistent,
|
2020-12-29 22:45:16 -08:00
|
|
|
"Preferences was not empty! Preferences in table: " + Arrays.toString(keys.toArray()));
|
2019-07-16 01:02:52 -04:00
|
|
|
}
|
|
|
|
|
|
2025-06-16 20:22:11 -07:00
|
|
|
@Test
|
|
|
|
|
void getNetworkTableTest() {
|
|
|
|
|
NetworkTable networkTable = Preferences.getNetworkTable();
|
|
|
|
|
|
|
|
|
|
assertEquals(m_table, networkTable);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-16 01:02:52 -04:00
|
|
|
@ParameterizedTest
|
|
|
|
|
@MethodSource("defaultKeyProvider")
|
|
|
|
|
void defaultKeysTest(String key) {
|
2022-05-04 20:37:27 -07:00
|
|
|
assertTrue(Preferences.containsKey(key));
|
2019-07-16 01:02:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
|
@MethodSource("defaultKeyProvider")
|
|
|
|
|
void defaultKeysAllTest(String key) {
|
2022-05-04 20:37:27 -07:00
|
|
|
assertTrue(Preferences.getKeys().contains(key));
|
2019-07-16 01:02:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void defaultValueTest() {
|
|
|
|
|
assertAll(
|
2022-05-04 20:37:27 -07:00
|
|
|
() -> assertEquals(172L, Preferences.getLong("checkedValueLong", 0)),
|
|
|
|
|
() -> assertEquals(0.2, Preferences.getDouble("checkedValueDouble", 0), 1e-6),
|
|
|
|
|
() -> assertEquals("Hello. How are you?", Preferences.getString("checkedValueString", "")),
|
|
|
|
|
() -> assertEquals(2, Preferences.getInt("checkedValueInt", 0)),
|
|
|
|
|
() -> assertEquals(3.4, Preferences.getFloat("checkedValueFloat", 0), 1e-6),
|
|
|
|
|
() -> assertFalse(Preferences.getBoolean("checkedValueBoolean", true)));
|
2019-07-16 01:02:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Nested
|
|
|
|
|
class PutGetTests {
|
|
|
|
|
@Test
|
|
|
|
|
void intTest() {
|
|
|
|
|
final String key = "test";
|
|
|
|
|
final int value = 123;
|
|
|
|
|
|
2022-05-04 20:37:27 -07:00
|
|
|
Preferences.setInt(key, value);
|
2019-07-16 01:02:52 -04:00
|
|
|
|
|
|
|
|
assertAll(
|
2022-05-04 20:37:27 -07:00
|
|
|
() -> assertEquals(value, Preferences.getInt(key, -1)),
|
2020-12-29 22:45:16 -08:00
|
|
|
() -> assertEquals(value, m_table.getEntry(key).getNumber(-1).intValue()));
|
2019-07-16 01:02:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void longTest() {
|
|
|
|
|
final String key = "test";
|
|
|
|
|
final long value = 190L;
|
|
|
|
|
|
2022-05-04 20:37:27 -07:00
|
|
|
Preferences.setLong(key, value);
|
2019-07-16 01:02:52 -04:00
|
|
|
|
|
|
|
|
assertAll(
|
2022-05-04 20:37:27 -07:00
|
|
|
() -> assertEquals(value, Preferences.getLong(key, -1)),
|
2020-12-29 22:45:16 -08:00
|
|
|
() -> assertEquals(value, m_table.getEntry(key).getNumber(-1).longValue()));
|
2019-07-16 01:02:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void floatTest() {
|
|
|
|
|
final String key = "test";
|
|
|
|
|
final float value = 9.42f;
|
|
|
|
|
|
2022-05-04 20:37:27 -07:00
|
|
|
Preferences.setFloat(key, value);
|
2019-07-16 01:02:52 -04:00
|
|
|
|
|
|
|
|
assertAll(
|
2022-05-04 20:37:27 -07:00
|
|
|
() -> assertEquals(value, Preferences.getFloat(key, -1), 1e-6),
|
2020-12-29 22:45:16 -08:00
|
|
|
() -> assertEquals(value, m_table.getEntry(key).getNumber(-1).floatValue(), 1e-6));
|
2019-07-16 01:02:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void doubleTest() {
|
|
|
|
|
final String key = "test";
|
|
|
|
|
final double value = 6.28;
|
|
|
|
|
|
2022-05-04 20:37:27 -07:00
|
|
|
Preferences.setDouble(key, value);
|
2019-07-16 01:02:52 -04:00
|
|
|
|
|
|
|
|
assertAll(
|
2022-05-04 20:37:27 -07:00
|
|
|
() -> assertEquals(value, Preferences.getDouble(key, -1), 1e-6),
|
2020-12-29 22:45:16 -08:00
|
|
|
() -> assertEquals(value, m_table.getEntry(key).getNumber(-1).doubleValue(), 1e-6));
|
2019-07-16 01:02:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void stringTest() {
|
|
|
|
|
final String key = "test";
|
|
|
|
|
final String value = "value";
|
|
|
|
|
|
2022-05-04 20:37:27 -07:00
|
|
|
Preferences.setString(key, value);
|
2019-07-16 01:02:52 -04:00
|
|
|
|
|
|
|
|
assertAll(
|
2022-05-04 20:37:27 -07:00
|
|
|
() -> assertEquals(value, Preferences.getString(key, "")),
|
2020-12-29 22:45:16 -08:00
|
|
|
() -> assertEquals(value, m_table.getEntry(key).getString("")));
|
2019-07-16 01:02:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void booleanTest() {
|
|
|
|
|
final String key = "test";
|
|
|
|
|
final boolean value = true;
|
|
|
|
|
|
2022-05-04 20:37:27 -07:00
|
|
|
Preferences.setBoolean(key, value);
|
2019-07-16 01:02:52 -04:00
|
|
|
|
|
|
|
|
assertAll(
|
2022-05-04 20:37:27 -07:00
|
|
|
() -> assertEquals(value, Preferences.getBoolean(key, false)),
|
2020-12-29 22:45:16 -08:00
|
|
|
() -> assertEquals(value, m_table.getEntry(key).getBoolean(false)));
|
2019-07-16 01:02:52 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Stream<String> defaultKeyProvider() {
|
|
|
|
|
return Stream.of(
|
|
|
|
|
"checkedValueLong",
|
|
|
|
|
"checkedValueDouble",
|
|
|
|
|
"checkedValueString",
|
|
|
|
|
"checkedValueInt",
|
|
|
|
|
"checkedValueFloat",
|
2020-12-29 22:45:16 -08:00
|
|
|
"checkedValueBoolean");
|
2019-07-16 01:02:52 -04:00
|
|
|
}
|
|
|
|
|
}
|