1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.fileupload;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertNotNull;
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
25
26 import java.io.ByteArrayInputStream;
27 import java.io.ByteArrayOutputStream;
28 import java.io.File;
29 import java.io.IOException;
30 import java.io.ObjectInputStream;
31 import java.io.ObjectOutputStream;
32 import java.io.OutputStream;
33 import java.nio.file.InvalidPathException;
34
35 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
36 import org.apache.commons.io.FileUtils;
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.Test;
40
41
42
43
44 public class DiskFileItemSerializeTest {
45
46
47 private static final File REPO = new File(System.getProperty("java.io.tmpdir"), "diskfileitemrepo");
48
49
50
51
52 private static final String CONTENT_TYPE_TEXT = "text/plain";
53
54
55
56
57 private static final int THRESHOLD = 16;
58
59
60
61
62 private void compareBytes(final String text, final byte[] origBytes, final byte[] newBytes) {
63 assertNotNull("origBytes must not be null", origBytes);
64 assertNotNull("newBytes must not be null", newBytes);
65 assertEquals(text + " byte[] length", origBytes.length, newBytes.length);
66 for (int i = 0; i < origBytes.length; i++) {
67 assertEquals(text + " byte[" + i + "]", origBytes[i], newBytes[i]);
68 }
69 }
70
71
72
73
74 private byte[] createContentBytes(final int size) {
75 final StringBuilder buffer = new StringBuilder(size);
76 byte count = 0;
77 for (int i = 0; i < size; i++) {
78 buffer.append(count + "");
79 count++;
80 if (count > 9) {
81 count = 0;
82 }
83 }
84 return buffer.toString().getBytes();
85 }
86
87
88
89
90
91
92 private FileItem createFileItem(final byte[] contentBytes) throws IOException {
93 return createFileItem(contentBytes, REPO);
94 }
95
96
97
98
99
100
101 private FileItem createFileItem(final byte[] contentBytes, final File repository) throws IOException {
102 final FileItemFactory factory = new DiskFileItemFactory(THRESHOLD, repository);
103 final String textFieldName = "textField";
104 final FileItem item = factory.createItem(textFieldName, CONTENT_TYPE_TEXT, true, "My File Name");
105 try (OutputStream os = item.getOutputStream()) {
106 os.write(contentBytes);
107 }
108 return item;
109 }
110
111
112
113
114 private Object deserialize(final ByteArrayOutputStream baos) throws Exception {
115 Object result = null;
116 final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
117 final ObjectInputStream ois = new ObjectInputStream(bais);
118 result = ois.readObject();
119 bais.close();
120 return result;
121 }
122
123
124
125
126 private ByteArrayOutputStream serialize(final Object target) throws Exception {
127 final ByteArrayOutputStream baos = new ByteArrayOutputStream();
128 try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
129 oos.writeObject(target);
130 oos.flush();
131 }
132 return baos;
133 }
134
135 @Before
136 public void setUp() throws Exception {
137 if (REPO.exists()) {
138 FileUtils.deleteDirectory(REPO);
139 }
140 FileUtils.forceMkdir(REPO);
141 }
142
143 @After
144 public void tearDown() throws IOException {
145 for (final File file : FileUtils.listFiles(REPO, null, true)) {
146 System.out.println("Found leftover file " + file);
147 }
148 FileUtils.deleteDirectory(REPO);
149 }
150
151
152
153
154
155
156 @Test
157 public void testAboveThreshold() throws IOException {
158
159 final byte[] testFieldValueBytes = createContentBytes(THRESHOLD + 1);
160 final FileItem item = createFileItem(testFieldValueBytes);
161
162 assertFalse("Initial: in memory", item.isInMemory());
163 assertEquals("Initial: size", item.getSize(), testFieldValueBytes.length);
164 compareBytes("Initial", item.get(), testFieldValueBytes);
165 item.delete();
166 }
167
168
169
170
171
172
173 @Test
174 public void testBelowThreshold() throws IOException {
175
176 final byte[] testFieldValueBytes = createContentBytes(THRESHOLD - 1);
177 testInMemoryObject(testFieldValueBytes);
178 }
179
180
181
182
183
184
185 private void testInMemoryObject(final byte[] testFieldValueBytes) throws IOException {
186 testInMemoryObject(testFieldValueBytes, REPO);
187 }
188
189
190
191
192
193
194 private void testInMemoryObject(final byte[] testFieldValueBytes, final File repository) throws IOException {
195 final FileItem item = createFileItem(testFieldValueBytes, repository);
196
197 assertTrue("Initial: in memory", item.isInMemory());
198 assertEquals("Initial: size", item.getSize(), testFieldValueBytes.length);
199 compareBytes("Initial", item.get(), testFieldValueBytes);
200 item.delete();
201 }
202
203
204
205
206 @Test(expected = IOException.class)
207 public void testInvalidRepository() throws Exception {
208
209 final byte[] testFieldValueBytes = createContentBytes(THRESHOLD);
210 final File repository = new File(System.getProperty("java.io.tmpdir"), "file");
211 final FileItem item = createFileItem(testFieldValueBytes, repository);
212 deserialize(serialize(item));
213 }
214
215
216
217
218 @Test(expected = InvalidPathException.class)
219 public void testInvalidRepositoryWithNullChar() throws Exception {
220
221 final byte[] testFieldValueBytes = createContentBytes(THRESHOLD);
222 final File repository = new File(System.getProperty("java.io.tmpdir"), "\0");
223 final FileItem item = createFileItem(testFieldValueBytes, repository);
224 deserialize(serialize(item));
225 }
226
227
228
229
230
231
232 @Test
233 public void testThreshold() throws IOException {
234
235 final byte[] testFieldValueBytes = createContentBytes(THRESHOLD);
236 testInMemoryObject(testFieldValueBytes);
237 }
238
239
240
241
242
243
244 @Test
245 public void testValidRepository() throws IOException {
246
247 final byte[] testFieldValueBytes = createContentBytes(THRESHOLD);
248 testInMemoryObject(testFieldValueBytes, REPO);
249 }
250 }