View Javadoc

1   /* RepositionableInputStreamTest.java
2    *
3    * $Id: RepositionableInputStreamTest.java 4074 2005-12-21 19:20:38Z stack-sf $
4    *
5    * Created Dec 20, 2005
6    *
7    * Copyright (C) 2005 Internet Archive.
8    *
9    * 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      protected void setUp() throws Exception {
38          super.setUp();
39          this.testFile = new File(getTmpDir(), this.getClass().getName());
40          PrintWriter pw = new PrintWriter(new FileOutputStream(testFile));
41          for (int i = 0; i < 100; i++) {
42              pw.print(LINE);
43          }
44          pw.close();
45      }
46      protected void tearDown() throws Exception {
47          super.tearDown();
48      }
49      public void testname() throws Exception {
50          // Make buffer awkward size so we run into buffers spanning issues.
51          RepositionableInputStream ris =
52              new RepositionableInputStream(new FileInputStream(this.testFile),
53                  57);
54          int c = ris.read();
55          assertEquals(1, ris.position());
56          ris.read();
57          ris.position(0);
58          assertEquals(0, ris.position());
59          int c1 = ris.read();
60          assertEquals(c, c1);
61          ris.position(0);
62          byte [] bytes = new byte[LINE.length()];
63          long offset = 0;
64          for (int i = 0; i < 10; i++) {
65              ris.read(bytes, 0, LINE.length());
66              assertEquals(LINE, new String(bytes));
67              offset += LINE.length();
68              assertEquals(offset, ris.position());
69          }
70          long p = ris.position();
71          ris.position(p - LINE.length());
72          assertEquals(p - LINE.length(), ris.position());
73          c = ris.read();
74          assertEquals(c, c1);
75      }
76  }