1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.fileupload.portlet;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertTrue;
22
23 import java.util.List;
24 import java.util.Map;
25
26 import javax.portlet.ActionRequest;
27
28 import org.apache.commons.fileupload.Constants;
29 import org.apache.commons.fileupload.FileItem;
30 import org.apache.commons.fileupload.FileUploadTest;
31 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
32 import org.junit.Before;
33 import org.junit.Test;
34
35
36
37
38
39
40
41 public class PortletFileUploadTest {
42
43 private PortletFileUpload upload;
44
45 @Test
46 public void parseParameterMap() throws Exception {
47
48 final String text = "-----1234\r\n"
49 + "Content-Disposition: form-data; name=\"file\"; filename=\"foo.tab\"\r\n"
50 + "Content-Type: text/whatever\r\n"
51 + "\r\n"
52 + "This is the content of the file\n"
53 + "\r\n"
54 + "-----1234\r\n"
55 + "Content-Disposition: form-data; name=\"field\"\r\n"
56 + "\r\n"
57 + "fieldValue\r\n"
58 + "-----1234\r\n"
59 + "Content-Disposition: form-data; name=\"multi\"\r\n"
60 + "\r\n"
61 + "value1\r\n"
62 + "-----1234\r\n"
63 + "Content-Disposition: form-data; name=\"multi\"\r\n"
64 + "\r\n"
65 + "value2\r\n"
66 + "-----1234--\r\n";
67
68 final byte[] bytes = text.getBytes("US-ASCII");
69 final ActionRequest request = new MockPortletActionRequest(bytes, Constants.CONTENT_TYPE);
70 final Map<String, List<FileItem>> mappedParameters = upload.parseParameterMap(request);
71 assertTrue(mappedParameters.containsKey("file"));
72 assertEquals(1, mappedParameters.get("file").size());
73 assertTrue(mappedParameters.containsKey("field"));
74 assertEquals(1, mappedParameters.get("field").size());
75 assertTrue(mappedParameters.containsKey("multi"));
76 assertEquals(2, mappedParameters.get("multi").size());
77 }
78
79 @Before
80 public void setUp() {
81 upload = new PortletFileUpload(new DiskFileItemFactory());
82 }
83 }