01: /* BufferedSeekInputStreamTest
02: *
03: * Created on September 18, 2006
04: *
05: * Copyright (C) 2006 Internet Archive.
06: *
07: * This file is part of the Heritrix web crawler (crawler.archive.org).
08: *
09: * Heritrix is free software; you can redistribute it and/or modify
10: * it under the terms of the GNU Lesser Public License as published by
11: * the Free Software Foundation; either version 2.1 of the License, or
12: * any later version.
13: *
14: * Heritrix is distributed in the hope that it will be useful,
15: * but WITHOUT ANY WARRANTY; without even the implied warranty of
16: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17: * GNU Lesser Public License for more details.
18: *
19: * You should have received a copy of the GNU Lesser Public License
20: * along with Heritrix; if not, write to the Free Software
21: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22: */
23: package org.archive.io;
24:
25: import java.util.Random;
26:
27: import junit.framework.TestCase;
28:
29: /**
30: * Unit test for BufferedSeekInputStream. The tests do some random
31: * repositioning in the stream to make sure the buffer is always valid.
32: *
33: * @author pjack
34: */
35: public class BufferedSeekInputStreamTest extends TestCase {
36:
37: private static byte[] TEST_DATA = makeTestData();
38:
39: public void testPosition() throws Exception {
40: Random random = new Random();
41: ArraySeekInputStream asis = new ArraySeekInputStream(TEST_DATA);
42: BufferedSeekInputStream bsis = new BufferedSeekInputStream(
43: asis, 11);
44: for (int i = 0; i < TEST_DATA.length; i++) {
45: byte b = (byte) bsis.read();
46: assertEquals(TEST_DATA[i], b);
47: }
48: for (int i = 0; i < 1000; i++) {
49: int index = random.nextInt(TEST_DATA.length);
50: bsis.position(index);
51: char expected = (char) ((int) TEST_DATA[index] & 0xFF);
52: char read = (char) (bsis.read() & 0xFF);
53: assertEquals(expected, read);
54: }
55: }
56:
57: private static byte[] makeTestData() {
58: String s = "If the dull substance of my flesh were thought\n"
59: + "Injurious distance could not stop my way\n"
60: + "For then, despite of space, I would be brought\n"
61: + "From limits far remote where thou dost stay.\n";
62: byte[] r = new byte[s.length()];
63: for (int i = 0; i < r.length; i++) {
64: r[i] = (byte) s.charAt(i);
65: // r[i] = (byte)s.charAt(i);
66: }
67: return r;
68: }
69: }
|