+
+ addTest(new TestCase("getBytesRead()") {
+ @Override
+ public void test() throws Exception {
+ byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 };
+ NextableInputStream in = new NextableInputStream(
+ new ByteArrayInputStream(data),
+ new NextableInputStreamStep(12));
+
+ in.nextAll();
+ IOUtils.toByteArray(in);
+
+ assertEquals("The number of bytes read is not correct",
+ data.length, in.getBytesRead());
+ }
+ });
+
+ addTest(new TestCase("bytes array input") {
+ @Override
+ public void test() throws Exception {
+ byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 };
+ NextableInputStream in = new NextableInputStream(data,
+ new NextableInputStreamStep(12));
+
+ checkNext(this, "FIRST", in, new byte[] { 42 });
+ checkNext(this, "SECOND", in, new byte[] { 0, 127 });
+ checkNext(this, "THIRD", in, new byte[] { 51, 11 });
+ }
+ });
+ }
+
+ static void checkNext(TestCase test, String prefix, NextableInputStream in,
+ byte[] expected) throws Exception {
+ test.assertEquals("Cannot get " + prefix + " entry", true, in.next());
+ byte[] actual = IOUtils.toByteArray(in);
+ test.assertEquals("The " + prefix
+ + " resulting array has not the correct number of items",
+ expected.length, actual.length);
+ for (int i = 0; i < actual.length; i++) {
+ test.assertEquals("Item " + i + " (0-based) is not the same",
+ expected[i], actual[i]);
+ }
+ }
+
+ static void checkNextAll(TestCase test, String prefix,
+ NextableInputStream in, byte[] expected) throws Exception {
+ test.assertEquals("Cannot get " + prefix + " entries", true,
+ in.nextAll());
+ byte[] actual = IOUtils.toByteArray(in);
+ test.assertEquals("The " + prefix
+ + " resulting array has not the correct number of items",
+ expected.length, actual.length);
+ for (int i = 0; i < actual.length; i++) {
+ test.assertEquals("Item " + i + " (0-based) is not the same",
+ expected[i], actual[i]);
+ }