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