View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Tests the {@link ProgressListener}.
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       * Parse a very long file upload by using a progress listener.
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                  * This used to be
110                  *     assertEquals((byte) j, (byte) istream.read());
111                  * but this seems to trigger a bug in JRockit, so
112                  * we express the same like this:
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 }