Skip to content

Commit b95421c

Browse files
committed
[IO-872] PathUtils.directoryAndFileContentEquals doesn't work across
FileSystems Add tests and fix NPE
1 parent 64945d5 commit b95421c

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed

src/main/java/org/apache/commons/io/file/PathUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,8 +1739,8 @@ public static BigInteger sizeOfDirectoryAsBigInteger(final Path directory) throw
17391739
private static Path stripTrailingSeparator(final Path cdir) {
17401740
final Path dir = cdir.normalize();
17411741
final String separator = dir.getFileSystem().getSeparator();
1742-
final String fileName = dir.getFileName().toString();
1743-
return fileName.endsWith(separator) ? dir.resolveSibling(fileName.substring(0, fileName.length() - 1)) : dir;
1742+
final String fileName = Objects.toString(dir.getFileName(), null);
1743+
return fileName != null && fileName.endsWith(separator) ? dir.resolveSibling(fileName.substring(0, fileName.length() - 1)) : dir;
17441744
}
17451745

17461746
/**

src/test/java/org/apache/commons/io/file/PathUtilsContentEqualsTest.java

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.nio.file.Files;
2929
import java.nio.file.Path;
3030
import java.nio.file.Paths;
31+
import java.nio.file.StandardCopyOption;
3132

3233
import org.junit.jupiter.api.Test;
3334
import org.junit.jupiter.api.io.TempDir;
@@ -106,7 +107,7 @@ public void testDirectoryAndFileContentEquals() throws Exception {
106107
* @throws Exception on test failure.
107108
*/
108109
@Test
109-
public void testDirectoryAndFileContentEqualsDifferentFileSystems() throws Exception {
110+
public void testDirectoryAndFileContentEqualsDifferentFileSystemsFileVsZip() throws Exception {
110111
final Path dir1 = Paths.get("src/test/resources/dir-equals-tests");
111112
try (FileSystem fileSystem = FileSystems.newFileSystem(dir1.resolveSibling(dir1.getFileName() + ".zip"), null)) {
112113
final Path dir2 = fileSystem.getPath("/dir-equals-tests");
@@ -115,6 +116,59 @@ public void testDirectoryAndFileContentEqualsDifferentFileSystems() throws Excep
115116
}
116117
}
117118

119+
/**
120+
* Tests IO-872 PathUtils.directoryAndFileContentEquals doesn't work across FileSystems.
121+
*
122+
* @throws Exception on test failure.
123+
*/
124+
@Test
125+
public void testDirectoryAndFileContentEqualsDifferentFileSystemsZipVsZip() throws Exception {
126+
final Path zipPath = Paths.get("src/test/resources/dir-equals-tests.zip");
127+
final Path zipCopy = temporaryFolder.toPath().resolve("copy.zip");
128+
Files.copy(zipPath, zipCopy, StandardCopyOption.REPLACE_EXISTING);
129+
try (FileSystem fileSystem1 = FileSystems.newFileSystem(zipPath, null);
130+
FileSystem fileSystem2 = FileSystems.newFileSystem(zipCopy, null)) {
131+
final Path dir1 = fileSystem1.getPath("/dir-equals-tests");
132+
final Path dir2 = fileSystem2.getPath("/dir-equals-tests");
133+
// WindowsPath, UnixPath, and ZipPath equals() methods always return false if the argument is not of the same instance as itself.
134+
assertTrue(PathUtils.directoryAndFileContentEquals(dir1, dir2));
135+
}
136+
}
137+
138+
/**
139+
* Tests IO-872 PathUtils.directoryAndFileContentEquals doesn't work across FileSystems.
140+
*
141+
* @throws Exception on test failure.
142+
*/
143+
@Test
144+
public void testDirectoryAndFileContentEqualsDifferentFileSystemsZipVsZipEmpty() throws Exception {
145+
final Path zipPath = Paths.get("src/test/resources/dir-equals-tests.zip");
146+
final Path emptyZip = Paths.get("src/test/resources/org/apache/commons/io/empty.zip");
147+
Files.copy(zipPath, emptyZip, StandardCopyOption.REPLACE_EXISTING);
148+
try (FileSystem fileSystem1 = FileSystems.newFileSystem(zipPath, null);
149+
FileSystem fileSystem2 = FileSystems.newFileSystem(emptyZip, null)) {
150+
final Path dir1 = fileSystem1.getPath("/dir-equals-tests");
151+
final Path dir2 = fileSystem2.getPath("/");
152+
// WindowsPath, UnixPath, and ZipPath equals() methods always return false if the argument is not of the same instance as itself.
153+
assertFalse(PathUtils.directoryAndFileContentEquals(dir1, dir2));
154+
}
155+
try (FileSystem fileSystem1 = FileSystems.newFileSystem(zipPath, null);
156+
FileSystem fileSystem2 = FileSystems.newFileSystem(emptyZip, null)) {
157+
final Path dir1 = fileSystem1.getPath("/dir-equals-tests");
158+
final Path dir2 = fileSystem2.getRootDirectories().iterator().next();
159+
// WindowsPath, UnixPath, and ZipPath equals() methods always return false if the argument is not of the same instance as itself.
160+
assertFalse(PathUtils.directoryAndFileContentEquals(dir1, dir2));
161+
}
162+
final Path zipCopy = temporaryFolder.toPath().resolve("copy.zip");
163+
Files.copy(emptyZip, zipCopy, StandardCopyOption.REPLACE_EXISTING);
164+
try (FileSystem fileSystem1 = FileSystems.newFileSystem(emptyZip, null);
165+
FileSystem fileSystem2 = FileSystems.newFileSystem(zipCopy, null)) {
166+
final Path dir1 = fileSystem1.getPath("");
167+
final Path dir2 = fileSystem2.getPath("");
168+
// WindowsPath, UnixPath, and ZipPath equals() methods always return false if the argument is not of the same instance as itself.
169+
assertTrue(PathUtils.directoryAndFileContentEquals(dir1, dir2));
170+
}
171+
}
118172

119173
@Test
120174
public void testDirectoryContentEquals() throws Exception {
8.76 KB
Binary file not shown.

0 commit comments

Comments
 (0)