ppkarwasz commented on code in PR #785:
URL: https://github.com/apache/commons-io/pull/785#discussion_r2367851713


##########
src/test/java/org/apache/commons/io/IOUtilsTest.java:
##########
@@ -1965,4 +1970,23 @@ void testWriteLittleString() throws IOException {
         }
     }
 
+    @Test
+    void testToByteArray_ThrowsIOExceptionOnHugeStream() {
+        try (MockedStatic<IOUtils> utils = Mockito.mockStatic(IOUtils.class, 
Mockito.CALLS_REAL_METHODS)) {
+            // Prepare the mocks
+            final UnsynchronizedByteArrayOutputStream mockOutputStream =
+                    mock(UnsynchronizedByteArrayOutputStream.class);
+            utils.when(() -> IOUtils.copyToOutputStream(
+                            Mockito.any(InputStream.class), Mockito.anyLong(), 
Mockito.anyInt()))
+                    .thenReturn(mockOutputStream);
+            
when(mockOutputStream.size()).thenReturn(IOUtils.SOFT_MAX_ARRAY_LENGTH + 1);
+
+            // Test and check
+            final InputStream mockInputStream = mock(InputStream.class);
+            final IOException exception = assertThrows(IOException.class, () 
-> IOUtils.toByteArray(mockInputStream));
+            assertTrue(
+                    exception.getMessage().contains(String.format("%,d", 
IOUtils.SOFT_MAX_ARRAY_LENGTH)),
+                    "Exception message does not contain the maximum length");
+        }

Review Comment:
   The entire purpose of the test is **not** to consume a real 2 GiB 
inputstream, just to test the exception.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to