You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2024/01/27 22:31:55 UTC
(commons-io) 01/03: Sort members
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 a2eb0ac0eebda2eb7c5f4ba9655236586199180e
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jan 27 17:29:25 2024 -0500
Sort members
---
.../java/org/apache/commons/io/file/PathUtils.java | 36 ++---
.../java/org/apache/commons/io/FileUtilsTest.java | 172 ++++++++++-----------
2 files changed, 104 insertions(+), 104 deletions(-)
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 5812cc7a..fc667065 100644
--- a/src/main/java/org/apache/commons/io/file/PathUtils.java
+++ b/src/main/java/org/apache/commons/io/file/PathUtils.java
@@ -855,6 +855,24 @@ public final class PathUtils {
return Files.getFileAttributeView(path, AclFileAttributeView.class, options);
}
+ /**
+ * Gets the base name (the part up to and not including the last ".") of the last path segment of a file name.
+ * <p>
+ * Will return the file name itself if it doesn't contain any dots. All leading directories of the {@code file name} parameter are skipped.
+ * </p>
+ *
+ * @return the base name of file name
+ * @param path the path of the file to obtain the base name of.
+ * @since 2.16.0
+ */
+ public static String getBaseName(final Path path) {
+ if (path == null) {
+ return null;
+ }
+ final Path fileName = path.getFileName();
+ return fileName != null ? FilenameUtils.removeExtension(fileName.toString()) : null;
+ }
+
/**
* Shorthand for {@code Files.getFileAttributeView(path, DosFileAttributeView.class)}.
*
@@ -1855,24 +1873,6 @@ public final class PathUtils {
return path;
}
- /**
- * Gets the base name (the part up to and not including the last ".") of the last path segment of a file name.
- * <p>
- * Will return the file name itself if it doesn't contain any dots. All leading directories of the {@code file name} parameter are skipped.
- * </p>
- *
- * @return the base name of file name
- * @param path the path of the file to obtain the base name of.
- * @since 2.16.0
- */
- public static String getBaseName(final Path path) {
- if (path == null) {
- return null;
- }
- final Path fileName = path.getFileName();
- return fileName != null ? FilenameUtils.removeExtension(fileName.toString()) : null;
- }
-
/**
* Prevents instantiation.
*/
diff --git a/src/test/java/org/apache/commons/io/FileUtilsTest.java b/src/test/java/org/apache/commons/io/FileUtilsTest.java
index 6291dfb6..f19ed32b 100644
--- a/src/test/java/org/apache/commons/io/FileUtilsTest.java
+++ b/src/test/java/org/apache/commons/io/FileUtilsTest.java
@@ -750,31 +750,70 @@ public class FileUtilsTest extends AbstractTempDirTest {
assertTrue(FileUtils.contentEqualsIgnoreEOL(file1, file2, null));
}
+ /**
+ * See what happens when copyDirectory copies a directory that is a symlink
+ * to another directory containing non-symlinked files.
+ * This is a characterization test to explore current behavior, and arguably
+ * represents a bug. This behavior, and the test, is likely to change
+ * and should not be relied on.
+ */
@Test
- public void testCopyDirectory_symLink() throws IOException {
- // Make a file
- final File sourceDirectory = new File(tempDirFile, "source_directory");
- sourceDirectory.mkdir();
- final File targetFile = new File(sourceDirectory, "hello.txt");
- FileUtils.writeStringToFile(targetFile, "HELLO WORLD", "UTF8");
+ public void testCopyDir_symLink() throws Exception {
+ // Make a directory
+ final File realDirectory = new File(tempDirFile, "real_directory");
+ realDirectory.mkdir();
+ final File content = new File(realDirectory, "hello.txt");
+ FileUtils.writeStringToFile(content, "HELLO WORLD", "UTF8");
- // Make a symlink to the file
- final Path targetPath = targetFile.toPath();
- final Path linkPath = sourceDirectory.toPath().resolve("linkfile");
- Files.createSymbolicLink(linkPath, targetPath);
- assumeTrue(Files.isSymbolicLink(linkPath), () -> "Expected a symlink here: " + linkPath);
- assumeTrue(Files.exists(linkPath));
- assumeTrue(Files.exists(linkPath, LinkOption.NOFOLLOW_LINKS));
+ // Make a symlink to the directory
+ final Path linkPath = tempDirFile.toPath().resolve("link_to_directory");
+ Files.createSymbolicLink(linkPath, realDirectory.toPath());
- // Now copy sourceDirectory to another directory
+ // Now copy symlink
final File destination = new File(tempDirFile, "destination");
- FileUtils.copyDirectory(sourceDirectory, destination);
- assertTrue(destination.exists());
- final Path copiedSymlink = new File(destination, "linkfile").toPath();
- // test for the existence of the copied symbolic link as a link
- assertTrue(Files.isSymbolicLink(copiedSymlink));
- assertTrue(Files.exists(copiedSymlink));
+ // Is the copy a symlink or an actual directory?
+ FileUtils.copyDirectory(linkPath.toFile(), destination);
+
+ // delete the original file so that if we can read the bytes from the
+ // copied directory it's definitely been copied, not linked.
+ assumeTrue(content.delete());
+
+ assertFalse(Files.isSymbolicLink(destination.toPath()));
+ final File copied_content = new File(destination, "hello.txt");
+ final String actual = FileUtils.readFileToString(copied_content, "UTF8");
+ assertEquals("HELLO WORLD", actual);
+ }
+
+ @Test
+ public void testCopyDir_symLinkCycle() throws Exception {
+ // Make a directory
+ final File topDirectory = new File(tempDirFile, "topDirectory");
+ topDirectory.mkdir();
+ final File content = new File(topDirectory, "hello.txt");
+ FileUtils.writeStringToFile(content, "HELLO WORLD", "UTF8");
+ final File childDirectory = new File(topDirectory, "child_directory");
+ childDirectory.mkdir();
+
+ // Make a symlink to the top directory
+ final Path linkPath = childDirectory.toPath().resolve("link_to_top");
+ Files.createSymbolicLink(linkPath, topDirectory.toPath());
+
+ // Now copy symlink
+ final File destination = new File(tempDirFile, "destination");
+ FileUtils.copyDirectory(linkPath.toFile(), destination);
+
+ // delete the original file so that if we can read the bytes from the
+ // copied directory it's definitely been copied, not linked.
+ assumeTrue(content.delete());
+
+ assertFalse(Files.isSymbolicLink(destination.toPath()));
+ final File copied_content = new File(destination, "hello.txt");
+ final String actual = FileUtils.readFileToString(copied_content, "UTF8");
+ assertEquals("HELLO WORLD", actual);
+
+ final File[] copied = destination.listFiles();
+ assertEquals(2, copied.length);
}
/**
@@ -815,6 +854,33 @@ public class FileUtilsTest extends AbstractTempDirTest {
assertFalse(Files.exists(copiedBrokenSymlink));
}
+ @Test
+ public void testCopyDirectory_symLink() throws IOException {
+ // Make a file
+ final File sourceDirectory = new File(tempDirFile, "source_directory");
+ sourceDirectory.mkdir();
+ final File targetFile = new File(sourceDirectory, "hello.txt");
+ FileUtils.writeStringToFile(targetFile, "HELLO WORLD", "UTF8");
+
+ // Make a symlink to the file
+ final Path targetPath = targetFile.toPath();
+ final Path linkPath = sourceDirectory.toPath().resolve("linkfile");
+ Files.createSymbolicLink(linkPath, targetPath);
+ assumeTrue(Files.isSymbolicLink(linkPath), () -> "Expected a symlink here: " + linkPath);
+ assumeTrue(Files.exists(linkPath));
+ assumeTrue(Files.exists(linkPath, LinkOption.NOFOLLOW_LINKS));
+
+ // Now copy sourceDirectory to another directory
+ final File destination = new File(tempDirFile, "destination");
+ FileUtils.copyDirectory(sourceDirectory, destination);
+ assertTrue(destination.exists());
+ final Path copiedSymlink = new File(destination, "linkfile").toPath();
+
+ // test for the existence of the copied symbolic link as a link
+ assertTrue(Files.isSymbolicLink(copiedSymlink));
+ assertTrue(Files.exists(copiedSymlink));
+ }
+
@Test
public void testCopyDirectoryExceptions() {
//
@@ -1113,72 +1179,6 @@ public class FileUtilsTest extends AbstractTempDirTest {
assertEquals("HELLO WORLD", contents);
}
- /**
- * See what happens when copyDirectory copies a directory that is a symlink
- * to another directory containing non-symlinked files.
- * This is a characterization test to explore current behavior, and arguably
- * represents a bug. This behavior, and the test, is likely to change
- * and should not be relied on.
- */
- @Test
- public void testCopyDir_symLink() throws Exception {
- // Make a directory
- final File realDirectory = new File(tempDirFile, "real_directory");
- realDirectory.mkdir();
- final File content = new File(realDirectory, "hello.txt");
- FileUtils.writeStringToFile(content, "HELLO WORLD", "UTF8");
-
- // Make a symlink to the directory
- final Path linkPath = tempDirFile.toPath().resolve("link_to_directory");
- Files.createSymbolicLink(linkPath, realDirectory.toPath());
-
- // Now copy symlink
- final File destination = new File(tempDirFile, "destination");
-
- // Is the copy a symlink or an actual directory?
- FileUtils.copyDirectory(linkPath.toFile(), destination);
-
- // delete the original file so that if we can read the bytes from the
- // copied directory it's definitely been copied, not linked.
- assumeTrue(content.delete());
-
- assertFalse(Files.isSymbolicLink(destination.toPath()));
- final File copied_content = new File(destination, "hello.txt");
- final String actual = FileUtils.readFileToString(copied_content, "UTF8");
- assertEquals("HELLO WORLD", actual);
- }
-
- @Test
- public void testCopyDir_symLinkCycle() throws Exception {
- // Make a directory
- final File topDirectory = new File(tempDirFile, "topDirectory");
- topDirectory.mkdir();
- final File content = new File(topDirectory, "hello.txt");
- FileUtils.writeStringToFile(content, "HELLO WORLD", "UTF8");
- final File childDirectory = new File(topDirectory, "child_directory");
- childDirectory.mkdir();
-
- // Make a symlink to the top directory
- final Path linkPath = childDirectory.toPath().resolve("link_to_top");
- Files.createSymbolicLink(linkPath, topDirectory.toPath());
-
- // Now copy symlink
- final File destination = new File(tempDirFile, "destination");
- FileUtils.copyDirectory(linkPath.toFile(), destination);
-
- // delete the original file so that if we can read the bytes from the
- // copied directory it's definitely been copied, not linked.
- assumeTrue(content.delete());
-
- assertFalse(Files.isSymbolicLink(destination.toPath()));
- final File copied_content = new File(destination, "hello.txt");
- final String actual = FileUtils.readFileToString(copied_content, "UTF8");
- assertEquals("HELLO WORLD", actual);
-
- final File[] copied = destination.listFiles();
- assertEquals(2, copied.length);
- }
-
@Test
public void testCopyFile1() throws Exception {
final File destination = new File(tempDirFile, "copy1.txt");