Skip to content

Commit 683590f

Browse files
author
Gary Gregory
committed
Add a factory method and move test class to proper package.
1 parent 52e4444 commit 683590f

3 files changed

Lines changed: 35 additions & 4 deletions

File tree

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,18 @@ public static boolean isNewer(final Path file, final long timeMillis, final Link
677677
return Files.getLastModifiedTime(file, options).toMillis() > timeMillis;
678678
}
679679

680+
/**
681+
* Creates a new DirectoryStream for Paths rooted at the given directory.
682+
*
683+
* @param dir the path to the directory to stream.
684+
* @param pathFilter the directory stream filter.
685+
* @return a new instance.
686+
* @throws IOException if an I/O error occurs.
687+
*/
688+
public static DirectoryStream<Path> newDirectoryStream(final Path dir, PathFilter pathFilter) throws IOException {
689+
return Files.newDirectoryStream(dir, new DirectoryStreamFilter(pathFilter));
690+
}
691+
680692
/**
681693
* Returns true if the given options contain {@link StandardDeleteOption#OVERRIDE_READ_ONLY}.
682694
*

src/test/java/org/apache/commons/io/DirectoryStreamFilterTest.java renamed to src/test/java/org/apache/commons/io/file/DirectoryStreamFilterTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
package org.apache.commons.io;
18+
package org.apache.commons.io.file;
1919

2020
import static org.junit.jupiter.api.Assertions.assertEquals;
2121
import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -29,6 +29,7 @@
2929

3030
import org.apache.commons.io.file.DirectoryStreamFilter;
3131
import org.apache.commons.io.file.PathFilter;
32+
import org.apache.commons.io.file.PathUtils;
3233
import org.apache.commons.io.filefilter.NameFileFilter;
3334
import org.junit.jupiter.api.Test;
3435

@@ -63,4 +64,5 @@ public void testFilterByNameNot() throws Exception {
6364
}
6465
}
6566
}
67+
6668
}

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,46 @@
1818
package org.apache.commons.io.file;
1919

2020
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.assertFalse;
2122
import static org.junit.jupiter.api.Assertions.assertTrue;
2223

2324
import java.io.IOException;
25+
import java.nio.file.DirectoryStream;
2426
import java.nio.file.Files;
2527
import java.nio.file.Path;
2628
import java.nio.file.Paths;
29+
import java.util.Iterator;
2730

31+
import org.apache.commons.io.filefilter.NameFileFilter;
2832
import org.junit.jupiter.api.Test;
2933

3034
public class PathUtilsTest extends TestArguments {
3135

36+
private static final String PATH_FIXTURE = "NOTICE.txt";
37+
3238
@Test
3339
public void testCopyFile() throws IOException {
3440
final Path tempDir = Files.createTempDirectory(getClass().getCanonicalName());
3541
try {
36-
final Path sourceFile = Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1/file-size-1.bin");
37-
final Path targetFile = PathUtils.copyFileToDirectory(
38-
sourceFile, tempDir);
42+
final Path sourceFile = Paths
43+
.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1/file-size-1.bin");
44+
final Path targetFile = PathUtils.copyFileToDirectory(sourceFile, tempDir);
3945
assertTrue(Files.exists(targetFile));
4046
assertEquals(Files.size(sourceFile), Files.size(targetFile));
4147
} finally {
4248
PathUtils.deleteDirectory(tempDir);
4349
}
4450
}
4551

52+
@Test
53+
public void testNewDirectoryStream() throws Exception {
54+
final PathFilter pathFilter = new NameFileFilter(PATH_FIXTURE);
55+
try (final DirectoryStream<Path> stream = PathUtils.newDirectoryStream(Paths.get("."), pathFilter)) {
56+
final Iterator<Path> iterator = stream.iterator();
57+
final Path path = iterator.next();
58+
assertEquals(PATH_FIXTURE, path.getFileName().toString());
59+
assertFalse(iterator.hasNext());
60+
}
61+
}
62+
4663
}

0 commit comments

Comments
 (0)