You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ki...@apache.org on 2017/06/25 05:05:05 UTC
[1/2] commons-fileupload git commit: FILEUPLOAD-283: add tests for
the portlet package
Repository: commons-fileupload
Updated Branches:
refs/heads/master 94e5e1a77 -> 86a79bcea
FILEUPLOAD-283: add tests for the portlet package
Project: http://git-wip-us.apache.org/repos/asf/commons-fileupload/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-fileupload/commit/f8f529ee
Tree: http://git-wip-us.apache.org/repos/asf/commons-fileupload/tree/f8f529ee
Diff: http://git-wip-us.apache.org/repos/asf/commons-fileupload/diff/f8f529ee
Branch: refs/heads/master
Commit: f8f529ee0baecc9262317cc5375af1eb42aadab4
Parents: b148d5f
Author: Bruno P. Kinoshita <br...@yahoo.com.br>
Authored: Sun Jun 11 17:23:07 2017 +1200
Committer: Bruno P. Kinoshita <br...@yahoo.com.br>
Committed: Sat Jun 17 11:05:42 2017 +1200
----------------------------------------------------------------------
.../apache/commons/fileupload/Constants.java | 32 ++
.../commons/fileupload/DiskFileUploadTest.java | 67 +++
.../commons/fileupload/FileUploadTest.java | 399 +++++++++++++++++
.../commons/fileupload/FileUploadTestCase.java | 52 ---
.../fileupload/MockHttpServletRequest.java | 2 +-
.../commons/fileupload/MultipartStreamTest.java | 1 -
.../fileupload/ProgressListenerTest.java | 15 +-
.../fileupload/ServletFileUploadTest.java | 446 -------------------
.../apache/commons/fileupload/SizesTest.java | 30 +-
.../org/apache/commons/fileupload/Util.java | 63 +++
.../portlet/MockPortletActionRequest.java | 271 +++++++++++
.../portlet/PortletFileUploadTest.java | 86 ++++
.../servlet/ServletFileUploadTest.java | 89 ++++
13 files changed, 1033 insertions(+), 520 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-fileupload/blob/f8f529ee/src/test/java/org/apache/commons/fileupload/Constants.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/fileupload/Constants.java b/src/test/java/org/apache/commons/fileupload/Constants.java
new file mode 100644
index 0000000..69eaa53
--- /dev/null
+++ b/src/test/java/org/apache/commons/fileupload/Constants.java
@@ -0,0 +1,32 @@
+/*
+ * 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.fileupload;
+
+/**
+ * Constants used for testing.
+ *
+ * @since 1.4
+ */
+public final class Constants {
+
+ /**
+ * The content type used in several tests.
+ */
+ public static final String CONTENT_TYPE = "multipart/form-data; boundary=---1234";
+
+ private Constants() {}
+}
http://git-wip-us.apache.org/repos/asf/commons-fileupload/blob/f8f529ee/src/test/java/org/apache/commons/fileupload/DiskFileUploadTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/fileupload/DiskFileUploadTest.java b/src/test/java/org/apache/commons/fileupload/DiskFileUploadTest.java
new file mode 100644
index 0000000..49f65f0
--- /dev/null
+++ b/src/test/java/org/apache/commons/fileupload/DiskFileUploadTest.java
@@ -0,0 +1,67 @@
+/*
+ * 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.fileupload;
+
+import static org.junit.Assert.fail;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Test for {@link DiskFileUpload}. Remove when deprecated class is removed.
+ *
+ * @since 1.4
+ */
+@SuppressWarnings({"deprecation"}) // unit tests for deprecated class
+public class DiskFileUploadTest {
+
+ private DiskFileUpload upload;
+
+ @Before
+ public void setUp() {
+ upload = new DiskFileUpload();
+ }
+
+ @Test
+ public void testWithInvalidRequest() {
+ HttpServletRequest req = HttpServletRequestFactory.createInvalidHttpServletRequest();
+
+ try {
+ upload.parseRequest(req);
+ fail("testWithInvalidRequest: expected exception was not thrown");
+ } catch (FileUploadException expected) {
+ // this exception is expected
+ }
+ }
+
+ @Test
+ public void testWithNullContentType() {
+ HttpServletRequest req = HttpServletRequestFactory.createHttpServletRequestWithNullContentType();
+
+ try {
+ upload.parseRequest(req);
+ fail("testWithNullContentType: expected exception was not thrown");
+ } catch (DiskFileUpload.InvalidContentTypeException expected) {
+ // this exception is expected
+ } catch (FileUploadException unexpected) {
+ fail("testWithNullContentType: unexpected exception was thrown");
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-fileupload/blob/f8f529ee/src/test/java/org/apache/commons/fileupload/FileUploadTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/fileupload/FileUploadTest.java b/src/test/java/org/apache/commons/fileupload/FileUploadTest.java
new file mode 100644
index 0000000..a795c7e
--- /dev/null
+++ b/src/test/java/org/apache/commons/fileupload/FileUploadTest.java
@@ -0,0 +1,399 @@
+/*
+ * 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.fileupload;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.util.List;
+
+import org.apache.commons.fileupload.portlet.PortletFileUploadTest;
+import org.apache.commons.fileupload.servlet.ServletFileUploadTest;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameter;
+import org.junit.runners.Parameterized.Parameters;
+
+/**
+ * Common tests for implementations of {@link FileUpload}. This is a parameterized test.
+ * Tests must be valid and common to all implementations of FileUpload added as parameter
+ * in this class.
+ *
+ * @see ServletFileUploadTest
+ * @see PortletFileUploadTest
+ * @since 1.4
+ */
+@RunWith(Parameterized.class)
+public class FileUploadTest {
+
+ /**
+ * @return {@link FileUpload} classes under test.
+ */
+ @Parameters(name="{0}")
+ public static Iterable<? extends Object> data() {
+ return Util.fileUploadImplementations();
+ }
+
+ /**
+ * Current parameterized FileUpload.
+ */
+ @Parameter
+ public FileUpload upload;
+
+ // --- Test methods common to all implementations of a FileUpload
+
+ @Test
+ public void testFileUpload()
+ throws IOException, FileUploadException {
+ List<FileItem> fileItems = Util.parseUpload(upload,
+ "-----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");
+ assertEquals(4, fileItems.size());
+
+ FileItem file = fileItems.get(0);
+ assertEquals("file", file.getFieldName());
+ assertFalse(file.isFormField());
+ assertEquals("This is the content of the file\n", file.getString());
+ assertEquals("text/whatever", file.getContentType());
+ assertEquals("foo.tab", file.getName());
+
+ FileItem field = fileItems.get(1);
+ assertEquals("field", field.getFieldName());
+ assertTrue(field.isFormField());
+ assertEquals("fieldValue", field.getString());
+
+ FileItem multi0 = fileItems.get(2);
+ assertEquals("multi", multi0.getFieldName());
+ assertTrue(multi0.isFormField());
+ assertEquals("value1", multi0.getString());
+
+ FileItem multi1 = fileItems.get(3);
+ assertEquals("multi", multi1.getFieldName());
+ assertTrue(multi1.isFormField());
+ assertEquals("value2", multi1.getString());
+ }
+
+ @Test
+ public void testFilenameCaseSensitivity()
+ throws IOException, FileUploadException {
+ List<FileItem> fileItems = Util.parseUpload(upload,
+ "-----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");
+ assertEquals(1, fileItems.size());
+
+ FileItem file = fileItems.get(0);
+ assertEquals("FiLe", file.getFieldName());
+ assertEquals("FOO.tab", file.getName());
+ }
+
+ /**
+ * This is what the browser does if you submit the form without choosing a file.
+ */
+ @Test
+ public void testEmptyFile()
+ throws UnsupportedEncodingException, FileUploadException {
+ List<FileItem> fileItems = Util.parseUpload (upload,
+ "-----1234\r\n" +
+ "Content-Disposition: form-data; name=\"file\"; filename=\"\"\r\n" +
+ "\r\n" +
+ "\r\n" +
+ "-----1234--\r\n");
+ assertEquals(1, fileItems.size());
+
+ FileItem file = fileItems.get(0);
+ assertFalse(file.isFormField());
+ assertEquals("", file.getString());
+ assertEquals("", file.getName());
+ }
+
+ /**
+ * Internet Explorer 5 for the Mac has a bug where the carriage
+ * return is missing on any boundary line immediately preceding
+ * an input with type=image. (type=submit does not have the bug.)
+ */
+ @Test
+ public void testIE5MacBug()
+ throws UnsupportedEncodingException, FileUploadException {
+ List<FileItem> fileItems = Util.parseUpload(upload,
+ "-----1234\r\n" +
+ "Content-Disposition: form-data; name=\"field1\"\r\n" +
+ "\r\n" +
+ "fieldValue\r\n" +
+ "-----1234\n" + // NOTE \r missing
+ "Content-Disposition: form-data; name=\"submitName.x\"\r\n" +
+ "\r\n" +
+ "42\r\n" +
+ "-----1234\n" + // NOTE \r missing
+ "Content-Disposition: form-data; name=\"submitName.y\"\r\n" +
+ "\r\n" +
+ "21\r\n" +
+ "-----1234\r\n" +
+ "Content-Disposition: form-data; name=\"field2\"\r\n" +
+ "\r\n" +
+ "fieldValue2\r\n" +
+ "-----1234--\r\n");
+
+ assertEquals(4, fileItems.size());
+
+ FileItem field1 = fileItems.get(0);
+ assertEquals("field1", field1.getFieldName());
+ assertTrue(field1.isFormField());
+ assertEquals("fieldValue", field1.getString());
+
+ FileItem submitX = fileItems.get(1);
+ assertEquals("submitName.x", submitX.getFieldName());
+ assertTrue(submitX.isFormField());
+ assertEquals("42", submitX.getString());
+
+ FileItem submitY = fileItems.get(2);
+ assertEquals("submitName.y", submitY.getFieldName());
+ assertTrue(submitY.isFormField());
+ assertEquals("21", submitY.getString());
+
+ FileItem field2 = fileItems.get(3);
+ assertEquals("field2", field2.getFieldName());
+ assertTrue(field2.isFormField());
+ assertEquals("fieldValue2", field2.getString());
+ }
+
+ /**
+ * Test for <a href="http://issues.apache.org/jira/browse/FILEUPLOAD-62">FILEUPLOAD-62</a>
+ */
+ @Test
+ public void testFILEUPLOAD62() throws Exception {
+ final String contentType = "multipart/form-data; boundary=AaB03x";
+ final String request =
+ "--AaB03x\r\n" +
+ "content-disposition: form-data; name=\"field1\"\r\n" +
+ "\r\n" +
+ "Joe Blow\r\n" +
+ "--AaB03x\r\n" +
+ "content-disposition: form-data; name=\"pics\"\r\n" +
+ "Content-type: multipart/mixed; boundary=BbC04y\r\n" +
+ "\r\n" +
+ "--BbC04y\r\n" +
+ "Content-disposition: attachment; filename=\"file1.txt\"\r\n" +
+ "Content-Type: text/plain\r\n" +
+ "\r\n" +
+ "... contents of file1.txt ...\r\n" +
+ "--BbC04y\r\n" +
+ "Content-disposition: attachment; filename=\"file2.gif\"\r\n" +
+ "Content-type: image/gif\r\n" +
+ "Content-Transfer-Encoding: binary\r\n" +
+ "\r\n" +
+ "...contents of file2.gif...\r\n" +
+ "--BbC04y--\r\n" +
+ "--AaB03x--";
+ List<FileItem> fileItems = Util.parseUpload(upload, request.getBytes("US-ASCII"), contentType);
+ assertEquals(3, fileItems.size());
+ FileItem item0 = fileItems.get(0);
+ assertEquals("field1", item0.getFieldName());
+ assertNull(item0.getName());
+ assertEquals("Joe Blow", new String(item0.get()));
+ FileItem item1 = fileItems.get(1);
+ assertEquals("pics", item1.getFieldName());
+ assertEquals("file1.txt", item1.getName());
+ assertEquals("... contents of file1.txt ...", new String(item1.get()));
+ FileItem item2 = fileItems.get(2);
+ assertEquals("pics", item2.getFieldName());
+ assertEquals("file2.gif", item2.getName());
+ assertEquals("...contents of file2.gif...", new String(item2.get()));
+ }
+
+ /**
+ * Test for <a href="http://issues.apache.org/jira/browse/FILEUPLOAD-111">FILEUPLOAD-111</a>
+ */
+ @Test
+ public void testFoldedHeaders()
+ throws IOException, FileUploadException {
+ List<FileItem> fileItems = Util.parseUpload(upload, "-----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; \r\n" +
+ "\tname=\"field\"\r\n" +
+ "\r\n" +
+ "fieldValue\r\n" +
+ "-----1234\r\n" +
+ "Content-Disposition: form-data;\r\n" +
+ " 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");
+ assertEquals(4, fileItems.size());
+
+ FileItem file = fileItems.get(0);
+ assertEquals("file", file.getFieldName());
+ assertFalse(file.isFormField());
+ assertEquals("This is the content of the file\n", file.getString());
+ assertEquals("text/whatever", file.getContentType());
+ assertEquals("foo.tab", file.getName());
+
+ FileItem field = fileItems.get(1);
+ assertEquals("field", field.getFieldName());
+ assertTrue(field.isFormField());
+ assertEquals("fieldValue", field.getString());
+
+ FileItem multi0 = fileItems.get(2);
+ assertEquals("multi", multi0.getFieldName());
+ assertTrue(multi0.isFormField());
+ assertEquals("value1", multi0.getString());
+
+ FileItem multi1 = fileItems.get(3);
+ assertEquals("multi", multi1.getFieldName());
+ assertTrue(multi1.isFormField());
+ assertEquals("value2", multi1.getString());
+ }
+
+ /**
+ * Test case for <a href="http://issues.apache.org/jira/browse/FILEUPLOAD-130">
+ */
+ @Test
+ public void testFileUpload130()
+ throws Exception {
+ final String[] headerNames = new String[]
+ {
+ "SomeHeader", "OtherHeader", "YetAnotherHeader", "WhatAHeader"
+ };
+ final String[] headerValues = new String[]
+ {
+ "present", "Is there", "Here", "Is That"
+ };
+ List<FileItem> fileItems = Util.parseUpload(upload,
+ "-----1234\r\n" +
+ "Content-Disposition: form-data; name=\"file\"; filename=\"foo.tab\"\r\n" +
+ "Content-Type: text/whatever\r\n" +
+ headerNames[0] + ": " + headerValues[0] + "\r\n" +
+ "\r\n" +
+ "This is the content of the file\n" +
+ "\r\n" +
+ "-----1234\r\n" +
+ "Content-Disposition: form-data; \r\n" +
+ "\tname=\"field\"\r\n" +
+ headerNames[1] + ": " + headerValues[1] + "\r\n" +
+ "\r\n" +
+ "fieldValue\r\n" +
+ "-----1234\r\n" +
+ "Content-Disposition: form-data;\r\n" +
+ " name=\"multi\"\r\n" +
+ headerNames[2] + ": " + headerValues[2] + "\r\n" +
+ "\r\n" +
+ "value1\r\n" +
+ "-----1234\r\n" +
+ "Content-Disposition: form-data; name=\"multi\"\r\n" +
+ headerNames[3] + ": " + headerValues[3] + "\r\n" +
+ "\r\n" +
+ "value2\r\n" +
+ "-----1234--\r\n");
+ assertEquals(4, fileItems.size());
+
+ FileItem file = fileItems.get(0);
+ assertHeaders(headerNames, headerValues, file, 0);
+
+ FileItem field = fileItems.get(1);
+ assertHeaders(headerNames, headerValues, field, 1);
+
+ FileItem multi0 = fileItems.get(2);
+ assertHeaders(headerNames, headerValues, multi0, 2);
+
+ FileItem multi1 = fileItems.get(3);
+ assertHeaders(headerNames, headerValues, multi1, 3);
+ }
+
+ /**
+ * Test for <a href="http://issues.apache.org/jira/browse/FILEUPLOAD-239">FILEUPLOAD-239</a>
+ */
+ @Test
+ public void testContentTypeAttachment()
+ throws IOException, FileUploadException {
+ List<FileItem> fileItems = Util.parseUpload(upload,
+ "-----1234\r\n" +
+ "content-disposition: form-data; name=\"field1\"\r\n" +
+ "\r\n" +
+ "Joe Blow\r\n" +
+ "-----1234\r\n" +
+ "content-disposition: form-data; name=\"pics\"\r\n" +
+ "Content-type: multipart/mixed, boundary=---9876\r\n" +
+ "\r\n" +
+ "-----9876\r\n" +
+ "Content-disposition: attachment; filename=\"file1.txt\"\r\n" +
+ "Content-Type: text/plain\r\n" +
+ "\r\n" +
+ "... contents of file1.txt ...\r\n" +
+ "-----9876--\r\n" +
+ "-----1234--\r\n");
+ assertEquals(2, fileItems.size());
+
+ FileItem field = fileItems.get(0);
+ assertEquals("field1", field.getFieldName());
+ assertTrue(field.isFormField());
+ assertEquals("Joe Blow", field.getString());
+
+ FileItem file = fileItems.get(1);
+ assertEquals("pics", file.getFieldName());
+ assertFalse(file.isFormField());
+ assertEquals("... contents of file1.txt ...", file.getString());
+ assertEquals("text/plain", file.getContentType());
+ assertEquals("file1.txt", file.getName());
+ }
+
+ private void assertHeaders(String[] pHeaderNames, String[] pHeaderValues,
+ FileItem pItem, int pIndex) {
+ for (int i = 0; i < pHeaderNames.length; i++) {
+ final String value = pItem.getHeaders().getHeader(pHeaderNames[i]);
+ if (i == pIndex) {
+ assertEquals(pHeaderValues[i], value);
+ } else {
+ assertNull(value);
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/commons-fileupload/blob/f8f529ee/src/test/java/org/apache/commons/fileupload/FileUploadTestCase.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/fileupload/FileUploadTestCase.java b/src/test/java/org/apache/commons/fileupload/FileUploadTestCase.java
deleted file mode 100644
index f93ebb4..0000000
--- a/src/test/java/org/apache/commons/fileupload/FileUploadTestCase.java
+++ /dev/null
@@ -1,52 +0,0 @@
-/*
- * 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.fileupload;
-
-import java.io.UnsupportedEncodingException;
-import java.util.List;
-
-import javax.servlet.http.HttpServletRequest;
-
-import org.apache.commons.fileupload.disk.DiskFileItemFactory;
-import org.apache.commons.fileupload.servlet.ServletFileUpload;
-
-/**
- * Base class for deriving test cases.
- */
-public abstract class FileUploadTestCase {
-
- protected static final String CONTENT_TYPE = "multipart/form-data; boundary=---1234";
-
- protected List<FileItem> parseUpload(byte[] bytes) throws FileUploadException {
- return parseUpload(bytes, CONTENT_TYPE);
- }
-
- protected List<FileItem> parseUpload(byte[] bytes, String contentType) throws FileUploadException {
- ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
- HttpServletRequest request = new MockHttpServletRequest(bytes, contentType);
-
- List<FileItem> fileItems = upload.parseRequest(request);
- return fileItems;
- }
-
- protected List<FileItem> parseUpload(String content)
- throws UnsupportedEncodingException, FileUploadException {
- byte[] bytes = content.getBytes("US-ASCII");
- return parseUpload(bytes, CONTENT_TYPE);
- }
-
-}
http://git-wip-us.apache.org/repos/asf/commons-fileupload/blob/f8f529ee/src/test/java/org/apache/commons/fileupload/MockHttpServletRequest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/fileupload/MockHttpServletRequest.java b/src/test/java/org/apache/commons/fileupload/MockHttpServletRequest.java
index 0d04ed1..7df2bdd 100644
--- a/src/test/java/org/apache/commons/fileupload/MockHttpServletRequest.java
+++ b/src/test/java/org/apache/commons/fileupload/MockHttpServletRequest.java
@@ -32,7 +32,7 @@ import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
-class MockHttpServletRequest implements HttpServletRequest {
+public class MockHttpServletRequest implements HttpServletRequest {
private final InputStream m_requestData;
http://git-wip-us.apache.org/repos/asf/commons-fileupload/blob/f8f529ee/src/test/java/org/apache/commons/fileupload/MultipartStreamTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/fileupload/MultipartStreamTest.java b/src/test/java/org/apache/commons/fileupload/MultipartStreamTest.java
index a7d3eec..edbc370 100644
--- a/src/test/java/org/apache/commons/fileupload/MultipartStreamTest.java
+++ b/src/test/java/org/apache/commons/fileupload/MultipartStreamTest.java
@@ -46,7 +46,6 @@ public class MultipartStreamTest {
assertNotNull(ms);
}
- @SuppressWarnings("unused")
@Test(expected=IllegalArgumentException.class)
public void testSmallBuffer() throws Exception {
final String strData = "foobar";
http://git-wip-us.apache.org/repos/asf/commons-fileupload/blob/f8f529ee/src/test/java/org/apache/commons/fileupload/ProgressListenerTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/fileupload/ProgressListenerTest.java b/src/test/java/org/apache/commons/fileupload/ProgressListenerTest.java
index 30aa1de..721498f 100644
--- a/src/test/java/org/apache/commons/fileupload/ProgressListenerTest.java
+++ b/src/test/java/org/apache/commons/fileupload/ProgressListenerTest.java
@@ -23,14 +23,15 @@ import static org.junit.Assert.fail;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.junit.Test;
/**
- * Tests the progress listener.
+ * Tests the {@link ProgressListener}.
*/
-public class ProgressListenerTest extends FileUploadTestCase {
+public class ProgressListenerTest {
private class ProgressListenerImpl implements ProgressListener {
@@ -76,18 +77,18 @@ public class ProgressListenerTest extends FileUploadTestCase {
String header = "-----1234\r\n"
+ "Content-Disposition: form-data; name=\"field" + (i+1) + "\"\r\n"
+ "\r\n";
- baos.write(header.getBytes("US-ASCII"));
+ baos.write(header.getBytes(StandardCharsets.US_ASCII.name()));
for (int j = 0; j < 16384+i; j++) {
baos.write((byte) j);
}
- baos.write("\r\n".getBytes("US-ASCII"));
+ baos.write("\r\n".getBytes(StandardCharsets.US_ASCII.name()));
}
- baos.write("-----1234--\r\n".getBytes("US-ASCII"));
+ baos.write("-----1234--\r\n".getBytes(StandardCharsets.US_ASCII.name()));
byte[] contents = baos.toByteArray();
- MockHttpServletRequest request = new MockHttpServletRequest(contents, "multipart/form-data; boundary=---1234");
+ MockHttpServletRequest request = new MockHttpServletRequest(contents, Constants.CONTENT_TYPE);
runTest(NUM_ITEMS, contents.length, request);
- request = new MockHttpServletRequest(contents, "multipart/form-data; boundary=---1234"){
+ request = new MockHttpServletRequest(contents, Constants.CONTENT_TYPE){
@Override
public int getContentLength() {
return -1;
http://git-wip-us.apache.org/repos/asf/commons-fileupload/blob/f8f529ee/src/test/java/org/apache/commons/fileupload/ServletFileUploadTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/fileupload/ServletFileUploadTest.java b/src/test/java/org/apache/commons/fileupload/ServletFileUploadTest.java
deleted file mode 100644
index 4b36f56..0000000
--- a/src/test/java/org/apache/commons/fileupload/ServletFileUploadTest.java
+++ /dev/null
@@ -1,446 +0,0 @@
-/*
- * 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.fileupload;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
-
-import java.io.IOException;
-import java.io.UnsupportedEncodingException;
-import java.util.List;
-import java.util.Map;
-
-import javax.servlet.http.HttpServletRequest;
-
-import org.apache.commons.fileupload.disk.DiskFileItemFactory;
-import org.apache.commons.fileupload.servlet.ServletFileUpload;
-import org.junit.Test;
-
-/**
- * Unit tests {@link org.apache.commons.fileupload.DiskFileUpload}.
- */
-@SuppressWarnings({"deprecation", "javadoc"}) // unit tests for deprecated class
-public class ServletFileUploadTest extends FileUploadTestCase {
-
- @Test
- public void testWithInvalidRequest() {
- FileUploadBase fu = null;
-
- fu = new DiskFileUpload();
-
- HttpServletRequest req = HttpServletRequestFactory.createInvalidHttpServletRequest();
-
-
- try {
- fu.parseRequest(req);
- fail("testWithInvalidRequest: expected exception was not thrown");
- } catch (FileUploadException expected) {
- // this exception is expected
- }
- }
-
- @Test
- public void testWithNullContentType() {
- FileUploadBase fu = new DiskFileUpload();
-
- HttpServletRequest req = HttpServletRequestFactory.createHttpServletRequestWithNullContentType();
-
- try {
- fu.parseRequest(req);
- fail("testWithNullContentType: expected exception was not thrown");
- } catch (DiskFileUpload.InvalidContentTypeException expected) {
- // this exception is expected
- } catch (FileUploadException unexpected) {
- fail("testWithNullContentType: unexpected exception was thrown");
- }
- }
-
- @Test
- public void testFileUpload()
- throws IOException, FileUploadException {
- List<FileItem> fileItems = parseUpload("-----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");
- assertEquals(4, fileItems.size());
-
- FileItem file = fileItems.get(0);
- assertEquals("file", file.getFieldName());
- assertFalse(file.isFormField());
- assertEquals("This is the content of the file\n", file.getString());
- assertEquals("text/whatever", file.getContentType());
- assertEquals("foo.tab", file.getName());
-
- FileItem field = fileItems.get(1);
- assertEquals("field", field.getFieldName());
- assertTrue(field.isFormField());
- assertEquals("fieldValue", field.getString());
-
- FileItem multi0 = fileItems.get(2);
- assertEquals("multi", multi0.getFieldName());
- assertTrue(multi0.isFormField());
- assertEquals("value1", multi0.getString());
-
- FileItem multi1 = fileItems.get(3);
- assertEquals("multi", multi1.getFieldName());
- assertTrue(multi1.isFormField());
- assertEquals("value2", multi1.getString());
- }
-
- @Test
- public void testFilenameCaseSensitivity()
- throws IOException, FileUploadException {
- List<FileItem> fileItems = parseUpload("-----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");
- assertEquals(1, fileItems.size());
-
- FileItem file = fileItems.get(0);
- assertEquals("FiLe", file.getFieldName());
- assertEquals("FOO.tab", file.getName());
- }
-
- /**
- * This is what the browser does if you submit the form without choosing a file.
- */
- @Test
- public void testEmptyFile()
- throws UnsupportedEncodingException, FileUploadException {
- List<FileItem> fileItems = parseUpload ("-----1234\r\n" +
- "Content-Disposition: form-data; name=\"file\"; filename=\"\"\r\n" +
- "\r\n" +
- "\r\n" +
- "-----1234--\r\n");
- assertEquals(1, fileItems.size());
-
- FileItem file = fileItems.get(0);
- assertFalse(file.isFormField());
- assertEquals("", file.getString());
- assertEquals("", file.getName());
- }
-
- /**
- * Internet Explorer 5 for the Mac has a bug where the carriage
- * return is missing on any boundary line immediately preceding
- * an input with type=image. (type=submit does not have the bug.)
- */
- @Test
- public void testIE5MacBug()
- throws UnsupportedEncodingException, FileUploadException {
- List<FileItem> fileItems = parseUpload("-----1234\r\n" +
- "Content-Disposition: form-data; name=\"field1\"\r\n" +
- "\r\n" +
- "fieldValue\r\n" +
- "-----1234\n" + // NOTE \r missing
- "Content-Disposition: form-data; name=\"submitName.x\"\r\n" +
- "\r\n" +
- "42\r\n" +
- "-----1234\n" + // NOTE \r missing
- "Content-Disposition: form-data; name=\"submitName.y\"\r\n" +
- "\r\n" +
- "21\r\n" +
- "-----1234\r\n" +
- "Content-Disposition: form-data; name=\"field2\"\r\n" +
- "\r\n" +
- "fieldValue2\r\n" +
- "-----1234--\r\n");
-
- assertEquals(4, fileItems.size());
-
- FileItem field1 = fileItems.get(0);
- assertEquals("field1", field1.getFieldName());
- assertTrue(field1.isFormField());
- assertEquals("fieldValue", field1.getString());
-
- FileItem submitX = fileItems.get(1);
- assertEquals("submitName.x", submitX.getFieldName());
- assertTrue(submitX.isFormField());
- assertEquals("42", submitX.getString());
-
- FileItem submitY = fileItems.get(2);
- assertEquals("submitName.y", submitY.getFieldName());
- assertTrue(submitY.isFormField());
- assertEquals("21", submitY.getString());
-
- FileItem field2 = fileItems.get(3);
- assertEquals("field2", field2.getFieldName());
- assertTrue(field2.isFormField());
- assertEquals("fieldValue2", field2.getString());
- }
-
- /**
- * Test for <a href="http://issues.apache.org/jira/browse/FILEUPLOAD-62">FILEUPLOAD-62</a>
- */
- @Test
- public void testFILEUPLOAD62() throws Exception {
- final String contentType = "multipart/form-data; boundary=AaB03x";
- final String request =
- "--AaB03x\r\n" +
- "content-disposition: form-data; name=\"field1\"\r\n" +
- "\r\n" +
- "Joe Blow\r\n" +
- "--AaB03x\r\n" +
- "content-disposition: form-data; name=\"pics\"\r\n" +
- "Content-type: multipart/mixed; boundary=BbC04y\r\n" +
- "\r\n" +
- "--BbC04y\r\n" +
- "Content-disposition: attachment; filename=\"file1.txt\"\r\n" +
- "Content-Type: text/plain\r\n" +
- "\r\n" +
- "... contents of file1.txt ...\r\n" +
- "--BbC04y\r\n" +
- "Content-disposition: attachment; filename=\"file2.gif\"\r\n" +
- "Content-type: image/gif\r\n" +
- "Content-Transfer-Encoding: binary\r\n" +
- "\r\n" +
- "...contents of file2.gif...\r\n" +
- "--BbC04y--\r\n" +
- "--AaB03x--";
- List<FileItem> fileItems = parseUpload(request.getBytes("US-ASCII"), contentType);
- assertEquals(3, fileItems.size());
- FileItem item0 = fileItems.get(0);
- assertEquals("field1", item0.getFieldName());
- assertNull(item0.getName());
- assertEquals("Joe Blow", new String(item0.get()));
- FileItem item1 = fileItems.get(1);
- assertEquals("pics", item1.getFieldName());
- assertEquals("file1.txt", item1.getName());
- assertEquals("... contents of file1.txt ...", new String(item1.get()));
- FileItem item2 = fileItems.get(2);
- assertEquals("pics", item2.getFieldName());
- assertEquals("file2.gif", item2.getName());
- assertEquals("...contents of file2.gif...", new String(item2.get()));
- }
-
- /**
- * Test for <a href="http://issues.apache.org/jira/browse/FILEUPLOAD-111">FILEUPLOAD-111</a>
- */
- @Test
- public void testFoldedHeaders()
- throws IOException, FileUploadException {
- List<FileItem> fileItems = parseUpload("-----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; \r\n" +
- "\tname=\"field\"\r\n" +
- "\r\n" +
- "fieldValue\r\n" +
- "-----1234\r\n" +
- "Content-Disposition: form-data;\r\n" +
- " 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");
- assertEquals(4, fileItems.size());
-
- FileItem file = fileItems.get(0);
- assertEquals("file", file.getFieldName());
- assertFalse(file.isFormField());
- assertEquals("This is the content of the file\n", file.getString());
- assertEquals("text/whatever", file.getContentType());
- assertEquals("foo.tab", file.getName());
-
- FileItem field = fileItems.get(1);
- assertEquals("field", field.getFieldName());
- assertTrue(field.isFormField());
- assertEquals("fieldValue", field.getString());
-
- FileItem multi0 = fileItems.get(2);
- assertEquals("multi", multi0.getFieldName());
- assertTrue(multi0.isFormField());
- assertEquals("value1", multi0.getString());
-
- FileItem multi1 = fileItems.get(3);
- assertEquals("multi", multi1.getFieldName());
- assertTrue(multi1.isFormField());
- assertEquals("value2", multi1.getString());
- }
-
- /**
- * Test case for <a href="http://issues.apache.org/jira/browse/FILEUPLOAD-130">
- */
- @Test
- public void testFileUpload130()
- throws Exception {
- final String[] headerNames = new String[]
- {
- "SomeHeader", "OtherHeader", "YetAnotherHeader", "WhatAHeader"
- };
- final String[] headerValues = new String[]
- {
- "present", "Is there", "Here", "Is That"
- };
- List<FileItem> fileItems = parseUpload("-----1234\r\n" +
- "Content-Disposition: form-data; name=\"file\"; filename=\"foo.tab\"\r\n" +
- "Content-Type: text/whatever\r\n" +
- headerNames[0] + ": " + headerValues[0] + "\r\n" +
- "\r\n" +
- "This is the content of the file\n" +
- "\r\n" +
- "-----1234\r\n" +
- "Content-Disposition: form-data; \r\n" +
- "\tname=\"field\"\r\n" +
- headerNames[1] + ": " + headerValues[1] + "\r\n" +
- "\r\n" +
- "fieldValue\r\n" +
- "-----1234\r\n" +
- "Content-Disposition: form-data;\r\n" +
- " name=\"multi\"\r\n" +
- headerNames[2] + ": " + headerValues[2] + "\r\n" +
- "\r\n" +
- "value1\r\n" +
- "-----1234\r\n" +
- "Content-Disposition: form-data; name=\"multi\"\r\n" +
- headerNames[3] + ": " + headerValues[3] + "\r\n" +
- "\r\n" +
- "value2\r\n" +
- "-----1234--\r\n");
- assertEquals(4, fileItems.size());
-
- FileItem file = fileItems.get(0);
- assertHeaders(headerNames, headerValues, file, 0);
-
- FileItem field = fileItems.get(1);
- assertHeaders(headerNames, headerValues, field, 1);
-
- FileItem multi0 = fileItems.get(2);
- assertHeaders(headerNames, headerValues, multi0, 2);
-
- FileItem multi1 = fileItems.get(3);
- assertHeaders(headerNames, headerValues, multi1, 3);
- }
-
- /**
- * Test case for <a href="http://issues.apache.org/jira/browse/FILEUPLOAD-210">
- */
- @Test
- public void parseParameterMap()
- throws Exception {
- 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";
- byte[] bytes = text.getBytes("US-ASCII");
- ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
- HttpServletRequest request = new MockHttpServletRequest(bytes, CONTENT_TYPE);
-
- Map<String, List<FileItem>> 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());
- }
-
- /**
- * Test for <a href="http://issues.apache.org/jira/browse/FILEUPLOAD-239">FILEUPLOAD-239</a>
- */
- @Test
- public void testContentTypeAttachment()
- throws IOException, FileUploadException {
- List<FileItem> fileItems = parseUpload(
- "-----1234\r\n" +
- "content-disposition: form-data; name=\"field1\"\r\n" +
- "\r\n" +
- "Joe Blow\r\n" +
- "-----1234\r\n" +
- "content-disposition: form-data; name=\"pics\"\r\n" +
- "Content-type: multipart/mixed, boundary=---9876\r\n" +
- "\r\n" +
- "-----9876\r\n" +
- "Content-disposition: attachment; filename=\"file1.txt\"\r\n" +
- "Content-Type: text/plain\r\n" +
- "\r\n" +
- "... contents of file1.txt ...\r\n" +
- "-----9876--\r\n" +
- "-----1234--\r\n");
- assertEquals(2, fileItems.size());
-
- FileItem field = fileItems.get(0);
- assertEquals("field1", field.getFieldName());
- assertTrue(field.isFormField());
- assertEquals("Joe Blow", field.getString());
-
- FileItem file = fileItems.get(1);
- assertEquals("pics", file.getFieldName());
- assertFalse(file.isFormField());
- assertEquals("... contents of file1.txt ...", file.getString());
- assertEquals("text/plain", file.getContentType());
- assertEquals("file1.txt", file.getName());
- }
-
- private void assertHeaders(String[] pHeaderNames, String[] pHeaderValues,
- FileItem pItem, int pIndex) {
- for (int i = 0; i < pHeaderNames.length; i++) {
- final String value = pItem.getHeaders().getHeader(pHeaderNames[i]);
- if (i == pIndex) {
- assertEquals(pHeaderValues[i], value);
- } else {
- assertNull(value);
- }
- }
- }
-
-}
http://git-wip-us.apache.org/repos/asf/commons-fileupload/blob/f8f529ee/src/test/java/org/apache/commons/fileupload/SizesTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/fileupload/SizesTest.java b/src/test/java/org/apache/commons/fileupload/SizesTest.java
index 806f793..f328d41 100644
--- a/src/test/java/org/apache/commons/fileupload/SizesTest.java
+++ b/src/test/java/org/apache/commons/fileupload/SizesTest.java
@@ -24,6 +24,7 @@ import static org.junit.Assert.fail;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.List;
@@ -39,7 +40,7 @@ import org.junit.Test;
/**
* Unit test for items with varying sizes.
*/
-public class SizesTest extends FileUploadTestCase {
+public class SizesTest {
/**
* Runs a test with varying file sizes.
@@ -65,7 +66,8 @@ public class SizesTest extends FileUploadTestCase {
}
baos.write("-----1234--\r\n".getBytes("US-ASCII"));
- List<FileItem> fileItems = parseUpload(baos.toByteArray());
+ List<FileItem> fileItems =
+ Util.parseUpload(new ServletFileUpload(new DiskFileItemFactory()), baos.toByteArray());
Iterator<FileItem> fileIter = fileItems.iterator();
add = 16;
num = 0;
@@ -100,7 +102,8 @@ public class SizesTest extends FileUploadTestCase {
ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
upload.setFileSizeMax(-1);
- HttpServletRequest req = new MockHttpServletRequest(request.getBytes("US-ASCII"), CONTENT_TYPE);
+ HttpServletRequest req = new MockHttpServletRequest(
+ request.getBytes(StandardCharsets.US_ASCII.name()), Constants.CONTENT_TYPE);
List<FileItem> fileItems = upload.parseRequest(req);
assertEquals(1, fileItems.size());
FileItem item = fileItems.get(0);
@@ -108,7 +111,7 @@ public class SizesTest extends FileUploadTestCase {
upload = new ServletFileUpload(new DiskFileItemFactory());
upload.setFileSizeMax(40);
- req = new MockHttpServletRequest(request.getBytes("US-ASCII"), CONTENT_TYPE);
+ req = new MockHttpServletRequest(request.getBytes(StandardCharsets.US_ASCII.name()), Constants.CONTENT_TYPE);
fileItems = upload.parseRequest(req);
assertEquals(1, fileItems.size());
item = fileItems.get(0);
@@ -116,7 +119,7 @@ public class SizesTest extends FileUploadTestCase {
upload = new ServletFileUpload(new DiskFileItemFactory());
upload.setFileSizeMax(30);
- req = new MockHttpServletRequest(request.getBytes("US-ASCII"), CONTENT_TYPE);
+ req = new MockHttpServletRequest(request.getBytes(StandardCharsets.US_ASCII.name()), Constants.CONTENT_TYPE);
try {
upload.parseRequest(req);
fail("Expected exception.");
@@ -142,7 +145,8 @@ public class SizesTest extends FileUploadTestCase {
ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
upload.setFileSizeMax(-1);
- HttpServletRequest req = new MockHttpServletRequest(request.getBytes("US-ASCII"), CONTENT_TYPE);
+ HttpServletRequest req = new MockHttpServletRequest(
+ request.getBytes(StandardCharsets.US_ASCII.name()), Constants.CONTENT_TYPE);
List<FileItem> fileItems = upload.parseRequest(req);
assertEquals(1, fileItems.size());
FileItem item = fileItems.get(0);
@@ -150,7 +154,7 @@ public class SizesTest extends FileUploadTestCase {
upload = new ServletFileUpload(new DiskFileItemFactory());
upload.setFileSizeMax(40);
- req = new MockHttpServletRequest(request.getBytes("US-ASCII"), CONTENT_TYPE);
+ req = new MockHttpServletRequest(request.getBytes(StandardCharsets.US_ASCII.name()), Constants.CONTENT_TYPE);
fileItems = upload.parseRequest(req);
assertEquals(1, fileItems.size());
item = fileItems.get(0);
@@ -159,7 +163,7 @@ public class SizesTest extends FileUploadTestCase {
// provided Content-Length is larger than the FileSizeMax -> handled by ctor
upload = new ServletFileUpload(new DiskFileItemFactory());
upload.setFileSizeMax(5);
- req = new MockHttpServletRequest(request.getBytes("US-ASCII"), CONTENT_TYPE);
+ req = new MockHttpServletRequest(request.getBytes(StandardCharsets.US_ASCII.name()), Constants.CONTENT_TYPE);
try {
upload.parseRequest(req);
fail("Expected exception.");
@@ -170,7 +174,7 @@ public class SizesTest extends FileUploadTestCase {
// provided Content-Length is wrong, actual content is larger -> handled by LimitedInputStream
upload = new ServletFileUpload(new DiskFileItemFactory());
upload.setFileSizeMax(15);
- req = new MockHttpServletRequest(request.getBytes("US-ASCII"), CONTENT_TYPE);
+ req = new MockHttpServletRequest(request.getBytes(StandardCharsets.US_ASCII.name()), Constants.CONTENT_TYPE);
try {
upload.parseRequest(req);
fail("Expected exception.");
@@ -204,14 +208,14 @@ public class SizesTest extends FileUploadTestCase {
upload.setFileSizeMax(-1);
upload.setSizeMax(200);
- MockHttpServletRequest req = new MockHttpServletRequest(request.getBytes("US-ASCII"), CONTENT_TYPE);
+ MockHttpServletRequest req = new MockHttpServletRequest(
+ request.getBytes(StandardCharsets.US_ASCII.name()), Constants.CONTENT_TYPE);
try {
upload.parseRequest(req);
fail("Expected exception.");
} catch (FileUploadBase.SizeLimitExceededException e) {
assertEquals(200, e.getPermittedSize());
}
-
}
@Test
@@ -241,7 +245,8 @@ public class SizesTest extends FileUploadTestCase {
// set the read limit to 10 to simulate a "real" stream
// otherwise the buffer would be immediately filled
- MockHttpServletRequest req = new MockHttpServletRequest(request.getBytes("US-ASCII"), CONTENT_TYPE);
+ MockHttpServletRequest req = new MockHttpServletRequest(
+ request.getBytes(StandardCharsets.US_ASCII.name()), Constants.CONTENT_TYPE);
req.setContentLength(-1);
req.setReadLimit(10);
@@ -277,7 +282,6 @@ public class SizesTest extends FileUploadTestCase {
} catch (FileUploadIOException e) {
// expected
}
-
}
}
http://git-wip-us.apache.org/repos/asf/commons-fileupload/blob/f8f529ee/src/test/java/org/apache/commons/fileupload/Util.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/fileupload/Util.java b/src/test/java/org/apache/commons/fileupload/Util.java
new file mode 100644
index 0000000..42041e3
--- /dev/null
+++ b/src/test/java/org/apache/commons/fileupload/Util.java
@@ -0,0 +1,63 @@
+/*
+ * 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.fileupload;
+
+import java.io.UnsupportedEncodingException;
+import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
+import java.util.List;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.commons.fileupload.disk.DiskFileItemFactory;
+import org.apache.commons.fileupload.portlet.PortletFileUpload;
+import org.apache.commons.fileupload.servlet.ServletFileUpload;
+import org.apache.commons.fileupload.servlet.ServletRequestContext;
+
+/**
+ * Test utility methods.
+ *
+ * @since 1.4
+ */
+public class Util {
+
+ public static List<FileItem> parseUpload(FileUpload upload, byte[] bytes) throws FileUploadException {
+ return parseUpload(upload, bytes, Constants.CONTENT_TYPE);
+ }
+
+ public static List<FileItem> parseUpload(FileUpload upload, byte[] bytes, String contentType) throws FileUploadException {
+ final HttpServletRequest request = new MockHttpServletRequest(bytes, contentType);
+ List<FileItem> fileItems = upload.parseRequest(new ServletRequestContext(request));
+ return fileItems;
+ }
+
+ public static List<FileItem> parseUpload(FileUpload upload, String content)
+ throws UnsupportedEncodingException, FileUploadException {
+ byte[] bytes = content.getBytes(StandardCharsets.US_ASCII.name());
+ return parseUpload(upload, bytes, Constants.CONTENT_TYPE);
+ }
+
+ /**
+ * Return a list of {@link FileUpload} implementations for parameterized tests.
+ * @return a list of {@link FileUpload} implementations
+ */
+ public static List<FileUpload> fileUploadImplementations() {
+ return Arrays.asList(
+ new ServletFileUpload(new DiskFileItemFactory()),
+ new PortletFileUpload(new DiskFileItemFactory()));
+ }
+}
http://git-wip-us.apache.org/repos/asf/commons-fileupload/blob/f8f529ee/src/test/java/org/apache/commons/fileupload/portlet/MockPortletActionRequest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/fileupload/portlet/MockPortletActionRequest.java b/src/test/java/org/apache/commons/fileupload/portlet/MockPortletActionRequest.java
new file mode 100644
index 0000000..5f13788
--- /dev/null
+++ b/src/test/java/org/apache/commons/fileupload/portlet/MockPortletActionRequest.java
@@ -0,0 +1,271 @@
+/*
+ * 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.fileupload.portlet;
+
+import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
+import java.security.Principal;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.Locale;
+import java.util.Map;
+
+import javax.portlet.ActionRequest;
+import javax.portlet.PortalContext;
+import javax.portlet.PortletMode;
+import javax.portlet.PortletPreferences;
+import javax.portlet.PortletSession;
+import javax.portlet.WindowState;
+
+import org.apache.commons.fileupload.FileUploadBase;
+
+/**
+ * Mock class for tests. Implements an {@link ActionRequest}.
+ *
+ * @see PortletFileUploadTest
+ * @since 1.4
+ */
+@SuppressWarnings("rawtypes") // because of the portlet ActionRequest API does not use generics
+public class MockPortletActionRequest implements ActionRequest {
+
+ private final Hashtable<String, Object> attributes = new Hashtable<String, Object>();
+
+ private final Map<String, String> parameters = new HashMap<String, String>();
+
+ private String characterEncoding;
+ private int length;
+ private final String contentType;
+ private final InputStream requestData;
+
+ public MockPortletActionRequest(final byte[] requestData, final String contentType) {
+ this(new ByteArrayInputStream(requestData), requestData.length, contentType);
+ }
+
+ public MockPortletActionRequest(ByteArrayInputStream byteArrayInputStream, int requestLength, String contentType) {
+ this.requestData = byteArrayInputStream;
+ length = requestLength;
+ this.contentType = contentType;
+ attributes.put(FileUploadBase.CONTENT_TYPE, contentType);
+ }
+
+ @Override
+ public Object getAttribute(String key) {
+ return attributes.get(key);
+ }
+
+ @Override
+ public Enumeration getAttributeNames() {
+ return attributes.keys();
+ }
+
+ @Override
+ public String getAuthType() {
+ return null;
+ }
+
+ @Override
+ public String getContextPath() {
+ return null;
+ }
+
+ @Override
+ public Locale getLocale() {
+ return Locale.getDefault();
+ }
+
+ @Override
+ public Enumeration getLocales() {
+ return Collections.enumeration(Arrays.asList(Locale.getAvailableLocales()));
+ }
+
+ @Override
+ public String getParameter(String key) {
+ return parameters.get(key);
+ }
+
+ @Override
+ public Map getParameterMap() {
+ return Collections.unmodifiableMap(parameters);
+ }
+
+ @Override
+ public Enumeration getParameterNames() {
+ return Collections.enumeration(parameters.keySet());
+ }
+
+ @Override
+ public String[] getParameterValues(String arg0) {
+ return null;
+ }
+
+ @Override
+ public PortalContext getPortalContext() {
+ return null;
+ }
+
+ @Override
+ public PortletMode getPortletMode() {
+ return null;
+ }
+
+ @Override
+ public PortletSession getPortletSession() {
+ return null;
+ }
+
+ @Override
+ public PortletSession getPortletSession(boolean arg0) {
+ return null;
+ }
+
+ @Override
+ public PortletPreferences getPreferences() {
+ return null;
+ }
+
+ @Override
+ public Enumeration getProperties(String arg0) {
+ return null;
+ }
+
+ @Override
+ public String getProperty(String arg0) {
+ return null;
+ }
+
+ @Override
+ public Enumeration getPropertyNames() {
+ return null;
+ }
+
+ @Override
+ public String getRemoteUser() {
+ return null;
+ }
+
+ @Override
+ public String getRequestedSessionId() {
+ return null;
+ }
+
+ @Override
+ public String getResponseContentType() {
+ return null;
+ }
+
+ @Override
+ public Enumeration getResponseContentTypes() {
+ return null;
+ }
+
+ @Override
+ public String getScheme() {
+ return null;
+ }
+
+ @Override
+ public String getServerName() {
+ return null;
+ }
+
+ @Override
+ public int getServerPort() {
+ return 0;
+ }
+
+ @Override
+ public Principal getUserPrincipal() {
+ return null;
+ }
+
+ @Override
+ public WindowState getWindowState() {
+ return null;
+ }
+
+ @Override
+ public boolean isPortletModeAllowed(PortletMode arg0) {
+ return false;
+ }
+
+ @Override
+ public boolean isRequestedSessionIdValid() {
+ return false;
+ }
+
+ @Override
+ public boolean isSecure() {
+ return false;
+ }
+
+ @Override
+ public boolean isUserInRole(String arg0) {
+ return false;
+ }
+
+ @Override
+ public boolean isWindowStateAllowed(WindowState arg0) {
+ return false;
+ }
+
+ @Override
+ public void removeAttribute(String key) {
+ attributes.remove(key);
+ }
+
+ @Override
+ public void setAttribute(String key, Object value) {
+ attributes.put(key, value);
+ }
+
+ @Override
+ public String getCharacterEncoding() {
+ return characterEncoding;
+ }
+
+ @Override
+ public int getContentLength() {
+ return length;
+ }
+
+ @Override
+ public String getContentType() {
+ return contentType;
+ }
+
+ @Override
+ public InputStream getPortletInputStream() throws IOException {
+ return requestData;
+ }
+
+ @Override
+ public BufferedReader getReader() throws UnsupportedEncodingException, IOException {
+ return null;
+ }
+
+ @Override
+ public void setCharacterEncoding(String characterEncoding) throws UnsupportedEncodingException {
+ this.characterEncoding = characterEncoding;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-fileupload/blob/f8f529ee/src/test/java/org/apache/commons/fileupload/portlet/PortletFileUploadTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/fileupload/portlet/PortletFileUploadTest.java b/src/test/java/org/apache/commons/fileupload/portlet/PortletFileUploadTest.java
new file mode 100644
index 0000000..7b2aa80
--- /dev/null
+++ b/src/test/java/org/apache/commons/fileupload/portlet/PortletFileUploadTest.java
@@ -0,0 +1,86 @@
+/*
+ * 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.fileupload.portlet;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+import java.util.Map;
+
+import javax.portlet.ActionRequest;
+
+import org.apache.commons.fileupload.Constants;
+import org.apache.commons.fileupload.FileItem;
+import org.apache.commons.fileupload.FileUploadTest;
+import org.apache.commons.fileupload.disk.DiskFileItemFactory;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Test for {@link PortletFileUpload}.
+ *
+ * @see FileUploadTest
+ * @since 1.4
+ */
+public class PortletFileUploadTest {
+
+ private PortletFileUpload upload;
+
+ @Before
+ public void setUp() {
+ upload = new PortletFileUpload(new DiskFileItemFactory());
+ }
+
+ @Test
+ public void parseParameterMap()
+ throws Exception {
+ 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";
+ byte[] bytes = text.getBytes(StandardCharsets.US_ASCII.name());
+ ActionRequest request = new MockPortletActionRequest(bytes, Constants.CONTENT_TYPE);
+
+ Map<String, List<FileItem>> 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());
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-fileupload/blob/f8f529ee/src/test/java/org/apache/commons/fileupload/servlet/ServletFileUploadTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/fileupload/servlet/ServletFileUploadTest.java b/src/test/java/org/apache/commons/fileupload/servlet/ServletFileUploadTest.java
new file mode 100644
index 0000000..69480d7
--- /dev/null
+++ b/src/test/java/org/apache/commons/fileupload/servlet/ServletFileUploadTest.java
@@ -0,0 +1,89 @@
+/*
+ * 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.fileupload.servlet;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+import java.util.Map;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.commons.fileupload.Constants;
+import org.apache.commons.fileupload.FileItem;
+import org.apache.commons.fileupload.MockHttpServletRequest;
+import org.apache.commons.fileupload.disk.DiskFileItemFactory;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Test for {@link ServletFileUpload}.
+ *
+ * @see FileUploadTest
+ * @since 1.4
+ */
+public class ServletFileUploadTest {
+
+ private ServletFileUpload upload;
+
+ @Before
+ public void setUp() {
+ upload = new ServletFileUpload(new DiskFileItemFactory());
+ }
+
+ /**
+ * Test case for <a href="http://issues.apache.org/jira/browse/FILEUPLOAD-210">
+ */
+ @Test
+ public void parseParameterMap()
+ throws Exception {
+ 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";
+ byte[] bytes = text.getBytes(StandardCharsets.US_ASCII.name());
+ HttpServletRequest request = new MockHttpServletRequest(bytes, Constants.CONTENT_TYPE);
+
+ Map<String, List<FileItem>> 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());
+ }
+
+}
[2/2] commons-fileupload git commit: Merge remote-tracking branch
'kinow/FILEUPLOAD-283'
Posted by ki...@apache.org.
Merge remote-tracking branch 'kinow/FILEUPLOAD-283'
This closes #8
Project: http://git-wip-us.apache.org/repos/asf/commons-fileupload/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-fileupload/commit/86a79bce
Tree: http://git-wip-us.apache.org/repos/asf/commons-fileupload/tree/86a79bce
Diff: http://git-wip-us.apache.org/repos/asf/commons-fileupload/diff/86a79bce
Branch: refs/heads/master
Commit: 86a79bceac4a267819de986ca814f11b48d81da3
Parents: 94e5e1a f8f529e
Author: Bruno P. Kinoshita <br...@yahoo.com.br>
Authored: Sun Jun 25 17:04:04 2017 +1200
Committer: Bruno P. Kinoshita <br...@yahoo.com.br>
Committed: Sun Jun 25 17:04:04 2017 +1200
----------------------------------------------------------------------
.../apache/commons/fileupload/Constants.java | 32 ++
.../commons/fileupload/DiskFileUploadTest.java | 67 +++
.../commons/fileupload/FileUploadTest.java | 399 +++++++++++++++++
.../commons/fileupload/FileUploadTestCase.java | 52 ---
.../fileupload/MockHttpServletRequest.java | 2 +-
.../commons/fileupload/MultipartStreamTest.java | 1 -
.../fileupload/ProgressListenerTest.java | 15 +-
.../fileupload/ServletFileUploadTest.java | 446 -------------------
.../apache/commons/fileupload/SizesTest.java | 30 +-
.../org/apache/commons/fileupload/Util.java | 63 +++
.../portlet/MockPortletActionRequest.java | 271 +++++++++++
.../portlet/PortletFileUploadTest.java | 86 ++++
.../servlet/ServletFileUploadTest.java | 89 ++++
13 files changed, 1033 insertions(+), 520 deletions(-)
----------------------------------------------------------------------