This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-fileupload.git
commit cad46d5cc2182fe0b16a460a6e6615edbca3a216 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Sun Jul 9 18:26:16 2023 -0400 Create convenience typed subclasses for disk access --- .../jakarta/JakartaServletDiskFileUpload.java | 36 +++++ .../jakarta/JakartaProgressListenerDiskTest.java | 44 ++++++ .../jakarta/JakartaServletFileUploadDiskTest.java | 117 +++++++++++++++ .../fileupload2/jakarta/JakartaSizesDiskTest.java | 43 ++++++ .../jakarta/JakartaStreamingDiskTest.java | 53 +++++++ .../javax/JavaxServletDiskFileUpload.java | 36 +++++ .../javax/JavaxProgressListenerDiskTest.java | 44 ++++++ .../javax/JavaxServletFileUploadDiskTest.java | 163 +++++++++++++++++++++ .../fileupload2/javax/JavaxSizesDiskTest.java | 43 ++++++ .../fileupload2/javax/JavaxStreamingDiskTest.java | 53 +++++++ 10 files changed, 632 insertions(+) diff --git a/commons-fileupload2-jakarta/src/main/java/org/apache/commons/fileupload2/jakarta/JakartaServletDiskFileUpload.java b/commons-fileupload2-jakarta/src/main/java/org/apache/commons/fileupload2/jakarta/JakartaServletDiskFileUpload.java new file mode 100644 index 0000000..8158062 --- /dev/null +++ b/commons-fileupload2-jakarta/src/main/java/org/apache/commons/fileupload2/jakarta/JakartaServletDiskFileUpload.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.fileupload2.jakarta; + +import org.apache.commons.fileupload2.core.disk.DiskFileItem; +import org.apache.commons.fileupload2.core.disk.DiskFileItemFactory; + +/** + * A JakartaServletFileUpload for {@link DiskFileItem} and {@link DiskFileItemFactory}. + */ +public class JakartaServletDiskFileUpload extends JakartaServletFileUpload<DiskFileItem, DiskFileItemFactory> { + + public JakartaServletDiskFileUpload() { + super(DiskFileItemFactory.builder().get()); + } + + public JakartaServletDiskFileUpload(final DiskFileItemFactory fileItemFactory) { + super(fileItemFactory); + } + +} diff --git a/commons-fileupload2-jakarta/src/test/java/org/apache/commons/fileupload2/jakarta/JakartaProgressListenerDiskTest.java b/commons-fileupload2-jakarta/src/test/java/org/apache/commons/fileupload2/jakarta/JakartaProgressListenerDiskTest.java new file mode 100644 index 0000000..98e192b --- /dev/null +++ b/commons-fileupload2-jakarta/src/test/java/org/apache/commons/fileupload2/jakarta/JakartaProgressListenerDiskTest.java @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.fileupload2.jakarta; + +import java.io.InputStream; + +import org.apache.commons.fileupload2.core.AbstractProgressListenerTest; +import org.apache.commons.fileupload2.core.ProgressListener; +import org.apache.commons.fileupload2.core.disk.DiskFileItem; +import org.apache.commons.fileupload2.core.disk.DiskFileItemFactory; + +import jakarta.servlet.http.HttpServletRequest; + +/** + * Tests the {@link ProgressListener}. + */ +public class JakartaProgressListenerDiskTest extends + AbstractProgressListenerTest<JakartaServletFileUpload<DiskFileItem, DiskFileItemFactory>, HttpServletRequest, DiskFileItem, DiskFileItemFactory> { + + @Override + protected JakartaServletDiskFileUpload newFileUpload() { + return new JakartaServletDiskFileUpload(); + } + + @Override + protected HttpServletRequest newMockHttpServletRequest(final InputStream request, final long requestLength, final String contentType, final int readLimit) { + return new JakartaMockHttpServletRequest(request, requestLength, contentType, readLimit); + } + +} diff --git a/commons-fileupload2-jakarta/src/test/java/org/apache/commons/fileupload2/jakarta/JakartaServletFileUploadDiskTest.java b/commons-fileupload2-jakarta/src/test/java/org/apache/commons/fileupload2/jakarta/JakartaServletFileUploadDiskTest.java new file mode 100644 index 0000000..2810d29 --- /dev/null +++ b/commons-fileupload2-jakarta/src/test/java/org/apache/commons/fileupload2/jakarta/JakartaServletFileUploadDiskTest.java @@ -0,0 +1,117 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.fileupload2.jakarta; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.nio.charset.StandardCharsets; +import java.util.List; +import java.util.Map; + +import org.apache.commons.fileupload2.core.AbstractFileUploadTest; +import org.apache.commons.fileupload2.core.Constants; +import org.apache.commons.fileupload2.core.FileUploadException; +import org.apache.commons.fileupload2.core.disk.DiskFileItem; +import org.apache.commons.fileupload2.core.disk.DiskFileItemFactory; +import org.junit.jupiter.api.Test; + +import jakarta.servlet.http.HttpServletRequest; + +/** + * Tests {@link JakartaServletFileUpload} and + * + * @see AbstractFileUploadTest + */ +public class JakartaServletFileUploadDiskTest extends AbstractFileUploadTest<JakartaServletDiskFileUpload, HttpServletRequest, DiskFileItem, DiskFileItemFactory> { + + public JakartaServletFileUploadDiskTest() { + super(new JakartaServletDiskFileUpload()); + } + + @Test + public void parseImpliedUtf8() throws Exception { + // utf8 encoded form-data without explicit content-type encoding + // @formatter:off + final String text = "-----1234\r\n" + + "Content-Disposition: form-data; name=\"utf8Html\"\r\n" + + "\r\n" + + "Thís ís the coñteñt of the fíle\n" + + "\r\n" + + "-----1234--\r\n"; + // @formatter:on + + final byte[] bytes = text.getBytes(StandardCharsets.UTF_8); + final HttpServletRequest request = new JakartaMockServletHttpRequest(bytes, Constants.CONTENT_TYPE); + // @formatter:off + final DiskFileItemFactory fileItemFactory = DiskFileItemFactory.builder() + .setCharset(StandardCharsets.UTF_8) + .get(); + // @formatter:on + final JakartaServletDiskFileUpload upload = new JakartaServletDiskFileUpload(fileItemFactory); + final List<DiskFileItem> fileItems = upload.parseRequest(request); + final DiskFileItem fileItem = fileItems.get(0); + assertTrue(fileItem.getString().contains("coñteñt"), fileItem.getString()); + } + + /* + * Test case for <a href="https://issues.apache.org/jira/browse/FILEUPLOAD-210"> + */ + @Test + public void parseParameterMap() throws Exception { + // @formatter:off + final String text = "-----1234\r\n" + + "Content-Disposition: form-data; name=\"file\"; filename=\"foo.tab\"\r\n" + + "Content-Type: text/whatever\r\n" + + "\r\n" + + "This is the content of the file\n" + + "\r\n" + + "-----1234\r\n" + + "Content-Disposition: form-data; name=\"field\"\r\n" + + "\r\n" + + "fieldValue\r\n" + + "-----1234\r\n" + + "Content-Disposition: form-data; name=\"multi\"\r\n" + + "\r\n" + + "value1\r\n" + + "-----1234\r\n" + + "Content-Disposition: form-data; name=\"multi\"\r\n" + + "\r\n" + + "value2\r\n" + + "-----1234--\r\n"; + // @formatter:on + final byte[] bytes = text.getBytes(StandardCharsets.US_ASCII); + final HttpServletRequest request = new JakartaMockServletHttpRequest(bytes, Constants.CONTENT_TYPE); + + final JakartaServletDiskFileUpload upload = new JakartaServletDiskFileUpload(); + final Map<String, List<DiskFileItem>> mappedParameters = upload.parseParameterMap(request); + assertTrue(mappedParameters.containsKey("file")); + assertEquals(1, mappedParameters.get("file").size()); + + assertTrue(mappedParameters.containsKey("field")); + assertEquals(1, mappedParameters.get("field").size()); + + assertTrue(mappedParameters.containsKey("multi")); + assertEquals(2, mappedParameters.get("multi").size()); + } + + @Override + public List<DiskFileItem> parseUpload(final JakartaServletDiskFileUpload upload, final byte[] bytes, final String contentType) throws FileUploadException { + final HttpServletRequest request = new JakartaMockHttpServletRequest(bytes, contentType); + return upload.parseRequest(new JakartaServletRequestContext(request)); + } +} diff --git a/commons-fileupload2-jakarta/src/test/java/org/apache/commons/fileupload2/jakarta/JakartaSizesDiskTest.java b/commons-fileupload2-jakarta/src/test/java/org/apache/commons/fileupload2/jakarta/JakartaSizesDiskTest.java new file mode 100644 index 0000000..03115c2 --- /dev/null +++ b/commons-fileupload2-jakarta/src/test/java/org/apache/commons/fileupload2/jakarta/JakartaSizesDiskTest.java @@ -0,0 +1,43 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.fileupload2.jakarta; + +import java.io.InputStream; + +import org.apache.commons.fileupload2.core.AbstractSizesTest; +import org.apache.commons.fileupload2.core.disk.DiskFileItem; +import org.apache.commons.fileupload2.core.disk.DiskFileItemFactory; + +import jakarta.servlet.http.HttpServletRequest; + +/** + * Unit test for items with varying sizes. + */ +public class JakartaSizesDiskTest extends AbstractSizesTest<JakartaServletDiskFileUpload, HttpServletRequest, DiskFileItem, DiskFileItemFactory> { + + @Override + protected JakartaServletDiskFileUpload newFileUpload() { + return new JakartaServletDiskFileUpload(); + } + + @Override + protected JakartaMockHttpServletRequest newMockHttpServletRequest(final InputStream request, final long requestLength, final String contentType, + final int readLimit) { + return new JakartaMockHttpServletRequest(request, requestLength, contentType, readLimit); + } + +} diff --git a/commons-fileupload2-jakarta/src/test/java/org/apache/commons/fileupload2/jakarta/JakartaStreamingDiskTest.java b/commons-fileupload2-jakarta/src/test/java/org/apache/commons/fileupload2/jakarta/JakartaStreamingDiskTest.java new file mode 100644 index 0000000..ba6b782 --- /dev/null +++ b/commons-fileupload2-jakarta/src/test/java/org/apache/commons/fileupload2/jakarta/JakartaStreamingDiskTest.java @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.fileupload2.jakarta; + +import java.io.InputStream; + +import org.apache.commons.fileupload2.core.AbstractStreamingTest; +import org.apache.commons.fileupload2.core.disk.DiskFileItem; +import org.apache.commons.fileupload2.core.disk.DiskFileItemFactory; + +import jakarta.servlet.http.HttpServletRequest; + +/** + * Unit test for items with varying sizes. + */ +public class JakartaStreamingDiskTest + extends AbstractStreamingTest<JakartaServletDiskFileUpload, HttpServletRequest, JakartaServletRequestContext, DiskFileItem, DiskFileItemFactory> { + + @Override + protected DiskFileItemFactory newDiskFileItemFactory() { + return DiskFileItemFactory.builder().get(); + } + + @Override + protected JakartaServletDiskFileUpload newFileUpload() { + return new JakartaServletDiskFileUpload(); + } + + @Override + protected HttpServletRequest newMockHttpServletRequest(final InputStream request, final long requestLength, final String contentType, final int readLimit) { + return new JakartaMockHttpServletRequest(request, requestLength, contentType, readLimit); + } + + @Override + protected JakartaServletRequestContext newServletRequestContext(final HttpServletRequest request) { + return new JakartaServletRequestContext(request); + } + +} diff --git a/commons-fileupload2-javax/src/main/java/org/apache/commons/fileupload2/javax/JavaxServletDiskFileUpload.java b/commons-fileupload2-javax/src/main/java/org/apache/commons/fileupload2/javax/JavaxServletDiskFileUpload.java new file mode 100644 index 0000000..ddbaecb --- /dev/null +++ b/commons-fileupload2-javax/src/main/java/org/apache/commons/fileupload2/javax/JavaxServletDiskFileUpload.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.fileupload2.javax; + +import org.apache.commons.fileupload2.core.disk.DiskFileItem; +import org.apache.commons.fileupload2.core.disk.DiskFileItemFactory; + +/** + * A JavaxServletFileUpload for {@link DiskFileItem} and {@link DiskFileItemFactory}. + */ +public class JavaxServletDiskFileUpload extends JavaxServletFileUpload<DiskFileItem, DiskFileItemFactory> { + + public JavaxServletDiskFileUpload() { + super(DiskFileItemFactory.builder().get()); + } + + public JavaxServletDiskFileUpload(final DiskFileItemFactory fileItemFactory) { + super(fileItemFactory); + } + +} diff --git a/commons-fileupload2-javax/src/test/java/org/apache/commons/fileupload2/javax/JavaxProgressListenerDiskTest.java b/commons-fileupload2-javax/src/test/java/org/apache/commons/fileupload2/javax/JavaxProgressListenerDiskTest.java new file mode 100644 index 0000000..a5323b6 --- /dev/null +++ b/commons-fileupload2-javax/src/test/java/org/apache/commons/fileupload2/javax/JavaxProgressListenerDiskTest.java @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.fileupload2.javax; + +import java.io.InputStream; + +import javax.servlet.http.HttpServletRequest; + +import org.apache.commons.fileupload2.core.AbstractProgressListenerTest; +import org.apache.commons.fileupload2.core.ProgressListener; +import org.apache.commons.fileupload2.core.disk.DiskFileItem; +import org.apache.commons.fileupload2.core.disk.DiskFileItemFactory; + +/** + * Tests the {@link ProgressListener}. + */ +public class JavaxProgressListenerDiskTest + extends AbstractProgressListenerTest<JavaxServletDiskFileUpload, HttpServletRequest, DiskFileItem, DiskFileItemFactory> { + + @Override + protected JavaxServletDiskFileUpload newFileUpload() { + return new JavaxServletDiskFileUpload(); + } + + @Override + protected HttpServletRequest newMockHttpServletRequest(final InputStream request, final long requestLength, final String contentType, final int readLimit) { + return new JavaxMockHttpServletRequest(request, requestLength, contentType, readLimit); + } + +} diff --git a/commons-fileupload2-javax/src/test/java/org/apache/commons/fileupload2/javax/JavaxServletFileUploadDiskTest.java b/commons-fileupload2-javax/src/test/java/org/apache/commons/fileupload2/javax/JavaxServletFileUploadDiskTest.java new file mode 100644 index 0000000..12880cc --- /dev/null +++ b/commons-fileupload2-javax/src/test/java/org/apache/commons/fileupload2/javax/JavaxServletFileUploadDiskTest.java @@ -0,0 +1,163 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.fileupload2.javax; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; + +import org.apache.commons.fileupload2.core.AbstractFileUploadTest; +import org.apache.commons.fileupload2.core.Constants; +import org.apache.commons.fileupload2.core.FileUploadException; +import org.apache.commons.fileupload2.core.disk.DiskFileItem; +import org.apache.commons.fileupload2.core.disk.DiskFileItemFactory; +import org.junit.jupiter.api.Test; + +/** + * Tests {@link JavaxServletFileUpload}. + * + * @see AbstractFileUploadTest + */ +public class JavaxServletFileUploadDiskTest extends AbstractFileUploadTest<JavaxServletDiskFileUpload, HttpServletRequest, DiskFileItem, DiskFileItemFactory> { + + public JavaxServletFileUploadDiskTest() { + super(new JavaxServletDiskFileUpload()); + } + + @Test + public void parseImpliedUtf8() throws Exception { + // utf8 encoded form-data without explicit content-type encoding + // @formatter:off + final String text = "-----1234\r\n" + + "Content-Disposition: form-data; name=\"utf8Html\"\r\n" + + "\r\n" + + "Thís ís the coñteñt of the fíle\n" + + "\r\n" + + "-----1234--\r\n"; + // @formatter:on + + final byte[] bytes = text.getBytes(StandardCharsets.UTF_8); + final HttpServletRequest request = new JavaxMockHttpServletRequest(bytes, Constants.CONTENT_TYPE); + // @formatter:off + final DiskFileItemFactory fileItemFactory = DiskFileItemFactory.builder() + .setCharset(StandardCharsets.UTF_8) + .get(); + // @formatter:on + final JavaxServletFileUpload<DiskFileItem, DiskFileItemFactory> upload = new JavaxServletFileUpload<>(fileItemFactory); + final List<DiskFileItem> fileItems = upload.parseRequest(request); + final DiskFileItem fileItem = fileItems.get(0); + assertTrue(fileItem.getString().contains("coñteñt"), fileItem.getString()); + } + + /* + * Test case for <a href="https://issues.apache.org/jira/browse/FILEUPLOAD-210"> + */ + @Test + public void parseParameterMap() throws Exception { + // @formatter:off + final String text = "-----1234\r\n" + + "Content-Disposition: form-data; name=\"file\"; filename=\"foo.tab\"\r\n" + + "Content-Type: text/whatever\r\n" + + "\r\n" + + "This is the content of the file\n" + + "\r\n" + + "-----1234\r\n" + + "Content-Disposition: form-data; name=\"field\"\r\n" + + "\r\n" + + "fieldValue\r\n" + + "-----1234\r\n" + + "Content-Disposition: form-data; name=\"multi\"\r\n" + + "\r\n" + + "value1\r\n" + + "-----1234\r\n" + + "Content-Disposition: form-data; name=\"multi\"\r\n" + + "\r\n" + + "value2\r\n" + + "-----1234--\r\n"; + // @formatter:on + final byte[] bytes = text.getBytes(StandardCharsets.US_ASCII); + final HttpServletRequest request = new JavaxMockHttpServletRequest(bytes, Constants.CONTENT_TYPE); + + final JavaxServletFileUpload<DiskFileItem, DiskFileItemFactory> upload = new JavaxServletFileUpload<>(DiskFileItemFactory.builder().get()); + final Map<String, List<DiskFileItem>> mappedParameters = upload.parseParameterMap(request); + assertTrue(mappedParameters.containsKey("file")); + assertEquals(1, mappedParameters.get("file").size()); + + assertTrue(mappedParameters.containsKey("field")); + assertEquals(1, mappedParameters.get("field").size()); + + assertTrue(mappedParameters.containsKey("multi")); + assertEquals(2, mappedParameters.get("multi").size()); + } + + @Override + public List<DiskFileItem> parseUpload(final JavaxServletDiskFileUpload upload, final byte[] bytes, final String contentType) throws FileUploadException { + final HttpServletRequest request = new JavaxMockHttpServletRequest(bytes, contentType); + return upload.parseRequest(new JavaxServletRequestContext(request)); + } + + /** + * Runs a test with varying file sizes. + */ + @Override + @Test + public void testFileUpload() throws IOException, FileUploadException { + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); + int add = 16; + int num = 0; + for (int i = 0; i < 16384; i += add) { + if (++add == 32) { + add = 16; + } + final String header = "-----1234\r\n" + "Content-Disposition: form-data; name=\"field" + (num++) + "\"\r\n" + "\r\n"; + baos.write(header.getBytes(StandardCharsets.US_ASCII)); + for (int j = 0; j < i; j++) { + baos.write((byte) j); + } + baos.write("\r\n".getBytes(StandardCharsets.US_ASCII)); + } + baos.write("-----1234--\r\n".getBytes(StandardCharsets.US_ASCII)); + + final List<DiskFileItem> fileItems = parseUpload(new JavaxServletDiskFileUpload(), baos.toByteArray()); + final Iterator<DiskFileItem> fileIter = fileItems.iterator(); + add = 16; + num = 0; + for (int i = 0; i < 16384; i += add) { + if (++add == 32) { + add = 16; + } + final DiskFileItem item = fileIter.next(); + assertEquals("field" + (num++), item.getFieldName()); + final byte[] bytes = item.get(); + assertEquals(i, bytes.length); + for (int j = 0; j < i; j++) { + assertEquals((byte) j, bytes[j]); + } + } + assertTrue(!fileIter.hasNext()); + } + +} diff --git a/commons-fileupload2-javax/src/test/java/org/apache/commons/fileupload2/javax/JavaxSizesDiskTest.java b/commons-fileupload2-javax/src/test/java/org/apache/commons/fileupload2/javax/JavaxSizesDiskTest.java new file mode 100644 index 0000000..8477791 --- /dev/null +++ b/commons-fileupload2-javax/src/test/java/org/apache/commons/fileupload2/javax/JavaxSizesDiskTest.java @@ -0,0 +1,43 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.fileupload2.javax; + +import java.io.InputStream; + +import javax.servlet.http.HttpServletRequest; + +import org.apache.commons.fileupload2.core.AbstractSizesTest; +import org.apache.commons.fileupload2.core.disk.DiskFileItem; +import org.apache.commons.fileupload2.core.disk.DiskFileItemFactory; + +/** + * Unit test for items with varying sizes. + */ +public class JavaxSizesDiskTest extends AbstractSizesTest<JavaxServletDiskFileUpload, HttpServletRequest, DiskFileItem, DiskFileItemFactory> { + + @Override + protected JavaxServletDiskFileUpload newFileUpload() { + return new JavaxServletDiskFileUpload(); + } + + @Override + protected JavaxMockHttpServletRequest newMockHttpServletRequest(final InputStream request, final long requestLength, final String contentType, + final int readLimit) { + return new JavaxMockHttpServletRequest(request, requestLength, contentType, readLimit); + } + +} diff --git a/commons-fileupload2-javax/src/test/java/org/apache/commons/fileupload2/javax/JavaxStreamingDiskTest.java b/commons-fileupload2-javax/src/test/java/org/apache/commons/fileupload2/javax/JavaxStreamingDiskTest.java new file mode 100644 index 0000000..7010efa --- /dev/null +++ b/commons-fileupload2-javax/src/test/java/org/apache/commons/fileupload2/javax/JavaxStreamingDiskTest.java @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.fileupload2.javax; + +import java.io.InputStream; + +import javax.servlet.http.HttpServletRequest; + +import org.apache.commons.fileupload2.core.AbstractStreamingTest; +import org.apache.commons.fileupload2.core.disk.DiskFileItem; +import org.apache.commons.fileupload2.core.disk.DiskFileItemFactory; + +/** + * Unit test for items with varying sizes. + */ +public class JavaxStreamingDiskTest + extends AbstractStreamingTest<JavaxServletDiskFileUpload, HttpServletRequest, JavaxServletRequestContext, DiskFileItem, DiskFileItemFactory> { + + @Override + protected DiskFileItemFactory newDiskFileItemFactory() { + return DiskFileItemFactory.builder().get(); + } + + @Override + protected JavaxServletDiskFileUpload newFileUpload() { + return new JavaxServletDiskFileUpload(); + } + + @Override + protected HttpServletRequest newMockHttpServletRequest(final InputStream request, final long requestLength, final String contentType, final int readLimit) { + return new JavaxMockHttpServletRequest(request, requestLength, contentType, readLimit); + } + + @Override + protected JavaxServletRequestContext newServletRequestContext(final HttpServletRequest request) { + return new JavaxServletRequestContext(request); + } + +}