1 16 17 package org.springframework.web.multipart.cos; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 22 import javax.servlet.ServletException ; 23 24 import junit.framework.TestCase; 25 26 import org.springframework.core.io.FileSystemResource; 27 import org.springframework.mock.web.MockHttpServletRequest; 28 import org.springframework.mock.web.MockServletContext; 29 import org.springframework.web.context.support.StaticWebApplicationContext; 30 import org.springframework.web.multipart.MultipartException; 31 import org.springframework.web.util.WebUtils; 32 33 38 public class CosMultipartResolverTests extends TestCase { 39 40 public void testWithApplicationContext() throws MultipartException { 41 StaticWebApplicationContext wac = new StaticWebApplicationContext(); 42 wac.setServletContext(new MockServletContext()); 43 wac.getServletContext().setAttribute(WebUtils.TEMP_DIR_CONTEXT_ATTRIBUTE, new File ("mytemp")); 44 wac.refresh(); 45 CosMultipartResolver resolver = new CosMultipartResolver(); 46 resolver.setMaxUploadSize(1000); 47 resolver.setDefaultEncoding("enc"); 48 resolver.setServletContext(wac.getServletContext()); 49 assertEquals(1000, resolver.getMaxUploadSize()); 50 assertEquals("enc", resolver.getDefaultEncoding()); 51 assertEquals(new File ("mytemp"), resolver.getUploadTempDir()); 52 53 MockHttpServletRequest originalRequest = new MockHttpServletRequest(); 54 originalRequest.setContentType("multipart/form-data"); 55 originalRequest.addHeader("Content-type", "multipart/form-data"); 56 assertTrue(resolver.isMultipart(originalRequest)); 57 } 58 59 public void testWithServletContext() throws ServletException , IOException { 60 MockServletContext sc = new MockServletContext(); 61 sc.setAttribute(WebUtils.TEMP_DIR_CONTEXT_ATTRIBUTE, new File ("mytemp")); 62 CosMultipartResolver resolver = new CosMultipartResolver(sc); 63 assertEquals(new File ("mytemp"), resolver.getUploadTempDir()); 64 } 65 66 public void testMultipartResolution() throws MultipartException, IOException { 67 MockServletContext sc = new MockServletContext(); 68 MockHttpServletRequest rq = new MockHttpServletRequest(sc); 69 CosMultipartResolver resolver = new CosMultipartResolver(sc); 70 resolver.setUploadTempDir(new FileSystemResource("bogusTmpDir")); 71 try { 72 resolver.resolveMultipart(rq); 73 fail("the http request was mocked, expected a MultipartException"); 74 } 75 catch (MultipartException e){ 76 } 78 new File ("bogusTmpDir").delete(); 79 } 80 81 public void testWithPhysicalFile() throws IOException { 82 MockServletContext sc = new MockServletContext(); 83 CosMultipartResolver resolver = new CosMultipartResolver(sc); 84 resolver.setUploadTempDir(new FileSystemResource("bogusTmpDir")); 85 assertTrue(new File ("bogusTmpDir").exists()); 86 new File ("bogusTmpDir").delete(); 87 } 88 89 } 90 | Popular Tags |