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 4373928 [IO-632] Add PathUtils for operations on NIO Path. 4373928 is described below commit 437392898ad91b3234da87d9225abadb4ca0ebad Author: Gary Gregory <gardgreg...@gmail.com> AuthorDate: Fri Oct 11 13:21:09 2019 -0400 [IO-632] Add PathUtils for operations on NIO Path. Fix test class name and all delete(Path). 100% JaCoCo coverage for the whole package. --- .../commons/io/file/CountingPathFileVisitor.java | 29 +++++---- ...ountingPathFileVisitor.java => PathCounts.java} | 30 ++-------- .../java/org/apache/commons/io/file/PathUtils.java | 56 ++++++++++++++++-- .../org/apache/commons/io/file/PathCountsTest.java | 38 ++++++++++++ .../commons/io/file/PathUtilsCountingTest.java | 26 ++++----- ...Test.java => PathUtilsDeleteDirectoryTest.java} | 64 ++++++++++---------- ...sitorTest.java => PathUtilsDeleteFileTest.java} | 67 ++++++++++----------- ...leVisitorTest.java => PathUtilsDeleteTest.java} | 68 +++++++++++----------- 8 files changed, 225 insertions(+), 153 deletions(-) diff --git a/src/main/java/org/apache/commons/io/file/CountingPathFileVisitor.java b/src/main/java/org/apache/commons/io/file/CountingPathFileVisitor.java index b7e71d6..6c5085e 100644 --- a/src/main/java/org/apache/commons/io/file/CountingPathFileVisitor.java +++ b/src/main/java/org/apache/commons/io/file/CountingPathFileVisitor.java @@ -22,7 +22,6 @@ import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.attribute.BasicFileAttributes; -import java.util.concurrent.atomic.AtomicLong; /** * Counts files, directories, and sizes, as a visit proceeds. @@ -31,9 +30,7 @@ import java.util.concurrent.atomic.AtomicLong; */ public class CountingPathFileVisitor extends SimplePathFileVisitor { - private final AtomicLong byteCount = new AtomicLong(); - private final AtomicLong directoryCount = new AtomicLong(); - private final AtomicLong fileCount = new AtomicLong(); + private final PathCounts pathCounts = new PathCounts(); /** * Gets the byte count of visited files. @@ -41,7 +38,7 @@ public class CountingPathFileVisitor extends SimplePathFileVisitor { * @return the byte count of visited files. */ public long getByteCount() { - return this.byteCount.get(); + return this.pathCounts.byteCount.get(); } /** @@ -50,7 +47,7 @@ public class CountingPathFileVisitor extends SimplePathFileVisitor { * @return the count of visited directories. */ public long getDirectoryCount() { - return this.directoryCount.get(); + return this.pathCounts.directoryCount.get(); } /** @@ -59,26 +56,34 @@ public class CountingPathFileVisitor extends SimplePathFileVisitor { * @return the byte count of visited files. */ public long getFileCount() { - return this.fileCount.get(); + return this.pathCounts.fileCount.get(); + } + + /** + * Gets the visitation counts. + * + * @return the visitation counts. + */ + public PathCounts getPathCounts() { + return pathCounts; } @Override public FileVisitResult postVisitDirectory(final Path dir, final IOException exc) throws IOException { - directoryCount.incrementAndGet(); + pathCounts.directoryCount.incrementAndGet(); return FileVisitResult.CONTINUE; } @Override public String toString() { - return String.format("%,d files in %,d directories for %,d bytes", Long.valueOf(fileCount.longValue()), - Long.valueOf(directoryCount.longValue()), Long.valueOf(byteCount.longValue())); + return pathCounts.toString(); } @Override public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException { if (Files.exists(file)) { - fileCount.incrementAndGet(); - byteCount.addAndGet(attrs.size()); + pathCounts.fileCount.incrementAndGet(); + pathCounts.byteCount.addAndGet(attrs.size()); } return FileVisitResult.CONTINUE; } diff --git a/src/main/java/org/apache/commons/io/file/CountingPathFileVisitor.java b/src/main/java/org/apache/commons/io/file/PathCounts.java similarity index 62% copy from src/main/java/org/apache/commons/io/file/CountingPathFileVisitor.java copy to src/main/java/org/apache/commons/io/file/PathCounts.java index b7e71d6..1b2c7b3 100644 --- a/src/main/java/org/apache/commons/io/file/CountingPathFileVisitor.java +++ b/src/main/java/org/apache/commons/io/file/PathCounts.java @@ -17,11 +17,6 @@ package org.apache.commons.io.file; -import java.io.IOException; -import java.nio.file.FileVisitResult; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.attribute.BasicFileAttributes; import java.util.concurrent.atomic.AtomicLong; /** @@ -29,11 +24,11 @@ import java.util.concurrent.atomic.AtomicLong; * * @since 2.7 */ -public class CountingPathFileVisitor extends SimplePathFileVisitor { +public class PathCounts { - private final AtomicLong byteCount = new AtomicLong(); - private final AtomicLong directoryCount = new AtomicLong(); - private final AtomicLong fileCount = new AtomicLong(); + final AtomicLong byteCount = new AtomicLong(); + final AtomicLong directoryCount = new AtomicLong(); + final AtomicLong fileCount = new AtomicLong(); /** * Gets the byte count of visited files. @@ -63,24 +58,9 @@ public class CountingPathFileVisitor extends SimplePathFileVisitor { } @Override - public FileVisitResult postVisitDirectory(final Path dir, final IOException exc) throws IOException { - directoryCount.incrementAndGet(); - return FileVisitResult.CONTINUE; - } - - @Override public String toString() { - return String.format("%,d files in %,d directories for %,d bytes", Long.valueOf(fileCount.longValue()), + return String.format("%,d files, %,d directories, %,d bytes", Long.valueOf(fileCount.longValue()), Long.valueOf(directoryCount.longValue()), Long.valueOf(byteCount.longValue())); } - @Override - public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException { - if (Files.exists(file)) { - fileCount.incrementAndGet(); - byteCount.addAndGet(attrs.size()); - } - return FileVisitResult.CONTINUE; - } - } 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 547e86d..949a7a3 100644 --- a/src/main/java/org/apache/commons/io/file/PathUtils.java +++ b/src/main/java/org/apache/commons/io/file/PathUtils.java @@ -21,6 +21,7 @@ import java.io.IOException; import java.nio.file.DirectoryStream; import java.nio.file.FileVisitor; import java.nio.file.Files; +import java.nio.file.NotDirectoryException; import java.nio.file.Path; /** @@ -37,10 +38,52 @@ public final class PathUtils { * @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()); + public static PathCounts countDirectory(final Path directory) throws IOException { + return visitFileTree(directory, new CountingPathFileVisitor()).getPathCounts(); } - + + /** + * Deletes a file or directory. If the path is a directory, delete it and all sub-directories. + * <p> + * The difference between File.delete() and this method are: + * </p> + * <ul> + * <li>A directory to delete does not have to be empty.</li> + * <li>You get exceptions when a file or directory cannot be deleted; {@link java.io.File#delete()} returns a + * boolean. + * </ul> + * + * @param path file or directory to delete, must not be {@code null} + * @return The visitor used to delete the given directory. + * @throws NullPointerException if the directory is {@code null} + * @throws IOException if an I/O error is thrown by a visitor method or if an I/O error occurs. + */ + public static PathCounts delete(final Path path) throws IOException { + return Files.isDirectory(path) ? deleteDirectory(path) : deleteFile(path); + } + + /** + * Deletes the given file. + * + * @param file The file to delete. + * @return A visitor with path counts set to 1 file, 0 directories, and the size of the deleted file. + * @throws IOException if an I/O error occurs. + * @throws NotDirectoryException if the file is a directory. + */ + public static PathCounts deleteFile(final Path file) throws IOException { + if (Files.isDirectory(file)) { + throw new NotDirectoryException(file.toString()); + } + final PathCounts pathCounts = new PathCounts(); + final long size = Files.exists(file) ? Files.size(file) : 0; + if (Files.deleteIfExists(file)) { + pathCounts.fileCount.set(1); + pathCounts.directoryCount.set(0); + pathCounts.byteCount.set(size); + } + return pathCounts; + } + /** * Deletes a directory including sub-directories. * @@ -48,8 +91,8 @@ public final class PathUtils { * @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()); + public static PathCounts deleteDirectory(final Path directory) throws IOException { + return visitFileTree(directory, new DeletingPathFileVisitor()).getPathCounts(); } /** @@ -108,6 +151,9 @@ public final class PathUtils { return visitor; } + /** + * Does allow to instantiate. + */ private PathUtils() { // do not instantiate. } diff --git a/src/test/java/org/apache/commons/io/file/PathCountsTest.java b/src/test/java/org/apache/commons/io/file/PathCountsTest.java new file mode 100644 index 0000000..40292c6 --- /dev/null +++ b/src/test/java/org/apache/commons/io/file/PathCountsTest.java @@ -0,0 +1,38 @@ +/* + * 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 org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class PathCountsTest { + + @Test + public void testCtor() { + final PathCounts pathCounts = new PathCounts(); + Assertions.assertEquals(pathCounts.getByteCount(), 0); + Assertions.assertEquals(pathCounts.getDirectoryCount(), 0); + Assertions.assertEquals(pathCounts.getFileCount(), 0); + } + + @Test + public void testToString() { + // Does not blow up + new PathCounts().toString(); + } +} diff --git a/src/test/java/org/apache/commons/io/file/PathUtilsCountingTest.java b/src/test/java/org/apache/commons/io/file/PathUtilsCountingTest.java index c2c279c..57fe890 100644 --- a/src/test/java/org/apache/commons/io/file/PathUtilsCountingTest.java +++ b/src/test/java/org/apache/commons/io/file/PathUtilsCountingTest.java @@ -46,10 +46,10 @@ public class PathUtilsCountingTest { public void testCountEmptyFolder() throws IOException { final Path tempDirectory = Files.createTempDirectory(getClass().getCanonicalName()); try { - final CountingPathFileVisitor visitor = PathUtils.countDirectory(tempDirectory); - Assertions.assertEquals(1, visitor.getDirectoryCount()); - Assertions.assertEquals(0, visitor.getFileCount()); - Assertions.assertEquals(0, visitor.getByteCount()); + final PathCounts pathCounts = PathUtils.countDirectory(tempDirectory); + Assertions.assertEquals(1, pathCounts.getDirectoryCount()); + Assertions.assertEquals(0, pathCounts.getFileCount()); + Assertions.assertEquals(0, pathCounts.getByteCount()); } finally { Files.deleteIfExists(tempDirectory); } @@ -60,11 +60,11 @@ public class PathUtilsCountingTest { */ @Test public void testCountFolders1FileSize0() throws IOException { - final CountingPathFileVisitor visitor = PathUtils + final PathCounts pathCounts = 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()); + Assertions.assertEquals(1, pathCounts.getDirectoryCount()); + Assertions.assertEquals(1, pathCounts.getFileCount()); + Assertions.assertEquals(0, pathCounts.getByteCount()); } /** @@ -72,7 +72,7 @@ public class PathUtilsCountingTest { */ @Test public void testCountFolders1FileSize1() throws IOException { - final CountingPathFileVisitor visitor = PathUtils + final PathCounts 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()); @@ -84,10 +84,10 @@ public class PathUtilsCountingTest { */ @Test public void testCountFolders2FileSize2() throws IOException { - final CountingPathFileVisitor visitor = PathUtils + final PathCounts pathCounts = 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()); + Assertions.assertEquals(3, pathCounts.getDirectoryCount()); + Assertions.assertEquals(2, pathCounts.getFileCount()); + Assertions.assertEquals(2, pathCounts.getByteCount()); } } diff --git a/src/test/java/org/apache/commons/io/file/PathUtilsDeletingFileVisitorTest.java b/src/test/java/org/apache/commons/io/file/PathUtilsDeleteDirectoryTest.java similarity index 68% copy from src/test/java/org/apache/commons/io/file/PathUtilsDeletingFileVisitorTest.java copy to src/test/java/org/apache/commons/io/file/PathUtilsDeleteDirectoryTest.java index bad8c6b..af51754 100644 --- a/src/test/java/org/apache/commons/io/file/PathUtilsDeletingFileVisitorTest.java +++ b/src/test/java/org/apache/commons/io/file/PathUtilsDeleteDirectoryTest.java @@ -31,7 +31,7 @@ import org.junit.jupiter.api.Test; /** * Tests {@link DeletingPathFileVisitor}. */ -public class PathUtilsDeletingFileVisitorTest { +public class PathUtilsDeleteDirectoryTest { private Path tempDirectory; @@ -49,32 +49,16 @@ public class PathUtilsDeletingFileVisitorTest { } /** - * Tests an empty folder. - */ - @Test - public void testDeleteEmptyDirectory() throws IOException { - testDeleteEmptyDirectory(PathUtils.deleteDirectory(tempDirectory)); - // This will throw if not empty. - Files.deleteIfExists(tempDirectory); - } - - private void testDeleteEmptyDirectory(final DeletingPathFileVisitor visitor) throws IOException { - Assertions.assertEquals(1, visitor.getDirectoryCount()); - Assertions.assertEquals(0, visitor.getFileCount()); - Assertions.assertEquals(0, visitor.getByteCount()); - } - - /** * Tests a directory with one file of size 0. */ @Test - public void testDeleteFolders1FileSize0() throws IOException { + public void testDeleteDirectory1FileSize0() throws IOException { FileUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-0").toFile(), tempDirectory.toFile()); - final CountingPathFileVisitor visitor = PathUtils.deleteDirectory(tempDirectory); - Assertions.assertEquals(1, visitor.getDirectoryCount()); - Assertions.assertEquals(1, visitor.getFileCount()); - Assertions.assertEquals(0, visitor.getByteCount()); + final PathCounts pathCounts = PathUtils.deleteDirectory(tempDirectory); + Assertions.assertEquals(1, pathCounts.getDirectoryCount()); + Assertions.assertEquals(1, pathCounts.getFileCount()); + Assertions.assertEquals(0, pathCounts.getByteCount()); // This will throw if not empty. Files.deleteIfExists(tempDirectory); } @@ -83,13 +67,13 @@ public class PathUtilsDeletingFileVisitorTest { * Tests a directory with one file of size 1. */ @Test - public void testDeleteFolders1FileSize1() throws IOException { + public void testDeleteDirectory1FileSize1() throws IOException { FileUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1").toFile(), tempDirectory.toFile()); - final CountingPathFileVisitor visitor = PathUtils.deleteDirectory(tempDirectory); - Assertions.assertEquals(1, visitor.getDirectoryCount()); - Assertions.assertEquals(1, visitor.getFileCount()); - Assertions.assertEquals(1, visitor.getByteCount()); + final PathCounts pathCounts = PathUtils.deleteDirectory(tempDirectory); + Assertions.assertEquals(1, pathCounts.getDirectoryCount()); + Assertions.assertEquals(1, pathCounts.getFileCount()); + Assertions.assertEquals(1, pathCounts.getByteCount()); // This will throw if not empty. Files.deleteIfExists(tempDirectory); } @@ -98,14 +82,30 @@ public class PathUtilsDeletingFileVisitorTest { * Tests a directory with two subdirectorys, each containing one file of size 1. */ @Test - public void testDeleteFolders2FileSize2() throws IOException { + public void testDeleteDirectory2FileSize2() throws IOException { FileUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-2-file-size-2").toFile(), tempDirectory.toFile()); - final CountingPathFileVisitor visitor = PathUtils.deleteDirectory(tempDirectory); - Assertions.assertEquals(3, visitor.getDirectoryCount()); - Assertions.assertEquals(2, visitor.getFileCount()); - Assertions.assertEquals(2, visitor.getByteCount()); + final PathCounts pathCounts = PathUtils.deleteDirectory(tempDirectory); + Assertions.assertEquals(3, pathCounts.getDirectoryCount()); + Assertions.assertEquals(2, pathCounts.getFileCount()); + Assertions.assertEquals(2, pathCounts.getByteCount()); // This will throw if not empty. Files.deleteIfExists(tempDirectory); } + + /** + * Tests an empty folder. + */ + @Test + public void testDeleteEmptyDirectory() throws IOException { + testDeleteEmptyDirectory(PathUtils.deleteDirectory(tempDirectory)); + // This will throw if not empty. + Files.deleteIfExists(tempDirectory); + } + + private void testDeleteEmptyDirectory(final PathCounts pathCounts) throws IOException { + Assertions.assertEquals(1, pathCounts.getDirectoryCount()); + Assertions.assertEquals(0, pathCounts.getFileCount()); + Assertions.assertEquals(0, pathCounts.getByteCount()); + } } diff --git a/src/test/java/org/apache/commons/io/file/PathUtilsDeletingFileVisitorTest.java b/src/test/java/org/apache/commons/io/file/PathUtilsDeleteFileTest.java similarity index 54% copy from src/test/java/org/apache/commons/io/file/PathUtilsDeletingFileVisitorTest.java copy to src/test/java/org/apache/commons/io/file/PathUtilsDeleteFileTest.java index bad8c6b..dfac100 100644 --- a/src/test/java/org/apache/commons/io/file/PathUtilsDeletingFileVisitorTest.java +++ b/src/test/java/org/apache/commons/io/file/PathUtilsDeleteFileTest.java @@ -19,6 +19,7 @@ package org.apache.commons.io.file; import java.io.IOException; import java.nio.file.Files; +import java.nio.file.NotDirectoryException; import java.nio.file.Path; import java.nio.file.Paths; @@ -31,7 +32,7 @@ import org.junit.jupiter.api.Test; /** * Tests {@link DeletingPathFileVisitor}. */ -public class PathUtilsDeletingFileVisitorTest { +public class PathUtilsDeleteFileTest { private Path tempDirectory; @@ -49,62 +50,62 @@ public class PathUtilsDeletingFileVisitorTest { } /** - * Tests an empty folder. + * Tests a directory with one file of size 0. */ @Test - public void testDeleteEmptyDirectory() throws IOException { - testDeleteEmptyDirectory(PathUtils.deleteDirectory(tempDirectory)); + public void testDeleteFileDirectory1FileSize0() throws IOException { + final String fileName = "file-size-0.bin"; + FileUtils.copyFileToDirectory( + Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-0/" + fileName).toFile(), + tempDirectory.toFile()); + final PathCounts pathCounts = PathUtils.deleteFile(tempDirectory.resolve(fileName)); + Assertions.assertEquals(0, pathCounts.getDirectoryCount()); + Assertions.assertEquals(1, pathCounts.getFileCount()); + Assertions.assertEquals(0, pathCounts.getByteCount()); // This will throw if not empty. Files.deleteIfExists(tempDirectory); } - private void testDeleteEmptyDirectory(final DeletingPathFileVisitor visitor) throws IOException { - Assertions.assertEquals(1, visitor.getDirectoryCount()); - Assertions.assertEquals(0, visitor.getFileCount()); - Assertions.assertEquals(0, visitor.getByteCount()); - } - /** - * Tests a directory with one file of size 0. + * Tests a directory with one file of size 1. */ @Test - public void testDeleteFolders1FileSize0() throws IOException { - FileUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-0").toFile(), + public void testDeleteFileDirectory1FileSize1() throws IOException { + final String fileName = "file-size-1.bin"; + FileUtils.copyFileToDirectory( + Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1/" + fileName).toFile(), tempDirectory.toFile()); - final CountingPathFileVisitor visitor = PathUtils.deleteDirectory(tempDirectory); - Assertions.assertEquals(1, visitor.getDirectoryCount()); - Assertions.assertEquals(1, visitor.getFileCount()); - Assertions.assertEquals(0, visitor.getByteCount()); + final PathCounts pathCounts = PathUtils.deleteFile(tempDirectory.resolve(fileName)); + Assertions.assertEquals(0, pathCounts.getDirectoryCount()); + Assertions.assertEquals(1, pathCounts.getFileCount()); + Assertions.assertEquals(1, pathCounts.getByteCount()); // This will throw if not empty. Files.deleteIfExists(tempDirectory); } /** - * Tests a directory with one file of size 1. + * Tests a file that does not exist. */ @Test - 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 CountingPathFileVisitor visitor = PathUtils.deleteDirectory(tempDirectory); - Assertions.assertEquals(1, visitor.getDirectoryCount()); - Assertions.assertEquals(1, visitor.getFileCount()); - Assertions.assertEquals(1, visitor.getByteCount()); + public void testDeleteFileDoesNotExist() throws IOException { + testDeleteFileEmpty(PathUtils.deleteFile(tempDirectory.resolve("file-does-not-exist.bin"))); // This will throw if not empty. Files.deleteIfExists(tempDirectory); } + private void testDeleteFileEmpty(final PathCounts pathCounts) throws IOException { + Assertions.assertEquals(0, pathCounts.getDirectoryCount(), "getDirectoryCount()"); + Assertions.assertEquals(0, pathCounts.getFileCount(), "getFileCount()"); + Assertions.assertEquals(0, pathCounts.getByteCount(), "getByteCount()"); + } + /** - * Tests a directory with two subdirectorys, each containing one file of size 1. + * Tests an empty folder. */ @Test - 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 CountingPathFileVisitor visitor = PathUtils.deleteDirectory(tempDirectory); - Assertions.assertEquals(3, visitor.getDirectoryCount()); - Assertions.assertEquals(2, visitor.getFileCount()); - Assertions.assertEquals(2, visitor.getByteCount()); + public void testDeleteFileEmptyDirectory() throws IOException { + Assertions.assertThrows(NotDirectoryException.class, + () -> testDeleteFileEmpty(PathUtils.deleteFile(tempDirectory))); // This will throw if not empty. Files.deleteIfExists(tempDirectory); } diff --git a/src/test/java/org/apache/commons/io/file/PathUtilsDeletingFileVisitorTest.java b/src/test/java/org/apache/commons/io/file/PathUtilsDeleteTest.java similarity index 54% rename from src/test/java/org/apache/commons/io/file/PathUtilsDeletingFileVisitorTest.java rename to src/test/java/org/apache/commons/io/file/PathUtilsDeleteTest.java index bad8c6b..9c70e85 100644 --- a/src/test/java/org/apache/commons/io/file/PathUtilsDeletingFileVisitorTest.java +++ b/src/test/java/org/apache/commons/io/file/PathUtilsDeleteTest.java @@ -31,7 +31,7 @@ import org.junit.jupiter.api.Test; /** * Tests {@link DeletingPathFileVisitor}. */ -public class PathUtilsDeletingFileVisitorTest { +public class PathUtilsDeleteTest { private Path tempDirectory; @@ -49,62 +49,64 @@ public class PathUtilsDeletingFileVisitorTest { } /** - * Tests an empty folder. + * Tests a directory with one file of size 0. */ @Test - public void testDeleteEmptyDirectory() throws IOException { - testDeleteEmptyDirectory(PathUtils.deleteDirectory(tempDirectory)); + public void testDeleteDirectory1FileSize0() throws IOException { + final String fileName = "file-size-0.bin"; + FileUtils.copyFileToDirectory( + Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-0/" + fileName).toFile(), + tempDirectory.toFile()); + final PathCounts pathCounts = PathUtils.delete(tempDirectory.resolve(fileName)); + Assertions.assertEquals(0, pathCounts.getDirectoryCount()); + Assertions.assertEquals(1, pathCounts.getFileCount()); + Assertions.assertEquals(0, pathCounts.getByteCount()); // This will throw if not empty. Files.deleteIfExists(tempDirectory); } - private void testDeleteEmptyDirectory(final DeletingPathFileVisitor visitor) throws IOException { - Assertions.assertEquals(1, visitor.getDirectoryCount()); - Assertions.assertEquals(0, visitor.getFileCount()); - Assertions.assertEquals(0, visitor.getByteCount()); - } - /** - * Tests a directory with one file of size 0. + * Tests a directory with one file of size 1. */ @Test - public void testDeleteFolders1FileSize0() throws IOException { - FileUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-0").toFile(), + public void testDeleteDirectory1FileSize1() throws IOException { + final String fileName = "file-size-1.bin"; + FileUtils.copyFileToDirectory( + Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1/" + fileName).toFile(), tempDirectory.toFile()); - final CountingPathFileVisitor visitor = PathUtils.deleteDirectory(tempDirectory); - Assertions.assertEquals(1, visitor.getDirectoryCount()); - Assertions.assertEquals(1, visitor.getFileCount()); - Assertions.assertEquals(0, visitor.getByteCount()); + final PathCounts pathCounts = PathUtils.delete(tempDirectory.resolve(fileName)); + Assertions.assertEquals(0, pathCounts.getDirectoryCount()); + Assertions.assertEquals(1, pathCounts.getFileCount()); + Assertions.assertEquals(1, pathCounts.getByteCount()); // This will throw if not empty. Files.deleteIfExists(tempDirectory); } /** - * Tests a directory with one file of size 1. + * Tests an empty folder. */ @Test - 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 CountingPathFileVisitor visitor = PathUtils.deleteDirectory(tempDirectory); - Assertions.assertEquals(1, visitor.getDirectoryCount()); - Assertions.assertEquals(1, visitor.getFileCount()); - Assertions.assertEquals(1, visitor.getByteCount()); + public void testDeleteEmptyDirectory() throws IOException { + testDeleteEmptyDirectory(PathUtils.delete(tempDirectory)); // This will throw if not empty. Files.deleteIfExists(tempDirectory); } + private void testDeleteEmptyDirectory(final PathCounts pathCounts) throws IOException { + Assertions.assertEquals(1, pathCounts.getDirectoryCount(), "getDirectoryCount()"); + Assertions.assertEquals(0, pathCounts.getFileCount(), "getFileCount()"); + Assertions.assertEquals(0, pathCounts.getByteCount(), "getByteCount()"); + } + /** - * Tests a directory with two subdirectorys, each containing one file of size 1. + * Tests a file that does not exist. */ @Test - 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 CountingPathFileVisitor visitor = PathUtils.deleteDirectory(tempDirectory); - Assertions.assertEquals(3, visitor.getDirectoryCount()); - Assertions.assertEquals(2, visitor.getFileCount()); - Assertions.assertEquals(2, visitor.getByteCount()); + public void testDeleteFileDoesNotExist() throws IOException { + final PathCounts pathCounts = PathUtils.deleteFile(tempDirectory.resolve("file-does-not-exist.bin")); + Assertions.assertEquals(0, pathCounts.getDirectoryCount()); + Assertions.assertEquals(0, pathCounts.getFileCount()); + Assertions.assertEquals(0, pathCounts.getByteCount()); // This will throw if not empty. Files.deleteIfExists(tempDirectory); }