| 1 | package nl.timfalken.copperkeys; |
| 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 | |
| 17 | class PluginConfigTest { |
| 18 | |
| 19 | @Test |
| 20 | @SuppressWarnings("unchecked") |
| 21 | void pluginYmlDeclaresKeynameCommand() throws IOException { |
| 22 | Map<String, Object> yaml = loadYaml("plugin.yml"); |
| 23 | assertEquals("CopperKeys", yaml.get("name")); |
| 24 | assertEquals("nl.timfalken.copperkeys.CopperKeysPlugin", yaml.get("main")); |
| 25 | assertEquals("26.2", String.valueOf(yaml.get("api-version"))); |
| 26 | assertEquals(gradleVersion(), String.valueOf(yaml.get("version"))); |
| 27 | Map<String, Object> commands = (Map<String, Object>) yaml.get("commands"); |
| 28 | assertEquals(Set.of("keyname"), commands.keySet()); |
| 29 | assertEquals("copperkeys.keyname", ((Map<String, Object>) commands.get("keyname")).get("permission")); |
| 30 | assertEquals(true, ((Map<String, Object>) ((Map<String, Object>) yaml.get("permissions")).get("copperkeys.keyname")).get("default")); |
| 31 | } |
| 32 | |
| 33 | private static Map<String, Object> loadYaml(String name) throws IOException { |
| 34 | try (InputStream in = PluginConfigTest.class.getClassLoader().getResourceAsStream(name)) { |
| 35 | assertNotNull(in, name); |
| 36 | return new Yaml().load(in); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | private static String gradleVersion() throws IOException { |
| 41 | Path gradle = Path.of("build.gradle.kts"); |
| 42 | if (!Files.isRegularFile(gradle)) gradle = Path.of("..", "build.gradle.kts"); |
| 43 | for (String line : Files.readAllLines(gradle, StandardCharsets.UTF_8)) { |
| 44 | line = line.trim(); |
| 45 | if (line.startsWith("version = \"")) { |
| 46 | return line.substring("version = \"".length(), line.length() - 1); |
| 47 | } |
| 48 | } |
| 49 | throw new IOException("version not found"); |
| 50 | } |
| 51 | } |