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.assertNotNull;
    20
    21  import java.io.ByteArrayInputStream;
    22  import java.io.InputStream;
    23
    24  import org.junit.Test;
    25
    26  /**
    27   * Unit tests {@link org.apache.commons.fileupload.MultipartStream}.
    28   */
    29  public class MultipartStreamTest {
    30
    31      static private final String BOUNDARY_TEXT = "myboundary";
    32
    33      @Test
    34      public void testThreeParamConstructor() throws Exception {
    35          final String strData = "foobar";
    36          final byte[] contents = strData.getBytes();
    37          InputStream input = new ByteArrayInputStream(contents);
    38          byte[] boundary = BOUNDARY_TEXT.getBytes();
    39          int iBufSize =
    40                  boundary.length + MultipartStream.BOUNDARY_PREFIX.length + 1;
    41          MultipartStream ms = new MultipartStream(
    42                  input,
    43                  boundary,
    44                  iBufSize,
    45                  new MultipartStream.ProgressNotifier(null, contents.length));
    46          assertNotNull(ms);
    47      }
    48
    49      @Test(expected=IllegalArgumentException.class)
    50      public void testSmallBuffer() throws Exception {
    51          final String strData = "foobar";
    52          final byte[] contents = strData.getBytes();
    53          InputStream input = new ByteArrayInputStream(contents);
    54          byte[] boundary = BOUNDARY_TEXT.getBytes();
    55          int iBufSize = 1;
    56          new MultipartStream(
    57                  input,
    58                  boundary,
    59                  iBufSize,
    60                  new MultipartStream.ProgressNotifier(null, contents.length));
    61      }
    62
    63      @Test
    64      public void testTwoParamConstructor() throws Exception {
    65          final String strData = "foobar";
    66          final byte[] contents = strData.getBytes();
    67          InputStream input = new ByteArrayInputStream(contents);
    68          byte[] boundary = BOUNDARY_TEXT.getBytes();
    69          MultipartStream ms = new MultipartStream(
    70                  input,
    71                  boundary,
    72                  new MultipartStream.ProgressNotifier(null, contents.length));
    73          assertNotNull(ms);
    74      }
    75
    76  }