|
22 | 22 | import static org.junit.jupiter.api.Assertions.assertThrows; |
23 | 23 | import static org.junit.jupiter.api.Assertions.assertTrue; |
24 | 24 | import static org.junit.jupiter.api.Assertions.fail; |
| 25 | +import static org.junit.jupiter.api.Assumptions.assumeFalse; |
25 | 26 |
|
26 | 27 | import java.io.File; |
27 | 28 | import java.io.IOException; |
|
41 | 42 | import org.apache.commons.io.file.PathUtils; |
42 | 43 | import org.apache.commons.io.filefilter.FileFilterUtils; |
43 | 44 | import org.apache.commons.io.filefilter.IOFileFilter; |
44 | | -import org.apache.commons.io.function.Uncheck; |
| 45 | +import org.apache.commons.lang3.JavaVersion; |
| 46 | +import org.apache.commons.lang3.SystemUtils; |
45 | 47 | import org.apache.commons.lang3.function.Consumers; |
46 | 48 | import org.junit.jupiter.api.BeforeEach; |
47 | 49 | import org.junit.jupiter.api.Test; |
@@ -216,24 +218,6 @@ void testListFilesMissing() { |
216 | 218 | assertTrue(FileUtils.listFiles(new File(temporaryFolder, "dir/does/not/exist/at/all"), null, false).isEmpty()); |
217 | 219 | } |
218 | 220 |
|
219 | | - @Test |
220 | | - void testListFilesWithDeletion() throws IOException { |
221 | | - final String[] extensions = {"xml", "txt"}; |
222 | | - final List<File> list; |
223 | | - final File xFile = new File(temporaryFolder, "x.xml"); |
224 | | - if (!xFile.createNewFile()) { |
225 | | - fail("could not create test file: " + xFile); |
226 | | - } |
227 | | - final Collection<File> files = FileUtils.listFiles(temporaryFolder, extensions, true); |
228 | | - assertEquals(5, files.size()); |
229 | | - try (Stream<File> stream = Uncheck.get(() -> FileUtils.streamFiles(temporaryFolder, true, extensions))) { |
230 | | - assertTrue(xFile.delete()); |
231 | | - list = stream.collect(Collectors.toList()); |
232 | | - assertFalse(list.contains(xFile), list::toString); |
233 | | - } |
234 | | - assertEquals(4, list.size()); |
235 | | - } |
236 | | - |
237 | 221 | /** |
238 | 222 | * Tests <a href="https://issues.apache.org/jira/browse/IO-856">IO-856</a> ListFiles should not fail on vanishing files. |
239 | 223 | */ |
@@ -289,6 +273,86 @@ void testListFilesWithDeletionThreaded() throws ExecutionException, InterruptedE |
289 | 273 | c2.get(); |
290 | 274 | } |
291 | 275 |
|
| 276 | + @Test |
| 277 | + void testStreamFilesWithDeletionCollect() throws IOException { |
| 278 | + final String[] extensions = {"xml", "txt"}; |
| 279 | + final File xFile = new File(temporaryFolder, "x.xml"); |
| 280 | + if (!xFile.createNewFile()) { |
| 281 | + fail("could not create test file: " + xFile); |
| 282 | + } |
| 283 | + final Collection<File> files = FileUtils.listFiles(temporaryFolder, extensions, true); |
| 284 | + assertEquals(5, files.size()); |
| 285 | + final List<File> list; |
| 286 | + try (Stream<File> stream = FileUtils.streamFiles(temporaryFolder, true, extensions)) { |
| 287 | + assertTrue(xFile.delete()); |
| 288 | + // TODO? Should we create a custom stream to ignore missing files for Java 24 and up? |
| 289 | + // collect() will fail on Java 24 and up here |
| 290 | + // GitHub CI: |
| 291 | + // Fails on Java 24 macOS, but OK on Windows and Ubuntu |
| 292 | + // Fails on Java 25-EA Windows and macOS, but OK on Ubuntu |
| 293 | + // forEach() will fail on Java 24 and up here |
| 294 | + assumeFalse(SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_24)); |
| 295 | + list = stream.collect(Collectors.toList()); |
| 296 | + assertFalse(list.contains(xFile), list::toString); |
| 297 | + } |
| 298 | + assertEquals(4, list.size()); |
| 299 | + } |
| 300 | + |
| 301 | + @Test |
| 302 | + void testStreamFilesWithDeletionForEach() throws IOException { |
| 303 | + final String[] extensions = {"xml", "txt"}; |
| 304 | + final File xFile = new File(temporaryFolder, "x.xml"); |
| 305 | + if (!xFile.createNewFile()) { |
| 306 | + fail("could not create test file: " + xFile); |
| 307 | + } |
| 308 | + final Collection<File> files = FileUtils.listFiles(temporaryFolder, extensions, true); |
| 309 | + assertEquals(5, files.size()); |
| 310 | + final List<File> list; |
| 311 | + try (Stream<File> stream = FileUtils.streamFiles(temporaryFolder, true, extensions)) { |
| 312 | + assertTrue(xFile.delete()); |
| 313 | + list = new ArrayList<>(); |
| 314 | + // TODO? Should we create a custom stream to ignore missing files for Java 24 and up? |
| 315 | + // forEach() will fail on Java 24 and up here |
| 316 | + // GitHub CI: |
| 317 | + // Fails on Java 24 macOS, but OK on Windows and Ubuntu |
| 318 | + // Fails on Java 25-EA Windows and macOS, but OK on Ubuntu |
| 319 | + // forEach() will fail on Java 24 and up here |
| 320 | + assumeFalse(SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_24)); |
| 321 | + stream.forEach(list::add); |
| 322 | + assertFalse(list.contains(xFile), list::toString); |
| 323 | + } |
| 324 | + assertEquals(4, list.size()); |
| 325 | + } |
| 326 | + |
| 327 | + @Test |
| 328 | + void testStreamFilesWithDeletionIterator() throws IOException { |
| 329 | + final String[] extensions = {"xml", "txt"}; |
| 330 | + final File xFile = new File(temporaryFolder, "x.xml"); |
| 331 | + if (!xFile.createNewFile()) { |
| 332 | + fail("could not create test file: " + xFile); |
| 333 | + } |
| 334 | + final Collection<File> files = FileUtils.listFiles(temporaryFolder, extensions, true); |
| 335 | + assertEquals(5, files.size()); |
| 336 | + final List<File> list; |
| 337 | + try (Stream<File> stream = FileUtils.streamFiles(temporaryFolder, true, extensions)) { |
| 338 | + assertTrue(xFile.delete()); |
| 339 | + list = new ArrayList<>(); |
| 340 | + final Iterator<File> iterator = stream.iterator(); |
| 341 | + // TODO? Should we create a custom stream to ignore missing files for Java 24 and up? |
| 342 | + // hasNext() will fail on Java 24 and up here |
| 343 | + // GitHub CI: |
| 344 | + // Fails on Java 24 macOS, but OK on Windows and Ubuntu |
| 345 | + // Fails on Java 25-EA Windows and macOS, but OK on Ubuntu |
| 346 | + // forEach() will fail on Java 24 and up here |
| 347 | + assumeFalse(SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_24)); |
| 348 | + while (iterator.hasNext()) { |
| 349 | + list.add(iterator.next()); |
| 350 | + } |
| 351 | + assertFalse(list.contains(xFile), list::toString); |
| 352 | + } |
| 353 | + assertEquals(4, list.size()); |
| 354 | + } |
| 355 | + |
292 | 356 | private Collection<String> toFileNames(final Collection<File> files) { |
293 | 357 | return files.stream().map(File::getName).collect(Collectors.toList()); |
294 | 358 | } |
|
0 commit comments