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 java.io.ByteArrayOutputStream;
20 import java.io.IOException;
21 import java.io.InputStream;
22
23 import org.apache.commons.fileupload.ProgressListener;
24 import org.apache.commons.fileupload.servlet.ServletFileUpload;
25
26
27
28
29 public class ProgressListenerTest extends FileUploadTestCase {
30 private class ProgressListenerImpl implements ProgressListener {
31 private final long expectedContentLength;
32 private final int expectedItems;
33 private Long bytesRead;
34 private Integer items;
35 ProgressListenerImpl(long pContentLength, int pItems) {
36 expectedContentLength = pContentLength;
37 expectedItems = pItems;
38 }
39 public void update(long pBytesRead, long pContentLength, int pItems) {
40 assertTrue(pBytesRead >= 0 && pBytesRead <= expectedContentLength);
41 assertTrue(pContentLength == -1 || pContentLength == expectedContentLength);
42 assertTrue(pItems >= 0 && pItems <= expectedItems);
43
44 assertTrue(bytesRead == null || pBytesRead >= bytesRead.longValue());
45 bytesRead = new Long(pBytesRead);
46 assertTrue(items == null || pItems >= items.intValue());
47 items = new Integer(pItems);
48 }
49 void checkFinished(){
50 assertEquals(expectedContentLength, bytesRead.longValue());
51 assertEquals(expectedItems, items.intValue());
52 }
53 }
54
55
56
57
58 public void testProgressListener() throws Exception {
59 final int NUM_ITEMS = 512;
60 ByteArrayOutputStream baos = new ByteArrayOutputStream();
61 for (int i = 0; i < NUM_ITEMS; i++) {
62 String header = "-----1234\r\n"
63 + "Content-Disposition: form-data; name=\"field" + (i+1) + "\"\r\n"
64 + "\r\n";
65 baos.write(header.getBytes("US-ASCII"));
66 for (int j = 0; j < 16384+i; j++) {
67 baos.write((byte) j);
68 }
69 baos.write("\r\n".getBytes("US-ASCII"));
70 }
71 baos.write("-----1234--\r\n".getBytes("US-ASCII"));
72 byte[] contents = baos.toByteArray();
73
74 MockHttpServletRequest request = new MockHttpServletRequest(contents, "multipart/form-data; boundary=---1234");
75 runTest(NUM_ITEMS, contents.length, request);
76 request = new MockHttpServletRequest(contents, "multipart/form-data; boundary=---1234"){
77 public int getContentLength() {
78 return -1;
79 }
80 };
81 runTest(NUM_ITEMS, contents.length, request);
82 }
83
84 private void runTest(final int NUM_ITEMS, long pContentLength, MockHttpServletRequest request) throws FileUploadException, IOException {
85 ServletFileUpload upload = new ServletFileUpload();
86 ProgressListenerImpl listener = new ProgressListenerImpl(pContentLength, NUM_ITEMS);
87 upload.setProgressListener(listener);
88 FileItemIterator iter = upload.getItemIterator(request);
89 for (int i = 0; i < NUM_ITEMS; i++) {
90 FileItemStream stream = iter.next();
91 InputStream istream = stream.openStream();
92 for (int j = 0; j < 16384+i; j++) {
93
94
95
96
97
98
99 byte b1 = (byte) j;
100 byte b2 = (byte) istream.read();
101 if (b1 != b2) {
102 fail("Expected " + b1 + ", got " + b2);
103 }
104 }
105 assertEquals(-1, istream.read());
106 }
107 assertTrue(!iter.hasNext());
108 listener.checkFinished();
109 }
110 }