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.*;
    20
    21  import java.io.File;
    22  import java.util.List;
    23
    24  import javax.servlet.http.HttpServletRequest;
    25
    26  import org.apache.commons.fileupload.disk.DiskFileItem;
    27  import org.junit.Before;
    28  import org.junit.Test;
    29
    30  /**
    31   * Test for {@link DiskFileUpload}. Remove when deprecated class is removed.
    32   *
    33   * @since 1.4
    34   */
    35  @SuppressWarnings({"deprecation"}) // unit tests for deprecated class
    36  public class DiskFileUploadTest {
    37
    38      private DiskFileUpload upload;
    39
    40      @Before
    41      public void setUp() {
    42          upload = new DiskFileUpload();
    43      }
    44
    45      @Test
    46      public void testWithInvalidRequest() {
    47          HttpServletRequest req = HttpServletRequestFactory.createInvalidHttpServletRequest();
    48
    49          try {
    50              upload.parseRequest(req);
    51              fail("testWithInvalidRequest: expected exception was not thrown");
    52          } catch (FileUploadException expected) {
    53              // this exception is expected
    54          }
    55      }
    56
    57      @Test
    58      public void testWithNullContentType() {
    59          HttpServletRequest req = HttpServletRequestFactory.createHttpServletRequestWithNullContentType();
    60
    61          try {
    62              upload.parseRequest(req);
    63              fail("testWithNullContentType: expected exception was not thrown");
    64          } catch (DiskFileUpload.InvalidContentTypeException expected) {
    65              // this exception is expected
    66          } catch (FileUploadException unexpected) {
    67              fail("testWithNullContentType: unexpected exception was thrown");
    68          }
    69      }
    70
    71      /** Proposed test for FILEUPLOAD-293. As of yet, doesn't reproduce the problem.
    72       */
    73      @Test
    74      public void testMoveFile() throws Exception {
    75          DiskFileUpload myUpload = new DiskFileUpload();
    76          myUpload.setSizeThreshold(0);
    77      	final String content =
    78                  "-----1234\r\n" +
    79                  "Content-Disposition: form-data; name=\"file\";"
    80                  		+ "filename=\"foo.tab\"\r\n" +
    81                  "Content-Type: text/whatever\r\n" +
    82                  "\r\n" +
    83                  "This is the content of the file\n" +
    84                  "\r\n" +
    85                  "-----1234--\r\n";
    86      	final byte[] contentBytes = content.getBytes("US-ASCII");
    87          final HttpServletRequest request = new MockHttpServletRequest(contentBytes, Constants.CONTENT_TYPE);
    88          final List<FileItem> items = myUpload.parseRequest(request);
    89          assertNotNull(items);
    90          assertFalse(items.isEmpty());
    91          final DiskFileItem dfi = (DiskFileItem) items.get(0);
    92          final File out = File.createTempFile("install", ".tmp");
    93          dfi.write(out);
    94      }
    95  }