View Javadoc

1   /* BufferedSeekInputStreamTest
2   *
3   * Created on September 18, 2006
4   *
5   * Copyright (C) 2006 Internet Archive.
6   *
7   * This file is part of the Heritrix web crawler (crawler.archive.org).
8   *
9   * 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  /***
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  //            r[i] = (byte)s.charAt(i);
68          }
69          return r;
70      }
71  }