001: /*
002: * Copyright (c) 2002-2006 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.interceptor;
006:
007: import com.opensymphony.util.ClassLoaderUtil;
008: import com.opensymphony.webwork.ServletActionContext;
009: import com.opensymphony.webwork.WebWorkTestCase;
010: import com.opensymphony.webwork.dispatcher.multipart.MultiPartRequestWrapper;
011: import com.opensymphony.xwork.*;
012: import com.opensymphony.xwork.mock.MockActionInvocation;
013: import org.springframework.mock.web.MockHttpServletRequest;
014:
015: import javax.servlet.http.HttpServletRequest;
016: import java.io.File;
017: import java.io.IOException;
018: import java.net.URI;
019: import java.net.URL;
020: import java.util.HashMap;
021: import java.util.List;
022: import java.util.Locale;
023: import java.util.Map;
024:
025: /**
026: * Test case for FileUploadInterceptor.
027: *
028: * @author tmjee
029: * @author Claus Ibsen
030: * @version $Date: 2007-02-08 02:57:26 +0100 (Thu, 08 Feb 2007) $ $Id: FileUploadInterceptorTest.java 2839 2007-02-08 01:57:26Z tschneider22 $
031: */
032: public class FileUploadInterceptorTest extends WebWorkTestCase {
033:
034: private FileUploadInterceptor interceptor;
035:
036: public void testAcceptFileWithEmptyAllowedTypes() throws Exception {
037: // when allowed type is empty
038: ValidationAwareSupport validation = new ValidationAwareSupport();
039: boolean ok = interceptor.acceptFile(new File(""), "text/plain",
040: "inputName", validation, Locale.getDefault());
041:
042: assertTrue(ok);
043: assertTrue(validation.getFieldErrors().isEmpty());
044: assertFalse(validation.hasErrors());
045: }
046:
047: public void testAcceptFileWithoutEmptyTypes() throws Exception {
048: interceptor.setAllowedTypes("text/plain");
049:
050: // when file is of allowed types
051: ValidationAwareSupport validation = new ValidationAwareSupport();
052: boolean ok = interceptor.acceptFile(new File(""), "text/plain",
053: "inputName", validation, Locale.getDefault());
054:
055: assertTrue(ok);
056: assertTrue(validation.getFieldErrors().isEmpty());
057: assertFalse(validation.hasErrors());
058:
059: // when file is not of allowed types
060: validation = new ValidationAwareSupport();
061: boolean notOk = interceptor.acceptFile(new File(""),
062: "text/html", "inputName", validation, Locale
063: .getDefault());
064:
065: assertFalse(notOk);
066: assertFalse(validation.getFieldErrors().isEmpty());
067: assertTrue(validation.hasErrors());
068: }
069:
070: public void testAcceptFileWithNoFile() throws Exception {
071: FileUploadInterceptor interceptor = new FileUploadInterceptor();
072: interceptor.setAllowedTypes("text/plain");
073:
074: // when file is not of allowed types
075: ValidationAwareSupport validation = new ValidationAwareSupport();
076: boolean notOk = interceptor.acceptFile(null, "text/html",
077: "inputName", validation, Locale.getDefault());
078:
079: assertFalse(notOk);
080: assertFalse(validation.getFieldErrors().isEmpty());
081: assertTrue(validation.hasErrors());
082: List errors = (List) validation.getFieldErrors().get(
083: "inputName");
084: assertEquals(1, errors.size());
085: String msg = (String) errors.get(0);
086: assertTrue(msg.startsWith("Error uploading:"));
087: assertTrue(msg.indexOf("inputName") > 0);
088: }
089:
090: public void testAcceptFileWithMaxSize() throws Exception {
091: interceptor.setAllowedTypes("text/plain");
092: interceptor.setMaximumSize(new Long(10));
093:
094: // when file is not of allowed types
095: ValidationAwareSupport validation = new ValidationAwareSupport();
096:
097: URL url = ClassLoaderUtil.getResource("log4j.properties",
098: FileUploadInterceptorTest.class);
099: File file = new File(new URI(url.toString()));
100: assertTrue("log4j.properties should be in src/test folder",
101: file.exists());
102: boolean notOk = interceptor.acceptFile(file, "text/html",
103: "inputName", validation, Locale.getDefault());
104:
105: assertFalse(notOk);
106: assertFalse(validation.getFieldErrors().isEmpty());
107: assertTrue(validation.hasErrors());
108: List errors = (List) validation.getFieldErrors().get(
109: "inputName");
110: assertEquals(1, errors.size());
111: String msg = (String) errors.get(0);
112: // the error message shoul contain at least this test
113: assertTrue(msg
114: .startsWith("The file is to large to be uploaded"));
115: assertTrue(msg.indexOf("inputName") > 0);
116: assertTrue(msg.indexOf("log4j.properties") > 0);
117: }
118:
119: public void testNoMultipartRequest() throws Exception {
120: MyFileupAction action = new MyFileupAction();
121:
122: MockActionInvocation mai = new MockActionInvocation();
123: mai.setAction(action);
124: mai.setResultCode("NoMultipart");
125: mai.setInvocationContext(ActionContext.getContext());
126:
127: // if no multipart request it will bypass and execute it
128: assertEquals("NoMultipart", interceptor.intercept(mai));
129: }
130:
131: public void testInvalidContentTypeMultipartRequest()
132: throws Exception {
133: MockHttpServletRequest req = new MockHttpServletRequest();
134:
135: req.setCharacterEncoding("text/html");
136: req.setContentType("text/xml"); // not a multipart contentype
137: req.addHeader("Content-type", "multipart/form-data");
138:
139: MyFileupAction action = new MyFileupAction();
140: MockActionInvocation mai = new MockActionInvocation();
141: mai.setAction(action);
142: mai.setResultCode("success");
143: mai.setInvocationContext(ActionContext.getContext());
144:
145: Map param = new HashMap();
146: ActionContext.getContext().setParameters(param);
147: ActionContext.getContext().put(
148: ServletActionContext.HTTP_REQUEST,
149: createMultipartRequest((HttpServletRequest) req, 2000));
150:
151: interceptor.intercept(mai);
152:
153: assertTrue(action.hasErrors());
154: }
155:
156: public void testNoContentMultipartRequest() throws Exception {
157: MockHttpServletRequest req = new MockHttpServletRequest();
158:
159: req.setCharacterEncoding("text/html");
160: req.setContentType("multipart/form-data; boundary=---1234");
161: req.setContent(null); // there is no content
162:
163: MyFileupAction action = new MyFileupAction();
164: MockActionInvocation mai = new MockActionInvocation();
165: mai.setAction(action);
166: mai.setResultCode("success");
167: mai.setInvocationContext(ActionContext.getContext());
168:
169: Map param = new HashMap();
170: ActionContext.getContext().setParameters(param);
171: ActionContext.getContext().put(
172: ServletActionContext.HTTP_REQUEST,
173: createMultipartRequest((HttpServletRequest) req, 2000));
174:
175: interceptor.intercept(mai);
176:
177: assertTrue(action.hasErrors());
178: }
179:
180: public void testSuccessUploadOfATextFileMultipartRequest()
181: throws Exception {
182: if (true)
183: return; // this test doesn't work with the build currently, so disable for now
184:
185: MockHttpServletRequest req = new MockHttpServletRequest();
186: req.setCharacterEncoding("text/html");
187: req.setContentType("multipart/form-data; boundary=---1234");
188: req.addHeader("Content-type", "multipart/form-data");
189:
190: // inspired by the unit tests for jakarta commons fileupload
191: String content = ("-----1234\r\n"
192: + "Content-Disposition: form-data; name=\"file\"; filename=\"deleteme.txt\"\r\n"
193: + "Content-Type: text/html\r\n" + "\r\n"
194: + "Unit test of FileUploadInterceptor" + "\r\n"
195: + "-----1234--\r\n");
196: req.setContent(content.getBytes("US-ASCII"));
197:
198: MyFileupAction action = new MyFileupAction();
199:
200: MockActionInvocation mai = new MockActionInvocation();
201: mai.setAction(action);
202: mai.setResultCode("success");
203: mai.setInvocationContext(ActionContext.getContext());
204: Map param = new HashMap();
205: ActionContext.getContext().setParameters(param);
206: ActionContext.getContext().put(
207: ServletActionContext.HTTP_REQUEST,
208: createMultipartRequest((HttpServletRequest) req, 2000));
209:
210: interceptor.intercept(mai);
211:
212: assertTrue(!action.hasErrors());
213:
214: assertTrue(param.size() == 3);
215: File[] files = (File[]) param.get("file");
216: String[] fileContentTypes = (String[]) param
217: .get("fileContentType");
218: String[] fileRealFilenames = (String[]) param
219: .get("fileFileName");
220:
221: assertNotNull(files);
222: assertNotNull(fileContentTypes);
223: assertNotNull(fileRealFilenames);
224: assertTrue(files.length == 1);
225: assertTrue(fileContentTypes.length == 1);
226: assertTrue(fileRealFilenames.length == 1);
227: assertEquals("text/html", fileContentTypes[0]);
228: assertNotNull("deleteme.txt", fileRealFilenames[0]);
229: }
230:
231: private MultiPartRequestWrapper createMultipartRequest(
232: HttpServletRequest req, int maxsize) throws IOException {
233: return new MultiPartRequestWrapper(req, "build"
234: + File.separator + "test", maxsize);
235: }
236:
237: protected void setUp() throws Exception {
238: super .setUp();
239: interceptor = new FileUploadInterceptor();
240: }
241:
242: protected void tearDown() throws Exception {
243: interceptor.destroy();
244: super .tearDown();
245: }
246:
247: private class MyFileupAction extends ActionSupport {
248: // no methods
249: }
250:
251: }
|