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
The following commit(s) were added to refs/heads/master by this push: new 3798a6b [IO-632] Add PathUtils for operations on NIO Path. 3798a6b is described below commit 3798a6b504d3c8d46dcb50289e71741c7f029472 Author: Gary Gregory <gardgreg...@gmail.com> AuthorDate: Fri Oct 11 11:03:54 2019 -0400 [IO-632] Add PathUtils for operations on NIO Path. Refactor, add PathUtils.deleteDirectory(), add countDirectory(). --- ...leVisitor.java => CountingPathFileVisitor.java} | 11 ++--- ...leVisitor.java => DeletingPathFileVisitor.java} | 10 ++-- .../java/org/apache/commons/io/file/PathUtils.java | 47 ++++++++++++++++++- .../commons/io/file/SimplePathFileVisitor.java | 37 +++++++++++++++ .../commons/io/file/CountingFileVisitorTest.java | 25 +++++----- .../commons/io/file/DeletingFileVisitorTest.java | 28 +++++------ ...VisitorTest.java => PathUtilsCountingTest.java} | 41 ++++++++-------- ....java => PathUtilsDeletingFileVisitorTest.java} | 54 +++++----------------- ...athUtilsTest.java => PathUtilsIsEmptyTest.java} | 2 +- 9 files changed, 154 insertions(+), 101 deletions(-) diff --git a/src/main/java/org/apache/commons/io/file/CountingFileVisitor.java b/src/main/java/org/apache/commons/io/file/CountingPathFileVisitor.java similarity index 95% rename from src/main/java/org/apache/commons/io/file/CountingFileVisitor.java rename to src/main/java/org/apache/commons/io/file/CountingPathFileVisitor.java index 8c59fc2..b7e71d6 100644 --- a/src/main/java/org/apache/commons/io/file/CountingFileVisitor.java +++ b/src/main/java/org/apache/commons/io/file/CountingPathFileVisitor.java @@ -21,16 +21,15 @@ import java.io.IOException; import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; import java.util.concurrent.atomic.AtomicLong; /** * Counts files, directories, and sizes, as a visit proceeds. - * + * * @since 2.7 */ -public class CountingFileVisitor extends SimpleFileVisitor<Path> { +public class CountingPathFileVisitor extends SimplePathFileVisitor { private final AtomicLong byteCount = new AtomicLong(); private final AtomicLong directoryCount = new AtomicLong(); @@ -38,7 +37,7 @@ public class CountingFileVisitor extends SimpleFileVisitor<Path> { /** * Gets the byte count of visited files. - * + * * @return the byte count of visited files. */ public long getByteCount() { @@ -47,7 +46,7 @@ public class CountingFileVisitor extends SimpleFileVisitor<Path> { /** * Gets the count of visited directories. - * + * * @return the count of visited directories. */ public long getDirectoryCount() { @@ -56,7 +55,7 @@ public class CountingFileVisitor extends SimpleFileVisitor<Path> { /** * Gets the count of visited files. - * + * * @return the byte count of visited files. */ public long getFileCount() { diff --git a/src/main/java/org/apache/commons/io/file/DeletingFileVisitor.java b/src/main/java/org/apache/commons/io/file/DeletingPathFileVisitor.java similarity index 94% rename from src/main/java/org/apache/commons/io/file/DeletingFileVisitor.java rename to src/main/java/org/apache/commons/io/file/DeletingPathFileVisitor.java index 4624bcb..fac4c4b 100644 --- a/src/main/java/org/apache/commons/io/file/DeletingFileVisitor.java +++ b/src/main/java/org/apache/commons/io/file/DeletingPathFileVisitor.java @@ -26,20 +26,20 @@ import java.util.Arrays; /** * Deletes files and directories as a visit proceeds. - * + * * @since 2.7 */ -public class DeletingFileVisitor extends CountingFileVisitor { +public class DeletingPathFileVisitor extends CountingPathFileVisitor { private static final String[] EMPTY_STRING_ARRAY = new String[0]; private final String[] skip; /** * Constructs a new visitor that deletes files except for the files and directories explicitly given. - * + * * @param skip The files to skip deleting. */ - public DeletingFileVisitor(final String... skip) { + public DeletingPathFileVisitor(final String... skip) { final String[] temp = skip != null ? skip.clone() : EMPTY_STRING_ARRAY; Arrays.sort(temp); this.skip = temp; @@ -47,7 +47,7 @@ public class DeletingFileVisitor extends CountingFileVisitor { /** * Returns true to process the given path, false if not. - * + * * @param path the path to test. * @return true to process the given path, false if not. */ diff --git a/src/main/java/org/apache/commons/io/file/PathUtils.java b/src/main/java/org/apache/commons/io/file/PathUtils.java index dd11d56..547e86d 100644 --- a/src/main/java/org/apache/commons/io/file/PathUtils.java +++ b/src/main/java/org/apache/commons/io/file/PathUtils.java @@ -19,6 +19,7 @@ package org.apache.commons.io.file; import java.io.IOException; import java.nio.file.DirectoryStream; +import java.nio.file.FileVisitor; import java.nio.file.Files; import java.nio.file.Path; @@ -27,7 +28,29 @@ import java.nio.file.Path; * * @since 2.7 */ -public class PathUtils { +public final class PathUtils { + + /** + * Counts aspects of a directory including sub-directories. + * + * @param directory directory to delete. + * @return The visitor used to count the given directory. + * @throws IOException if an I/O error is thrown by a visitor method. + */ + public static CountingPathFileVisitor countDirectory(final Path directory) throws IOException { + return visitFileTree(directory, new CountingPathFileVisitor()); + } + + /** + * Deletes a directory including sub-directories. + * + * @param directory directory to delete. + * @return The visitor used to delete the given directory. + * @throws IOException if an I/O error is thrown by a visitor method. + */ + public static DeletingPathFileVisitor deleteDirectory(final Path directory) throws IOException { + return visitFileTree(directory, new DeletingPathFileVisitor()); + } /** * Returns whether the given file or directory is empty. @@ -67,4 +90,26 @@ public class PathUtils { return Files.size(file) <= 0; } + /** + * Performs {@link Files#walkFileTree(Path,FileVisitor)} and returns the given visitor. + * + * Note that {@link Files#walkFileTree(Path,FileVisitor)} returns the given path. + * + * @param directory See {@link Files#walkFileTree(Path,FileVisitor)}. + * @param visitor See {@link Files#walkFileTree(Path,FileVisitor)}. + * @param <T> See {@link Files#walkFileTree(Path,FileVisitor)}. + * @return the given visitor. + * + * @throws IOException if an I/O error is thrown by a visitor method + */ + public static <T extends FileVisitor<? super Path>> T visitFileTree(final Path directory, final T visitor) + throws IOException { + Files.walkFileTree(directory, visitor); + return visitor; + } + + private PathUtils() { + // do not instantiate. + } + } diff --git a/src/main/java/org/apache/commons/io/file/SimplePathFileVisitor.java b/src/main/java/org/apache/commons/io/file/SimplePathFileVisitor.java new file mode 100644 index 0000000..8cc95c0 --- /dev/null +++ b/src/main/java/org/apache/commons/io/file/SimplePathFileVisitor.java @@ -0,0 +1,37 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.io.file; + +import java.nio.file.Path; +import java.nio.file.SimpleFileVisitor; + +/** + * A {@link SimpleFileVisitor} typed to a {@link Path}. + * + * @since 2.7 + */ +public abstract class SimplePathFileVisitor extends SimpleFileVisitor<Path> { + + /** + * Constructs a new instance. + */ + protected SimplePathFileVisitor() { + super(); + } + +} diff --git a/src/test/java/org/apache/commons/io/file/CountingFileVisitorTest.java b/src/test/java/org/apache/commons/io/file/CountingFileVisitorTest.java index cd2c828..920bb6a 100644 --- a/src/test/java/org/apache/commons/io/file/CountingFileVisitorTest.java +++ b/src/test/java/org/apache/commons/io/file/CountingFileVisitorTest.java @@ -26,7 +26,7 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; /** - * Tests {@link CountingFileVisitor}. + * Tests {@link CountingPathFileVisitor}. */ public class CountingFileVisitorTest { @@ -34,10 +34,10 @@ public class CountingFileVisitorTest { * Tests an empty folder. */ @Test - public void testEmptyFolder() throws IOException { + public void testCountEmptyFolder() throws IOException { final Path tempDirectory = Files.createTempDirectory(getClass().getCanonicalName()); try { - final CountingFileVisitor visitor = new CountingFileVisitor(); + final CountingPathFileVisitor visitor = new CountingPathFileVisitor(); Files.walkFileTree(tempDirectory, visitor); Assertions.assertEquals(1, visitor.getDirectoryCount()); Assertions.assertEquals(0, visitor.getFileCount()); @@ -51,8 +51,8 @@ public class CountingFileVisitorTest { * Tests a directory with one file of size 0. */ @Test - public void testFolders1FileSize0() throws IOException { - final CountingFileVisitor visitor = new CountingFileVisitor(); + public void testCountFolders1FileSize0() throws IOException { + final CountingPathFileVisitor visitor = new CountingPathFileVisitor(); Files.walkFileTree(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-0"), visitor); Assertions.assertEquals(1, visitor.getDirectoryCount()); Assertions.assertEquals(1, visitor.getFileCount()); @@ -63,8 +63,8 @@ public class CountingFileVisitorTest { * Tests a directory with one file of size 1. */ @Test - public void testFolders1FileSize1() throws IOException { - final CountingFileVisitor visitor = new CountingFileVisitor(); + public void testCountFolders1FileSize1() throws IOException { + final CountingPathFileVisitor visitor = new CountingPathFileVisitor(); Files.walkFileTree(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1"), visitor); Assertions.assertEquals(1, visitor.getDirectoryCount()); Assertions.assertEquals(1, visitor.getFileCount()); @@ -75,16 +75,17 @@ public class CountingFileVisitorTest { * Tests a directory with two subdirectorys, each containing one file of size 1. */ @Test - public void testFolders2FileSize2() throws IOException { - final CountingFileVisitor visitor = new CountingFileVisitor(); + public void testCountFolders2FileSize2() throws IOException { + final CountingPathFileVisitor visitor = new CountingPathFileVisitor(); Files.walkFileTree(Paths.get("src/test/resources/org/apache/commons/io/dirs-2-file-size-2"), visitor); Assertions.assertEquals(3, visitor.getDirectoryCount()); Assertions.assertEquals(2, visitor.getFileCount()); Assertions.assertEquals(2, visitor.getByteCount()); } - - @Test void testToString() { + + @Test + void testToString() { // Make sure it does not blow up - new CountingFileVisitor().toString(); + new CountingPathFileVisitor().toString(); } } diff --git a/src/test/java/org/apache/commons/io/file/DeletingFileVisitorTest.java b/src/test/java/org/apache/commons/io/file/DeletingFileVisitorTest.java index f980664..2c5988e 100644 --- a/src/test/java/org/apache/commons/io/file/DeletingFileVisitorTest.java +++ b/src/test/java/org/apache/commons/io/file/DeletingFileVisitorTest.java @@ -29,7 +29,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; /** - * Tests {@link DeletingFileVisitor}. + * Tests {@link DeletingPathFileVisitor}. */ public class DeletingFileVisitorTest { @@ -52,13 +52,13 @@ public class DeletingFileVisitorTest { * Tests an empty folder. */ @Test - public void testEmptyDirectory() throws IOException { - testEmptyDirectory(new DeletingFileVisitor()); + public void testDeleteEmptyDirectory() throws IOException { + testDeleteEmptyDirectory(new DeletingPathFileVisitor()); // This will throw if not empty. Files.deleteIfExists(tempDirectory); } - private void testEmptyDirectory(final CountingFileVisitor visitor) throws IOException { + private void testDeleteEmptyDirectory(final DeletingPathFileVisitor visitor) throws IOException { Files.walkFileTree(tempDirectory, visitor); Assertions.assertEquals(1, visitor.getDirectoryCount()); Assertions.assertEquals(0, visitor.getFileCount()); @@ -69,8 +69,8 @@ public class DeletingFileVisitorTest { * Tests an empty folder. */ @Test - public void testEmptyDirectoryNullCtorArg() throws IOException { - testEmptyDirectory(new DeletingFileVisitor((String[]) null)); + public void testDeleteEmptyDirectoryNullCtorArg() throws IOException { + testDeleteEmptyDirectory(new DeletingPathFileVisitor((String[]) null)); // This will throw if not empty. Files.deleteIfExists(tempDirectory); } @@ -79,10 +79,10 @@ public class DeletingFileVisitorTest { * Tests a directory with one file of size 0. */ @Test - public void testFolders1FileSize0() throws IOException { + public void testDeleteFolders1FileSize0() throws IOException { FileUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-0").toFile(), tempDirectory.toFile()); - final CountingFileVisitor visitor = new DeletingFileVisitor(); + final CountingPathFileVisitor visitor = new DeletingPathFileVisitor(); Files.walkFileTree(tempDirectory, visitor); Assertions.assertEquals(1, visitor.getDirectoryCount()); Assertions.assertEquals(1, visitor.getFileCount()); @@ -95,10 +95,10 @@ public class DeletingFileVisitorTest { * Tests a directory with one file of size 1. */ @Test - public void testFolders1FileSize1() throws IOException { + public void testDeleteFolders1FileSize1() throws IOException { FileUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1").toFile(), tempDirectory.toFile()); - final CountingFileVisitor visitor = new DeletingFileVisitor(); + final CountingPathFileVisitor visitor = new DeletingPathFileVisitor(); Files.walkFileTree(tempDirectory, visitor); Assertions.assertEquals(1, visitor.getDirectoryCount()); Assertions.assertEquals(1, visitor.getFileCount()); @@ -111,11 +111,11 @@ public class DeletingFileVisitorTest { * Tests a directory with one file of size 1 but skip that file. */ @Test - public void testFolders1FileSize1Skip() throws IOException { + public void testDeleteFolders1FileSize1Skip() throws IOException { FileUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1").toFile(), tempDirectory.toFile()); final String skipFileName = "file-size-1.bin"; - final CountingFileVisitor visitor = new DeletingFileVisitor(skipFileName); + final CountingPathFileVisitor visitor = new DeletingPathFileVisitor(skipFileName); Files.walkFileTree(tempDirectory, visitor); Assertions.assertEquals(1, visitor.getDirectoryCount()); Assertions.assertEquals(1, visitor.getFileCount()); @@ -129,10 +129,10 @@ public class DeletingFileVisitorTest { * Tests a directory with two subdirectorys, each containing one file of size 1. */ @Test - public void testFolders2FileSize2() throws IOException { + public void testDeleteFolders2FileSize2() throws IOException { FileUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-2-file-size-2").toFile(), tempDirectory.toFile()); - final CountingFileVisitor visitor = new DeletingFileVisitor(); + final CountingPathFileVisitor visitor = new DeletingPathFileVisitor(); Files.walkFileTree(tempDirectory, visitor); Assertions.assertEquals(3, visitor.getDirectoryCount()); Assertions.assertEquals(2, visitor.getFileCount()); diff --git a/src/test/java/org/apache/commons/io/file/CountingFileVisitorTest.java b/src/test/java/org/apache/commons/io/file/PathUtilsCountingTest.java similarity index 63% copy from src/test/java/org/apache/commons/io/file/CountingFileVisitorTest.java copy to src/test/java/org/apache/commons/io/file/PathUtilsCountingTest.java index cd2c828..c2c279c 100644 --- a/src/test/java/org/apache/commons/io/file/CountingFileVisitorTest.java +++ b/src/test/java/org/apache/commons/io/file/PathUtilsCountingTest.java @@ -22,23 +22,31 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import org.apache.commons.io.file.PathUtils; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; /** - * Tests {@link CountingFileVisitor}. + * Tests {@link PathUtils}. */ -public class CountingFileVisitorTest { +public class PathUtilsCountingTest { + + private static final Path DIR_SIZE_1 = Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1"); + + private static final Path FILE_SIZE_0 = Paths + .get("src/test/resources/org/apache/commons/io/dirs-1-file-size-0/file-size-0.bin"); + + private static final Path FILE_SIZE_1 = Paths + .get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1/file-size-1.bin"); /** * Tests an empty folder. */ @Test - public void testEmptyFolder() throws IOException { + public void testCountEmptyFolder() throws IOException { final Path tempDirectory = Files.createTempDirectory(getClass().getCanonicalName()); try { - final CountingFileVisitor visitor = new CountingFileVisitor(); - Files.walkFileTree(tempDirectory, visitor); + final CountingPathFileVisitor visitor = PathUtils.countDirectory(tempDirectory); Assertions.assertEquals(1, visitor.getDirectoryCount()); Assertions.assertEquals(0, visitor.getFileCount()); Assertions.assertEquals(0, visitor.getByteCount()); @@ -51,9 +59,9 @@ public class CountingFileVisitorTest { * Tests a directory with one file of size 0. */ @Test - public void testFolders1FileSize0() throws IOException { - final CountingFileVisitor visitor = new CountingFileVisitor(); - Files.walkFileTree(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-0"), visitor); + public void testCountFolders1FileSize0() throws IOException { + final CountingPathFileVisitor visitor = PathUtils + .countDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-0")); Assertions.assertEquals(1, visitor.getDirectoryCount()); Assertions.assertEquals(1, visitor.getFileCount()); Assertions.assertEquals(0, visitor.getByteCount()); @@ -63,9 +71,9 @@ public class CountingFileVisitorTest { * Tests a directory with one file of size 1. */ @Test - public void testFolders1FileSize1() throws IOException { - final CountingFileVisitor visitor = new CountingFileVisitor(); - Files.walkFileTree(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1"), visitor); + public void testCountFolders1FileSize1() throws IOException { + final CountingPathFileVisitor visitor = PathUtils + .countDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1")); Assertions.assertEquals(1, visitor.getDirectoryCount()); Assertions.assertEquals(1, visitor.getFileCount()); Assertions.assertEquals(1, visitor.getByteCount()); @@ -75,16 +83,11 @@ public class CountingFileVisitorTest { * Tests a directory with two subdirectorys, each containing one file of size 1. */ @Test - public void testFolders2FileSize2() throws IOException { - final CountingFileVisitor visitor = new CountingFileVisitor(); - Files.walkFileTree(Paths.get("src/test/resources/org/apache/commons/io/dirs-2-file-size-2"), visitor); + public void testCountFolders2FileSize2() throws IOException { + final CountingPathFileVisitor visitor = PathUtils + .countDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-2-file-size-2")); Assertions.assertEquals(3, visitor.getDirectoryCount()); Assertions.assertEquals(2, visitor.getFileCount()); Assertions.assertEquals(2, visitor.getByteCount()); } - - @Test void testToString() { - // Make sure it does not blow up - new CountingFileVisitor().toString(); - } } diff --git a/src/test/java/org/apache/commons/io/file/DeletingFileVisitorTest.java b/src/test/java/org/apache/commons/io/file/PathUtilsDeletingFileVisitorTest.java similarity index 63% copy from src/test/java/org/apache/commons/io/file/DeletingFileVisitorTest.java copy to src/test/java/org/apache/commons/io/file/PathUtilsDeletingFileVisitorTest.java index f980664..bad8c6b 100644 --- a/src/test/java/org/apache/commons/io/file/DeletingFileVisitorTest.java +++ b/src/test/java/org/apache/commons/io/file/PathUtilsDeletingFileVisitorTest.java @@ -29,9 +29,9 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; /** - * Tests {@link DeletingFileVisitor}. + * Tests {@link DeletingPathFileVisitor}. */ -public class DeletingFileVisitorTest { +public class PathUtilsDeletingFileVisitorTest { private Path tempDirectory; @@ -52,38 +52,26 @@ public class DeletingFileVisitorTest { * Tests an empty folder. */ @Test - public void testEmptyDirectory() throws IOException { - testEmptyDirectory(new DeletingFileVisitor()); + public void testDeleteEmptyDirectory() throws IOException { + testDeleteEmptyDirectory(PathUtils.deleteDirectory(tempDirectory)); // This will throw if not empty. Files.deleteIfExists(tempDirectory); } - private void testEmptyDirectory(final CountingFileVisitor visitor) throws IOException { - Files.walkFileTree(tempDirectory, visitor); + private void testDeleteEmptyDirectory(final DeletingPathFileVisitor visitor) throws IOException { Assertions.assertEquals(1, visitor.getDirectoryCount()); Assertions.assertEquals(0, visitor.getFileCount()); Assertions.assertEquals(0, visitor.getByteCount()); } /** - * Tests an empty folder. - */ - @Test - public void testEmptyDirectoryNullCtorArg() throws IOException { - testEmptyDirectory(new DeletingFileVisitor((String[]) null)); - // This will throw if not empty. - Files.deleteIfExists(tempDirectory); - } - - /** * Tests a directory with one file of size 0. */ @Test - public void testFolders1FileSize0() throws IOException { + public void testDeleteFolders1FileSize0() throws IOException { FileUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-0").toFile(), tempDirectory.toFile()); - final CountingFileVisitor visitor = new DeletingFileVisitor(); - Files.walkFileTree(tempDirectory, visitor); + final CountingPathFileVisitor visitor = PathUtils.deleteDirectory(tempDirectory); Assertions.assertEquals(1, visitor.getDirectoryCount()); Assertions.assertEquals(1, visitor.getFileCount()); Assertions.assertEquals(0, visitor.getByteCount()); @@ -95,11 +83,10 @@ public class DeletingFileVisitorTest { * Tests a directory with one file of size 1. */ @Test - public void testFolders1FileSize1() throws IOException { + public void testDeleteFolders1FileSize1() throws IOException { FileUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1").toFile(), tempDirectory.toFile()); - final CountingFileVisitor visitor = new DeletingFileVisitor(); - Files.walkFileTree(tempDirectory, visitor); + final CountingPathFileVisitor visitor = PathUtils.deleteDirectory(tempDirectory); Assertions.assertEquals(1, visitor.getDirectoryCount()); Assertions.assertEquals(1, visitor.getFileCount()); Assertions.assertEquals(1, visitor.getByteCount()); @@ -108,32 +95,13 @@ public class DeletingFileVisitorTest { } /** - * Tests a directory with one file of size 1 but skip that file. - */ - @Test - public void testFolders1FileSize1Skip() throws IOException { - FileUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1").toFile(), - tempDirectory.toFile()); - final String skipFileName = "file-size-1.bin"; - final CountingFileVisitor visitor = new DeletingFileVisitor(skipFileName); - Files.walkFileTree(tempDirectory, visitor); - Assertions.assertEquals(1, visitor.getDirectoryCount()); - Assertions.assertEquals(1, visitor.getFileCount()); - Assertions.assertEquals(1, visitor.getByteCount()); - final Path skippedFile = tempDirectory.resolve(skipFileName); - Assertions.assertTrue(Files.exists(skippedFile)); - Files.delete(skippedFile); - } - - /** * Tests a directory with two subdirectorys, each containing one file of size 1. */ @Test - public void testFolders2FileSize2() throws IOException { + public void testDeleteFolders2FileSize2() throws IOException { FileUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-2-file-size-2").toFile(), tempDirectory.toFile()); - final CountingFileVisitor visitor = new DeletingFileVisitor(); - Files.walkFileTree(tempDirectory, visitor); + final CountingPathFileVisitor visitor = PathUtils.deleteDirectory(tempDirectory); Assertions.assertEquals(3, visitor.getDirectoryCount()); Assertions.assertEquals(2, visitor.getFileCount()); Assertions.assertEquals(2, visitor.getByteCount()); diff --git a/src/test/java/org/apache/commons/io/file/PathUtilsTest.java b/src/test/java/org/apache/commons/io/file/PathUtilsIsEmptyTest.java similarity index 98% rename from src/test/java/org/apache/commons/io/file/PathUtilsTest.java rename to src/test/java/org/apache/commons/io/file/PathUtilsIsEmptyTest.java index 41a2ac7..20fe56c 100644 --- a/src/test/java/org/apache/commons/io/file/PathUtilsTest.java +++ b/src/test/java/org/apache/commons/io/file/PathUtilsIsEmptyTest.java @@ -29,7 +29,7 @@ import org.junit.jupiter.api.Test; /** * Tests {@link PathUtils}. */ -public class PathUtilsTest { +public class PathUtilsIsEmptyTest { private static final Path DIR_SIZE_1 = Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1");