+ checkNext(this, "FIRST", in, new byte[] { 42 });
+ checkNext(this, "SECOND", in, new byte[] { 0, 127 });
+ checkNext(this, "THIRD", in, new byte[] { 51, 11 });
+ }
+ });
+
+ addTest(new TestCase("Encapsulation") {
+ @Override
+ public void test() throws Exception {
+ byte[] data = new byte[] { 42, 12, 0, 4, 127, 12, 5 };
+ NextableInputStream in4 = new NextableInputStream(
+ new ByteArrayInputStream(data),
+ new NextableInputStreamStep(4));
+ NextableInputStream subIn12 = new NextableInputStream(in4,
+ new NextableInputStreamStep(12));
+
+ in4.next();
+ checkNext(this, "SUB FIRST", subIn12, new byte[] { 42 });
+ checkNext(this, "SUB SECOND", subIn12, new byte[] { 0 });
+
+ assertEquals("The subIn still has some data", false,
+ subIn12.next());
+
+ checkNext(this, "MAIN LAST", in4, new byte[] { 127, 12, 5 });
+ }
+ });
+
+ addTest(new TestCase("UTF-8 text lines test") {
+ @Override
+ public void test() throws Exception {
+ String ln1 = "Ligne première";
+ String ln2 = "Ligne la deuxième du nom";
+ byte[] data = (ln1 + "\n" + ln2).getBytes("UTF-8");
+ NextableInputStream in = new NextableInputStream(
+ new ByteArrayInputStream(data),
+ new NextableInputStreamStep('\n'));
+
+ checkNext(this, "FIRST", in, ln1.getBytes("UTF-8"));
+ checkNext(this, "SECOND", in, ln2.getBytes("UTF-8"));
+ }
+ });
+
+ addTest(new TestCase("nextAll()") {
+ @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));
+
+ checkNext(this, "FIRST", in, new byte[] { 42 });
+ checkNextAll(this, "REST", in, new byte[] { 0, 127, 12, 51, 11,
+ 12 });
+ assertEquals("The stream still has some data", false, in.next());
+ }
+ });
+
+ 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 });
+ }
+ });
+
+ addTest(new TestCase("Skip data") {
+ @Override
+ public void test() throws Exception {
+ byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 };
+ NextableInputStream in = new NextableInputStream(data, null);
+ in.next();
+
+ byte[] rest = new byte[] { 12, 51, 11, 12 };
+
+ in.skip(4);
+ assertEquals("STARTS_WITH OK_1", true, in.startsWith(rest));
+ assertEquals("STARTS_WITH KO_1", false,
+ in.startsWith(new byte[] { 0 }));
+ assertEquals("STARTS_WITH KO_2", false, in.startsWith(data));
+ assertEquals("STARTS_WITH KO_3", false,
+ in.startsWith(new byte[] { 1, 2, 3 }));
+ assertEquals("STARTS_WITH OK_2", true, in.startsWith(rest));
+ assertEquals("READ REST", IOUtils.readSmallStream(in),
+ new String(rest));
+ in.close();
+ }
+ });
+
+ addTest(new TestCase("Starts with") {
+ @Override
+ public void test() throws Exception {
+ byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 };
+ NextableInputStream in = new NextableInputStream(data, null);
+ in.next();