diff --git a/Core/src/test/java/com/songoda/core/configuration/songoda/SongodaYamlConfigRoundtripTest.java b/Core/src/test/java/com/songoda/core/configuration/songoda/SongodaYamlConfigRoundtripTest.java index 6914061b..1e2f6f10 100644 --- a/Core/src/test/java/com/songoda/core/configuration/songoda/SongodaYamlConfigRoundtripTest.java +++ b/Core/src/test/java/com/songoda/core/configuration/songoda/SongodaYamlConfigRoundtripTest.java @@ -9,36 +9,41 @@ import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; +import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.*; class SongodaYamlConfigRoundtripTest { - Path cfg; + private Path testDirectoryPath; @BeforeEach void setUp() throws IOException { - Path path = Files.createTempFile("SongodaYamlConfigTest", "yml"); - File file = path.toFile(); - file.deleteOnExit(); - - this.cfg = path; + this.testDirectoryPath = Files.createTempDirectory("SongodaCore-YamlConfigRoundtripTest"); + this.testDirectoryPath.toFile().deleteOnExit(); } @AfterEach void tearDown() throws IOException { - Files.deleteIfExists(this.cfg); + try (Stream paths = Files.list(this.testDirectoryPath)) { + for (Path path : paths.toArray(Path[]::new)) { + Files.deleteIfExists(path); + } + } + Files.deleteIfExists(this.testDirectoryPath); } @Test void roundtripTest() throws IOException { - Files.write(this.cfg, ("# Don't touch this – it's used to track the version of the config.\n" + + Path testFilePath = this.testDirectoryPath.resolve("config.yml"); + + Files.write(testFilePath, ("# Don't touch this – it's used to track the version of the config.\n" + "version: 1\n" + "messages:\n" + " # This message is shown when the 'foo' command succeeds.\n" + " fooSuccess: Remastered success value\n" + "# This is the range of the 'foo' command\n").getBytes()); - SongodaYamlConfig cfg = new SongodaYamlConfig(this.cfg.toFile()) + SongodaYamlConfig cfg = new SongodaYamlConfig(testFilePath.toFile()) .withVersion(3); ConfigEntry cmdFooSuccess = cfg.createEntry("command.foo.success", "Default success value") @@ -94,6 +99,6 @@ class SongodaYamlConfigRoundtripTest { "# This is the incrementer of the 'foo' command\n" + "incrementer: 0\n" + "# This is the entry without an upgrade step\n" + - "entryWithoutUpgradeStep: Default value\n", new String(Files.readAllBytes(this.cfg))); + "entryWithoutUpgradeStep: Default value\n", new String(Files.readAllBytes(testFilePath))); } } diff --git a/Core/src/test/java/com/songoda/core/locale/LocaleFileManagerTest.java b/Core/src/test/java/com/songoda/core/locale/LocaleFileManagerTest.java index b6966b65..422d956f 100644 --- a/Core/src/test/java/com/songoda/core/locale/LocaleFileManagerTest.java +++ b/Core/src/test/java/com/songoda/core/locale/LocaleFileManagerTest.java @@ -13,32 +13,35 @@ import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import java.nio.file.Path; import java.util.List; -import java.util.Objects; +import java.util.stream.Stream; class LocaleFileManagerTest { private final byte[] validIndexFile = ("# This is a comment\n\nen_US.lang\nen.yml\nde.txt\n").getBytes(StandardCharsets.UTF_8); - private File testDirectory; + private Path testDirectoryPath; @BeforeEach void setUp() throws IOException { - this.testDirectory = Files.createTempDirectory("SongodaCore-LocaleFileManagerTest").toFile(); - this.testDirectory.deleteOnExit(); + this.testDirectoryPath = Files.createTempDirectory("SongodaCore-LocaleFileManagerTest"); + this.testDirectoryPath.toFile().deleteOnExit(); } @AfterEach void tearDown() throws IOException { - for (File file : Objects.requireNonNull(this.testDirectory.listFiles())) { - Files.deleteIfExists(file.toPath()); + try (Stream paths = Files.list(this.testDirectoryPath)) { + for (Path path : paths.toArray(Path[]::new)) { + Files.deleteIfExists(path); + } } - Files.deleteIfExists(this.testDirectory.toPath()); + Files.deleteIfExists(this.testDirectoryPath); } @Test void downloadMissingTranslations_EmptyTargetDir() throws IOException { Plugin plugin = Mockito.mock(Plugin.class); - Mockito.when(plugin.getDataFolder()).thenReturn(this.testDirectory); + Mockito.when(plugin.getDataFolder()).thenReturn(this.testDirectoryPath.toFile()); MockHttpClient httpClient = new MockHttpClient(new MockHttpResponse(200, this.validIndexFile)); LocaleFileManager localeFileManager = new LocaleFileManager(httpClient, "test"); @@ -64,7 +67,7 @@ class LocaleFileManagerTest { @Test void downloadMissingTranslations() throws IOException { Plugin plugin = Mockito.mock(Plugin.class); - Mockito.when(plugin.getDataFolder()).thenReturn(this.testDirectory); + Mockito.when(plugin.getDataFolder()).thenReturn(this.testDirectoryPath.toFile()); Files.createDirectories(plugin.getDataFolder().toPath()); Files.createFile(new File(plugin.getDataFolder(), "en_US.lang").toPath());