001: /* RecordingInputStreamTest
002: *
003: * $Id: RecordingInputStreamTest.java 5080 2007-04-13 20:30:49Z gojomo $
004: *
005: * Created on Aug 1, 2005
006: *
007: * Copyright (C) 2005 Internet Archive.
008: *
009: * This file is part of the Heritrix web crawler (crawler.archive.org).
010: *
011: * Heritrix is free software; you can redistribute it and/or modify
012: * it under the terms of the GNU Lesser Public License as published by
013: * the Free Software Foundation; either version 2.1 of the License, or
014: * any later version.
015: *
016: * Heritrix is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
019: * GNU Lesser Public License for more details.
020: *
021: * You should have received a copy of the GNU Lesser Public License
022: * along with Heritrix; if not, write to the Free Software
023: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024: */
025: package org.archive.io;
026:
027: import java.io.ByteArrayInputStream;
028: import java.io.ByteArrayOutputStream;
029: import java.io.File;
030: import java.io.IOException;
031: import java.io.PipedInputStream;
032: import java.io.PipedOutputStream;
033:
034: import org.archive.util.TmpDirTestCase;
035:
036: /**
037: * Test cases for RecordingInputStream.
038: *
039: * @author gojomo
040: */
041: public class RecordingInputStreamTest extends TmpDirTestCase {
042:
043: /*
044: * @see TmpDirTestCase#setUp()
045: */
046: protected void setUp() throws Exception {
047: super .setUp();
048: }
049:
050: /**
051: * Test readFullyOrUntil soft (no exception) and hard (exception)
052: * length cutoffs, timeout, and rate-throttling.
053: *
054: * @throws IOException
055: * @throws InterruptedException
056: * @throws RecorderTimeoutException
057: */
058: public void testReadFullyOrUntil() throws RecorderTimeoutException,
059: IOException, InterruptedException {
060: RecordingInputStream ris = new RecordingInputStream(16384,
061: (new File(getTmpDir(), "testReadFullyOrUntil")
062: .getAbsolutePath()));
063: ByteArrayInputStream bais = new ByteArrayInputStream(
064: "abcdefghijklmnopqrstuvwxyz".getBytes());
065: // test soft max
066: ris.open(bais);
067: ris.setLimits(10, 0, 0);
068: ris.readFullyOrUntil(7);
069: ris.close();
070: ReplayInputStream res = ris.getReplayInputStream();
071: ByteArrayOutputStream baos = new ByteArrayOutputStream();
072: res.readFullyTo(baos);
073: assertEquals("soft max cutoff", "abcdefg", new String(baos
074: .toByteArray()));
075: // test hard max
076: bais.reset();
077: baos.reset();
078: ris.open(bais);
079: boolean exceptionThrown = false;
080: try {
081: ris.setLimits(10, 0, 0);
082: ris.readFullyOrUntil(13);
083: } catch (RecorderLengthExceededException ex) {
084: exceptionThrown = true;
085: }
086: assertTrue("hard max exception", exceptionThrown);
087: ris.close();
088: res = ris.getReplayInputStream();
089: res.readFullyTo(baos);
090: assertEquals("hard max cutoff", "abcdefghijk", new String(baos
091: .toByteArray()));
092: // test timeout
093: PipedInputStream pin = new PipedInputStream();
094: PipedOutputStream pout = new PipedOutputStream(pin);
095: ris.open(pin);
096: exceptionThrown = false;
097: trickle("abcdefghijklmnopqrstuvwxyz".getBytes(), pout);
098: try {
099: ris.setLimits(0, 5000, 0);
100: ris.readFullyOrUntil(0);
101: } catch (RecorderTimeoutException ex) {
102: exceptionThrown = true;
103: }
104: assertTrue("timeout exception", exceptionThrown);
105: ris.close();
106: // test rate limit
107: bais = new ByteArrayInputStream(new byte[1024 * 2 * 5]);
108: ris.open(bais);
109: long startTime = System.currentTimeMillis();
110: ris.setLimits(0, 0, 2);
111: ris.readFullyOrUntil(0);
112: long endTime = System.currentTimeMillis();
113: long duration = endTime - startTime;
114: assertTrue("read too fast: " + duration, duration >= 5000);
115: ris.close();
116: }
117:
118: protected void trickle(final byte[] bytes,
119: final PipedOutputStream pout) {
120: new Thread() {
121: public void run() {
122: try {
123: for (int i = 0; i < bytes.length; i++) {
124: Thread.sleep(1000);
125: pout.write(bytes[i]);
126: }
127: pout.close();
128: } catch (IOException e) {
129: // do nothing
130: } catch (Exception e) {
131: System.err.print(e);
132: }
133: }
134: }.start();
135:
136: }
137: }
|