1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package org.apache.harmony.tests.java.io;
19
20import java.io.EOFException;
21import java.io.File;
22import java.io.FileInputStream;
23import java.io.FileNotFoundException;
24import java.io.FileOutputStream;
25import java.io.IOException;
26import java.io.RandomAccessFile;
27import java.nio.channels.FileChannel;
28import java.nio.channels.NonWritableChannelException;
29
30public class RandomAccessFileTest extends junit.framework.TestCase {
31
32    public String fileName;
33
34    public boolean ufile = true;
35
36    java.io.RandomAccessFile raf;
37
38    java.io.File f;
39
40    String unihw = "\u0048\u0065\u006C\u0801\u006C\u006F\u0020\u0057\u0081\u006F\u0072\u006C\u0064";
41
42    //java.io.FileOutputStream fos;
43
44    public String fileString = "Test_All_Tests\nTest_java_io_BufferedInputStream\nTest_java_io_BufferedOutputStream\nTest_java_io_ByteArrayInputStream\nTest_java_io_ByteArrayOutputStream\nTest_java_io_DataInputStream\nTest_java_io_File\nTest_java_io_FileDescriptor\nTest_java_io_FileInputStream\nTest_java_io_FileNotFoundException\nTest_java_io_FileOutputStream\nTest_java_io_FilterInputStream\nTest_java_io_FilterOutputStream\nTest_java_io_InputStream\nTest_java_io_IOException\nTest_java_io_OutputStream\nTest_java_io_PrintStream\nTest_RandomAccessFile\nTest_java_io_SyncFailedException\nTest_java_lang_AbstractMethodError\nTest_java_lang_ArithmeticException\nTest_java_lang_ArrayIndexOutOfBoundsException\nTest_java_lang_ArrayStoreException\nTest_java_lang_Boolean\nTest_java_lang_Byte\nTest_java_lang_Character\nTest_java_lang_Class\nTest_java_lang_ClassCastException\nTest_java_lang_ClassCircularityError\nTest_java_lang_ClassFormatError\nTest_java_lang_ClassLoader\nTest_java_lang_ClassNotFoundException\nTest_java_lang_CloneNotSupportedException\nTest_java_lang_Double\nTest_java_lang_Error\nTest_java_lang_Exception\nTest_java_lang_ExceptionInInitializerError\nTest_java_lang_Float\nTest_java_lang_IllegalAccessError\nTest_java_lang_IllegalAccessException\nTest_java_lang_IllegalArgumentException\nTest_java_lang_IllegalMonitorStateException\nTest_java_lang_IllegalThreadStateException\nTest_java_lang_IncompatibleClassChangeError\nTest_java_lang_IndexOutOfBoundsException\nTest_java_lang_InstantiationError\nTest_java_lang_InstantiationException\nTest_java_lang_Integer\nTest_java_lang_InternalError\nTest_java_lang_InterruptedException\nTest_java_lang_LinkageError\nTest_java_lang_Long\nTest_java_lang_Math\nTest_java_lang_NegativeArraySizeException\nTest_java_lang_NoClassDefFoundError\nTest_java_lang_NoSuchFieldError\nTest_java_lang_NoSuchMethodError\nTest_java_lang_NullPointerException\nTest_java_lang_Number\nTest_java_lang_NumberFormatException\nTest_java_lang_Object\nTest_java_lang_OutOfMemoryError\nTest_java_lang_RuntimeException\nTest_java_lang_SecurityManager\nTest_java_lang_Short\nTest_java_lang_StackOverflowError\nTest_java_lang_String\nTest_java_lang_StringBuffer\nTest_java_lang_StringIndexOutOfBoundsException\nTest_java_lang_System\nTest_java_lang_Thread\nTest_java_lang_ThreadDeath\nTest_java_lang_ThreadGroup\nTest_java_lang_Throwable\nTest_java_lang_UnknownError\nTest_java_lang_UnsatisfiedLinkError\nTest_java_lang_VerifyError\nTest_java_lang_VirtualMachineError\nTest_java_lang_vm_Image\nTest_java_lang_vm_MemorySegment\nTest_java_lang_vm_ROMStoreException\nTest_java_lang_vm_VM\nTest_java_lang_Void\nTest_java_net_BindException\nTest_java_net_ConnectException\nTest_java_net_DatagramPacket\nTest_java_net_DatagramSocket\nTest_java_net_DatagramSocketImpl\nTest_java_net_InetAddress\nTest_java_net_NoRouteToHostException\nTest_java_net_PlainDatagramSocketImpl\nTest_java_net_PlainSocketImpl\nTest_java_net_Socket\nTest_java_net_SocketException\nTest_java_net_SocketImpl\nTest_java_net_SocketInputStream\nTest_java_net_SocketOutputStream\nTest_java_net_UnknownHostException\nTest_java_util_ArrayEnumerator\nTest_java_util_Date\nTest_java_util_EventObject\nTest_java_util_HashEnumerator\nTest_java_util_Hashtable\nTest_java_util_Properties\nTest_java_util_ResourceBundle\nTest_java_util_tm\nTest_java_util_Vector\n";
45
46    /**
47     * java.io.RandomAccessFile#RandomAccessFile(java.io.File,
48     *java.lang.String)
49     */
50    public void test_ConstructorLjava_io_FileLjava_lang_String()
51            throws Exception {
52        // Test for method java.io.RandomAccessFile(java.io.File,
53        // java.lang.String)
54        RandomAccessFile raf = new java.io.RandomAccessFile(f, "rw");
55        raf.write(20);
56        raf.seek(0);
57        assertEquals("Incorrect int read/written", 20, raf.read());
58        raf.close();
59
60        raf = new java.io.RandomAccessFile(f, "rwd");
61        raf.write(20);
62        raf.seek(0);
63        assertEquals("Incorrect int read/written", 20, raf.read());
64        raf.close();
65
66        raf = new java.io.RandomAccessFile(f, "rws");
67        raf.write(20);
68        raf.seek(0);
69        assertEquals("Incorrect int read/written", 20, raf.read());
70        raf.close();
71
72        // Regression for HARMONY-50
73        File f = File.createTempFile("xxx", "yyy");
74        f.deleteOnExit();
75        raf = new RandomAccessFile(f, "rws");
76        raf.close();
77
78        f = File.createTempFile("xxx", "yyy");
79        f.deleteOnExit();
80        raf = new RandomAccessFile(f, "rwd");
81        raf.close();
82    }
83
84    /**
85     * java.io.RandomAccessFile#RandomAccessFile(java.lang.String,
86     *java.lang.String)
87     */
88    public void test_ConstructorLjava_lang_StringLjava_lang_String()
89            throws IOException {
90        // Test for method java.io.RandomAccessFile(java.lang.String,
91        // java.lang.String)
92        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
93        raf.write("Test".getBytes(), 0, 4);
94        raf.close();
95
96        raf = new java.io.RandomAccessFile(fileName, "rwd");
97        raf.write("Test".getBytes(), 0, 4);
98        raf.close();
99
100        raf = new java.io.RandomAccessFile(fileName, "rws");
101        raf.write("Test".getBytes(), 0, 4);
102        raf.close();
103    }
104
105    /**
106     * java.io.RandomAccessFile#RandomAccessFile(java.lang.String,
107     *java.lang.String)
108     */
109    public void test_ConstructorLjava_lang_StringLjava_lang_String_I()
110            throws IOException {
111        RandomAccessFile raf = null;
112        try {
113            raf = new RandomAccessFile("", "r");
114            fail("should throw FileNotFoundException.");
115        } catch (FileNotFoundException e) {
116            // Expected
117        } finally {
118            if (raf != null) {
119                raf.close();
120                raf = null;
121            }
122        }
123        try {
124            raf = new RandomAccessFile(new File(""), "r");
125            fail("should throw FileNotFoundException.");
126        } catch (FileNotFoundException e) {
127            // Expected
128        } finally {
129            if (raf != null) {
130                raf.close();
131                raf = null;
132            }
133        }
134        File dir = new File("/");
135        assertTrue(dir.isDirectory());
136        try {
137            raf = new RandomAccessFile(dir.getPath(), "r");
138            fail();
139        } catch (FileNotFoundException expected) {
140        } finally {
141            if (raf != null) {
142                raf.close();
143                raf = null;
144            }
145        }
146    }
147
148    /**
149     * java.io.RandomAccessFile#close()
150     */
151    public void test_close() {
152        // Test for method void java.io.RandomAccessFile.close()
153        try {
154            RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
155            raf.close();
156            raf.write("Test".getBytes(), 0, 4);
157            fail("Failed to close file properly");
158        } catch (IOException e) {
159        }
160    }
161
162    /**
163     * java.io.RandomAccessFile#getFD()
164     */
165    public void test_getFD() throws IOException {
166        // Test for method java.io.FileDescriptor
167        // java.io.RandomAccessFile.getFD()
168
169        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
170        assertTrue("Returned invalid fd", raf.getFD().valid());
171
172        raf.close();
173        assertFalse("Returned valid fd after close", raf.getFD().valid());
174    }
175
176    /**
177     * java.io.RandomAccessFile#getFilePointer()
178     */
179    public void test_getFilePointer() throws IOException {
180        // Test for method long java.io.RandomAccessFile.getFilePointer()
181        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
182        raf.write(fileString.getBytes(), 0, 1000);
183        assertEquals("Incorrect filePointer returned", 1000, raf
184                .getFilePointer());
185        raf.close();
186    }
187
188    /**
189     * java.io.RandomAccessFile#length()
190     */
191    public void test_length() throws IOException {
192        // Test for method long java.io.RandomAccessFile.length()
193        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
194        raf.write(fileString.getBytes());
195        assertEquals("Incorrect length returned", fileString.length(), raf
196                .length());
197        raf.close();
198    }
199
200    /**
201     * java.io.RandomAccessFile#read()
202     */
203    public void test_read() throws IOException {
204        // Test for method int java.io.RandomAccessFile.read()
205        FileOutputStream fos = new java.io.FileOutputStream(fileName);
206        fos.write(fileString.getBytes("UTF-8"), 0, fileString.length());
207        fos.close();
208
209        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "r");
210        assertEquals("Incorrect bytes returned from read",
211                fileString.charAt(0), raf.read());
212        raf.close();
213    }
214
215    /**
216     * java.io.RandomAccessFile#read(byte[])
217     */
218    public void test_read$B() throws IOException {
219        // Test for method int java.io.RandomAccessFile.read(byte [])
220        FileOutputStream fos = new java.io.FileOutputStream(fileName);
221        fos.write(fileString.getBytes(), 0, fileString.length());
222        fos.close();
223
224        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "r");
225        byte[] rbuf = new byte[4000];
226        raf.read(rbuf);
227        assertEquals("Incorrect bytes returned from read", fileString,
228                new String(rbuf, 0, fileString.length()));
229        raf.close();
230    }
231
232    /**
233     * java.io.RandomAccessFile#read(byte[], int, int)
234     */
235    public void test_read$BII() throws IOException {
236        // Test for method int java.io.RandomAccessFile.read(byte [], int, int)
237        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
238        byte[] rbuf = new byte[4000];
239        FileOutputStream fos = new java.io.FileOutputStream(fileName);
240        fos.write(fileString.getBytes(), 0, fileString.length());
241        fos.close();
242        raf.read(rbuf, 0, fileString.length());
243        assertEquals("Incorrect bytes returned from read", fileString,
244                new String(rbuf, 0, fileString.length()));
245        raf.close();
246    }
247
248    /**
249     * java.io.RandomAccessFile#readBoolean()
250     */
251    public void test_readBoolean() throws IOException {
252        // Test for method boolean java.io.RandomAccessFile.readBoolean()
253        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
254        raf.writeBoolean(true);
255        raf.seek(0);
256        assertTrue("Incorrect boolean read/written", raf.readBoolean());
257        raf.close();
258    }
259
260    /**
261     * java.io.RandomAccessFile#readByte()
262     */
263    public void test_readByte() throws IOException {
264        // Test for method byte java.io.RandomAccessFile.readByte()
265        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
266        raf.writeByte(127);
267        raf.seek(0);
268        assertEquals("Incorrect bytes read/written", 127, raf.readByte());
269        raf.close();
270    }
271
272    /**
273     * java.io.RandomAccessFile#readChar()
274     */
275    public void test_readChar() throws IOException {
276        // Test for method char java.io.RandomAccessFile.readChar()
277        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
278        raf.writeChar('T');
279        raf.seek(0);
280        assertEquals("Incorrect char read/written", 'T', raf.readChar());
281        raf.close();
282    }
283
284    /**
285     * java.io.RandomAccessFile#readDouble()
286     */
287    public void test_readDouble() throws IOException {
288        // Test for method double java.io.RandomAccessFile.readDouble()
289        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
290        raf.writeDouble(Double.MAX_VALUE);
291        raf.seek(0);
292        assertEquals("Incorrect double read/written", Double.MAX_VALUE, raf
293                .readDouble(), 0);
294        raf.close();
295    }
296
297    /**
298     * java.io.RandomAccessFile#readFloat()
299     */
300    public void test_readFloat() throws IOException {
301        // Test for method float java.io.RandomAccessFile.readFloat()
302        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
303        raf.writeFloat(Float.MAX_VALUE);
304        raf.seek(0);
305        assertEquals("Incorrect float read/written", Float.MAX_VALUE, raf
306                .readFloat(), 0);
307        raf.close();
308    }
309
310    /**
311     * java.io.RandomAccessFile#readFully(byte[])
312     */
313    public void test_readFully$B() throws IOException {
314        // Test for method void java.io.RandomAccessFile.readFully(byte [])
315        byte[] buf = new byte[10];
316        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
317        raf.writeBytes("HelloWorld");
318        raf.seek(0);
319        raf.readFully(buf);
320        assertEquals("Incorrect bytes read/written", "HelloWorld", new String(
321                buf, 0, 10, "UTF-8"));
322        raf.close();
323    }
324
325    /**
326     * java.io.RandomAccessFile#readFully(byte[], int, int)
327     */
328    public void test_readFully$BII() throws IOException {
329        // Test for method void java.io.RandomAccessFile.readFully(byte [], int,
330        // int)
331        byte[] buf = new byte[10];
332        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
333        raf.writeBytes("HelloWorld");
334        raf.seek(0);
335        raf.readFully(buf, 0, buf.length);
336        assertEquals("Incorrect bytes read/written", "HelloWorld", new String(
337                buf, 0, 10, "UTF-8"));
338        try {
339            raf.readFully(buf, 0, buf.length);
340            fail("Reading past end of buffer did not throw EOFException");
341        } catch (EOFException e) {
342        }
343        raf.close();
344    }
345
346    /**
347     * java.io.RandomAccessFile#readInt()
348     */
349    public void test_readInt() throws IOException {
350        // Test for method int java.io.RandomAccessFile.readInt()
351        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
352        raf.writeInt(Integer.MIN_VALUE);
353        raf.seek(0);
354        assertEquals("Incorrect int read/written", Integer.MIN_VALUE, raf
355                .readInt());
356        raf.close();
357    }
358
359    /**
360     * java.io.RandomAccessFile#readLine()
361     */
362    public void test_readLine() throws IOException {
363        // Test for method java.lang.String java.io.RandomAccessFile.readLine()
364        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
365        String s = "Goodbye\nCruel\nWorld\n";
366        raf.write(s.getBytes("UTF-8"), 0, s.length());
367        raf.seek(0);
368
369        assertEquals("Goodbye", raf.readLine());
370        assertEquals("Cruel", raf.readLine());
371        assertEquals("World", raf.readLine());
372
373        raf.close();
374    }
375
376    /**
377     * java.io.RandomAccessFile#readLong()
378     */
379    public void test_readLong() throws IOException {
380        // Test for method long java.io.RandomAccessFile.readLong()
381        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
382        raf.writeLong(Long.MAX_VALUE);
383        raf.seek(0);
384        assertEquals("Incorrect long read/written", Long.MAX_VALUE, raf
385                .readLong());
386        raf.close();
387    }
388
389    /**
390     * java.io.RandomAccessFile#readShort()
391     */
392    public void test_readShort() throws IOException {
393        // Test for method short java.io.RandomAccessFile.readShort()
394        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
395        raf.writeShort(Short.MIN_VALUE);
396        raf.seek(0);
397        assertEquals("Incorrect long read/written", Short.MIN_VALUE, raf
398                .readShort());
399        raf.close();
400    }
401
402    /**
403     * java.io.RandomAccessFile#readUnsignedByte()
404     */
405    public void test_readUnsignedByte() throws IOException {
406        // Test for method int java.io.RandomAccessFile.readUnsignedByte()
407        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
408        raf.writeByte(-1);
409        raf.seek(0);
410        assertEquals("Incorrect byte read/written", 255, raf.readUnsignedByte());
411        raf.close();
412    }
413
414    /**
415     * java.io.RandomAccessFile#readUnsignedShort()
416     */
417    public void test_readUnsignedShort() throws IOException {
418        // Test for method int java.io.RandomAccessFile.readUnsignedShort()
419        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
420        raf.writeShort(-1);
421        raf.seek(0);
422        assertEquals("Incorrect byte read/written", 65535, raf
423                .readUnsignedShort());
424        raf.close();
425    }
426
427    /**
428     * java.io.RandomAccessFile#readUTF()
429     */
430    public void test_readUTF() throws IOException {
431        // Test for method java.lang.String java.io.RandomAccessFile.readUTF()
432
433        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
434        raf.writeUTF(unihw);
435        raf.seek(0);
436        assertEquals("Incorrect utf string read", unihw, raf.readUTF());
437        raf.close();
438    }
439
440    /**
441     * java.io.RandomAccessFile#seek(long)
442     */
443    public void test_seekJ() throws IOException {
444        // Test for method void java.io.RandomAccessFile.seek(long)
445        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
446        raf.write(fileString.getBytes(), 0, fileString.length());
447        raf.seek(12);
448        assertEquals("Seek failed to set filePointer", 12, raf.getFilePointer());
449        raf.close();
450    }
451
452    /**
453     * java.io.RandomAccessFile#skipBytes(int)
454     */
455    public void test_skipBytesI() throws IOException {
456        // Test for method int java.io.RandomAccessFile.skipBytes(int)
457        byte[] buf = new byte[5];
458        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
459        raf.writeBytes("HelloWorld");
460        raf.seek(0);
461        raf.skipBytes(5);
462        raf.readFully(buf);
463        assertEquals("Failed to skip bytes", "World", new String(buf, 0, 5, "UTF-8"));
464        raf.close();
465    }
466
467    /**
468     * java.io.RandomAccessFile#write(byte[])
469     */
470    public void test_write$B() throws IOException {
471        // Test for method void java.io.RandomAccessFile.write(byte [])
472        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
473
474        byte[] nullByteArray = null;
475        try {
476            raf.write(nullByteArray);
477            fail("should throw NullPointerException");
478        } catch (NullPointerException e) {
479            //expected
480        }
481
482        byte[] rbuf = new byte[4000];
483        raf.write(fileString.getBytes());
484        raf.close();
485
486        try {
487            raf.write(nullByteArray);
488            fail("should throw NullPointerException");
489        } catch (NullPointerException e) {
490            //expected
491        }
492
493        //will not throw IOException if array's length is 0
494        raf.write(new byte[0]);
495
496        try {
497            raf.write(fileString.getBytes());
498            fail("should throw IOException");
499        } catch (IOException e) {
500            //expected
501        }
502
503        FileInputStream fis = new java.io.FileInputStream(fileName);
504        fis.read(rbuf, 0, fileString.length());
505        assertEquals("Incorrect bytes written", fileString, new String(rbuf, 0,
506                fileString.length()));
507        fis.close();
508    }
509
510    /**
511     * java.io.RandomAccessFile#write(byte[], int, int)
512     */
513    public void test_write$BII() throws IOException {
514        // Test for method void java.io.RandomAccessFile.write(byte [], int,
515        // int)
516        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
517        byte[] rbuf = new byte[4000];
518        raf.write(fileString.getBytes(), 0, fileString.length());
519        raf.close();
520        FileInputStream fis = new java.io.FileInputStream(fileName);
521        fis.read(rbuf, 0, fileString.length());
522        assertEquals("Incorrect bytes written", fileString, new String(rbuf, 0,
523                fileString.length()));
524        fis.close();
525    }
526
527    /**
528     * java.io.RandomAccessFile#write(byte[], int, int)
529     */
530    public void test_write_$BII_Exception() throws IOException {
531        raf = new java.io.RandomAccessFile(f, "rw");
532        byte[] nullByteArray = null;
533        byte[] byteArray = new byte[10];
534
535        try {
536            raf.write(nullByteArray, -1, -1);
537            fail("should throw NullPointerException");
538        } catch (NullPointerException e) {
539            // expected
540        }
541
542        try {
543            raf.write(nullByteArray, 0, 0);
544            fail("should throw NullPointerException");
545        } catch (NullPointerException e) {
546            // expected
547        }
548
549        try {
550            raf.write(nullByteArray, 1, -1);
551            fail("should throw NullPointerException");
552        } catch (NullPointerException e) {
553            // expected
554        }
555
556        try {
557            raf.write(nullByteArray, 1, 0);
558            fail("should throw NullPointerException");
559        } catch (NullPointerException e) {
560            // expected
561        }
562
563        try {
564            raf.write(nullByteArray, 1, 1);
565            fail("should throw NullPointerException");
566        } catch (NullPointerException e) {
567            // expected
568        }
569
570        try {
571            raf.write(byteArray, -1, -1);
572            fail("should throw IndexOutOfBoundsException");
573        } catch (IndexOutOfBoundsException e) {
574            // expected
575        }
576
577        try {
578            raf.write(byteArray, -1, 0);
579            fail("should throw IndexOutOfBoundsException");
580        } catch (IndexOutOfBoundsException e) {
581            // expected
582        }
583
584        try {
585            raf.write(byteArray, -1, 1);
586            fail("should throw IndexOutOfBoundsException");
587        } catch (IndexOutOfBoundsException e) {
588            // expected
589        }
590
591        try {
592            raf.write(byteArray, 0, -1);
593            fail("should throw IndexOutOfBoundsException");
594        } catch (IndexOutOfBoundsException e) {
595            // expected
596        }
597
598        raf.write(byteArray, 0, 0);
599        raf.write(byteArray, 0, byteArray.length);
600        raf.write(byteArray, 1, 0);
601        raf.write(byteArray, byteArray.length, 0);
602
603        try {
604            raf.write(byteArray, byteArray.length + 1, 0);
605            fail("should throw IndexOutOfBoundsException");
606        } catch (IndexOutOfBoundsException e) {
607            //expected
608        }
609
610        try {
611            raf.write(byteArray, byteArray.length + 1, 1);
612            fail("should throw IndexOutOfBoundsException");
613        } catch (IndexOutOfBoundsException e) {
614            //expected
615        }
616
617        raf.close();
618
619        try {
620            raf.write(nullByteArray, -1, -1);
621            fail("should throw NullPointerException");
622        } catch (NullPointerException e) {
623            // expected
624        }
625
626        try {
627            raf.write(byteArray, -1, -1);
628            fail("should throw IndexOutOfBoundsException");
629        } catch (IndexOutOfBoundsException e) {
630            // expected
631        }
632
633        try {
634            raf.write(byteArray, 0, 1);
635            fail("should throw IOException");
636        } catch (IOException e) {
637            //expected
638        }
639
640        try {
641            raf.write(byteArray, 0, byteArray.length);
642            fail("should throw IOException");
643        } catch (IOException e) {
644            //expected
645        }
646
647        try {
648            raf.write(byteArray, 1, 1);
649            fail("should throw IOException");
650        } catch (IOException e) {
651            //expected
652        }
653
654        try {
655            raf.write(byteArray, byteArray.length + 1, 0);
656            fail("should throw IndexOutOfBoundsException");
657        } catch (IndexOutOfBoundsException e) {
658            //expected
659        }
660
661        // will not throw IOException if count = 0
662        raf.write(byteArray, 0, 0);
663        raf.write(byteArray, byteArray.length, 0);
664    }
665
666
667    /**
668     * java.io.RandomAccessFile#write(int)
669     */
670    public void test_writeI() throws IOException {
671        // Test for method void java.io.RandomAccessFile.write(int)
672        byte[] rbuf = new byte[4000];
673        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
674        raf.write('t');
675        raf.close();
676        FileInputStream fis = new java.io.FileInputStream(fileName);
677        fis.read(rbuf, 0, 1);
678        assertEquals("Incorrect byte written", 't', rbuf[0]);
679        fis.close();
680    }
681
682    /**
683     * java.io.RandomAccessFile#writeBoolean(boolean)
684     */
685    public void test_writeBooleanZ() throws IOException {
686        // Test for method void java.io.RandomAccessFile.writeBoolean(boolean)
687        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
688        raf.writeBoolean(true);
689        raf.seek(0);
690        assertTrue("Incorrect boolean read/written", raf.readBoolean());
691        raf.close();
692    }
693
694    /**
695     * java.io.RandomAccessFile#writeByte(int)
696     */
697    public void test_writeByteI() throws IOException {
698        // Test for method void java.io.RandomAccessFile.writeByte(int)
699        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
700        raf.writeByte(127);
701        raf.seek(0);
702        assertEquals("Incorrect byte read/written", 127, raf.readByte());
703        raf.close();
704    }
705
706    /**
707     * java.io.RandomAccessFile#writeBytes(java.lang.String)
708     */
709    public void test_writeBytesLjava_lang_String() throws IOException {
710        // Test for method void
711        // java.io.RandomAccessFile.writeBytes(java.lang.String)
712        byte[] buf = new byte[10];
713        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
714        raf.writeBytes("HelloWorld");
715        raf.seek(0);
716        raf.readFully(buf);
717        assertEquals("Incorrect bytes read/written", "HelloWorld", new String(
718                buf, 0, 10, "UTF-8"));
719        raf.close();
720
721    }
722
723    /**
724     * java.io.RandomAccessFile#writeChar(int)
725     */
726    public void test_writeCharI() throws IOException {
727        // Test for method void java.io.RandomAccessFile.writeChar(int)
728        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
729        raf.writeChar('T');
730        raf.seek(0);
731        assertEquals("Incorrect char read/written", 'T', raf.readChar());
732        raf.close();
733    }
734
735    /**
736     * java.io.RandomAccessFile#writeChars(java.lang.String)
737     */
738    public void test_writeCharsLjava_lang_String() throws IOException {
739        // Test for method void
740        // java.io.RandomAccessFile.writeChars(java.lang.String)
741        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
742        raf.writeChars("HelloWorld");
743        char[] hchars = new char[10];
744        "HelloWorld".getChars(0, 10, hchars, 0);
745        raf.seek(0);
746        for (int i = 0; i < hchars.length; i++)
747            assertEquals("Incorrect string written", hchars[i], raf.readChar());
748        raf.close();
749    }
750
751    /**
752     * java.io.RandomAccessFile#writeDouble(double)
753     */
754    public void test_writeDoubleD() throws IOException {
755        // Test for method void java.io.RandomAccessFile.writeDouble(double)
756        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
757        raf.writeDouble(Double.MAX_VALUE);
758        raf.seek(0);
759        assertEquals("Incorrect double read/written", Double.MAX_VALUE, raf
760                .readDouble(), 0);
761        raf.close();
762    }
763
764    /**
765     * java.io.RandomAccessFile#writeFloat(float)
766     */
767    public void test_writeFloatF() throws IOException {
768        // Test for method void java.io.RandomAccessFile.writeFloat(float)
769        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
770        raf.writeFloat(Float.MAX_VALUE);
771        raf.seek(0);
772        assertEquals("Incorrect float read/written", Float.MAX_VALUE, raf
773                .readFloat(), 0);
774        raf.close();
775    }
776
777    /**
778     * java.io.RandomAccessFile#writeInt(int)
779     */
780    public void test_writeIntI() throws IOException {
781        // Test for method void java.io.RandomAccessFile.writeInt(int)
782        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
783        raf.writeInt(Integer.MIN_VALUE);
784        raf.seek(0);
785        assertEquals("Incorrect int read/written", Integer.MIN_VALUE, raf
786                .readInt());
787        raf.close();
788    }
789
790    /**
791     * java.io.RandomAccessFile#writeLong(long)
792     */
793    public void test_writeLongJ() throws IOException {
794        // Test for method void java.io.RandomAccessFile.writeLong(long)
795        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
796        raf.writeLong(Long.MAX_VALUE);
797        raf.seek(0);
798        assertEquals("Incorrect long read/written", Long.MAX_VALUE, raf
799                .readLong());
800        raf.close();
801    }
802
803    /**
804     * java.io.RandomAccessFile#writeShort(int)
805     */
806    public void test_writeShortI() throws IOException {
807        // Test for method void java.io.RandomAccessFile.writeShort(int)
808        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
809        raf.writeShort(Short.MIN_VALUE);
810        raf.seek(0);
811        assertEquals("Incorrect long read/written", Short.MIN_VALUE, raf
812                .readShort());
813        raf.close();
814    }
815
816    /**
817     * java.io.RandomAccessFile#writeUTF(java.lang.String)
818     */
819    public void test_writeUTFLjava_lang_String() throws IOException {
820        // Test for method void
821        // java.io.RandomAccessFile.writeUTF(java.lang.String)
822        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
823        raf.writeUTF(unihw);
824        raf.seek(0);
825        assertEquals("Incorrect utf string", unihw, raf.readUTF());
826        raf.close();
827    }
828
829    /**
830     * java.io.RandomAccessFile#seek(long)
831     * <p/>
832     * Regression for HARMONY-374
833     */
834    public void test_seekI() throws IOException {
835        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
836        try {
837            raf.seek(-1);
838            fail("IOException must be thrown if pos < 0");
839        } catch (IOException e) {
840        }
841        raf.close();
842    }
843
844    /**
845     * java.io.RandomAccessFile#read(byte[], int, int)
846     * <p/>
847     * Regression for HARMONY-377
848     */
849    public void test_readBII() throws IOException {
850        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
851        try {
852            raf.read(new byte[1], -1, 1);
853            fail("IndexOutOfBoundsException must be thrown if off <0");
854        } catch (IndexOutOfBoundsException e) {
855        }
856
857        try {
858            raf.read(new byte[1], 0, -1);
859            fail("IndexOutOfBoundsException must be thrown if len <0");
860        } catch (IndexOutOfBoundsException e) {
861        }
862
863        try {
864            raf.read(new byte[1], 0, 5);
865            fail("IndexOutOfBoundsException must be thrown if off+len > b.length");
866        } catch (IndexOutOfBoundsException e) {
867        }
868
869        try {
870            raf.read(new byte[10], Integer.MAX_VALUE, 5);
871            fail("IndexOutOfBoundsException expected");
872        } catch (IndexOutOfBoundsException e) {
873        }
874
875        try {
876            raf.read(new byte[10], 5, Integer.MAX_VALUE);
877            fail("IndexOutOfBoundsException expected");
878        } catch (IndexOutOfBoundsException e) {
879        }
880
881        raf.close();
882    }
883
884    /**
885     * java.io.RandomAccessFile#read(byte[], int, int)
886     */
887    public void test_read_$BII_IndexOutOfBoundsException() throws IOException {
888        FileOutputStream fos = new java.io.FileOutputStream(fileName);
889        fos.write(fileString.getBytes(), 0, fileString.length());
890        fos.close();
891
892        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "r");
893        byte[] rbuf = new byte[100];
894        raf.close();
895        try {
896            raf.read(rbuf, -1, 0);
897            fail("should throw IndexOutOfBoundsException");
898        } catch (IndexOutOfBoundsException e) {
899            //expected
900        }
901    }
902
903    /**
904     * java.io.RandomAccessFile#read(byte[], int, int)
905     */
906    public void test_read_$BII_IOException() throws IOException {
907        FileOutputStream fos = new java.io.FileOutputStream(fileName);
908        fos.write(fileString.getBytes(), 0, fileString.length());
909        fos.close();
910
911        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "r");
912        byte[] rbuf = new byte[100];
913        raf.close();
914        int read = raf.read(rbuf, 0, 0);
915        assertEquals(0, read);
916    }
917
918    /**
919     * java.io.RandomAccessFile#read(byte[])
920     */
921    public void test_read_$B_IOException() throws IOException {
922        FileOutputStream fos = new java.io.FileOutputStream(fileName);
923        fos.write(fileString.getBytes(), 0, fileString.length());
924        fos.close();
925
926        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "r");
927        byte[] rbuf = new byte[0];
928        raf.close();
929        int read = raf.read(rbuf);
930        assertEquals(0, read);
931    }
932
933    /**
934     * java.io.RandomAccessFile#read(byte[], int, int)
935     */
936    public void test_read_$BII_NullPointerException() throws IOException {
937        File f = File.createTempFile("tmp", "tmp");
938        f.deleteOnExit();
939        RandomAccessFile raf = new RandomAccessFile(f, "r");
940        byte[] rbuf = null;
941        try {
942            raf.read(rbuf, 0, -1);
943            fail("should throw NullPointerException");
944        } catch (NullPointerException e) {
945            // expected
946        }
947        raf.close();
948    }
949
950    /**
951     * java.io.RandomAccessFile#write(byte[], int, int)
952     * <p/>
953     * Regression for HARMONY-377
954     */
955    public void test_writeBII() throws IOException {
956        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
957        try {
958            raf.write(new byte[1], -1, 1);
959            fail("IndexOutOfBoundsException must be thrown if off <0");
960        } catch (IndexOutOfBoundsException e) {
961        }
962
963        try {
964            raf.write(new byte[1], 0, -1);
965            fail("IndexOutOfBoundsException must be thrown if len <0");
966        } catch (IndexOutOfBoundsException e) {
967        }
968
969        try {
970            raf.write(new byte[1], 0, 5);
971            fail("IndexOutOfBoundsException must be thrown if off+len > b.length");
972        } catch (IndexOutOfBoundsException e) {
973        }
974
975        try {
976            raf.write(new byte[10], Integer.MAX_VALUE, 5);
977            fail("IndexOutOfBoundsException expected");
978        } catch (IndexOutOfBoundsException e) {
979        }
980
981        try {
982            raf.write(new byte[10], 5, Integer.MAX_VALUE);
983            fail("IndexOutOfBoundsException expected");
984        } catch (IndexOutOfBoundsException e) {
985        }
986        raf.close();
987    }
988
989    /**
990     * Regression for HARMONY-69
991     */
992    public void testRandomAccessFile_String_String() throws IOException {
993        f.createNewFile();
994        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "r");
995        FileChannel fcr = raf.getChannel();
996
997        try {
998            fcr.lock(0L, Long.MAX_VALUE, false);
999            fail("NonWritableChannelException expected!");
1000        } catch (NonWritableChannelException e) {
1001        }
1002        raf.close();
1003    }
1004
1005    // Regression test for HARMONY-6542
1006    public void testRandomAccessFile_seekMoreThan2gb() throws IOException {
1007        if (File.separator != "/") {
1008            // skip windows until a test can be implemented that doesn't
1009            // require 2GB of free disk space
1010            return;
1011        }
1012        // (all?) unix platforms support sparse files so this should not
1013        // need to have 2GB free disk space to pass
1014        RandomAccessFile raf = new RandomAccessFile(f, "rw");
1015        // write a few bytes so we get more helpful error messages
1016        // if we land in the wrong places
1017        raf.write(1);
1018        raf.write(2);
1019        raf.seek(2147483647);
1020        raf.write(3);
1021        raf.write(4);
1022        raf.write(5);
1023        raf.write(6);
1024        raf.seek(0);
1025        assertEquals("seek 0", 1, raf.read());
1026        raf.seek(2147483649L);
1027        assertEquals("seek >2gb", 5, raf.read());
1028        raf.seek(0);
1029        assertEquals("seek back to 0", 1, raf.read());
1030        raf.close();
1031    }
1032
1033    /**
1034     * Sets up the fixture, for example, open a network connection. This method
1035     * is called before a test is executed.
1036     */
1037    protected void setUp() throws Exception {
1038        super.setUp();
1039        f = File.createTempFile("raf", "tst");
1040        if (!f.delete()) {
1041            fail("Unable to delete test file : " + f);
1042        }
1043        fileName = f.getAbsolutePath();
1044    }
1045
1046    /**
1047     * Tears down the fixture, for example, close a network connection. This
1048     * method is called after a test is executed.
1049     *
1050     * @throws Exception
1051     */
1052    protected void tearDown() throws Exception {
1053        if (f.exists()) {
1054            f.delete();
1055        }
1056        super.tearDown();
1057    }
1058
1059}
1060