KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > io > output > DeferredFileOutputStreamTest


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17
18 package org.apache.commons.io.output;
19
20 import junit.framework.TestCase;
21 //import org.apache.commons.fileupload.DeferredFileOutputStream;
22

23 import java.io.File JavaDoc;
24 import java.io.FileInputStream JavaDoc;
25 import java.io.FileNotFoundException JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.util.Arrays JavaDoc;
28
29 /**
30  * Unit tests for the <code>DeferredFileOutputStream</code> class.
31  *
32  * @author <a HREF="mailto:martinc@apache.org">Martin Cooper</a>
33  *
34  * @version $Id: DeferredFileOutputStreamTest.java,v 1.2 2004/02/23 05:02:25 bayard Exp $
35  */

36 public class DeferredFileOutputStreamTest extends TestCase
37  {
38
39     /**
40      * The test data as a string (which is the simplest form).
41      */

42     private String JavaDoc testString = "0123456789";
43
44     /**
45      * The test data as a byte array, derived from the string.
46      */

47     private byte[] testBytes = testString.getBytes();
48
49     /**
50      * Standard JUnit test case constructor.
51      *
52      * @param name The name of the test case.
53      */

54     public DeferredFileOutputStreamTest(String JavaDoc name)
55     {
56         super(name);
57     }
58
59     /**
60      * Tests the case where the amount of data falls below the threshold, and
61      * is therefore confined to memory.
62      */

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

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

110     public void testAboveThreshold() {
111         File JavaDoc testFile = new File JavaDoc("testAboveThreshold.dat");
112
113         // Ensure that the test starts from a clean base.
114
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 JavaDoc e) {
124             fail("Unexpected IOException");
125         }
126         assertFalse(dfos.isInMemory());
127         assertNull(dfos.getData());
128
129         verifyResultFile(testFile);
130
131         // Ensure that the test starts from a clean base.
132
testFile.delete();
133     }
134
135     /**
136      * Tests the case where there are multiple writes beyond the threshold, to
137      * ensure that the <code>thresholdReached()</code> method is only called
138      * once, as the threshold is crossed for the first time.
139      */

140     public void testThresholdReached() {
141         File JavaDoc testFile = new File JavaDoc("testThresholdReached.dat");
142
143         // Ensure that the test starts from a clean base.
144
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 JavaDoc e) {
159             fail("Unexpected IOException");
160         }
161         assertFalse(dfos.isInMemory());
162         assertNull(dfos.getData());
163
164         verifyResultFile(testFile);
165
166         // Ensure that the test starts from a clean base.
167
testFile.delete();
168     }
169
170     /**
171      * Verifies that the specified file contains the same data as the original
172      * test data.
173      *
174      * @param testFile The file containing the test output.
175      */

176     private void verifyResultFile(File JavaDoc testFile) {
177         try
178         {
179             FileInputStream JavaDoc fis = new FileInputStream JavaDoc(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 JavaDoc e) {
193                 // Ignore an exception on close
194
}
195         }
196         catch (FileNotFoundException JavaDoc e) {
197             fail("Unexpected FileNotFoundException");
198         }
199         catch (IOException JavaDoc e) {
200             fail("Unexpected IOException");
201         }
202     }
203 }
204
Popular Tags