|
Struts example source code file (FileUploadInterceptorTest.java)
The Struts FileUploadInterceptorTest.java source code/* * $Id: FileUploadInterceptorTest.java 1065090 2011-01-29 18:39:22Z mcucchiara $ * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.struts2.interceptor; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ValidationAwareSupport; import com.opensymphony.xwork2.mock.MockActionInvocation; import com.opensymphony.xwork2.util.ClassLoaderUtil; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.struts2.ServletActionContext; import org.apache.struts2.StrutsTestCase; import org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest; import org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper; import org.springframework.mock.web.MockHttpServletRequest; import javax.servlet.http.HttpServletRequest; import java.io.File; import java.io.IOException; import java.net.URI; import java.net.URL; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; /** * Test case for FileUploadInterceptor. */ public class FileUploadInterceptorTest extends StrutsTestCase { private FileUploadInterceptor interceptor; private File tempDir; public void testAcceptFileWithEmptyAllowedTypesAndExtensions() throws Exception { // when allowed type is empty ValidationAwareSupport validation = new ValidationAwareSupport(); boolean ok = interceptor.acceptFile(null, new File(""), "filename", "text/plain", "inputName", validation, Locale.getDefault()); assertTrue(ok); assertTrue(validation.getFieldErrors().isEmpty()); assertFalse(validation.hasErrors()); } public void testAcceptFileWithoutEmptyTypes() throws Exception { interceptor.setAllowedTypes("text/plain"); // when file is of allowed types ValidationAwareSupport validation = new ValidationAwareSupport(); boolean ok = interceptor.acceptFile(null, new File(""), "filename.txt", "text/plain", "inputName", validation, Locale.getDefault()); assertTrue(ok); assertTrue(validation.getFieldErrors().isEmpty()); assertFalse(validation.hasErrors()); // when file is not of allowed types validation = new ValidationAwareSupport(); boolean notOk = interceptor.acceptFile(null, new File(""), "filename.html", "text/html", "inputName", validation, Locale.getDefault()); assertFalse(notOk); assertFalse(validation.getFieldErrors().isEmpty()); assertTrue(validation.hasErrors()); } public void testAcceptFileWithWildcardContent() throws Exception { interceptor.setAllowedTypes("text/*"); ValidationAwareSupport validation = new ValidationAwareSupport(); boolean ok = interceptor.acceptFile(null, new File(""), "filename.txt", "text/plain", "inputName", validation, Locale.getDefault()); assertTrue(ok); assertTrue(validation.getFieldErrors().isEmpty()); assertFalse(validation.hasErrors()); interceptor.setAllowedTypes("text/h*"); validation = new ValidationAwareSupport(); boolean notOk = interceptor.acceptFile(null, new File(""), "filename.html", "text/plain", "inputName", validation, Locale.getDefault()); assertFalse(notOk); assertFalse(validation.getFieldErrors().isEmpty()); assertTrue(validation.hasErrors()); } public void testAcceptFileWithoutEmptyExtensions() throws Exception { interceptor.setAllowedExtensions(".txt"); // when file is of allowed extensions ValidationAwareSupport validation = new ValidationAwareSupport(); boolean ok = interceptor.acceptFile(null, new File(""), "filename.txt", "text/plain", "inputName", validation, Locale.getDefault()); assertTrue(ok); assertTrue(validation.getFieldErrors().isEmpty()); assertFalse(validation.hasErrors()); // when file is not of allowed extensions validation = new ValidationAwareSupport(); boolean notOk = interceptor.acceptFile(null, new File(""), "filename.html", "text/html", "inputName", validation, Locale.getDefault()); assertFalse(notOk); assertFalse(validation.getFieldErrors().isEmpty()); assertTrue(validation.hasErrors()); //test with multiple extensions interceptor.setAllowedExtensions(".txt,.lol"); validation = new ValidationAwareSupport(); ok = interceptor.acceptFile(null, new File(""), "filename.lol", "text/plain", "inputName", validation, Locale.getDefault()); assertTrue(ok); assertTrue(validation.getFieldErrors().isEmpty()); assertFalse(validation.hasErrors()); } public void testAcceptFileWithNoFile() throws Exception { FileUploadInterceptor interceptor = new FileUploadInterceptor(); interceptor.setAllowedTypes("text/plain"); // when file is not of allowed types ValidationAwareSupport validation = new ValidationAwareSupport(); boolean notOk = interceptor.acceptFile(null, null, "filename.html", "text/html", "inputName", validation, Locale.getDefault()); assertFalse(notOk); assertFalse(validation.getFieldErrors().isEmpty()); assertTrue(validation.hasErrors()); List errors = (List) validation.getFieldErrors().get("inputName"); assertEquals(1, errors.size()); String msg = (String) errors.get(0); assertTrue(msg.startsWith("Error uploading:")); assertTrue(msg.indexOf("inputName") > 0); } public void testAcceptFileWithMaxSize() throws Exception { interceptor.setAllowedTypes("text/plain"); interceptor.setMaximumSize(new Long(10)); // when file is not of allowed types ValidationAwareSupport validation = new ValidationAwareSupport(); URL url = ClassLoaderUtil.getResource("log4j.properties", FileUploadInterceptorTest.class); File file = new File(new URI(url.toString())); assertTrue("log4j.properties should be in src/test folder", file.exists()); boolean notOk = interceptor.acceptFile(null, file, "filename", "text/html", "inputName", validation, Locale.getDefault()); assertFalse(notOk); assertFalse(validation.getFieldErrors().isEmpty()); assertTrue(validation.hasErrors()); List errors = (List) validation.getFieldErrors().get("inputName"); assertEquals(1, errors.size()); String msg = (String) errors.get(0); // the error message shoul contain at least this test assertTrue(msg.startsWith("The file is to large to be uploaded")); assertTrue(msg.indexOf("inputName") > 0); assertTrue(msg.indexOf("log4j.properties") > 0); } public void testNoMultipartRequest() throws Exception { MyFileupAction action = new MyFileupAction(); MockActionInvocation mai = new MockActionInvocation(); mai.setAction(action); mai.setResultCode("NoMultipart"); mai.setInvocationContext(ActionContext.getContext()); // if no multipart request it will bypass and execute it assertEquals("NoMultipart", interceptor.intercept(mai)); } public void testInvalidContentTypeMultipartRequest() throws Exception { MockHttpServletRequest req = new MockHttpServletRequest(); req.setCharacterEncoding("text/html"); req.setContentType("text/xml"); // not a multipart contentype req.addHeader("Content-type", "multipart/form-data"); MyFileupAction action = new MyFileupAction(); MockActionInvocation mai = new MockActionInvocation(); mai.setAction(action); mai.setResultCode("success"); mai.setInvocationContext(ActionContext.getContext()); Map param = new HashMap(); ActionContext.getContext().setParameters(param); ActionContext.getContext().put(ServletActionContext.HTTP_REQUEST, createMultipartRequest(req, 2000)); interceptor.intercept(mai); assertTrue(action.hasErrors()); } public void testNoContentMultipartRequest() throws Exception { MockHttpServletRequest req = new MockHttpServletRequest(); req.setCharacterEncoding("text/html"); req.setContentType("multipart/form-data; boundary=---1234"); req.setContent(null); // there is no content MyFileupAction action = new MyFileupAction(); MockActionInvocation mai = new MockActionInvocation(); mai.setAction(action); mai.setResultCode("success"); mai.setInvocationContext(ActionContext.getContext()); Map param = new HashMap(); ActionContext.getContext().setParameters(param); ActionContext.getContext().put(ServletActionContext.HTTP_REQUEST, createMultipartRequest((HttpServletRequest) req, 2000)); interceptor.intercept(mai); assertTrue(action.hasErrors()); } public void testSuccessUploadOfATextFileMultipartRequest() throws Exception { MockHttpServletRequest req = new MockHttpServletRequest(); req.setCharacterEncoding("text/html"); req.setContentType("multipart/form-data; boundary=---1234"); req.addHeader("Content-type", "multipart/form-data"); // inspired by the unit tests for jakarta commons fileupload String content = ("-----1234\r\n" + "Content-Disposition: form-data; name=\"file\"; filename=\"deleteme.txt\"\r\n" + "Content-Type: text/html\r\n" + "\r\n" + "Unit test of FileUploadInterceptor" + "\r\n" + "-----1234--\r\n"); req.setContent(content.getBytes("US-ASCII")); MyFileupAction action = new MyFileupAction(); MockActionInvocation mai = new MockActionInvocation(); mai.setAction(action); mai.setResultCode("success"); mai.setInvocationContext(ActionContext.getContext()); Map<String, Object> param = new HashMap Other Struts examples (source code examples)Here is a short list of links related to this Struts FileUploadInterceptorTest.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.