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