Path: blob/master/test/jdk/java/nio/channels/FileChannel/TryLock.java
41154 views
/*1* Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 4486154 449572926* @summary The FileChannel file locking27*/2829import java.io.*;30import java.nio.channels.*;31import java.nio.*;3233/**34* Testing FileChannel's locking methods.35*/3637public class TryLock {3839public static void main(String[] args) throws Exception {40test1(true, true);41test1(false, true);42test1(true, false);43test1(false, false);4445test2(true, true);46test2(false, true);47test2(true, false);48test2(false, false);4950test3(true, true);51test3(false, true);52test3(true, false);53test3(false, false);54}5556public static void test1(boolean shared, boolean trylock) throws Exception {57File testFile = File.createTempFile("test1", null);58FileInputStream fis = new FileInputStream(testFile);59FileChannel fc = fis.getChannel();60FileLock fl = null;61try {62if (trylock)63fl = fc.tryLock(0, fc.size(), shared);64else65fl = fc.lock(0, fc.size(), shared);66if (!shared)67throw new RuntimeException("No exception thrown for test1");68} catch (NonWritableChannelException e) {69if (shared)70throw new RuntimeException("Exception thrown for wrong case test1");71} finally {72if (fl != null)73fl.release();74fc.close();75testFile.delete();76}77}7879public static void test2(boolean shared, boolean trylock) throws Exception {80File testFile = File.createTempFile("test2", null);81FileOutputStream fis = new FileOutputStream(testFile);82FileChannel fc = fis.getChannel();83FileLock fl = null;84try {85if (trylock)86fl = fc.tryLock(0, fc.size(), shared);87else88fl = fc.lock(0, fc.size(), shared);89if (shared)90throw new RuntimeException("No exception thrown for test2");91} catch (NonReadableChannelException e) {92if (!shared)93throw new RuntimeException("Exception thrown incorrectly for test2");94} finally {95if (fl != null)96fl.release();97fc.close();98testFile.delete();99}100}101102public static void test3(boolean shared, boolean trylock) throws Exception {103File testFile = File.createTempFile("test3", null);104RandomAccessFile fis = new RandomAccessFile(testFile, "rw");105FileChannel fc = fis.getChannel();106try {107FileLock fl = null;108if (trylock)109fl = fc.tryLock(0, fc.size(), shared);110else111fl = fc.lock(0, fc.size(), shared);112fl.release();113} finally {114fc.close();115testFile.delete();116}117}118}119120121