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