/* * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/* @test * @bug 8006884 8019526 8132539 * @library .. * @build PassThroughFileSystem FaultyFileSystem * @run testng StreamTest * @summary Unit test for java.nio.file.Files methods that return a Stream
*/
publicvoid 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 (Stream<Path> s = Files.walk(testFolder, FileVisitOption.FOLLOW_LINKS)) {
Object[] actual = s.sorted().toArray();
assertEquals(actual, all_folowLinks);
} catch (IOException ioe) {
fail("Unexpected IOException");
}
}
// loop in descendant.
validateFileSystemLoopException(dir, cause); // loop in self
validateFileSystemLoopException(d1, cause); // start from other place via link
validateFileSystemLoopException(linkdir,
linkdir.resolve(Paths.get("d1", "lnSelf")));
Files.delete(cause);
// loop to parent.
cause = d1.resolve("lnParent");
Files.createSymbolicLink(cause, dir);
// loop should be detected at test/dir/d1/lnParent/d1
validateFileSystemLoopException(d1, cause.resolve("d1")); // loop should be detected at link
validateFileSystemLoopException(dir, cause); // loop should be detected at test/linkdir/d1/lnParent // which is test/dir we have visited via test/linkdir
validateFileSystemLoopException(linkdir,
linkdir.resolve(Paths.get("d1", "lnParent")));
Files.delete(cause);
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 (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");
} catch (IOException ioe) {
assertTrue(ioe instanceof FaultyFileSystem.FaultyException);
} catch (UncheckedIOException ex) {
fail("Top level should be repored as is");
}
} finally { // Cleanup if (fs != null) {
fs.close();
}
Files.delete(triggerFile);
TestUtil.removeAll(triggerDir);
}
}
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 (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 (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 (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 (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 (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 (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 (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 (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 (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 (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 (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");
} catch (SecurityException se) {
assertTrue(se.getCause() instanceof FaultyFileSystem.FaultyException);
}
} finally { // Cleanup if (fs != null) {
fs.close();
} if (supportsLinks) {
Files.delete(triggerLink);
Files.delete(linkTriggerDir);
Files.delete(linkTriggerFile);
}
Files.delete(triggerFile);
Files.delete(sampleFile);
Files.delete(sample);
TestUtil.removeAll(triggerDir);
}
}
publicvoid testConstructException() { 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);
}
}
publicvoid testClosedStream() throws IOException { try (Stream<Path> s = Files.list(testFolder)) {
s.close();
Object[] actual = s.sorted().toArray();
fail("Operate on closed stream should throw IllegalStateException");
} catch (IllegalStateException ex) { // expected
}
try (Stream<Path> s = Files.walk(testFolder)) {
s.close();
Object[] actual = s.sorted().toArray();
fail("Operate on closed stream should throw IllegalStateException");
} catch (IllegalStateException ex) { // expected
}
try (Stream<Path> s = Files.find(testFolder, Integer.MAX_VALUE,
(p, attr) -> true)) {
s.close();
Object[] actual = s.sorted().toArray();
fail("Operate on closed stream should throw IllegalStateException");
} catch (IllegalStateException ex) { // expected
}
}
publicvoid testProcFile() throws IOException { if (System.getProperty("os.name").equals("Linux")) {
Path path = Paths.get("/proc/cpuinfo"); if (Files.exists(path)) {
String NEW_LINE = System.getProperty("line.separator");
String s =
Files.lines(path).collect(Collectors.joining(NEW_LINE)); if (s.length() == 0) {
fail("Files.lines(\"" + path + "\") returns no data");
}
}
}
}
}
Messung V0.5
¤ Dauer der Verarbeitung: 0.3 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.