Path: blob/master/test/jdk/java/nio/channels/FileLock/FileLockConstructor.java
41154 views
/*1* Copyright (c) 2015, 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*/2223import java.io.File;24import java.io.IOException;25import java.nio.channels.AsynchronousFileChannel;26import java.nio.channels.FileChannel;27import java.nio.channels.FileLock;28import java.nio.file.StandardOpenOption;2930/*31* @test32* @bug 688073733* @summary Test FileLock constructor parameter validation.34*/35public class FileLockConstructor {36public static void main(String[] args) throws IOException {37FileLock fileLock = null;38int failures = 0;3940// null FileChannel41boolean exceptionThrown = false;42try {43fileLock = new FileLockSub((FileChannel)null, 0, 0, false);44} catch (NullPointerException npe) {45exceptionThrown = true;46}47if (!exceptionThrown) {48System.err.println("FileLock constructor did not throw NPE for null FileChannel");49failures++;50}5152// null AsynchronousFileChannel53exceptionThrown = false;54try {55fileLock = new FileLockSub((AsynchronousFileChannel)null, 0, 0, true);56} catch (NullPointerException npe) {57exceptionThrown = true;58}59if (!exceptionThrown) {60System.err.println("FileLock constructor did not throw NPE for null AsynchronousFileChannel");61failures++;62}6364// create temporary file65File tmpFile = File.createTempFile("FileLock", "tmp");66tmpFile.deleteOnExit();6768// position and size preconditions69long[][] posAndSize = new long[][] {70{0, 42}, // valid71{-1, 42}, // invalid: position < 072{0, -1}, // invalid: size < 073{Long.MAX_VALUE, 1} // invalid: position + size < 074};7576// test position and size preconditions for FileChannel case77try (FileChannel syncChannel = FileChannel.open(tmpFile.toPath(),78StandardOpenOption.READ, StandardOpenOption.WRITE)) {7980for (int i = 0; i < posAndSize.length; i++) {81boolean preconditionsHold = i == 0;82exceptionThrown = false;83try {84fileLock = new FileLockSub(syncChannel, posAndSize[i][0],85posAndSize[i][1], true);86} catch (IllegalArgumentException iae) {87exceptionThrown = true;88} catch (Exception e) {89System.err.println("Unexpected exception \"" + e + "\" caught"90+ " for position " + posAndSize[i][0] + " and size "91+ posAndSize[i][1] + " for FileChannel variant");92failures++;93continue;94}95if (preconditionsHold && exceptionThrown) {96System.err.println("FileLock constructor incorrectly threw IAE"97+ " for position " + posAndSize[i][0] + " and size "98+ posAndSize[i][1] + " for FileChannel variant");99failures++;100} else if (!preconditionsHold && !exceptionThrown) {101System.err.println("FileLock constructor did not throw IAE"102+ " for position " + posAndSize[i][0] + " and size "103+ posAndSize[i][1] + " for FileChannel variant");104failures++;105}106}107}108109// test position and size preconditions for AsynchronousFileChannel case110try (AsynchronousFileChannel asyncChannel111= AsynchronousFileChannel.open(tmpFile.toPath(),112StandardOpenOption.READ, StandardOpenOption.WRITE)) {113for (int i = 0; i < posAndSize.length; i++) {114boolean preconditionsHold = i == 0;115exceptionThrown = false;116try {117fileLock = new FileLockSub(asyncChannel, posAndSize[i][0],118posAndSize[i][1], true);119} catch (IllegalArgumentException iae) {120exceptionThrown = true;121} catch (Exception e) {122System.err.println("Unexpected exception \"" + e + "\" caught"123+ " for position " + posAndSize[i][0] + " and size "124+ posAndSize[i][1] + " for AsynchronousFileChannel variant");125failures++;126continue;127}128if (preconditionsHold && exceptionThrown) {129System.err.println("FileLock constructor incorrectly threw IAE"130+ " for position " + posAndSize[i][0] + " and size "131+ posAndSize[i][1] + " for AsynchronousFileChannel variant");132failures++;133} else if (!preconditionsHold && !exceptionThrown) {134System.err.println("FileLock constructor did not throw IAE"135+ " for position " + posAndSize[i][0] + " and size "136+ posAndSize[i][1] + " for AsynchronousFileChannel variant");137failures++;138}139}140}141142if (failures > 0) {143throw new RuntimeException("Incurred " + failures +144" failures while testing FileLock.");145}146}147}148149class FileLockSub extends FileLock {150FileLockSub(FileChannel channel, long position, long size, boolean shared) {151super(channel, position, size, shared);152}153154FileLockSub(AsynchronousFileChannel channel, long position, long size,155boolean shared) {156super(channel, position, size, shared);157}158159@Override160public boolean isValid() {161return false;162}163164@Override165public void release() throws IOException {166// do nothing167}168}169170171