| 1 | package me.fridtjof.minecarttrains; |
| 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.regex.Matcher; |
| 10 | import java.util.regex.Pattern; |
| 11 | |
| 12 | import org.junit.jupiter.api.Test; |
| 13 | import org.yaml.snakeyaml.Yaml; |
| 14 | |
| 15 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 16 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 17 | import static org.junit.jupiter.api.Assertions.assertTrue; |
| 18 | |
| 19 | class PluginConfigTest { |
| 20 | |
| 21 | @Test |
| 22 | void pluginYmlDeclaresPaper26AndMainClass() throws IOException { |
| 23 | Map<String, Object> yaml = loadYaml("plugin.yml"); |
| 24 | assertEquals("MinecartTrains", yaml.get("name")); |
| 25 | assertEquals("me.fridtjof.minecarttrains.MinecartTrains", yaml.get("main")); |
| 26 | assertEquals("26.2", String.valueOf(yaml.get("api-version"))); |
| 27 | String version = String.valueOf(yaml.get("version")); |
| 28 | assertTrue( |
| 29 | version.equals("${project.version}") || version.equals(pomVersion()), |
| 30 | "unexpected version: " + version); |
| 31 | } |
| 32 | |
| 33 | @Test |
| 34 | void pomVersionIsSemver() throws IOException { |
| 35 | assertTrue(pomVersion().matches("\\d+\\.\\d+\\.\\d+"), pomVersion()); |
| 36 | } |
| 37 | |
| 38 | private static Map<String, Object> loadYaml(String name) throws IOException { |
| 39 | try (InputStream in = PluginConfigTest.class.getClassLoader().getResourceAsStream(name)) { |
| 40 | assertNotNull(in, name); |
| 41 | return new Yaml().load(in); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | private static String pomVersion() throws IOException { |
| 46 | String pom = Files.readString(Path.of("pom.xml"), StandardCharsets.UTF_8); |
| 47 | Matcher m = Pattern.compile( |
| 48 | "<artifactId>minecarttrains</artifactId>\\s*<version>([^<]+)</version>", |
| 49 | Pattern.DOTALL).matcher(pom); |
| 50 | if (!m.find()) throw new IOException("project version in pom.xml"); |
| 51 | return m.group(1); |
| 52 | } |
| 53 | } |