changeset 1765:169ced1d9939

6879463: (file) DirectoryStream#iterator's remove method throws wrong exception when stream is closed Reviewed-by: sherman
author alanb
date Mon, 19 Oct 2009 20:01:45 +0100
parents 5c6c6627b0b7
children d71241866584
files src/solaris/classes/sun/nio/fs/UnixDirectoryStream.java src/windows/classes/sun/nio/fs/WindowsDirectoryStream.java test/java/nio/file/DirectoryStream/Basic.java
diffstat 3 files changed, 21 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/solaris/classes/sun/nio/fs/UnixDirectoryStream.java	Mon Oct 19 19:59:22 2009 +0100
+++ b/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/src/windows/classes/sun/nio/fs/WindowsDirectoryStream.java	Mon Oct 19 19:59:22 2009 +0100
+++ b/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/test/java/nio/file/DirectoryStream/Basic.java	Mon Oct 19 19:59:22 2009 +0100
+++ b/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 {