01: /*
02: * Copyright 2002-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.util.Collections;
20: import java.util.Iterator;
21: import java.util.Map;
22:
23: import org.springframework.core.CollectionFactory;
24: import org.springframework.util.Assert;
25: import org.springframework.web.multipart.MultipartFile;
26: import org.springframework.web.multipart.MultipartHttpServletRequest;
27:
28: /**
29: * Mock implementation of the
30: * {@link org.springframework.web.multipart.MultipartHttpServletRequest} interface.
31: *
32: * <p>Useful for testing application controllers that access multipart uploads.
33: * The {@link MockMultipartFile} can be used to populate these mock requests
34: * with files.
35: *
36: * @author Juergen Hoeller
37: * @author Eric Crampton
38: * @since 2.0
39: * @see MockMultipartFile
40: */
41: public class MockMultipartHttpServletRequest extends
42: MockHttpServletRequest implements MultipartHttpServletRequest {
43:
44: private final Map multipartFiles = CollectionFactory
45: .createLinkedMapIfPossible(4);
46:
47: /**
48: * Add a file to this request. The parameter name from the multipart
49: * form is taken from the {@link MultipartFile#getName()}.
50: * @param file multipart file to be added
51: */
52: public void addFile(MultipartFile file) {
53: Assert.notNull(file, "MultipartFile must not be null");
54: this .multipartFiles.put(file.getName(), file);
55: }
56:
57: public Iterator getFileNames() {
58: return getFileMap().keySet().iterator();
59: }
60:
61: public MultipartFile getFile(String name) {
62: return (MultipartFile) this .multipartFiles.get(name);
63: }
64:
65: public Map getFileMap() {
66: return Collections.unmodifiableMap(this.multipartFiles);
67: }
68:
69: }
|