J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1997 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | @test |
| 26 | @bug 4030253 4030278 4030243 |
| 27 | @summary Test for correct parameter checking in read(byte[], int, int), |
| 28 | readFully(byte[], int, int) and write(byte[], int, int) of RandomAccessFile |
| 29 | */ |
| 30 | |
| 31 | import java.io.*; |
| 32 | |
| 33 | public class ParameterCheck { |
| 34 | |
| 35 | static int off[] = {-1, -1, 0, 0, 33, 33, 0, 32, |
| 36 | 32, 4, 1, 0, -1, Integer.MAX_VALUE, 1}; |
| 37 | static int len[] = {-1, 0, -1, 33, 0, 4, 32, |
| 38 | 0, 4, 16, 31, 0, Integer.MAX_VALUE, |
| 39 | Integer.MAX_VALUE, Integer.MAX_VALUE}; |
| 40 | static boolean results[] = { false, false, false, false, false, false, |
| 41 | true, true, false, true, true, true, false, |
| 42 | false, false }; |
| 43 | static int numBad = 0; |
| 44 | |
| 45 | private static void doTest(String method) throws Exception { |
| 46 | File fn = new File("x.ParameterCheck"); |
| 47 | |
| 48 | try { |
| 49 | byte b[] = new byte[32]; |
| 50 | int numCases = off.length; |
| 51 | int[] got = new int[numCases]; |
| 52 | int numGood = 0; |
| 53 | FileOutputStream fout = new FileOutputStream(fn); |
| 54 | for (int i = 0; i < 32; i++) { |
| 55 | fout.write(i); |
| 56 | } |
| 57 | fout.close(); |
| 58 | RandomAccessFile raf = new RandomAccessFile(fn , "rw"); |
| 59 | |
| 60 | System.err.println("-----------------------------" + |
| 61 | "-----------------------------"); |
| 62 | System.err.println("\nRandomAccessFile." + method + |
| 63 | "\nTotal test cases = " + (off.length+1)); |
| 64 | System.err.println("-----------------------------" + |
| 65 | "-----------------------------"); |
| 66 | for(int i = 0; i < numCases; i++) { |
| 67 | try { |
| 68 | if (method.equals("readFully")) { |
| 69 | raf.readFully(b , off[i] , len[i]); |
| 70 | } |
| 71 | if (method.equals("read")) { |
| 72 | raf.read(b , off[i] , len[i]); |
| 73 | } |
| 74 | if (method.equals("write")) { |
| 75 | raf.write(b , off[i] , len[i]); |
| 76 | } |
| 77 | raf.seek(0); |
| 78 | } catch(IndexOutOfBoundsException aiobe) { |
| 79 | if (results[i]) { |
| 80 | printErr(method , numGood, |
| 81 | i, "java.lang.IndexOutOfBoundsException"); |
| 82 | } else { |
| 83 | numGood++; |
| 84 | } |
| 85 | continue; |
| 86 | } catch(OutOfMemoryError ome) { |
| 87 | printErr(method, numGood, |
| 88 | i, "java.lang.OutOfMemoryError"); |
| 89 | continue; |
| 90 | } |
| 91 | |
| 92 | if (results[i]) { |
| 93 | numGood++; |
| 94 | } |
| 95 | else { |
| 96 | printErr(method, numGood, |
| 97 | i, "No java.lang.IndexOutOfBoundsException"); |
| 98 | } |
| 99 | |
| 100 | } |
| 101 | |
| 102 | raf.seek(0); |
| 103 | boolean thrown = false; |
| 104 | try { |
| 105 | if (method.equals("readFully")) { |
| 106 | raf.readFully(null, 1, 2); |
| 107 | } |
| 108 | if (method.equals("read")) { |
| 109 | raf.read(null, 1, 2); |
| 110 | } |
| 111 | if (method.equals("write")) { |
| 112 | raf.write(null, 1, 2); |
| 113 | } |
| 114 | |
| 115 | } catch(NullPointerException npe) { |
| 116 | numGood++; |
| 117 | thrown = true; |
| 118 | } |
| 119 | if (!thrown) { |
| 120 | printErr(method, numGood, -1, |
| 121 | "no NullPointerException for null b"); |
| 122 | } |
| 123 | |
| 124 | System.err.println("\nTotal passed = " + numGood); |
| 125 | System.err.println("-----------------------------" + |
| 126 | "-----------------------------"); |
| 127 | } finally { |
| 128 | fn.delete(); |
| 129 | } |
| 130 | |
| 131 | } |
| 132 | |
| 133 | private static void printErr(String method, int numGood, |
| 134 | int i, String expStr) { |
| 135 | numBad++; |
| 136 | System.err.println("\nNumber passed so far = " + numGood + |
| 137 | "\nUnexpected " + expStr); |
| 138 | if ( i < 0 ) { |
| 139 | System.err.println("for case : b = null"); |
| 140 | } else { |
| 141 | System.err.println("for case : b.length = " + 32 + |
| 142 | " off = " + off[i] + |
| 143 | " len = " + len[i]); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | public static void main(String argv[]) throws Exception{ |
| 148 | doTest("read"); |
| 149 | doTest("readFully"); |
| 150 | doTest("write"); |
| 151 | |
| 152 | if (numBad > 0) { |
| 153 | throw new RuntimeException("Failed " + numBad + " tests"); |
| 154 | } |
| 155 | } |
| 156 | } |