|
27 | 27 | import java.util.concurrent.atomic.AtomicBoolean; |
28 | 28 |
|
29 | 29 | import org.apache.commons.io.IOUtils; |
| 30 | +import org.apache.commons.lang3.mutable.MutableInt; |
30 | 31 | import org.junit.jupiter.api.Test; |
31 | 32 | import org.junit.jupiter.params.ParameterizedTest; |
32 | 33 | import org.junit.jupiter.params.provider.ValueSource; |
|
36 | 37 | */ |
37 | 38 | public class BoundedInputStreamTest { |
38 | 39 |
|
39 | | - private void compare(final String msg, final byte[] expected, final byte[] actual) { |
40 | | - assertEquals(expected.length, actual.length, msg + " length"); |
| 40 | + private void compare(final String message, final byte[] expected, final byte[] actual) { |
| 41 | + assertEquals(expected.length, actual.length, () -> message + " (array length equals check)"); |
| 42 | + final MutableInt mi = new MutableInt(); |
41 | 43 | for (int i = 0; i < expected.length; i++) { |
42 | | - assertEquals(expected[i], actual[i], msg + " byte[" + i + "]"); |
| 44 | + mi.setValue(i); |
| 45 | + assertEquals(expected[i], actual[i], () -> message + " byte[" + mi + "]"); |
43 | 46 | } |
44 | 47 | } |
45 | 48 |
|
@@ -144,6 +147,107 @@ public void testCounts(final long startCount) throws Exception { |
144 | 147 | } |
145 | 148 | } |
146 | 149 |
|
| 150 | + @Test |
| 151 | + public void testMarkReset() throws Exception { |
| 152 | + final byte[] helloWorld = "Hello World".getBytes(StandardCharsets.UTF_8); |
| 153 | + final int helloWorldLen = helloWorld.length; |
| 154 | + final byte[] hello = "Hello".getBytes(StandardCharsets.UTF_8); |
| 155 | + final byte[] world = " World".getBytes(StandardCharsets.UTF_8); |
| 156 | + final int helloLen = hello.length; |
| 157 | + // limit = -1 |
| 158 | + try (BoundedInputStream bounded = BoundedInputStream.builder().setInputStream(new ByteArrayInputStream(helloWorld)).get()) { |
| 159 | + assertTrue(bounded.markSupported()); |
| 160 | + bounded.mark(0); |
| 161 | + compare("limit = -1", helloWorld, IOUtils.toByteArray(bounded)); |
| 162 | + // should be invariant |
| 163 | + assertTrue(bounded.markSupported()); |
| 164 | + // again |
| 165 | + bounded.reset(); |
| 166 | + compare("limit = -1", hello, IOUtils.toByteArray(bounded, helloLen)); |
| 167 | + bounded.mark(helloWorldLen); |
| 168 | + compare("limit = -1", world, IOUtils.toByteArray(bounded)); |
| 169 | + bounded.reset(); |
| 170 | + compare("limit = -1", world, IOUtils.toByteArray(bounded)); |
| 171 | + // should be invariant |
| 172 | + assertTrue(bounded.markSupported()); |
| 173 | + } |
| 174 | + // limit = 0 |
| 175 | + try (BoundedInputStream bounded = BoundedInputStream.builder().setInputStream(new ByteArrayInputStream(helloWorld)).setMaxCount(0).get()) { |
| 176 | + assertTrue(bounded.markSupported()); |
| 177 | + bounded.mark(0); |
| 178 | + compare("limit = 0", IOUtils.EMPTY_BYTE_ARRAY, IOUtils.toByteArray(bounded)); |
| 179 | + // should be invariant |
| 180 | + assertTrue(bounded.markSupported()); |
| 181 | + // again |
| 182 | + bounded.reset(); |
| 183 | + compare("limit = 0", IOUtils.EMPTY_BYTE_ARRAY, IOUtils.toByteArray(bounded)); |
| 184 | + bounded.mark(helloWorldLen); |
| 185 | + compare("limit = 0", IOUtils.EMPTY_BYTE_ARRAY, IOUtils.toByteArray(bounded)); |
| 186 | + // should be invariant |
| 187 | + assertTrue(bounded.markSupported()); |
| 188 | + } |
| 189 | + // limit = length |
| 190 | + try (BoundedInputStream bounded = BoundedInputStream.builder().setInputStream(new ByteArrayInputStream(helloWorld)) |
| 191 | + .setMaxCount(helloWorld.length).get()) { |
| 192 | + assertTrue(bounded.markSupported()); |
| 193 | + bounded.mark(0); |
| 194 | + compare("limit = length", helloWorld, IOUtils.toByteArray(bounded)); |
| 195 | + // should be invariant |
| 196 | + assertTrue(bounded.markSupported()); |
| 197 | + // again |
| 198 | + bounded.reset(); |
| 199 | + compare("limit = length", hello, IOUtils.toByteArray(bounded, helloLen)); |
| 200 | + bounded.mark(helloWorldLen); |
| 201 | + compare("limit = length", world, IOUtils.toByteArray(bounded)); |
| 202 | + bounded.reset(); |
| 203 | + compare("limit = length", world, IOUtils.toByteArray(bounded)); |
| 204 | + // should be invariant |
| 205 | + assertTrue(bounded.markSupported()); |
| 206 | + } |
| 207 | + // limit > length |
| 208 | + try (BoundedInputStream bounded = BoundedInputStream.builder().setInputStream(new ByteArrayInputStream(helloWorld)) |
| 209 | + .setMaxCount(helloWorld.length + 1).get()) { |
| 210 | + assertTrue(bounded.markSupported()); |
| 211 | + bounded.mark(0); |
| 212 | + compare("limit > length", helloWorld, IOUtils.toByteArray(bounded)); |
| 213 | + // should be invariant |
| 214 | + assertTrue(bounded.markSupported()); |
| 215 | + // again |
| 216 | + bounded.reset(); |
| 217 | + compare("limit > length", helloWorld, IOUtils.toByteArray(bounded)); |
| 218 | + bounded.reset(); |
| 219 | + compare("limit > length", hello, IOUtils.toByteArray(bounded, helloLen)); |
| 220 | + bounded.mark(helloWorldLen); |
| 221 | + compare("limit > length", world, IOUtils.toByteArray(bounded)); |
| 222 | + bounded.reset(); |
| 223 | + compare("limit > length", world, IOUtils.toByteArray(bounded)); |
| 224 | + // should be invariant |
| 225 | + assertTrue(bounded.markSupported()); |
| 226 | + } |
| 227 | + // limit < length |
| 228 | + try (BoundedInputStream bounded = BoundedInputStream.builder().setInputStream(new ByteArrayInputStream(helloWorld)) |
| 229 | + .setMaxCount(helloWorld.length - (hello.length + 1)).get()) { |
| 230 | + assertTrue(bounded.markSupported()); |
| 231 | + bounded.mark(0); |
| 232 | + compare("limit < length", hello, IOUtils.toByteArray(bounded)); |
| 233 | + // should be invariant |
| 234 | + assertTrue(bounded.markSupported()); |
| 235 | + // again |
| 236 | + bounded.reset(); |
| 237 | + compare("limit < length", hello, IOUtils.toByteArray(bounded)); |
| 238 | + // |
| 239 | + bounded.reset(); |
| 240 | + compare("limit < length", hello, IOUtils.toByteArray(bounded, helloLen)); |
| 241 | + bounded.mark(helloWorldLen); |
| 242 | + compare("limit < length", IOUtils.EMPTY_BYTE_ARRAY, IOUtils.toByteArray(bounded)); |
| 243 | + bounded.reset(); |
| 244 | + compare("limit < length", IOUtils.EMPTY_BYTE_ARRAY, IOUtils.toByteArray(bounded)); |
| 245 | + |
| 246 | + // should be invariant |
| 247 | + assertTrue(bounded.markSupported()); |
| 248 | + } |
| 249 | + } |
| 250 | + |
147 | 251 | @SuppressWarnings("deprecation") |
148 | 252 | @Test |
149 | 253 | public void testOnMaxLength() throws Exception { |
@@ -326,35 +430,70 @@ public void testReadSingle() throws Exception { |
326 | 430 | public void testReset() throws Exception { |
327 | 431 | final byte[] helloWorld = "Hello World".getBytes(StandardCharsets.UTF_8); |
328 | 432 | final byte[] hello = "Hello".getBytes(StandardCharsets.UTF_8); |
| 433 | + // limit = -1 |
329 | 434 | try (BoundedInputStream bounded = BoundedInputStream.builder().setInputStream(new ByteArrayInputStream(helloWorld)).get()) { |
330 | 435 | assertTrue(bounded.markSupported()); |
| 436 | + bounded.reset(); |
| 437 | + compare("limit = -1", helloWorld, IOUtils.toByteArray(bounded)); |
| 438 | + // should be invariant |
| 439 | + assertTrue(bounded.markSupported()); |
| 440 | + // again |
| 441 | + bounded.reset(); |
331 | 442 | compare("limit = -1", helloWorld, IOUtils.toByteArray(bounded)); |
332 | 443 | // should be invariant |
333 | 444 | assertTrue(bounded.markSupported()); |
334 | 445 | } |
| 446 | + // limit = 0 |
335 | 447 | try (BoundedInputStream bounded = BoundedInputStream.builder().setInputStream(new ByteArrayInputStream(helloWorld)).setMaxCount(0).get()) { |
336 | 448 | assertTrue(bounded.markSupported()); |
| 449 | + bounded.reset(); |
| 450 | + compare("limit = 0", IOUtils.EMPTY_BYTE_ARRAY, IOUtils.toByteArray(bounded)); |
| 451 | + // should be invariant |
| 452 | + assertTrue(bounded.markSupported()); |
| 453 | + // again |
| 454 | + bounded.reset(); |
337 | 455 | compare("limit = 0", IOUtils.EMPTY_BYTE_ARRAY, IOUtils.toByteArray(bounded)); |
338 | 456 | // should be invariant |
339 | 457 | assertTrue(bounded.markSupported()); |
340 | 458 | } |
| 459 | + // limit = length |
341 | 460 | try (BoundedInputStream bounded = BoundedInputStream.builder().setInputStream(new ByteArrayInputStream(helloWorld)) |
342 | 461 | .setMaxCount(helloWorld.length).get()) { |
343 | 462 | assertTrue(bounded.markSupported()); |
| 463 | + bounded.reset(); |
| 464 | + compare("limit = length", helloWorld, IOUtils.toByteArray(bounded)); |
| 465 | + // should be invariant |
| 466 | + assertTrue(bounded.markSupported()); |
| 467 | + // again |
| 468 | + bounded.reset(); |
344 | 469 | compare("limit = length", helloWorld, IOUtils.toByteArray(bounded)); |
345 | 470 | // should be invariant |
346 | 471 | assertTrue(bounded.markSupported()); |
347 | 472 | } |
| 473 | + // limit > length |
348 | 474 | try (BoundedInputStream bounded = BoundedInputStream.builder().setInputStream(new ByteArrayInputStream(helloWorld)) |
349 | 475 | .setMaxCount(helloWorld.length + 1).get()) { |
350 | 476 | assertTrue(bounded.markSupported()); |
| 477 | + bounded.reset(); |
| 478 | + compare("limit > length", helloWorld, IOUtils.toByteArray(bounded)); |
| 479 | + // should be invariant |
| 480 | + assertTrue(bounded.markSupported()); |
| 481 | + // again |
| 482 | + bounded.reset(); |
351 | 483 | compare("limit > length", helloWorld, IOUtils.toByteArray(bounded)); |
352 | 484 | // should be invariant |
353 | 485 | assertTrue(bounded.markSupported()); |
354 | 486 | } |
| 487 | + // limit < length |
355 | 488 | try (BoundedInputStream bounded = BoundedInputStream.builder().setInputStream(new ByteArrayInputStream(helloWorld)) |
356 | 489 | .setMaxCount(helloWorld.length - 6).get()) { |
357 | 490 | assertTrue(bounded.markSupported()); |
| 491 | + bounded.reset(); |
| 492 | + compare("limit < length", hello, IOUtils.toByteArray(bounded)); |
| 493 | + // should be invariant |
| 494 | + assertTrue(bounded.markSupported()); |
| 495 | + // again |
| 496 | + bounded.reset(); |
358 | 497 | compare("limit < length", hello, IOUtils.toByteArray(bounded)); |
359 | 498 | // should be invariant |
360 | 499 | assertTrue(bounded.markSupported()); |
|
0 commit comments