KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > multipart > cos > CosMultipartResolverTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.web.multipart.cos;
18
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21
22 import javax.servlet.ServletException JavaDoc;
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 /**
34  * INCOMPLETE: How to mock <code>com.oreilly.servlet.MultipartRequest</code>?
35  * @author Juergen Hoeller
36  * @since 08.10.2003
37  */

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 JavaDoc("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 JavaDoc("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 JavaDoc, IOException JavaDoc {
60         MockServletContext sc = new MockServletContext();
61         sc.setAttribute(WebUtils.TEMP_DIR_CONTEXT_ATTRIBUTE, new File JavaDoc("mytemp"));
62         CosMultipartResolver resolver = new CosMultipartResolver(sc);
63         assertEquals(new File JavaDoc("mytemp"), resolver.getUploadTempDir());
64     }
65     
66     public void testMultipartResolution() throws MultipartException, IOException JavaDoc{
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             // expected
77
}
78         new File JavaDoc("bogusTmpDir").delete();
79     }
80
81     public void testWithPhysicalFile() throws IOException JavaDoc{
82         MockServletContext sc = new MockServletContext();
83         CosMultipartResolver resolver = new CosMultipartResolver(sc);
84         resolver.setUploadTempDir(new FileSystemResource("bogusTmpDir"));
85         assertTrue(new File JavaDoc("bogusTmpDir").exists());
86         new File JavaDoc("bogusTmpDir").delete();
87     }
88
89 }
90
Popular Tags