View Javadoc
1   /*
2    * Copyright 2002-2012 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.mock.web.test;
18  
19  import java.io.ByteArrayInputStream;
20  import java.io.File;
21  import java.io.IOException;
22  import java.io.InputStream;
23  
24  import org.springframework.util.Assert;
25  import org.springframework.util.FileCopyUtils;
26  import org.springframework.web.multipart.MultipartFile;
27  
28  /**
29   * Mock implementation of the {@link org.springframework.web.multipart.MultipartFile}
30   * interface.
31   *
32   * <p>Useful in conjunction with a {@link MockMultipartHttpServletRequest}
33   * for testing application controllers that access multipart uploads.
34   *
35   * @author Juergen Hoeller
36   * @author Eric Crampton
37   * @since 2.0
38   * @see MockMultipartHttpServletRequest
39   */
40  public class MockMultipartFile implements MultipartFile {
41  
42  	private final String name;
43  
44  	private String originalFilename;
45  
46  	private String contentType;
47  
48  	private final byte[] content;
49  
50  
51  	/**
52  	 * Create a new MockMultipartFile with the given content.
53  	 * @param name the name of the file
54  	 * @param content the content of the file
55  	 */
56  	public MockMultipartFile(String name, byte[] content) {
57  		this(name, "", null, content);
58  	}
59  
60  	/**
61  	 * Create a new MockMultipartFile with the given content.
62  	 * @param name the name of the file
63  	 * @param contentStream the content of the file as stream
64  	 * @throws IOException if reading from the stream failed
65  	 */
66  	public MockMultipartFile(String name, InputStream contentStream) throws IOException {
67  		this(name, "", null, FileCopyUtils.copyToByteArray(contentStream));
68  	}
69  
70  	/**
71  	 * Create a new MockMultipartFile with the given content.
72  	 * @param name the name of the file
73  	 * @param originalFilename the original filename (as on the client's machine)
74  	 * @param contentType the content type (if known)
75  	 * @param content the content of the file
76  	 */
77  	public MockMultipartFile(String name, String originalFilename, String contentType, byte[] content) {
78  		Assert.hasLength(name, "Name must not be null");
79  		this.name = name;
80  		this.originalFilename = (originalFilename != null ? originalFilename : "");
81  		this.contentType = contentType;
82  		this.content = (content != null ? content : new byte[0]);
83  	}
84  
85  	/**
86  	 * Create a new MockMultipartFile with the given content.
87  	 * @param name the name of the file
88  	 * @param originalFilename the original filename (as on the client's machine)
89  	 * @param contentType the content type (if known)
90  	 * @param contentStream the content of the file as stream
91  	 * @throws IOException if reading from the stream failed
92  	 */
93  	public MockMultipartFile(String name, String originalFilename, String contentType, InputStream contentStream)
94  			throws IOException {
95  
96  		this(name, originalFilename, contentType, FileCopyUtils.copyToByteArray(contentStream));
97  	}
98  
99  
100 	@Override
101 	public String getName() {
102 		return this.name;
103 	}
104 
105 	@Override
106 	public String getOriginalFilename() {
107 		return this.originalFilename;
108 	}
109 
110 	@Override
111 	public String getContentType() {
112 		return this.contentType;
113 	}
114 
115 	@Override
116 	public boolean isEmpty() {
117 		return (this.content.length == 0);
118 	}
119 
120 	@Override
121 	public long getSize() {
122 		return this.content.length;
123 	}
124 
125 	@Override
126 	public byte[] getBytes() throws IOException {
127 		return this.content;
128 	}
129 
130 	@Override
131 	public InputStream getInputStream() throws IOException {
132 		return new ByteArrayInputStream(this.content);
133 	}
134 
135 	@Override
136 	public void transferTo(File dest) throws IOException, IllegalStateException {
137 		FileCopyUtils.copy(this.content, dest);
138 	}
139 
140 }