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.fileupload;
018:
019: import java.io.File;
020: import java.io.IOException;
021: import java.io.OutputStream;
022: import java.util.Arrays;
023:
024: import junit.framework.TestCase;
025: import org.apache.commons.fileupload.DefaultFileItem;
026: import org.apache.commons.fileupload.DefaultFileItemFactory;
027:
028: /**
029: * Unit tests for {@link org.apache.commons.fileupload.DefaultFileItem}.
030: *
031: * @author <a href="mailto:martinc@apache.org">Martin Cooper</a>
032: */
033: public class DefaultFileItemTest extends TestCase {
034:
035: /**
036: * Content type for regular form items.
037: */
038: private static final String textContentType = "text/plain";
039:
040: /**
041: * Content type for file uploads.
042: */
043: private static final String fileContentType = "application/octet-stream";
044:
045: /**
046: * Very low threshold for testing memory versus disk options.
047: */
048: private static final int threshold = 16;
049:
050: /**
051: * Standard JUnit test case constructor.
052: *
053: * @param name The name of the test case.
054: */
055: public DefaultFileItemTest(String name) {
056: super (name);
057: }
058:
059: /**
060: * Test construction of a regular text field.
061: */
062: public void testTextFieldConstruction() {
063: FileItemFactory factory = createFactory(null);
064: String textFieldName = "textField";
065:
066: FileItem item = factory.createItem(textFieldName,
067: textContentType, true, null);
068: assertNotNull(item);
069: assertEquals(item.getFieldName(), textFieldName);
070: assertEquals(item.getContentType(), textContentType);
071: assertTrue(item.isFormField());
072: assertNull(item.getName());
073: }
074:
075: /**
076: * Test construction of a file field.
077: */
078: public void testFileFieldConstruction() {
079: FileItemFactory factory = createFactory(null);
080: String fileFieldName = "fileField";
081: String fileName = "originalFileName";
082:
083: FileItem item = factory.createItem(fileFieldName,
084: fileContentType, false, fileName);
085: assertNotNull(item);
086: assertEquals(item.getFieldName(), fileFieldName);
087: assertEquals(item.getContentType(), fileContentType);
088: assertFalse(item.isFormField());
089: assertEquals(item.getName(), fileName);
090: }
091:
092: /**
093: * Test creation of a field for which the amount of data falls below the
094: * configured threshold.
095: */
096: public void testBelowThreshold() {
097: FileItemFactory factory = createFactory(null);
098: String textFieldName = "textField";
099: String textFieldValue = "0123456789";
100: byte[] testFieldValueBytes = textFieldValue.getBytes();
101:
102: FileItem item = factory.createItem(textFieldName,
103: textContentType, true, null);
104: assertNotNull(item);
105:
106: try {
107: OutputStream os = item.getOutputStream();
108: os.write(testFieldValueBytes);
109: os.close();
110: } catch (IOException e) {
111: fail("Unexpected IOException");
112: }
113: assertTrue(item.isInMemory());
114: assertEquals(item.getSize(), testFieldValueBytes.length);
115: assertTrue(Arrays.equals(item.get(), testFieldValueBytes));
116: assertEquals(item.getString(), textFieldValue);
117: }
118:
119: /**
120: * Test creation of a field for which the amount of data falls above the
121: * configured threshold, where no specific repository is configured.
122: */
123: public void testAboveThresholdDefaultRepository() {
124: doTestAboveThreshold(null);
125: }
126:
127: /**
128: * Test creation of a field for which the amount of data falls above the
129: * configured threshold, where a specific repository is configured.
130: */
131: public void testAboveThresholdSpecifiedRepository() {
132: String tempPath = System.getProperty("java.io.tmpdir");
133: String tempDirName = "testAboveThresholdSpecifiedRepository";
134: File tempDir = new File(tempPath, tempDirName);
135: tempDir.mkdir();
136: doTestAboveThreshold(tempDir);
137: assertTrue(tempDir.delete());
138: }
139:
140: /**
141: * Common code for cases where the amount of data is above the configured
142: * threshold, but the ultimate destination of the data has not yet been
143: * determined.
144: *
145: * @param repository The directory within which temporary files will be
146: * created.
147: */
148: public void doTestAboveThreshold(File repository) {
149: FileItemFactory factory = createFactory(repository);
150: String textFieldName = "textField";
151: String textFieldValue = "01234567890123456789";
152: byte[] testFieldValueBytes = textFieldValue.getBytes();
153:
154: FileItem item = factory.createItem(textFieldName,
155: textContentType, true, null);
156: assertNotNull(item);
157:
158: try {
159: OutputStream os = item.getOutputStream();
160: os.write(testFieldValueBytes);
161: os.close();
162: } catch (IOException e) {
163: fail("Unexpected IOException");
164: }
165: assertFalse(item.isInMemory());
166: assertEquals(item.getSize(), testFieldValueBytes.length);
167: assertTrue(Arrays.equals(item.get(), testFieldValueBytes));
168: assertEquals(item.getString(), textFieldValue);
169:
170: assertTrue(item instanceof DefaultFileItem);
171: DefaultFileItem dfi = (DefaultFileItem) item;
172: File storeLocation = dfi.getStoreLocation();
173: assertNotNull(storeLocation);
174: assertTrue(storeLocation.exists());
175: assertEquals(storeLocation.length(), testFieldValueBytes.length);
176:
177: if (repository != null) {
178: assertEquals(storeLocation.getParentFile(), repository);
179: }
180:
181: item.delete();
182: }
183:
184: /**
185: * Creates a new <code>FileItemFactory</code> and returns it, obscuring
186: * from the caller the underlying implementation of this interface.
187: *
188: * @param repository The directory within which temporary files will be
189: * created.
190: * @return the new <code>FileItemFactory</code> instance.
191: */
192: protected FileItemFactory createFactory(File repository) {
193: return new DefaultFileItemFactory(threshold, repository);
194: }
195:
196: static final String CHARSET_ISO88591 = "ISO-8859-1";
197: static final String CHARSET_ASCII = "US-ASCII";
198: static final String CHARSET_UTF8 = "UTF-8";
199: static final String CHARSET_KOI8_R = "KOI8_R";
200: static final String CHARSET_WIN1251 = "Cp1251";
201:
202: static final int SWISS_GERMAN_STUFF_UNICODE[] = { 0x47, 0x72, 0xFC,
203: 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4 };
204:
205: static final int SWISS_GERMAN_STUFF_ISO8859_1[] = { 0x47, 0x72,
206: 0xFC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4 };
207:
208: static final int SWISS_GERMAN_STUFF_UTF8[] = { 0x47, 0x72, 0xC3,
209: 0xBC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xC3, 0xA4, 0x6D, 0xC3,
210: 0xA4 };
211:
212: static final int RUSSIAN_STUFF_UNICODE[] = { 0x412, 0x441, 0x435,
213: 0x43C, 0x5F, 0x43F, 0x440, 0x438, 0x432, 0x435, 0x442 };
214:
215: static final int RUSSIAN_STUFF_UTF8[] = { 0xD0, 0x92, 0xD1, 0x81,
216: 0xD0, 0xB5, 0xD0, 0xBC, 0x5F, 0xD0, 0xBF, 0xD1, 0x80, 0xD0,
217: 0xB8, 0xD0, 0xB2, 0xD0, 0xB5, 0xD1, 0x82 };
218:
219: static final int RUSSIAN_STUFF_KOI8R[] = { 0xF7, 0xD3, 0xC5, 0xCD,
220: 0x5F, 0xD0, 0xD2, 0xC9, 0xD7, 0xC5, 0xD4 };
221:
222: static final int RUSSIAN_STUFF_WIN1251[] = { 0xC2, 0xF1, 0xE5,
223: 0xEC, 0x5F, 0xEF, 0xF0, 0xE8, 0xE2, 0xE5, 0xF2 };
224:
225: private static String constructString(int[] unicodeChars) {
226: StringBuffer buffer = new StringBuffer();
227: if (unicodeChars != null) {
228: for (int i = 0; i < unicodeChars.length; i++) {
229: buffer.append((char) unicodeChars[i]);
230: }
231: }
232: return buffer.toString();
233: }
234:
235: /**
236: * Test construction of content charset.
237: */
238: public void testContentCharSet() throws Exception {
239: FileItemFactory factory = createFactory(null);
240:
241: String teststr = constructString(SWISS_GERMAN_STUFF_UNICODE);
242:
243: FileItem item = factory.createItem("doesnotmatter",
244: "text/plain; charset=" + CHARSET_ISO88591, true, null);
245: OutputStream outstream = item.getOutputStream();
246: for (int i = 0; i < SWISS_GERMAN_STUFF_ISO8859_1.length; i++) {
247: outstream.write(SWISS_GERMAN_STUFF_ISO8859_1[i]);
248: }
249: outstream.close();
250: assertEquals(teststr, teststr, item.getString());
251:
252: item = factory.createItem("doesnotmatter",
253: "text/plain; charset=" + CHARSET_UTF8, true, null);
254: outstream = item.getOutputStream();
255: for (int i = 0; i < SWISS_GERMAN_STUFF_UTF8.length; i++) {
256: outstream.write(SWISS_GERMAN_STUFF_UTF8[i]);
257: }
258: outstream.close();
259: assertEquals(teststr, teststr, item.getString());
260:
261: teststr = constructString(RUSSIAN_STUFF_UNICODE);
262:
263: item = factory.createItem("doesnotmatter",
264: "text/plain; charset=" + CHARSET_KOI8_R, true, null);
265: outstream = item.getOutputStream();
266: for (int i = 0; i < RUSSIAN_STUFF_KOI8R.length; i++) {
267: outstream.write(RUSSIAN_STUFF_KOI8R[i]);
268: }
269: outstream.close();
270: assertEquals(teststr, teststr, item.getString());
271:
272: item = factory.createItem("doesnotmatter",
273: "text/plain; charset=" + CHARSET_WIN1251, true, null);
274: outstream = item.getOutputStream();
275: for (int i = 0; i < RUSSIAN_STUFF_WIN1251.length; i++) {
276: outstream.write(RUSSIAN_STUFF_WIN1251[i]);
277: }
278: outstream.close();
279: assertEquals(teststr, teststr, item.getString());
280:
281: item = factory.createItem("doesnotmatter",
282: "text/plain; charset=" + CHARSET_UTF8, true, null);
283: outstream = item.getOutputStream();
284: for (int i = 0; i < RUSSIAN_STUFF_UTF8.length; i++) {
285: outstream.write(RUSSIAN_STUFF_UTF8[i]);
286: }
287: outstream.close();
288: assertEquals(teststr, teststr, item.getString());
289: }
290: }
|