001: // Copyright 2007 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.upload.services;
016:
017: import org.apache.commons.fileupload.FileItem;
018: import org.apache.commons.fileupload.servlet.ServletFileUpload;
019: import org.apache.tapestry.test.TapestryTestCase;
020: import org.apache.tapestry.upload.services.MultipartDecoderImpl;
021: import org.testng.annotations.Test;
022:
023: import javax.servlet.http.HttpServletRequest;
024: import java.util.ArrayList;
025: import java.util.Arrays;
026: import java.util.Collections;
027: import java.util.List;
028:
029: public class MultipartDecoderImplTest extends TapestryTestCase {
030:
031: @Test
032: public void createFileUploadGetsConfigurationFromSymbols()
033: throws Exception {
034: MultipartDecoderImpl decoder = new MultipartDecoderImpl("/tmp",
035: 888, 7777, 6666);
036:
037: replay();
038:
039: ServletFileUpload servletFileUpload = decoder
040: .createFileUpload();
041: assertNotNull(servletFileUpload);
042: verify();
043:
044: assertEquals(servletFileUpload.getFileSizeMax(), 6666);
045: assertEquals(servletFileUpload.getSizeMax(), 7777);
046: }
047:
048: @Test
049: public void processFileItemsDoesNothingWhenNullFileItems()
050: throws Exception {
051: HttpServletRequest request = mockHttpServletRequest();
052: MultipartDecoderImpl decoder = new MultipartDecoderImpl("/tmp",
053: 888, -1, -1);
054:
055: replay();
056: HttpServletRequest decodedRequest = decoder.processFileItems(
057: request, null);
058: verify();
059:
060: assertSame(request, decodedRequest);
061: }
062:
063: @Test
064: public void processFileItemsDoesNothingWhenEmptyFileItems()
065: throws Exception {
066: HttpServletRequest request = mockHttpServletRequest();
067: MultipartDecoderImpl decoder = new MultipartDecoderImpl("/tmp",
068: 888, -1, -1);
069: List<FileItem> fileItems = Collections.emptyList();
070: replay();
071:
072: HttpServletRequest decodedRequest = decoder.processFileItems(
073: request, fileItems);
074: verify();
075:
076: assertSame(request, decodedRequest);
077: }
078:
079: @Test
080: public void processFileItemsCreatesWrappedRequestAndSetsNonFileParameters()
081: throws Exception {
082: HttpServletRequest request = mockHttpServletRequest();
083: MultipartDecoderImpl decoder = new MultipartDecoderImpl("/tmp",
084: 888, -1, -1);
085: List<FileItem> fileItems = Arrays.asList(createValueItem("one",
086: "first"), createValueItem("two", "second"));
087: replay();
088:
089: HttpServletRequest decodedRequest = decoder.processFileItems(
090: request, fileItems);
091:
092: assertNotSame(decodedRequest, request);
093:
094: assertEquals(decodedRequest.getParameter("one"), "first");
095: assertEquals(decodedRequest.getParameter("two"), "second");
096:
097: verify();
098: }
099:
100: @Test
101: public void processFileItemsSetsFileParametersWithFileName()
102: throws Exception {
103: HttpServletRequest request = mockHttpServletRequest();
104: MultipartDecoderImpl decoder = new MultipartDecoderImpl("/tmp",
105: 888, -1, -1);
106: List<FileItem> fileItems = Arrays.asList(createFileItem("one",
107: "first.txt"), createFileItem("two", "second.txt"));
108: replay();
109:
110: HttpServletRequest decodedRequest = decoder.processFileItems(
111: request, fileItems);
112:
113: assertNotSame(decodedRequest, request);
114:
115: assertEquals(decodedRequest.getParameter("one"), "first.txt");
116: assertEquals(decodedRequest.getParameter("two"), "second.txt");
117:
118: verify();
119: }
120:
121: @Test
122: public void processFileItemsStoresUploadedFile() throws Exception {
123: HttpServletRequest request = mockHttpServletRequest();
124: MultipartDecoderImpl decoder = new MultipartDecoderImpl("/tmp",
125: 888, -1, -1);
126: List<FileItem> fileItems = Arrays.asList(createFileItem("one",
127: "first.txt"), createFileItem("two", "second.txt"));
128: replay();
129:
130: decoder.processFileItems(request, fileItems);
131:
132: verify();
133: assertNotNull(decoder.getFileUpload("one"));
134: assertEquals(decoder.getFileUpload("one").getFileName(),
135: "first.txt");
136: assertNotNull(decoder.getFileUpload("two"));
137: assertEquals(decoder.getFileUpload("two").getFileName(),
138: "second.txt");
139: }
140:
141: @Test
142: public void threadDidCleanupDeletesAllFileItems() throws Exception {
143: HttpServletRequest request = mockHttpServletRequest();
144: MultipartDecoderImpl decoder = new MultipartDecoderImpl("/tmp",
145: 888, -1, -1);
146: StubFileItem firstItem = new StubFileItem("one");
147: firstItem.setFormField(false);
148: StubFileItem secondItem = new StubFileItem("two");
149: secondItem.setFormField(false);
150:
151: List<FileItem> fileItems = new ArrayList<FileItem>();
152: fileItems.add(firstItem);
153: fileItems.add(secondItem);
154: replay();
155:
156: decoder.processFileItems(request, fileItems);
157:
158: assertFalse(firstItem.isDeleted());
159: assertFalse(secondItem.isDeleted());
160: decoder.threadDidCleanup();
161: assertTrue(firstItem.isDeleted());
162: assertTrue(secondItem.isDeleted());
163:
164: verify();
165: }
166:
167: private FileItem createValueItem(String name, String value) {
168: StubFileItem item = new StubFileItem();
169: item.setFieldName(name);
170: item.setValue(value);
171: item.setFormField(true);
172: return item;
173: }
174:
175: private FileItem createFileItem(String name, String fileName) {
176: StubFileItem item = new StubFileItem();
177: item.setFieldName(name);
178: item.setFileName(fileName);
179: item.setFormField(false);
180: return item;
181: }
182:
183: }
|