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.io.ByteArrayInputStream;
023: import java.io.ByteArrayOutputStream;
024: import java.io.ObjectInputStream;
025: import java.io.ObjectOutputStream;
026: import java.util.Arrays;
027:
028: import junit.framework.TestCase;
029: import org.apache.commons.fileupload.disk.DiskFileItem;
030: import org.apache.commons.fileupload.disk.DiskFileItemFactory;
031:
032: /**
033: * Serialization Unit tests for
034: * {@link org.apache.commons.fileupload.disk.DiskFileItem}.
035: */
036: public class DiskFileItemSerializeTest extends TestCase {
037:
038: /**
039: * Content type for regular form items.
040: */
041: private static final String textContentType = "text/plain";
042:
043: /**
044: * Content type for file uploads.
045: */
046: private static final String fileContentType = "application/octet-stream";
047:
048: /**
049: * Very low threshold for testing memory versus disk options.
050: */
051: private static final int threshold = 16;
052:
053: /**
054: * Standard JUnit test case constructor.
055: *
056: * @param name The name of the test case.
057: */
058: public DiskFileItemSerializeTest(String name) {
059: super (name);
060: }
061:
062: /**
063: * Test creation of a field for which the amount of data falls below the
064: * configured threshold.
065: */
066: public void testBelowThreshold() {
067:
068: // Create the FileItem
069: byte[] testFieldValueBytes = createContentBytes(threshold - 1);
070: FileItem item = createFileItem(testFieldValueBytes);
071:
072: // Check state is as expected
073: assertTrue("Initial: in memory", item.isInMemory());
074: assertEquals("Initial: size", item.getSize(),
075: testFieldValueBytes.length);
076: compareBytes("Initial", item.get(), testFieldValueBytes);
077:
078: // Serialize & Deserialize
079: try {
080: FileItem newItem = (FileItem) serializeDeserialize(item);
081:
082: // Test deserialized content is as expected
083: assertTrue("Check in memory", newItem.isInMemory());
084: compareBytes("Check", testFieldValueBytes, newItem.get());
085:
086: // Compare FileItem's (except byte[])
087: compareFileItems(item, newItem);
088:
089: } catch (Exception e) {
090: fail("Error Serializing/Deserializing: " + e);
091: }
092:
093: }
094:
095: /**
096: * Test creation of a field for which the amount of data equals the
097: * configured threshold.
098: */
099: public void testThreshold() {
100: // Create the FileItem
101: byte[] testFieldValueBytes = createContentBytes(threshold);
102: FileItem item = createFileItem(testFieldValueBytes);
103:
104: // Check state is as expected
105: assertTrue("Initial: in memory", item.isInMemory());
106: assertEquals("Initial: size", item.getSize(),
107: testFieldValueBytes.length);
108: compareBytes("Initial", item.get(), testFieldValueBytes);
109:
110: // Serialize & Deserialize
111: try {
112: FileItem newItem = (FileItem) serializeDeserialize(item);
113:
114: // Test deserialized content is as expected
115: assertTrue("Check in memory", newItem.isInMemory());
116: compareBytes("Check", testFieldValueBytes, newItem.get());
117:
118: // Compare FileItem's (except byte[])
119: compareFileItems(item, newItem);
120:
121: } catch (Exception e) {
122: fail("Error Serializing/Deserializing: " + e);
123: }
124: }
125:
126: /**
127: * Test creation of a field for which the amount of data falls above the
128: * configured threshold.
129: */
130: public void testAboveThreshold() {
131:
132: // Create the FileItem
133: byte[] testFieldValueBytes = createContentBytes(threshold + 1);
134: FileItem item = createFileItem(testFieldValueBytes);
135:
136: // Check state is as expected
137: assertFalse("Initial: in memory", item.isInMemory());
138: assertEquals("Initial: size", item.getSize(),
139: testFieldValueBytes.length);
140: compareBytes("Initial", item.get(), testFieldValueBytes);
141:
142: // Serialize & Deserialize
143: try {
144: FileItem newItem = (FileItem) serializeDeserialize(item);
145:
146: // Test deserialized content is as expected
147: assertFalse("Check in memory", newItem.isInMemory());
148: compareBytes("Check", testFieldValueBytes, newItem.get());
149:
150: // Compare FileItem's (except byte[])
151: compareFileItems(item, newItem);
152:
153: } catch (Exception e) {
154: fail("Error Serializing/Deserializing: " + e);
155: }
156: }
157:
158: /**
159: * Compare FileItem's (except the byte[] content)
160: */
161: private void compareFileItems(FileItem origItem, FileItem newItem) {
162: assertTrue("Compare: is in Memory",
163: origItem.isInMemory() == newItem.isInMemory());
164: assertTrue("Compare: is Form Field",
165: origItem.isFormField() == newItem.isFormField());
166: assertEquals("Compare: Field Name", origItem.getFieldName(),
167: newItem.getFieldName());
168: assertEquals("Compare: Content Type",
169: origItem.getContentType(), newItem.getContentType());
170: assertEquals("Compare: File Name", origItem.getName(), newItem
171: .getName());
172: }
173:
174: /**
175: * Compare content bytes.
176: */
177: private void compareBytes(String text, byte[] origBytes,
178: byte[] newBytes) {
179: if (origBytes == null) {
180: fail(text + " origBytes are null");
181: }
182: if (newBytes == null) {
183: fail(text + " newBytes are null");
184: }
185: assertEquals(text + " byte[] length", origBytes.length,
186: newBytes.length);
187: for (int i = 0; i < origBytes.length; i++) {
188: assertEquals(text + " byte[" + i + "]", origBytes[i],
189: newBytes[i]);
190: }
191: }
192:
193: /**
194: * Create content bytes of a specified size.
195: */
196: private byte[] createContentBytes(int size) {
197: StringBuffer buffer = new StringBuffer(size);
198: byte count = 0;
199: for (int i = 0; i < size; i++) {
200: buffer.append(count + "");
201: count++;
202: if (count > 9) {
203: count = 0;
204: }
205: }
206: return buffer.toString().getBytes();
207: }
208:
209: /**
210: * Create a FileItem with the specfied content bytes.
211: */
212: private FileItem createFileItem(byte[] contentBytes) {
213: FileItemFactory factory = new DiskFileItemFactory(threshold,
214: null);
215: String textFieldName = "textField";
216:
217: FileItem item = factory.createItem(textFieldName,
218: textContentType, true, "My File Name");
219: try {
220: OutputStream os = item.getOutputStream();
221: os.write(contentBytes);
222: os.close();
223: } catch (IOException e) {
224: fail("Unexpected IOException" + e);
225: }
226:
227: return item;
228:
229: }
230:
231: /**
232: * Do serialization and deserialization.
233: */
234: private Object serializeDeserialize(Object target) {
235:
236: // Serialize the test object
237: ByteArrayOutputStream baos = new ByteArrayOutputStream();
238: try {
239: ObjectOutputStream oos = new ObjectOutputStream(baos);
240: oos.writeObject(target);
241: oos.flush();
242: oos.close();
243: } catch (Exception e) {
244: fail("Exception during serialization: " + e);
245: }
246:
247: // Deserialize the test object
248: Object result = null;
249: try {
250: ByteArrayInputStream bais = new ByteArrayInputStream(baos
251: .toByteArray());
252: ObjectInputStream ois = new ObjectInputStream(bais);
253: result = ois.readObject();
254: bais.close();
255: } catch (Exception e) {
256: fail("Exception during deserialization: " + e);
257: }
258: return result;
259:
260: }
261:
262: }
|