Improve test coverage + stability of configuration.yaml/songoda classes

This commit is contained in:
Christian Koop 2022-12-30 18:22:11 +01:00
parent 45e1319f9c
commit ef6c37b80c
No known key found for this signature in database
GPG Key ID: 89A8181384E010A3
4 changed files with 72 additions and 4 deletions

View File

@ -28,4 +28,22 @@ public class ReadOnlyConfigEntryTest {
assertNull(entry.getDefaultValue());
assertNull(entry.getUpgradeSteps());
}
@Test
void testWritingMethodsDoingNothing() {
YamlConfiguration config = new YamlConfiguration();
ConfigEntry entry = new ReadOnlyConfigEntry(config, "key");
assertThrows(UnsupportedOperationException.class, () -> entry.setDefaultValue("value"));
assertThrows(UnsupportedOperationException.class, () -> entry.withDefaultValue("value"));
assertThrows(UnsupportedOperationException.class, () -> entry.withComment("A comment."));
assertThrows(UnsupportedOperationException.class, () -> entry.withUpgradeStep(0, "old-key"));
assertThrows(UnsupportedOperationException.class, () -> entry.set("value"));
assertNull(entry.getDefaultValue());
assertNull(entry.getUpgradeSteps());
assertNull(config.getNodeComment("key"));
assertNull(entry.getUpgradeSteps());
assertNull(entry.get());
}
}

View File

@ -5,7 +5,6 @@ import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
@ -46,7 +45,8 @@ class SongodaYamlConfigRoundtripTest {
SongodaYamlConfig cfg = new SongodaYamlConfig(testFilePath.toFile())
.withVersion(3);
ConfigEntry cmdFooSuccess = cfg.createEntry("command.foo.success", "Default success value")
ConfigEntry cmdFooSuccess = cfg.createEntry("command.foo.success")
.withDefaultValue("Default success value")
.withComment("This message is shown when the 'foo' command succeeds.")
.withUpgradeStep(1, "messages.fooSuccess");
ConfigEntry range = cfg.createEntry("range")
@ -69,7 +69,8 @@ class SongodaYamlConfigRoundtripTest {
return (int) o + 1;
})
.withUpgradeStep(3, null, (o) -> "text");
ConfigEntry entryWithoutUpgradeStep = cfg.createEntry("entryWithoutUpgradeStep", "Default value")
ConfigEntry entryWithoutUpgradeStep = cfg.createEntry("entryWithoutUpgradeStep")
.withDefaultValue("Default value")
.withComment("This is the entry without an upgrade step");
assertTrue(cfg.init());

View File

@ -4,6 +4,7 @@ import com.songoda.core.configuration.ConfigEntry;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import java.io.File;
import java.io.IOException;
@ -13,6 +14,8 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Comparator;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.*;
@ -34,7 +37,8 @@ class SongodaYamlConfigTest {
try (Stream<Path> stream = Files.walk(this.tmpDir)) {
stream
.sorted(Comparator.reverseOrder())
.map(Path::toFile).forEach(File::delete);
.map(Path::toFile)
.forEach(File::delete);
}
}
@ -94,6 +98,17 @@ class SongodaYamlConfigTest {
);
}
@Test
void testWithZeroVersion() throws IOException {
SongodaYamlConfig cfg = new SongodaYamlConfig(this.cfg.toFile());
cfg.withVersion("version-key", 0, null);
assertEquals(0, cfg.get("version-key"));
cfg.save();
assertEquals("version-key: 0\n", new String(Files.readAllBytes(this.cfg)));
}
@Test
void testWithNegativeVersion() {
SongodaYamlConfig cfg = new SongodaYamlConfig(this.cfg.toFile());
@ -213,4 +228,19 @@ class SongodaYamlConfigTest {
entry.set("new-value");
assertEquals("new-value", readOnlyConfigEntry.get());
}
@Test
void testInit_Failure() {
assertTrue(this.cfg.toFile().setWritable(false));
Logger mockLogger = Mockito.mock(Logger.class);
SongodaYamlConfig cfg = new SongodaYamlConfig(this.cfg.toFile(), mockLogger);
cfg.createEntry("key", "default-value");
assertFalse(cfg.init());
Mockito.verify(mockLogger).log(Mockito.eq(Level.SEVERE), Mockito.anyString(), Mockito.any(IOException.class));
assertTrue(this.cfg.toFile().setWritable(true));
}
}

View File

@ -3,6 +3,7 @@ package com.songoda.core.configuration.yaml;
import com.songoda.core.compatibility.CompatibleMaterial;
import com.songoda.core.configuration.ConfigEntry;
import com.songoda.core.configuration.songoda.SongodaYamlConfig;
import com.songoda.ultimatestacker.core.configuration.Config;
import org.bukkit.Material;
import org.junit.jupiter.api.Test;
@ -20,6 +21,18 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
class YamlConfigEntryTest {
@Test
void testHas() {
YamlConfiguration config = new YamlConfiguration();
config.set("key-2", "value-2");
ConfigEntry entry1 = new YamlConfigEntry(config, "key-1", null);
ConfigEntry entry2 = new YamlConfigEntry(config, "key-2", null);
assertFalse(entry1.has());
assertTrue(entry2.has());
}
@Test
void testGetKey() {
ConfigEntry entry = new YamlConfigEntry(new YamlConfiguration(), "key-1", null);
@ -42,6 +55,9 @@ class YamlConfigEntryTest {
entry.setDefaultValue("new-value");
assertEquals("new-value", entry.getDefaultValue());
entry.withDefaultValue("new-value-2");
assertEquals("new-value-2", entry.getDefaultValue());
}
@Test
@ -235,5 +251,8 @@ class YamlConfigEntryTest {
other = new YamlConfigEntry(cfg, "key2", "value2");
assertNotEquals(entry, other);
assertNotEquals(entry.hashCode(), other.hashCode());
assertNotEquals(entry, null);
assertNotEquals(entry, "key");
}
}