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