mirror of
https://github.com/songoda/SongodaCore.git
synced 2025-01-25 00:31:22 +01:00
Improve test coverage + stability of configuration.yaml/songoda classes
This commit is contained in:
parent
45e1319f9c
commit
ef6c37b80c
@ -28,4 +28,22 @@ public class ReadOnlyConfigEntryTest {
|
|||||||
assertNull(entry.getDefaultValue());
|
assertNull(entry.getDefaultValue());
|
||||||
assertNull(entry.getUpgradeSteps());
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,6 @@ import org.junit.jupiter.api.AfterEach;
|
|||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
@ -46,7 +45,8 @@ class SongodaYamlConfigRoundtripTest {
|
|||||||
SongodaYamlConfig cfg = new SongodaYamlConfig(testFilePath.toFile())
|
SongodaYamlConfig cfg = new SongodaYamlConfig(testFilePath.toFile())
|
||||||
.withVersion(3);
|
.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.")
|
.withComment("This message is shown when the 'foo' command succeeds.")
|
||||||
.withUpgradeStep(1, "messages.fooSuccess");
|
.withUpgradeStep(1, "messages.fooSuccess");
|
||||||
ConfigEntry range = cfg.createEntry("range")
|
ConfigEntry range = cfg.createEntry("range")
|
||||||
@ -69,7 +69,8 @@ class SongodaYamlConfigRoundtripTest {
|
|||||||
return (int) o + 1;
|
return (int) o + 1;
|
||||||
})
|
})
|
||||||
.withUpgradeStep(3, null, (o) -> "text");
|
.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");
|
.withComment("This is the entry without an upgrade step");
|
||||||
|
|
||||||
assertTrue(cfg.init());
|
assertTrue(cfg.init());
|
||||||
|
@ -4,6 +4,7 @@ import com.songoda.core.configuration.ConfigEntry;
|
|||||||
import org.junit.jupiter.api.AfterEach;
|
import org.junit.jupiter.api.AfterEach;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -13,6 +14,8 @@ import java.nio.file.Files;
|
|||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
@ -34,7 +37,8 @@ class SongodaYamlConfigTest {
|
|||||||
try (Stream<Path> stream = Files.walk(this.tmpDir)) {
|
try (Stream<Path> stream = Files.walk(this.tmpDir)) {
|
||||||
stream
|
stream
|
||||||
.sorted(Comparator.reverseOrder())
|
.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
|
@Test
|
||||||
void testWithNegativeVersion() {
|
void testWithNegativeVersion() {
|
||||||
SongodaYamlConfig cfg = new SongodaYamlConfig(this.cfg.toFile());
|
SongodaYamlConfig cfg = new SongodaYamlConfig(this.cfg.toFile());
|
||||||
@ -213,4 +228,19 @@ class SongodaYamlConfigTest {
|
|||||||
entry.set("new-value");
|
entry.set("new-value");
|
||||||
assertEquals("new-value", readOnlyConfigEntry.get());
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package com.songoda.core.configuration.yaml;
|
|||||||
import com.songoda.core.compatibility.CompatibleMaterial;
|
import com.songoda.core.compatibility.CompatibleMaterial;
|
||||||
import com.songoda.core.configuration.ConfigEntry;
|
import com.songoda.core.configuration.ConfigEntry;
|
||||||
import com.songoda.core.configuration.songoda.SongodaYamlConfig;
|
import com.songoda.core.configuration.songoda.SongodaYamlConfig;
|
||||||
|
import com.songoda.ultimatestacker.core.configuration.Config;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.junit.jupiter.api.Test;
|
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;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
class YamlConfigEntryTest {
|
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
|
@Test
|
||||||
void testGetKey() {
|
void testGetKey() {
|
||||||
ConfigEntry entry = new YamlConfigEntry(new YamlConfiguration(), "key-1", null);
|
ConfigEntry entry = new YamlConfigEntry(new YamlConfiguration(), "key-1", null);
|
||||||
@ -42,6 +55,9 @@ class YamlConfigEntryTest {
|
|||||||
|
|
||||||
entry.setDefaultValue("new-value");
|
entry.setDefaultValue("new-value");
|
||||||
assertEquals("new-value", entry.getDefaultValue());
|
assertEquals("new-value", entry.getDefaultValue());
|
||||||
|
|
||||||
|
entry.withDefaultValue("new-value-2");
|
||||||
|
assertEquals("new-value-2", entry.getDefaultValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -235,5 +251,8 @@ class YamlConfigEntryTest {
|
|||||||
other = new YamlConfigEntry(cfg, "key2", "value2");
|
other = new YamlConfigEntry(cfg, "key2", "value2");
|
||||||
assertNotEquals(entry, other);
|
assertNotEquals(entry, other);
|
||||||
assertNotEquals(entry.hashCode(), other.hashCode());
|
assertNotEquals(entry.hashCode(), other.hashCode());
|
||||||
|
|
||||||
|
assertNotEquals(entry, null);
|
||||||
|
assertNotEquals(entry, "key");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user