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 libcore.java.io;
19
20import java.io.EOFException;
21import java.io.File;
22import java.io.FileInputStream;
23import java.io.FileOutputStream;
24import java.io.FileNotFoundException;
25import java.io.IOException;
26import java.io.RandomAccessFile;
27
28import java.nio.channels.FileChannel;
29import java.nio.channels.NonWritableChannelException;
30
31public class OldRandomAccessFileTest extends junit.framework.TestCase {
32
33    public String fileName;
34
35    public boolean ufile = true;
36
37    java.io.RandomAccessFile raf;
38
39    java.io.File f;
40
41    String unihw = "\u0048\u0065\u006C\u0801\u006C\u006F\u0020\u0057\u0081\u006F\u0072\u006C\u0064";
42
43    static final String testString = "Lorem ipsum dolor sit amet,\n" +
44    "consectetur adipisicing elit,\nsed do eiusmod tempor incididunt ut" +
45    "labore et dolore magna aliqua.\n";
46    static final int testLength = testString.length();
47
48    /**
49     * java.io.RandomAccessFile#RandomAccessFile(java.io.File,
50     *        java.lang.String)
51     */
52    public void test_ConstructorLjava_io_FileLjava_lang_String() throws Exception {
53        RandomAccessFile raf = null;
54        File tmpFile = new File(fileName);
55
56        try {
57            raf = new java.io.RandomAccessFile(tmpFile, "r");
58            fail("Test 1: FileNotFoundException expected.");
59        } catch (FileNotFoundException e) {
60            // Expected.
61        } catch (IllegalArgumentException e) {
62            fail("Test 2: Unexpected IllegalArgumentException: " + e.getMessage());
63        }
64
65        tmpFile.createNewFile();
66
67        try {
68            // Checking the remaining valid mode parameters.
69            try {
70                raf = new java.io.RandomAccessFile(tmpFile, "rwd");
71            } catch (IllegalArgumentException e) {
72                fail("Test 3: Unexpected IllegalArgumentException: " + e.getMessage());
73            }
74            raf.close();
75            try {
76                raf = new java.io.RandomAccessFile(tmpFile, "rws");
77            } catch (IllegalArgumentException e) {
78                fail("Test 4: Unexpected IllegalArgumentException: " + e.getMessage());
79            }
80            raf.close();
81            try {
82                raf = new java.io.RandomAccessFile(tmpFile, "rw");
83            } catch (IllegalArgumentException e) {
84                fail("Test 5: Unexpected IllegalArgumentException: " + e.getMessage());
85            }
86            raf.close();
87
88            // Checking an invalid mode parameter.
89            try {
90                raf = new java.io.RandomAccessFile(tmpFile, "i");
91                fail("Test 6: IllegalArgumentException expected.");
92            } catch (IllegalArgumentException e) {
93                // Expected.
94            }
95        } finally {
96            if (raf != null ) raf.close();
97            tmpFile.delete();
98        }
99    }
100
101    /**
102     * java.io.RandomAccessFile#RandomAccessFile(java.lang.String,
103     *        java.lang.String)
104     */
105    public void test_ConstructorLjava_lang_StringLjava_lang_String()
106            throws IOException {
107        RandomAccessFile raf = null;
108        File tmpFile = new File(fileName);
109
110        try {
111            raf = new java.io.RandomAccessFile(fileName, "r");
112            fail("Test 1: FileNotFoundException expected.");
113        } catch (FileNotFoundException e) {
114            // Expected.
115        } catch (IllegalArgumentException e) {
116            fail("Test 2: Unexpected IllegalArgumentException: " + e.getMessage());
117        }
118
119        try {
120            // Checking the remaining valid mode parameters.
121            try {
122                raf = new java.io.RandomAccessFile(fileName, "rwd");
123            } catch (IllegalArgumentException e) {
124                fail("Test 3: Unexpected IllegalArgumentException: " + e.getMessage());
125            }
126            raf.close();
127            try {
128                raf = new java.io.RandomAccessFile(fileName, "rws");
129            } catch (IllegalArgumentException e) {
130                fail("Test 4: Unexpected IllegalArgumentException: " + e.getMessage());
131            }
132            raf.close();
133            try {
134                raf = new java.io.RandomAccessFile(fileName, "rw");
135            } catch (IllegalArgumentException e) {
136                fail("Test 5: Unexpected IllegalArgumentException: " + e.getMessage());
137            }
138            raf.close();
139
140            // Checking an invalid mode parameter.
141            try {
142                raf = new java.io.RandomAccessFile(fileName, "i");
143                fail("Test 6: IllegalArgumentException expected.");
144            } catch (IllegalArgumentException e) {
145                // Expected.
146            }
147
148            // Checking for NoWritableChannelException.
149            raf = new java.io.RandomAccessFile(fileName, "r");
150            FileChannel fcr = raf.getChannel();
151
152            try {
153                fcr.lock(0L, Long.MAX_VALUE, false);
154                fail("Test 7: NonWritableChannelException expected.");
155            } catch (NonWritableChannelException e) {
156                // Expected.
157            }
158
159        } finally {
160            if (raf != null ) raf.close();
161            if (tmpFile.exists()) tmpFile.delete();
162        }
163    }
164
165    /**
166     * java.io.RandomAccessFile#close()
167     */
168    public void test_close() {
169        // Test for method void java.io.RandomAccessFile.close()
170        try {
171            RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
172            raf.close();
173            raf.write("Test".getBytes(), 0, 4);
174            fail("Failed to close file properly.");
175        } catch (IOException e) {}
176    }
177
178    /**
179     * java.io.RandomAccessFile#getChannel()
180     */
181    public void test_getChannel() throws IOException {
182
183        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
184        FileChannel fc = raf.getChannel();
185
186        // Indirect test: If the file's file pointer moves then the position
187        // in the channel has to move accordingly.
188        assertTrue("Test 1: Channel position expected to be 0.", fc.position() == 0);
189
190        raf.write(testString.getBytes());
191        assertEquals("Test 2: Unexpected channel position.",
192                testLength, fc.position());
193        assertTrue("Test 3: Channel position is not equal to file pointer.",
194                fc.position() == raf.getFilePointer());
195        raf.close();
196    }
197
198    /**
199     * java.io.RandomAccessFile#getFD()
200     */
201    public void test_getFD() throws IOException {
202        // Test for method java.io.FileDescriptor
203        // java.io.RandomAccessFile.getFD()
204
205        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
206        assertTrue("Test 1: Returned invalid fd.", raf.getFD().valid());
207
208        raf.close();
209        assertFalse("Test 2: Returned valid fd after close", raf.getFD().valid());
210    }
211
212    /**
213     * java.io.RandomAccessFile#getFilePointer()
214     */
215    public void test_getFilePointer() throws IOException {
216        // Test for method long java.io.RandomAccessFile.getFilePointer()
217        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
218        raf.write(testString.getBytes(), 0, testLength);
219        assertEquals("Test 1: Incorrect filePointer returned. ", testLength, raf
220                .getFilePointer());
221        raf.close();
222        try {
223            raf.getFilePointer();
224            fail("Test 2: IOException expected.");
225        } catch (IOException e) {
226            // Expected.
227        }
228    }
229
230    /**
231     * java.io.RandomAccessFile#length()
232     */
233    public void test_length() throws IOException {
234        // Test for method long java.io.RandomAccessFile.length()
235        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
236        raf.write(testString.getBytes());
237        assertEquals("Test 1: Incorrect length returned. ", testLength,
238                raf.length());
239        raf.close();
240        try {
241            raf.length();
242            fail("Test 2: IOException expected.");
243        } catch (IOException e) {
244            // Expected.
245        }
246    }
247
248    /**
249     * java.io.RandomAccessFile#read()
250     */
251    public void test_read_write() throws IOException {
252        int i;
253        byte[] testBuf = testString.getBytes();
254        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
255        for (i = 0; i < testString.length(); i++) {
256            try {
257                raf.write(testBuf[i]);
258            } catch (Exception e) {
259                fail("Test 1: Unexpected exception while writing: "
260                        + e.getMessage());
261            }
262        }
263
264        raf.seek(0);
265
266        for (i = 0; i < testString.length(); i++) {
267            assertEquals(String.format("Test 2: Incorrect value written or read at index %d; ", i),
268                    testBuf[i], raf.read());
269        }
270
271        assertTrue("Test 3: End of file indicator (-1) expected.", raf.read() == -1);
272
273        raf.close();
274        try {
275            raf.write(42);
276            fail("Test 4: IOException expected.");
277        } catch (IOException e) {
278            // Expected.
279        }
280        try {
281            raf.read();
282            fail("Test 5: IOException expected.");
283        } catch (IOException e) {
284            // Expected.
285        }
286    }
287
288    /**
289     * java.io.RandomAccessFile#read(byte[])
290     */
291    public void test_read$B() throws IOException {
292        FileOutputStream fos = new java.io.FileOutputStream(fileName);
293        fos.write(testString.getBytes(), 0, testLength);
294        fos.close();
295
296        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "r");
297        byte[] rbuf = new byte[testLength + 10];
298
299        int bytesRead = raf.read(rbuf);
300        assertEquals("Test 1: Incorrect number of bytes read. ",
301                testLength, bytesRead);
302        assertEquals("Test 2: Incorrect bytes read. ", testString,
303                new String(rbuf, 0, testLength));
304
305        bytesRead = raf.read(rbuf);
306        assertTrue("Test 3: EOF (-1) expected. ", bytesRead == -1);
307
308        raf.close();
309        try {
310            bytesRead = raf.read(rbuf);
311            fail("Test 4: IOException expected.");
312        } catch (IOException e) {
313            // Expected.
314        }
315    }
316
317    /**
318     * java.io.RandomAccessFile#read(byte[], int, int)
319     */
320    public void test_read$BII() throws IOException {
321        int bytesRead;
322        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
323        byte[] rbuf = new byte[4000];
324
325        FileOutputStream fos = new java.io.FileOutputStream(fileName);
326        fos.write(testString.getBytes(), 0, testLength);
327        fos.close();
328
329        // Read half of the file contents.
330        bytesRead = raf.read(rbuf, 10, testLength / 2);
331        assertEquals("Test 1: Incorrect number of bytes read. ",
332                testLength / 2, bytesRead);
333        assertEquals("Test 2: Incorrect bytes read. ",
334                testString.substring(0, testLength / 2),
335                new String(rbuf, 10, testLength / 2));
336
337        // Read the rest of the file contents.
338        bytesRead = raf.read(rbuf, 0, testLength);
339        assertEquals("Test 3: Incorrect number of bytes read. ",
340                testLength - (testLength / 2), bytesRead);
341        assertEquals("Test 4: Incorrect bytes read. ",
342                testString.substring(testLength / 2, (testLength / 2) + bytesRead),
343                new String(rbuf, 0, bytesRead));
344
345        // Try to read even more.
346        bytesRead = raf.read(rbuf, 0, 1);
347        assertTrue("Test 5: EOF (-1) expected. ", bytesRead == -1);
348
349        // Illegal parameter value tests.
350        try {
351            raf.read(rbuf, -1, 1);
352            fail("Test 6: IndexOutOfBoundsException expected.");
353        } catch (IndexOutOfBoundsException e) {
354            // Expected.
355        }
356        try {
357            raf.read(rbuf, 0, -1);
358            fail("Test 7: IndexOutOfBoundsException expected.");
359        } catch (IndexOutOfBoundsException e) {
360            // Expected.
361        }
362        try {
363            raf.read(rbuf, 2000, 2001);
364            fail("Test 8: IndexOutOfBoundsException expected.");
365        } catch (IndexOutOfBoundsException e) {
366            // Expected.
367        }
368
369        // IOException test.
370        raf.close();
371        try {
372            bytesRead = raf.read(rbuf, 0, 1);
373            fail("Test 9: IOException expected.");
374        } catch (IOException e) {
375            // Expected.
376        }
377    }
378
379    /**
380     * java.io.RandomAccessFile#readBoolean()
381     * java.io.RandomAccessFile#writeBoolean(boolean)
382     */
383    public void test_read_writeBoolean() throws IOException {
384        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
385        raf.writeBoolean(true);
386        raf.writeBoolean(false);
387        raf.seek(0);
388
389        assertEquals("Test 1: Incorrect value written or read;",
390                true, raf.readBoolean());
391        assertEquals("Test 2: Incorrect value written or read;",
392                false, raf.readBoolean());
393
394        try {
395            raf.readBoolean();
396            fail("Test 3: EOFException expected.");
397        } catch (EOFException e) {
398            // Expected.
399        }
400
401        raf.close();
402        try {
403            raf.writeBoolean(false);
404            fail("Test 4: IOException expected.");
405        } catch (IOException e) {
406            // Expected.
407        }
408        try {
409            raf.readBoolean();
410            fail("Test 5: IOException expected.");
411        } catch (IOException e) {
412            // Expected.
413        }
414    }
415
416    /**
417     * java.io.RandomAccessFile#readByte()
418     * java.io.RandomAccessFile#writeByte(byte)
419     */
420    public void test_read_writeByte() throws IOException {
421        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
422        raf.writeByte(Byte.MIN_VALUE);
423        raf.writeByte(11);
424        raf.writeByte(Byte.MAX_VALUE);
425        raf.writeByte(Byte.MIN_VALUE - 1);
426        raf.writeByte(Byte.MAX_VALUE + 1);
427        raf.seek(0);
428
429        assertEquals("Test 1: Incorrect value written or read;",
430                Byte.MIN_VALUE, raf.readByte());
431        assertEquals("Test 2: Incorrect value written or read;",
432                11, raf.readByte());
433        assertEquals("Test 3: Incorrect value written or read;",
434                Byte.MAX_VALUE, raf.readByte());
435        assertEquals("Test 4: Incorrect value written or read;",
436                127, raf.readByte());
437        assertEquals("Test 5: Incorrect value written or read;",
438                -128, raf.readByte());
439
440        try {
441            raf.readByte();
442            fail("Test 6: EOFException expected.");
443        } catch (EOFException e) {
444            // Expected.
445        }
446
447        raf.close();
448        try {
449            raf.writeByte(13);
450            fail("Test 7: IOException expected.");
451        } catch (IOException e) {
452            // Expected.
453        }
454        try {
455            raf.readByte();
456            fail("Test 8: IOException expected.");
457        } catch (IOException e) {
458            // Expected.
459        }
460    }
461
462    /**
463     * java.io.RandomAccessFile#readChar()
464     * java.io.RandomAccessFile#writeChar(char)
465     */
466    public void test_read_writeChar() throws IOException {
467        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
468        raf.writeChar(Character.MIN_VALUE);
469        raf.writeChar('T');
470        raf.writeChar(Character.MAX_VALUE);
471        raf.writeChar(Character.MIN_VALUE - 1);
472        raf.writeChar(Character.MAX_VALUE + 1);
473        raf.seek(0);
474
475        assertEquals("Test 1: Incorrect value written or read;",
476                Character.MIN_VALUE, raf.readChar());
477        assertEquals("Test 2: Incorrect value written or read;",
478                'T', raf.readChar());
479        assertEquals("Test 3: Incorrect value written or read;",
480                Character.MAX_VALUE, raf.readChar());
481        assertEquals("Test 4: Incorrect value written or read;",
482                0xffff, raf.readChar());
483        assertEquals("Test 5: Incorrect value written or read;",
484                0, raf.readChar());
485
486        try {
487            raf.readChar();
488            fail("Test 6: EOFException expected.");
489        } catch (EOFException e) {
490            // Expected.
491        }
492
493        raf.close();
494        try {
495            raf.writeChar('E');
496            fail("Test 7: IOException expected.");
497        } catch (IOException e) {
498            // Expected.
499        }
500        try {
501            raf.readChar();
502            fail("Test 8: IOException expected.");
503        } catch (IOException e) {
504            // Expected.
505        }
506    }
507
508    /**
509     * java.io.RandomAccessFile#readDouble()
510     * java.io.RandomAccessFile#writeDouble(double)
511     */
512    public void test_read_writeDouble() throws IOException {
513        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
514        raf.writeDouble(Double.MAX_VALUE);
515        raf.writeDouble(424242.4242);
516        raf.seek(0);
517
518        assertEquals("Test 1: Incorrect value written or read;",
519                Double.MAX_VALUE, raf.readDouble());
520        assertEquals("Test 2: Incorrect value written or read;",
521                424242.4242, raf.readDouble());
522
523        try {
524            raf.readDouble();
525            fail("Test 3: EOFException expected.");
526        } catch (EOFException e) {
527            // Expected.
528        }
529
530        raf.close();
531        try {
532            raf.writeDouble(Double.MIN_VALUE);
533            fail("Test 4: IOException expected.");
534        } catch (IOException e) {
535            // Expected.
536        }
537        try {
538            raf.readDouble();
539            fail("Test 5: IOException expected.");
540        } catch (IOException e) {
541            // Expected.
542        }
543    }
544
545    /**
546     * java.io.RandomAccessFile#readFloat()
547     * java.io.RandomAccessFile#writeFloat(double)
548     */
549    public void test_read_writeFloat() throws IOException {
550        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
551        raf.writeFloat(Float.MAX_VALUE);
552        raf.writeFloat(555.55f);
553        raf.seek(0);
554
555        assertEquals("Test 1: Incorrect value written or read. ",
556                Float.MAX_VALUE, raf.readFloat());
557        assertEquals("Test 2: Incorrect value written or read. ",
558                555.55f, raf.readFloat());
559
560        try {
561            raf.readFloat();
562            fail("Test 3: EOFException expected.");
563        } catch (EOFException e) {
564            // Expected.
565        }
566
567        raf.close();
568        try {
569            raf.writeFloat(Float.MIN_VALUE);
570            fail("Test 4: IOException expected.");
571        } catch (IOException e) {
572            // Expected.
573        }
574        try {
575            raf.readFloat();
576            fail("Test 5: IOException expected.");
577        } catch (IOException e) {
578            // Expected.
579        }
580    }
581
582    /**
583     * java.io.RandomAccessFile#readInt()
584     * java.io.RandomAccessFile#writeInt(char)
585     */
586    public void test_read_writeInt() throws IOException {
587        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
588        raf.writeInt(Integer.MIN_VALUE);
589        raf.writeInt('T');
590        raf.writeInt(Integer.MAX_VALUE);
591        raf.writeInt(Integer.MIN_VALUE - 1);
592        raf.writeInt(Integer.MAX_VALUE + 1);
593        raf.seek(0);
594
595        assertEquals("Test 1: Incorrect value written or read;",
596                Integer.MIN_VALUE, raf.readInt());
597        assertEquals("Test 2: Incorrect value written or read;",
598                'T', raf.readInt());
599        assertEquals("Test 3: Incorrect value written or read;",
600                Integer.MAX_VALUE, raf.readInt());
601        assertEquals("Test 4: Incorrect value written or read;",
602                0x7fffffff, raf.readInt());
603        assertEquals("Test 5: Incorrect value written or read;",
604                0x80000000, raf.readInt());
605
606        try {
607            raf.readInt();
608            fail("Test 6: EOFException expected.");
609        } catch (EOFException e) {
610            // Expected.
611        }
612
613        raf.close();
614        try {
615            raf.writeInt('E');
616            fail("Test 7: IOException expected.");
617        } catch (IOException e) {
618            // Expected.
619        }
620        try {
621            raf.readInt();
622            fail("Test 8: IOException expected.");
623        } catch (IOException e) {
624            // Expected.
625        }
626    }
627
628    /**
629     * java.io.RandomAccessFile#readLong()
630     * java.io.RandomAccessFile#writeLong(char)
631     */
632    public void test_read_writeLong() throws IOException {
633        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
634        raf.writeLong(Long.MIN_VALUE);
635        raf.writeLong('T');
636        raf.writeLong(Long.MAX_VALUE);
637        raf.writeLong(Long.MIN_VALUE - 1);
638        raf.writeLong(Long.MAX_VALUE + 1);
639        raf.seek(0);
640
641        assertEquals("Test 1: Incorrect value written or read;",
642                Long.MIN_VALUE, raf.readLong());
643        assertEquals("Test 2: Incorrect value written or read;",
644                'T', raf.readLong());
645        assertEquals("Test 3: Incorrect value written or read;",
646                Long.MAX_VALUE, raf.readLong());
647        assertEquals("Test 4: Incorrect value written or read;",
648                0x7fffffffffffffffl, raf.readLong());
649        assertEquals("Test 5: Incorrect value written or read;",
650                0x8000000000000000l, raf.readLong());
651
652        try {
653            raf.readLong();
654            fail("Test 6: EOFException expected.");
655        } catch (EOFException e) {
656            // Expected.
657        }
658
659        raf.close();
660        try {
661            raf.writeLong('E');
662            fail("Test 7: IOException expected.");
663        } catch (IOException e) {
664            // Expected.
665        }
666        try {
667            raf.readLong();
668            fail("Test 8: IOException expected.");
669        } catch (IOException e) {
670            // Expected.
671        }
672    }
673
674    /**
675     * java.io.RandomAccessFile#readShort()
676     * java.io.RandomAccessFile#writeShort(short)
677     */
678    public void test_read_writeShort() throws IOException {
679        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
680        raf.writeShort(Short.MIN_VALUE);
681        raf.writeShort('T');
682        raf.writeShort(Short.MAX_VALUE);
683        raf.writeShort(Short.MIN_VALUE - 1);
684        raf.writeShort(Short.MAX_VALUE + 1);
685        raf.seek(0);
686
687        assertEquals("Test 1: Incorrect value written or read;",
688                Short.MIN_VALUE, raf.readShort());
689        assertEquals("Test 2: Incorrect value written or read;",
690                'T', raf.readShort());
691        assertEquals("Test 3: Incorrect value written or read;",
692                Short.MAX_VALUE, raf.readShort());
693        assertEquals("Test 4: Incorrect value written or read;",
694                0x7fff, raf.readShort());
695        assertEquals("Test 5: Incorrect value written or read;",
696                (short) 0x8000, raf.readShort());
697
698        try {
699            raf.readShort();
700            fail("Test 6: EOFException expected.");
701        } catch (EOFException e) {
702            // Expected.
703        }
704
705        raf.close();
706        try {
707            raf.writeShort('E');
708            fail("Test 7: IOException expected.");
709        } catch (IOException e) {
710            // Expected.
711        }
712        try {
713            raf.readShort();
714            fail("Test 8: IOException expected.");
715        } catch (IOException e) {
716            // Expected.
717        }
718    }
719
720    /**
721     * java.io.RandomAccessFile#readUTF()
722     * java.io.RandomAccessFile#writeShort(char)
723     */
724    public void test_read_writeUTF() throws IOException {
725        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
726        raf.writeUTF(unihw);
727        raf.seek(0);
728        assertEquals("Test 1: Incorrect UTF string written or read;",
729                unihw, raf.readUTF());
730
731        try {
732            raf.readUTF();
733            fail("Test 2: EOFException expected.");
734        } catch (EOFException e) {
735            // Expected.
736        }
737
738        raf.close();
739        try {
740            raf.writeUTF("Already closed.");
741            fail("Test 3: IOException expected.");
742        } catch (IOException e) {
743            // Expected.
744        }
745        try {
746            raf.readUTF();
747            fail("Test 4: IOException expected.");
748        } catch (IOException e) {
749            // Expected.
750        }
751    }
752
753    /**
754     * java.io.RandomAccessFile#writeBytes(java.lang.String)
755     * java.io.RandomAccessFile#readFully(byte[])
756     */
757    public void test_readFully$B_writeBytesLjava_lang_String() throws IOException {
758        byte[] buf = new byte[testLength];
759        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
760        raf.writeBytes(testString);
761        raf.seek(0);
762
763        try {
764            raf.readFully(null);
765            fail("Test 1: NullPointerException expected.");
766        } catch (NullPointerException e) {
767            // Expected.
768        }
769
770        raf.readFully(buf);
771        assertEquals("Test 2: Incorrect bytes written or read;",
772                testString, new String(buf));
773
774        try {
775            raf.readFully(buf);
776            fail("Test 3: EOFException expected.");
777        } catch (EOFException e) {
778            // Expected.
779        }
780
781        raf.close();
782        try {
783            raf.writeBytes("Already closed.");
784            fail("Test 4: IOException expected.");
785        } catch (IOException e) {
786            // Expected.
787        }
788        try {
789            raf.readFully(buf);
790            fail("Test 5: IOException expected.");
791        } catch (IOException e) {
792            // Expected.
793        }
794    }
795
796    /**
797     * java.io.RandomAccessFile#writeBytes(java.lang.String)
798     * java.io.RandomAccessFile#readFully(byte[], int, int)
799     */
800    public void test_readFully$BII() throws IOException {
801        byte[] buf = new byte[testLength];
802        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
803        raf.writeBytes(testString);
804        raf.seek(0);
805
806        try {
807            raf.readFully(null);
808            fail("Test 1: NullPointerException expected.");
809        } catch (NullPointerException e) {
810            // Expected.
811        }
812
813        raf.readFully(buf, 5, testLength - 10);
814        for (int i = 0; i < 5; i++) {
815            assertEquals("Test 2: Incorrect bytes read;", 0, buf[i]);
816        }
817        assertEquals("Test 3: Incorrect bytes written or read;",
818                testString.substring(0, testLength - 10),
819                new String(buf, 5, testLength - 10));
820
821        // Reading past the end of the file.
822        try {
823            raf.readFully(buf, 3, testLength - 6);
824            fail("Test 4: EOFException expected.");
825        } catch (EOFException e) {
826            // Expected.
827        }
828
829        // Passing invalid arguments.
830        try {
831            raf.readFully(buf, -1, 1);
832            fail("Test 5: IndexOutOfBoundsException expected.");
833        } catch (IndexOutOfBoundsException e) {
834            // Expected.
835        }
836        try {
837            raf.readFully(buf, 0, -1);
838            fail("Test 6: IndexOutOfBoundsException expected.");
839        } catch (IndexOutOfBoundsException e) {
840            // Expected.
841        }
842        try {
843            raf.readFully(buf, 2, testLength);
844            fail("Test 7: IndexOutOfBoundsException expected.");
845        } catch (IndexOutOfBoundsException e) {
846            // Expected.
847        }
848
849        // Reading from a closed file.
850        raf.close();
851        try {
852            raf.readFully(buf);
853            fail("Test 8: IOException expected.");
854        } catch (IOException e) {
855            // Expected.
856        }
857    }
858
859    /**
860     * java.io.RandomAccessFile#readUnsignedByte()
861     */
862    public void test_readUnsignedByte() throws IOException {
863        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
864        raf.writeByte(-1);
865        raf.seek(0);
866
867        assertEquals("Test 1: Incorrect value written or read;",
868                255, raf.readUnsignedByte());
869
870        try {
871            raf.readUnsignedByte();
872            fail("Test 2: EOFException expected.");
873        } catch (EOFException e) {
874            // Expected.
875        }
876
877        raf.close();
878        try {
879            raf.readUnsignedByte();
880            fail("Test 3: IOException expected.");
881        } catch (IOException e) {
882            // Expected.
883        }
884    }
885
886    /**
887     * java.io.RandomAccessFile#readUnsignedShort()
888     */
889    public void test_readUnsignedShort() throws IOException {
890        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
891        raf.writeShort(-1);
892        raf.seek(0);
893
894        assertEquals("Test 1: Incorrect value written or read;",
895                65535, raf.readUnsignedShort());
896
897        try {
898            raf.readUnsignedShort();
899            fail("Test 2: EOFException expected.");
900        } catch (EOFException e) {
901            // Expected.
902        }
903
904        raf.close();
905        try {
906            raf.readUnsignedShort();
907            fail("Test 3: IOException expected.");
908        } catch (IOException e) {
909            // Expected.
910        }
911    }
912
913    /**
914     * java.io.RandomAccessFile#readLine()
915     */
916    public void test_readLine() throws IOException {
917        // Test for method java.lang.String java.io.RandomAccessFile.readLine()
918        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
919        String s = "Goodbye\nCruel\nWorld\n";
920        raf.write(s.getBytes(), 0, s.length());
921        raf.seek(0);
922
923        assertEquals("Test 1: Incorrect line read;", "Goodbye", raf.readLine());
924        assertEquals("Test 2: Incorrect line read;", "Cruel", raf.readLine());
925        assertEquals("Test 3: Incorrect line read;", "World", raf.readLine());
926        assertNull("Test 4: Incorrect line read; null expected.", raf.readLine());
927
928        raf.close();
929        try {
930            raf.readLine();
931            fail("Test 5: IOException expected.");
932        } catch (IOException e) {
933            // Expected.
934        }
935
936    }
937
938    /**
939     * java.io.RandomAccessFile#seek(long)
940     */
941    public void test_seekJ() throws IOException {
942        // Test for method void java.io.RandomAccessFile.seek(long)
943        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
944
945        try {
946            raf.seek(-1);
947            fail("Test 1: IOException expected.");
948        } catch (IOException e) {
949            // Expected.
950        }
951
952        raf.write(testString.getBytes(), 0, testLength);
953        raf.seek(12);
954        assertEquals("Test 3: Seek failed to set file pointer.", 12,
955                raf.getFilePointer());
956
957        raf.close();
958        try {
959            raf.seek(1);
960            fail("Test 4: IOException expected.");
961        } catch (IOException e) {
962            // Expected.
963        }
964    }
965
966    /**
967     * java.io.RandomAccessFile#skipBytes(int)
968     */
969    public void test_skipBytesI() throws IOException {
970        byte[] buf = new byte[5];
971        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
972        raf.writeBytes("HelloWorld");
973        raf.seek(0);
974
975        assertTrue("Test 1: Nothing should be skipped if parameter is less than zero",
976                raf.skipBytes(-1) == 0);
977
978        assertEquals("Test 4: Incorrect number of bytes skipped; ",
979                5, raf.skipBytes(5));
980
981        raf.readFully(buf);
982        assertEquals("Test 3: Failed to skip bytes.",
983                "World", new String(buf, 0, 5));
984
985        raf.seek(0);
986        assertEquals("Test 4: Incorrect number of bytes skipped; ",
987                10, raf.skipBytes(20));
988
989        raf.close();
990        try {
991            raf.skipBytes(1);
992            fail("Test 5: IOException expected.");
993        } catch (IOException e) {
994            // Expected.
995        }
996    }
997
998    /**
999     * java.io.RandomAccessFile#skipBytes(int)
1000     */
1001    public void test_setLengthJ() throws IOException {
1002        int bytesRead;
1003        long truncLength = (long) (testLength * 0.75);
1004        byte[] rbuf = new byte[testLength + 10];
1005
1006        // Setup the test file.
1007        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
1008        raf.write(testString.getBytes());
1009        assertEquals("Test 1: Incorrect file length;",
1010                testLength, raf.length());
1011
1012        // Truncate the file.
1013        raf.setLength(truncLength);
1014        assertTrue("Test 2: File pointer not moved to the end of the truncated file.",
1015                raf.getFilePointer() == truncLength);
1016
1017        raf.close();
1018        raf = new java.io.RandomAccessFile(fileName, "rw");
1019        assertEquals("Test 3: Incorrect file length;",
1020                truncLength, raf.length());
1021        bytesRead = raf.read(rbuf);
1022        assertEquals("Test 4: Incorrect number of bytes read;",
1023                truncLength, bytesRead);
1024        assertEquals("Test 5: Incorrect bytes read. ",
1025                testString.substring(0, bytesRead),
1026                new String(rbuf, 0, bytesRead));
1027
1028        // Expand the file.
1029        raf.setLength(testLength + 2);
1030        assertTrue("Test 6: File pointer incorrectly moved.",
1031                raf.getFilePointer() == truncLength);
1032        assertEquals("Test 7: Incorrect file length;",
1033                testLength + 2, raf.length());
1034
1035        // Exception testing.
1036        try {
1037            raf.setLength(-1);
1038            fail("Test 9: IllegalArgumentException expected.");
1039        } catch (IOException expected) {
1040        } catch (IllegalArgumentException expected) {
1041        }
1042
1043        raf.close();
1044        try {
1045            raf.setLength(truncLength);
1046            fail("Test 10: IOException expected.");
1047        } catch (IOException e) {
1048            // Expected.
1049        }
1050    }
1051
1052    /**
1053     * java.io.RandomAccessFile#write(byte[])
1054     */
1055    public void test_write$B() throws IOException {
1056        byte[] rbuf = new byte[4000];
1057        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
1058
1059        byte[] nullByteArray = null;
1060        try {
1061            raf.write(nullByteArray);
1062            fail("Test 1: NullPointerException expected.");
1063        } catch (NullPointerException e) {
1064            // Expected.
1065        }
1066
1067        try {
1068            raf.write(testString.getBytes());
1069        } catch (Exception e) {
1070            fail("Test 2: Unexpected exception: " + e.getMessage());
1071        }
1072
1073        raf.close();
1074
1075        try {
1076            raf.write(new byte[0]);
1077        } catch (IOException e) {
1078            fail("Test 3: Unexpected IOException: " + e.getMessage());
1079        }
1080
1081        try {
1082            raf.write(testString.getBytes());
1083            fail("Test 4: IOException expected.");
1084        } catch (IOException e) {
1085            // Expected.
1086        }
1087
1088        FileInputStream fis = new java.io.FileInputStream(fileName);
1089        fis.read(rbuf, 0, testLength);
1090        assertEquals("Incorrect bytes written", testString, new String(rbuf, 0,
1091                testLength));
1092    }
1093
1094    /**
1095     * java.io.RandomAccessFile#write(byte[], int, int)
1096     */
1097    public void test_write$BII() throws Exception {
1098        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
1099        byte[] rbuf = new byte[4000];
1100        byte[] testBuf = null;
1101        int bytesRead;
1102
1103        try {
1104            raf.write(testBuf, 1, 1);
1105            fail("Test 1: NullPointerException expected.");
1106        } catch (NullPointerException e) {
1107            // Expected.
1108        }
1109
1110        testBuf = testString.getBytes();
1111
1112        try {
1113            raf.write(testBuf, -1, 10);
1114            fail("Test 2: IndexOutOfBoundsException expected.");
1115        } catch (IndexOutOfBoundsException expected) {
1116        }
1117
1118        try {
1119            raf.write(testBuf, 0, -1);
1120            fail("Test 3: IndexOutOfBoundsException expected.");
1121        } catch (IndexOutOfBoundsException expected) {
1122        }
1123
1124        try {
1125            raf.write(testBuf, 5, testLength);
1126            fail("Test 4: IndexOutOfBoundsException expected.");
1127        } catch (IndexOutOfBoundsException expected) {
1128        }
1129
1130        // Positive test: The following write should not fail.
1131        try {
1132            raf.write(testBuf, 3, testLength - 5);
1133        } catch (Exception e) {
1134            fail("Test 5: Unexpected exception: " + e.getMessage());
1135        }
1136
1137        raf.close();
1138
1139        // Writing nothing to a closed file should not fail either.
1140        try {
1141            raf.write(new byte[0]);
1142        } catch (IOException e) {
1143            fail("Test 6: Unexpected IOException: " + e.getMessage());
1144        }
1145
1146        // Writing something to a closed file should fail.
1147        try {
1148            raf.write(testString.getBytes());
1149            fail("Test 7: IOException expected.");
1150        } catch (IOException e) {
1151            // Expected.
1152        }
1153
1154        FileInputStream fis = new java.io.FileInputStream(fileName);
1155        bytesRead = fis.read(rbuf, 0, testLength);
1156        assertEquals("Test 8: Incorrect number of bytes written or read;",
1157                testLength - 5, bytesRead);
1158        assertEquals("Test 9: Incorrect bytes written or read; ",
1159                testString.substring(3, testLength - 2),
1160                new String(rbuf, 0, bytesRead));
1161    }
1162
1163    /**
1164     * java.io.RandomAccessFile#writeChars(java.lang.String)
1165     */
1166    public void test_writeCharsLjava_lang_String() throws IOException {
1167        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
1168        raf.writeChars(unihw);
1169        char[] hchars = new char[unihw.length()];
1170        unihw.getChars(0, unihw.length(), hchars, 0);
1171        raf.seek(0);
1172        for (int i = 0; i < hchars.length; i++)
1173            assertEquals("Test 1: Incorrect character written or read at index " + i + ";",
1174                    hchars[i], raf.readChar());
1175        raf.close();
1176        try {
1177            raf.writeChars("Already closed.");
1178            fail("Test 2: IOException expected.");
1179        } catch (IOException e) {
1180            // Expected.
1181        }
1182    }
1183
1184    /**
1185     * Sets up the fixture, for example, open a network connection. This method
1186     * is called before a test is executed.
1187     */
1188    protected void setUp() throws Exception {
1189        super.setUp();
1190
1191        f = File.createTempFile("raf", "tst");
1192        if (!f.delete()) {
1193            fail("Unable to delete test file : " + f);
1194        }
1195        fileName = f.getAbsolutePath();
1196    }
1197
1198    /**
1199     * Tears down the fixture, for example, close a network connection. This
1200     * method is called after a test is executed.
1201     * @throws Exception
1202     */
1203    protected void tearDown() throws Exception {
1204        if (f.exists()) {
1205            f.delete();
1206        }
1207        super.tearDown();
1208    }
1209
1210}
1211