Add green unit test, slightly modified, from
by chadlwilson.
MODIFY
src/test/java/org/apache/commons/io/file/PathUtilsTest.java
src/test/java/org/apache/commons/io/file/PathUtilsTest.java
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;
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;
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<>();
@@ -159,6 +169,12 @@ public void testCreateDirectoriesNew() throws IOException {
assertEquals(tempDirPath, PathUtils.createParentDirectories(tempDirPath.resolve("child")));
}
+ @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"));
@@ -184,7 +200,7 @@ public void testIsPosix() throws IOException {
try {
Files.getPosixFilePermissions(PathUtils.current());
isPosix = true;
- } catch (UnsupportedOperationException e) {
+ } catch (final UnsupportedOperationException e) {
isPosix = false;
}
assertEquals(isPosix, PathUtils.isPosix(PathUtils.current()));
@@ -241,13 +257,21 @@ public void testNewOutputStreamNewFileAppendTrue() throws IOException {
testNewOutputStreamNewFile(true);
}
+ @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);

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