@@ -1730,6 +1730,21 @@ void testToByteArray_String() throws Exception {
17301730 }
17311731 }
17321732
1733+ @ Test
1734+ void testToByteArray_ThrowsIOExceptionOnHugeStream () {
1735+ try (MockedStatic <IOUtils > utils = Mockito .mockStatic (IOUtils .class , Mockito .CALLS_REAL_METHODS )) {
1736+ // Prepare the mocks
1737+ final UnsynchronizedByteArrayOutputStream mockOutputStream = mock (UnsynchronizedByteArrayOutputStream .class );
1738+ utils .when (() -> IOUtils .copyToOutputStream (Mockito .any (InputStream .class ), Mockito .anyLong (), Mockito .anyInt ())).thenReturn (mockOutputStream );
1739+ when (mockOutputStream .size ()).thenReturn (IOUtils .SOFT_MAX_ARRAY_LENGTH + 1 );
1740+ // Test and check
1741+ final InputStream mockInputStream = mock (InputStream .class );
1742+ final IOException exception = assertThrows (IOException .class , () -> IOUtils .toByteArray (mockInputStream ));
1743+ assertTrue (exception .getMessage ().contains (String .format ("%,d" , IOUtils .SOFT_MAX_ARRAY_LENGTH )),
1744+ "Exception message does not contain the maximum length" );
1745+ }
1746+ }
1747+
17331748 @ Test
17341749 void testToByteArray_URI () throws Exception {
17351750 final URI url = testFile .toURI ();
@@ -1969,24 +1984,4 @@ void testWriteLittleString() throws IOException {
19691984 }
19701985 }
19711986 }
1972-
1973- @ Test
1974- void testToByteArray_ThrowsIOExceptionOnHugeStream () {
1975- try (MockedStatic <IOUtils > utils = Mockito .mockStatic (IOUtils .class , Mockito .CALLS_REAL_METHODS )) {
1976- // Prepare the mocks
1977- final UnsynchronizedByteArrayOutputStream mockOutputStream =
1978- mock (UnsynchronizedByteArrayOutputStream .class );
1979- utils .when (() -> IOUtils .copyToOutputStream (
1980- Mockito .any (InputStream .class ), Mockito .anyLong (), Mockito .anyInt ()))
1981- .thenReturn (mockOutputStream );
1982- when (mockOutputStream .size ()).thenReturn (IOUtils .SOFT_MAX_ARRAY_LENGTH + 1 );
1983-
1984- // Test and check
1985- final InputStream mockInputStream = mock (InputStream .class );
1986- final IOException exception = assertThrows (IOException .class , () -> IOUtils .toByteArray (mockInputStream ));
1987- assertTrue (
1988- exception .getMessage ().contains (String .format ("%,d" , IOUtils .SOFT_MAX_ARRAY_LENGTH )),
1989- "Exception message does not contain the maximum length" );
1990- }
1991- }
19921987}
0 commit comments