01: /* RepositionableInputStreamTest.java
02: *
03: * $Id: RepositionableInputStreamTest.java 4074 2005-12-21 19:20:38Z stack-sf $
04: *
05: * Created Dec 20, 2005
06: *
07: * Copyright (C) 2005 Internet Archive.
08: *
09: * This file is part of the Heritrix web crawler (crawler.archive.org).
10: *
11: * Heritrix is free software; you can redistribute it and/or modify
12: * it under the terms of the GNU Lesser Public License as published by
13: * the Free Software Foundation; either version 2.1 of the License, or
14: * any later version.
15: *
16: * Heritrix is distributed in the hope that it will be useful,
17: * but WITHOUT ANY WARRANTY; without even the implied warranty of
18: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19: * GNU Lesser Public License for more details.
20: *
21: * You should have received a copy of the GNU Lesser Public License
22: * along with Heritrix; if not, write to the Free Software
23: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24: */
25: package org.archive.io;
26:
27: import java.io.File;
28: import java.io.FileInputStream;
29: import java.io.FileOutputStream;
30: import java.io.PrintWriter;
31:
32: import org.archive.util.TmpDirTestCase;
33:
34: public class RepositionableInputStreamTest extends TmpDirTestCase {
35: private File testFile;
36: private static final String LINE = "0123456789abcdefghijklmnopqrstuv";
37:
38: protected void setUp() throws Exception {
39: super .setUp();
40: this .testFile = new File(getTmpDir(), this .getClass().getName());
41: PrintWriter pw = new PrintWriter(new FileOutputStream(testFile));
42: for (int i = 0; i < 100; i++) {
43: pw.print(LINE);
44: }
45: pw.close();
46: }
47:
48: protected void tearDown() throws Exception {
49: super .tearDown();
50: }
51:
52: public void testname() throws Exception {
53: // Make buffer awkward size so we run into buffers spanning issues.
54: RepositionableInputStream ris = new RepositionableInputStream(
55: new FileInputStream(this .testFile), 57);
56: int c = ris.read();
57: assertEquals(1, ris.position());
58: ris.read();
59: ris.position(0);
60: assertEquals(0, ris.position());
61: int c1 = ris.read();
62: assertEquals(c, c1);
63: ris.position(0);
64: byte[] bytes = new byte[LINE.length()];
65: long offset = 0;
66: for (int i = 0; i < 10; i++) {
67: ris.read(bytes, 0, LINE.length());
68: assertEquals(LINE, new String(bytes));
69: offset += LINE.length();
70: assertEquals(offset, ris.position());
71: }
72: long p = ris.position();
73: ris.position(p - LINE.length());
74: assertEquals(p - LINE.length(), ris.position());
75: c = ris.read();
76: assertEquals(c, c1);
77: }
78: }
|