KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > fileupload > DeferredFileOutputStreamTest


1 /*
2  * $Header: /home/cvs/jakarta-commons/fileupload/src/test/org/apache/commons/fileupload/DeferredFileOutputStreamTest.java,v 1.2 2003/05/03 04:47:38 martinc Exp $
3  * $Revision: 1.2 $
4  * $Date: 2003/05/03 04:47:38 $
5  *
6  * ====================================================================
7  *
8  * The Apache Software License, Version 1.1
9  *
10  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
11  * reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  *
17  * 1. Redistributions of source code must retain the above copyright
18  * notice, this list of conditions and the following disclaimer.
19  *
20  * 2. Redistributions in binary form must reproduce the above copyright
21  * notice, this list of conditions and the following disclaimer in
22  * the documentation and/or other materials provided with the
23  * distribution.
24  *
25  * 3. The end-user documentation included with the redistribution, if
26  * any, must include the following acknowlegement:
27  * "This product includes software developed by the
28  * Apache Software Foundation (http://www.apache.org/)."
29  * Alternately, this acknowlegement may appear in the software itself,
30  * if and wherever such third-party acknowlegements normally appear.
31  *
32  * 4. The names "The Jakarta Project", "Commons", and "Apache Software
33  * Foundation" must not be used to endorse or promote products derived
34  * from this software without prior written permission. For written
35  * permission, please contact apache@apache.org.
36  *
37  * 5. Products derived from this software may not be called "Apache"
38  * nor may "Apache" appear in their names without prior written
39  * permission of the Apache Group.
40  *
41  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52  * SUCH DAMAGE.
53  * ====================================================================
54  *
55  * This software consists of voluntary contributions made by many
56  * individuals on behalf of the Apache Software Foundation. For more
57  * information on the Apache Software Foundation, please see
58  * <http://www.apache.org/>.
59  *
60  */

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 JavaDoc;
69 import java.io.FileInputStream JavaDoc;
70 import java.io.FileNotFoundException JavaDoc;
71 import java.io.IOException JavaDoc;
72 import java.util.Arrays JavaDoc;
73
74 /**
75  * Unit tests for the <code>DeferredFileOutputStream</code> class.
76  *
77  * @author <a HREF="mailto:martinc@apache.org">Martin Cooper</a>
78  *
79  * @version $Id: DeferredFileOutputStreamTest.java,v 1.2 2003/05/03 04:47:38 martinc Exp $
80  */

81 public class DeferredFileOutputStreamTest extends TestCase
82  {
83
84     /**
85      * The test data as a string (which is the simplest form).
86      */

87     private String JavaDoc testString = "0123456789";
88
89     /**
90      * The test data as a byte array, derived from the string.
91      */

92     private byte[] testBytes = testString.getBytes();
93
94     /**
95      * Standard JUnit test case constructor.
96      *
97      * @param name The name of the test case.
98      */

99     public DeferredFileOutputStreamTest(String JavaDoc name)
100     {
101         super(name);
102     }
103
104     /**
105      * Tests the case where the amount of data falls below the threshold, and
106      * is therefore confined to memory.
107      */

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 JavaDoc 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     /**
128      * Tests the case where the amount of data is exactly the same as the
129      * threshold. The behavior should be the same as that for the amount of
130      * data being below (i.e. not exceeding) the threshold.
131      */

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 JavaDoc 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     /**
151      * Tests the case where the amount of data exceeds the threshold, and is
152      * therefore written to disk. The actual data written to disk is verified,
153      * as is the file itself.
154      */

155     public void testAboveThreshold() {
156         File JavaDoc testFile = new File JavaDoc("testAboveThreshold.dat");
157
158         // Ensure that the test starts from a clean base.
159
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 JavaDoc e) {
169             fail("Unexpected IOException");
170         }
171         assertFalse(dfos.isInMemory());
172         assertNull(dfos.getData());
173
174         verifyResultFile(testFile);
175
176         // Ensure that the test starts from a clean base.
177
testFile.delete();
178     }
179
180     /**
181      * Tests the case where there are multiple writes beyond the threshold, to
182      * ensure that the <code>thresholdReached()</code> method is only called
183      * once, as the threshold is crossed for the first time.
184      */

185     public void testThresholdReached() {
186         File JavaDoc testFile = new File JavaDoc("testThresholdReached.dat");
187
188         // Ensure that the test starts from a clean base.
189
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 JavaDoc e) {
204             fail("Unexpected IOException");
205         }
206         assertFalse(dfos.isInMemory());
207         assertNull(dfos.getData());
208
209         verifyResultFile(testFile);
210
211         // Ensure that the test starts from a clean base.
212
testFile.delete();
213     }
214
215     /**
216      * Verifies that the specified file contains the same data as the original
217      * test data.
218      *
219      * @param testFile The file containing the test output.
220      */

221     private void verifyResultFile(File JavaDoc testFile) {
222         try
223         {
224             FileInputStream JavaDoc fis = new FileInputStream JavaDoc(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 JavaDoc e) {
238                 // Ignore an exception on close
239
}
240         }
241         catch (FileNotFoundException JavaDoc e) {
242             fail("Unexpected FileNotFoundException");
243         }
244         catch (IOException JavaDoc e) {
245             fail("Unexpected IOException");
246         }
247     }
248 }
249
Popular Tags