From f8b52ea3f6c96b319d30fbe3d2b8a545335c368a Mon Sep 17 00:00:00 2001 From: Wouter Born Date: Mon, 28 Aug 2023 22:30:56 +0200 Subject: [PATCH] Use JUnit TempDir which automatically removes temp dirs (#3786) --- .../common/osgi/ResourceBundleClassLoaderTest.java | 11 ++++++----- .../plugin/GenerateDefaultTranslationsMojoTest.java | 12 ++---------- 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/bundles/org.openhab.core/src/test/java/org/openhab/core/common/osgi/ResourceBundleClassLoaderTest.java b/bundles/org.openhab.core/src/test/java/org/openhab/core/common/osgi/ResourceBundleClassLoaderTest.java index 5c9a5a8e7..89ca58745 100644 --- a/bundles/org.openhab.core/src/test/java/org/openhab/core/common/osgi/ResourceBundleClassLoaderTest.java +++ b/bundles/org.openhab.core/src/test/java/org/openhab/core/common/osgi/ResourceBundleClassLoaderTest.java @@ -18,7 +18,6 @@ import static org.mockito.Mockito.*; import java.io.File; import java.net.URL; -import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Collections; @@ -26,6 +25,7 @@ import java.util.List; import org.eclipse.jdt.annotation.NonNullByDefault; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; import org.osgi.framework.Bundle; /** @@ -34,7 +34,9 @@ import org.osgi.framework.Bundle; @NonNullByDefault public class ResourceBundleClassLoaderTest { - static URL createTmpTestPropetiesFile(Path root, String relativeFile) throws Exception { + private @TempDir @NonNullByDefault({}) Path tempDir; + + static URL createTmpTestPropertiesFile(Path root, String relativeFile) throws Exception { Path filePath = Paths.get(relativeFile); Path dirPath = filePath.getParent(); @@ -47,9 +49,8 @@ public class ResourceBundleClassLoaderTest { @Test public void testName() throws Exception { - Path tmp = Files.createTempDirectory("tmp"); - URL hostPropertiesURL = createTmpTestPropetiesFile(tmp, "host/OH-INF/i18n/test.properties"); - URL fragmentPropertiesURL = createTmpTestPropetiesFile(tmp, "fragment/OH-INF/i18n/test.properties"); + URL hostPropertiesURL = createTmpTestPropertiesFile(tempDir, "host/OH-INF/i18n/test.properties"); + URL fragmentPropertiesURL = createTmpTestPropertiesFile(tempDir, "fragment/OH-INF/i18n/test.properties"); Bundle bundleMock = mock(Bundle.class); when(bundleMock.findEntries(any(), any(), anyBoolean())) diff --git a/tools/i18n-plugin/src/test/java/org/openhab/core/tools/i18n/plugin/GenerateDefaultTranslationsMojoTest.java b/tools/i18n-plugin/src/test/java/org/openhab/core/tools/i18n/plugin/GenerateDefaultTranslationsMojoTest.java index 2e5274de4..ac42058f9 100644 --- a/tools/i18n-plugin/src/test/java/org/openhab/core/tools/i18n/plugin/GenerateDefaultTranslationsMojoTest.java +++ b/tools/i18n-plugin/src/test/java/org/openhab/core/tools/i18n/plugin/GenerateDefaultTranslationsMojoTest.java @@ -17,22 +17,20 @@ import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.MatcherAssert.assertThat; import static org.openhab.core.tools.i18n.plugin.DefaultTranslationsGenerationMode.*; -import java.io.File; import java.io.IOException; import java.io.UncheckedIOException; import java.nio.file.DirectoryStream; import java.nio.file.Files; import java.nio.file.Path; -import java.util.Comparator; import java.util.stream.Collectors; import java.util.stream.Stream; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugin.logging.SystemStreamLog; import org.eclipse.jdt.annotation.NonNullByDefault; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; /** * Tests {@link GenerateDefaultTranslationsMojo}. @@ -42,7 +40,7 @@ import org.junit.jupiter.api.Test; @NonNullByDefault public class GenerateDefaultTranslationsMojoTest { - public @NonNullByDefault({}) Path tempPath; + public @TempDir @NonNullByDefault({}) Path tempPath; public @NonNullByDefault({}) Path tempI18nPath; public static final Path TTS_RESOURCES_PATH = Path.of("src/test/resources/acmetts.bundle"); @@ -95,7 +93,6 @@ public class GenerateDefaultTranslationsMojoTest { @BeforeEach public void before() throws IOException { - tempPath = Files.createTempDirectory("i18n-"); tempI18nPath = tempPath.resolve("OH-INF/i18n"); mojo = new GenerateDefaultTranslationsMojo(); @@ -104,11 +101,6 @@ public class GenerateDefaultTranslationsMojoTest { mojo.setTargetDirectory(tempI18nPath.toFile()); } - @AfterEach - public void afterEach() throws IOException { - Files.walk(tempPath).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete); - } - private void assertSameProperties(Path expectedPath, Path actualPath) throws IOException { String expected = Files.readAllLines(expectedPath).stream().collect(Collectors.joining(System.lineSeparator())); String actual = Files.readAllLines(actualPath).stream().collect(Collectors.joining(System.lineSeparator()));