| 1 | package nl.timfalken.chestpreview; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | import java.io.InputStream; |
| 5 | import java.nio.charset.StandardCharsets; |
| 6 | import java.nio.file.Files; |
| 7 | import java.nio.file.Path; |
| 8 | import java.util.Map; |
| 9 | import java.util.Set; |
| 10 | |
| 11 | import org.junit.jupiter.api.Test; |
| 12 | import org.yaml.snakeyaml.Yaml; |
| 13 | |
| 14 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 15 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 16 | import static org.junit.jupiter.api.Assertions.assertTrue; |
| 17 | |
| 18 | class PluginConfigTest { |
| 19 | |
| 20 | @Test |
| 21 | @SuppressWarnings("unchecked") |
| 22 | void pluginYmlDeclaresAdminCommand() throws IOException { |
| 23 | Map<String, Object> yaml = loadYaml("plugin.yml"); |
| 24 | assertEquals("ChestPreview", yaml.get("name")); |
| 25 | assertEquals("nl.timfalken.chestpreview.ChestPreviewPlugin", yaml.get("main")); |
| 26 | assertEquals("26.2", String.valueOf(yaml.get("api-version"))); |
| 27 | assertEquals(gradleVersion(), String.valueOf(yaml.get("version"))); |
| 28 | Map<String, Object> commands = (Map<String, Object>) yaml.get("commands"); |
| 29 | assertEquals(Set.of("chestpreview"), commands.keySet()); |
| 30 | assertEquals("chestpreview.admin", ((Map<String, Object>) commands.get("chestpreview")).get("permission")); |
| 31 | assertEquals("op", ((Map<String, Object>) ((Map<String, Object>) yaml.get("permissions")).get("chestpreview.admin")).get("default")); |
| 32 | } |
| 33 | |
| 34 | @Test |
| 35 | @SuppressWarnings("unchecked") |
| 36 | void configYmlEnablesCommonContainersAndDisplayDefaults() throws IOException { |
| 37 | Map<String, Object> yaml = loadYaml("config.yml"); |
| 38 | Map<String, Object> containers = (Map<String, Object>) yaml.get("containers"); |
| 39 | assertTrue((Boolean) containers.get("chest")); |
| 40 | assertTrue((Boolean) containers.get("barrel")); |
| 41 | Map<String, Object> display = (Map<String, Object>) yaml.get("display"); |
| 42 | assertEquals(0.40, ((Number) display.get("scale")).doubleValue(), 0.001); |
| 43 | Map<String, Object> cycling = (Map<String, Object>) yaml.get("cycling"); |
| 44 | assertTrue((Boolean) cycling.get("enabled")); |
| 45 | assertEquals(20, ((Number) cycling.get("interval-ticks")).intValue()); |
| 46 | } |
| 47 | |
| 48 | private static Map<String, Object> loadYaml(String name) throws IOException { |
| 49 | try (InputStream in = PluginConfigTest.class.getClassLoader().getResourceAsStream(name)) { |
| 50 | assertNotNull(in, name); |
| 51 | return new Yaml().load(in); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | private static String gradleVersion() throws IOException { |
| 56 | Path gradle = Path.of("build.gradle.kts"); |
| 57 | if (!Files.isRegularFile(gradle)) gradle = Path.of("..", "build.gradle.kts"); |
| 58 | for (String line : Files.readAllLines(gradle, StandardCharsets.UTF_8)) { |
| 59 | line = line.trim(); |
| 60 | if (line.startsWith("version = \"")) { |
| 61 | return line.substring("version = \"".length(), line.length() - 1); |
| 62 | } |
| 63 | } |
| 64 | throw new IOException("version not found"); |
| 65 | } |
| 66 | } |