001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.fileupload;
018    
019    import java.io.UnsupportedEncodingException;
020    import java.util.List;
021    
022    import javax.servlet.http.HttpServletRequest;
023    
024    import org.apache.commons.fileupload.disk.DiskFileItemFactory;
025    import org.apache.commons.fileupload.servlet.ServletFileUpload;
026    
027    import junit.framework.TestCase;
028    
029    
030    /**
031     * Base class for deriving test cases.
032     */
033    public abstract class FileUploadTestCase extends TestCase {
034            protected static final String CONTENT_TYPE = "multipart/form-data; boundary=---1234";
035    
036            protected List parseUpload(byte[] bytes) throws FileUploadException {
037            return parseUpload(bytes, CONTENT_TYPE);
038        }
039    
040            protected List parseUpload(byte[] bytes, String contentType) throws FileUploadException {
041                    ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
042            HttpServletRequest request = new MockHttpServletRequest(bytes, contentType);
043    
044            List fileItems = upload.parseRequest(request);
045            return fileItems;
046            }
047    
048            protected List parseUpload(String content)
049            throws UnsupportedEncodingException, FileUploadException
050        {
051                    byte[] bytes = content.getBytes("US-ASCII");
052                    return parseUpload(bytes, CONTENT_TYPE);
053        }
054    }