View Javadoc

1   /* RecordingInputStreamTest
2    *
3    * $Id: RecordingInputStreamTest.java 5080 2007-04-13 20:30:49Z gojomo $
4    *
5    * Created on Aug 1, 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.ByteArrayInputStream;
28  import java.io.ByteArrayOutputStream;
29  import java.io.File;
30  import java.io.IOException;
31  import java.io.PipedInputStream;
32  import java.io.PipedOutputStream;
33  
34  import org.archive.util.TmpDirTestCase;
35  
36  
37  /***
38   * Test cases for RecordingInputStream.
39   *
40   * @author gojomo
41   */
42  public class RecordingInputStreamTest extends TmpDirTestCase
43  {
44  
45  
46      /*
47       * @see TmpDirTestCase#setUp()
48       */
49      protected void setUp() throws Exception
50      {
51          super.setUp();
52      }
53  
54      /***
55       * Test readFullyOrUntil soft (no exception) and hard (exception) 
56       * length cutoffs, timeout, and rate-throttling. 
57       * 
58       * @throws IOException
59       * @throws InterruptedException
60       * @throws RecorderTimeoutException
61       */
62      public void testReadFullyOrUntil() throws RecorderTimeoutException, IOException, InterruptedException
63      {
64          RecordingInputStream ris = new RecordingInputStream(16384, (new File(
65                  getTmpDir(), "testReadFullyOrUntil").getAbsolutePath()));
66          ByteArrayInputStream bais = new ByteArrayInputStream(
67                  "abcdefghijklmnopqrstuvwxyz".getBytes());
68          // test soft max
69          ris.open(bais);
70          ris.setLimits(10,0,0);
71          ris.readFullyOrUntil(7);
72          ris.close();
73          ReplayInputStream res = ris.getReplayInputStream();
74          ByteArrayOutputStream baos = new ByteArrayOutputStream();
75          res.readFullyTo(baos);
76          assertEquals("soft max cutoff","abcdefg",new String(baos.toByteArray()));
77          // test hard max
78          bais.reset();
79          baos.reset();
80          ris.open(bais);
81          boolean exceptionThrown = false; 
82          try {
83              ris.setLimits(10,0,0);
84              ris.readFullyOrUntil(13);
85          } catch (RecorderLengthExceededException ex) {
86              exceptionThrown = true;
87          }
88          assertTrue("hard max exception",exceptionThrown);
89          ris.close();
90          res = ris.getReplayInputStream();
91          res.readFullyTo(baos);
92          assertEquals("hard max cutoff","abcdefghijk",
93                  new String(baos.toByteArray()));
94          // test timeout
95          PipedInputStream pin = new PipedInputStream(); 
96          PipedOutputStream pout = new PipedOutputStream(pin); 
97          ris.open(pin);
98          exceptionThrown = false; 
99          trickle("abcdefghijklmnopqrstuvwxyz".getBytes(),pout);
100         try {
101             ris.setLimits(0,5000,0);
102             ris.readFullyOrUntil(0);
103         } catch (RecorderTimeoutException ex) {
104             exceptionThrown = true;
105         }
106         assertTrue("timeout exception",exceptionThrown);
107         ris.close();
108         // test rate limit
109         bais = new ByteArrayInputStream(new byte[1024*2*5]);
110         ris.open(bais);
111         long startTime = System.currentTimeMillis();
112         ris.setLimits(0,0,2);
113         ris.readFullyOrUntil(0);
114         long endTime = System.currentTimeMillis(); 
115         long duration = endTime - startTime; 
116         assertTrue("read too fast: "+duration,duration>=5000);
117         ris.close();
118     }
119 
120     protected void trickle(final byte[] bytes, final PipedOutputStream pout) {
121         new Thread() {
122             public void run() {
123                 try {
124                     for (int i = 0; i < bytes.length; i++) {
125                         Thread.sleep(1000);
126                         pout.write(bytes[i]);
127                     }
128                     pout.close();
129                 } catch (IOException e) {
130                     // do nothing
131                 } catch (Exception e) {
132                     System.err.print(e); 
133                 }                
134             }
135         }.start();
136         
137     }
138 }