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 2022/02/06 15:32:14 UTC

[commons-io] branch master updated: Add green unit test, slightly modified, from https://github.com/apache/commons-io/pull/324 by chadlwilson.

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 4cc8f4f  Add green unit test, slightly modified, from https://github.com/apache/commons-io/pull/324 by chadlwilson.
4cc8f4f is described below

commit 4cc8f4f922af910f5dc28bb8ccf4037c35bd63bc
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Feb 6 10:32:11 2022 -0500

    Add green unit test, slightly modified, from
    https://github.com/apache/commons-io/pull/324 by chadlwilson.
---
 .../org/apache/commons/io/file/PathUtilsTest.java  | 28 ++++++++++++++++++++--
 1 file changed, 26 insertions(+), 2 deletions(-)

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 40b93a7..1f2c346 100644
--- a/src/test/java/org/apache/commons/io/file/PathUtilsTest.java
+++ b/src/test/java/org/apache/commons/io/file/PathUtilsTest.java
@@ -20,6 +20,7 @@ package org.apache.commons.io.file;
 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrowsExactly;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.junit.jupiter.api.Assumptions.assumeFalse;
 
@@ -28,6 +29,7 @@ import java.io.OutputStream;
 import java.net.URI;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.DirectoryStream;
+import java.nio.file.FileAlreadyExistsException;
 import java.nio.file.FileSystem;
 import java.nio.file.FileSystems;
 import java.nio.file.Files;
@@ -61,6 +63,14 @@ public class PathUtilsTest extends AbstractTempDirTest {
 
     private static final String PATH_FIXTURE = "NOTICE.txt";
 
+    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 FileSystem openArchive(final Path p, final boolean createNew) throws IOException {
         if (createNew) {
             final Map<String, String> env = new HashMap<>();
@@ -160,6 +170,12 @@ public class PathUtilsTest extends AbstractTempDirTest {
     }
 
     @Test
+    public void testCreateDirectoriesWithClashingSymlink() throws IOException {
+        final Path symlinkedDir = createTempSymlinkedRelativeDir();
+        assertThrowsExactly(FileAlreadyExistsException.class, () -> PathUtils.createParentDirectories(symlinkedDir.resolve("child")));
+    }
+
+    @Test
     public void testGetTempDirectory() {
         final Path tempDirectory = Paths.get(System.getProperty("java.io.tmpdir"));
         assertEquals(tempDirectory, PathUtils.getTempDirectory());
@@ -184,7 +200,7 @@ public class PathUtilsTest extends AbstractTempDirTest {
         try {
             Files.getPosixFilePermissions(PathUtils.current());
             isPosix = true;
-        } catch (UnsupportedOperationException e) {
+        } catch (final UnsupportedOperationException e) {
             isPosix = false;
         }
         assertEquals(isPosix, PathUtils.isPosix(PathUtils.current()));
@@ -242,12 +258,20 @@ public class PathUtilsTest extends AbstractTempDirTest {
     }
 
     @Test
+    public void testNewOutputStreamNewFileInsideExistingSymlinkedDir() throws IOException {
+        final Path symlinkDir = createTempSymlinkedRelativeDir();
+
+        final Path file = symlinkDir.resolve("test.txt");
+        assertThrowsExactly(FileAlreadyExistsException.class, () -> PathUtils.newOutputStream(file, false));
+    }
+
+    @Test
     public void testReadAttributesPosix() throws IOException {
         boolean isPosix;
         try {
             Files.getPosixFilePermissions(PathUtils.current());
             isPosix = true;
-        } catch (UnsupportedOperationException e) {
+        } catch (final UnsupportedOperationException e) {
             isPosix = false;
         }
         assertEquals(isPosix, PathUtils.readAttributes(PathUtils.current(), PosixFileAttributes.class) != null);