001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.io.output;
018:
019: import junit.framework.TestCase; //import org.apache.commons.fileupload.DeferredFileOutputStream;
020:
021: import java.io.File;
022: import java.io.FileInputStream;
023: import java.io.FileNotFoundException;
024: import java.io.IOException;
025: import java.util.Arrays;
026:
027: /**
028: * Unit tests for the <code>DeferredFileOutputStream</code> class.
029: *
030: * @author <a href="mailto:martinc@apache.org">Martin Cooper</a>
031: *
032: * @version $Id: DeferredFileOutputStreamTest.java 437680 2006-08-28 11:57:00Z scolebourne $
033: */
034: public class DeferredFileOutputStreamTest extends TestCase {
035:
036: /**
037: * The test data as a string (which is the simplest form).
038: */
039: private String testString = "0123456789";
040:
041: /**
042: * The test data as a byte array, derived from the string.
043: */
044: private byte[] testBytes = testString.getBytes();
045:
046: /**
047: * Standard JUnit test case constructor.
048: *
049: * @param name The name of the test case.
050: */
051: public DeferredFileOutputStreamTest(String name) {
052: super (name);
053: }
054:
055: /**
056: * Tests the case where the amount of data falls below the threshold, and
057: * is therefore confined to memory.
058: */
059: public void testBelowThreshold() {
060: DeferredFileOutputStream dfos = new DeferredFileOutputStream(
061: testBytes.length + 42, null);
062: try {
063: dfos.write(testBytes, 0, testBytes.length);
064: dfos.close();
065: } catch (IOException e) {
066: fail("Unexpected IOException");
067: }
068: assertTrue(dfos.isInMemory());
069:
070: byte[] resultBytes = dfos.getData();
071: assertTrue(resultBytes.length == testBytes.length);
072: assertTrue(Arrays.equals(resultBytes, testBytes));
073: }
074:
075: /**
076: * Tests the case where the amount of data is exactly the same as the
077: * threshold. The behavior should be the same as that for the amount of
078: * data being below (i.e. not exceeding) the threshold.
079: */
080: public void testAtThreshold() {
081: DeferredFileOutputStream dfos = new DeferredFileOutputStream(
082: testBytes.length, null);
083: try {
084: dfos.write(testBytes, 0, testBytes.length);
085: dfos.close();
086: } catch (IOException e) {
087: fail("Unexpected IOException");
088: }
089: assertTrue(dfos.isInMemory());
090:
091: byte[] resultBytes = dfos.getData();
092: assertTrue(resultBytes.length == testBytes.length);
093: assertTrue(Arrays.equals(resultBytes, testBytes));
094: }
095:
096: /**
097: * Tests the case where the amount of data exceeds the threshold, and is
098: * therefore written to disk. The actual data written to disk is verified,
099: * as is the file itself.
100: */
101: public void testAboveThreshold() {
102: File testFile = new File("testAboveThreshold.dat");
103:
104: // Ensure that the test starts from a clean base.
105: testFile.delete();
106:
107: DeferredFileOutputStream dfos = new DeferredFileOutputStream(
108: testBytes.length - 5, testFile);
109: try {
110: dfos.write(testBytes, 0, testBytes.length);
111: dfos.close();
112: } catch (IOException e) {
113: fail("Unexpected IOException");
114: }
115: assertFalse(dfos.isInMemory());
116: assertNull(dfos.getData());
117:
118: verifyResultFile(testFile);
119:
120: // Ensure that the test starts from a clean base.
121: testFile.delete();
122: }
123:
124: /**
125: * Tests the case where there are multiple writes beyond the threshold, to
126: * ensure that the <code>thresholdReached()</code> method is only called
127: * once, as the threshold is crossed for the first time.
128: */
129: public void testThresholdReached() {
130: File testFile = new File("testThresholdReached.dat");
131:
132: // Ensure that the test starts from a clean base.
133: testFile.delete();
134:
135: DeferredFileOutputStream dfos = new DeferredFileOutputStream(
136: testBytes.length / 2, testFile);
137: int chunkSize = testBytes.length / 3;
138:
139: try {
140: dfos.write(testBytes, 0, chunkSize);
141: dfos.write(testBytes, chunkSize, chunkSize);
142: dfos.write(testBytes, chunkSize * 2, testBytes.length
143: - chunkSize * 2);
144: dfos.close();
145: } catch (IOException e) {
146: fail("Unexpected IOException");
147: }
148: assertFalse(dfos.isInMemory());
149: assertNull(dfos.getData());
150:
151: verifyResultFile(testFile);
152:
153: // Ensure that the test starts from a clean base.
154: testFile.delete();
155: }
156:
157: /**
158: * Test wether writeTo() properly writes small content.
159: */
160: public void testWriteToSmall() {
161: File testFile = new File("testWriteToMem.dat");
162: ByteArrayOutputStream baos = new ByteArrayOutputStream();
163: // Ensure that the test starts from a clean base.
164: testFile.delete();
165:
166: DeferredFileOutputStream dfos = new DeferredFileOutputStream(
167: testBytes.length * 2, testFile);
168: try {
169: dfos.write(testBytes);
170:
171: assertFalse(testFile.exists());
172: assertTrue(dfos.isInMemory());
173:
174: try {
175: dfos.writeTo(baos);
176: fail("Should not have been able to write before closing");
177: } catch (IOException ioe) {
178: // ok, as expected
179: }
180:
181: dfos.close();
182: dfos.writeTo(baos);
183: } catch (IOException ioe) {
184: fail("Unexpected IOException");
185: }
186: byte[] copiedBytes = baos.toByteArray();
187: assertTrue(Arrays.equals(testBytes, copiedBytes));
188:
189: testFile.delete();
190: }
191:
192: /**
193: * Test wether writeTo() properly writes large content.
194: */
195: public void testWriteToLarge() {
196: File testFile = new File("testWriteToFile.dat");
197: ByteArrayOutputStream baos = new ByteArrayOutputStream();
198: // Ensure that the test starts from a clean base.
199: testFile.delete();
200:
201: DeferredFileOutputStream dfos = new DeferredFileOutputStream(
202: testBytes.length / 2, testFile);
203: try {
204: dfos.write(testBytes);
205:
206: assertTrue(testFile.exists());
207: assertFalse(dfos.isInMemory());
208:
209: try {
210: dfos.writeTo(baos);
211: fail("Should not have been able to write before closeing");
212: } catch (IOException ioe) {
213: // ok, as expected
214: }
215:
216: dfos.close();
217: dfos.writeTo(baos);
218: } catch (IOException ioe) {
219: fail("Unexpected IOException");
220: }
221: byte[] copiedBytes = baos.toByteArray();
222: assertTrue(Arrays.equals(testBytes, copiedBytes));
223: verifyResultFile(testFile);
224: testFile.delete();
225: }
226:
227: /**
228: * Verifies that the specified file contains the same data as the original
229: * test data.
230: *
231: * @param testFile The file containing the test output.
232: */
233: private void verifyResultFile(File testFile) {
234: try {
235: FileInputStream fis = new FileInputStream(testFile);
236: assertTrue(fis.available() == testBytes.length);
237:
238: byte[] resultBytes = new byte[testBytes.length];
239: assertTrue(fis.read(resultBytes) == testBytes.length);
240:
241: assertTrue(Arrays.equals(resultBytes, testBytes));
242: assertTrue(fis.read(resultBytes) == -1);
243:
244: try {
245: fis.close();
246: } catch (IOException e) {
247: // Ignore an exception on close
248: }
249: } catch (FileNotFoundException e) {
250: fail("Unexpected FileNotFoundException");
251: } catch (IOException e) {
252: fail("Unexpected IOException");
253: }
254: }
255: }
|