Skip to content

Commit 3bd1826

Browse files
committed
Assert markSupported()
1 parent 8167452 commit 3bd1826

1 file changed

Lines changed: 72 additions & 0 deletions

File tree

src/test/java/org/apache/commons/io/input/BoundedInputStreamTest.java

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ public void testCounts(final long startCount) throws Exception {
8686
assertEquals(readCount + actualStart, bounded.getCount());
8787
assertEquals(0, bounded.getRemaining());
8888
assertEquals(0, bounded.available());
89+
// should be invariant
90+
assertTrue(bounded.markSupported());
8991
}
9092
// limit > length
9193
final int maxCountP1 = helloWorld.length + 1;
@@ -115,6 +117,8 @@ public void testCounts(final long startCount) throws Exception {
115117
assertEquals(maxCountP1, bounded.getMaxLength());
116118
assertEquals(readCount + actualStart, bounded.getCount());
117119
assertEquals(Math.max(0, maxCountP1 - bounded.getCount()), bounded.getRemaining());
120+
// should be invariant
121+
assertTrue(bounded.markSupported());
118122
}
119123
// limit < length
120124
try (BoundedInputStream bounded = new BoundedInputStream(new ByteArrayInputStream(helloWorld), hello.length)) {
@@ -135,6 +139,8 @@ public void testCounts(final long startCount) throws Exception {
135139
assertEquals(hello.length, bounded.getMaxLength());
136140
assertEquals(readCount, bounded.getCount());
137141
assertEquals(bounded.getMaxLength() - readCount, bounded.getRemaining());
142+
// should be invariant
143+
assertTrue(bounded.markSupported());
138144
}
139145
}
140146

@@ -175,6 +181,8 @@ protected void onMaxLength(final long max, final long readCount) {
175181
assertEquals(readCount, bounded.getCount());
176182
assertEquals(bounded.getMaxLength() - readCount, bounded.getRemaining());
177183
assertTrue(boolRef.get());
184+
// should be invariant
185+
assertTrue(bounded.markSupported());
178186
}
179187
// limit > length
180188
boolRef.set(false);
@@ -204,6 +212,8 @@ protected void onMaxLength(final long max, final long readCount) {
204212
assertEquals(readCount, bounded.getCount());
205213
assertEquals(bounded.getMaxLength() - readCount, bounded.getRemaining());
206214
assertFalse(boolRef.get());
215+
// should be invariant
216+
assertTrue(bounded.markSupported());
207217
}
208218
// limit < length
209219
boolRef.set(false);
@@ -231,6 +241,8 @@ protected void onMaxLength(final long max, final long readCount) {
231241
assertEquals(readCount, bounded.getCount());
232242
assertEquals(bounded.getMaxLength() - readCount, bounded.getRemaining());
233243
assertTrue(boolRef.get());
244+
// should be invariant
245+
assertTrue(bounded.markSupported());
234246
}
235247
}
236248

@@ -239,22 +251,76 @@ public void testReadArray() throws Exception {
239251
final byte[] helloWorld = "Hello World".getBytes(StandardCharsets.UTF_8);
240252
final byte[] hello = "Hello".getBytes(StandardCharsets.UTF_8);
241253
try (BoundedInputStream bounded = BoundedInputStream.builder().setInputStream(new ByteArrayInputStream(helloWorld)).get()) {
254+
assertTrue(bounded.markSupported());
255+
compare("limit = -1", helloWorld, IOUtils.toByteArray(bounded));
256+
// should be invariant
257+
assertTrue(bounded.markSupported());
258+
}
259+
try (BoundedInputStream bounded = BoundedInputStream.builder().setInputStream(new ByteArrayInputStream(helloWorld)).setMaxCount(0).get()) {
260+
assertTrue(bounded.markSupported());
261+
compare("limit = 0", IOUtils.EMPTY_BYTE_ARRAY, IOUtils.toByteArray(bounded));
262+
// should be invariant
263+
assertTrue(bounded.markSupported());
264+
}
265+
try (BoundedInputStream bounded = BoundedInputStream.builder().setInputStream(new ByteArrayInputStream(helloWorld))
266+
.setMaxCount(helloWorld.length).get()) {
267+
assertTrue(bounded.markSupported());
268+
compare("limit = length", helloWorld, IOUtils.toByteArray(bounded));
269+
// should be invariant
270+
assertTrue(bounded.markSupported());
271+
}
272+
try (BoundedInputStream bounded = BoundedInputStream.builder().setInputStream(new ByteArrayInputStream(helloWorld))
273+
.setMaxCount(helloWorld.length + 1).get()) {
274+
assertTrue(bounded.markSupported());
275+
compare("limit > length", helloWorld, IOUtils.toByteArray(bounded));
276+
// should be invariant
277+
assertTrue(bounded.markSupported());
278+
}
279+
try (BoundedInputStream bounded = BoundedInputStream.builder().setInputStream(new ByteArrayInputStream(helloWorld))
280+
.setMaxCount(helloWorld.length - 6).get()) {
281+
assertTrue(bounded.markSupported());
282+
compare("limit < length", hello, IOUtils.toByteArray(bounded));
283+
// should be invariant
284+
assertTrue(bounded.markSupported());
285+
}
286+
}
287+
288+
@Test
289+
public void testReset() throws Exception {
290+
final byte[] helloWorld = "Hello World".getBytes(StandardCharsets.UTF_8);
291+
final byte[] hello = "Hello".getBytes(StandardCharsets.UTF_8);
292+
try (BoundedInputStream bounded = BoundedInputStream.builder().setInputStream(new ByteArrayInputStream(helloWorld)).get()) {
293+
assertTrue(bounded.markSupported());
242294
compare("limit = -1", helloWorld, IOUtils.toByteArray(bounded));
295+
// should be invariant
296+
assertTrue(bounded.markSupported());
243297
}
244298
try (BoundedInputStream bounded = BoundedInputStream.builder().setInputStream(new ByteArrayInputStream(helloWorld)).setMaxCount(0).get()) {
299+
assertTrue(bounded.markSupported());
245300
compare("limit = 0", IOUtils.EMPTY_BYTE_ARRAY, IOUtils.toByteArray(bounded));
301+
// should be invariant
302+
assertTrue(bounded.markSupported());
246303
}
247304
try (BoundedInputStream bounded = BoundedInputStream.builder().setInputStream(new ByteArrayInputStream(helloWorld))
248305
.setMaxCount(helloWorld.length).get()) {
306+
assertTrue(bounded.markSupported());
249307
compare("limit = length", helloWorld, IOUtils.toByteArray(bounded));
308+
// should be invariant
309+
assertTrue(bounded.markSupported());
250310
}
251311
try (BoundedInputStream bounded = BoundedInputStream.builder().setInputStream(new ByteArrayInputStream(helloWorld))
252312
.setMaxCount(helloWorld.length + 1).get()) {
313+
assertTrue(bounded.markSupported());
253314
compare("limit > length", helloWorld, IOUtils.toByteArray(bounded));
315+
// should be invariant
316+
assertTrue(bounded.markSupported());
254317
}
255318
try (BoundedInputStream bounded = BoundedInputStream.builder().setInputStream(new ByteArrayInputStream(helloWorld))
256319
.setMaxCount(helloWorld.length - 6).get()) {
320+
assertTrue(bounded.markSupported());
257321
compare("limit < length", hello, IOUtils.toByteArray(bounded));
322+
// should be invariant
323+
assertTrue(bounded.markSupported());
258324
}
259325
}
260326

@@ -270,6 +336,8 @@ public void testReadSingle() throws Exception {
270336
assertEquals(helloWorld[i], bounded.read(), "limit = length byte[" + i + "]");
271337
}
272338
assertEquals(-1, bounded.read(), "limit = length end");
339+
// should be invariant
340+
assertTrue(bounded.markSupported());
273341
}
274342
// limit > length
275343
try (BoundedInputStream bounded = new BoundedInputStream(new ByteArrayInputStream(helloWorld), helloWorld.length + 1)) {
@@ -278,6 +346,8 @@ public void testReadSingle() throws Exception {
278346
assertEquals(helloWorld[i], bounded.read(), "limit > length byte[" + i + "]");
279347
}
280348
assertEquals(-1, bounded.read(), "limit > length end");
349+
// should be invariant
350+
assertTrue(bounded.markSupported());
281351
}
282352
// limit < length
283353
try (BoundedInputStream bounded = new BoundedInputStream(new ByteArrayInputStream(helloWorld), hello.length)) {
@@ -286,6 +356,8 @@ public void testReadSingle() throws Exception {
286356
assertEquals(hello[i], bounded.read(), "limit < length byte[" + i + "]");
287357
}
288358
assertEquals(-1, bounded.read(), "limit < length end");
359+
// should be invariant
360+
assertTrue(bounded.markSupported());
289361
}
290362
}
291363
}

0 commit comments

Comments
 (0)