0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one or more
0003: * contributor license agreements. See the NOTICE file distributed with
0004: * this work for additional information regarding copyright ownership.
0005: * The ASF licenses this file to You under the Apache License, Version 2.0
0006: * (the "License"); you may not use this file except in compliance with
0007: * the License. You may obtain a copy of the License at
0008: *
0009: * http://www.apache.org/licenses/LICENSE-2.0
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS,
0013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014: * See the License for the specific language governing permissions and
0015: * limitations under the License.
0016: */
0017:
0018: package org.apache.harmony.luni.tests.java.io;
0019:
0020: import java.io.EOFException;
0021: import java.io.File;
0022: import java.io.FileInputStream;
0023: import java.io.FileOutputStream;
0024: import java.io.IOException;
0025: import java.io.RandomAccessFile;
0026:
0027: import java.nio.channels.FileChannel;
0028: import java.nio.channels.NonWritableChannelException;
0029:
0030: public class RandomAccessFileTest extends junit.framework.TestCase {
0031:
0032: public String fileName;
0033:
0034: public boolean ufile = true;
0035:
0036: java.io.RandomAccessFile raf;
0037:
0038: java.io.File f;
0039:
0040: String unihw = "\u0048\u0065\u006C\u0801\u006C\u006F\u0020\u0057\u0081\u006F\u0072\u006C\u0064";
0041:
0042: //java.io.FileOutputStream fos;
0043:
0044: 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";
0045:
0046: /**
0047: * @tests java.io.RandomAccessFile#RandomAccessFile(java.io.File,
0048: * java.lang.String)
0049: */
0050: public void test_ConstructorLjava_io_FileLjava_lang_String()
0051: throws Exception {
0052: // Test for method java.io.RandomAccessFile(java.io.File,
0053: // java.lang.String)
0054: RandomAccessFile raf = new java.io.RandomAccessFile(f, "rw");
0055: raf.write(20);
0056: raf.seek(0);
0057: assertEquals("Incorrect int read/written", 20, raf.read());
0058: raf.close();
0059:
0060: // Regression for HARMONY-50
0061: File f = File.createTempFile("xxx", "yyy");
0062: f.deleteOnExit();
0063: raf = new RandomAccessFile(f, "rws");
0064: raf.close();
0065:
0066: f = File.createTempFile("xxx", "yyy");
0067: f.deleteOnExit();
0068: raf = new RandomAccessFile(f, "rwd");
0069: raf.close();
0070: }
0071:
0072: /**
0073: * @tests java.io.RandomAccessFile#RandomAccessFile(java.lang.String,
0074: * java.lang.String)
0075: */
0076: public void test_ConstructorLjava_lang_StringLjava_lang_String()
0077: throws IOException {
0078: // Test for method java.io.RandomAccessFile(java.lang.String,
0079: // java.lang.String)
0080: RandomAccessFile raf = new java.io.RandomAccessFile(fileName,
0081: "rw");
0082: raf.write("Test".getBytes(), 0, 4);
0083: raf.close();
0084: }
0085:
0086: /**
0087: * @tests java.io.RandomAccessFile#close()
0088: */
0089: public void test_close() {
0090: // Test for method void java.io.RandomAccessFile.close()
0091: try {
0092: RandomAccessFile raf = new java.io.RandomAccessFile(
0093: fileName, "rw");
0094: raf.close();
0095: raf.write("Test".getBytes(), 0, 4);
0096: fail("Failed to close file properly");
0097: } catch (IOException e) {
0098: }
0099: }
0100:
0101: /**
0102: * @tests java.io.RandomAccessFile#getFD()
0103: */
0104: public void test_getFD() throws IOException {
0105: // Test for method java.io.FileDescriptor
0106: // java.io.RandomAccessFile.getFD()
0107:
0108: RandomAccessFile raf = new java.io.RandomAccessFile(fileName,
0109: "rw");
0110: assertTrue("Returned invalid fd", raf.getFD().valid());
0111:
0112: raf.close();
0113: assertFalse("Returned valid fd after close", raf.getFD()
0114: .valid());
0115: }
0116:
0117: /**
0118: * @tests java.io.RandomAccessFile#getFilePointer()
0119: */
0120: public void test_getFilePointer() throws IOException {
0121: // Test for method long java.io.RandomAccessFile.getFilePointer()
0122: RandomAccessFile raf = new java.io.RandomAccessFile(fileName,
0123: "rw");
0124: raf.write(fileString.getBytes(), 0, 1000);
0125: assertEquals("Incorrect filePointer returned", 1000, raf
0126: .getFilePointer());
0127: }
0128:
0129: /**
0130: * @tests java.io.RandomAccessFile#length()
0131: */
0132: public void test_length() throws IOException {
0133: // Test for method long java.io.RandomAccessFile.length()
0134: RandomAccessFile raf = new java.io.RandomAccessFile(fileName,
0135: "rw");
0136: raf.write(fileString.getBytes());
0137: assertEquals("Incorrect length returned", fileString.length(),
0138: raf.length());
0139: }
0140:
0141: /**
0142: * @tests java.io.RandomAccessFile#read()
0143: */
0144: public void test_read() throws IOException {
0145: // Test for method int java.io.RandomAccessFile.read()
0146: FileOutputStream fos = new java.io.FileOutputStream(fileName);
0147: fos.write(fileString.getBytes(), 0, fileString.length());
0148: fos.close();
0149:
0150: RandomAccessFile raf = new java.io.RandomAccessFile(fileName,
0151: "r");
0152: assertEquals("Incorrect bytes returned from read", fileString
0153: .charAt(0), raf.read());
0154: }
0155:
0156: /**
0157: * @tests java.io.RandomAccessFile#read(byte[])
0158: */
0159: public void test_read$B() throws IOException {
0160: // Test for method int java.io.RandomAccessFile.read(byte [])
0161: FileOutputStream fos = new java.io.FileOutputStream(fileName);
0162: fos.write(fileString.getBytes(), 0, fileString.length());
0163: fos.close();
0164:
0165: RandomAccessFile raf = new java.io.RandomAccessFile(fileName,
0166: "r");
0167: byte[] rbuf = new byte[4000];
0168: raf.read(rbuf);
0169: assertEquals("Incorrect bytes returned from read", fileString,
0170: new String(rbuf, 0, fileString.length()));
0171:
0172: }
0173:
0174: /**
0175: * @tests java.io.RandomAccessFile#read(byte[], int, int)
0176: */
0177: public void test_read$BII() throws IOException {
0178: // Test for method int java.io.RandomAccessFile.read(byte [], int, int)
0179: RandomAccessFile raf = new java.io.RandomAccessFile(fileName,
0180: "rw");
0181: byte[] rbuf = new byte[4000];
0182: FileOutputStream fos = new java.io.FileOutputStream(fileName);
0183: fos.write(fileString.getBytes(), 0, fileString.length());
0184: fos.close();
0185: raf.read(rbuf, 0, fileString.length());
0186: assertEquals("Incorrect bytes returned from read", fileString,
0187: new String(rbuf, 0, fileString.length()));
0188: }
0189:
0190: /**
0191: * @tests java.io.RandomAccessFile#readBoolean()
0192: */
0193: public void test_readBoolean() throws IOException {
0194: // Test for method boolean java.io.RandomAccessFile.readBoolean()
0195: RandomAccessFile raf = new java.io.RandomAccessFile(fileName,
0196: "rw");
0197: raf.writeBoolean(true);
0198: raf.seek(0);
0199: assertTrue("Incorrect boolean read/written", raf.readBoolean());
0200: raf.close();
0201: }
0202:
0203: /**
0204: * @tests java.io.RandomAccessFile#readByte()
0205: */
0206: public void test_readByte() throws IOException {
0207: // Test for method byte java.io.RandomAccessFile.readByte()
0208: RandomAccessFile raf = new java.io.RandomAccessFile(fileName,
0209: "rw");
0210: raf.writeByte(127);
0211: raf.seek(0);
0212: assertEquals("Incorrect bytes read/written", 127, raf
0213: .readByte());
0214: raf.close();
0215: }
0216:
0217: /**
0218: * @tests java.io.RandomAccessFile#readChar()
0219: */
0220: public void test_readChar() throws IOException {
0221: // Test for method char java.io.RandomAccessFile.readChar()
0222: RandomAccessFile raf = new java.io.RandomAccessFile(fileName,
0223: "rw");
0224: raf.writeChar('T');
0225: raf.seek(0);
0226: assertEquals("Incorrect char read/written", 'T', raf.readChar());
0227: raf.close();
0228: }
0229:
0230: /**
0231: * @tests java.io.RandomAccessFile#readDouble()
0232: */
0233: public void test_readDouble() throws IOException {
0234: // Test for method double java.io.RandomAccessFile.readDouble()
0235: RandomAccessFile raf = new java.io.RandomAccessFile(fileName,
0236: "rw");
0237: raf.writeDouble(Double.MAX_VALUE);
0238: raf.seek(0);
0239: assertEquals("Incorrect double read/written", Double.MAX_VALUE,
0240: raf.readDouble(), 0);
0241: raf.close();
0242: }
0243:
0244: /**
0245: * @tests java.io.RandomAccessFile#readFloat()
0246: */
0247: public void test_readFloat() throws IOException {
0248: // Test for method float java.io.RandomAccessFile.readFloat()
0249: RandomAccessFile raf = new java.io.RandomAccessFile(fileName,
0250: "rw");
0251: raf.writeFloat(Float.MAX_VALUE);
0252: raf.seek(0);
0253: assertEquals("Incorrect float read/written", Float.MAX_VALUE,
0254: raf.readFloat(), 0);
0255: raf.close();
0256: }
0257:
0258: /**
0259: * @tests java.io.RandomAccessFile#readFully(byte[])
0260: */
0261: public void test_readFully$B() throws IOException {
0262: // Test for method void java.io.RandomAccessFile.readFully(byte [])
0263: byte[] buf = new byte[10];
0264: RandomAccessFile raf = new java.io.RandomAccessFile(fileName,
0265: "rw");
0266: raf.writeBytes("HelloWorld");
0267: raf.seek(0);
0268: raf.readFully(buf);
0269: assertEquals("Incorrect bytes read/written", "HelloWorld",
0270: new String(buf, 0, 10));
0271: raf.close();
0272: }
0273:
0274: /**
0275: * @tests java.io.RandomAccessFile#readFully(byte[], int, int)
0276: */
0277: public void test_readFully$BII() throws IOException {
0278: // Test for method void java.io.RandomAccessFile.readFully(byte [], int,
0279: // int)
0280: byte[] buf = new byte[10];
0281: RandomAccessFile raf = new java.io.RandomAccessFile(fileName,
0282: "rw");
0283: raf.writeBytes("HelloWorld");
0284: raf.seek(0);
0285: raf.readFully(buf, 0, buf.length);
0286: assertEquals("Incorrect bytes read/written", "HelloWorld",
0287: new String(buf, 0, 10));
0288: try {
0289: raf.readFully(buf, 0, buf.length);
0290: fail("Reading past end of buffer did not throw EOFException");
0291: } catch (EOFException e) {
0292: }
0293: }
0294:
0295: /**
0296: * @tests java.io.RandomAccessFile#readInt()
0297: */
0298: public void test_readInt() throws IOException {
0299: // Test for method int java.io.RandomAccessFile.readInt()
0300: RandomAccessFile raf = new java.io.RandomAccessFile(fileName,
0301: "rw");
0302: raf.writeInt(Integer.MIN_VALUE);
0303: raf.seek(0);
0304: assertEquals("Incorrect int read/written", Integer.MIN_VALUE,
0305: raf.readInt());
0306: raf.close();
0307: }
0308:
0309: /**
0310: * @tests java.io.RandomAccessFile#readLine()
0311: */
0312: public void test_readLine() throws IOException {
0313: // Test for method java.lang.String java.io.RandomAccessFile.readLine()
0314: RandomAccessFile raf = new java.io.RandomAccessFile(fileName,
0315: "rw");
0316: String s = "Goodbye\nCruel\nWorld\n";
0317: raf.write(s.getBytes(), 0, s.length());
0318: raf.seek(0);
0319:
0320: assertEquals("Goodbye", raf.readLine());
0321: assertEquals("Cruel", raf.readLine());
0322: assertEquals("World", raf.readLine());
0323: }
0324:
0325: /**
0326: * @tests java.io.RandomAccessFile#readLong()
0327: */
0328: public void test_readLong() throws IOException {
0329: // Test for method long java.io.RandomAccessFile.readLong()
0330: RandomAccessFile raf = new java.io.RandomAccessFile(fileName,
0331: "rw");
0332: raf.writeLong(Long.MAX_VALUE);
0333: raf.seek(0);
0334: assertEquals("Incorrect long read/written", Long.MAX_VALUE, raf
0335: .readLong());
0336: raf.close();
0337: }
0338:
0339: /**
0340: * @tests java.io.RandomAccessFile#readShort()
0341: */
0342: public void test_readShort() throws IOException {
0343: // Test for method short java.io.RandomAccessFile.readShort()
0344: RandomAccessFile raf = new java.io.RandomAccessFile(fileName,
0345: "rw");
0346: raf.writeShort(Short.MIN_VALUE);
0347: raf.seek(0);
0348: assertEquals("Incorrect long read/written", Short.MIN_VALUE,
0349: raf.readShort());
0350: raf.close();
0351: }
0352:
0353: /**
0354: * @tests java.io.RandomAccessFile#readUnsignedByte()
0355: */
0356: public void test_readUnsignedByte() throws IOException {
0357: // Test for method int java.io.RandomAccessFile.readUnsignedByte()
0358: RandomAccessFile raf = new java.io.RandomAccessFile(fileName,
0359: "rw");
0360: raf.writeByte(-1);
0361: raf.seek(0);
0362: assertEquals("Incorrect byte read/written", 255, raf
0363: .readUnsignedByte());
0364: raf.close();
0365: }
0366:
0367: /**
0368: * @tests java.io.RandomAccessFile#readUnsignedShort()
0369: */
0370: public void test_readUnsignedShort() throws IOException {
0371: // Test for method int java.io.RandomAccessFile.readUnsignedShort()
0372: RandomAccessFile raf = new java.io.RandomAccessFile(fileName,
0373: "rw");
0374: raf.writeShort(-1);
0375: raf.seek(0);
0376: assertEquals("Incorrect byte read/written", 65535, raf
0377: .readUnsignedShort());
0378: raf.close();
0379: }
0380:
0381: /**
0382: * @tests java.io.RandomAccessFile#readUTF()
0383: */
0384: public void test_readUTF() throws IOException {
0385: // Test for method java.lang.String java.io.RandomAccessFile.readUTF()
0386:
0387: RandomAccessFile raf = new java.io.RandomAccessFile(fileName,
0388: "rw");
0389: raf.writeUTF(unihw);
0390: raf.seek(0);
0391: assertEquals("Incorrect utf string read", unihw, raf.readUTF());
0392: raf.close();
0393: }
0394:
0395: /**
0396: * @tests java.io.RandomAccessFile#seek(long)
0397: */
0398: public void test_seekJ() throws IOException {
0399: // Test for method void java.io.RandomAccessFile.seek(long)
0400: RandomAccessFile raf = new java.io.RandomAccessFile(fileName,
0401: "rw");
0402: raf.write(fileString.getBytes(), 0, fileString.length());
0403: raf.seek(12);
0404: assertEquals("Seek failed to set filePointer", 12, raf
0405: .getFilePointer());
0406: }
0407:
0408: /**
0409: * @tests java.io.RandomAccessFile#skipBytes(int)
0410: */
0411: public void test_skipBytesI() throws IOException {
0412: // Test for method int java.io.RandomAccessFile.skipBytes(int)
0413: byte[] buf = new byte[5];
0414: RandomAccessFile raf = new java.io.RandomAccessFile(fileName,
0415: "rw");
0416: raf.writeBytes("HelloWorld");
0417: raf.seek(0);
0418: raf.skipBytes(5);
0419: raf.readFully(buf);
0420: assertEquals("Failed to skip bytes", "World", new String(buf,
0421: 0, 5));
0422: raf.close();
0423: }
0424:
0425: /**
0426: * @tests java.io.RandomAccessFile#write(byte[])
0427: */
0428: public void test_write$B() throws IOException {
0429: // Test for method void java.io.RandomAccessFile.write(byte [])
0430: RandomAccessFile raf = new java.io.RandomAccessFile(fileName,
0431: "rw");
0432:
0433: byte[] nullByteArray = null;
0434: try {
0435: raf.write(nullByteArray);
0436: fail("should throw NullPointerException");
0437: } catch (NullPointerException e) {
0438: //expected
0439: }
0440:
0441: byte[] rbuf = new byte[4000];
0442: raf.write(fileString.getBytes());
0443: raf.close();
0444:
0445: try {
0446: raf.write(nullByteArray);
0447: fail("should throw NullPointerException");
0448: } catch (NullPointerException e) {
0449: //expected
0450: }
0451:
0452: //will not throw IOException if array's length is 0
0453: raf.write(new byte[0]);
0454:
0455: try {
0456: raf.write(fileString.getBytes());
0457: fail("should throw IOException");
0458: } catch (IOException e) {
0459: //expected
0460: }
0461:
0462: FileInputStream fis = new java.io.FileInputStream(fileName);
0463: fis.read(rbuf, 0, fileString.length());
0464: assertEquals("Incorrect bytes written", fileString, new String(
0465: rbuf, 0, fileString.length()));
0466: }
0467:
0468: /**
0469: * @tests java.io.RandomAccessFile#write(byte[], int, int)
0470: */
0471: public void test_write$BII() throws IOException {
0472: // Test for method void java.io.RandomAccessFile.write(byte [], int,
0473: // int)
0474: RandomAccessFile raf = new java.io.RandomAccessFile(fileName,
0475: "rw");
0476: byte[] rbuf = new byte[4000];
0477: raf.write(fileString.getBytes(), 0, fileString.length());
0478: raf.close();
0479: FileInputStream fis = new java.io.FileInputStream(fileName);
0480: fis.read(rbuf, 0, fileString.length());
0481: assertEquals("Incorrect bytes written", fileString, new String(
0482: rbuf, 0, fileString.length()));
0483: }
0484:
0485: /**
0486: * @tests java.io.RandomAccessFile#write(byte[], int, int)
0487: */
0488: public void test_write_$BII_Exception() throws IOException {
0489: raf = new java.io.RandomAccessFile(f, "rw");
0490: byte[] nullByteArray = null;
0491: byte[] byteArray = new byte[10];
0492:
0493: try {
0494: raf.write(nullByteArray, -1, -1);
0495: fail("should throw NullPointerException");
0496: } catch (NullPointerException e) {
0497: // expected
0498: }
0499:
0500: try {
0501: raf.write(nullByteArray, 0, 0);
0502: fail("should throw NullPointerException");
0503: } catch (NullPointerException e) {
0504: // expected
0505: }
0506:
0507: try {
0508: raf.write(nullByteArray, 1, -1);
0509: fail("should throw NullPointerException");
0510: } catch (NullPointerException e) {
0511: // expected
0512: }
0513:
0514: try {
0515: raf.write(nullByteArray, 1, 0);
0516: fail("should throw NullPointerException");
0517: } catch (NullPointerException e) {
0518: // expected
0519: }
0520:
0521: try {
0522: raf.write(nullByteArray, 1, 1);
0523: fail("should throw NullPointerException");
0524: } catch (NullPointerException e) {
0525: // expected
0526: }
0527:
0528: try {
0529: raf.write(byteArray, -1, -1);
0530: fail("should throw IndexOutOfBoundsException");
0531: } catch (IndexOutOfBoundsException e) {
0532: // expected
0533: }
0534:
0535: try {
0536: raf.write(byteArray, -1, 0);
0537: fail("should throw IndexOutOfBoundsException");
0538: } catch (IndexOutOfBoundsException e) {
0539: // expected
0540: }
0541:
0542: try {
0543: raf.write(byteArray, -1, 1);
0544: fail("should throw IndexOutOfBoundsException");
0545: } catch (IndexOutOfBoundsException e) {
0546: // expected
0547: }
0548:
0549: try {
0550: raf.write(byteArray, 0, -1);
0551: fail("should throw IndexOutOfBoundsException");
0552: } catch (IndexOutOfBoundsException e) {
0553: // expected
0554: }
0555:
0556: raf.write(byteArray, 0, 0);
0557: raf.write(byteArray, 0, byteArray.length);
0558: raf.write(byteArray, 1, 0);
0559: raf.write(byteArray, byteArray.length, 0);
0560:
0561: try {
0562: raf.write(byteArray, byteArray.length + 1, 0);
0563: fail("should throw IndexOutOfBoundsException");
0564: } catch (IndexOutOfBoundsException e) {
0565: //expected
0566: }
0567:
0568: try {
0569: raf.write(byteArray, byteArray.length + 1, 1);
0570: fail("should throw IndexOutOfBoundsException");
0571: } catch (IndexOutOfBoundsException e) {
0572: //expected
0573: }
0574:
0575: raf.close();
0576:
0577: try {
0578: raf.write(nullByteArray, -1, -1);
0579: fail("should throw NullPointerException");
0580: } catch (NullPointerException e) {
0581: // expected
0582: }
0583:
0584: try {
0585: raf.write(byteArray, -1, -1);
0586: fail("should throw IndexOutOfBoundsException");
0587: } catch (IndexOutOfBoundsException e) {
0588: // expected
0589: }
0590:
0591: try {
0592: raf.write(byteArray, 0, 1);
0593: fail("should throw IOException");
0594: } catch (IOException e) {
0595: //expected
0596: }
0597:
0598: try {
0599: raf.write(byteArray, 0, byteArray.length);
0600: fail("should throw IOException");
0601: } catch (IOException e) {
0602: //expected
0603: }
0604:
0605: try {
0606: raf.write(byteArray, 1, 1);
0607: fail("should throw IOException");
0608: } catch (IOException e) {
0609: //expected
0610: }
0611:
0612: try {
0613: raf.write(byteArray, byteArray.length + 1, 0);
0614: fail("should throw IndexOutOfBoundsException");
0615: } catch (IndexOutOfBoundsException e) {
0616: //expected
0617: }
0618:
0619: // will not throw IOException if count = 0
0620: raf.write(byteArray, 0, 0);
0621: raf.write(byteArray, byteArray.length, 0);
0622: }
0623:
0624: /**
0625: * @tests java.io.RandomAccessFile#write(int)
0626: */
0627: public void test_writeI() throws IOException {
0628: // Test for method void java.io.RandomAccessFile.write(int)
0629: byte[] rbuf = new byte[4000];
0630: RandomAccessFile raf = new java.io.RandomAccessFile(fileName,
0631: "rw");
0632: raf.write('t');
0633: raf.close();
0634: FileInputStream fis = new java.io.FileInputStream(fileName);
0635: fis.read(rbuf, 0, 1);
0636: assertEquals("Incorrect byte written", 't', rbuf[0]);
0637: }
0638:
0639: /**
0640: * @tests java.io.RandomAccessFile#writeBoolean(boolean)
0641: */
0642: public void test_writeBooleanZ() throws IOException {
0643: // Test for method void java.io.RandomAccessFile.writeBoolean(boolean)
0644: RandomAccessFile raf = new java.io.RandomAccessFile(fileName,
0645: "rw");
0646: raf.writeBoolean(true);
0647: raf.seek(0);
0648: assertTrue("Incorrect boolean read/written", raf.readBoolean());
0649: raf.close();
0650: }
0651:
0652: /**
0653: * @tests java.io.RandomAccessFile#writeByte(int)
0654: */
0655: public void test_writeByteI() throws IOException {
0656: // Test for method void java.io.RandomAccessFile.writeByte(int)
0657: RandomAccessFile raf = new java.io.RandomAccessFile(fileName,
0658: "rw");
0659: raf.writeByte(127);
0660: raf.seek(0);
0661: assertEquals("Incorrect byte read/written", 127, raf.readByte());
0662: raf.close();
0663: }
0664:
0665: /**
0666: * @tests java.io.RandomAccessFile#writeBytes(java.lang.String)
0667: */
0668: public void test_writeBytesLjava_lang_String() throws IOException {
0669: // Test for method void
0670: // java.io.RandomAccessFile.writeBytes(java.lang.String)
0671: byte[] buf = new byte[10];
0672: RandomAccessFile raf = new java.io.RandomAccessFile(fileName,
0673: "rw");
0674: raf.writeBytes("HelloWorld");
0675: raf.seek(0);
0676: raf.readFully(buf);
0677: assertEquals("Incorrect bytes read/written", "HelloWorld",
0678: new String(buf, 0, 10));
0679: raf.close();
0680:
0681: }
0682:
0683: /**
0684: * @tests java.io.RandomAccessFile#writeChar(int)
0685: */
0686: public void test_writeCharI() throws IOException {
0687: // Test for method void java.io.RandomAccessFile.writeChar(int)
0688: RandomAccessFile raf = new java.io.RandomAccessFile(fileName,
0689: "rw");
0690: raf.writeChar('T');
0691: raf.seek(0);
0692: assertEquals("Incorrect char read/written", 'T', raf.readChar());
0693: raf.close();
0694: }
0695:
0696: /**
0697: * @tests java.io.RandomAccessFile#writeChars(java.lang.String)
0698: */
0699: public void test_writeCharsLjava_lang_String() throws IOException {
0700: // Test for method void
0701: // java.io.RandomAccessFile.writeChars(java.lang.String)
0702: RandomAccessFile raf = new java.io.RandomAccessFile(fileName,
0703: "rw");
0704: raf.writeChars("HelloWorld");
0705: char[] hchars = new char[10];
0706: "HelloWorld".getChars(0, 10, hchars, 0);
0707: raf.seek(0);
0708: for (int i = 0; i < hchars.length; i++)
0709: assertEquals("Incorrect string written", hchars[i], raf
0710: .readChar());
0711: raf.close();
0712: }
0713:
0714: /**
0715: * @tests java.io.RandomAccessFile#writeDouble(double)
0716: */
0717: public void test_writeDoubleD() throws IOException {
0718: // Test for method void java.io.RandomAccessFile.writeDouble(double)
0719: RandomAccessFile raf = new java.io.RandomAccessFile(fileName,
0720: "rw");
0721: raf.writeDouble(Double.MAX_VALUE);
0722: raf.seek(0);
0723: assertEquals("Incorrect double read/written", Double.MAX_VALUE,
0724: raf.readDouble(), 0);
0725: raf.close();
0726: }
0727:
0728: /**
0729: * @tests java.io.RandomAccessFile#writeFloat(float)
0730: */
0731: public void test_writeFloatF() throws IOException {
0732: // Test for method void java.io.RandomAccessFile.writeFloat(float)
0733: RandomAccessFile raf = new java.io.RandomAccessFile(fileName,
0734: "rw");
0735: raf.writeFloat(Float.MAX_VALUE);
0736: raf.seek(0);
0737: assertEquals("Incorrect float read/written", Float.MAX_VALUE,
0738: raf.readFloat(), 0);
0739: raf.close();
0740: }
0741:
0742: /**
0743: * @tests java.io.RandomAccessFile#writeInt(int)
0744: */
0745: public void test_writeIntI() throws IOException {
0746: // Test for method void java.io.RandomAccessFile.writeInt(int)
0747: RandomAccessFile raf = new java.io.RandomAccessFile(fileName,
0748: "rw");
0749: raf.writeInt(Integer.MIN_VALUE);
0750: raf.seek(0);
0751: assertEquals("Incorrect int read/written", Integer.MIN_VALUE,
0752: raf.readInt());
0753: raf.close();
0754: }
0755:
0756: /**
0757: * @tests java.io.RandomAccessFile#writeLong(long)
0758: */
0759: public void test_writeLongJ() throws IOException {
0760: // Test for method void java.io.RandomAccessFile.writeLong(long)
0761: RandomAccessFile raf = new java.io.RandomAccessFile(fileName,
0762: "rw");
0763: raf.writeLong(Long.MAX_VALUE);
0764: raf.seek(0);
0765: assertEquals("Incorrect long read/written", Long.MAX_VALUE, raf
0766: .readLong());
0767: raf.close();
0768: }
0769:
0770: /**
0771: * @tests java.io.RandomAccessFile#writeShort(int)
0772: */
0773: public void test_writeShortI() throws IOException {
0774: // Test for method void java.io.RandomAccessFile.writeShort(int)
0775: RandomAccessFile raf = new java.io.RandomAccessFile(fileName,
0776: "rw");
0777: raf.writeShort(Short.MIN_VALUE);
0778: raf.seek(0);
0779: assertEquals("Incorrect long read/written", Short.MIN_VALUE,
0780: raf.readShort());
0781: raf.close();
0782: }
0783:
0784: /**
0785: * @tests java.io.RandomAccessFile#writeUTF(java.lang.String)
0786: */
0787: public void test_writeUTFLjava_lang_String() throws IOException {
0788: // Test for method void
0789: // java.io.RandomAccessFile.writeUTF(java.lang.String)
0790: RandomAccessFile raf = new java.io.RandomAccessFile(fileName,
0791: "rw");
0792: raf.writeUTF(unihw);
0793: raf.seek(0);
0794: assertEquals("Incorrect utf string", unihw, raf.readUTF());
0795: raf.close();
0796: }
0797:
0798: /**
0799: * @tests java.io.RandomAccessFile#seek(long)
0800: *
0801: * Regression for HARMONY-374
0802: */
0803: public void test_seekI() throws IOException {
0804: RandomAccessFile raf = new java.io.RandomAccessFile(fileName,
0805: "rw");
0806: try {
0807: raf.seek(-1);
0808: fail("IOException must be thrown if pos < 0");
0809: } catch (IOException e) {
0810: }
0811: }
0812:
0813: /**
0814: * @tests java.io.RandomAccessFile#read(byte[], int, int)
0815: *
0816: * Regression for HARMONY-377
0817: */
0818: public void test_readBII() throws IOException {
0819: RandomAccessFile raf = new java.io.RandomAccessFile(fileName,
0820: "rw");
0821: try {
0822: raf.read(new byte[1], -1, 1);
0823: fail("IndexOutOfBoundsException must be thrown if off <0");
0824: } catch (IndexOutOfBoundsException e) {
0825: }
0826:
0827: try {
0828: raf.read(new byte[1], 0, -1);
0829: fail("IndexOutOfBoundsException must be thrown if len <0");
0830: } catch (IndexOutOfBoundsException e) {
0831: }
0832:
0833: try {
0834: raf.read(new byte[1], 0, 5);
0835: fail("IndexOutOfBoundsException must be thrown if off+len > b.length");
0836: } catch (IndexOutOfBoundsException e) {
0837: }
0838:
0839: try {
0840: raf.read(new byte[10], Integer.MAX_VALUE, 5);
0841: fail("IndexOutOfBoundsException expected");
0842: } catch (IndexOutOfBoundsException e) {
0843: }
0844:
0845: try {
0846: raf.read(new byte[10], 5, Integer.MAX_VALUE);
0847: fail("IndexOutOfBoundsException expected");
0848: } catch (IndexOutOfBoundsException e) {
0849: }
0850:
0851: raf.close();
0852: }
0853:
0854: /**
0855: * @tests java.io.RandomAccessFile#read(byte[],int,int)
0856: */
0857: public void test_read_$BII_IndexOutOfBoundsException()
0858: throws IOException {
0859: FileOutputStream fos = new java.io.FileOutputStream(fileName);
0860: fos.write(fileString.getBytes(), 0, fileString.length());
0861: fos.close();
0862:
0863: RandomAccessFile raf = new java.io.RandomAccessFile(fileName,
0864: "r");
0865: byte[] rbuf = new byte[100];
0866: raf.close();
0867: try {
0868: raf.read(rbuf, -1, 0);
0869: fail("should throw IndexOutOfBoundsException");
0870: } catch (IndexOutOfBoundsException e) {
0871: //expected
0872: }
0873: }
0874:
0875: /**
0876: * @tests java.io.RandomAccessFile#read(byte[],int,int)
0877: */
0878: public void test_read_$BII_IOException() throws IOException {
0879: FileOutputStream fos = new java.io.FileOutputStream(fileName);
0880: fos.write(fileString.getBytes(), 0, fileString.length());
0881: fos.close();
0882:
0883: RandomAccessFile raf = new java.io.RandomAccessFile(fileName,
0884: "r");
0885: byte[] rbuf = new byte[100];
0886: raf.close();
0887: int read = raf.read(rbuf, 0, 0);
0888: assertEquals(0, read);
0889: }
0890:
0891: /**
0892: * @tests java.io.RandomAccessFile#read(byte[])
0893: */
0894: public void test_read_$B_IOException() throws IOException {
0895: FileOutputStream fos = new java.io.FileOutputStream(fileName);
0896: fos.write(fileString.getBytes(), 0, fileString.length());
0897: fos.close();
0898:
0899: RandomAccessFile raf = new java.io.RandomAccessFile(fileName,
0900: "r");
0901: byte[] rbuf = new byte[0];
0902: raf.close();
0903: int read = raf.read(rbuf);
0904: assertEquals(0, read);
0905: }
0906:
0907: /**
0908: * @tests java.io.RandomAccessFile#read(byte[],int,int)
0909: */
0910: public void test_read_$BII_NullPointerException()
0911: throws IOException {
0912: RandomAccessFile raf = new RandomAccessFile(File
0913: .createTempFile("tmp", "tmp"), "r");
0914: byte[] rbuf = null;
0915: try {
0916: raf.read(rbuf, 0, -1);
0917: fail("should throw NullPointerException");
0918: } catch (NullPointerException e) {
0919: // expected
0920: }
0921: }
0922:
0923: /**
0924: * @tests java.io.RandomAccessFile#write(byte[], int, int)
0925: *
0926: * Regression for HARMONY-377
0927: */
0928: public void test_writeBII() throws IOException {
0929: RandomAccessFile raf = new java.io.RandomAccessFile(fileName,
0930: "rw");
0931: try {
0932: raf.write(new byte[1], -1, 1);
0933: fail("IndexOutOfBoundsException must be thrown if off <0");
0934: } catch (IndexOutOfBoundsException e) {
0935: }
0936:
0937: try {
0938: raf.write(new byte[1], 0, -1);
0939: fail("IndexOutOfBoundsException must be thrown if len <0");
0940: } catch (IndexOutOfBoundsException e) {
0941: }
0942:
0943: try {
0944: raf.write(new byte[1], 0, 5);
0945: fail("IndexOutOfBoundsException must be thrown if off+len > b.length");
0946: } catch (IndexOutOfBoundsException e) {
0947: }
0948:
0949: try {
0950: raf.write(new byte[10], Integer.MAX_VALUE, 5);
0951: fail("IndexOutOfBoundsException expected");
0952: } catch (IndexOutOfBoundsException e) {
0953: }
0954:
0955: try {
0956: raf.write(new byte[10], 5, Integer.MAX_VALUE);
0957: fail("IndexOutOfBoundsException expected");
0958: } catch (IndexOutOfBoundsException e) {
0959: }
0960: raf.close();
0961: }
0962:
0963: /**
0964: * Regression for HARMONY-69
0965: */
0966: public void testRandomAccessFile_String_String() throws IOException {
0967: f.createNewFile();
0968: RandomAccessFile raf = new java.io.RandomAccessFile(fileName,
0969: "r");
0970: FileChannel fcr = raf.getChannel();
0971:
0972: try {
0973: fcr.lock(0L, Long.MAX_VALUE, false);
0974: fail("NonWritableChannelException expected!");
0975: } catch (NonWritableChannelException e) {
0976: }
0977: raf.close();
0978: }
0979:
0980: /**
0981: * Sets up the fixture, for example, open a network connection. This method
0982: * is called before a test is executed.
0983: */
0984: protected void setUp() throws Exception {
0985: super .setUp();
0986: f = File.createTempFile("raf", "tst");
0987: if (!f.delete()) {
0988: fail("Unable to delete test file : " + f);
0989: }
0990: fileName = f.getAbsolutePath();
0991: }
0992:
0993: /**
0994: * Tears down the fixture, for example, close a network connection. This
0995: * method is called after a test is executed.
0996: * @throws Exception
0997: */
0998: protected void tearDown() throws Exception {
0999: if (f.exists()) {
1000: f.delete();
1001: }
1002: super.tearDown();
1003: }
1004:
1005: }
|