001: /*
002: * Copyright 2002-2006 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: * http://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:
017: package org.springframework.mock.web;
018:
019: import java.io.ByteArrayInputStream;
020: import java.io.File;
021: import java.io.IOException;
022: import java.io.InputStream;
023:
024: import org.springframework.util.Assert;
025: import org.springframework.util.FileCopyUtils;
026: import 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: */
040: public 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: * Create a new MockMultipartFile with the given content.
052: * @param name the name of the file
053: * @param content the content of the file
054: */
055: public MockMultipartFile(String name, byte[] content) {
056: this (name, "", null, content);
057: }
058:
059: /**
060: * Create a new MockMultipartFile with the given content.
061: * @param name the name of the file
062: * @param contentStream the content of the file as stream
063: * @throws IOException if reading from the stream failed
064: */
065: public MockMultipartFile(String name, InputStream contentStream)
066: throws IOException {
067: this (name, "", null, FileCopyUtils
068: .copyToByteArray(contentStream));
069: }
070:
071: /**
072: * Create a new MockMultipartFile with the given content.
073: * @param name the name of the file
074: * @param originalFilename the original filename (as on the client's machine)
075: * @param contentType the content type (if known)
076: * @param content the content of the file
077: */
078: public MockMultipartFile(String name, String originalFilename,
079: String contentType, byte[] content) {
080: Assert.hasLength(name, "Name must not be null");
081: this .name = name;
082: this .originalFilename = (originalFilename != null ? originalFilename
083: : "");
084: this .contentType = contentType;
085: this .content = (content != null ? content : new byte[0]);
086: }
087:
088: /**
089: * Create a new MockMultipartFile with the given content.
090: * @param name the name of the file
091: * @param originalFilename the original filename (as on the client's machine)
092: * @param contentType the content type (if known)
093: * @param contentStream the content of the file as stream
094: * @throws IOException if reading from the stream failed
095: */
096: public MockMultipartFile(String name, String originalFilename,
097: String contentType, InputStream contentStream)
098: throws IOException {
099:
100: this (name, originalFilename, contentType, FileCopyUtils
101: .copyToByteArray(contentStream));
102: }
103:
104: public String getName() {
105: return this .name;
106: }
107:
108: public String getOriginalFilename() {
109: return this .originalFilename;
110: }
111:
112: public String getContentType() {
113: return this .contentType;
114: }
115:
116: public boolean isEmpty() {
117: return (this .content.length == 0);
118: }
119:
120: public long getSize() {
121: return this .content.length;
122: }
123:
124: public byte[] getBytes() throws IOException {
125: return this .content;
126: }
127:
128: public InputStream getInputStream() throws IOException {
129: return new ByteArrayInputStream(this .content);
130: }
131:
132: public void transferTo(File dest) throws IOException,
133: IllegalStateException {
134: FileCopyUtils.copy(this.content, dest);
135: }
136:
137: }
|