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 */
017package org.apache.commons.fileupload2.core;
018
019import static org.junit.jupiter.api.Assertions.assertNotNull;
020import static org.junit.jupiter.api.Assertions.assertThrows;
021
022import java.io.ByteArrayInputStream;
023import java.io.InputStream;
024
025import org.junit.jupiter.api.Test;
026
027/**
028 * Unit tests {@link MultipartInput}.
029 */
030public class MultipartStreamTest {
031
032    static private final String BOUNDARY_TEXT = "myboundary";
033
034    @Test
035    public void testSmallBuffer() {
036        final var strData = "foobar";
037        final var contents = strData.getBytes();
038        final InputStream input = new ByteArrayInputStream(contents);
039        final var boundary = BOUNDARY_TEXT.getBytes();
040        final var iBufSize = 1;
041        assertThrows(IllegalArgumentException.class, () -> MultipartInput.builder().setInputStream(input).setBoundary(boundary).setBufferSize(iBufSize)
042                .setProgressNotifier(new MultipartInput.ProgressNotifier(null, contents.length)).get());
043    }
044
045    @Test
046    public void testThreeParamConstructor() throws Exception {
047        final var strData = "foobar";
048        final var contents = strData.getBytes();
049        final InputStream input = new ByteArrayInputStream(contents);
050        final var boundary = BOUNDARY_TEXT.getBytes();
051        final var iBufSize = boundary.length + MultipartInput.BOUNDARY_PREFIX.length + 1;
052        final var ms = MultipartInput.builder().setInputStream(input).setBoundary(boundary).setBufferSize(iBufSize)
053                .setProgressNotifier(new MultipartInput.ProgressNotifier(null, contents.length)).get();
054        assertNotNull(ms);
055    }
056
057    @Test
058    public void testTwoParamConstructor() throws Exception {
059        final var strData = "foobar";
060        final var contents = strData.getBytes();
061        final InputStream input = new ByteArrayInputStream(contents);
062        final var boundary = BOUNDARY_TEXT.getBytes();
063        final var ms = MultipartInput.builder().setInputStream(input).setBoundary(boundary)
064                .setProgressNotifier(new MultipartInput.ProgressNotifier(null, contents.length)).get();
065        assertNotNull(ms);
066    }
067
068}