fix limit in replace for BufferedInputStream
[fanfix.git] / src / be / nikiroo / utils / test_code / NextableInputStreamTest.java
index 87d64ac20d86cb10ef7941698f2ba69901fc9b2c..4e5982363af1925d4b7422ba0eeb4a60ec69204a 100644 (file)
@@ -3,8 +3,8 @@ package be.nikiroo.utils.test_code;
 import java.io.ByteArrayInputStream;
 
 import be.nikiroo.utils.IOUtils;
-import be.nikiroo.utils.NextableInputStream;
-import be.nikiroo.utils.NextableInputStreamStep;
+import be.nikiroo.utils.streams.NextableInputStream;
+import be.nikiroo.utils.streams.NextableInputStreamStep;
 import be.nikiroo.utils.test.TestCase;
 import be.nikiroo.utils.test.TestLauncher;
 
@@ -18,15 +18,7 @@ public class NextableInputStreamTest extends TestLauncher {
                                byte[] expected = new byte[] { 42, 12, 0, 127 };
                                NextableInputStream in = new NextableInputStream(
                                                new ByteArrayInputStream(expected), null);
-                               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);
                        }
                });
 
@@ -38,7 +30,7 @@ public class NextableInputStreamTest extends TestLauncher {
                                                new ByteArrayInputStream(expected),
                                                new NextableInputStreamStep(12));
 
-                               checkNext(this, false, "FIRST", in, new byte[] { 42 });
+                               checkNext(this, "FIRST", in, new byte[] { 42 });
                        }
                });
 
@@ -50,9 +42,9 @@ public class NextableInputStreamTest extends TestLauncher {
                                                new ByteArrayInputStream(data),
                                                new NextableInputStreamStep(12));
 
-                               checkNext(this, false, "FIRST", in, new byte[] { 42 });
-                               checkNext(this, true, "SECOND", in, new byte[] { 0, 127 });
-                               checkNext(this, true, "THIRD", in, new byte[] { 51, 11 });
+                               checkNext(this, "FIRST", in, new byte[] { 42 });
+                               checkNext(this, "SECOND", in, new byte[] { 0, 127 });
+                               checkNext(this, "THIRD", in, new byte[] { 51, 11 });
                        }
                });
 
@@ -66,14 +58,14 @@ public class NextableInputStreamTest extends TestLauncher {
                                NextableInputStream subIn12 = new NextableInputStream(in4,
                                                new NextableInputStreamStep(12));
 
-                               checkNext(this, false, "SUB FIRST", subIn12, new byte[] { 42 });
-                               checkNext(this, true, "SUB SECOND", subIn12, new byte[] { 0 });
+                               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, true, "MAIN LAST", in4,
-                                               new byte[] { 127, 12, 5 });
+                               checkNext(this, "MAIN LAST", in4, new byte[] { 127, 12, 5 });
                        }
                });
 
@@ -87,18 +79,255 @@ public class NextableInputStreamTest extends TestLauncher {
                                                new ByteArrayInputStream(data),
                                                new NextableInputStreamStep('\n'));
 
-                               checkNext(this, false, "FIRST", in, ln1.getBytes("UTF-8"));
-                               checkNext(this, true, "SECOND", in, ln2.getBytes("UTF-8"));
+                               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();
+
+                               // 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
+                               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();
+                       }
+               });
+
+               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.startsWith("F"));
+                               assertEquals("It actually starts with that", true,
+                                               in.startsWith("Fanfan et"));
+                               assertEquals("It actually is the same text", true,
+                                               in.startsWith(text));
+
+                               // no
+                               assertEquals("It actually does not start with that", false,
+                                               in.startsWith("Toto"));
+                               assertEquals("It actually does not start with that", false,
+                                               in.startsWith("Fanfan et Toto vont à la mee"));
+                               
+                               // too big
+                               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();
+                       }
+               });
+
+               addTest(new TestCase("Starts With strings + steps") {
+                       @Override
+                       public void test() throws Exception {
+                               String data = "{\nREF: fanfan\n}";
+                               NextableInputStream in = new NextableInputStream(
+                                               data.getBytes("UTF-8"), new NextableInputStreamStep(
+                                                               '\n'));
+                               in.next();
+
+                               assertEquals("STARTS_WITH OK", true, in.startsWith("{"));
+                               in.skip(1);
+                               assertEquals("STARTS_WITH WHEN SPENT", false,
+                                               in.startsWith("{"));
+
+                               checkNext(this, "PARTIAL CONTENT", in,
+                                               "REF: fanfan".getBytes("UTF-8"));
+                       }
+               });
+
+               addTest(new TestCase("InputStream is(String)") {
+                       @Override
+                       public void test() throws Exception {
+                               String data = "{\nREF: fanfan\n}";
+                               NextableInputStream in = new NextableInputStream(
+                                               new ByteArrayInputStream(data.getBytes("UTF-8")),
+                                               new NextableInputStreamStep('\n'));
+
+                               in.next();
+                               assertEquals("Item 1 OK", true, in.is("{"));
+                               assertEquals("Item 1 KO_1", false, in.is("|"));
+                               assertEquals("Item 1 KO_2", false, in.is("{}"));
+                               in.skip(1);
+                               in.next();
+                               assertEquals("Item 2 OK", true, in.is("REF: fanfan"));
+                               assertEquals("Item 2 KO", false, in.is("REF: fanfan."));
+                               IOUtils.readSmallStream(in);
+                               in.next();
+                               assertEquals("Item 3 OK", true, in.is("}"));
+
+                               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, boolean callNext, String prefix,
+       static void checkNext(TestCase test, String prefix, NextableInputStream in,
+                       byte[] expected) throws Exception {
+               test.assertEquals("Cannot get " + prefix + " entry", true, in.next());
+               checkArrays(test, prefix, in, expected);
+       }
+
+       static void checkNextAll(TestCase test, String prefix,
+                       NextableInputStream in, byte[] expected) throws Exception {
+               test.assertEquals("Cannot get " + prefix + " entries", true,
+                               in.nextAll());
+               checkArrays(test, prefix, in, expected);
+       }
+
+       static void checkArrays(TestCase test, String prefix,
                        NextableInputStream in, byte[] expected) throws Exception {
-               if (callNext) {
-                       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",