1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.bremersee.spring.web.multipart;
18
19 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertFalse;
22 import static org.junit.jupiter.api.Assertions.assertNotNull;
23 import static org.junit.jupiter.api.Assertions.assertTrue;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.when;
26
27 import java.io.ByteArrayInputStream;
28 import java.io.ByteArrayOutputStream;
29 import java.io.File;
30 import java.io.FileWriter;
31 import java.io.InputStream;
32 import java.nio.charset.StandardCharsets;
33 import java.nio.file.Files;
34 import java.nio.file.Path;
35 import java.nio.file.StandardOpenOption;
36 import java.util.Optional;
37 import org.junit.jupiter.api.Test;
38 import org.springframework.core.io.Resource;
39 import org.springframework.http.MediaType;
40 import org.springframework.util.FileCopyUtils;
41 import org.springframework.web.multipart.MultipartFile;
42
43
44
45
46
47
48 class FileAwareMultipartFileTest {
49
50
51
52
53
54
55 @SuppressWarnings({"ConstantConditions", "SimplifiableJUnitAssertion", "EqualsWithItself"})
56 @Test
57 void empty() throws Exception {
58 MultipartFile file = FileAwareMultipartFile.empty();
59 assertNotNull(file);
60 assertTrue(file.isEmpty());
61
62 file = new FileAwareMultipartFile(file);
63 assertNotNull(file);
64 assertTrue(file.isEmpty());
65
66 Resource resource0 = file.getResource();
67 assertNotNull(resource0);
68 assertNotNull(resource0.getDescription());
69 assertNotNull(resource0.getInputStream());
70
71 file = new FileAwareMultipartFile(null, System.getProperty("java.io.tmpdir"));
72 assertNotNull(file);
73 assertTrue(file.isEmpty());
74
75 assertFalse(resource0.equals(null));
76 assertFalse(resource0.equals(new Object()));
77 assertTrue(resource0.equals(resource0));
78 Resource resource1 = file.getResource();
79 assertTrue(resource0.equals(resource1));
80 assertEquals(resource0.hashCode(), resource1.hashCode());
81 assertEquals(resource0.toString(), resource1.toString());
82 }
83
84
85
86
87
88
89 @Test
90 void delete() throws Exception {
91 File file = File.createTempFile(
92 "junit",
93 ".txt",
94 new File(System.getProperty("java.io.tmpdir")));
95 try(FileWriter fileWriter = new FileWriter(file)) {
96 FileCopyUtils.copy("Hello", fileWriter);
97 MultipartFile multipartFile = new FileAwareMultipartFile(
98 file,
99 "foo",
100 file.getName(),
101 MediaType.TEXT_PLAIN_VALUE);
102 FileAwareMultipartFile.delete(multipartFile);
103 assertFalse(file.exists());
104
105 } finally {
106 if (file.exists()) {
107 Files.delete(file.toPath());
108 }
109 }
110 }
111
112
113
114
115
116
117 @Test
118 void getName() throws Exception {
119 MultipartFile file = new FileAwareMultipartFile(
120 new ByteArrayInputStream("Hello".getBytes(StandardCharsets.UTF_8)),
121 "foo",
122 "bar.txt",
123 MediaType.TEXT_PLAIN_VALUE);
124 try {
125 assertEquals("foo", file.getName());
126 } finally {
127 FileAwareMultipartFile.delete(file);
128 }
129 }
130
131
132
133
134
135
136 @Test
137 void getOriginalFilename() throws Exception {
138 MultipartFile file = new FileAwareMultipartFile(
139 new ByteArrayInputStream("Hello".getBytes(StandardCharsets.UTF_8)),
140 System.getProperty("java.io.tmpdir"),
141 "foo",
142 "bar.txt",
143 MediaType.TEXT_PLAIN_VALUE);
144 try {
145 assertEquals("bar.txt", file.getOriginalFilename());
146 } finally {
147 FileAwareMultipartFile.delete(file);
148 }
149 }
150
151
152
153
154
155
156 @Test
157 void getContentType() throws Exception {
158 Path file = Files
159 .createTempFile(Path.of(System.getProperty("java.io.tmpdir")), "junit", ".txt");
160 MultipartFile multipartFile = null;
161 try {
162 FileCopyUtils.copy(
163 "Hello".getBytes(StandardCharsets.UTF_8),
164 Files.newOutputStream(
165 file,
166 StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING));
167 multipartFile = new FileAwareMultipartFile(
168 file,
169 "foo",
170 "bar.txt",
171 MediaType.TEXT_PLAIN_VALUE);
172 assertEquals(MediaType.TEXT_PLAIN_VALUE, multipartFile.getContentType());
173
174 } finally {
175 Optional.ofNullable(multipartFile).ifPresent(FileAwareMultipartFile::delete);
176 }
177 }
178
179
180
181
182 @Test
183 void isEmpty() {
184 assertTrue(new FileAwareMultipartFile((File) null, null, null, null).isEmpty());
185 }
186
187
188
189
190
191
192 @Test
193 void getSize() throws Exception {
194 byte[] content = "Hello".getBytes(StandardCharsets.UTF_8);
195 MultipartFile file = new FileAwareMultipartFile(
196 new ByteArrayInputStream(content),
197 "foo",
198 "bar.txt",
199 MediaType.TEXT_PLAIN_VALUE);
200 try {
201 assertEquals(content.length, (int) file.getSize());
202 } finally {
203 FileAwareMultipartFile.delete(file);
204 }
205 }
206
207
208
209
210
211
212 @Test
213 void getBytes() throws Exception {
214 byte[] content = "Hello".getBytes(StandardCharsets.UTF_8);
215 MultipartFile file = new FileAwareMultipartFile(
216 new ByteArrayInputStream(content),
217 "foo",
218 "bar.txt",
219 MediaType.TEXT_PLAIN_VALUE);
220 try {
221 assertArrayEquals(content, file.getBytes());
222 } finally {
223 FileAwareMultipartFile.delete(file);
224 }
225 }
226
227
228
229
230
231
232 @Test
233 void getInputStream() throws Exception {
234 byte[] content = "Hello".getBytes(StandardCharsets.UTF_8);
235 MultipartFile file = new FileAwareMultipartFile(
236 new ByteArrayInputStream(content),
237 "foo",
238 "bar.txt",
239 MediaType.TEXT_PLAIN_VALUE);
240 try (InputStream in = file.getInputStream()) {
241 ByteArrayOutputStream out = new ByteArrayOutputStream();
242 FileCopyUtils.copy(in, out);
243 assertArrayEquals(content, out.toByteArray());
244
245 } finally {
246 FileAwareMultipartFile.delete(file);
247 }
248 }
249
250
251
252
253
254
255 @Test
256 void getResource() throws Exception {
257 byte[] content = "Hello".getBytes(StandardCharsets.UTF_8);
258 MultipartFile file = new FileAwareMultipartFile(
259 new ByteArrayInputStream(content),
260 "foo",
261 "bar.txt",
262 MediaType.TEXT_PLAIN_VALUE);
263 try {
264 Resource resource = file.getResource();
265 assertNotNull(resource);
266 assertTrue(resource.isFile());
267
268 } finally {
269 FileAwareMultipartFile.delete(file);
270 }
271 }
272
273
274
275
276
277
278 @Test
279 void transferTo() throws Exception {
280 byte[] content = "Hello".getBytes(StandardCharsets.UTF_8);
281 MultipartFile tmp = mock(MultipartFile.class);
282 when(tmp.getInputStream()).thenReturn(new ByteArrayInputStream(content));
283 when(tmp.getName()).thenReturn("foo");
284 when(tmp.getOriginalFilename()).thenReturn("bar.txt");
285 when(tmp.getContentType()).thenReturn(MediaType.TEXT_PLAIN_VALUE);
286 when(tmp.getSize()).thenReturn((long) content.length);
287 when(tmp.isEmpty()).thenReturn(false);
288 MultipartFile file = new FileAwareMultipartFile(tmp);
289 File destFile = null;
290 try {
291 destFile = File.createTempFile(
292 "junit",
293 ".txt",
294 new File(System.getProperty("java.io.tmpdir")));
295 file.transferTo(destFile);
296 assertArrayEquals(content, FileCopyUtils.copyToByteArray(destFile));
297
298 } finally {
299 FileAwareMultipartFile.delete(file);
300 if (destFile != null) {
301 Files.delete(destFile.toPath());
302 }
303 }
304 }
305
306
307
308
309
310
311 @SuppressWarnings({"ConstantConditions", "SimplifiableJUnitAssertion", "EqualsWithItself"})
312 @Test
313 void equalsAndHashCode() throws Exception {
314 MultipartFile file0 = new FileAwareMultipartFile(
315 new ByteArrayInputStream("Hello".getBytes(StandardCharsets.UTF_8)),
316 "foo",
317 "bar.txt",
318 MediaType.TEXT_PLAIN_VALUE);
319 assertFalse(file0.equals(null));
320 assertFalse(file0.equals(new Object()));
321 assertTrue(file0.equals(file0));
322 MultipartFile file1 = new FileAwareMultipartFile(file0);
323 assertTrue(file0.equals(file1));
324 assertEquals(file0.hashCode(), file1.hashCode());
325 assertEquals(file0.toString(), file1.toString());
326 }
327 }