1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.archive.io;
24
25 import java.util.Random;
26
27 import junit.framework.TestCase;
28
29
30 /***
31 * Unit test for BufferedSeekInputStream. The tests do some random
32 * repositioning in the stream to make sure the buffer is always valid.
33 *
34 * @author pjack
35 */
36 public class BufferedSeekInputStreamTest extends TestCase {
37
38
39 private static byte[] TEST_DATA = makeTestData();
40
41 public void testPosition() throws Exception {
42 Random random = new Random();
43 ArraySeekInputStream asis = new ArraySeekInputStream(TEST_DATA);
44 BufferedSeekInputStream bsis = new BufferedSeekInputStream(asis, 11);
45 for (int i = 0; i < TEST_DATA.length; i++) {
46 byte b = (byte)bsis.read();
47 assertEquals(TEST_DATA[i], b);
48 }
49 for (int i = 0; i < 1000; i++) {
50 int index = random.nextInt(TEST_DATA.length);
51 bsis.position(index);
52 char expected = (char)((int)TEST_DATA[index] & 0xFF);
53 char read = (char)(bsis.read() & 0xFF);
54 assertEquals(expected, read);
55 }
56 }
57
58
59 private static byte[] makeTestData() {
60 String s = "If the dull substance of my flesh were thought\n"
61 + "Injurious distance could not stop my way\n"
62 + "For then, despite of space, I would be brought\n"
63 + "From limits far remote where thou dost stay.\n";
64 byte[] r = new byte[s.length()];
65 for (int i = 0; i < r.length; i++) {
66 r[i] = (byte)s.charAt(i);
67
68 }
69 return r;
70 }
71 }