001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.fileupload2.portlet; 018 019import static org.junit.jupiter.api.Assertions.assertEquals; 020import static org.junit.jupiter.api.Assertions.assertTrue; 021 022import java.nio.charset.StandardCharsets; 023import java.util.List; 024 025import javax.portlet.ActionRequest; 026 027import org.apache.commons.fileupload2.core.AbstractFileUploadTest; 028import org.apache.commons.fileupload2.core.Constants; 029import org.apache.commons.fileupload2.core.DiskFileItem; 030import org.apache.commons.fileupload2.core.DiskFileItemFactory; 031import org.apache.commons.fileupload2.core.FileUploadException; 032import org.junit.jupiter.api.Test; 033 034/** 035 * Tests {@link JavaxPortletFileUpload}. 036 * 037 * @see AbstractFileUploadTest 038 */ 039public class JavaxPortletFileUploadTest 040 extends AbstractFileUploadTest<JavaxPortletFileUpload<DiskFileItem, DiskFileItemFactory>, ActionRequest, DiskFileItem, DiskFileItemFactory> { 041 042 public JavaxPortletFileUploadTest() { 043 super(new JavaxPortletFileUpload<>(DiskFileItemFactory.builder().get())); 044 } 045 046 @Override 047 public List<DiskFileItem> parseUpload(final JavaxPortletFileUpload<DiskFileItem, DiskFileItemFactory> upload, final byte[] bytes, final String contentType) 048 throws FileUploadException { 049 final ActionRequest request = new JavaxPortletMockActionRequest(bytes, contentType); 050 return upload.parseRequest(new JavaxPortletRequestContext(request)); 051 } 052 053 @Test 054 public void testParseParameterMap() throws Exception { 055 // @formatter:off 056 final var text = "-----1234\r\n" + 057 "Content-Disposition: form-data; name=\"file\"; filename=\"foo.tab\"\r\n" + 058 "Content-Type: text/whatever\r\n" + 059 "\r\n" + 060 "This is the content of the file\n" + 061 "\r\n" + 062 "-----1234\r\n" + 063 "Content-Disposition: form-data; name=\"field\"\r\n" + 064 "\r\n" + 065 "fieldValue\r\n" + 066 "-----1234\r\n" + 067 "Content-Disposition: form-data; name=\"multi\"\r\n" + 068 "\r\n" + 069 "value1\r\n" + 070 "-----1234\r\n" + 071 "Content-Disposition: form-data; name=\"multi\"\r\n" + 072 "\r\n" + 073 "value2\r\n" + 074 "-----1234--\r\n"; 075 // @formatter:on 076 final var bytes = text.getBytes(StandardCharsets.US_ASCII); 077 final ActionRequest request = new JavaxPortletMockActionRequest(bytes, Constants.CONTENT_TYPE); 078 079 final var mappedParameters = upload.parseParameterMap(request); 080 assertTrue(mappedParameters.containsKey("file")); 081 assertEquals(1, mappedParameters.get("file").size()); 082 083 assertTrue(mappedParameters.containsKey("field")); 084 assertEquals(1, mappedParameters.get("field").size()); 085 086 assertTrue(mappedParameters.containsKey("multi")); 087 assertEquals(2, mappedParameters.get("multi").size()); 088 } 089 090}