-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(core): add test for ConfigHolder
- Loading branch information
1 parent
28c53f1
commit 47ebb77
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
core/src/test/java/net/okocraft/servermonitor/core/config/ConfigHolderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |