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.fileupload2.portlet;
    18
    19  import static org.junit.jupiter.api.Assertions.assertEquals;
    20  import static org.junit.jupiter.api.Assertions.assertTrue;
    21
    22  import java.nio.charset.StandardCharsets;
    23  import java.util.List;
    24
    25  import javax.portlet.ActionRequest;
    26
    27  import org.apache.commons.fileupload2.core.AbstractFileUploadTest;
    28  import org.apache.commons.fileupload2.core.Constants;
    29  import org.apache.commons.fileupload2.core.DiskFileItem;
    30  import org.apache.commons.fileupload2.core.DiskFileItemFactory;
    31  import org.apache.commons.fileupload2.core.FileUploadException;
    32  import org.junit.jupiter.api.Test;
    33
    34  /**
    35   * Tests {@link JavaxPortletFileUpload}.
    36   *
    37   * @see AbstractFileUploadTest
    38   */
    39  public class JavaxPortletFileUploadTest
    40          extends AbstractFileUploadTest<JavaxPortletFileUpload<DiskFileItem, DiskFileItemFactory>, ActionRequest, DiskFileItem, DiskFileItemFactory> {
    41
    42      public JavaxPortletFileUploadTest() {
    43          super(new JavaxPortletFileUpload<>(DiskFileItemFactory.builder().get()));
    44      }
    45
    46      @Override
    47      public List<DiskFileItem> parseUpload(final JavaxPortletFileUpload<DiskFileItem, DiskFileItemFactory> upload, final byte[] bytes, final String contentType)
    48              throws FileUploadException {
    49          final ActionRequest request = new JavaxPortletMockActionRequest(bytes, contentType);
    50          return upload.parseRequest(new JavaxPortletRequestContext(request));
    51      }
    52
    53      @Test
    54      public void testParseParameterMap() throws Exception {
    55          // @formatter:off
    56          final var text = "-----1234\r\n" +
    57                        "Content-Disposition: form-data; name=\"file\"; filename=\"foo.tab\"\r\n" +
    58                        "Content-Type: text/whatever\r\n" +
    59                        "\r\n" +
    60                        "This is the content of the file\n" +
    61                        "\r\n" +
    62                        "-----1234\r\n" +
    63                        "Content-Disposition: form-data; name=\"field\"\r\n" +
    64                        "\r\n" +
    65                        "fieldValue\r\n" +
    66                        "-----1234\r\n" +
    67                        "Content-Disposition: form-data; name=\"multi\"\r\n" +
    68                        "\r\n" +
    69                        "value1\r\n" +
    70                        "-----1234\r\n" +
    71                        "Content-Disposition: form-data; name=\"multi\"\r\n" +
    72                        "\r\n" +
    73                        "value2\r\n" +
    74                        "-----1234--\r\n";
    75          // @formatter:on
    76          final var bytes = text.getBytes(StandardCharsets.US_ASCII);
    77          final ActionRequest request = new JavaxPortletMockActionRequest(bytes, Constants.CONTENT_TYPE);
    78
    79          final var mappedParameters = upload.parseParameterMap(request);
    80          assertTrue(mappedParameters.containsKey("file"));
    81          assertEquals(1, mappedParameters.get("file").size());
    82
    83          assertTrue(mappedParameters.containsKey("field"));
    84          assertEquals(1, mappedParameters.get("field").size());
    85
    86          assertTrue(mappedParameters.containsKey("multi"));
    87          assertEquals(2, mappedParameters.get("multi").size());
    88      }
    89
    90  }