test/java/nio/file/Files/StreamTest.java
Print this page
rev 7633 : 8017513: Support for closeable streams
Reviewed-by:
Contributed-by: brian.goetz@oracle.com
@@ -48,11 +48,11 @@
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;
import java.util.function.BiPredicate;
-import java.util.stream.CloseableStream;
+import java.util.stream.Stream;
import java.util.stream.Collectors;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import static org.testng.Assert.*;
@@ -136,36 +136,36 @@
void cleanupTestFolder() throws IOException {
TestUtil.removeAll(testFolder);
}
public void testBasic() {
- try (CloseableStream<Path> s = Files.list(testFolder)) {
+ try (Stream<Path> s = Files.list(testFolder)) {
Object[] actual = s.sorted(Comparator.naturalOrder()).toArray();
assertEquals(actual, level1);
} catch (IOException ioe) {
fail("Unexpected IOException");
}
- try (CloseableStream<Path> s = Files.list(testFolder.resolve("empty"))) {
+ try (Stream<Path> s = Files.list(testFolder.resolve("empty"))) {
int count = s.mapToInt(p -> 1).reduce(0, Integer::sum);
assertEquals(count, 0, "Expect empty stream.");
} catch (IOException ioe) {
fail("Unexpected IOException");
}
}
public void testWalk() {
- try (CloseableStream<Path> s = Files.walk(testFolder)) {
+ try (Stream<Path> s = Files.walk(testFolder)) {
Object[] actual = s.sorted(Comparator.naturalOrder()).toArray();
assertEquals(actual, all);
} catch (IOException ioe) {
fail("Unexpected IOException");
}
}
public void testWalkOneLevel() {
- try (CloseableStream<Path> s = Files.walk(testFolder, 1)) {
+ try (Stream<Path> s = Files.walk(testFolder, 1)) {
Object[] actual = s.filter(path -> ! path.equals(testFolder))
.sorted(Comparator.naturalOrder())
.toArray();
assertEquals(actual, level1);
} catch (IOException ioe) {
@@ -174,20 +174,20 @@
}
public void testWalkFollowLink() {
// If link is not supported, the directory structure won't have link.
// We still want to test the behavior with FOLLOW_LINKS option.
- try (CloseableStream<Path> s = Files.walk(testFolder, FileVisitOption.FOLLOW_LINKS)) {
+ try (Stream<Path> s = Files.walk(testFolder, FileVisitOption.FOLLOW_LINKS)) {
Object[] actual = s.sorted(Comparator.naturalOrder()).toArray();
assertEquals(actual, all_folowLinks);
} catch (IOException ioe) {
fail("Unexpected IOException");
}
}
private void validateFileSystemLoopException(Path start, Path... causes) {
- try (CloseableStream<Path> s = Files.walk(start, FileVisitOption.FOLLOW_LINKS)) {
+ try (Stream<Path> s = Files.walk(start, FileVisitOption.FOLLOW_LINKS)) {
try {
int count = s.mapToInt(p -> 1).reduce(0, Integer::sum);
fail("Should got FileSystemLoopException, but got " + count + "elements.");
} catch (UncheckedIOException uioe) {
IOException ioe = uioe.getCause();
@@ -280,32 +280,32 @@
}
public void testFind() throws IOException {
PathBiPredicate pred = new PathBiPredicate((path, attrs) -> true);
- try (CloseableStream<Path> s = Files.find(testFolder, Integer.MAX_VALUE, pred)) {
+ try (Stream<Path> s = Files.find(testFolder, Integer.MAX_VALUE, pred)) {
Set<Path> result = s.collect(Collectors.toCollection(TreeSet::new));
assertEquals(pred.visited(), all);
assertEquals(result.toArray(new Path[0]), pred.visited());
}
pred = new PathBiPredicate((path, attrs) -> attrs.isSymbolicLink());
- try (CloseableStream<Path> s = Files.find(testFolder, Integer.MAX_VALUE, pred)) {
+ try (Stream<Path> s = Files.find(testFolder, Integer.MAX_VALUE, pred)) {
s.forEach(path -> assertTrue(Files.isSymbolicLink(path)));
assertEquals(pred.visited(), all);
}
pred = new PathBiPredicate((path, attrs) ->
path.getFileName().toString().startsWith("e"));
- try (CloseableStream<Path> s = Files.find(testFolder, Integer.MAX_VALUE, pred)) {
+ try (Stream<Path> s = Files.find(testFolder, Integer.MAX_VALUE, pred)) {
s.forEach(path -> assertEquals(path.getFileName().toString(), "empty"));
assertEquals(pred.visited(), all);
}
pred = new PathBiPredicate((path, attrs) ->
path.getFileName().toString().startsWith("l") && attrs.isRegularFile());
- try (CloseableStream<Path> s = Files.find(testFolder, Integer.MAX_VALUE, pred)) {
+ try (Stream<Path> s = Files.find(testFolder, Integer.MAX_VALUE, pred)) {
s.forEach(path -> fail("Expect empty stream"));
assertEquals(pred.visited(), all);
}
}
@@ -315,36 +315,36 @@
Path tmpfile = Files.createTempFile("blah", "txt");
try {
// zero lines
assertTrue(Files.size(tmpfile) == 0, "File should be empty");
- try (CloseableStream<String> s = Files.lines(tmpfile, US_ASCII)) {
+ try (Stream<String> s = Files.lines(tmpfile, US_ASCII)) {
assertEquals(s.mapToInt(l -> 1).reduce(0, Integer::sum), 0, "No line expected");
}
// one line
byte[] hi = { (byte)'h', (byte)'i' };
Files.write(tmpfile, hi);
- try (CloseableStream<String> s = Files.lines(tmpfile, US_ASCII)) {
+ try (Stream<String> s = Files.lines(tmpfile, US_ASCII)) {
List<String> lines = s.collect(Collectors.toList());
assertTrue(lines.size() == 1, "One line expected");
assertTrue(lines.get(0).equals("hi"), "'Hi' expected");
}
// two lines using platform's line separator
List<String> expected = Arrays.asList("hi", "there");
Files.write(tmpfile, expected, US_ASCII);
assertTrue(Files.size(tmpfile) > 0, "File is empty");
- try (CloseableStream<String> s = Files.lines(tmpfile, US_ASCII)) {
+ try (Stream<String> s = Files.lines(tmpfile, US_ASCII)) {
List<String> lines = s.collect(Collectors.toList());
assertTrue(lines.equals(expected), "Unexpected lines");
}
// MalformedInputException
byte[] bad = { (byte)0xff, (byte)0xff };
Files.write(tmpfile, bad);
- try (CloseableStream<String> s = Files.lines(tmpfile, US_ASCII)) {
+ try (Stream<String> s = Files.lines(tmpfile, US_ASCII)) {
try {
List<String> lines = s.collect(Collectors.toList());
throw new RuntimeException("UncheckedIOException expected");
} catch (UncheckedIOException ex) {
assertTrue(ex.getCause() instanceof MalformedInputException,
@@ -376,11 +376,11 @@
try {
fsp.setFaultyMode(false);
Path fakeRoot = fs.getRoot();
try {
- try (CloseableStream<Path> s = Files.list(fakeRoot)) {
+ try (Stream<Path> s = Files.list(fakeRoot)) {
s.forEach(path -> assertEquals(path.getFileName().toString(), "DirectoryIteratorException"));
}
} catch (UncheckedIOException uioe) {
fail("Unexpected exception.");
}
@@ -396,11 +396,11 @@
fail("Shoule throw DirectoryIteratorException");
} catch (DirectoryIteratorException die) {
}
try {
- try (CloseableStream<Path> s = Files.list(fakeRoot)) {
+ try (Stream<Path> s = Files.list(fakeRoot)) {
s.forEach(path -> fail("should not get here"));
}
} catch (UncheckedIOException uioe) {
assertTrue(uioe.getCause() instanceof FaultyFileSystem.FaultyException);
} catch (DirectoryIteratorException die) {
@@ -425,38 +425,38 @@
FaultyFileSystem fs = (FaultyFileSystem) fsp.newFileSystem(testFolder, null);
try {
fsp.setFaultyMode(false);
Path fakeRoot = fs.getRoot();
- try (CloseableStream<Path> s = Files.list(fakeRoot.resolve("dir2"))) {
+ try (Stream<Path> s = Files.list(fakeRoot.resolve("dir2"))) {
// only one file
s.forEach(path -> assertEquals(path.getFileName().toString(), "IOException"));
}
- try (CloseableStream<Path> s = Files.walk(fakeRoot.resolve("empty"))) {
+ try (Stream<Path> s = Files.walk(fakeRoot.resolve("empty"))) {
String[] result = s.map(path -> path.getFileName().toString())
.toArray(String[]::new);
// ordered as depth-first
assertEquals(result, new String[] { "empty", "IOException", "file"});
}
fsp.setFaultyMode(true);
- try (CloseableStream<Path> s = Files.list(fakeRoot.resolve("dir2"))) {
+ try (Stream<Path> s = Files.list(fakeRoot.resolve("dir2"))) {
s.forEach(path -> fail("should have caused exception"));
} catch (UncheckedIOException uioe) {
assertTrue(uioe.getCause() instanceof FaultyFileSystem.FaultyException);
}
- try (CloseableStream<Path> s = Files.walk(fakeRoot.resolve("empty"))) {
+ try (Stream<Path> s = Files.walk(fakeRoot.resolve("empty"))) {
String[] result = s.map(path -> path.getFileName().toString())
.toArray(String[]::new);
fail("should not reach here due to IOException");
} catch (UncheckedIOException uioe) {
assertTrue(uioe.getCause() instanceof FaultyFileSystem.FaultyException);
}
- try (CloseableStream<Path> s = Files.walk(
+ try (Stream<Path> s = Files.walk(
fakeRoot.resolve("empty").resolve("IOException")))
{
String[] result = s.map(path -> path.getFileName().toString())
.toArray(String[]::new);
fail("should not reach here due to IOException");
@@ -500,109 +500,109 @@
try {
fsp.setFaultyMode(false);
Path fakeRoot = fs.getRoot();
// validate setting
- try (CloseableStream<Path> s = Files.list(fakeRoot.resolve("empty"))) {
+ try (Stream<Path> s = Files.list(fakeRoot.resolve("empty"))) {
String[] result = s.map(path -> path.getFileName().toString())
.toArray(String[]::new);
assertEqualsNoOrder(result, new String[] { "SecurityException", "sample" });
}
- try (CloseableStream<Path> s = Files.walk(fakeRoot.resolve("dir2"))) {
+ try (Stream<Path> s = Files.walk(fakeRoot.resolve("dir2"))) {
String[] result = s.map(path -> path.getFileName().toString())
.toArray(String[]::new);
assertEqualsNoOrder(result, new String[] { "dir2", "SecurityException", "fileInSE", "file" });
}
if (supportsLinks) {
- try (CloseableStream<Path> s = Files.list(fakeRoot.resolve("dir"))) {
+ try (Stream<Path> s = Files.list(fakeRoot.resolve("dir"))) {
String[] result = s.map(path -> path.getFileName().toString())
.toArray(String[]::new);
assertEqualsNoOrder(result, new String[] { "d1", "f1", "lnDir2", "SecurityException", "lnDirSE", "lnFileSE" });
}
}
// execute test
fsp.setFaultyMode(true);
// ignore file cause SecurityException
- try (CloseableStream<Path> s = Files.walk(fakeRoot.resolve("empty"))) {
+ try (Stream<Path> s = Files.walk(fakeRoot.resolve("empty"))) {
String[] result = s.map(path -> path.getFileName().toString())
.toArray(String[]::new);
assertEqualsNoOrder(result, new String[] { "empty", "sample" });
}
// skip folder cause SecurityException
- try (CloseableStream<Path> s = Files.walk(fakeRoot.resolve("dir2"))) {
+ try (Stream<Path> s = Files.walk(fakeRoot.resolve("dir2"))) {
String[] result = s.map(path -> path.getFileName().toString())
.toArray(String[]::new);
assertEqualsNoOrder(result, new String[] { "dir2", "file" });
}
if (supportsLinks) {
// not following links
- try (CloseableStream<Path> s = Files.walk(fakeRoot.resolve("dir"))) {
+ try (Stream<Path> s = Files.walk(fakeRoot.resolve("dir"))) {
String[] result = s.map(path -> path.getFileName().toString())
.toArray(String[]::new);
assertEqualsNoOrder(result, new String[] { "dir", "d1", "f1", "lnDir2", "lnDirSE", "lnFileSE" });
}
// following links
- try (CloseableStream<Path> s = Files.walk(fakeRoot.resolve("dir"), FileVisitOption.FOLLOW_LINKS)) {
+ try (Stream<Path> s = Files.walk(fakeRoot.resolve("dir"), FileVisitOption.FOLLOW_LINKS)) {
String[] result = s.map(path -> path.getFileName().toString())
.toArray(String[]::new);
// ?? Should fileInSE show up?
// With FaultyFS, it does as no exception thrown for link to "SecurityException" with read on "lnXxxSE"
assertEqualsNoOrder(result, new String[] { "dir", "d1", "f1", "lnDir2", "file", "lnDirSE", "lnFileSE", "fileInSE" });
}
}
// list instead of walk
- try (CloseableStream<Path> s = Files.list(fakeRoot.resolve("empty"))) {
+ try (Stream<Path> s = Files.list(fakeRoot.resolve("empty"))) {
String[] result = s.map(path -> path.getFileName().toString())
.toArray(String[]::new);
assertEqualsNoOrder(result, new String[] { "sample" });
}
- try (CloseableStream<Path> s = Files.list(fakeRoot.resolve("dir2"))) {
+ try (Stream<Path> s = Files.list(fakeRoot.resolve("dir2"))) {
String[] result = s.map(path -> path.getFileName().toString())
.toArray(String[]::new);
assertEqualsNoOrder(result, new String[] { "file" });
}
// root cause SecurityException should be reported
- try (CloseableStream<Path> s = Files.walk(
+ try (Stream<Path> s = Files.walk(
fakeRoot.resolve("dir2").resolve("SecurityException")))
{
String[] result = s.map(path -> path.getFileName().toString())
.toArray(String[]::new);
fail("should not reach here due to SecurityException");
} catch (SecurityException se) {
assertTrue(se.getCause() instanceof FaultyFileSystem.FaultyException);
}
// Walk a file cause SecurityException, we should get SE
- try (CloseableStream<Path> s = Files.walk(
+ try (Stream<Path> s = Files.walk(
fakeRoot.resolve("dir").resolve("SecurityException")))
{
String[] result = s.map(path -> path.getFileName().toString())
.toArray(String[]::new);
fail("should not reach here due to SecurityException");
} catch (SecurityException se) {
assertTrue(se.getCause() instanceof FaultyFileSystem.FaultyException);
}
// List a file cause SecurityException, we should get SE as cannot read attribute
- try (CloseableStream<Path> s = Files.list(
+ try (Stream<Path> s = Files.list(
fakeRoot.resolve("dir2").resolve("SecurityException")))
{
String[] result = s.map(path -> path.getFileName().toString())
.toArray(String[]::new);
fail("should not reach here due to SecurityException");
} catch (SecurityException se) {
assertTrue(se.getCause() instanceof FaultyFileSystem.FaultyException);
}
- try (CloseableStream<Path> s = Files.list(
+ try (Stream<Path> s = Files.list(
fakeRoot.resolve("dir").resolve("SecurityException")))
{
String[] result = s.map(path -> path.getFileName().toString())
.toArray(String[]::new);
fail("should not reach here due to SecurityException");
@@ -625,33 +625,35 @@
TestUtil.removeAll(triggerDir);
}
}
public void testConstructException() {
- try (CloseableStream<String> s = Files.lines(testFolder.resolve("notExist"), Charset.forName("UTF-8"))) {
+ try (Stream<String> s = Files.lines(testFolder.resolve("notExist"), Charset.forName("UTF-8"))) {
s.forEach(l -> fail("File is not even exist!"));
} catch (IOException ioe) {
assertTrue(ioe instanceof NoSuchFileException);
}
}
public void testClosedStream() throws IOException {
- try (CloseableStream<Path> s = Files.list(testFolder)) {
+ try (Stream<Path> s = Files.list(testFolder)) {
s.close();
Object[] actual = s.sorted(Comparator.naturalOrder()).toArray();
- assertTrue(actual.length <= level1.length);
+ fail("Operate on closed stream should throw IllegalStateException");
+ } catch (IllegalStateException ex) {
+ // expected
}
- try (CloseableStream<Path> s = Files.walk(testFolder)) {
+ try (Stream<Path> s = Files.walk(testFolder)) {
s.close();
Object[] actual = s.sorted(Comparator.naturalOrder()).toArray();
fail("Operate on closed stream should throw IllegalStateException");
} catch (IllegalStateException ex) {
// expected
}
- try (CloseableStream<Path> s = Files.find(testFolder, Integer.MAX_VALUE,
+ try (Stream<Path> s = Files.find(testFolder, Integer.MAX_VALUE,
(p, attr) -> true)) {
s.close();
Object[] actual = s.sorted(Comparator.naturalOrder()).toArray();
fail("Operate on closed stream should throw IllegalStateException");
} catch (IllegalStateException ex) {