001/*
002 * Copyright 2002-2018 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;
018
019import java.io.ByteArrayInputStream;
020import java.io.File;
021import java.io.IOException;
022import java.io.InputStream;
023
024import org.springframework.lang.Nullable;
025import org.springframework.util.Assert;
026import org.springframework.util.FileCopyUtils;
027import org.springframework.web.multipart.MultipartFile;
028
029/**
030 * Mock implementation of the {@link org.springframework.web.multipart.MultipartFile}
031 * interface.
032 *
033 * <p>Useful in conjunction with a {@link MockMultipartHttpServletRequest}
034 * for testing application controllers that access multipart uploads.
035 *
036 * @author Juergen Hoeller
037 * @author Eric Crampton
038 * @since 2.0
039 * @see MockMultipartHttpServletRequest
040 */
041public class MockMultipartFile implements MultipartFile {
042
043        private final String name;
044
045        private String originalFilename;
046
047        @Nullable
048        private String contentType;
049
050        private final byte[] content;
051
052
053        /**
054         * Create a new MockMultipartFile with the given content.
055         * @param name the name of the file
056         * @param content the content of the file
057         */
058        public MockMultipartFile(String name, @Nullable byte[] content) {
059                this(name, "", null, content);
060        }
061
062        /**
063         * Create a new MockMultipartFile with the given content.
064         * @param name the name of the file
065         * @param contentStream the content of the file as stream
066         * @throws IOException if reading from the stream failed
067         */
068        public MockMultipartFile(String name, InputStream contentStream) throws IOException {
069                this(name, "", null, FileCopyUtils.copyToByteArray(contentStream));
070        }
071
072        /**
073         * Create a new MockMultipartFile with the given content.
074         * @param name the name of the file
075         * @param originalFilename the original filename (as on the client's machine)
076         * @param contentType the content type (if known)
077         * @param content the content of the file
078         */
079        public MockMultipartFile(
080                        String name, @Nullable String originalFilename, @Nullable String contentType, @Nullable byte[] content) {
081
082                Assert.hasLength(name, "Name must not be null");
083                this.name = name;
084                this.originalFilename = (originalFilename != null ? originalFilename : "");
085                this.contentType = contentType;
086                this.content = (content != null ? content : new byte[0]);
087        }
088
089        /**
090         * Create a new MockMultipartFile with the given content.
091         * @param name the name of the file
092         * @param originalFilename the original filename (as on the client's machine)
093         * @param contentType the content type (if known)
094         * @param contentStream the content of the file as stream
095         * @throws IOException if reading from the stream failed
096         */
097        public MockMultipartFile(
098                        String name, @Nullable String originalFilename, @Nullable String contentType, InputStream contentStream)
099                        throws IOException {
100
101                this(name, originalFilename, contentType, FileCopyUtils.copyToByteArray(contentStream));
102        }
103
104
105        @Override
106        public String getName() {
107                return this.name;
108        }
109
110        @Override
111        public String getOriginalFilename() {
112                return this.originalFilename;
113        }
114
115        @Override
116        @Nullable
117        public String getContentType() {
118                return this.contentType;
119        }
120
121        @Override
122        public boolean isEmpty() {
123                return (this.content.length == 0);
124        }
125
126        @Override
127        public long getSize() {
128                return this.content.length;
129        }
130
131        @Override
132        public byte[] getBytes() throws IOException {
133                return this.content;
134        }
135
136        @Override
137        public InputStream getInputStream() throws IOException {
138                return new ByteArrayInputStream(this.content);
139        }
140
141        @Override
142        public void transferTo(File dest) throws IOException, IllegalStateException {
143                FileCopyUtils.copy(this.content, dest);
144        }
145
146}