1 16 17 18 package org.apache.commons.io.output; 19 20 import junit.framework.TestCase; 21 23 import java.io.File ; 24 import java.io.FileInputStream ; 25 import java.io.FileNotFoundException ; 26 import java.io.IOException ; 27 import java.util.Arrays ; 28 29 36 public class DeferredFileOutputStreamTest extends TestCase 37 { 38 39 42 private String testString = "0123456789"; 43 44 47 private byte[] testBytes = testString.getBytes(); 48 49 54 public DeferredFileOutputStreamTest(String name) 55 { 56 super(name); 57 } 58 59 63 public void testBelowThreshold() 64 { 65 DeferredFileOutputStream dfos = 66 new DeferredFileOutputStream(testBytes.length + 42, null); 67 try 68 { 69 dfos.write(testBytes, 0, testBytes.length); 70 dfos.close(); 71 } 72 catch (IOException e) { 73 fail("Unexpected IOException"); 74 } 75 assertTrue(dfos.isInMemory()); 76 77 byte[] resultBytes = dfos.getData(); 78 assertTrue(resultBytes.length == testBytes.length); 79 assertTrue(Arrays.equals(resultBytes, testBytes)); 80 } 81 82 87 public void testAtThreshold() { 88 DeferredFileOutputStream dfos = 89 new DeferredFileOutputStream(testBytes.length, null); 90 try 91 { 92 dfos.write(testBytes, 0, testBytes.length); 93 dfos.close(); 94 } 95 catch (IOException e) { 96 fail("Unexpected IOException"); 97 } 98 assertTrue(dfos.isInMemory()); 99 100 byte[] resultBytes = dfos.getData(); 101 assertTrue(resultBytes.length == testBytes.length); 102 assertTrue(Arrays.equals(resultBytes, testBytes)); 103 } 104 105 110 public void testAboveThreshold() { 111 File testFile = new File ("testAboveThreshold.dat"); 112 113 testFile.delete(); 115 116 DeferredFileOutputStream dfos = 117 new DeferredFileOutputStream(testBytes.length - 5, testFile); 118 try 119 { 120 dfos.write(testBytes, 0, testBytes.length); 121 dfos.close(); 122 } 123 catch (IOException e) { 124 fail("Unexpected IOException"); 125 } 126 assertFalse(dfos.isInMemory()); 127 assertNull(dfos.getData()); 128 129 verifyResultFile(testFile); 130 131 testFile.delete(); 133 } 134 135 140 public void testThresholdReached() { 141 File testFile = new File ("testThresholdReached.dat"); 142 143 testFile.delete(); 145 146 DeferredFileOutputStream dfos = 147 new DeferredFileOutputStream(testBytes.length / 2, testFile); 148 int chunkSize = testBytes.length / 3; 149 150 try 151 { 152 dfos.write(testBytes, 0, chunkSize); 153 dfos.write(testBytes, chunkSize, chunkSize); 154 dfos.write(testBytes, chunkSize * 2, 155 testBytes.length - chunkSize * 2); 156 dfos.close(); 157 } 158 catch (IOException e) { 159 fail("Unexpected IOException"); 160 } 161 assertFalse(dfos.isInMemory()); 162 assertNull(dfos.getData()); 163 164 verifyResultFile(testFile); 165 166 testFile.delete(); 168 } 169 170 176 private void verifyResultFile(File testFile) { 177 try 178 { 179 FileInputStream fis = new FileInputStream (testFile); 180 assertTrue(fis.available() == testBytes.length); 181 182 byte[] resultBytes = new byte[testBytes.length]; 183 assertTrue(fis.read(resultBytes) == testBytes.length); 184 185 assertTrue(Arrays.equals(resultBytes, testBytes)); 186 assertTrue(fis.read(resultBytes) == -1); 187 188 try 189 { 190 fis.close(); 191 } 192 catch (IOException e) { 193 } 195 } 196 catch (FileNotFoundException e) { 197 fail("Unexpected FileNotFoundException"); 198 } 199 catch (IOException e) { 200 fail("Unexpected IOException"); 201 } 202 } 203 } 204 | Popular Tags |