1 /*************************************************************************
2 /* RandomAccessFileTest.java -- Tests RandomAccessFile's
4 /* Copyright (c) 1998 Free Software Foundation, Inc.
5 /* Written by Aaron M. Renn (arenn@urbanophile.com)
7 /* This program is free software; you can redistribute it and/or modify
8 /* it under the terms of the GNU General Public License as published
9 /* by the Free Software Foundation, either version 2 of the License, or
10 /* (at your option) any later version.
12 /* This program is distributed in the hope that it will be useful, but
13 /* WITHOUT ANY WARRANTY; without even the implied warranty of
14 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 /* GNU General Public License for more details.
17 /* You should have received a copy of the GNU General Public License
18 /* along with this program; if not, write to the Free Software Foundation
19 /* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 /*************************************************************************/
24 // Write some data using DataOutput and read it using DataInput.
26 public class RandomAccessFileTest
30 runReadTest(String filename
, int seq
, String testname
)
34 System
.out
.println("Test " + seq
+ ": " + testname
);
36 RandomAccessFile ras
= new RandomAccessFile(filename
, "r");
38 boolean passed
= true;
40 boolean b
= ras
.readBoolean();
44 System
.out
.println("Failed to read boolean. Expected true and got false");
46 b
= ras
.readBoolean();
50 System
.out
.println("Failed to read boolean. Expected false and got true");
52 byte bt
= ras
.readByte();
56 System
.out
.println("Failed to read byte. Expected 8 and got "+ bt
);
62 System
.out
.println("Failed to read byte. Expected -122 and got "+ bt
);
64 char c
= ras
.readChar();
68 System
.out
.println("Failed to read char. Expected a and got " + c
);
74 System
.out
.println("Failed to read char. Expected \\uE2D2 and got " + c
);
76 short s
= ras
.readShort();
80 System
.out
.println("Failed to read short. Expected 32000 and got " + s
);
82 int i
= ras
.readInt();
86 System
.out
.println("Failed to read int. Expected 8675309 and got " + i
);
88 long l
= ras
.readLong();
89 if (l
!= 696969696969L)
92 System
.out
.println("Failed to read long. Expected 696969696969 and got " + l
);
94 float f
= ras
.readFloat();
95 if (!Float
.toString(f
).equals("3.1415"))
98 System
.out
.println("Failed to read float. Expected 3.1415 and got " + f
);
100 double d
= ras
.readDouble();
101 if (d
!= 999999999.999)
104 System
.out
.println("Failed to read double. Expected 999999999.999 and got " + d
);
106 String str
= ras
.readUTF();
107 if (!str
.equals("Testing code is such a boring activity but it must be done"))
110 System
.out
.println("Read unexpected String: " + str
);
113 if (!str
.equals("a-->\u01FF\uA000\u6666\u0200RRR"))
116 System
.out
.println("Read unexpected String: " + str
);
120 System
.out
.println("PASSED: " + testname
+ " read test");
122 System
.out
.println("FAILED: " + testname
+ " read test");
124 catch (IOException e
)
126 System
.out
.println("FAILED: " + testname
+ " read test: " + e
);
134 System
.out
.println("Started test of RandomAccessFile");
136 System
.out
.println("Test 1: RandomAccessFile write test");
139 RandomAccessFile raf
= new RandomAccessFile("dataoutput.out", "rw");
141 raf
.writeBoolean(true);
142 raf
.writeBoolean(false);
143 raf
.writeByte((byte)8);
144 raf
.writeByte((byte)-122);
145 raf
.writeChar((char)'a');
146 raf
.writeChar((char)'\uE2D2');
147 raf
.writeShort((short)32000);
148 raf
.writeInt((int)8675309);
149 raf
.writeLong((long) 696969696969L);
150 raf
.writeFloat((float)3.1415);
151 raf
.writeDouble((double)999999999.999);
152 raf
.writeUTF("Testing code is such a boring activity but it must be done");
153 raf
.writeUTF("a-->\u01FF\uA000\u6666\u0200RRR");
156 // We'll find out if this was really right later, but conditionally
157 // report success for now
158 System
.out
.println("PASSED: RandomAccessFile write test");
162 System
.out
.println("FAILED: RandomAccessFile write test: " + e
);
165 runReadTest("dataoutput.out", 2, "Read of JCL written data file");
166 runReadTest("dataoutput-jdk.out", 3, "Read of JDK written data file");
168 System
.out
.println("Test 2: Seek Test");
171 RandomAccessFile raf
= new RandomAccessFile("/etc/services", "r");
173 System
.out
.println("Length: " + raf
.length());
176 if (raf
.getFilePointer() != 24)
177 throw new IOException("Unexpected file pointer value " +
178 raf
.getFilePointer());
181 if (raf
.getFilePointer() != 0)
182 throw new IOException("Unexpected file pointer value " +
183 raf
.getFilePointer());
186 if (raf
.getFilePointer() != 100)
187 throw new IOException("Unexpected file pointer value " +
188 raf
.getFilePointer());
190 System
.out
.println("PASSED: Seek Test");
194 System
.out
.println("FAILED: Seek Test: " + e
);
197 System
.out
.println("Test 3: Validation Test");
198 boolean failed
= false;
201 new RandomAccessFile("/vmlinuz", "rwx");
202 System
.out
.println("Did not detect invalid mode");
205 catch (IllegalArgumentException e
) { ; }
206 catch (IOException e
) { ; }
210 new RandomAccessFile("/vmlinuz", "rw");
211 System
.out
.println("Did not detect read only file opened for write");
214 catch (IOException e
) { ; }
218 new RandomAccessFile("/sherlockholmes", "r");
219 System
.out
.println("Did not detect non-existent file");
222 catch (IOException e
) { ; }
226 RandomAccessFile raf
= new RandomAccessFile("/etc/services", "r");
227 raf
.seek(raf
.length());
229 System
.out
.println("Did not detect invalid write operation on read only file");
232 catch (IOException e
) { ; }
235 System
.out
.println("FAILED: Validation Test");
237 System
.out
.println("PASSED: Validation Test");
240 System.out.println("Test 4: Set File Length Rest");
243 File f = new File("tmptmptmp");
244 RandomAccessFile raf = new RandomAccessFile("tmptmptmp", "rw");
247 if (raf.length() != 50)
248 throw new IOException("Bad length on extending file of " + raf.length());
251 if (raf.length() != 25)
252 throw new IOException("Bad length on extending file of " + raf.length());
257 System.out.println("PASSED: Set File Length Test");
261 System.out.println("FAILED: Set File Length Test: " + e);
262 (new File("tmptmptmp")).delete();
265 System
.out
.println("Finished test of RandomAccessFile");
268 } // class DataInputOutputTest