|
50 | 50 | import java.io.SequenceInputStream; |
51 | 51 | import java.io.StringReader; |
52 | 52 | import java.io.Writer; |
| 53 | +import java.lang.reflect.InvocationTargetException; |
53 | 54 | import java.net.ServerSocket; |
54 | 55 | import java.net.Socket; |
55 | 56 | import java.net.URI; |
|
65 | 66 | import java.util.Arrays; |
66 | 67 | import java.util.Collections; |
67 | 68 | import java.util.List; |
| 69 | +import java.util.Objects; |
68 | 70 | import java.util.concurrent.atomic.AtomicBoolean; |
69 | 71 | import java.util.function.Consumer; |
70 | 72 | import java.util.function.Supplier; |
|
85 | 87 | import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream; |
86 | 88 | import org.apache.commons.io.test.TestUtils; |
87 | 89 | import org.apache.commons.io.test.ThrowOnCloseReader; |
| 90 | +import org.apache.commons.lang3.JavaVersion; |
88 | 91 | import org.apache.commons.lang3.StringUtils; |
| 92 | +import org.apache.commons.lang3.SystemUtils; |
89 | 93 | import org.apache.commons.lang3.exception.ExceptionUtils; |
90 | 94 | import org.junit.jupiter.api.AfterAll; |
91 | 95 | import org.junit.jupiter.api.BeforeAll; |
@@ -348,6 +352,54 @@ void testByteArrayWithNegativeSize() { |
348 | 352 | assertThrows(NegativeArraySizeException.class, () -> IOUtils.byteArray(-1)); |
349 | 353 | } |
350 | 354 |
|
| 355 | + static Stream<Arguments> testCheckFromIndexSizeValidCases() { |
| 356 | + return Stream.of( |
| 357 | + // Valid cases |
| 358 | + Arguments.of(0, 0, 42), |
| 359 | + Arguments.of(0, 1, 42), |
| 360 | + Arguments.of(0, 42, 42), |
| 361 | + Arguments.of(41, 1, 42), |
| 362 | + Arguments.of(42, 0, 42) |
| 363 | + ); |
| 364 | + } |
| 365 | + |
| 366 | + @ParameterizedTest |
| 367 | + @MethodSource |
| 368 | + void testCheckFromIndexSizeValidCases(int off, int len, int arrayLength) { |
| 369 | + assertDoesNotThrow(() -> IOUtils.checkFromIndexSize(off, len, arrayLength)); |
| 370 | + } |
| 371 | + |
| 372 | + static Stream<Arguments> testCheckFromIndexSizeInvalidCases() { |
| 373 | + return Stream.of( |
| 374 | + Arguments.of(-1, 0, 42), |
| 375 | + Arguments.of(0, -1, 42), |
| 376 | + Arguments.of(0, 0, -1), |
| 377 | + // off + len > arrayLength |
| 378 | + Arguments.of(1, 42, 42), |
| 379 | + Arguments.of(Integer.MAX_VALUE, 1, Integer.MAX_VALUE) |
| 380 | + ); |
| 381 | + } |
| 382 | + |
| 383 | + @ParameterizedTest |
| 384 | + @MethodSource |
| 385 | + void testCheckFromIndexSizeInvalidCases(int off, int len, int arrayLength) { |
| 386 | + final IndexOutOfBoundsException ex = assertThrows(IndexOutOfBoundsException.class, () -> IOUtils.checkFromIndexSize(off, len, arrayLength)); |
| 387 | + assertTrue(ex.getMessage().contains(String.valueOf(off))); |
| 388 | + assertTrue(ex.getMessage().contains(String.valueOf(len))); |
| 389 | + assertTrue(ex.getMessage().contains(String.valueOf(arrayLength))); |
| 390 | + // Optional requirement: compare the exception message for Java 8 and Java 9+ |
| 391 | + if (SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_9)) { |
| 392 | + final IndexOutOfBoundsException jreEx = assertThrows(IndexOutOfBoundsException.class, () -> { |
| 393 | + try { |
| 394 | + Objects.class.getDeclaredMethod("checkFromIndexSize", int.class, int.class, int.class).invoke(null, off, len, arrayLength); |
| 395 | + } catch (InvocationTargetException ite) { |
| 396 | + throw ite.getTargetException(); |
| 397 | + } |
| 398 | + }); |
| 399 | + assertEquals(jreEx.getMessage(), ex.getMessage()); |
| 400 | + } |
| 401 | + } |
| 402 | + |
351 | 403 | @Test |
352 | 404 | void testClose() { |
353 | 405 | assertDoesNotThrow(() -> IOUtils.close((Closeable) null)); |
|
0 commit comments