001/*
002 * Copyright 2002-2012 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.mock.web.portlet;
018
019import java.util.Collections;
020import java.util.Iterator;
021import java.util.List;
022import java.util.Map;
023
024import org.springframework.util.Assert;
025import org.springframework.util.LinkedMultiValueMap;
026import org.springframework.util.MultiValueMap;
027import org.springframework.web.multipart.MultipartFile;
028import org.springframework.web.portlet.multipart.MultipartActionRequest;
029
030/**
031 * Mock implementation of the
032 * {@link org.springframework.web.portlet.multipart.MultipartActionRequest} interface.
033 *
034 * <p>Useful for testing application controllers that access multipart uploads.
035 * The {@link org.springframework.mock.web.MockMultipartFile} can be used to
036 * populate these mock requests with files.
037 *
038 * @author Juergen Hoeller
039 * @author Arjen Poutsma
040 * @since 2.0
041 * @see org.springframework.mock.web.MockMultipartFile
042 */
043public class MockMultipartActionRequest extends MockActionRequest implements MultipartActionRequest {
044
045        private final MultiValueMap<String, MultipartFile> multipartFiles =
046                        new LinkedMultiValueMap<String, MultipartFile>();
047
048
049        /**
050         * Add a file to this request. The parameter name from the multipart
051         * form is taken from the {@link org.springframework.web.multipart.MultipartFile#getName()}.
052         * @param file multipart file to be added
053         */
054        public void addFile(MultipartFile file) {
055                Assert.notNull(file, "MultipartFile must not be null");
056                this.multipartFiles.add(file.getName(), file);
057        }
058
059        @Override
060        public Iterator<String> getFileNames() {
061                return this.multipartFiles.keySet().iterator();
062        }
063
064        @Override
065        public MultipartFile getFile(String name) {
066                return this.multipartFiles.getFirst(name);
067        }
068
069        @Override
070        public List<MultipartFile> getFiles(String name) {
071                List<MultipartFile> multipartFiles = this.multipartFiles.get(name);
072                if (multipartFiles != null) {
073                        return multipartFiles;
074                }
075                else {
076                        return Collections.emptyList();
077                }
078        }
079
080        @Override
081        public Map<String, MultipartFile> getFileMap() {
082                return this.multipartFiles.toSingleValueMap();
083        }
084
085        @Override
086        public MultiValueMap<String, MultipartFile> getMultiFileMap() {
087                return new LinkedMultiValueMap<String, MultipartFile>(this.multipartFiles);
088        }
089
090        @Override
091        public String getMultipartContentType(String paramOrFileName) {
092                MultipartFile file = getFile(paramOrFileName);
093                if (file != null) {
094                        return file.getContentType();
095                }
096                else {
097                        return null;
098                }
099        }
100
101}