KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > mock > web > MockMultipartFile


1 /*
2  * Copyright 2002-2006 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;
18
19 import java.io.ByteArrayInputStream JavaDoc;
20 import java.io.File JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
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 JavaDoc name;
43
44     private String JavaDoc originalFilename;
45
46     private String JavaDoc 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 JavaDoc 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 JavaDoc name, InputStream JavaDoc contentStream) throws IOException JavaDoc {
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 JavaDoc name, String JavaDoc originalFilename, String JavaDoc 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 JavaDoc name, String JavaDoc originalFilename, String JavaDoc contentType, InputStream JavaDoc contentStream)
94             throws IOException JavaDoc {
95
96         this(name, originalFilename, contentType, FileCopyUtils.copyToByteArray(contentStream));
97     }
98
99
100     public String JavaDoc getName() {
101         return this.name;
102     }
103
104     public String JavaDoc getOriginalFilename() {
105         return this.originalFilename;
106     }
107
108     public String JavaDoc getContentType() {
109         return this.contentType;
110     }
111
112     public boolean isEmpty() {
113         return (this.content.length == 0);
114     }
115
116     public long getSize() {
117         return this.content.length;
118     }
119
120     public byte[] getBytes() throws IOException JavaDoc {
121         return this.content;
122     }
123
124     public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
125         return new ByteArrayInputStream JavaDoc(this.content);
126     }
127
128     public void transferTo(File JavaDoc dest) throws IOException JavaDoc, IllegalStateException JavaDoc {
129         FileCopyUtils.copy(this.content, dest);
130     }
131
132 }
133
Popular Tags