| 1 |
/* |
| 2 |
* Copyright 2007 - 2018 ETH Zuerich, CISD and SIS. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package ch.systemsx.cisd.base.io; |
| 18 |
|
| 19 |
import java.io.IOException; |
| 20 |
import java.nio.ByteOrder; |
| 21 |
import java.nio.charset.Charset; |
| 22 |
|
| 23 |
import org.apache.commons.io.IOUtils; |
| 24 |
import org.testng.annotations.Test; |
| 25 |
|
| 26 |
import ch.systemsx.cisd.base.convert.NativeData; |
| 27 |
import ch.systemsx.cisd.base.tests.AbstractFileSystemTestCase; |
| 28 |
|
| 29 |
/** |
| 30 |
* Test cases for {@link IRandomAccessFile} implementations. |
| 31 |
* |
| 32 |
* @author Bernd Rinn |
| 33 |
*/ |
| 34 |
public abstract class IRandomAccessFileTests extends AbstractFileSystemTestCase |
| 35 |
{ |
| 36 |
|
| 37 |
abstract protected IRandomAccessFile createRandomAccessFile(String name); |
| 38 |
|
| 39 |
abstract protected IRandomAccessFile createRandomAccessFile(String name, byte[] content); |
| 40 |
|
| 41 |
@Test |
| 42 |
public void testSkip() |
| 43 |
{ |
| 44 |
final IRandomAccessFile raf = createRandomAccessFile("testSkip"); |
| 45 |
final byte[] b = new byte[4096]; |
| 46 |
for (int i = 0; i < b.length; ++i) |
| 47 |
{ |
| 48 |
b[i] = (byte) i; |
| 49 |
} |
| 50 |
raf.write(b); |
| 51 |
raf.seek(0); |
| 52 |
assertEquals(509, raf.skip(509)); |
| 53 |
assertEquals(509, raf.getFilePointer()); |
| 54 |
assertEquals(-3, raf.readByte()); |
| 55 |
assertEquals(4096 - 509 - 1, raf.skip(4096)); |
| 56 |
assertEquals(4096, raf.getFilePointer()); |
| 57 |
raf.close(); |
| 58 |
} |
| 59 |
|
| 60 |
@Test |
| 61 |
public void testLongByteOrder() |
| 62 |
{ |
| 63 |
final IRandomAccessFile raf = createRandomAccessFile("testLongByteOrder"); |
| 64 |
raf.writeLong(1); |
| 65 |
final byte[] buf = new byte[8]; |
| 66 |
raf.seek(0); |
| 67 |
raf.read(buf); |
| 68 |
// Default is big endian |
| 69 |
assertEquals(0, buf[0]); |
| 70 |
assertEquals(1, buf[7]); |
| 71 |
|
| 72 |
raf.seek(0); |
| 73 |
raf.setByteOrder(ByteOrder.LITTLE_ENDIAN); |
| 74 |
raf.writeLong(1); |
| 75 |
raf.seek(0); |
| 76 |
raf.read(buf); |
| 77 |
assertEquals(1, buf[0]); |
| 78 |
assertEquals(0, buf[7]); |
| 79 |
raf.close(); |
| 80 |
} |
| 81 |
|
| 82 |
@Test |
| 83 |
public void testMark() |
| 84 |
{ |
| 85 |
final IRandomAccessFile raf = createRandomAccessFile("testMark"); |
| 86 |
final byte[] buf = new byte[128]; |
| 87 |
raf.write(buf); |
| 88 |
raf.seek(0); |
| 89 |
assertTrue(raf.markSupported()); |
| 90 |
raf.mark(0); |
| 91 |
raf.read(); |
| 92 |
assertEquals(1, raf.getFilePointer()); |
| 93 |
raf.reset(); |
| 94 |
assertEquals(0, raf.getFilePointer()); |
| 95 |
raf.read(); |
| 96 |
raf.read(); |
| 97 |
raf.read(); |
| 98 |
raf.mark(0); |
| 99 |
raf.read(); |
| 100 |
raf.read(); |
| 101 |
raf.read(); |
| 102 |
raf.reset(); |
| 103 |
assertEquals(3, raf.getFilePointer()); |
| 104 |
raf.close(); |
| 105 |
} |
| 106 |
|
| 107 |
@Test |
| 108 |
public void testWriteReadByte() |
| 109 |
{ |
| 110 |
final IRandomAccessFile raf = createRandomAccessFile("testWriteReadByte"); |
| 111 |
raf.write(254); |
| 112 |
raf.seek(0); |
| 113 |
assertEquals(-2, raf.readByte()); |
| 114 |
raf.seek(0); |
| 115 |
assertEquals(254, raf.read()); |
| 116 |
raf.seek(0); |
| 117 |
assertEquals(254, raf.readUnsignedByte()); |
| 118 |
raf.close(); |
| 119 |
} |
| 120 |
|
| 121 |
@Test |
| 122 |
public void testWriteReadShort() |
| 123 |
{ |
| 124 |
final IRandomAccessFile raf = createRandomAccessFile("testWriteReadShort"); |
| 125 |
raf.writeShort(65534); |
| 126 |
raf.seek(0); |
| 127 |
assertEquals(-2, raf.readShort()); |
| 128 |
raf.seek(0); |
| 129 |
assertEquals(65534, raf.readUnsignedShort()); |
| 130 |
raf.close(); |
| 131 |
} |
| 132 |
|
| 133 |
@Test |
| 134 |
public void testAvailable() |
| 135 |
{ |
| 136 |
final IRandomAccessFile raf = createRandomAccessFile("testAvailable"); |
| 137 |
assertEquals(0, raf.available()); |
| 138 |
raf.writeDouble(5.5); |
| 139 |
raf.seek(0); |
| 140 |
assertEquals(8, raf.available()); |
| 141 |
raf.close(); |
| 142 |
} |
| 143 |
|
| 144 |
@Test |
| 145 |
public void testWriteReadStringBytes() |
| 146 |
{ |
| 147 |
final String s = "teststring"; |
| 148 |
final IRandomAccessFile raf = createRandomAccessFile("testWriteReadStringBytes"); |
| 149 |
raf.writeBytes(s); |
| 150 |
raf.seek(0); |
| 151 |
assertEquals(s.length(), raf.available()); |
| 152 |
final byte[] buf = new byte[raf.available()]; |
| 153 |
raf.read(buf); |
| 154 |
assertEquals(s, new String(buf)); |
| 155 |
raf.close(); |
| 156 |
} |
| 157 |
|
| 158 |
@Test |
| 159 |
public void testWriteReadStringChars() |
| 160 |
{ |
| 161 |
final String s = "teststring"; |
| 162 |
final IRandomAccessFile raf = createRandomAccessFile("testWriteReadStringChars"); |
| 163 |
raf.writeChars(s); |
| 164 |
raf.seek(0); |
| 165 |
assertEquals(2 * s.length(), raf.available()); |
| 166 |
final byte[] buf = new byte[raf.available()]; |
| 167 |
raf.read(buf); |
| 168 |
assertEquals( |
| 169 |
s, |
| 170 |
new String(NativeData.byteToChar(buf, |
| 171 |
ch.systemsx.cisd.base.convert.NativeData.ByteOrder.BIG_ENDIAN))); |
| 172 |
raf.close(); |
| 173 |
} |
| 174 |
|
| 175 |
@Test |
| 176 |
public void testReadLine() throws IOException |
| 177 |
{ |
| 178 |
final byte[] bytes = "hello world".getBytes(); |
| 179 |
final IRandomAccessFile raf = createRandomAccessFile("testWriteReadStringChars", bytes); |
| 180 |
final AdapterIInputStreamToInputStream is = new AdapterIInputStreamToInputStream(raf); |
| 181 |
|
| 182 |
assertEquals("[hello world]", IOUtils.readLines(is, Charset.defaultCharset()).toString()); |
| 183 |
raf.close(); |
| 184 |
} |
| 185 |
|
| 186 |
@Test |
| 187 |
public void testToByteArray() throws IOException |
| 188 |
{ |
| 189 |
final byte[] bytes = "hello world".getBytes(); |
| 190 |
final IRandomAccessFile raf = createRandomAccessFile("testWriteReadStringChars", bytes); |
| 191 |
final AdapterIInputStreamToInputStream is = new AdapterIInputStreamToInputStream(raf); |
| 192 |
|
| 193 |
assertEquals(bytes, IOUtils.toByteArray(is)); |
| 194 |
raf.close(); |
| 195 |
} |
| 196 |
|
| 197 |
} |