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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.harmony.luni.tests.java.io;
20 import java.io.EOFException;
22 import java.io.FileInputStream;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.io.RandomAccessFile;
27 import java.nio.channels.FileChannel;
28 import java.nio.channels.NonWritableChannelException;
30 public class RandomAccessFileTest extends junit.framework.TestCase {
32 public String fileName;
34 public boolean ufile = true;
36 java.io.RandomAccessFile raf;
40 String unihw = "\u0048\u0065\u006C\u0801\u006C\u006F\u0020\u0057\u0081\u006F\u0072\u006C\u0064";
42 //java.io.FileOutputStream fos;
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";
47 * @tests java.io.RandomAccessFile#RandomAccessFile(java.io.File,
50 public void test_ConstructorLjava_io_FileLjava_lang_String()
52 // Test for method java.io.RandomAccessFile(java.io.File,
54 RandomAccessFile raf = new java.io.RandomAccessFile(f, "rw");
57 assertEquals("Incorrect int read/written", 20, raf.read());
60 raf = new java.io.RandomAccessFile(f, "rwd");
63 assertEquals("Incorrect int read/written", 20, raf.read());
66 raf = new java.io.RandomAccessFile(f, "rws");
69 assertEquals("Incorrect int read/written", 20, raf.read());
72 // Regression for HARMONY-50
73 File f = File.createTempFile("xxx", "yyy");
75 raf = new RandomAccessFile(f, "rws");
78 f = File.createTempFile("xxx", "yyy");
80 raf = new RandomAccessFile(f, "rwd");
85 * @tests java.io.RandomAccessFile#RandomAccessFile(java.lang.String,
88 public void test_ConstructorLjava_lang_StringLjava_lang_String()
90 // Test for method java.io.RandomAccessFile(java.lang.String,
92 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
93 raf.write("Test".getBytes(), 0, 4);
96 raf = new java.io.RandomAccessFile(fileName, "rwd");
97 raf.write("Test".getBytes(), 0, 4);
100 raf = new java.io.RandomAccessFile(fileName, "rws");
101 raf.write("Test".getBytes(), 0, 4);
106 * @tests java.io.RandomAccessFile#close()
108 public void test_close() {
109 // Test for method void java.io.RandomAccessFile.close()
111 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
113 raf.write("Test".getBytes(), 0, 4);
114 fail("Failed to close file properly");
115 } catch (IOException e) {}
119 * @tests java.io.RandomAccessFile#getFD()
121 public void test_getFD() throws IOException {
122 // Test for method java.io.FileDescriptor
123 // java.io.RandomAccessFile.getFD()
125 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
126 assertTrue("Returned invalid fd", raf.getFD().valid());
129 assertFalse("Returned valid fd after close", raf.getFD().valid());
133 * @tests java.io.RandomAccessFile#getFilePointer()
135 public void test_getFilePointer() throws IOException {
136 // Test for method long java.io.RandomAccessFile.getFilePointer()
137 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
138 raf.write(fileString.getBytes(), 0, 1000);
139 assertEquals("Incorrect filePointer returned", 1000, raf
145 * @tests java.io.RandomAccessFile#length()
147 public void test_length() throws IOException {
148 // Test for method long java.io.RandomAccessFile.length()
149 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
150 raf.write(fileString.getBytes());
151 assertEquals("Incorrect length returned", fileString.length(), raf
157 * @tests java.io.RandomAccessFile#read()
159 public void test_read() throws IOException {
160 // Test for method int java.io.RandomAccessFile.read()
161 FileOutputStream fos = new java.io.FileOutputStream(fileName);
162 fos.write(fileString.getBytes("UTF-8"), 0, fileString.length());
165 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "r");
166 assertEquals("Incorrect bytes returned from read",
167 fileString.charAt(0), raf.read());
172 * @tests java.io.RandomAccessFile#read(byte[])
174 public void test_read$B() throws IOException {
175 // Test for method int java.io.RandomAccessFile.read(byte [])
176 FileOutputStream fos = new java.io.FileOutputStream(fileName);
177 fos.write(fileString.getBytes(), 0, fileString.length());
180 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "r");
181 byte[] rbuf = new byte[4000];
183 assertEquals("Incorrect bytes returned from read", fileString,
184 new String(rbuf, 0, fileString.length()));
189 * @tests java.io.RandomAccessFile#read(byte[], int, int)
191 public void test_read$BII() throws IOException {
192 // Test for method int java.io.RandomAccessFile.read(byte [], int, int)
193 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
194 byte[] rbuf = new byte[4000];
195 FileOutputStream fos = new java.io.FileOutputStream(fileName);
196 fos.write(fileString.getBytes(), 0, fileString.length());
198 raf.read(rbuf, 0, fileString.length());
199 assertEquals("Incorrect bytes returned from read", fileString,
200 new String(rbuf, 0, fileString.length()));
205 * @tests java.io.RandomAccessFile#readBoolean()
207 public void test_readBoolean() throws IOException {
208 // Test for method boolean java.io.RandomAccessFile.readBoolean()
209 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
210 raf.writeBoolean(true);
212 assertTrue("Incorrect boolean read/written", raf.readBoolean());
217 * @tests java.io.RandomAccessFile#readByte()
219 public void test_readByte() throws IOException {
220 // Test for method byte java.io.RandomAccessFile.readByte()
221 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
224 assertEquals("Incorrect bytes read/written", 127, raf.readByte());
229 * @tests java.io.RandomAccessFile#readChar()
231 public void test_readChar() throws IOException {
232 // Test for method char java.io.RandomAccessFile.readChar()
233 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
236 assertEquals("Incorrect char read/written", 'T', raf.readChar());
241 * @tests java.io.RandomAccessFile#readDouble()
243 public void test_readDouble() throws IOException {
244 // Test for method double java.io.RandomAccessFile.readDouble()
245 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
246 raf.writeDouble(Double.MAX_VALUE);
248 assertEquals("Incorrect double read/written", Double.MAX_VALUE, raf
254 * @tests java.io.RandomAccessFile#readFloat()
256 public void test_readFloat() throws IOException {
257 // Test for method float java.io.RandomAccessFile.readFloat()
258 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
259 raf.writeFloat(Float.MAX_VALUE);
261 assertEquals("Incorrect float read/written", Float.MAX_VALUE, raf
267 * @tests java.io.RandomAccessFile#readFully(byte[])
269 public void test_readFully$B() throws IOException {
270 // Test for method void java.io.RandomAccessFile.readFully(byte [])
271 byte[] buf = new byte[10];
272 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
273 raf.writeBytes("HelloWorld");
276 assertEquals("Incorrect bytes read/written", "HelloWorld", new String(
277 buf, 0, 10, "UTF-8"));
282 * @tests java.io.RandomAccessFile#readFully(byte[], int, int)
284 public void test_readFully$BII() throws IOException {
285 // Test for method void java.io.RandomAccessFile.readFully(byte [], int,
287 byte[] buf = new byte[10];
288 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
289 raf.writeBytes("HelloWorld");
291 raf.readFully(buf, 0, buf.length);
292 assertEquals("Incorrect bytes read/written", "HelloWorld", new String(
293 buf, 0, 10, "UTF-8"));
295 raf.readFully(buf, 0, buf.length);
296 fail("Reading past end of buffer did not throw EOFException");
297 } catch (EOFException e) {}
302 * @tests java.io.RandomAccessFile#readInt()
304 public void test_readInt() throws IOException {
305 // Test for method int java.io.RandomAccessFile.readInt()
306 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
307 raf.writeInt(Integer.MIN_VALUE);
309 assertEquals("Incorrect int read/written", Integer.MIN_VALUE, raf
315 * @tests java.io.RandomAccessFile#readLine()
317 public void test_readLine() throws IOException {
318 // Test for method java.lang.String java.io.RandomAccessFile.readLine()
319 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
320 String s = "Goodbye\nCruel\nWorld\n";
321 raf.write(s.getBytes("UTF-8"), 0, s.length());
324 assertEquals("Goodbye", raf.readLine());
325 assertEquals("Cruel", raf.readLine());
326 assertEquals("World", raf.readLine());
332 * @tests java.io.RandomAccessFile#readLong()
334 public void test_readLong() throws IOException {
335 // Test for method long java.io.RandomAccessFile.readLong()
336 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
337 raf.writeLong(Long.MAX_VALUE);
339 assertEquals("Incorrect long read/written", Long.MAX_VALUE, raf
345 * @tests java.io.RandomAccessFile#readShort()
347 public void test_readShort() throws IOException {
348 // Test for method short java.io.RandomAccessFile.readShort()
349 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
350 raf.writeShort(Short.MIN_VALUE);
352 assertEquals("Incorrect long read/written", Short.MIN_VALUE, raf
358 * @tests java.io.RandomAccessFile#readUnsignedByte()
360 public void test_readUnsignedByte() throws IOException {
361 // Test for method int java.io.RandomAccessFile.readUnsignedByte()
362 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
365 assertEquals("Incorrect byte read/written", 255, raf.readUnsignedByte());
370 * @tests java.io.RandomAccessFile#readUnsignedShort()
372 public void test_readUnsignedShort() throws IOException {
373 // Test for method int java.io.RandomAccessFile.readUnsignedShort()
374 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
377 assertEquals("Incorrect byte read/written", 65535, raf
378 .readUnsignedShort());
383 * @tests java.io.RandomAccessFile#readUTF()
385 public void test_readUTF() throws IOException {
386 // Test for method java.lang.String java.io.RandomAccessFile.readUTF()
388 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
391 assertEquals("Incorrect utf string read", unihw, raf.readUTF());
396 * @tests java.io.RandomAccessFile#seek(long)
398 public void test_seekJ() throws IOException {
399 // Test for method void java.io.RandomAccessFile.seek(long)
400 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
401 raf.write(fileString.getBytes(), 0, fileString.length());
403 assertEquals("Seek failed to set filePointer", 12, raf.getFilePointer());
408 * @tests java.io.RandomAccessFile#skipBytes(int)
410 public void test_skipBytesI() throws IOException {
411 // Test for method int java.io.RandomAccessFile.skipBytes(int)
412 byte[] buf = new byte[5];
413 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
414 raf.writeBytes("HelloWorld");
418 assertEquals("Failed to skip bytes", "World", new String(buf, 0, 5, "UTF-8"));
423 * @tests java.io.RandomAccessFile#write(byte[])
425 public void test_write$B() throws IOException {
426 // Test for method void java.io.RandomAccessFile.write(byte [])
427 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
429 byte[] nullByteArray = null;
431 raf.write(nullByteArray);
432 fail("should throw NullPointerException");
433 } catch (NullPointerException e) {
437 byte[] rbuf = new byte[4000];
438 raf.write(fileString.getBytes());
442 raf.write(nullByteArray);
443 fail("should throw NullPointerException");
444 } catch (NullPointerException e) {
448 //will not throw IOException if array's length is 0
449 raf.write(new byte[0]);
452 raf.write(fileString.getBytes());
453 fail("should throw IOException");
454 } catch (IOException e) {
458 FileInputStream fis = new java.io.FileInputStream(fileName);
459 fis.read(rbuf, 0, fileString.length());
460 assertEquals("Incorrect bytes written", fileString, new String(rbuf, 0,
461 fileString.length()));
466 * @tests java.io.RandomAccessFile#write(byte[], int, int)
468 public void test_write$BII() throws IOException {
469 // Test for method void java.io.RandomAccessFile.write(byte [], int,
471 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
472 byte[] rbuf = new byte[4000];
473 raf.write(fileString.getBytes(), 0, fileString.length());
475 FileInputStream fis = new java.io.FileInputStream(fileName);
476 fis.read(rbuf, 0, fileString.length());
477 assertEquals("Incorrect bytes written", fileString, new String(rbuf, 0,
478 fileString.length()));
483 * @tests java.io.RandomAccessFile#write(byte[], int, int)
485 public void test_write_$BII_Exception() throws IOException {
486 raf = new java.io.RandomAccessFile(f, "rw");
487 byte[] nullByteArray = null;
488 byte[] byteArray = new byte[10];
491 raf.write(nullByteArray, -1, -1);
492 fail("should throw NullPointerException");
493 } catch (NullPointerException e) {
498 raf.write(nullByteArray, 0, 0);
499 fail("should throw NullPointerException");
500 } catch (NullPointerException e) {
505 raf.write(nullByteArray, 1, -1);
506 fail("should throw NullPointerException");
507 } catch (NullPointerException e) {
512 raf.write(nullByteArray, 1, 0);
513 fail("should throw NullPointerException");
514 } catch (NullPointerException e) {
519 raf.write(nullByteArray, 1, 1);
520 fail("should throw NullPointerException");
521 } catch (NullPointerException e) {
526 raf.write(byteArray, -1, -1);
527 fail("should throw IndexOutOfBoundsException");
528 } catch (IndexOutOfBoundsException e) {
533 raf.write(byteArray, -1, 0);
534 fail("should throw IndexOutOfBoundsException");
535 } catch (IndexOutOfBoundsException e) {
540 raf.write(byteArray, -1, 1);
541 fail("should throw IndexOutOfBoundsException");
542 } catch (IndexOutOfBoundsException e) {
547 raf.write(byteArray, 0, -1);
548 fail("should throw IndexOutOfBoundsException");
549 } catch (IndexOutOfBoundsException e) {
553 raf.write(byteArray, 0, 0);
554 raf.write(byteArray, 0, byteArray.length);
555 raf.write(byteArray, 1, 0);
556 raf.write(byteArray, byteArray.length, 0);
559 raf.write(byteArray, byteArray.length + 1, 0);
560 fail("should throw IndexOutOfBoundsException");
561 } catch (IndexOutOfBoundsException e) {
566 raf.write(byteArray, byteArray.length + 1, 1);
567 fail("should throw IndexOutOfBoundsException");
568 } catch (IndexOutOfBoundsException e) {
575 raf.write(nullByteArray, -1, -1);
576 fail("should throw NullPointerException");
577 } catch (NullPointerException e) {
582 raf.write(byteArray, -1, -1);
583 fail("should throw IndexOutOfBoundsException");
584 } catch (IndexOutOfBoundsException e) {
589 raf.write(byteArray, 0, 1);
590 fail("should throw IOException");
591 } catch (IOException e) {
596 raf.write(byteArray, 0, byteArray.length);
597 fail("should throw IOException");
598 } catch (IOException e) {
603 raf.write(byteArray, 1, 1);
604 fail("should throw IOException");
605 } catch (IOException e) {
610 raf.write(byteArray, byteArray.length + 1, 0);
611 fail("should throw IndexOutOfBoundsException");
612 } catch (IndexOutOfBoundsException e) {
616 // will not throw IOException if count = 0
617 raf.write(byteArray, 0, 0);
618 raf.write(byteArray, byteArray.length, 0);
623 * @tests java.io.RandomAccessFile#write(int)
625 public void test_writeI() throws IOException {
626 // Test for method void java.io.RandomAccessFile.write(int)
627 byte[] rbuf = new byte[4000];
628 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
631 FileInputStream fis = new java.io.FileInputStream(fileName);
632 fis.read(rbuf, 0, 1);
633 assertEquals("Incorrect byte written", 't', rbuf[0]);
638 * @tests java.io.RandomAccessFile#writeBoolean(boolean)
640 public void test_writeBooleanZ() throws IOException {
641 // Test for method void java.io.RandomAccessFile.writeBoolean(boolean)
642 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
643 raf.writeBoolean(true);
645 assertTrue("Incorrect boolean read/written", raf.readBoolean());
650 * @tests java.io.RandomAccessFile#writeByte(int)
652 public void test_writeByteI() throws IOException {
653 // Test for method void java.io.RandomAccessFile.writeByte(int)
654 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
657 assertEquals("Incorrect byte read/written", 127, raf.readByte());
662 * @tests java.io.RandomAccessFile#writeBytes(java.lang.String)
664 public void test_writeBytesLjava_lang_String() throws IOException {
665 // Test for method void
666 // java.io.RandomAccessFile.writeBytes(java.lang.String)
667 byte[] buf = new byte[10];
668 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
669 raf.writeBytes("HelloWorld");
672 assertEquals("Incorrect bytes read/written", "HelloWorld", new String(
673 buf, 0, 10, "UTF-8"));
679 * @tests java.io.RandomAccessFile#writeChar(int)
681 public void test_writeCharI() throws IOException {
682 // Test for method void java.io.RandomAccessFile.writeChar(int)
683 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
686 assertEquals("Incorrect char read/written", 'T', raf.readChar());
691 * @tests java.io.RandomAccessFile#writeChars(java.lang.String)
693 public void test_writeCharsLjava_lang_String() throws IOException {
694 // Test for method void
695 // java.io.RandomAccessFile.writeChars(java.lang.String)
696 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
697 raf.writeChars("HelloWorld");
698 char[] hchars = new char[10];
699 "HelloWorld".getChars(0, 10, hchars, 0);
701 for (int i = 0; i < hchars.length; i++)
702 assertEquals("Incorrect string written", hchars[i], raf.readChar());
707 * @tests java.io.RandomAccessFile#writeDouble(double)
709 public void test_writeDoubleD() throws IOException {
710 // Test for method void java.io.RandomAccessFile.writeDouble(double)
711 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
712 raf.writeDouble(Double.MAX_VALUE);
714 assertEquals("Incorrect double read/written", Double.MAX_VALUE, raf
720 * @tests java.io.RandomAccessFile#writeFloat(float)
722 public void test_writeFloatF() throws IOException {
723 // Test for method void java.io.RandomAccessFile.writeFloat(float)
724 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
725 raf.writeFloat(Float.MAX_VALUE);
727 assertEquals("Incorrect float read/written", Float.MAX_VALUE, raf
733 * @tests java.io.RandomAccessFile#writeInt(int)
735 public void test_writeIntI() throws IOException {
736 // Test for method void java.io.RandomAccessFile.writeInt(int)
737 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
738 raf.writeInt(Integer.MIN_VALUE);
740 assertEquals("Incorrect int read/written", Integer.MIN_VALUE, raf
746 * @tests java.io.RandomAccessFile#writeLong(long)
748 public void test_writeLongJ() throws IOException {
749 // Test for method void java.io.RandomAccessFile.writeLong(long)
750 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
751 raf.writeLong(Long.MAX_VALUE);
753 assertEquals("Incorrect long read/written", Long.MAX_VALUE, raf
759 * @tests java.io.RandomAccessFile#writeShort(int)
761 public void test_writeShortI() throws IOException {
762 // Test for method void java.io.RandomAccessFile.writeShort(int)
763 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
764 raf.writeShort(Short.MIN_VALUE);
766 assertEquals("Incorrect long read/written", Short.MIN_VALUE, raf
772 * @tests java.io.RandomAccessFile#writeUTF(java.lang.String)
774 public void test_writeUTFLjava_lang_String() throws IOException {
775 // Test for method void
776 // java.io.RandomAccessFile.writeUTF(java.lang.String)
777 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
780 assertEquals("Incorrect utf string", unihw, raf.readUTF());
785 * @tests java.io.RandomAccessFile#seek(long)
787 * Regression for HARMONY-374
789 public void test_seekI() throws IOException {
790 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
793 fail("IOException must be thrown if pos < 0");
794 } catch (IOException e) {
800 * @tests java.io.RandomAccessFile#read(byte[], int, int)
802 * Regression for HARMONY-377
804 public void test_readBII() throws IOException {
805 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
807 raf.read(new byte[1], -1, 1);
808 fail("IndexOutOfBoundsException must be thrown if off <0");
809 } catch (IndexOutOfBoundsException e) {
813 raf.read(new byte[1], 0, -1);
814 fail("IndexOutOfBoundsException must be thrown if len <0");
815 } catch (IndexOutOfBoundsException e) {
819 raf.read(new byte[1], 0, 5);
820 fail("IndexOutOfBoundsException must be thrown if off+len > b.length");
821 } catch (IndexOutOfBoundsException e) {
825 raf.read(new byte[10], Integer.MAX_VALUE, 5);
826 fail("IndexOutOfBoundsException expected");
827 } catch (IndexOutOfBoundsException e) {
831 raf.read(new byte[10], 5, Integer.MAX_VALUE);
832 fail("IndexOutOfBoundsException expected");
833 } catch (IndexOutOfBoundsException e) {
840 * @tests java.io.RandomAccessFile#read(byte[],int,int)
842 public void test_read_$BII_IndexOutOfBoundsException() throws IOException {
843 FileOutputStream fos = new java.io.FileOutputStream(fileName);
844 fos.write(fileString.getBytes(), 0, fileString.length());
847 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "r");
848 byte[] rbuf = new byte[100];
852 fail("should throw IndexOutOfBoundsException");
853 } catch (IndexOutOfBoundsException e) {
859 * @tests java.io.RandomAccessFile#read(byte[],int,int)
861 public void test_read_$BII_IOException() throws IOException {
862 FileOutputStream fos = new java.io.FileOutputStream(fileName);
863 fos.write(fileString.getBytes(), 0, fileString.length());
866 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "r");
867 byte[] rbuf = new byte[100];
869 int read = raf.read(rbuf,0,0);
870 assertEquals(0,read);
874 * @tests java.io.RandomAccessFile#read(byte[])
876 public void test_read_$B_IOException() throws IOException {
877 FileOutputStream fos = new java.io.FileOutputStream(fileName);
878 fos.write(fileString.getBytes(), 0, fileString.length());
881 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "r");
882 byte[] rbuf = new byte[0];
884 int read = raf.read(rbuf);
885 assertEquals(0,read);
889 * @tests java.io.RandomAccessFile#read(byte[],int,int)
891 public void test_read_$BII_NullPointerException() throws IOException {
892 File f = File.createTempFile("tmp", "tmp");
894 RandomAccessFile raf = new RandomAccessFile(f, "r");
897 raf.read(rbuf, 0, -1);
898 fail("should throw NullPointerException");
899 } catch (NullPointerException e) {
906 * @tests java.io.RandomAccessFile#write(byte[], int, int)
908 * Regression for HARMONY-377
910 public void test_writeBII() throws IOException {
911 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
913 raf.write(new byte[1], -1, 1);
914 fail("IndexOutOfBoundsException must be thrown if off <0");
915 } catch (IndexOutOfBoundsException e) {
919 raf.write(new byte[1], 0, -1);
920 fail("IndexOutOfBoundsException must be thrown if len <0");
921 } catch (IndexOutOfBoundsException e) {
925 raf.write(new byte[1], 0, 5);
926 fail("IndexOutOfBoundsException must be thrown if off+len > b.length");
927 } catch (IndexOutOfBoundsException e) {
931 raf.write(new byte[10], Integer.MAX_VALUE, 5);
932 fail("IndexOutOfBoundsException expected");
933 } catch (IndexOutOfBoundsException e) {
937 raf.write(new byte[10], 5, Integer.MAX_VALUE);
938 fail("IndexOutOfBoundsException expected");
939 } catch (IndexOutOfBoundsException e) {
945 * Regression for HARMONY-69
947 public void testRandomAccessFile_String_String() throws IOException {
949 RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "r");
950 FileChannel fcr = raf.getChannel();
953 fcr.lock(0L, Long.MAX_VALUE, false);
954 fail("NonWritableChannelException expected!");
955 } catch (NonWritableChannelException e) {}
960 * Sets up the fixture, for example, open a network connection. This method
961 * is called before a test is executed.
963 protected void setUp() throws Exception {
965 f = File.createTempFile("raf", "tst");
967 fail("Unable to delete test file : " + f);
969 fileName = f.getAbsolutePath();
973 * Tears down the fixture, for example, close a network connection. This
974 * method is called after a test is executed.
977 protected void tearDown() throws Exception {