|
| 1 | +/* |
| 2 | + * This file is part of the Heritrix web crawler (crawler.archive.org). |
| 3 | + * |
| 4 | + * Licensed to the Internet Archive (IA) by one or more individual |
| 5 | + * contributors. |
| 6 | + * |
| 7 | + * The IA licenses this file to You under the Apache License, Version 2.0 |
| 8 | + * (the "License"); you may not use this file except in compliance with |
| 9 | + * the License. You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | +package org.archive.io; |
| 20 | + |
| 21 | +import java.io.File; |
| 22 | +import java.io.FileInputStream; |
| 23 | +import java.io.FileOutputStream; |
| 24 | +import java.io.IOException; |
| 25 | + |
| 26 | +import org.archive.util.TmpDirTestCase; |
| 27 | + |
| 28 | + |
| 29 | +/** |
| 30 | + * Test casesfor RecordingOutputStream. |
| 31 | + * |
| 32 | + * @author stack |
| 33 | + */ |
| 34 | +public class RecordingOutputStreamTest extends TmpDirTestCase |
| 35 | +{ |
| 36 | + /** |
| 37 | + * Size of buffer used in tests. |
| 38 | + */ |
| 39 | + private static final int BUFFER_SIZE = 5; |
| 40 | + |
| 41 | + /** |
| 42 | + * How much to write total to testing RecordingOutputStream. |
| 43 | + */ |
| 44 | + private static final int WRITE_TOTAL = 10; |
| 45 | + |
| 46 | + |
| 47 | + /* |
| 48 | + * @see TmpDirTestCase#setUp() |
| 49 | + */ |
| 50 | + protected void setUp() throws Exception |
| 51 | + { |
| 52 | + super.setUp(); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Test reusing instance of RecordingOutputStream. |
| 57 | + * |
| 58 | + * @throws IOException Failed open of backing file or opening of |
| 59 | + * input streams verifying recording. |
| 60 | + */ |
| 61 | + public void testReuse() |
| 62 | + throws IOException |
| 63 | + { |
| 64 | + final String BASENAME = "testReuse"; |
| 65 | + cleanUpOldFiles(BASENAME); |
| 66 | + RecordingOutputStream ros = new RecordingOutputStream(BUFFER_SIZE, |
| 67 | + (new File(getTmpDir(), BASENAME + "Bkg.txt")).getAbsolutePath()); |
| 68 | + for (int i = 0; i < 3; i++) |
| 69 | + { |
| 70 | + reuse(BASENAME, ros, i); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private void reuse(String baseName, RecordingOutputStream ros, int index) |
| 75 | + throws IOException |
| 76 | + { |
| 77 | + final String BASENAME = baseName + Integer.toString(index); |
| 78 | + File f = writeIntRecordedFile(ros, BASENAME, WRITE_TOTAL); |
| 79 | + verifyRecording(ros, f, WRITE_TOTAL); |
| 80 | + // Do again to test that I can get a new ReplayInputStream on same |
| 81 | + // RecordingOutputStream. |
| 82 | + verifyRecording(ros, f, WRITE_TOTAL); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Method to test for void write(int). |
| 87 | + * |
| 88 | + * Uses small buffer size and small write size. Test mark and reset too. |
| 89 | + * |
| 90 | + * @throws IOException Failed open of backing file or opening of |
| 91 | + * input streams verifying recording. |
| 92 | + */ |
| 93 | + public void testWriteint() |
| 94 | + throws IOException |
| 95 | + { |
| 96 | + final String BASENAME = "testWriteint"; |
| 97 | + cleanUpOldFiles(BASENAME); |
| 98 | + RecordingOutputStream ros = new RecordingOutputStream(BUFFER_SIZE, |
| 99 | + (new File(getTmpDir(), BASENAME + "Backing.txt")).getAbsolutePath()); |
| 100 | + File f = writeIntRecordedFile(ros, BASENAME, WRITE_TOTAL); |
| 101 | + verifyRecording(ros, f, WRITE_TOTAL); |
| 102 | + // Do again to test that I can get a new ReplayInputStream on same |
| 103 | + // RecordingOutputStream. |
| 104 | + verifyRecording(ros, f, WRITE_TOTAL); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Method to test for void write(byte []). |
| 109 | + * |
| 110 | + * Uses small buffer size and small write size. |
| 111 | + * |
| 112 | + * @throws IOException Failed open of backing file or opening of |
| 113 | + * input streams verifying recording. |
| 114 | + */ |
| 115 | + public void testWritebytearray() |
| 116 | + throws IOException |
| 117 | + { |
| 118 | + final String BASENAME = "testWritebytearray"; |
| 119 | + cleanUpOldFiles(BASENAME); |
| 120 | + RecordingOutputStream ros = new RecordingOutputStream(BUFFER_SIZE, |
| 121 | + (new File(getTmpDir(), BASENAME + "Backing.txt")).getAbsolutePath()); |
| 122 | + File f = writeByteRecordedFile(ros, BASENAME, WRITE_TOTAL); |
| 123 | + verifyRecording(ros, f, WRITE_TOTAL); |
| 124 | + // Do again to test that I can get a new ReplayInputStream on same |
| 125 | + // RecordingOutputStream. |
| 126 | + verifyRecording(ros, f, WRITE_TOTAL); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Test mark and reset. |
| 131 | + * @throws IOException |
| 132 | + */ |
| 133 | + public void testMarkReset() throws IOException |
| 134 | + { |
| 135 | + final String BASENAME = "testMarkReset"; |
| 136 | + cleanUpOldFiles(BASENAME); |
| 137 | + RecordingOutputStream ros = new RecordingOutputStream(BUFFER_SIZE, |
| 138 | + (new File(getTmpDir(), BASENAME + "Backing.txt")).getAbsolutePath()); |
| 139 | + File f = writeByteRecordedFile(ros, BASENAME, WRITE_TOTAL); |
| 140 | + verifyRecording(ros, f, WRITE_TOTAL); |
| 141 | + ReplayInputStream ris = ros.getReplayInputStream(); |
| 142 | + ris.mark(10 /*Arbitrary value*/); |
| 143 | + // Read from the stream. |
| 144 | + ris.read(); |
| 145 | + ris.read(); |
| 146 | + ris.read(); |
| 147 | + // Reset it. It should be back at zero. |
| 148 | + ris.reset(); |
| 149 | + assertEquals("Reset to zero", ris.read(), 0); |
| 150 | + assertEquals("Reset to zero char 1", ris.read(), 1); |
| 151 | + assertEquals("Reset to zero char 2", ris.read(), 2); |
| 152 | + // Mark stream. Here. Next character should be '3'. |
| 153 | + ris.mark(10 /* Arbitrary value*/); |
| 154 | + ris.read(); |
| 155 | + ris.read(); |
| 156 | + ris.reset(); |
| 157 | + assertEquals("Reset to zero char 3", ris.read(), 3); |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Record a file write. |
| 162 | + * |
| 163 | + * Write a file w/ characters that start at null and ascend to |
| 164 | + * <code>filesize</code>. Record the writing w/ passed <code>ros</code> |
| 165 | + * recordingoutputstream. Return the file recorded as result of method. |
| 166 | + * The file output stream that is recorded is named |
| 167 | + * <code>basename</code> + ".txt". |
| 168 | + * |
| 169 | + * <p>This method writes a character at a time. |
| 170 | + * |
| 171 | + * @param ros RecordingOutputStream to record with. |
| 172 | + * @param basename Basename of file. |
| 173 | + * @param size How many characters to write. |
| 174 | + * @return Recorded output stream. |
| 175 | + */ |
| 176 | + private File writeIntRecordedFile(RecordingOutputStream ros, |
| 177 | + String basename, int size) |
| 178 | + throws IOException |
| 179 | + { |
| 180 | + File f = new File(getTmpDir(), basename + ".txt"); |
| 181 | + FileOutputStream fos = new FileOutputStream(f); |
| 182 | + ros.open(fos); |
| 183 | + for (int i = 0; i < WRITE_TOTAL; i++) |
| 184 | + { |
| 185 | + ros.write(i); |
| 186 | + } |
| 187 | + ros.close(); |
| 188 | + fos.close(); |
| 189 | + assertEquals("Content-Length test", size, |
| 190 | + ros.getResponseContentLength()); |
| 191 | + return f; |
| 192 | + } |
| 193 | + |
| 194 | + /** |
| 195 | + * Record a file byte array write. |
| 196 | + * |
| 197 | + * Write a file w/ characters that start at null and ascend to |
| 198 | + * <code>filesize</code>. Record the writing w/ passed <code>ros</code> |
| 199 | + * recordingoutputstream. Return the file recorded as result of method. |
| 200 | + * The file output stream that is recorded is named |
| 201 | + * <code>basename</code> + ".txt". |
| 202 | + * |
| 203 | + * <p>This method writes using a byte array. |
| 204 | + * |
| 205 | + * @param ros RecordingOutputStream to record with. |
| 206 | + * @param basename Basename of file. |
| 207 | + * @param size How many characters to write. |
| 208 | + * @return Recorded output stream. |
| 209 | + */ |
| 210 | + private File writeByteRecordedFile(RecordingOutputStream ros, |
| 211 | + String basename, int size) |
| 212 | + throws IOException |
| 213 | + { |
| 214 | + File f = new File(getTmpDir(), basename + ".txt"); |
| 215 | + FileOutputStream fos = new FileOutputStream(f); |
| 216 | + ros.open(fos); |
| 217 | + byte [] b = new byte[size]; |
| 218 | + for (int i = 0; i < size; i++) |
| 219 | + { |
| 220 | + b[i] = (byte)i; |
| 221 | + } |
| 222 | + ros.write(b); |
| 223 | + ros.close(); |
| 224 | + fos.close(); |
| 225 | + assertEquals("Content-Length test", size, |
| 226 | + ros.getResponseContentLength()); |
| 227 | + return f; |
| 228 | + } |
| 229 | + |
| 230 | + /** |
| 231 | + * Verify what was written is both in the file written to and in the |
| 232 | + * recording stream. |
| 233 | + * |
| 234 | + * @param ros Stream to check. |
| 235 | + * @param f File that was recorded. Stream should have its content |
| 236 | + * exactly. |
| 237 | + * @param size Amount of bytes written. |
| 238 | + * |
| 239 | + * @exception IOException Failure reading streams. |
| 240 | + */ |
| 241 | + private void verifyRecording(RecordingOutputStream ros, File f, |
| 242 | + int size) throws IOException |
| 243 | + { |
| 244 | + assertEquals("Recorded file size.", size, f.length()); |
| 245 | + FileInputStream fis = new FileInputStream(f); |
| 246 | + assertNotNull("FileInputStream not null", fis); |
| 247 | + ReplayInputStream ris = ros.getReplayInputStream(); |
| 248 | + assertNotNull("ReplayInputStream not null", ris); |
| 249 | + for (int i = 0; i < size; i++) |
| 250 | + { |
| 251 | + assertEquals("ReplayInputStream content verification", i, |
| 252 | + ris.read()); |
| 253 | + assertEquals("Recorded file content verification", i, |
| 254 | + fis.read()); |
| 255 | + } |
| 256 | + assertEquals("ReplayInputStream at EOF", -1, ris.read()); |
| 257 | + fis.close(); |
| 258 | + ris.close(); |
| 259 | + } |
| 260 | +} |
0 commit comments