Path: blob/master/test/jdk/java/io/RandomAccessFile/ParameterCheck.java
41152 views
/*1* Copyright (c) 1997, 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 4030253 4030278 403024326@summary Test for correct parameter checking in read(byte[], int, int),27readFully(byte[], int, int) and write(byte[], int, int) of RandomAccessFile28*/2930import java.io.*;3132public class ParameterCheck {3334static int off[] = {-1, -1, 0, 0, 33, 33, 0, 32,3532, 4, 1, 0, -1, Integer.MAX_VALUE, 1};36static int len[] = {-1, 0, -1, 33, 0, 4, 32,370, 4, 16, 31, 0, Integer.MAX_VALUE,38Integer.MAX_VALUE, Integer.MAX_VALUE};39static boolean results[] = { false, false, false, false, false, false,40true, true, false, true, true, true, false,41false, false };42static int numBad = 0;4344private static void doTest(String method) throws Exception {45File fn = new File("x.ParameterCheck");46RandomAccessFile raf = null;4748try {49byte b[] = new byte[32];50int numCases = off.length;51int[] got = new int[numCases];52int numGood = 0;53FileOutputStream fout = new FileOutputStream(fn);54for (int i = 0; i < 32; i++) {55fout.write(i);56}57fout.close();58raf = new RandomAccessFile(fn , "rw");5960System.err.println("-----------------------------" +61"-----------------------------");62System.err.println("\nRandomAccessFile." + method +63"\nTotal test cases = " + (off.length+1));64System.err.println("-----------------------------" +65"-----------------------------");66for(int i = 0; i < numCases; i++) {67try {68if (method.equals("readFully")) {69raf.readFully(b , off[i] , len[i]);70}71if (method.equals("read")) {72raf.read(b , off[i] , len[i]);73}74if (method.equals("write")) {75raf.write(b , off[i] , len[i]);76}77raf.seek(0);78} catch(IndexOutOfBoundsException aiobe) {79if (results[i]) {80printErr(method , numGood,81i, "java.lang.IndexOutOfBoundsException");82} else {83numGood++;84}85continue;86} catch(OutOfMemoryError ome) {87printErr(method, numGood,88i, "java.lang.OutOfMemoryError");89continue;90}9192if (results[i]) {93numGood++;94}95else {96printErr(method, numGood,97i, "No java.lang.IndexOutOfBoundsException");98}99100}101102raf.seek(0);103boolean thrown = false;104try {105if (method.equals("readFully")) {106raf.readFully(null, 1, 2);107}108if (method.equals("read")) {109raf.read(null, 1, 2);110}111if (method.equals("write")) {112raf.write(null, 1, 2);113}114115} catch(NullPointerException npe) {116numGood++;117thrown = true;118}119if (!thrown) {120printErr(method, numGood, -1,121"no NullPointerException for null b");122}123124System.err.println("\nTotal passed = " + numGood);125System.err.println("-----------------------------" +126"-----------------------------");127} finally {128if (raf != null)129raf.close();130fn.delete();131}132133}134135private static void printErr(String method, int numGood,136int i, String expStr) {137numBad++;138System.err.println("\nNumber passed so far = " + numGood +139"\nUnexpected " + expStr);140if ( i < 0 ) {141System.err.println("for case : b = null");142} else {143System.err.println("for case : b.length = " + 32 +144" off = " + off[i] +145" len = " + len[i]);146}147}148149public static void main(String argv[]) throws Exception{150doTest("read");151doTest("readFully");152doTest("write");153154if (numBad > 0) {155throw new RuntimeException("Failed " + numBad + " tests");156}157}158}159160161