+ 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();
+
+ in.skip(4);
+ checkArrays(this, "ONLY", in, new byte[] { 12, 51, 11, 12 });
+ }
+ });
+
+ 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();
+
+ // yes
+ assertEquals("It actually starts with that", true,
+ in.startsWith(new byte[] { 42 }));
+ assertEquals("It actually starts with that", true,
+ in.startsWith(new byte[] { 42, 12 }));
+ assertEquals("It actually is the same array", true,
+ in.startsWith(data));
+
+ // no
+ assertEquals("It actually does not start with that", false,
+ in.startsWith(new byte[] { 12 }));
+ assertEquals("It actually does not start with that", false,
+ in.startsWith(
+ new byte[] { 42, 12, 0, 127, 12, 51, 11, 11 }));
+
+ // too big
+ try {
+ in.startsWith(
+ new byte[] { 42, 12, 0, 127, 12, 51, 11, 12, 0 });
+ fail("Searching a prefix bigger than the array should throw an IOException");
+ } catch (IOException e) {
+ }
+ }
+ });
+
+ addTest(new TestCase("Starts with strings") {
+ @Override
+ public void test() throws Exception {
+ String text = "Fanfan et Toto vont à la mer";
+ byte[] data = text.getBytes("UTF-8");
+ NextableInputStream in = new NextableInputStream(data, null);
+ in.next();
+
+ // yes
+ assertEquals("It actually starts with that", true,
+ in.startsWiths("F"));
+ assertEquals("It actually starts with that", true,
+ in.startsWiths("Fanfan et"));
+ assertEquals("It actually is the same text", true,
+ in.startsWiths(text));
+
+ // no
+ assertEquals("It actually does not start with that", false,
+ in.startsWiths("Toto"));
+ assertEquals("It actually does not start with that", false,
+ in.startsWiths("Fanfan et Toto vont à la mee"));
+
+ // too big
+ try {
+ in.startsWiths("Fanfan et Toto vont à la mer.");
+ fail("Searching a prefix bigger than the array should throw an IOException");
+ } catch (IOException e) {
+ }