harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From py...@apache.org
Subject svn commit: r425671 - in /incubator/harmony/enhanced/classlib/trunk/modules/nio/src: main/java/org/apache/harmony/nio/internal/FileLockImpl.java test/java/common/org/apache/harmony/tests/java/nio/channels/FileLockTest.java
Date Wed, 26 Jul 2006 08:23:41 GMT
Author: pyang
Date: Wed Jul 26 01:23:41 2006
New Revision: 425671

URL: http://svn.apache.org/viewvc?rev=425671&view=rev
Log:
Fix for HARMONY-979 ([classlib][nio]java.nio.channels.FileLock.release() fails to throw ClosedChannelException
if the related file channel is closed)

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/FileLockImpl.java
    incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/common/org/apache/harmony/tests/java/nio/channels/FileLockTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/FileLockImpl.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/FileLockImpl.java?rev=425671&r1=425670&r2=425671&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/FileLockImpl.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/FileLockImpl.java
Wed Jul 26 01:23:41 2006
@@ -1,4 +1,4 @@
-/* Copyright 2004 The Apache Software Foundation or its licensors, as applicable
+/* Copyright 2006 The Apache Software Foundation or its licensors, as applicable
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
 
 
 import java.io.IOException;
+import java.nio.channels.ClosedChannelException;
 import java.nio.channels.FileChannel;
 import java.nio.channels.FileLock;
 
@@ -59,9 +60,13 @@
 	 * @see java.nio.channels.FileLock#release()
 	 */
 	public void release() throws IOException {
-		if (isValid()) {
-			((FileChannelImpl) channel()).release(this);
-			isReleased = true;
-		}
-	}
+        if (!channel().isOpen()) {
+            throw new ClosedChannelException();
+        }
+
+        if (!isReleased) {
+            ((FileChannelImpl) channel()).release(this);
+            isReleased = true;
+        }
+    }
 }

Modified: incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/common/org/apache/harmony/tests/java/nio/channels/FileLockTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/common/org/apache/harmony/tests/java/nio/channels/FileLockTest.java?rev=425671&r1=425670&r2=425671&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/common/org/apache/harmony/tests/java/nio/channels/FileLockTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/nio/src/test/java/common/org/apache/harmony/tests/java/nio/channels/FileLockTest.java
Wed Jul 26 01:23:41 2006
@@ -16,8 +16,10 @@
 package org.apache.harmony.tests.java.nio.channels;
 
 import java.io.File;
+import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.RandomAccessFile;
+import java.nio.channels.ClosedChannelException;
 import java.nio.channels.FileChannel;
 import java.nio.channels.FileLock;
 
@@ -156,4 +158,35 @@
 		fileLock.release();
 		assertFalse(fileLock.isValid());
 	}
+    
+    /**
+     * @tests java.nio.channels.FileLock#release()
+     */
+    public void test_release() throws Exception {
+        File file = File.createTempFile("test", "tmp");
+        file.deleteOnExit();
+        FileOutputStream fout = new FileOutputStream(file);
+        FileChannel fileChannel = fout.getChannel();
+        FileLock fileLock = fileChannel.lock();
+        fileChannel.close();
+        try {
+            fileLock.release();
+            fail("should throw ClosedChannelException");
+        } catch (ClosedChannelException e) {
+            // expected
+        }
+
+        // release after release
+        fout = new FileOutputStream(file);
+        fileChannel = fout.getChannel();
+        fileLock = fileChannel.lock();
+        fileLock.release();
+        fileChannel.close();
+        try {
+            fileLock.release();
+            fail("should throw ClosedChannelException");
+        } catch (ClosedChannelException e) {
+            //expected
+        }
+    }
 }



Mime
View raw message