6879463: (file) DirectoryStream#iterator's remove method throws wrong exception when stream is closed
authoralanb
Mon, 19 Oct 2009 20:01:45 +0100
changeset 4058 21c6bd5381ca
parent 4057 6b1c7ed677cf
child 4059 e838ac118185
6879463: (file) DirectoryStream#iterator's remove method throws wrong exception when stream is closed Reviewed-by: sherman
jdk/src/solaris/classes/sun/nio/fs/UnixDirectoryStream.java
jdk/src/windows/classes/sun/nio/fs/WindowsDirectoryStream.java
jdk/test/java/nio/file/DirectoryStream/Basic.java
--- a/jdk/src/solaris/classes/sun/nio/fs/UnixDirectoryStream.java	Mon Oct 19 19:59:22 2009 +0100
+++ b/jdk/src/solaris/classes/sun/nio/fs/UnixDirectoryStream.java	Mon Oct 19 20:01:45 2009 +0100
@@ -235,7 +235,8 @@
         @Override
         public void remove() {
             if (isClosed) {
-                throw new ClosedDirectoryStreamException();
+                throwAsConcurrentModificationException(new
+                    ClosedDirectoryStreamException());
             }
             Path entry;
             synchronized (this) {
--- a/jdk/src/windows/classes/sun/nio/fs/WindowsDirectoryStream.java	Mon Oct 19 19:59:22 2009 +0100
+++ b/jdk/src/windows/classes/sun/nio/fs/WindowsDirectoryStream.java	Mon Oct 19 20:01:45 2009 +0100
@@ -179,7 +179,7 @@
                 synchronized (closeLock) {
                     if (!isOpen)
                         throwAsConcurrentModificationException(new
-                            IllegalStateException("Directory stream is closed"));
+                            ClosedDirectoryStreamException());
                     try {
                         name = FindNextFile(handle, findDataBuffer.address());
                         if (name == null) {
@@ -236,7 +236,8 @@
         @Override
         public void remove() {
             if (!isOpen) {
-                throw new IllegalStateException("Directory stream is closed");
+                throwAsConcurrentModificationException(new
+                    ClosedDirectoryStreamException());
             }
             Path entry;
             synchronized (this) {
--- a/jdk/test/java/nio/file/DirectoryStream/Basic.java	Mon Oct 19 19:59:22 2009 +0100
+++ b/jdk/test/java/nio/file/DirectoryStream/Basic.java	Mon Oct 19 20:01:45 2009 +0100
@@ -154,8 +154,10 @@
         stream.close();
 
         // test IllegalStateException
+        dir.resolve(foo).createFile();
         stream =  dir.newDirectoryStream();
         i = stream.iterator();
+        i.next();
         try {
             stream.iterator();
             throw new RuntimeException("IllegalStateException not thrown as expected");
@@ -172,17 +174,26 @@
             throw new RuntimeException("ConcurrentModificationException not thrown as expected");
         } catch (ConcurrentModificationException x) {
             Throwable t = x.getCause();
-            if (!(t instanceof IllegalStateException))
-                throw new RuntimeException("Cause is not IllegalStateException as expected");
+            if (!(t instanceof ClosedDirectoryStreamException))
+                throw new RuntimeException("Cause is not ClosedDirectoryStreamException as expected");
         }
         try {
             i.next();
-            throw new RuntimeException("IllegalStateException not thrown as expected");
+            throw new RuntimeException("ConcurrentModificationException not thrown as expected");
         } catch (ConcurrentModificationException x) {
             Throwable t = x.getCause();
-            if (!(t instanceof IllegalStateException))
-                throw new RuntimeException("Cause is not IllegalStateException as expected");
+            if (!(t instanceof ClosedDirectoryStreamException))
+                throw new RuntimeException("Cause is not ClosedDirectoryStreamException as expected");
         }
+        try {
+            i.remove();
+            throw new RuntimeException("ConcurrentModificationException not thrown as expected");
+        } catch (ConcurrentModificationException x) {
+            Throwable t = x.getCause();
+            if (!(t instanceof ClosedDirectoryStreamException))
+                throw new RuntimeException("Cause is not ClosedDirectoryStreamException as expected");
+        }
+
     }
 
     public static void main(String[] args) throws IOException {