1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.fileupload;
18
19 import static org.junit.Assert.*;
20
21 import java.io.File;
22 import java.util.List;
23
24 import javax.servlet.http.HttpServletRequest;
25
26 import org.apache.commons.fileupload.disk.DiskFileItem;
27 import org.junit.Before;
28 import org.junit.Test;
29
30
31
32
33
34
35 @SuppressWarnings({"deprecation"})
36 public class DiskFileUploadTest {
37
38 private DiskFileUpload upload;
39
40 @Before
41 public void setUp() {
42 upload = new DiskFileUpload();
43 }
44
45 @Test
46 public void testWithInvalidRequest() {
47 HttpServletRequest req = HttpServletRequestFactory.createInvalidHttpServletRequest();
48
49 try {
50 upload.parseRequest(req);
51 fail("testWithInvalidRequest: expected exception was not thrown");
52 } catch (FileUploadException expected) {
53
54 }
55 }
56
57 @Test
58 public void testWithNullContentType() {
59 HttpServletRequest req = HttpServletRequestFactory.createHttpServletRequestWithNullContentType();
60
61 try {
62 upload.parseRequest(req);
63 fail("testWithNullContentType: expected exception was not thrown");
64 } catch (DiskFileUpload.InvalidContentTypeException expected) {
65
66 } catch (FileUploadException unexpected) {
67 fail("testWithNullContentType: unexpected exception was thrown");
68 }
69 }
70
71
72
73 @Test
74 public void testMoveFile() throws Exception {
75 DiskFileUpload myUpload = new DiskFileUpload();
76 myUpload.setSizeThreshold(0);
77 final String content =
78 "-----1234\r\n" +
79 "Content-Disposition: form-data; name=\"file\";"
80 + "filename=\"foo.tab\"\r\n" +
81 "Content-Type: text/whatever\r\n" +
82 "\r\n" +
83 "This is the content of the file\n" +
84 "\r\n" +
85 "-----1234--\r\n";
86 final byte[] contentBytes = content.getBytes("US-ASCII");
87 final HttpServletRequest request = new MockHttpServletRequest(contentBytes, Constants.CONTENT_TYPE);
88 final List<FileItem> items = myUpload.parseRequest(request);
89 assertNotNull(items);
90 assertFalse(items.isEmpty());
91 final DiskFileItem dfi = (DiskFileItem) items.get(0);
92 final File out = File.createTempFile("install", ".tmp");
93 dfi.write(out);
94 }
95 }