Use JUnit TempDir which automatically removes temp dirs (#3786)

This commit is contained in:
Wouter Born 2023-08-28 22:30:56 +02:00 committed by GitHub
parent a0eeed9292
commit f8b52ea3f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 15 deletions

View File

@ -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()))

View File

@ -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()));