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 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  /** Tests the progress listener.
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  	 * Parse a very long file upload by using a progress listener.
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                   * This used to be
95                   *     assertEquals((byte) j, (byte) istream.read());
96                   * but this seems to trigger a bug in JRockit, so
97                   * we express the same like this:
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 }