1 61 62 63 package org.apache.commons.fileupload; 64 65 import junit.framework.TestCase; 66 import org.apache.commons.fileupload.DeferredFileOutputStream; 67 68 import java.io.File ; 69 import java.io.FileInputStream ; 70 import java.io.FileNotFoundException ; 71 import java.io.IOException ; 72 import java.util.Arrays ; 73 74 81 public class DeferredFileOutputStreamTest extends TestCase 82 { 83 84 87 private String testString = "0123456789"; 88 89 92 private byte[] testBytes = testString.getBytes(); 93 94 99 public DeferredFileOutputStreamTest(String name) 100 { 101 super(name); 102 } 103 104 108 public void testBelowThreshold() 109 { 110 DeferredFileOutputStream dfos = 111 new DeferredFileOutputStream(testBytes.length + 42, null); 112 try 113 { 114 dfos.write(testBytes, 0, testBytes.length); 115 dfos.close(); 116 } 117 catch (IOException e) { 118 fail("Unexpected IOException"); 119 } 120 assertTrue(dfos.isInMemory()); 121 122 byte[] resultBytes = dfos.getData(); 123 assertTrue(resultBytes.length == testBytes.length); 124 assertTrue(Arrays.equals(resultBytes, testBytes)); 125 } 126 127 132 public void testAtThreshold() { 133 DeferredFileOutputStream dfos = 134 new DeferredFileOutputStream(testBytes.length, null); 135 try 136 { 137 dfos.write(testBytes, 0, testBytes.length); 138 dfos.close(); 139 } 140 catch (IOException e) { 141 fail("Unexpected IOException"); 142 } 143 assertTrue(dfos.isInMemory()); 144 145 byte[] resultBytes = dfos.getData(); 146 assertTrue(resultBytes.length == testBytes.length); 147 assertTrue(Arrays.equals(resultBytes, testBytes)); 148 } 149 150 155 public void testAboveThreshold() { 156 File testFile = new File ("testAboveThreshold.dat"); 157 158 testFile.delete(); 160 161 DeferredFileOutputStream dfos = 162 new DeferredFileOutputStream(testBytes.length - 5, testFile); 163 try 164 { 165 dfos.write(testBytes, 0, testBytes.length); 166 dfos.close(); 167 } 168 catch (IOException e) { 169 fail("Unexpected IOException"); 170 } 171 assertFalse(dfos.isInMemory()); 172 assertNull(dfos.getData()); 173 174 verifyResultFile(testFile); 175 176 testFile.delete(); 178 } 179 180 185 public void testThresholdReached() { 186 File testFile = new File ("testThresholdReached.dat"); 187 188 testFile.delete(); 190 191 DeferredFileOutputStream dfos = 192 new DeferredFileOutputStream(testBytes.length / 2, testFile); 193 int chunkSize = testBytes.length / 3; 194 195 try 196 { 197 dfos.write(testBytes, 0, chunkSize); 198 dfos.write(testBytes, chunkSize, chunkSize); 199 dfos.write(testBytes, chunkSize * 2, 200 testBytes.length - chunkSize * 2); 201 dfos.close(); 202 } 203 catch (IOException e) { 204 fail("Unexpected IOException"); 205 } 206 assertFalse(dfos.isInMemory()); 207 assertNull(dfos.getData()); 208 209 verifyResultFile(testFile); 210 211 testFile.delete(); 213 } 214 215 221 private void verifyResultFile(File testFile) { 222 try 223 { 224 FileInputStream fis = new FileInputStream (testFile); 225 assertTrue(fis.available() == testBytes.length); 226 227 byte[] resultBytes = new byte[testBytes.length]; 228 assertTrue(fis.read(resultBytes) == testBytes.length); 229 230 assertTrue(Arrays.equals(resultBytes, testBytes)); 231 assertTrue(fis.read(resultBytes) == -1); 232 233 try 234 { 235 fis.close(); 236 } 237 catch (IOException e) { 238 } 240 } 241 catch (FileNotFoundException e) { 242 fail("Unexpected FileNotFoundException"); 243 } 244 catch (IOException e) { 245 fail("Unexpected IOException"); 246 } 247 } 248 } 249 | Popular Tags |