1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
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  
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          
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          
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          
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         
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                     
131                 } catch (Exception e) {
132                     System.err.print(e); 
133                 }                
134             }
135         }.start();
136         
137     }
138 }