You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2025/06/05 12:21:32 UTC

(commons-fileupload) branch 1.x updated (dc8a0b7d -> f1028401)

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a change to branch 1.x
in repository https://gitbox.apache.org/repos/asf/commons-fileupload.git


    from dc8a0b7d Additional test to expand code coverage
     new c6b52f0c Better test assertions
     new c68f48e6 Adjust Javadoc @since tag
     new b247774a Sort members
     new 637c9344 Sort imports
     new d9a3b8bb Use final
     new 8342754c Remove trailing whitespace
     new fa91cf07 Use assertThrows
     new 9d3aeeee Let JUnit handle exceptions
     new b42339ab Use assertThrows
     new 80b888a5 Use assertThrows
     new f1028401 Use assertThrows and friends

The 11 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 pom.xml                                            |  5 ++
 .../apache/commons/fileupload/FileUploadBase.java  | 46 +++++------
 .../apache/commons/fileupload/MultipartStream.java |  4 +-
 .../fileupload/DiskFileItemSerializeTest.java      | 34 +++++---
 .../commons/fileupload/DiskFileUploadTest.java     | 20 +----
 .../apache/commons/fileupload/FileUploadTest.java  | 14 +---
 .../commons/fileupload/MultipartStreamTest.java    | 92 +++++++++------------
 .../org/apache/commons/fileupload/SizesTest.java   | 96 +++++++++-------------
 .../apache/commons/fileupload/StreamingTest.java   | 44 ++++------
 .../java/org/apache/commons/fileupload/Util.java   |  2 +-
 .../util/mime/QuotedPrintableDecoderTestCase.java  | 11 +--
 11 files changed, 162 insertions(+), 206 deletions(-)


(commons-fileupload) 03/11: Sort members

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch 1.x
in repository https://gitbox.apache.org/repos/asf/commons-fileupload.git

commit b247774a72a044f5d5380ae947140ee80af4e78b
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Thu Jun 5 07:49:12 2025 -0400

    Sort members
---
 .../apache/commons/fileupload/FileUploadBase.java  | 42 ++++++-------
 .../commons/fileupload/MultipartStreamTest.java    | 64 +++++++++----------
 .../org/apache/commons/fileupload/SizesTest.java   | 72 +++++++++++-----------
 3 files changed, 89 insertions(+), 89 deletions(-)

diff --git a/src/main/java/org/apache/commons/fileupload/FileUploadBase.java b/src/main/java/org/apache/commons/fileupload/FileUploadBase.java
index 4b19d75d..9448ead0 100644
--- a/src/main/java/org/apache/commons/fileupload/FileUploadBase.java
+++ b/src/main/java/org/apache/commons/fileupload/FileUploadBase.java
@@ -56,16 +56,6 @@ import org.apache.commons.io.IOUtils;
  */
 public abstract class FileUploadBase {
 
-    /**
-     * Line feed.
-     */
-    private static final char LF = '\n';
-
-    /**
-     * Carriage return.
-     */
-    private static final char CR = '\r';
-
     /**
      * The iterator, which is returned by
      * {@link FileUploadBase#getItemIterator(RequestContext)}.
@@ -785,6 +775,16 @@ public abstract class FileUploadBase {
 
     }
 
+    /**
+     * Line feed.
+     */
+    private static final char LF = '\n';
+
+    /**
+     * Carriage return.
+     */
+    private static final char CR = '\r';
+
     /**
      * HTTP content type header name.
      */
@@ -1142,17 +1142,6 @@ public abstract class FileUploadBase {
         }
     }
 
-    /**
-     * Obtain the per part size limit for headers.
-     *
-     * @return The maximum size of the headers for a single part in bytes.
-     *
-     * @since 1.6.0
-     */
-    public int getPartHeaderSizeMax() {
-        return partHeaderSizeMax;
-    }
-
     /**
      * <p> Parses the {@code header-part} and returns as key/value
      * pairs.
@@ -1198,6 +1187,17 @@ public abstract class FileUploadBase {
         return headers;
     }
 
+    /**
+     * Obtain the per part size limit for headers.
+     *
+     * @return The maximum size of the headers for a single part in bytes.
+     *
+     * @since 1.6.0
+     */
+    public int getPartHeaderSizeMax() {
+        return partHeaderSizeMax;
+    }
+
     /**
      * Returns the progress listener.
      *
diff --git a/src/test/java/org/apache/commons/fileupload/MultipartStreamTest.java b/src/test/java/org/apache/commons/fileupload/MultipartStreamTest.java
index 257c55c9..79533c24 100644
--- a/src/test/java/org/apache/commons/fileupload/MultipartStreamTest.java
+++ b/src/test/java/org/apache/commons/fileupload/MultipartStreamTest.java
@@ -37,38 +37,6 @@ public class MultipartStreamTest {
 
     static private final String BOUNDARY_TEXT = "myboundary";
 
-    @Test(expected = IllegalArgumentException.class)
-    public void testSmallBuffer() throws Exception {
-        final String strData = "foobar";
-        final byte[] contents = strData.getBytes();
-        final InputStream input = new ByteArrayInputStream(contents);
-        final byte[] boundary = BOUNDARY_TEXT.getBytes();
-        final int iBufSize = 1;
-        @SuppressWarnings("unused")
-        final MultipartStream unused = new MultipartStream(input, boundary, iBufSize, new MultipartStream.ProgressNotifier(null, contents.length));
-    }
-
-    @Test
-    public void testThreeParamConstructor() throws Exception {
-        final String strData = "foobar";
-        final byte[] contents = strData.getBytes();
-        final InputStream input = new ByteArrayInputStream(contents);
-        final byte[] boundary = BOUNDARY_TEXT.getBytes();
-        final int iBufSize = boundary.length + MultipartStream.BOUNDARY_PREFIX.length + 1;
-        final MultipartStream ms = new MultipartStream(input, boundary, iBufSize, new MultipartStream.ProgressNotifier(null, contents.length));
-        assertNotNull(ms);
-    }
-
-    @Test
-    public void testTwoParamConstructor() throws Exception {
-        final String strData = "foobar";
-        final byte[] contents = strData.getBytes();
-        final InputStream input = new ByteArrayInputStream(contents);
-        final byte[] boundary = BOUNDARY_TEXT.getBytes();
-        final MultipartStream ms = new MultipartStream(input, boundary, new MultipartStream.ProgressNotifier(null, contents.length));
-        assertNotNull(ms);
-    }
-
     @Test
     public void testMalformedUploadTruncatedHeaders()
             throws IOException, FileUploadException {
@@ -118,4 +86,36 @@ public class MultipartStreamTest {
         final FileUploadException e = assertThrows(FileUploadException.class, () -> upload.parseRequest(req));
         assertInstanceOf(MalformedStreamException.class, e.getCause());
     }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testSmallBuffer() throws Exception {
+        final String strData = "foobar";
+        final byte[] contents = strData.getBytes();
+        final InputStream input = new ByteArrayInputStream(contents);
+        final byte[] boundary = BOUNDARY_TEXT.getBytes();
+        final int iBufSize = 1;
+        @SuppressWarnings("unused")
+        final MultipartStream unused = new MultipartStream(input, boundary, iBufSize, new MultipartStream.ProgressNotifier(null, contents.length));
+    }
+
+    @Test
+    public void testThreeParamConstructor() throws Exception {
+        final String strData = "foobar";
+        final byte[] contents = strData.getBytes();
+        final InputStream input = new ByteArrayInputStream(contents);
+        final byte[] boundary = BOUNDARY_TEXT.getBytes();
+        final int iBufSize = boundary.length + MultipartStream.BOUNDARY_PREFIX.length + 1;
+        final MultipartStream ms = new MultipartStream(input, boundary, iBufSize, new MultipartStream.ProgressNotifier(null, contents.length));
+        assertNotNull(ms);
+    }
+
+    @Test
+    public void testTwoParamConstructor() throws Exception {
+        final String strData = "foobar";
+        final byte[] contents = strData.getBytes();
+        final InputStream input = new ByteArrayInputStream(contents);
+        final byte[] boundary = BOUNDARY_TEXT.getBytes();
+        final MultipartStream ms = new MultipartStream(input, boundary, new MultipartStream.ProgressNotifier(null, contents.length));
+        assertNotNull(ms);
+    }
 }
diff --git a/src/test/java/org/apache/commons/fileupload/SizesTest.java b/src/test/java/org/apache/commons/fileupload/SizesTest.java
index 73e1b0dc..90ae6f21 100644
--- a/src/test/java/org/apache/commons/fileupload/SizesTest.java
+++ b/src/test/java/org/apache/commons/fileupload/SizesTest.java
@@ -217,42 +217,6 @@ public class SizesTest {
         }
     }
 
-    /** Checks, whether the maxSize works.
-     */
-    @Test
-    public void testPartHeaderSizeMaxLimit()
-            throws IOException, FileUploadException {
-        final String request =
-            "-----1234\r\n" +
-            "Content-Disposition: form-data; name=\"file1\"; filename=\"foo1.tab\"\r\n" +
-            "Content-Type: text/whatever\r\n" +
-            "Content-Length: 10\r\n" +
-            "\r\n" +
-            "This is the content of the file\n" +
-            "\r\n" +
-            "-----1234\r\n" +
-            "Content-Disposition: form-data; name=\"file2\"; filename=\"foo2.tab\"\r\n" +
-            "Content-Type: text/whatever\r\n" +
-            "\r\n" +
-            "This is the content of the file\n" +
-            "\r\n" +
-            "-----1234--\r\n";
-
-        final ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
-        upload.setFileSizeMax(-1);
-        upload.setSizeMax(-1);
-        upload.setPartHeaderSizeMax(100);
-
-        final MockHttpServletRequest req = new MockHttpServletRequest(
-                request.getBytes("US-ASCII"), Constants.CONTENT_TYPE);
-        try {
-            upload.parseRequest(req);
-            fail("Expected exception.");
-        } catch (final FileUploadBase.SizeLimitExceededException e) {
-            assertEquals(100, e.getPermittedSize());
-        }
-    }
-
     @Test
     public void testMaxSizeLimitUnknownContentLength()
             throws IOException, FileUploadException {
@@ -316,4 +280,40 @@ public class SizesTest {
             // expected
         }
     }
+
+    /** Checks, whether the maxSize works.
+     */
+    @Test
+    public void testPartHeaderSizeMaxLimit()
+            throws IOException, FileUploadException {
+        final String request =
+            "-----1234\r\n" +
+            "Content-Disposition: form-data; name=\"file1\"; filename=\"foo1.tab\"\r\n" +
+            "Content-Type: text/whatever\r\n" +
+            "Content-Length: 10\r\n" +
+            "\r\n" +
+            "This is the content of the file\n" +
+            "\r\n" +
+            "-----1234\r\n" +
+            "Content-Disposition: form-data; name=\"file2\"; filename=\"foo2.tab\"\r\n" +
+            "Content-Type: text/whatever\r\n" +
+            "\r\n" +
+            "This is the content of the file\n" +
+            "\r\n" +
+            "-----1234--\r\n";
+
+        final ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
+        upload.setFileSizeMax(-1);
+        upload.setSizeMax(-1);
+        upload.setPartHeaderSizeMax(100);
+
+        final MockHttpServletRequest req = new MockHttpServletRequest(
+                request.getBytes("US-ASCII"), Constants.CONTENT_TYPE);
+        try {
+            upload.parseRequest(req);
+            fail("Expected exception.");
+        } catch (final FileUploadBase.SizeLimitExceededException e) {
+            assertEquals(100, e.getPermittedSize());
+        }
+    }
 }


(commons-fileupload) 10/11: Use assertThrows

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch 1.x
in repository https://gitbox.apache.org/repos/asf/commons-fileupload.git

commit 80b888a54bd6ea39c05829aee803e83e6063b8e3
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Thu Jun 5 08:16:05 2025 -0400

    Use assertThrows
---
 .../org/apache/commons/fileupload/SizesTest.java   | 30 ++++++++--------------
 1 file changed, 11 insertions(+), 19 deletions(-)

diff --git a/src/test/java/org/apache/commons/fileupload/SizesTest.java b/src/test/java/org/apache/commons/fileupload/SizesTest.java
index 90ae6f21..e7a51010 100644
--- a/src/test/java/org/apache/commons/fileupload/SizesTest.java
+++ b/src/test/java/org/apache/commons/fileupload/SizesTest.java
@@ -20,6 +20,8 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
@@ -30,7 +32,7 @@ import java.util.List;
 import javax.servlet.http.HttpServletRequest;
 
 import org.apache.commons.fileupload.FileUploadBase.FileUploadIOException;
-import org.apache.commons.fileupload.FileUploadBase.SizeException;
+import org.apache.commons.fileupload.MultipartStream.MalformedStreamException;
 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
 import org.apache.commons.fileupload.servlet.ServletFileUpload;
 import org.apache.commons.fileupload.util.Streams;
@@ -207,14 +209,8 @@ public class SizesTest {
         upload.setFileSizeMax(-1);
         upload.setSizeMax(200);
 
-        final MockHttpServletRequest req = new MockHttpServletRequest(
-                request.getBytes("US-ASCII"), Constants.CONTENT_TYPE);
-        try {
-            upload.parseRequest(req);
-            fail("Expected exception.");
-        } catch (final FileUploadBase.SizeLimitExceededException e) {
-            assertEquals(200, e.getPermittedSize());
-        }
+        final MockHttpServletRequest req = new MockHttpServletRequest(request.getBytes("US-ASCII"), Constants.CONTENT_TYPE);
+        assertEquals(200, assertThrows(FileUploadBase.SizeLimitExceededException.class, () -> upload.parseRequest(req)).getPermittedSize());
     }
 
     @Test
@@ -263,21 +259,17 @@ public class SizesTest {
         }
 
         // the second item is over the size max, thus we expect an error
-        try {
-            // the header is still within size max -> this shall still succeed
-            assertTrue(it.hasNext());
-        } catch (final SizeException e) {
-            fail();
-        }
+        // the header is still within size max -> this shall still succeed
+        assertDoesNotThrow(it::hasNext);
 
         item = it.next();
 
         try (InputStream stream = item.openStream()) {
             final ByteArrayOutputStream baos = new ByteArrayOutputStream();
-            Streams.copy(stream, baos, true);
-            fail();
-        } catch (final FileUploadIOException e) {
-            // expected
+            assertThrows(FileUploadIOException.class, () -> Streams.copy(stream, baos, true));
+            assertThrows(MalformedStreamException.class, stream::close);
+        } catch (final MalformedStreamException e) {
+            // expected as close() throws again.
         }
     }
 


(commons-fileupload) 05/11: Use final

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch 1.x
in repository https://gitbox.apache.org/repos/asf/commons-fileupload.git

commit d9a3b8bb2f69c3b8632fbbd4630a32696f5e6f65
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Thu Jun 5 07:50:56 2025 -0400

    Use final
---
 src/test/java/org/apache/commons/fileupload/MultipartStreamTest.java | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/test/java/org/apache/commons/fileupload/MultipartStreamTest.java b/src/test/java/org/apache/commons/fileupload/MultipartStreamTest.java
index 79533c24..a5709ced 100644
--- a/src/test/java/org/apache/commons/fileupload/MultipartStreamTest.java
+++ b/src/test/java/org/apache/commons/fileupload/MultipartStreamTest.java
@@ -71,7 +71,7 @@ public class MultipartStreamTest {
             "Content-Type: text/whatever\r\n" +
             "Content-Length: 10\r\n" +
             "X-Padding: ");
-        int paddingLength = MultipartStream.DEFAULT_BUFSIZE - request.length();
+        final int paddingLength = MultipartStream.DEFAULT_BUFSIZE - request.length();
         for (int i = 0; i < paddingLength; i++) {
             request.append('x');
         }


(commons-fileupload) 09/11: Use assertThrows

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch 1.x
in repository https://gitbox.apache.org/repos/asf/commons-fileupload.git

commit b42339abd4f0d482dfa36165c7b5dc4f275be6d4
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Thu Jun 5 08:05:31 2025 -0400

    Use assertThrows
---
 .../commons/fileupload/DiskFileUploadTest.java       | 20 ++++----------------
 1 file changed, 4 insertions(+), 16 deletions(-)

diff --git a/src/test/java/org/apache/commons/fileupload/DiskFileUploadTest.java b/src/test/java/org/apache/commons/fileupload/DiskFileUploadTest.java
index 73d9b5e0..72be2098 100644
--- a/src/test/java/org/apache/commons/fileupload/DiskFileUploadTest.java
+++ b/src/test/java/org/apache/commons/fileupload/DiskFileUploadTest.java
@@ -19,6 +19,8 @@ package org.apache.commons.fileupload;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.fail;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertThrowsExactly;
 
 import java.io.File;
 import java.util.List;
@@ -72,26 +74,12 @@ public class DiskFileUploadTest {
     @Test
     public void testWithInvalidRequest() {
         final HttpServletRequest req = HttpServletRequestFactory.createInvalidHttpServletRequest();
-
-        try {
-            upload.parseRequest(req);
-            fail("testWithInvalidRequest: expected exception was not thrown");
-        } catch (final FileUploadException expected) {
-            // this exception is expected
-        }
+        assertThrows(FileUploadException.class, () -> upload.parseRequest(req));
     }
 
     @Test
     public void testWithNullContentType() {
         final HttpServletRequest req = HttpServletRequestFactory.createHttpServletRequestWithNullContentType();
-
-        try {
-            upload.parseRequest(req);
-            fail("testWithNullContentType: expected exception was not thrown");
-        } catch (final DiskFileUpload.InvalidContentTypeException expected) {
-            // this exception is expected
-        } catch (final FileUploadException unexpected) {
-            fail("testWithNullContentType: unexpected exception was thrown");
-        }
+        assertThrowsExactly(DiskFileUpload.InvalidContentTypeException.class, () -> upload.parseRequest(req));
     }
 }


(commons-fileupload) 02/11: Adjust Javadoc @since tag

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch 1.x
in repository https://gitbox.apache.org/repos/asf/commons-fileupload.git

commit c68f48e65db576057ec059ba097e78e84dd92450
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Thu Jun 5 07:48:42 2025 -0400

    Adjust Javadoc @since tag
---
 src/main/java/org/apache/commons/fileupload/FileUploadBase.java  | 6 +++---
 src/main/java/org/apache/commons/fileupload/MultipartStream.java | 4 ++--
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/src/main/java/org/apache/commons/fileupload/FileUploadBase.java b/src/main/java/org/apache/commons/fileupload/FileUploadBase.java
index 5baca997..4b19d75d 100644
--- a/src/main/java/org/apache/commons/fileupload/FileUploadBase.java
+++ b/src/main/java/org/apache/commons/fileupload/FileUploadBase.java
@@ -845,7 +845,7 @@ public abstract class FileUploadBase {
     /**
      * Default per part header size limit in bytes.
      *
-     * @since 1.6
+     * @since 1.6.0
      */
     public static final int DEFAULT_PART_HEADER_SIZE_MAX = 512;
 
@@ -1147,7 +1147,7 @@ public abstract class FileUploadBase {
      *
      * @return The maximum size of the headers for a single part in bytes.
      *
-     * @since 1.6
+     * @since 1.6.0
      */
     public int getPartHeaderSizeMax() {
         return partHeaderSizeMax;
@@ -1444,7 +1444,7 @@ public abstract class FileUploadBase {
      *
      * @param partHeaderSizeMax The maximum size of the headers in bytes.
      *
-     * @since 1.6
+     * @since 1.6.0
      */
     public void setPartHeaderSizeMax(final int partHeaderSizeMax) {
         this.partHeaderSizeMax = partHeaderSizeMax;
diff --git a/src/main/java/org/apache/commons/fileupload/MultipartStream.java b/src/main/java/org/apache/commons/fileupload/MultipartStream.java
index 39ef5bab..07389e05 100644
--- a/src/main/java/org/apache/commons/fileupload/MultipartStream.java
+++ b/src/main/java/org/apache/commons/fileupload/MultipartStream.java
@@ -801,7 +801,7 @@ public class MultipartStream {
      *
      * @return The maximum size of the headers for a single part in bytes.
      *
-     * @since 1.6
+     * @since 1.6.0
      */
     public int getPartHeaderSizeMax() {
         return partHeaderSizeMax;
@@ -1000,7 +1000,7 @@ public class MultipartStream {
      *
      * @param partHeaderSizeMax The maximum size of the headers in bytes.
      *
-     * @since 1.6
+     * @since 1.6.0
      */
     public void setPartHeaderSizeMax(final int partHeaderSizeMax) {
         this.partHeaderSizeMax = partHeaderSizeMax;


(commons-fileupload) 11/11: Use assertThrows and friends

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch 1.x
in repository https://gitbox.apache.org/repos/asf/commons-fileupload.git

commit f1028401e3d59bd42aee9ab4d26995991db1aadc
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Thu Jun 5 08:21:26 2025 -0400

    Use assertThrows and friends
---
 .../org/apache/commons/fileupload/SizesTest.java   | 10 +----
 .../apache/commons/fileupload/StreamingTest.java   | 44 ++++++++--------------
 2 files changed, 17 insertions(+), 37 deletions(-)

diff --git a/src/test/java/org/apache/commons/fileupload/SizesTest.java b/src/test/java/org/apache/commons/fileupload/SizesTest.java
index e7a51010..13c9618f 100644
--- a/src/test/java/org/apache/commons/fileupload/SizesTest.java
+++ b/src/test/java/org/apache/commons/fileupload/SizesTest.java
@@ -299,13 +299,7 @@ public class SizesTest {
         upload.setSizeMax(-1);
         upload.setPartHeaderSizeMax(100);
 
-        final MockHttpServletRequest req = new MockHttpServletRequest(
-                request.getBytes("US-ASCII"), Constants.CONTENT_TYPE);
-        try {
-            upload.parseRequest(req);
-            fail("Expected exception.");
-        } catch (final FileUploadBase.SizeLimitExceededException e) {
-            assertEquals(100, e.getPermittedSize());
-        }
+        final MockHttpServletRequest req = new MockHttpServletRequest(request.getBytes("US-ASCII"), Constants.CONTENT_TYPE);
+        assertEquals(100, assertThrows(FileUploadBase.SizeLimitExceededException.class, () -> upload.parseRequest(req)).getPermittedSize());
     }
 }
diff --git a/src/test/java/org/apache/commons/fileupload/StreamingTest.java b/src/test/java/org/apache/commons/fileupload/StreamingTest.java
index b4d0390b..d8df54df 100644
--- a/src/test/java/org/apache/commons/fileupload/StreamingTest.java
+++ b/src/test/java/org/apache/commons/fileupload/StreamingTest.java
@@ -19,6 +19,8 @@ package org.apache.commons.fileupload;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
@@ -176,12 +178,8 @@ public class StreamingTest {
         final byte[] request = newRequest();
         final byte[] invalidRequest = new byte[request.length - 11];
         System.arraycopy(request, 0, invalidRequest, 0, request.length - 11);
-        try {
-            parseUpload(invalidRequest);
-            fail("Expected EndOfStreamException");
-        } catch (final IOFileUploadException e) {
-            assertTrue(e.getCause() instanceof MultipartStream.MalformedStreamException);
-        }
+        assertInstanceOf(MultipartStream.MalformedStreamException.class,
+                assertThrows(IOFileUploadException.class, () -> parseUpload(invalidRequest)).getCause());
     }
 
     /**
@@ -214,25 +212,17 @@ public class StreamingTest {
 
         final FileItemIterator fileItemIter = parseUpload(reqBytes.length, new ByteArrayInputStream(reqBytes));
         final FileItemStream fileItemStream = fileItemIter.next();
-        try {
-            fileItemStream.getName();
-            fail("Expected exception");
-        } catch (final InvalidFileNameException e) {
-            assertEquals(fileName, e.getName());
-            assertTrue(e.getMessage().indexOf(fileName) == -1);
-            assertTrue(e.getMessage().indexOf("foo.exe\\0.png") != -1);
-        }
+        InvalidFileNameException e = assertThrows(InvalidFileNameException.class, fileItemStream::getName);
+        assertEquals(fileName, e.getName());
+        assertTrue(e.getMessage().indexOf(fileName) == -1);
+        assertTrue(e.getMessage().indexOf("foo.exe\\0.png") != -1);
 
         final List<FileItem> fileItems = parseUpload(reqBytes);
         final FileItem fileItem = fileItems.get(0);
-        try {
-            fileItem.getName();
-            fail("Expected exception");
-        } catch (final InvalidFileNameException e) {
-            assertEquals(fileName, e.getName());
-            assertTrue(e.getMessage().indexOf(fileName) == -1);
-            assertTrue(e.getMessage().indexOf("foo.exe\\0.png") != -1);
-        }
+        e = assertThrows(InvalidFileNameException.class, fileItem::getName);
+        assertEquals(fileName, e.getName());
+        assertTrue(e.getMessage().indexOf(fileName) == -1);
+        assertTrue(e.getMessage().indexOf("foo.exe\\0.png") != -1);
     }
 
     /**
@@ -265,12 +255,8 @@ public class StreamingTest {
                 return length;
             }
         };
-        try {
-            parseUpload(stream, request.length);
-            fail("Expected IOException");
-        } catch (final FileUploadException e) {
-            assertTrue(e.getCause() instanceof IOException);
-            assertEquals("123", e.getCause().getMessage());
-        }
+        final FileUploadException e = assertThrows(FileUploadException.class, () -> parseUpload(stream, request.length));
+        assertTrue(e.getCause() instanceof IOException);
+        assertEquals("123", e.getCause().getMessage());
     }
 }


(commons-fileupload) 08/11: Let JUnit handle exceptions

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch 1.x
in repository https://gitbox.apache.org/repos/asf/commons-fileupload.git

commit 9d3aeeee9aac3637a59ac24ae594f4c91864ddaa
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Thu Jun 5 07:56:12 2025 -0400

    Let JUnit handle exceptions
---
 .../fileupload/DiskFileItemSerializeTest.java      | 34 +++++++++++++++-------
 1 file changed, 24 insertions(+), 10 deletions(-)

diff --git a/src/test/java/org/apache/commons/fileupload/DiskFileItemSerializeTest.java b/src/test/java/org/apache/commons/fileupload/DiskFileItemSerializeTest.java
index a75902a6..1d398a13 100644
--- a/src/test/java/org/apache/commons/fileupload/DiskFileItemSerializeTest.java
+++ b/src/test/java/org/apache/commons/fileupload/DiskFileItemSerializeTest.java
@@ -86,22 +86,24 @@ public class DiskFileItemSerializeTest {
 
     /**
      * Create a FileItem with the specfied content bytes.
+     *
+     * @throws IOException test failure. 
      */
-    private FileItem createFileItem(final byte[] contentBytes) {
+    private FileItem createFileItem(final byte[] contentBytes) throws IOException {
         return createFileItem(contentBytes, REPO);
     }
 
     /**
      * Create a FileItem with the specfied content bytes and repository.
+     *
+     * @throws IOException test failure. 
      */
-    private FileItem createFileItem(final byte[] contentBytes, final File repository) {
+    private FileItem createFileItem(final byte[] contentBytes, final File repository) throws IOException {
         final FileItemFactory factory = new DiskFileItemFactory(THRESHOLD, repository);
         final String textFieldName = "textField";
         final FileItem item = factory.createItem(textFieldName, CONTENT_TYPE_TEXT, true, "My File Name");
         try (OutputStream os = item.getOutputStream()) {
             os.write(contentBytes);
-        } catch (final IOException e) {
-            fail("Unexpected IOException" + e);
         }
         return item;
     }
@@ -148,9 +150,11 @@ public class DiskFileItemSerializeTest {
 
     /**
      * Test creation of a field for which the amount of data falls above the configured threshold.
+     *
+     * @throws IOException test failure. 
      */
     @Test
-    public void testAboveThreshold() {
+    public void testAboveThreshold() throws IOException {
         // Create the FileItem
         final byte[] testFieldValueBytes = createContentBytes(THRESHOLD + 1);
         final FileItem item = createFileItem(testFieldValueBytes);
@@ -163,9 +167,11 @@ public class DiskFileItemSerializeTest {
 
     /**
      * Test creation of a field for which the amount of data falls below the configured threshold.
+     *
+     * @throws IOException test failure. 
      */
     @Test
-    public void testBelowThreshold() {
+    public void testBelowThreshold() throws IOException {
         // Create the FileItem
         final byte[] testFieldValueBytes = createContentBytes(THRESHOLD - 1);
         testInMemoryObject(testFieldValueBytes);
@@ -173,15 +179,19 @@ public class DiskFileItemSerializeTest {
 
     /**
      * Helper method to test creation of a field.
+     *
+     * @throws IOException test failure. 
      */
-    private void testInMemoryObject(final byte[] testFieldValueBytes) {
+    private void testInMemoryObject(final byte[] testFieldValueBytes) throws IOException {
         testInMemoryObject(testFieldValueBytes, REPO);
     }
 
     /**
      * Helper method to test creation of a field when a repository is used.
+     *
+     * @throws IOException test failure. 
      */
-    private void testInMemoryObject(final byte[] testFieldValueBytes, final File repository) {
+    private void testInMemoryObject(final byte[] testFieldValueBytes, final File repository) throws IOException {
         final FileItem item = createFileItem(testFieldValueBytes, repository);
         // Check state is as expected
         assertTrue("Initial: in memory", item.isInMemory());
@@ -216,9 +226,11 @@ public class DiskFileItemSerializeTest {
 
     /**
      * Test creation of a field for which the amount of data equals the configured threshold.
+     *
+     * @throws IOException test failure. 
      */
     @Test
-    public void testThreshold() {
+    public void testThreshold() throws IOException {
         // Create the FileItem
         final byte[] testFieldValueBytes = createContentBytes(THRESHOLD);
         testInMemoryObject(testFieldValueBytes);
@@ -226,9 +238,11 @@ public class DiskFileItemSerializeTest {
 
     /**
      * Test serialization and deserialization when repository is not null.
+     *
+     * @throws IOException test failure. 
      */
     @Test
-    public void testValidRepository() {
+    public void testValidRepository() throws IOException {
         // Create the FileItem
         final byte[] testFieldValueBytes = createContentBytes(THRESHOLD);
         testInMemoryObject(testFieldValueBytes, REPO);


(commons-fileupload) 07/11: Use assertThrows

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch 1.x
in repository https://gitbox.apache.org/repos/asf/commons-fileupload.git

commit fa91cf078784f51ab7837fcecae456c0741d6773
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Thu Jun 5 07:54:26 2025 -0400

    Use assertThrows
---
 .../java/org/apache/commons/fileupload/FileUploadTest.java    | 10 ++--------
 .../fileupload/util/mime/QuotedPrintableDecoderTestCase.java  | 11 ++++-------
 2 files changed, 6 insertions(+), 15 deletions(-)

diff --git a/src/test/java/org/apache/commons/fileupload/FileUploadTest.java b/src/test/java/org/apache/commons/fileupload/FileUploadTest.java
index af119776..80d6d2a1 100644
--- a/src/test/java/org/apache/commons/fileupload/FileUploadTest.java
+++ b/src/test/java/org/apache/commons/fileupload/FileUploadTest.java
@@ -20,6 +20,7 @@ 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.jupiter.api.Assertions.assertThrows;
 
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
@@ -33,7 +34,6 @@ import org.apache.commons.fileupload.portlet.PortletFileUploadTest;
 import org.apache.commons.fileupload.servlet.ServletFileUpload;
 import org.apache.commons.fileupload.servlet.ServletFileUploadTest;
 import org.apache.commons.fileupload.util.Streams;
-import org.junit.Assert;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.junit.runners.Parameterized;
@@ -486,13 +486,7 @@ public class FileUploadTest {
             Streams.copy(stream, baos, true);
         }
 
-        try {
-            item.openStream();
-            Assert.fail("Attempt to open a closed stream did not throw an exception");
-        } catch (final IOException ioe) {
-            // Expected
-        }
-
+        assertThrows(IOException.class, item::openStream, "Attempt to open a closed stream did not throw an exception");
         // Should only be one item
         assertFalse(it.hasNext());
     }
diff --git a/src/test/java/org/apache/commons/fileupload/util/mime/QuotedPrintableDecoderTestCase.java b/src/test/java/org/apache/commons/fileupload/util/mime/QuotedPrintableDecoderTestCase.java
index 0b089124..e8436c91 100644
--- a/src/test/java/org/apache/commons/fileupload/util/mime/QuotedPrintableDecoderTestCase.java
+++ b/src/test/java/org/apache/commons/fileupload/util/mime/QuotedPrintableDecoderTestCase.java
@@ -19,6 +19,7 @@ package org.apache.commons.fileupload.util.mime;
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
@@ -47,13 +48,9 @@ public final class QuotedPrintableDecoderTestCase {
     private static void assertIOException(final String messageText, final String encoded) throws UnsupportedEncodingException {
         final ByteArrayOutputStream out = new ByteArrayOutputStream(encoded.length());
         final byte[] encodedData = encoded.getBytes(US_ASCII_CHARSET);
-        try {
-            QuotedPrintableDecoder.decode(encodedData, out);
-            fail("Expected IOException");
-        } catch (final IOException e) {
-            final String em = e.getMessage();
-            assertTrue("Expected to find " + messageText + " in '" + em + "'", em.contains(messageText));
-        }
+        final IOException e = assertThrows(IOException.class, () -> QuotedPrintableDecoder.decode(encodedData, out));
+        final String em = e.getMessage();
+        assertTrue("Expected to find " + messageText + " in '" + em + "'", em.contains(messageText));
     }
 
     @Test


(commons-fileupload) 01/11: Better test assertions

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch 1.x
in repository https://gitbox.apache.org/repos/asf/commons-fileupload.git

commit c6b52f0caff924eb5dc0deb717695af2ced1a138
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Thu Jun 5 07:47:29 2025 -0400

    Better test assertions
---
 pom.xml                                            |  5 +++++
 .../commons/fileupload/MultipartStreamTest.java    | 26 ++++++----------------
 2 files changed, 12 insertions(+), 19 deletions(-)

diff --git a/pom.xml b/pom.xml
index 189b9c8f..9c343e8b 100644
--- a/pom.xml
+++ b/pom.xml
@@ -88,6 +88,11 @@
       <artifactId>junit-vintage-engine</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.junit.jupiter</groupId>
+      <artifactId>junit-jupiter-api</artifactId>
+      <scope>test</scope>
+    </dependency>
     <dependency>
       <groupId>javax.servlet</groupId>
       <artifactId>servlet-api</artifactId>
diff --git a/src/test/java/org/apache/commons/fileupload/MultipartStreamTest.java b/src/test/java/org/apache/commons/fileupload/MultipartStreamTest.java
index 7fc80504..257c55c9 100644
--- a/src/test/java/org/apache/commons/fileupload/MultipartStreamTest.java
+++ b/src/test/java/org/apache/commons/fileupload/MultipartStreamTest.java
@@ -17,20 +17,18 @@
 
 package org.apache.commons.fileupload;
 
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.fail;
-
-import org.junit.Assert;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 
-import org.junit.Test;
-
 import org.apache.commons.fileupload.MultipartStream.MalformedStreamException;
 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
 import org.apache.commons.fileupload.servlet.ServletFileUpload;
+import org.junit.Test;
 
 /**
  * Tests {@link org.apache.commons.fileupload.MultipartStream}.
@@ -94,12 +92,7 @@ public class MultipartStreamTest {
 
         final MockHttpServletRequest req = new MockHttpServletRequest(
                 request.getBytes("US-ASCII"), Constants.CONTENT_TYPE);
-        try {
-            upload.parseRequest(req);
-            fail("Expected exception.");
-        } catch (final FileUploadBase.IOFileUploadException e) {
-            // Expected
-        }
+        assertThrows(FileUploadBase.IOFileUploadException.class, () -> upload.parseRequest(req));
     }
 
     @Test
@@ -122,12 +115,7 @@ public class MultipartStreamTest {
 
         final MockHttpServletRequest req = new MockHttpServletRequest(
                 request.toString().getBytes("US-ASCII"), Constants.CONTENT_TYPE);
-        try {
-            upload.parseRequest(req);
-            fail("Expected exception.");
-        } catch (final FileUploadException e) {
-            // Expected
-            Assert.assertTrue(e.getCause() instanceof MalformedStreamException);
-        }
+        final FileUploadException e = assertThrows(FileUploadException.class, () -> upload.parseRequest(req));
+        assertInstanceOf(MalformedStreamException.class, e.getCause());
     }
 }


(commons-fileupload) 04/11: Sort imports

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch 1.x
in repository https://gitbox.apache.org/repos/asf/commons-fileupload.git

commit 637c934496c9b3eb79da3e251afcad5a44772fdd
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Thu Jun 5 07:50:48 2025 -0400

    Sort imports
    
    - Use final
---
 src/test/java/org/apache/commons/fileupload/FileUploadTest.java | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/src/test/java/org/apache/commons/fileupload/FileUploadTest.java b/src/test/java/org/apache/commons/fileupload/FileUploadTest.java
index f0e69f7e..af119776 100644
--- a/src/test/java/org/apache/commons/fileupload/FileUploadTest.java
+++ b/src/test/java/org/apache/commons/fileupload/FileUploadTest.java
@@ -21,8 +21,6 @@ import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 
-import org.junit.Assert;
-
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
@@ -35,7 +33,7 @@ import org.apache.commons.fileupload.portlet.PortletFileUploadTest;
 import org.apache.commons.fileupload.servlet.ServletFileUpload;
 import org.apache.commons.fileupload.servlet.ServletFileUploadTest;
 import org.apache.commons.fileupload.util.Streams;
-
+import org.junit.Assert;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.junit.runners.Parameterized;
@@ -478,7 +476,7 @@ public class FileUploadTest {
         final FileItemIterator it = upload.getItemIterator(req);
         assertTrue(it.hasNext());
 
-        FileItemStream item = it.next();
+        final FileItemStream item = it.next();
         assertFalse(item.isFormField());
         assertEquals("file1", item.getFieldName());
         assertEquals("foo1.tab", item.getName());
@@ -491,7 +489,7 @@ public class FileUploadTest {
         try {
             item.openStream();
             Assert.fail("Attempt to open a closed stream did not throw an exception");
-        } catch (IOException ioe) {
+        } catch (final IOException ioe) {
             // Expected
         }
 


(commons-fileupload) 06/11: Remove trailing whitespace

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch 1.x
in repository https://gitbox.apache.org/repos/asf/commons-fileupload.git

commit 8342754c0e422249be367646d4793721dbc659d6
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Thu Jun 5 07:51:13 2025 -0400

    Remove trailing whitespace
---
 src/test/java/org/apache/commons/fileupload/Util.java | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/test/java/org/apache/commons/fileupload/Util.java b/src/test/java/org/apache/commons/fileupload/Util.java
index c4a7f028..f1c6cc3a 100644
--- a/src/test/java/org/apache/commons/fileupload/Util.java
+++ b/src/test/java/org/apache/commons/fileupload/Util.java
@@ -58,7 +58,7 @@ public class Util {
         final byte[] bytes = content.getBytes("US-ASCII");
         return parseUpload(upload, bytes, Constants.CONTENT_TYPE);
     }
-    
+
     private Util() {
         // empty
     }