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