1 25 package org.archive.io; 26 27 import java.io.File ; 28 import java.io.FileInputStream ; 29 import java.io.FileOutputStream ; 30 import java.io.IOException ; 31 32 import org.archive.util.TmpDirTestCase; 33 34 35 40 public class RecordingOutputStreamTest extends TmpDirTestCase 41 { 42 45 private static final int BUFFER_SIZE = 5; 46 47 50 private static final int WRITE_TOTAL = 10; 51 52 53 56 protected void setUp() throws Exception 57 { 58 super.setUp(); 59 } 60 61 67 public void testReuse() 68 throws IOException 69 { 70 final String BASENAME = "testReuse"; 71 cleanUpOldFiles(BASENAME); 72 RecordingOutputStream ros = new RecordingOutputStream(BUFFER_SIZE, 73 (new File (getTmpDir(), BASENAME + "Bkg.txt")).getAbsolutePath()); 74 for (int i = 0; i < 3; i++) 75 { 76 reuse(BASENAME, ros, i); 77 } 78 } 79 80 private void reuse(String baseName, RecordingOutputStream ros, int index) 81 throws IOException 82 { 83 final String BASENAME = baseName + Integer.toString(index); 84 File f = writeIntRecordedFile(ros, BASENAME, WRITE_TOTAL); 85 verifyRecording(ros, f, WRITE_TOTAL); 86 verifyRecording(ros, f, WRITE_TOTAL); 89 } 90 91 99 public void testWriteint() 100 throws IOException 101 { 102 final String BASENAME = "testWriteint"; 103 cleanUpOldFiles(BASENAME); 104 RecordingOutputStream ros = new RecordingOutputStream(BUFFER_SIZE, 105 (new File (getTmpDir(), BASENAME + "Backing.txt")).getAbsolutePath()); 106 File f = writeIntRecordedFile(ros, BASENAME, WRITE_TOTAL); 107 verifyRecording(ros, f, WRITE_TOTAL); 108 verifyRecording(ros, f, WRITE_TOTAL); 111 } 112 113 121 public void testWritebytearray() 122 throws IOException 123 { 124 final String BASENAME = "testWritebytearray"; 125 cleanUpOldFiles(BASENAME); 126 RecordingOutputStream ros = new RecordingOutputStream(BUFFER_SIZE, 127 (new File (getTmpDir(), BASENAME + "Backing.txt")).getAbsolutePath()); 128 File f = writeByteRecordedFile(ros, BASENAME, WRITE_TOTAL); 129 verifyRecording(ros, f, WRITE_TOTAL); 130 verifyRecording(ros, f, WRITE_TOTAL); 133 } 134 135 139 public void testMarkReset() throws IOException 140 { 141 final String BASENAME = "testMarkReset"; 142 cleanUpOldFiles(BASENAME); 143 RecordingOutputStream ros = new RecordingOutputStream(BUFFER_SIZE, 144 (new File (getTmpDir(), BASENAME + "Backing.txt")).getAbsolutePath()); 145 File f = writeByteRecordedFile(ros, BASENAME, WRITE_TOTAL); 146 verifyRecording(ros, f, WRITE_TOTAL); 147 ReplayInputStream ris = ros.getReplayInputStream(); 148 ris.mark(10 ); 149 ris.read(); 151 ris.read(); 152 ris.read(); 153 ris.reset(); 155 assertEquals("Reset to zero", ris.read(), 0); 156 assertEquals("Reset to zero char 1", ris.read(), 1); 157 assertEquals("Reset to zero char 2", ris.read(), 2); 158 ris.mark(10 ); 160 ris.read(); 161 ris.read(); 162 ris.reset(); 163 assertEquals("Reset to zero char 3", ris.read(), 3); 164 } 165 166 182 private File writeIntRecordedFile(RecordingOutputStream ros, 183 String basename, int size) 184 throws IOException 185 { 186 File f = new File (getTmpDir(), basename + ".txt"); 187 FileOutputStream fos = new FileOutputStream (f); 188 ros.open(fos); 189 for (int i = 0; i < WRITE_TOTAL; i++) 190 { 191 ros.write(i); 192 } 193 ros.close(); 194 fos.close(); 195 assertEquals("Content-Length test", size, 196 ros.getResponseContentLength()); 197 return f; 198 } 199 200 216 private File writeByteRecordedFile(RecordingOutputStream ros, 217 String basename, int size) 218 throws IOException 219 { 220 File f = new File (getTmpDir(), basename + ".txt"); 221 FileOutputStream fos = new FileOutputStream (f); 222 ros.open(fos); 223 byte [] b = new byte[size]; 224 for (int i = 0; i < size; i++) 225 { 226 b[i] = (byte)i; 227 } 228 ros.write(b); 229 ros.close(); 230 fos.close(); 231 assertEquals("Content-Length test", size, 232 ros.getResponseContentLength()); 233 return f; 234 } 235 236 247 private void verifyRecording(RecordingOutputStream ros, File f, 248 int size) throws IOException 249 { 250 assertEquals("Recorded file size.", size, f.length()); 251 FileInputStream fis = new FileInputStream (f); 252 assertNotNull("FileInputStream not null", fis); 253 ReplayInputStream ris = ros.getReplayInputStream(); 254 assertNotNull("ReplayInputStream not null", ris); 255 for (int i = 0; i < size; i++) 256 { 257 assertEquals("ReplayInputStream content verification", i, 258 ris.read()); 259 assertEquals("Recorded file content verification", i, 260 fis.read()); 261 } 262 assertEquals("ReplayInputStream at EOF", -1, ris.read()); 263 fis.close(); 264 ris.close(); 265 } 266 } 267 | Popular Tags |