Skip to content

Commit 5f64b1b

Browse files
committed
[IO-790] Fix symbolic link file filter #450.
Clean ups post PR merge
1 parent c9ed8da commit 5f64b1b

3 files changed

Lines changed: 70 additions & 67 deletions

File tree

src/changes/changes.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ The <action> type attribute can be add,update,fix,remove.
162162
Fix misleading comments in FileFilterTest #334.
163163
</action>
164164
<action dev="ggregory" type="fix" due-to="Diego Marcilio">
165-
Add missing javadoc for exceptions thrown for invalid arguments #339.
165+
Add missing Javadoc for exceptions thrown for invalid arguments #339.
166166
</action>
167167
<action dev="ggregory" type="fix" due-to="richarda23">
168168
FileFilterTest minor fixes #340.
@@ -215,6 +215,9 @@ The <action> type attribute can be add,update,fix,remove.
215215
<action issue="IO-782" dev="ggregory" type="fix" due-to="Matteo Di Giovinazzo, Gary Gregory">
216216
SequenceReader should close readers when its close method is called #391.
217217
</action>
218+
<action issue="IO-790" dev="ggregory" type="fix" due-to="Miguel Muñoz, Gary Gregory">
219+
Fix symbolic link file filter #450.
220+
</action>
218221
<!-- ADD -->
219222
<action type="add" dev="ggregory" due-to="Gary Gregory">
220223
Add GitHub coverage.yml.

src/main/java/org/apache/commons/io/filefilter/SymbolicLinkFileFilter.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,11 @@ public FileVisitResult accept(final Path path, final BasicFileAttributes attribu
124124
}
125125

126126
/**
127-
* Package access, so the unit test may override to mock it. To
128-
* facilitate unit testing, all calls to test if the file is a symbolic should go
129-
* through this method. (See the unit test for why.)
127+
* Delegates to {@link Files#isSymbolicLink(Path)} for testing.
128+
* <p>
129+
* Using package access for unit tests. To facilitate unit testing, all calls to test if the file is a symbolic should go through this method. (See the unit
130+
* test for why.)
131+
* </p>
130132
*
131133
* @param filePath The filePath to test
132134
* @return true if the file exists and is a symbolic link to either a file or directory, false otherwise.

src/test/java/org/apache/commons/io/filefilter/SymbolicLinkFileFilterTest.java

Lines changed: 61 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,23 @@
1717

1818
package org.apache.commons.io.filefilter;
1919

20+
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.assertFalse;
22+
import static org.junit.jupiter.api.Assertions.assertTrue;
23+
2024
import java.io.File;
2125
import java.io.IOException;
2226
import java.nio.file.FileVisitResult;
2327
import java.nio.file.Files;
2428
import java.nio.file.Path;
25-
import java.util.function.BiFunction;
2629

2730
import org.apache.commons.io.file.PathUtils;
31+
import org.apache.commons.io.function.IOBiFunction;
32+
import org.apache.commons.lang3.SystemUtils;
2833
import org.junit.jupiter.api.AfterAll;
2934
import org.junit.jupiter.api.BeforeAll;
3035
import org.junit.jupiter.api.Test;
3136

32-
import static org.junit.jupiter.api.Assertions.assertEquals;
33-
import static org.junit.jupiter.api.Assertions.assertFalse;
34-
import static org.junit.jupiter.api.Assertions.assertTrue;
35-
3637
/**
3738
* Tests {@link SymbolicLinkFileFilter}.
3839
*/
@@ -44,63 +45,69 @@ public class SymbolicLinkFileFilterTest {
4445
public static final String DIRECTORY_NAME = "SLFF_TargetDirectory";
4546
public static final String DIRECTORY_LINK_NAME = "SLFF_LinkDirectory";
4647
public static final String MISSING = "Missing";
47-
private static File testTargetFile; // hard file
48-
private static Path testTargetPath; // hard file Path
49-
private static File parentDirectoryFile; // System Temp directory
50-
private static File testLinkFile; // symbolic link to hard file
51-
private static String linkName; // Name of link file
52-
private static Path testLinkPath; // symbolic link to hard file Path
53-
private static File targetDirFile; //
54-
private static Path targetDirPath; // hard directory Path
55-
private static Path testLinkDirPath; // symbolic link to hardDirectory
48+
49+
private static File testTargetFile; // hard file
50+
private static Path testTargetPath; // hard file Path
51+
private static File parentDirectoryFile; // System Temp directory
52+
private static File testLinkFile; // symbolic link to hard file
53+
private static String linkName; // Name of link file
54+
private static Path testLinkPath; // symbolic link to hard file Path
55+
private static File targetDirFile; //
56+
private static Path targetDirPath; // hard directory Path
57+
private static Path testLinkDirPath; // symbolic link to hardDirectory
5658
private static File testLinkDirFile;
57-
private static File missingFile; // non-existent file
59+
private static File missingFile; // non-existent file
5860
private static SymbolicLinkFileFilter filter;
5961

60-
private static Path createRealSymbolicLink(Path link, Path target) {
61-
try {
62-
if (Files.exists(link)) {
63-
Files.delete(link);
64-
}
65-
return Files.createSymbolicLink(link, target);
66-
} catch (IOException e) {
67-
throw new IllegalStateException("Failure to create Symbolic Link", e);
68-
}
69-
}
70-
71-
private static Path createMockSymbolicLink(Path lnk, Path tgt) {
72-
try {
73-
return Files.createFile(lnk);
74-
} catch (IOException e) {
75-
throw new IllegalStateException("Failure to create Symbolic Link", e);
76-
}
77-
}
78-
7962
// Mock filter for testing on Windows.
8063
private static SymbolicLinkFileFilter createMockFilter() {
8164
return new SymbolicLinkFileFilter() {
65+
66+
private static final long serialVersionUID = 1L;
67+
8268
@Override
8369
boolean isSymbolicLink(final Path filePath) {
8470
return filePath.toFile().exists() && filePath.toString().contains("Link"); // Mock test
8571
}
8672
};
8773
}
8874

75+
private static Path createMockSymbolicLink(final Path link, final Path target) throws IOException {
76+
return Files.createFile(link);
77+
}
78+
79+
private static Path createRealSymbolicLink(final Path link, final Path target) throws IOException {
80+
Files.deleteIfExists(link);
81+
return Files.createSymbolicLink(link, target);
82+
}
83+
84+
@AfterAll
85+
static void tearDown() {
86+
// Fortunately, delete() doesn't throw an exception if the file doesn't exist.
87+
testLinkDirFile.delete();
88+
targetDirFile.delete();
89+
testLinkFile.delete();
90+
testTargetFile.delete();
91+
}
92+
8993
/**
90-
* <p>Unit test setup creates a hard file, a symbolic link to the hard file, a hard directory,
91-
* and a symbolic link to that directory. All are created in the temp directory</p>
92-
* <p>Unit test teardown deletes all four of these files.</p>
94+
* <p>
95+
* Unit test setup creates a hard file, a symbolic link to the hard file, a hard directory, and a symbolic link to that directory. All are created in the
96+
* temp directory
97+
* </p>
98+
* <p>
99+
* Unit test teardown deletes all four of these files.
100+
* </p>
93101
*
94102
* @throws IOException If it fails to create the temporary files
95103
*/
96104
@BeforeAll
97105
static void testSetup() throws IOException {
98-
final BiFunction<Path, Path, Path> symbolicLinkCreator;
106+
final IOBiFunction<Path, Path, Path> symbolicLinkCreator;
99107

100108
// We can't create symbolic links on Windows without admin privileges,
101109
// so iff that's our OS, we mock them.
102-
final String os = System.getProperty("os.name");
103-
if (os.toLowerCase().contains("windows")) {
110+
if (SystemUtils.IS_OS_WINDOWS) {
104111
symbolicLinkCreator = SymbolicLinkFileFilterTest::createMockSymbolicLink;
105112
filter = createMockFilter();
106113
} else {
@@ -123,18 +130,9 @@ static void testSetup() throws IOException {
123130
missingFile = new File(parentDirectoryPath.toFile(), MISSING);
124131
}
125132

126-
@AfterAll
127-
static void tearDown() {
128-
// Fortunately, delete() doesn't throw an exception if the file doesn't exist.
129-
testLinkDirFile.delete();
130-
targetDirFile.delete();
131-
testLinkFile.delete();
132-
testTargetFile.delete();
133-
}
134-
135133
@Test
136-
public void testSymbolicLinkFileFilter() {
137-
assertEquals(FileVisitResult.TERMINATE, SymbolicLinkFileFilter.INSTANCE.accept(PathUtils.current(), null));
134+
public void testFileFilter_HardDirectory() {
135+
assertFalse(filter.accept(targetDirFile));
138136
}
139137

140138
@Test
@@ -148,8 +146,8 @@ public void testFileFilter_Link() {
148146
}
149147

150148
@Test
151-
public void testFileFilter_HardDirectory() {
152-
assertFalse(filter.accept(targetDirFile));
149+
public void testFileFilter_missingFile() {
150+
assertFalse(filter.accept(missingFile));
153151
}
154152

155153
@Test
@@ -158,8 +156,8 @@ public void testFileFilter_PathLink() {
158156
}
159157

160158
@Test
161-
public void testFileFilter_missingFile() {
162-
assertFalse(filter.accept(missingFile));
159+
public void testFileNameFilter_HardDirectory() {
160+
assertFalse(filter.accept(parentDirectoryFile, DIRECTORY_NAME));
163161
}
164162

165163
@Test
@@ -173,8 +171,8 @@ public void testFileNameFilter_Link() {
173171
}
174172

175173
@Test
176-
public void testFileNameFilter_HardDirectory() {
177-
assertFalse(filter.accept(parentDirectoryFile, DIRECTORY_NAME));
174+
public void testFileNameFilter_missingFile() {
175+
assertFalse(filter.accept(parentDirectoryFile, MISSING));
178176
}
179177

180178
@Test
@@ -183,8 +181,8 @@ public void testFileNameFilter_PathLink() {
183181
}
184182

185183
@Test
186-
public void testFileNameFilter_missingFile() {
187-
assertFalse(filter.accept(parentDirectoryFile, MISSING));
184+
public void testPathFilter_HardDirectory() {
185+
assertEquals(FileVisitResult.TERMINATE, filter.accept(targetDirPath, null));
188186
}
189187

190188
@Test
@@ -198,8 +196,8 @@ public void testPathFilter_Link() {
198196
}
199197

200198
@Test
201-
public void testPathFilter_HardDirectory() {
202-
assertEquals(FileVisitResult.TERMINATE, filter.accept(targetDirPath, null));
199+
public void testPathFilter_missingFile() {
200+
assertEquals(FileVisitResult.TERMINATE, filter.accept(missingFile.toPath(), null));
203201
}
204202

205203
@Test
@@ -208,7 +206,7 @@ public void testPathFilter_PathLink() {
208206
}
209207

210208
@Test
211-
public void testPathFilter_missingFile() {
212-
assertEquals(FileVisitResult.TERMINATE, filter.accept(missingFile.toPath(), null));
209+
public void testSymbolicLinkFileFilter() {
210+
assertEquals(FileVisitResult.TERMINATE, SymbolicLinkFileFilter.INSTANCE.accept(PathUtils.current(), null));
213211
}
214212
}

0 commit comments

Comments
 (0)