test/java/nio/file/Files/StreamTest.java

Print this page
rev 8567 : 8014855: TEST_BUG: java/nio/file/Files/StreamTest.java fails when sym links not supported
Reviewed-by:
Contributed-by: henry.jen@oracle.com

@@ -474,58 +474,94 @@
             TestUtil.removeAll(triggerDir);
         }
     }
 
     public void testSecurityException() throws IOException {
-        Path triggerFile = testFolder.resolve(Paths.get("dir", "SecurityException"));
-        Files.createFile(triggerFile);
-        Path sampleFile = testFolder.resolve(Paths.get("dir", "sample"));
-        Files.createFile(sampleFile);
-        Path triggerDir = testFolder.resolve(Paths.get("dir2", "SecurityException"));
-        Files.createDirectories(triggerDir);
+        Path empty = testFolder.resolve("empty");
+        Path triggerFile = Files.createFile(empty.resolve("SecurityException"));
+        Path sampleFile = Files.createDirectories(empty.resolve("sample"));
+
+        Path dir2 = testFolder.resolve("dir2");
+        Path triggerDir = Files.createDirectories(dir2.resolve("SecurityException"));
         Files.createFile(triggerDir.resolve("fileInSE"));
-        Path sample = testFolder.resolve(Paths.get("dir2", "file"));
-        Files.createFile(sample);
+        Path sample = Files.createFile(dir2.resolve("file"));
+
+        Path triggerLink = null;
+        Path linkTriggerDir = null;
+        Path linkTriggerFile = null;
+        if (supportsLinks) {
+            Path dir = testFolder.resolve("dir");
+            triggerLink = Files.createSymbolicLink(dir.resolve("SecurityException"), empty);
+            linkTriggerDir = Files.createSymbolicLink(dir.resolve("lnDirSE"), triggerDir);
+            linkTriggerFile = Files.createSymbolicLink(dir.resolve("lnFileSE"), triggerFile);
+        }
+
         FaultyFileSystem.FaultyFSProvider fsp = FaultyFileSystem.FaultyFSProvider.getInstance();
         FaultyFileSystem fs = (FaultyFileSystem) fsp.newFileSystem(testFolder, null);
 
         try {
             fsp.setFaultyMode(false);
             Path fakeRoot = fs.getRoot();
             // validate setting
-            try (CloseableStream<Path> s = Files.list(fakeRoot.resolve("dir"))) {
+            try (CloseableStream<Path> s = Files.list(fakeRoot.resolve("empty"))) {
                 String[] result = s.map(path -> path.getFileName().toString())
                                    .toArray(String[]::new);
-                assertEqualsNoOrder(result, new String[] { "d1","f1", "lnDir2", "SecurityException", "sample" });
+                assertEqualsNoOrder(result, new String[] { "SecurityException", "sample" });
             }
 
             try (CloseableStream<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"))) {
+                    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("dir"))) {
+            try (CloseableStream<Path> s = Files.walk(fakeRoot.resolve("empty"))) {
                 String[] result = s.map(path -> path.getFileName().toString())
                                    .toArray(String[]::new);
-                assertEqualsNoOrder(result, new String[] { "dir", "d1","f1", "lnDir2", "sample" });
+                assertEqualsNoOrder(result, new String[] { "empty", "sample" });
             }
             // skip folder cause SecurityException
             try (CloseableStream<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"))) {
+                    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)) {
+                    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("dir"))) {
+            try (CloseableStream<Path> s = Files.list(fakeRoot.resolve("empty"))) {
                 String[] result = s.map(path -> path.getFileName().toString())
                                    .toArray(String[]::new);
-                assertEqualsNoOrder(result, new String[] { "d1","f1", "lnDir2", "sample" });
+                assertEqualsNoOrder(result, new String[] { "sample" });
             }
             try (CloseableStream<Path> s = Files.list(fakeRoot.resolve("dir2"))) {
                 String[] result = s.map(path -> path.getFileName().toString())
                                    .toArray(String[]::new);
                 assertEqualsNoOrder(result, new String[] { "file" });

@@ -576,10 +612,15 @@
          } 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);
         }

@@ -587,11 +628,10 @@
 
     public void testConstructException() {
         try (CloseableStream<String> s = Files.lines(testFolder.resolve("notExist"), Charset.forName("UTF-8"))) {
             s.forEach(l -> fail("File is not even exist!"));
         } catch (IOException ioe) {
-            ioe.printStackTrace(System.err);
             assertTrue(ioe instanceof NoSuchFileException);
         }
     }
 
     public void testClosedStream() throws IOException {