KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > archive > io > RecordingInputStreamTest


1 /* RecordingInputStreamTest
2  *
3  * $Id: RecordingInputStreamTest.java,v 1.2 2006/06/23 00:45:18 stack-sf Exp $
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 JavaDoc;
28 import java.io.ByteArrayOutputStream JavaDoc;
29 import java.io.File JavaDoc;
30 import java.io.IOException JavaDoc;
31
32 import org.archive.util.TmpDirTestCase;
33
34
35 /**
36  * Test cases for RecordingInputStream.
37  *
38  * @author gojomo
39  */

40 public class RecordingInputStreamTest extends TmpDirTestCase
41 {
42
43
44     /*
45      * @see TmpDirTestCase#setUp()
46      */

47     protected void setUp() throws Exception JavaDoc
48     {
49         super.setUp();
50     }
51
52     /**
53      * Test readFullyOrUntil soft (no exception) and hard (exception)
54      * length cutoffs.
55      *
56      * @throws IOException
57      * @throws InterruptedException
58      * @throws RecorderTimeoutException
59      */

60     public void testReadFullyOrUntil() throws RecorderTimeoutException, IOException JavaDoc, InterruptedException JavaDoc
61     {
62         RecordingInputStream ris = new RecordingInputStream(16384, (new File JavaDoc(
63                 getTmpDir(), "testReadFullyOrUntil").getAbsolutePath()));
64         ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(
65                 "abcdefghijklmnopqrstuvwxyz".getBytes());
66         // test soft max
67
ris.open(bais);
68         ris.readFullyOrUntil(7,10,0,0);
69         ris.close();
70         ReplayInputStream res = ris.getReplayInputStream();
71         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
72         res.readFullyTo(baos);
73         assertEquals("soft max cutoff","abcdefg",new String JavaDoc(baos.toByteArray()));
74         // test hard max
75
bais.reset();
76         baos.reset();
77         ris.open(bais);
78         boolean exceptionThrown = false;
79         try {
80             ris.readFullyOrUntil(13,10,0,0);
81         } catch (RecorderLengthExceededException ex) {
82             exceptionThrown = true;
83         }
84         assertTrue("hard max exception",exceptionThrown);
85         ris.close();
86         res = ris.getReplayInputStream();
87         res.readFullyTo(baos);
88         assertEquals("hard max cutoff","abcdefghijk",
89                 new String JavaDoc(baos.toByteArray()));
90         // TODO: test timeout?
91
}
92 }
93
Popular Tags