Skip to content

Commit 1841b14

Browse files
committed
consistent mocks between modules
1 parent 19fcbea commit 1841b14

File tree

7 files changed

+269
-43
lines changed

7 files changed

+269
-43
lines changed

org.springframework.test/src/main/java/org/springframework/mock/web/MockMultipartHttpServletRequest.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2010 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,13 +18,12 @@
1818

1919
import java.util.Collections;
2020
import java.util.Iterator;
21-
import java.util.LinkedHashMap;
22-
import java.util.Map;
2321
import java.util.List;
22+
import java.util.Map;
2423

2524
import org.springframework.util.Assert;
26-
import org.springframework.util.MultiValueMap;
2725
import org.springframework.util.LinkedMultiValueMap;
26+
import org.springframework.util.MultiValueMap;
2827
import org.springframework.web.multipart.MultipartFile;
2928
import org.springframework.web.multipart.MultipartHttpServletRequest;
3029

@@ -44,7 +43,8 @@
4443
*/
4544
public class MockMultipartHttpServletRequest extends MockHttpServletRequest implements MultipartHttpServletRequest {
4645

47-
private final MultiValueMap<String, MultipartFile> multipartFiles = new LinkedMultiValueMap<String, MultipartFile>();
46+
private final MultiValueMap<String, MultipartFile> multipartFiles =
47+
new LinkedMultiValueMap<String, MultipartFile>();
4848

4949

5050
/**
@@ -58,7 +58,7 @@ public void addFile(MultipartFile file) {
5858
}
5959

6060
public Iterator<String> getFileNames() {
61-
return getFileMap().keySet().iterator();
61+
return this.multipartFiles.keySet().iterator();
6262
}
6363

6464
public MultipartFile getFile(String name) {
@@ -74,13 +74,13 @@ public List<MultipartFile> getFiles(String name) {
7474
return Collections.emptyList();
7575
}
7676
}
77-
77+
7878
public Map<String, MultipartFile> getFileMap() {
79-
return Collections.unmodifiableMap(this.multipartFiles.toSingleValueMap());
79+
return this.multipartFiles.toSingleValueMap();
8080
}
8181

8282
public MultiValueMap<String, MultipartFile> getMultiFileMap() {
83-
return new LinkedMultiValueMap<String, MultipartFile>(Collections.unmodifiableMap(this.multipartFiles));
83+
return new LinkedMultiValueMap<String, MultipartFile>(this.multipartFiles);
8484
}
8585

8686
}

org.springframework.test/src/main/java/org/springframework/mock/web/MockServletContext.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2010 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -89,6 +89,8 @@ public class MockServletContext implements ServletContext {
8989

9090
private String contextPath = "";
9191

92+
private int minorVersion = 5;
93+
9294
private final Map<String, ServletContext> contexts = new HashMap<String, ServletContext>();
9395

9496
private final Map<String, String> initParameters = new LinkedHashMap<String, String>();
@@ -97,7 +99,6 @@ public class MockServletContext implements ServletContext {
9799

98100
private String servletContextName = "MockServletContext";
99101

100-
private int minorVersion = 5;
101102

102103
/**
103104
* Create a new MockServletContext, using no base path and a
@@ -181,6 +182,13 @@ public int getMajorVersion() {
181182
return 2;
182183
}
183184

185+
public void setMinorVersion(int minorVersion) {
186+
if (minorVersion < 3 || minorVersion > 5) {
187+
throw new IllegalArgumentException("Only Servlet minor versions between 3 and 5 are supported");
188+
}
189+
this.minorVersion = minorVersion;
190+
}
191+
184192
public int getMinorVersion() {
185193
return this.minorVersion;
186194
}
@@ -341,11 +349,6 @@ public String getServletContextName() {
341349
return this.servletContextName;
342350
}
343351

344-
public void setMinorVersion(int minorVersion) {
345-
if (minorVersion <3 || minorVersion > 5)
346-
throw new IllegalArgumentException("Only Servlet minor versions between 3 and 5 are supported");
347-
this.minorVersion = minorVersion;
348-
}
349352

350353
/**
351354
* Inner factory class used to just introduce a Java Activation Framework
@@ -357,4 +360,5 @@ public static String getMimeType(String filePath) {
357360
return FileTypeMap.getDefaultFileTypeMap().getContentType(filePath);
358361
}
359362
}
360-
}
363+
364+
}

org.springframework.test/src/main/java/org/springframework/mock/web/portlet/MockMultipartActionRequest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2010 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,13 +18,12 @@
1818

1919
import java.util.Collections;
2020
import java.util.Iterator;
21-
import java.util.LinkedHashMap;
22-
import java.util.Map;
2321
import java.util.List;
22+
import java.util.Map;
2423

2524
import org.springframework.util.Assert;
26-
import org.springframework.util.MultiValueMap;
2725
import org.springframework.util.LinkedMultiValueMap;
26+
import org.springframework.util.MultiValueMap;
2827
import org.springframework.web.multipart.MultipartFile;
2928
import org.springframework.web.portlet.multipart.MultipartActionRequest;
3029

@@ -43,7 +42,8 @@
4342
*/
4443
public class MockMultipartActionRequest extends MockActionRequest implements MultipartActionRequest {
4544

46-
private final MultiValueMap<String, MultipartFile> multipartFiles = new LinkedMultiValueMap<String, MultipartFile>();
45+
private final MultiValueMap<String, MultipartFile> multipartFiles =
46+
new LinkedMultiValueMap<String, MultipartFile>();
4747

4848

4949
/**
@@ -57,7 +57,7 @@ public void addFile(MultipartFile file) {
5757
}
5858

5959
public Iterator<String> getFileNames() {
60-
return getFileMap().keySet().iterator();
60+
return this.multipartFiles.keySet().iterator();
6161
}
6262

6363
public MultipartFile getFile(String name) {
@@ -75,11 +75,11 @@ public List<MultipartFile> getFiles(String name) {
7575
}
7676

7777
public Map<String, MultipartFile> getFileMap() {
78-
return Collections.unmodifiableMap(this.multipartFiles.toSingleValueMap());
78+
return this.multipartFiles.toSingleValueMap();
7979
}
8080

8181
public MultiValueMap<String, MultipartFile> getMultiFileMap() {
82-
return new LinkedMultiValueMap<String, MultipartFile>(Collections.unmodifiableMap(this.multipartFiles));
82+
return new LinkedMultiValueMap<String, MultipartFile>(this.multipartFiles);
8383
}
8484

8585
}

org.springframework.web.portlet/src/test/java/org/springframework/mock/web/portlet/MockMultipartActionRequest.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2010 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,13 +18,12 @@
1818

1919
import java.util.Collections;
2020
import java.util.Iterator;
21-
import java.util.LinkedHashMap;
22-
import java.util.Map;
2321
import java.util.List;
22+
import java.util.Map;
2423

2524
import org.springframework.util.Assert;
26-
import org.springframework.util.MultiValueMap;
2725
import org.springframework.util.LinkedMultiValueMap;
26+
import org.springframework.util.MultiValueMap;
2827
import org.springframework.web.multipart.MultipartFile;
2928
import org.springframework.web.portlet.multipart.MultipartActionRequest;
3029

@@ -43,7 +42,8 @@
4342
*/
4443
public class MockMultipartActionRequest extends MockActionRequest implements MultipartActionRequest {
4544

46-
private final MultiValueMap<String, MultipartFile> multipartFiles = new LinkedMultiValueMap<String, MultipartFile>();
45+
private final MultiValueMap<String, MultipartFile> multipartFiles =
46+
new LinkedMultiValueMap<String, MultipartFile>();
4747

4848

4949
/**
@@ -57,7 +57,7 @@ public void addFile(MultipartFile file) {
5757
}
5858

5959
public Iterator<String> getFileNames() {
60-
return getFileMap().keySet().iterator();
60+
return this.multipartFiles.keySet().iterator();
6161
}
6262

6363
public MultipartFile getFile(String name) {
@@ -75,10 +75,11 @@ public List<MultipartFile> getFiles(String name) {
7575
}
7676

7777
public Map<String, MultipartFile> getFileMap() {
78-
return Collections.unmodifiableMap(this.multipartFiles.toSingleValueMap());
78+
return this.multipartFiles.toSingleValueMap();
7979
}
8080

8181
public MultiValueMap<String, MultipartFile> getMultiFileMap() {
82-
return new LinkedMultiValueMap<String, MultipartFile>(Collections.unmodifiableMap(this.multipartFiles));
82+
return new LinkedMultiValueMap<String, MultipartFile>(this.multipartFiles);
8383
}
84+
8485
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
* Copyright 2002-2010 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;
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 java.io.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 java.io.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+
public String getName() {
101+
return this.name;
102+
}
103+
104+
public String getOriginalFilename() {
105+
return this.originalFilename;
106+
}
107+
108+
public String 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 {
121+
return this.content;
122+
}
123+
124+
public InputStream getInputStream() throws IOException {
125+
return new ByteArrayInputStream(this.content);
126+
}
127+
128+
public void transferTo(File dest) throws IOException, IllegalStateException {
129+
FileCopyUtils.copy(this.content, dest);
130+
}
131+
132+
}

0 commit comments

Comments
 (0)