Add unit test for SongodaYamlConfig persisting comments on key-upgrades

This commit is contained in:
Christian Koop 2022-11-16 02:29:41 +01:00
parent 02330b5ca7
commit d710b2d2d5
No known key found for this signature in database
GPG Key ID: 89A8181384E010A3
2 changed files with 31 additions and 0 deletions

View File

@ -183,6 +183,8 @@ public class SongodaYamlConfig extends YamlConfiguration {
upgradeOldConfigVersionByOne();
}
cleanValuesMap(this.values);
return true;
}

View File

@ -8,9 +8,11 @@ import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Comparator;
import java.util.Objects;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.*;
@ -161,6 +163,33 @@ class SongodaYamlConfigTest {
assertNull(entry.get());
}
@Test
void testVersionUpgradePersistsCommentsOnKeyChange() throws IOException {
SongodaYamlConfig cfg = new SongodaYamlConfig(this.cfg.toFile())
.withVersion(2);
cfg.createEntry("newKey", "value")
.withComment("This is a comment")
.withUpgradeStep(1, "oldKey");
cfg.load(new StringReader("version: 1\noldKey: old-value\n"));
assertNull(cfg.get("oldKey"));
assertNull(cfg.getNodeComment("oldKey"));
assertEquals("old-value", cfg.get("newKey"));
assertEquals("This is a comment", Objects.requireNonNull(cfg.getNodeComment("newKey")).get());
StringWriter writer = new StringWriter();
cfg.save(writer);
assertEquals("# Don't touch this it's used to track the version of the config.\n" +
"version: 2\n" +
"# This is a comment\n" +
"newKey: old-value\n",
writer.toString());
}
@Test
void testReadOnlyEntry() {
SongodaYamlConfig cfg = new SongodaYamlConfig(this.cfg.toFile());