KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/cvs/jakarta-commons/fileupload/src/test/org/apache/commons/fileupload/DefaultFileItemTest.java,v 1.1 2003/04/27 17:30:06 martinc Exp $
3  * $Revision: 1.1 $
4  * $Date: 2003/04/27 17:30:06 $
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
66 import java.io.File JavaDoc;
67 import java.io.IOException JavaDoc;
68 import java.io.OutputStream JavaDoc;
69 import java.util.Arrays JavaDoc;
70
71 import junit.framework.TestCase;
72 import org.apache.commons.fileupload.DefaultFileItem;
73 import org.apache.commons.fileupload.DefaultFileItemFactory;
74
75
76 /**
77  * Unit tests for {@link org.apache.commons.fileupload.DefaultFileItem}.
78  *
79  * @author <a HREF="mailto:martinc@apache.org">Martin Cooper</a>
80  */

81 public class DefaultFileItemTest extends TestCase
82  {
83
84     /**
85      * Content type for regular form items.
86      */

87     private static final String JavaDoc textContentType = "text/plain";
88
89     /**
90      * Content type for file uploads.
91      */

92     private static final String JavaDoc fileContentType = "application/octet-stream";
93
94     /**
95      * Very low threshold for testing memory vs. disk options.
96      */

97     private static final int threshold = 16;
98
99     /**
100      * Standard JUnit test case constructor.
101      *
102      * @param name The name of the test case.
103      */

104     public DefaultFileItemTest(String JavaDoc name)
105     {
106         super(name);
107     }
108
109     /**
110      * Test construction of a regular text field.
111      */

112     public void testTextFieldConstruction()
113     {
114         FileItemFactory factory = createFactory(null);
115         String JavaDoc textFieldName = "textField";
116
117         FileItem item = factory.createItem(
118                 textFieldName,
119                 textContentType,
120                 true,
121                 null
122         );
123         assertNotNull(item);
124         assertEquals(item.getFieldName(), textFieldName);
125         assertEquals(item.getContentType(), textContentType);
126         assertTrue(item.isFormField());
127         assertNull(item.getName());
128     }
129
130     /**
131      * Test construction of a file field.
132      */

133     public void testFileFieldConstruction()
134     {
135         FileItemFactory factory = createFactory(null);
136         String JavaDoc fileFieldName = "fileField";
137         String JavaDoc fileName = "originalFileName";
138
139         FileItem item = factory.createItem(
140                 fileFieldName,
141                 fileContentType,
142                 false,
143                 fileName
144         );
145         assertNotNull(item);
146         assertEquals(item.getFieldName(), fileFieldName);
147         assertEquals(item.getContentType(), fileContentType);
148         assertFalse(item.isFormField());
149         assertEquals(item.getName(), fileName);
150     }
151
152     /**
153      * Test creation of a field for which the amount of data falls below the
154      * configured threshold.
155      */

156     public void testBelowThreshold()
157     {
158         FileItemFactory factory = createFactory(null);
159         String JavaDoc textFieldName = "textField";
160         String JavaDoc textFieldValue = "0123456789";
161         byte[] testFieldValueBytes = textFieldValue.getBytes();
162
163         FileItem item = factory.createItem(
164                 textFieldName,
165                 textContentType,
166                 true,
167                 null
168         );
169         assertNotNull(item);
170
171         try
172         {
173             OutputStream JavaDoc os = item.getOutputStream();
174             os.write(testFieldValueBytes);
175             os.close();
176         }
177         catch(IOException JavaDoc e)
178         {
179             fail("Unexpected IOException");
180         }
181         assertTrue(item.isInMemory());
182         assertEquals(item.getSize(), testFieldValueBytes.length);
183         assertTrue(Arrays.equals(item.get(), testFieldValueBytes));
184         assertEquals(item.getString(), textFieldValue);
185     }
186
187     /**
188      * Test creation of a field for which the amount of data falls above the
189      * configured threshold, where no specific repository is configured.
190      */

191     public void testAboveThresholdDefaultRepository()
192     {
193         doTestAboveThreshold(null);
194     }
195
196     /**
197      * Test creation of a field for which the amount of data falls above the
198      * configured threshold, where a specific repository is configured.
199      */

200     public void testAboveThresholdSpecifiedRepository()
201     {
202         String JavaDoc tempPath = System.getProperty("java.io.tmpdir");
203         String JavaDoc tempDirName = "testAboveThresholdSpecifiedRepository";
204         File JavaDoc tempDir = new File JavaDoc(tempPath, tempDirName);
205         tempDir.mkdir();
206         doTestAboveThreshold(tempDir);
207         assertTrue(tempDir.delete());
208     }
209
210     /**
211      * Common code for cases where the amount of data is above the configured
212      * threshold, but the ultimate destination of the data has not yet been
213      * determined.
214      *
215      * @param repository The directory within which temporary files will be
216      * created.
217      */

218     public void doTestAboveThreshold(File JavaDoc repository)
219     {
220         FileItemFactory factory = createFactory(repository);
221         String JavaDoc textFieldName = "textField";
222         String JavaDoc textFieldValue = "01234567890123456789";
223         byte[] testFieldValueBytes = textFieldValue.getBytes();
224
225         FileItem item = factory.createItem(
226                 textFieldName,
227                 textContentType,
228                 true,
229                 null
230         );
231         assertNotNull(item);
232
233         try
234         {
235             OutputStream JavaDoc os = item.getOutputStream();
236             os.write(testFieldValueBytes);
237             os.close();
238         }
239         catch(IOException JavaDoc e)
240         {
241             fail("Unexpected IOException");
242         }
243         assertFalse(item.isInMemory());
244         assertEquals(item.getSize(), testFieldValueBytes.length);
245         assertTrue(Arrays.equals(item.get(), testFieldValueBytes));
246         assertEquals(item.getString(), textFieldValue);
247
248         assertTrue(item instanceof DefaultFileItem);
249         DefaultFileItem dfi = (DefaultFileItem) item;
250         File JavaDoc storeLocation = dfi.getStoreLocation();
251         assertNotNull(storeLocation);
252         assertTrue(storeLocation.exists());
253         assertEquals(storeLocation.length(), testFieldValueBytes.length);
254
255         if (repository != null)
256         {
257             assertEquals(storeLocation.getParentFile(), repository);
258         }
259
260         item.delete();
261     }
262
263
264     /**
265      * Creates a new <code>FileItemFactory</code> and returns it, obscuring
266      * from the caller the underlying implementation of this interface.
267      *
268      * @param repository The directory within which temporary files will be
269      * created.
270      * @return the new <code>FileItemFactory</code> instance.
271      */

272     protected FileItemFactory createFactory(File JavaDoc repository)
273     {
274         return new DefaultFileItemFactory(threshold, repository);
275     }
276 }
277
Popular Tags