X-Git-Url: http://git.nikiroo.be/?p=fanfix.git;a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Ftest_code%2FNextableInputStreamTest.java;h=4e5982363af1925d4b7422ba0eeb4a60ec69204a;hp=f8031f0d3b040035d77087b51e3534052fd801ae;hb=6d765e484c2f50fd8f5cb3af425b59c51adaca0d;hpb=d251f3dd38a8f9d369a7cf627185eacc4f66ece5 diff --git a/src/be/nikiroo/utils/test_code/NextableInputStreamTest.java b/src/be/nikiroo/utils/test_code/NextableInputStreamTest.java index f8031f0..4e59823 100644 --- a/src/be/nikiroo/utils/test_code/NextableInputStreamTest.java +++ b/src/be/nikiroo/utils/test_code/NextableInputStreamTest.java @@ -1,7 +1,6 @@ package be.nikiroo.utils.test_code; import java.io.ByteArrayInputStream; -import java.io.IOException; import be.nikiroo.utils.IOUtils; import be.nikiroo.utils.streams.NextableInputStream; @@ -19,16 +18,7 @@ public class NextableInputStreamTest extends TestLauncher { byte[] expected = new byte[] { 42, 12, 0, 127 }; NextableInputStream in = new NextableInputStream( new ByteArrayInputStream(expected), null); - in.next(); - byte[] actual = IOUtils.toByteArray(in); - - assertEquals( - "The resulting array has not the same number of items", - expected.length, actual.length); - for (int i = 0; i < expected.length; i++) { - assertEquals("Item " + i + " (0-based) is not the same", - expected[i], actual[i]); - } + checkNext(this, "READ", in, expected); } }); @@ -186,12 +176,10 @@ public class NextableInputStreamTest extends TestLauncher { 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) { - } + assertEquals( + "A search term bigger than the whole data cannot be found in the data", + false, in.startsWith(new byte[] { 42, 12, 0, 127, 12, + 51, 11, 12, 0 })); in.close(); } @@ -218,13 +206,11 @@ public class NextableInputStreamTest extends TestLauncher { in.startsWith("Toto")); assertEquals("It actually does not start with that", false, in.startsWith("Fanfan et Toto vont à la mee")); - + // too big - try { - in.startsWith("Fanfan et Toto vont à la mer."); - fail("Searching a prefix bigger than the array should throw an IOException"); - } catch (IOException e) { - } + assertEquals( + "A search term bigger than the whole data cannot be found in the data", + false, in.startsWith("Fanfan et Toto vont à la mer.")); in.close(); } @@ -272,6 +258,59 @@ public class NextableInputStreamTest extends TestLauncher { in.close(); } }); + + addTest(new TestCase("Bytes NextAll test") { + @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, "SECOND", in, new byte[] { 0, 127, 12, 51, + 11, 12 }); + } + }); + + addTest(new TestCase("String NextAll test") { + @Override + public void test() throws Exception { + String d1 = "^java.lang.String"; + String d2 = "\"http://example.com/query.html\""; + String data = d1 + ":" + d2; + NextableInputStream in = new NextableInputStream( + new ByteArrayInputStream(data.getBytes("UTF-8")), + new NextableInputStreamStep(':')); + + checkNext(this, "FIRST", in, d1.getBytes("UTF-8")); + checkNextAll(this, "SECOND", in, d2.getBytes("UTF-8")); + } + }); + + addTest(new TestCase("NextAll in Next test") { + @Override + public void test() throws Exception { + String line1 = "première ligne"; + String d1 = "^java.lang.String"; + String d2 = "\"http://example.com/query.html\""; + String line3 = "end of lines"; + String data = line1 + "\n" + d1 + ":" + d2 + "\n" + line3; + + NextableInputStream inL = new NextableInputStream( + new ByteArrayInputStream(data.getBytes("UTF-8")), + new NextableInputStreamStep('\n')); + + checkNext(this, "Line 1", inL, line1.getBytes("UTF-8")); + inL.next(); + + NextableInputStream in = new NextableInputStream(inL, + new NextableInputStreamStep(':')); + + checkNext(this, "Line 2 FIRST", in, d1.getBytes("UTF-8")); + checkNextAll(this, "Line 2 SECOND", in, d2.getBytes("UTF-8")); + } + }); } static void checkNext(TestCase test, String prefix, NextableInputStream in,