001: /* RecordingOutputStreamTest
002: *
003: * $Id: RecordingOutputStreamTest.java 1388 2004-02-18 00:11:36Z stack-sf $
004: *
005: * Created on Jan 21, 2004
006: *
007: * Copyright (C) 2004 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.File;
028: import java.io.FileInputStream;
029: import java.io.FileOutputStream;
030: import java.io.IOException;
031:
032: import org.archive.util.TmpDirTestCase;
033:
034: /**
035: * Test casesfor RecordingOutputStream.
036: *
037: * @author stack
038: */
039: public class RecordingOutputStreamTest extends TmpDirTestCase {
040: /**
041: * Size of buffer used in tests.
042: */
043: private static final int BUFFER_SIZE = 5;
044:
045: /**
046: * How much to write total to testing RecordingOutputStream.
047: */
048: private static final int WRITE_TOTAL = 10;
049:
050: /*
051: * @see TmpDirTestCase#setUp()
052: */
053: protected void setUp() throws Exception {
054: super .setUp();
055: }
056:
057: /**
058: * Test reusing instance of RecordingOutputStream.
059: *
060: * @throws IOException Failed open of backing file or opening of
061: * input streams verifying recording.
062: */
063: public void testReuse() throws IOException {
064: final String BASENAME = "testReuse";
065: cleanUpOldFiles(BASENAME);
066: RecordingOutputStream ros = new RecordingOutputStream(
067: BUFFER_SIZE, (new File(getTmpDir(), BASENAME
068: + "Bkg.txt")).getAbsolutePath());
069: for (int i = 0; i < 3; i++) {
070: reuse(BASENAME, ros, i);
071: }
072: }
073:
074: private void reuse(String baseName, RecordingOutputStream ros,
075: int index) throws IOException {
076: final String BASENAME = baseName + Integer.toString(index);
077: File f = writeIntRecordedFile(ros, BASENAME, WRITE_TOTAL);
078: verifyRecording(ros, f, WRITE_TOTAL);
079: // Do again to test that I can get a new ReplayInputStream on same
080: // RecordingOutputStream.
081: verifyRecording(ros, f, WRITE_TOTAL);
082: }
083:
084: /**
085: * Method to test for void write(int).
086: *
087: * Uses small buffer size and small write size. Test mark and reset too.
088: *
089: * @throws IOException Failed open of backing file or opening of
090: * input streams verifying recording.
091: */
092: public void testWriteint() throws IOException {
093: final String BASENAME = "testWriteint";
094: cleanUpOldFiles(BASENAME);
095: RecordingOutputStream ros = new RecordingOutputStream(
096: BUFFER_SIZE, (new File(getTmpDir(), BASENAME
097: + "Backing.txt")).getAbsolutePath());
098: File f = writeIntRecordedFile(ros, BASENAME, WRITE_TOTAL);
099: verifyRecording(ros, f, WRITE_TOTAL);
100: // Do again to test that I can get a new ReplayInputStream on same
101: // RecordingOutputStream.
102: verifyRecording(ros, f, WRITE_TOTAL);
103: }
104:
105: /**
106: * Method to test for void write(byte []).
107: *
108: * Uses small buffer size and small write size.
109: *
110: * @throws IOException Failed open of backing file or opening of
111: * input streams verifying recording.
112: */
113: public void testWritebytearray() throws IOException {
114: final String BASENAME = "testWritebytearray";
115: cleanUpOldFiles(BASENAME);
116: RecordingOutputStream ros = new RecordingOutputStream(
117: BUFFER_SIZE, (new File(getTmpDir(), BASENAME
118: + "Backing.txt")).getAbsolutePath());
119: File f = writeByteRecordedFile(ros, BASENAME, WRITE_TOTAL);
120: verifyRecording(ros, f, WRITE_TOTAL);
121: // Do again to test that I can get a new ReplayInputStream on same
122: // RecordingOutputStream.
123: verifyRecording(ros, f, WRITE_TOTAL);
124: }
125:
126: /**
127: * Test mark and reset.
128: * @throws IOException
129: */
130: public void testMarkReset() throws IOException {
131: final String BASENAME = "testMarkReset";
132: cleanUpOldFiles(BASENAME);
133: RecordingOutputStream ros = new RecordingOutputStream(
134: BUFFER_SIZE, (new File(getTmpDir(), BASENAME
135: + "Backing.txt")).getAbsolutePath());
136: File f = writeByteRecordedFile(ros, BASENAME, WRITE_TOTAL);
137: verifyRecording(ros, f, WRITE_TOTAL);
138: ReplayInputStream ris = ros.getReplayInputStream();
139: ris.mark(10 /*Arbitrary value*/);
140: // Read from the stream.
141: ris.read();
142: ris.read();
143: ris.read();
144: // Reset it. It should be back at zero.
145: ris.reset();
146: assertEquals("Reset to zero", ris.read(), 0);
147: assertEquals("Reset to zero char 1", ris.read(), 1);
148: assertEquals("Reset to zero char 2", ris.read(), 2);
149: // Mark stream. Here. Next character should be '3'.
150: ris.mark(10 /* Arbitrary value*/);
151: ris.read();
152: ris.read();
153: ris.reset();
154: assertEquals("Reset to zero char 3", ris.read(), 3);
155: }
156:
157: /**
158: * Record a file write.
159: *
160: * Write a file w/ characters that start at null and ascend to
161: * <code>filesize</code>. Record the writing w/ passed <code>ros</code>
162: * recordingoutputstream. Return the file recorded as result of method.
163: * The file output stream that is recorded is named
164: * <code>basename</code> + ".txt".
165: *
166: * <p>This method writes a character at a time.
167: *
168: * @param ros RecordingOutputStream to record with.
169: * @param basename Basename of file.
170: * @param size How many characters to write.
171: * @return Recorded output stream.
172: */
173: private File writeIntRecordedFile(RecordingOutputStream ros,
174: String basename, int size) throws IOException {
175: File f = new File(getTmpDir(), basename + ".txt");
176: FileOutputStream fos = new FileOutputStream(f);
177: ros.open(fos);
178: for (int i = 0; i < WRITE_TOTAL; i++) {
179: ros.write(i);
180: }
181: ros.close();
182: fos.close();
183: assertEquals("Content-Length test", size, ros
184: .getResponseContentLength());
185: return f;
186: }
187:
188: /**
189: * Record a file byte array write.
190: *
191: * Write a file w/ characters that start at null and ascend to
192: * <code>filesize</code>. Record the writing w/ passed <code>ros</code>
193: * recordingoutputstream. Return the file recorded as result of method.
194: * The file output stream that is recorded is named
195: * <code>basename</code> + ".txt".
196: *
197: * <p>This method writes using a byte array.
198: *
199: * @param ros RecordingOutputStream to record with.
200: * @param basename Basename of file.
201: * @param size How many characters to write.
202: * @return Recorded output stream.
203: */
204: private File writeByteRecordedFile(RecordingOutputStream ros,
205: String basename, int size) throws IOException {
206: File f = new File(getTmpDir(), basename + ".txt");
207: FileOutputStream fos = new FileOutputStream(f);
208: ros.open(fos);
209: byte[] b = new byte[size];
210: for (int i = 0; i < size; i++) {
211: b[i] = (byte) i;
212: }
213: ros.write(b);
214: ros.close();
215: fos.close();
216: assertEquals("Content-Length test", size, ros
217: .getResponseContentLength());
218: return f;
219: }
220:
221: /**
222: * Verify what was written is both in the file written to and in the
223: * recording stream.
224: *
225: * @param ros Stream to check.
226: * @param f File that was recorded. Stream should have its content
227: * exactly.
228: * @param size Amount of bytes written.
229: *
230: * @exception IOException Failure reading streams.
231: */
232: private void verifyRecording(RecordingOutputStream ros, File f,
233: int size) throws IOException {
234: assertEquals("Recorded file size.", size, f.length());
235: FileInputStream fis = new FileInputStream(f);
236: assertNotNull("FileInputStream not null", fis);
237: ReplayInputStream ris = ros.getReplayInputStream();
238: assertNotNull("ReplayInputStream not null", ris);
239: for (int i = 0; i < size; i++) {
240: assertEquals("ReplayInputStream content verification", i,
241: ris.read());
242: assertEquals("Recorded file content verification", i, fis
243: .read());
244: }
245: assertEquals("ReplayInputStream at EOF", -1, ris.read());
246: fis.close();
247: ris.close();
248: }
249: }
|