View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Test for {@link PortletFileUpload}.
37   *
38   * @see FileUploadTest
39   * @since 1.4
40   */
41  public class PortletFileUploadTest {
42  
43      private PortletFileUpload upload;
44  
45      @Test
46      public void parseParameterMap() throws Exception {
47          // @formatter:off
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          // @formatter:on
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  }