Skip to content

Commit f8c9f5b

Browse files
committed
Don't use the Maven target folder as a temporary test fixture.
Use a Junit @tempdir instead
1 parent 01641e8 commit f8c9f5b

1 file changed

Lines changed: 27 additions & 17 deletions

File tree

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

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@
5858
import org.apache.commons.io.output.NullOutputStream;
5959
import org.apache.commons.lang3.ArrayUtils;
6060
import org.junit.jupiter.api.AfterEach;
61+
import org.junit.jupiter.api.BeforeAll;
6162
import org.junit.jupiter.api.BeforeEach;
6263
import org.junit.jupiter.api.Test;
64+
import org.junit.jupiter.api.io.TempDir;
6365

6466
/**
6567
* Tests {@link FilesUncheck}.
@@ -74,17 +76,25 @@ class FilesUncheckTest {
7476

7577
private static final Path FILE_PATH_EMPTY = Paths.get("src/test/resources/org/apache/commons/io/test-file-empty.bin");
7678

77-
private static final Path NEW_DIR_PATH = Paths.get("target/newdir");
79+
private static Path NEW_DIR_PATH;
7880

79-
private static final Path NEW_FILE_PATH = Paths.get("target/file.txt");
81+
private static Path NEW_FILE_PATH;
8082

81-
private static final Path NEW_FILE_PATH_LINK = Paths.get("target/to_another_file.txt");
83+
private static Path NEW_FILE_PATH_LINK;
8284

8385
private static final String PREFIX = "prefix";
8486

8587
private static final String SUFFIX = "suffix";
8688

87-
private static final Path TARGET_PATH = Paths.get("target");
89+
@TempDir
90+
static Path DEST_PATH;
91+
92+
@BeforeAll
93+
public static void beforeAll() throws IOException {
94+
NEW_DIR_PATH = DEST_PATH.resolve("newdir");
95+
NEW_FILE_PATH = DEST_PATH.resolve("file.txt");
96+
NEW_FILE_PATH_LINK = DEST_PATH.resolve("to_another_file.txt");
97+
}
8898

8999
@BeforeEach
90100
@AfterEach
@@ -111,7 +121,7 @@ void testCopyPathPathCopyOptionArray() {
111121

112122
@Test
113123
void testCreateDirectories() {
114-
assertEquals(TARGET_PATH, FilesUncheck.createDirectories(TARGET_PATH, EMPTY_FILE_ATTRIBUTES_ARRAY));
124+
assertEquals(DEST_PATH, FilesUncheck.createDirectories(DEST_PATH, EMPTY_FILE_ATTRIBUTES_ARRAY));
115125
}
116126

117127
@Test
@@ -137,7 +147,7 @@ void testCreateSymbolicLink() {
137147

138148
@Test
139149
void testCreateTempDirectoryPathStringFileAttributeOfQArray() {
140-
assertEquals(TARGET_PATH, FilesUncheck.createTempDirectory(TARGET_PATH, PREFIX, EMPTY_FILE_ATTRIBUTES_ARRAY).getParent());
150+
assertEquals(DEST_PATH, FilesUncheck.createTempDirectory(DEST_PATH, PREFIX, EMPTY_FILE_ATTRIBUTES_ARRAY).getParent());
141151
}
142152

143153
@Test
@@ -147,7 +157,7 @@ void testCreateTempDirectoryStringFileAttributeOfQArray() {
147157

148158
@Test
149159
void testCreateTempFilePathStringStringFileAttributeOfQArray() {
150-
assertEquals(TARGET_PATH, FilesUncheck.createTempFile(TARGET_PATH, PREFIX, SUFFIX, EMPTY_FILE_ATTRIBUTES_ARRAY).getParent());
160+
assertEquals(DEST_PATH, FilesUncheck.createTempFile(DEST_PATH, PREFIX, SUFFIX, EMPTY_FILE_ATTRIBUTES_ARRAY).getParent());
151161
}
152162

153163
@Test
@@ -300,26 +310,26 @@ void testNewByteChannelPathSetOfQextendsOpenOptionFileAttributeOfQArray() {
300310
@Test
301311
void testNewDirectoryStreamPath() {
302312
Uncheck.run(() -> {
303-
try (DirectoryStream<Path> directoryStream = FilesUncheck.newDirectoryStream(TARGET_PATH)) {
304-
directoryStream.forEach(e -> assertEquals(TARGET_PATH, e.getParent()));
313+
try (DirectoryStream<Path> directoryStream = FilesUncheck.newDirectoryStream(DEST_PATH)) {
314+
directoryStream.forEach(e -> assertEquals(DEST_PATH, e.getParent()));
305315
}
306316
});
307317
}
308318

309319
@Test
310320
void testNewDirectoryStreamPathFilterOfQsuperPath() {
311321
Uncheck.run(() -> {
312-
try (DirectoryStream<Path> directoryStream = FilesUncheck.newDirectoryStream(TARGET_PATH, e -> true)) {
313-
directoryStream.forEach(e -> assertEquals(TARGET_PATH, e.getParent()));
322+
try (DirectoryStream<Path> directoryStream = FilesUncheck.newDirectoryStream(DEST_PATH, e -> true)) {
323+
directoryStream.forEach(e -> assertEquals(DEST_PATH, e.getParent()));
314324
}
315325
});
316326
}
317327

318328
@Test
319329
void testNewDirectoryStreamPathString() {
320330
Uncheck.run(() -> {
321-
try (DirectoryStream<Path> directoryStream = FilesUncheck.newDirectoryStream(TARGET_PATH, "*.xml")) {
322-
directoryStream.forEach(e -> assertEquals(TARGET_PATH, e.getParent()));
331+
try (DirectoryStream<Path> directoryStream = FilesUncheck.newDirectoryStream(DEST_PATH, "*.xml")) {
332+
directoryStream.forEach(e -> assertEquals(DEST_PATH, e.getParent()));
323333
}
324334
});
325335
}
@@ -425,24 +435,24 @@ void testSize() {
425435

426436
@Test
427437
void testWalkFileTreePathFileVisitorOfQsuperPath() {
428-
assertEquals(TARGET_PATH, FilesUncheck.walkFileTree(TARGET_PATH, NoopPathVisitor.INSTANCE));
438+
assertEquals(DEST_PATH, FilesUncheck.walkFileTree(DEST_PATH, NoopPathVisitor.INSTANCE));
429439
}
430440

431441
@Test
432442
void testWalkFileTreePathSetOfFileVisitOptionIntFileVisitorOfQsuperPath() {
433-
assertEquals(TARGET_PATH, FilesUncheck.walkFileTree(TARGET_PATH, new HashSet<>(), 1, NoopPathVisitor.INSTANCE));
443+
assertEquals(DEST_PATH, FilesUncheck.walkFileTree(DEST_PATH, new HashSet<>(), 1, NoopPathVisitor.INSTANCE));
434444
}
435445

436446
@Test
437447
void testWalkPathFileVisitOptionArray() {
438-
try (Stream<Path> stream = FilesUncheck.walk(TARGET_PATH, FileVisitOption.FOLLOW_LINKS)) {
448+
try (Stream<Path> stream = FilesUncheck.walk(DEST_PATH, FileVisitOption.FOLLOW_LINKS)) {
439449
assertTrue(0 < stream.count());
440450
}
441451
}
442452

443453
@Test
444454
void testWalkPathIntFileVisitOptionArray() {
445-
try (Stream<Path> stream = FilesUncheck.walk(TARGET_PATH, 0, FileVisitOption.FOLLOW_LINKS)) {
455+
try (Stream<Path> stream = FilesUncheck.walk(DEST_PATH, 0, FileVisitOption.FOLLOW_LINKS)) {
446456
assertEquals(1, stream.count());
447457
}
448458
}

0 commit comments

Comments
 (0)