File: IRandomAccessFileTests.java

package info (click to toggle)
libsis-base-java 18.09~pre1%2Bgit20180928.45fbd31%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,292 kB
  • sloc: java: 9,037; ansic: 813; xml: 160; sh: 139; makefile: 37
file content (197 lines) | stat: -rw-r--r-- 5,924 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 * Copyright 2007 - 2018 ETH Zuerich, CISD and SIS.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ch.systemsx.cisd.base.io;

import java.io.IOException;
import java.nio.ByteOrder;
import java.nio.charset.Charset;

import org.apache.commons.io.IOUtils;
import org.testng.annotations.Test;

import ch.systemsx.cisd.base.convert.NativeData;
import ch.systemsx.cisd.base.tests.AbstractFileSystemTestCase;

/**
 * Test cases for {@link IRandomAccessFile} implementations.
 * 
 * @author Bernd Rinn
 */
public abstract class IRandomAccessFileTests extends AbstractFileSystemTestCase
{

    abstract protected IRandomAccessFile createRandomAccessFile(String name);

    abstract protected IRandomAccessFile createRandomAccessFile(String name, byte[] content);

    @Test
    public void testSkip()
    {
        final IRandomAccessFile raf = createRandomAccessFile("testSkip");
        final byte[] b = new byte[4096];
        for (int i = 0; i < b.length; ++i)
        {
            b[i] = (byte) i;
        }
        raf.write(b);
        raf.seek(0);
        assertEquals(509, raf.skip(509));
        assertEquals(509, raf.getFilePointer());
        assertEquals(-3, raf.readByte());
        assertEquals(4096 - 509 - 1, raf.skip(4096));
        assertEquals(4096, raf.getFilePointer());
        raf.close();
    }

    @Test
    public void testLongByteOrder()
    {
        final IRandomAccessFile raf = createRandomAccessFile("testLongByteOrder");
        raf.writeLong(1);
        final byte[] buf = new byte[8];
        raf.seek(0);
        raf.read(buf);
        // Default is big endian
        assertEquals(0, buf[0]);
        assertEquals(1, buf[7]);

        raf.seek(0);
        raf.setByteOrder(ByteOrder.LITTLE_ENDIAN);
        raf.writeLong(1);
        raf.seek(0);
        raf.read(buf);
        assertEquals(1, buf[0]);
        assertEquals(0, buf[7]);
        raf.close();
    }

    @Test
    public void testMark()
    {
        final IRandomAccessFile raf = createRandomAccessFile("testMark");
        final byte[] buf = new byte[128];
        raf.write(buf);
        raf.seek(0);
        assertTrue(raf.markSupported());
        raf.mark(0);
        raf.read();
        assertEquals(1, raf.getFilePointer());
        raf.reset();
        assertEquals(0, raf.getFilePointer());
        raf.read();
        raf.read();
        raf.read();
        raf.mark(0);
        raf.read();
        raf.read();
        raf.read();
        raf.reset();
        assertEquals(3, raf.getFilePointer());
        raf.close();
    }

    @Test
    public void testWriteReadByte()
    {
        final IRandomAccessFile raf = createRandomAccessFile("testWriteReadByte");
        raf.write(254);
        raf.seek(0);
        assertEquals(-2, raf.readByte());
        raf.seek(0);
        assertEquals(254, raf.read());
        raf.seek(0);
        assertEquals(254, raf.readUnsignedByte());
        raf.close();
    }

    @Test
    public void testWriteReadShort()
    {
        final IRandomAccessFile raf = createRandomAccessFile("testWriteReadShort");
        raf.writeShort(65534);
        raf.seek(0);
        assertEquals(-2, raf.readShort());
        raf.seek(0);
        assertEquals(65534, raf.readUnsignedShort());
        raf.close();
    }

    @Test
    public void testAvailable()
    {
        final IRandomAccessFile raf = createRandomAccessFile("testAvailable");
        assertEquals(0, raf.available());
        raf.writeDouble(5.5);
        raf.seek(0);
        assertEquals(8, raf.available());
        raf.close();
    }

    @Test
    public void testWriteReadStringBytes()
    {
        final String s = "teststring";
        final IRandomAccessFile raf = createRandomAccessFile("testWriteReadStringBytes");
        raf.writeBytes(s);
        raf.seek(0);
        assertEquals(s.length(), raf.available());
        final byte[] buf = new byte[raf.available()];
        raf.read(buf);
        assertEquals(s, new String(buf));
        raf.close();
    }

    @Test
    public void testWriteReadStringChars()
    {
        final String s = "teststring";
        final IRandomAccessFile raf = createRandomAccessFile("testWriteReadStringChars");
        raf.writeChars(s);
        raf.seek(0);
        assertEquals(2 * s.length(), raf.available());
        final byte[] buf = new byte[raf.available()];
        raf.read(buf);
        assertEquals(
                s,
                new String(NativeData.byteToChar(buf,
                        ch.systemsx.cisd.base.convert.NativeData.ByteOrder.BIG_ENDIAN)));
        raf.close();
    }

    @Test
    public void testReadLine() throws IOException
    {
        final byte[] bytes = "hello world".getBytes();
        final IRandomAccessFile raf = createRandomAccessFile("testWriteReadStringChars", bytes);
        final AdapterIInputStreamToInputStream is = new AdapterIInputStreamToInputStream(raf);

        assertEquals("[hello world]", IOUtils.readLines(is, Charset.defaultCharset()).toString());
        raf.close();
    }

    @Test
    public void testToByteArray() throws IOException
    {
        final byte[] bytes = "hello world".getBytes();
        final IRandomAccessFile raf = createRandomAccessFile("testWriteReadStringChars", bytes);
        final AdapterIInputStreamToInputStream is = new AdapterIInputStreamToInputStream(raf);

        assertEquals(bytes, IOUtils.toByteArray(is));
        raf.close();
    }

}