Skip to content

Commit

Permalink
test(core): add test for ConfigHolder
Browse files Browse the repository at this point in the history
  • Loading branch information
Siroshun09 committed Apr 25, 2024
1 parent 28c53f1 commit 47ebb77
Showing 1 changed file with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package net.okocraft.servermonitor.core.config;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.function.UnaryOperator;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertLinesMatch;
import static org.junit.jupiter.api.Assertions.assertTrue;

class ConfigHolderTest {

@Test
void testLoading(@TempDir Path dir) throws IOException {
var filepath = dir.resolve("config.yml");
var defaultConfig = new Config(100, "test");
var holder = new ConfigHolder<>(defaultConfig, UnaryOperator.identity());

{ // first loading (saving default config entries)
holder.load(filepath);

assertTrue(Files.isRegularFile(filepath));
assertLinesMatch(defaultConfig.asYaml().lines(), Files.readString(filepath, StandardCharsets.UTF_8).lines());

assertEquals(defaultConfig, holder.get());
}

{ // second loading (file changed)
var modified = new Config(150, "TEST");
modifyAndLoadConfig(filepath, modified, holder, false);
}

{ // reloading (file changed)
var modified = new Config(500, "TEst");
modifyAndLoadConfig(filepath, modified, holder, true);
}
}

private static void modifyAndLoadConfig(Path filepath, Config modified, ConfigHolder<Config> holder, boolean reload) throws IOException {
Files.writeString(filepath, modified.asYaml(), StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);

if (reload) {
holder.reload(filepath);
} else {
holder.load(filepath);
}

assertEquals(modified, holder.get());
}

private record Config(int value, String string) {
public String asYaml() {
return "value: " + this.value + System.lineSeparator() +
"string: " + this.string + System.lineSeparator();
}
}
}

0 comments on commit 47ebb77

Please sign in to comment.