Path: blob/master/test/jdk/java/nio/channels/FileChannel/Args.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/* @test24* @bug 447001725* @summary Ensure that illegal arguments cause appropriate exceptions26* to be thrown27*/2829import java.io.*;30import java.nio.channels.*;313233public class Args {3435static void fail(String s) {36throw new RuntimeException(s);37}3839static interface Thunk {40public void run() throws Exception;41}4243private static void tryCatch(Class ex, Thunk thunk) {44boolean caught = false;45try {46thunk.run();47} catch (Throwable x) {48if (ex.isAssignableFrom(x.getClass())) {49caught = true;50System.err.println("Thrown as expected: " + x);51}52}53if (!caught)54fail(ex.getName() + " not thrown");55}5657public static void main(String[] args) throws Exception {5859File f = File.createTempFile("foo", null);60f.deleteOnExit();61final FileChannel fc = new RandomAccessFile(f, "rw").getChannel();6263tryCatch(IllegalArgumentException.class, new Thunk() {64public void run() throws Exception {65fc.transferFrom(fc, -1, 1);66}});6768tryCatch(IllegalArgumentException.class, new Thunk() {69public void run() throws Exception {70fc.transferFrom(fc, 0, -1);71}});7273tryCatch(IllegalArgumentException.class, new Thunk() {74public void run() throws Exception {75fc.transferTo(-1, 1, fc);76}});7778tryCatch(IllegalArgumentException.class, new Thunk() {79public void run() throws Exception {80fc.transferTo(0, -1, fc);81}});8283tryCatch(IllegalArgumentException.class, new Thunk() {84public void run() throws Exception {85fc.map(FileChannel.MapMode.READ_ONLY, -1, 0);86}});8788tryCatch(IllegalArgumentException.class, new Thunk() {89public void run() throws Exception {90fc.map(FileChannel.MapMode.READ_ONLY, 0, -1);91}});9293tryCatch(IllegalArgumentException.class, new Thunk() {94public void run() throws Exception {95fc.map(FileChannel.MapMode.READ_ONLY, 0,96(long)Integer.MAX_VALUE << 3);97}});9899fc.close();100f.delete();101}102103}104105106