Skip to content

Commit a86e546

Browse files
author
Gary Gregory
committed
Use JUnit TempDir instead of custom code.
1 parent 1d8900e commit a86e546

2 files changed

Lines changed: 4 additions & 25 deletions

File tree

src/test/java/org/apache/commons/io/file/CleaningPathVisitorTest.java

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,15 @@
1818
package org.apache.commons.io.file;
1919

2020
import static org.apache.commons.io.file.CounterAssertions.assertCounts;
21-
import static org.junit.jupiter.api.Assertions.assertTrue;
2221

2322
import java.io.IOException;
2423
import java.nio.file.Files;
2524
import java.nio.file.Path;
2625
import java.nio.file.Paths;
2726

2827
import org.apache.commons.io.file.Counters.PathCounters;
29-
import org.junit.jupiter.api.AfterEach;
3028
import org.junit.jupiter.api.Assertions;
31-
import org.junit.jupiter.api.BeforeEach;
29+
import org.junit.jupiter.api.io.TempDir;
3230
import org.junit.jupiter.params.ParameterizedTest;
3331
import org.junit.jupiter.params.provider.MethodSource;
3432

@@ -37,28 +35,14 @@
3735
*/
3836
public class CleaningPathVisitorTest extends TestArguments {
3937

38+
@TempDir
4039
private Path tempDir;
4140

42-
@AfterEach
43-
public void afterEach() throws IOException {
44-
// temp dir should still exist since we are cleaning and not deleting.
45-
assertTrue(Files.exists(tempDir));
46-
// backstop
47-
if (Files.exists(tempDir) && PathUtils.isEmptyDirectory(tempDir)) {
48-
Files.deleteIfExists(tempDir);
49-
}
50-
}
51-
5241
private void applyCleanEmptyDirectory(final CleaningPathVisitor visitor) throws IOException {
5342
Files.walkFileTree(tempDir, visitor);
5443
assertCounts(1, 0, 0, visitor);
5544
}
5645

57-
@BeforeEach
58-
public void beforeEach() throws IOException {
59-
tempDir = Files.createTempDirectory(getClass().getCanonicalName());
60-
}
61-
6246
/**
6347
* Tests an empty folder.
6448
*/

src/test/java/org/apache/commons/io/file/CountingPathVisitorTest.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
import static org.apache.commons.io.file.CounterAssertions.assertCounts;
2121

2222
import java.io.IOException;
23-
import java.nio.file.Files;
24-
import java.nio.file.Path;
2523

2624
import org.junit.jupiter.api.Assertions;
2725
import org.junit.jupiter.params.ParameterizedTest;
@@ -44,11 +42,8 @@ private void checkZeroCounts(final CountingPathVisitor visitor) {
4442
@MethodSource("countingPathVisitors")
4543
public void testCountEmptyFolder(final CountingPathVisitor visitor) throws IOException {
4644
checkZeroCounts(visitor);
47-
final Path tempDir = Files.createTempDirectory(getClass().getCanonicalName());
48-
try {
49-
assertCounts(1, 0, 0, PathUtils.visitFileTree(visitor, tempDir));
50-
} finally {
51-
Files.deleteIfExists(tempDir);
45+
try (final TempDirectory tempDir = TempDirectory.create(getClass().getCanonicalName())) {
46+
assertCounts(1, 0, 0, PathUtils.visitFileTree(visitor, tempDir.unwrap()));
5247
}
5348
}
5449

0 commit comments

Comments
 (0)