changeset 8460:6cf79416eec1

Rename DirectoryStream::entries to stream Clarify operated on a closed stream in Files throws IllegalStateException
author henryjen
date Fri, 03 May 2013 16:22:49 -0700
parents 3f0550ac4e9b
children 38379cea9127
files src/share/classes/java/nio/file/DirectoryStream.java src/share/classes/java/nio/file/Files.java test/java/nio/file/DirectoryStream/Entries.java test/java/nio/file/DirectoryStream/StreamTest.java
diffstat 4 files changed, 202 insertions(+), 200 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/nio/file/DirectoryStream.java	Fri May 03 11:14:15 2013 -0700
+++ b/src/share/classes/java/nio/file/DirectoryStream.java	Fri May 03 16:22:49 2013 -0700
@@ -41,7 +41,7 @@
  * <p> <b> While {@code DirectoryStream} extends {@code Iterable}, it is not a
  * general-purpose {@code Iterable}. A {@code DirectoryStream} supports only a
  * single iteration via either the {@link #iterator iterator} or the {@link
- * #entries entries} method. Invoking either method to do a second or
+ * #stream stream} method. Invoking either method to do a second or
  * subsequent iteration throws {@code IllegalStateException}. </b>
  *
  * <p> An important property of the directory stream's {@code Iterator} is that
@@ -173,7 +173,7 @@
      *         has already been returned
      * @since 1.8
      */
-    default Stream<T> entries() {
+    default Stream<T> stream() {
         return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator(), Spliterator.DISTINCT));
     }
 }
--- a/src/share/classes/java/nio/file/Files.java	Fri May 03 11:14:15 2013 -0700
+++ b/src/share/classes/java/nio/file/Files.java	Fri May 03 16:22:49 2013 -0700
@@ -3224,7 +3224,8 @@
      * <p> When not using the try-with-resources construct, then the stream's
      * {@link CloseableStream#close close} method should be invoked after the
      * operation is completed so as to free any resources held for the open
-     * directory.
+     * directory. Operate the stream after it is closed will throw an
+     * {@link java.lang.IllegalStateException}.
      *
      * <p> If an {@link IOException} is thrown when accessing the directory
      * after this method has returned, it is wrapped in an {@link
@@ -3247,7 +3248,7 @@
      *          method is invoked to check read access to the directory.
      *
      * @see     #newDirectoryStream(Path)
-     * @see     DirectoryStream#entries()
+     * @see     DirectoryStream#stream()
      * @since   1.8
      */
     public static CloseableStream<Path> list(Path dir) throws IOException {
@@ -3328,7 +3329,8 @@
      * <p> When not using the try-with-resources construct, then the stream's
      * {@link CloseableStream#close close} method should be invoked after the
      * operation is completed so as to free any resources held for the open
-     * directory.
+     * directory. Operate the stream after it is closed will throw an
+     * {@link java.lang.IllegalStateException}.
      *
      * <p> If an {@link IOException} is thrown when accessing the directory
      * after this method has returned, it is wrapped in an {@link
--- a/test/java/nio/file/DirectoryStream/Entries.java	Fri May 03 11:14:15 2013 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,195 +0,0 @@
-/*
- * Copyright (c) 2013, 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
- * @summary Unit test for java.nio.file.DirectoyStream
- * @library ..
- * @run testng Entries
- */
-
-import java.nio.file.DirectoryStream;
-import java.nio.file.DirectoryIteratorException;
-import java.nio.file.NotDirectoryException;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.io.IOException;
-import java.util.TreeSet;
-import java.util.Comparators;
-import java.util.stream.Stream;
-import java.util.Iterator;
-import org.testng.annotations.Test;
-import org.testng.annotations.BeforeClass;
-import org.testng.annotations.AfterClass;
-import static org.testng.Assert.*;
-
-@Test(groups = "unit")
-public class Entries {
-    /**
-     * Default test folder
-     * testFolder - empty
-     *            - file
-     *            - dir - d1
-     *                  - f1
-     *                  - lnDir2 (../dir2)
-     *            - dir2
-     *            - linkDir (./dir)
-     *            - linkFile(./file)
-     */
-    static Path testFolder;
-    static boolean supportsLinks;
-    static Path[] level1;
-    static Path[] level2;
-
-    @BeforeClass
-    void setupTestFolder() throws IOException {
-        testFolder = TestUtil.createTemporaryDirectory();
-        supportsLinks = TestUtil.supportsLinks(testFolder);
-        TreeSet<Path> set = new TreeSet<>();
-
-        // Level 1
-        Path empty = testFolder.resolve("empty");
-        Path file = testFolder.resolve("file");
-        Path dir = testFolder.resolve("dir");
-        Path dir2 = testFolder.resolve("dir2");
-        Files.createDirectory(empty);
-        Files.createFile(file);
-        Files.createDirectory(dir);
-        Files.createDirectory(dir2);
-        set.add(empty);
-        set.add(file);
-        set.add(dir);
-        set.add(dir2);
-        if (supportsLinks) {
-            Path tmp = testFolder.resolve("linkDir");
-            Files.createSymbolicLink(tmp, dir);
-            set.add(tmp);
-            tmp = testFolder.resolve("linkFile");
-            Files.createSymbolicLink(tmp, file);
-            set.add(tmp);
-        }
-        level1 = set.toArray(new Path[0]);
-
-        set.clear();
-        // Level 2
-        Path tmp = dir.resolve("d1");
-        Files.createDirectory(tmp);
-        set.add(tmp);
-        tmp = dir.resolve("f1");
-        Files.createFile(tmp);
-        set.add(tmp);
-        if (supportsLinks) {
-            tmp = dir.resolve("lnDir2");
-            Files.createSymbolicLink(tmp, dir2);
-            set.add(tmp);
-        }
-        level2 = set.toArray(new Path[0]);
-    }
-
-    @AfterClass
-    void cleanupTestFolder() throws IOException {
-        TestUtil.removeAll(testFolder);
-    }
-
-    public void testEntriesWithoutFilter() throws IOException {
-        try (DirectoryStream<Path> ds = Files.newDirectoryStream(testFolder)) {
-            Path[] actual = ds.entries().sorted(Comparators.naturalOrder()).toArray(Path[]::new);
-            assertEquals(actual, level1);
-        }
-
-        // link to a directory
-        if (supportsLinks) {
-            try (DirectoryStream<Path> ds = Files.newDirectoryStream(testFolder.resolve("linkDir"))) {
-                Path[] actual = ds.entries().sorted(Comparators.naturalOrder()).toArray(Path[]::new);
-                assertEquals(actual.length, level2.length);
-                for (int i = 0; i < actual.length; i++) {
-                    assertEquals(actual[i].getFileName(), level2[i].getFileName());
-                }
-            }
-        }
-    }
-
-    public void testEntriesWithFilter() throws IOException {
-        try (DirectoryStream<Path> ds = Files.newDirectoryStream(testFolder, "f*")) {
-            Path[] actual = ds.entries().toArray(Path[]::new);
-            assertEquals(actual.length, 1);
-            assertEquals(actual[0], testFolder.resolve("file"));
-        }
-
-        try (DirectoryStream<Path> ds = Files.newDirectoryStream(testFolder, "z*")) {
-            int count = ds.entries().mapToInt(p -> 1).reduce(0, Integer::sum);
-            assertEquals(count, 0, "Expect empty stream.");
-        }
-    }
-
-    public void testDirectoryIteratorException() throws IOException {
-        // check that an IOException thrown by a filter is propagated
-        DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<Path>() {
-            public boolean accept(Path file) throws IOException {
-                throw new java.util.zip.ZipException();
-            }
-        };
-
-        try (DirectoryStream<Path> ds = Files.newDirectoryStream(testFolder, filter)) {
-            Path[] actual = ds.entries().toArray(Path[]::new);
-            throw new RuntimeException("DirectoryIteratorException expected");
-        } catch (DirectoryIteratorException x) {
-            IOException cause = x.getCause();
-            if (!(cause instanceof java.util.zip.ZipException))
-                throw new RuntimeException("Expected IOException not propagated");
-        }
-    }
-
-    public void testEmptyFolder() throws IOException {
-        try (DirectoryStream<Path> ds = Files.newDirectoryStream(testFolder.resolve("empty"))) {
-            int count = ds.entries().mapToInt(p -> 1).reduce(0, Integer::sum);
-            assertEquals(count, 0, "Expect empty stream.");
-        }
-    }
-
-    public void testIllegalStateException() throws IOException {
-        try (DirectoryStream<Path> ds = Files.newDirectoryStream(testFolder)) {
-            Stream<Path> s = ds.entries();
-            try {
-                ds.iterator();
-                fail("Expect IllegalStateException from iterator() call.");
-            } catch (IllegalStateException ise1) {}
-        }
-
-        try (DirectoryStream<Path> ds = Files.newDirectoryStream(testFolder)) {
-            Iterator<Path> it = ds.iterator();
-            try {
-                ds.entries();
-                fail("Expect IllegalStateException from entries() call.");
-            } catch (IllegalStateException ise2) {}
-        }
-    }
-
-    public void testNotDirectoryException() throws IOException {
-        try {
-            Files.newDirectoryStream(testFolder.resolve("file"));
-            throw new RuntimeException("NotDirectoryException not thrown");
-        } catch (NotDirectoryException x) {
-        }
-    }
-}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/java/nio/file/DirectoryStream/StreamTest.java	Fri May 03 16:22:49 2013 -0700
@@ -0,0 +1,195 @@
+/*
+ * Copyright (c) 2013, 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
+ * @summary Unit test for java.nio.file.DirectoyStream
+ * @library ..
+ * @run testng StreamTest
+ */
+
+import java.nio.file.DirectoryStream;
+import java.nio.file.DirectoryIteratorException;
+import java.nio.file.NotDirectoryException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.io.IOException;
+import java.util.TreeSet;
+import java.util.Comparators;
+import java.util.stream.Stream;
+import java.util.Iterator;
+import org.testng.annotations.Test;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.AfterClass;
+import static org.testng.Assert.*;
+
+@Test(groups = "unit")
+public class StreamTest {
+    /**
+     * Default test folder
+     * testFolder - empty
+     *            - file
+     *            - dir - d1
+     *                  - f1
+     *                  - lnDir2 (../dir2)
+     *            - dir2
+     *            - linkDir (./dir)
+     *            - linkFile(./file)
+     */
+    static Path testFolder;
+    static boolean supportsLinks;
+    static Path[] level1;
+    static Path[] level2;
+
+    @BeforeClass
+    void setupTestFolder() throws IOException {
+        testFolder = TestUtil.createTemporaryDirectory();
+        supportsLinks = TestUtil.supportsLinks(testFolder);
+        TreeSet<Path> set = new TreeSet<>();
+
+        // Level 1
+        Path empty = testFolder.resolve("empty");
+        Path file = testFolder.resolve("file");
+        Path dir = testFolder.resolve("dir");
+        Path dir2 = testFolder.resolve("dir2");
+        Files.createDirectory(empty);
+        Files.createFile(file);
+        Files.createDirectory(dir);
+        Files.createDirectory(dir2);
+        set.add(empty);
+        set.add(file);
+        set.add(dir);
+        set.add(dir2);
+        if (supportsLinks) {
+            Path tmp = testFolder.resolve("linkDir");
+            Files.createSymbolicLink(tmp, dir);
+            set.add(tmp);
+            tmp = testFolder.resolve("linkFile");
+            Files.createSymbolicLink(tmp, file);
+            set.add(tmp);
+        }
+        level1 = set.toArray(new Path[0]);
+
+        set.clear();
+        // Level 2
+        Path tmp = dir.resolve("d1");
+        Files.createDirectory(tmp);
+        set.add(tmp);
+        tmp = dir.resolve("f1");
+        Files.createFile(tmp);
+        set.add(tmp);
+        if (supportsLinks) {
+            tmp = dir.resolve("lnDir2");
+            Files.createSymbolicLink(tmp, dir2);
+            set.add(tmp);
+        }
+        level2 = set.toArray(new Path[0]);
+    }
+
+    @AfterClass
+    void cleanupTestFolder() throws IOException {
+        TestUtil.removeAll(testFolder);
+    }
+
+    public void testEntriesWithoutFilter() throws IOException {
+        try (DirectoryStream<Path> ds = Files.newDirectoryStream(testFolder)) {
+            Path[] actual = ds.stream().sorted(Comparators.naturalOrder()).toArray(Path[]::new);
+            assertEquals(actual, level1);
+        }
+
+        // link to a directory
+        if (supportsLinks) {
+            try (DirectoryStream<Path> ds = Files.newDirectoryStream(testFolder.resolve("linkDir"))) {
+                Path[] actual = ds.stream().sorted(Comparators.naturalOrder()).toArray(Path[]::new);
+                assertEquals(actual.length, level2.length);
+                for (int i = 0; i < actual.length; i++) {
+                    assertEquals(actual[i].getFileName(), level2[i].getFileName());
+                }
+            }
+        }
+    }
+
+    public void testEntriesWithFilter() throws IOException {
+        try (DirectoryStream<Path> ds = Files.newDirectoryStream(testFolder, "f*")) {
+            Path[] actual = ds.stream().toArray(Path[]::new);
+            assertEquals(actual.length, 1);
+            assertEquals(actual[0], testFolder.resolve("file"));
+        }
+
+        try (DirectoryStream<Path> ds = Files.newDirectoryStream(testFolder, "z*")) {
+            int count = ds.stream().mapToInt(p -> 1).reduce(0, Integer::sum);
+            assertEquals(count, 0, "Expect empty stream.");
+        }
+    }
+
+    public void testDirectoryIteratorException() throws IOException {
+        // check that an IOException thrown by a filter is propagated
+        DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<Path>() {
+            public boolean accept(Path file) throws IOException {
+                throw new java.util.zip.ZipException();
+            }
+        };
+
+        try (DirectoryStream<Path> ds = Files.newDirectoryStream(testFolder, filter)) {
+            Path[] actual = ds.stream().toArray(Path[]::new);
+            throw new RuntimeException("DirectoryIteratorException expected");
+        } catch (DirectoryIteratorException x) {
+            IOException cause = x.getCause();
+            if (!(cause instanceof java.util.zip.ZipException))
+                throw new RuntimeException("Expected IOException not propagated");
+        }
+    }
+
+    public void testEmptyFolder() throws IOException {
+        try (DirectoryStream<Path> ds = Files.newDirectoryStream(testFolder.resolve("empty"))) {
+            int count = ds.stream().mapToInt(p -> 1).reduce(0, Integer::sum);
+            assertEquals(count, 0, "Expect empty stream.");
+        }
+    }
+
+    public void testIllegalStateException() throws IOException {
+        try (DirectoryStream<Path> ds = Files.newDirectoryStream(testFolder)) {
+            Stream<Path> s = ds.stream();
+            try {
+                ds.iterator();
+                fail("Expect IllegalStateException from iterator() call.");
+            } catch (IllegalStateException ise1) {}
+        }
+
+        try (DirectoryStream<Path> ds = Files.newDirectoryStream(testFolder)) {
+            Iterator<Path> it = ds.iterator();
+            try {
+                ds.stream();
+                fail("Expect IllegalStateException from stream() call.");
+            } catch (IllegalStateException ise2) {}
+        }
+    }
+
+    public void testNotDirectoryException() throws IOException {
+        try {
+            Files.newDirectoryStream(testFolder.resolve("file"));
+            throw new RuntimeException("NotDirectoryException not thrown");
+        } catch (NotDirectoryException x) {
+        }
+    }
+}
\ No newline at end of file