This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-io.git
commit e72d0107dfda56e9bd243886eb494ff7a1bbf40f Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Thu Apr 4 07:02:06 2024 -0400 Reuse AbstractTempDirTest --- .../commons/io/file/AbstractTempDirTest.java | 18 ++++++++ .../commons/io/file/DeletingPathVisitorTest.java | 48 +++++++++++----------- .../org/apache/commons/io/file/PathUtilsTest.java | 24 ++--------- 3 files changed, 44 insertions(+), 46 deletions(-) diff --git a/src/test/java/org/apache/commons/io/file/AbstractTempDirTest.java b/src/test/java/org/apache/commons/io/file/AbstractTempDirTest.java index b28598907..38c0a34ff 100644 --- a/src/test/java/org/apache/commons/io/file/AbstractTempDirTest.java +++ b/src/test/java/org/apache/commons/io/file/AbstractTempDirTest.java @@ -31,6 +31,24 @@ import org.junit.jupiter.api.io.TempDir; */ public abstract class AbstractTempDirTest { + /** + * Creates directory test fixtures. + * <ol> + * <li>tempDirPath/subdir</li> + * <li>tempDirPath/symlinked-dir -> tempDirPath/subdir</li> + * </ol> + * @param rootDir Root for directory entries. + * @return Path to tempDirPath/subdir. + * @throws IOException if an I/O error occurs or the parent directory does not exist. + */ + protected static Path createTempSymlinkedRelativeDir(final Path rootDir) throws IOException { + final Path targetDir = rootDir.resolve("subdir"); + final Path symlinkDir = rootDir.resolve("symlinked-dir"); + Files.createDirectory(targetDir); + Files.createSymbolicLink(symlinkDir, targetDir); + return symlinkDir; + } + /** * A temporary directory managed by JUnit. */ diff --git a/src/test/java/org/apache/commons/io/file/DeletingPathVisitorTest.java b/src/test/java/org/apache/commons/io/file/DeletingPathVisitorTest.java index 800c2723c..8570d4a85 100644 --- a/src/test/java/org/apache/commons/io/file/DeletingPathVisitorTest.java +++ b/src/test/java/org/apache/commons/io/file/DeletingPathVisitorTest.java @@ -26,20 +26,18 @@ import java.nio.file.Paths; import org.apache.commons.io.file.Counters.PathCounters; import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.io.TempDir; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; /** * Tests {@link DeletingPathVisitor}. */ -public class DeletingPathVisitorTest extends TestArguments { +public class DeletingPathVisitorTest extends AbstractTempDirTest { - @TempDir - private Path tempDir; + private static final String ARGS = "org.apache.commons.io.file.TestArguments#"; private void applyDeleteEmptyDirectory(final DeletingPathVisitor visitor) throws IOException { - Files.walkFileTree(tempDir, visitor); + Files.walkFileTree(tempDirPath, visitor); assertCounts(1, 0, 0, visitor); } @@ -47,59 +45,59 @@ public class DeletingPathVisitorTest extends TestArguments { * Tests an empty folder. */ @ParameterizedTest - @MethodSource("deletingPathVisitors") + @MethodSource(ARGS + "deletingPathVisitors") public void testDeleteEmptyDirectory(final DeletingPathVisitor visitor) throws IOException { applyDeleteEmptyDirectory(visitor); // This will throw if not empty. - Files.deleteIfExists(tempDir); + Files.deleteIfExists(tempDirPath); } /** * Tests an empty folder. */ @ParameterizedTest - @MethodSource("pathCounters") + @MethodSource(ARGS + "pathCounters") public void testDeleteEmptyDirectoryNullCtorArg(final PathCounters pathCounters) throws IOException { applyDeleteEmptyDirectory(new DeletingPathVisitor(pathCounters, (String[]) null)); // This will throw if not empty. - Files.deleteIfExists(tempDir); + Files.deleteIfExists(tempDirPath); } /** * Tests a directory with one file of size 0. */ @ParameterizedTest - @MethodSource("deletingPathVisitors") + @MethodSource(ARGS + "deletingPathVisitors") public void testDeleteFolders1FileSize0(final DeletingPathVisitor visitor) throws IOException { - PathUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-0"), tempDir); - assertCounts(1, 1, 0, PathUtils.visitFileTree(visitor, tempDir)); + PathUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-0"), tempDirPath); + assertCounts(1, 1, 0, PathUtils.visitFileTree(visitor, tempDirPath)); // This will throw if not empty. - Files.deleteIfExists(tempDir); + Files.deleteIfExists(tempDirPath); } /** * Tests a directory with one file of size 1. */ @ParameterizedTest - @MethodSource("deletingPathVisitors") + @MethodSource(ARGS + "deletingPathVisitors") public void testDeleteFolders1FileSize1(final DeletingPathVisitor visitor) throws IOException { - PathUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1"), tempDir); - assertCounts(1, 1, 1, PathUtils.visitFileTree(visitor, tempDir)); + PathUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1"), tempDirPath); + assertCounts(1, 1, 1, PathUtils.visitFileTree(visitor, tempDirPath)); // This will throw if not empty. - Files.deleteIfExists(tempDir); + Files.deleteIfExists(tempDirPath); } /** * Tests a directory with one file of size 1 but skip that file. */ @ParameterizedTest - @MethodSource("pathCounters") + @MethodSource(ARGS + "pathCounters") public void testDeleteFolders1FileSize1Skip(final PathCounters pathCounters) throws IOException { - PathUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1"), tempDir); + PathUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1"), tempDirPath); final String skipFileName = "file-size-1.bin"; final CountingPathVisitor visitor = new DeletingPathVisitor(pathCounters, skipFileName); - assertCounts(1, 1, 1, PathUtils.visitFileTree(visitor, tempDir)); - final Path skippedFile = tempDir.resolve(skipFileName); + assertCounts(1, 1, 1, PathUtils.visitFileTree(visitor, tempDirPath)); + final Path skippedFile = tempDirPath.resolve(skipFileName); Assertions.assertTrue(Files.exists(skippedFile)); Files.delete(skippedFile); } @@ -108,11 +106,11 @@ public class DeletingPathVisitorTest extends TestArguments { * Tests a directory with two subdirectories, each containing one file of size 1. */ @ParameterizedTest - @MethodSource("deletingPathVisitors") + @MethodSource(ARGS + "deletingPathVisitors") public void testDeleteFolders2FileSize2(final DeletingPathVisitor visitor) throws IOException { - PathUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-2-file-size-2"), tempDir); - assertCounts(3, 2, 2, PathUtils.visitFileTree(visitor, tempDir)); + PathUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-2-file-size-2"), tempDirPath); + assertCounts(3, 2, 2, PathUtils.visitFileTree(visitor, tempDirPath)); // This will throw if not empty. - Files.deleteIfExists(tempDir); + Files.deleteIfExists(tempDirPath); } } diff --git a/src/test/java/org/apache/commons/io/file/PathUtilsTest.java b/src/test/java/org/apache/commons/io/file/PathUtilsTest.java index 74c454009..a767a77f4 100644 --- a/src/test/java/org/apache/commons/io/file/PathUtilsTest.java +++ b/src/test/java/org/apache/commons/io/file/PathUtilsTest.java @@ -73,24 +73,6 @@ public class PathUtilsTest extends AbstractTempDirTest { private static final String PATH_FIXTURE = "NOTICE.txt"; - /** - * Creates directory test fixtures. - * <ol> - * <li>tempDirPath/subdir</li> - * <li>tempDirPath/symlinked-dir -> tempDirPath/subdir</li> - * </ol> - * - * @return Path to tempDirPath/subdir - * @throws IOException if an I/O error occurs or the parent directory does not exist. - */ - private Path createTempSymlinkedRelativeDir() throws IOException { - final Path targetDir = tempDirPath.resolve("subdir"); - final Path symlinkDir = tempDirPath.resolve("symlinked-dir"); - Files.createDirectory(targetDir); - Files.createSymbolicLink(symlinkDir, targetDir); - return symlinkDir; - } - private Path current() { return PathUtils.current(); } @@ -234,7 +216,7 @@ public class PathUtilsTest extends AbstractTempDirTest { @Test public void testCreateDirectoriesSymlink() throws IOException { - final Path symlinkedDir = createTempSymlinkedRelativeDir(); + final Path symlinkedDir = createTempSymlinkedRelativeDir(tempDirPath); final String leafDirName = "child"; final Path newDirFollowed = PathUtils.createParentDirectories(symlinkedDir.resolve(leafDirName), PathUtils.NULL_LINK_OPTION); assertEquals(Files.readSymbolicLink(symlinkedDir), newDirFollowed); @@ -242,7 +224,7 @@ public class PathUtilsTest extends AbstractTempDirTest { @Test public void testCreateDirectoriesSymlinkClashing() throws IOException { - final Path symlinkedDir = createTempSymlinkedRelativeDir(); + final Path symlinkedDir = createTempSymlinkedRelativeDir(tempDirPath); assertEquals(symlinkedDir, PathUtils.createParentDirectories(symlinkedDir.resolve("child"))); } @@ -428,7 +410,7 @@ public class PathUtilsTest extends AbstractTempDirTest { @Test public void testNewOutputStreamNewFileInsideExistingSymlinkedDir() throws IOException { - final Path symlinkDir = createTempSymlinkedRelativeDir(); + final Path symlinkDir = createTempSymlinkedRelativeDir(tempDirPath); final Path file = symlinkDir.resolve("test.txt"); try (OutputStream outputStream = PathUtils.newOutputStream(file, new LinkOption[] {})) { // empty