serial: fix Importer, more tests
authorNiki Roo <niki@nikiroo.be>
Sun, 12 May 2019 12:03:42 +0000 (14:03 +0200)
committerNiki Roo <niki@nikiroo.be>
Sun, 12 May 2019 12:03:42 +0000 (14:03 +0200)
src/be/nikiroo/utils/serial/Importer.java
src/be/nikiroo/utils/streams/NextableInputStream.java
src/be/nikiroo/utils/test_code/NextableInputStreamTest.java
src/be/nikiroo/utils/test_code/SerialTest.java

index 2608db272bf85373b0fcecda52aa8e76563b904c..81814dfdbadf6a71ec2224d14be5926314bbd105 100644 (file)
@@ -232,7 +232,7 @@ public class Importer {
                                nameThenContent.next();
                                String fieldName = IOUtils.readSmallStream(nameThenContent);
 
-                               if (nameThenContent.next() && !nameThenContent.eof()) {
+                               if (nameThenContent.nextAll() && !nameThenContent.eof()) {
                                        // field value is direct or custom
                                        Object value = null;
                                        value = SerialUtils.decode(nameThenContent);
index fb4d01b3101727f7a246c4d2440beed479712a74..dcab472efa0569bf6763425567330a536ac3eeeb 100644 (file)
@@ -218,7 +218,7 @@ public class NextableInputStream extends BufferedInputStream {
                        return true;
                }
 
-               // If started, must be stopped an no more data to continue
+               // If started, must be stopped and no more data to continue
                // i.e., sub-stream must be spent
                if (!stopped || hasMoreData()) {
                        return false;
@@ -265,8 +265,8 @@ public class NextableInputStream extends BufferedInputStream {
                                data = new String(Arrays.copyOfRange(buffer, 0, stop), "UTF-8");
                        } catch (UnsupportedEncodingException e) {
                        }
-                       if (data.length() > 50) {
-                               data = data.substring(0, 47) + "...";
+                       if (data.length() > 200) {
+                               data = data.substring(0, 197) + "...";
                        }
                }
                String rep = String.format(
index cfb205b2145e1216f9139c488882cd0104b60ea0..463a123652a14aebbc0962def6fc3f7a2e9106fd 100644 (file)
@@ -263,6 +263,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,
index 1581965445abe7567f1d0cf65646d5c16f24eef7..bf08f5c1b4dbd19f146bdd5af4b480c3fa4fd531 100644 (file)
@@ -157,6 +157,19 @@ class SerialTest extends TestLauncher {
                                encodeRecodeTest(this, data);
                        }
                });
+               addTest(new TestCase("Import/Export String in object") {
+                       @Override
+                       public void test() throws Exception {
+                               Data data = new DataString("fanfan");
+                               encodeRecodeTest(this, data);
+                               data = new DataString("http://example.com/query.html");
+                               encodeRecodeTest(this, data);
+                               data = new DataString("Test|Ché|http://|\"\\\"Pouch\\");
+                               encodeRecodeTest(this, data);
+                               data = new DataString("Test|Ché\\n|\nhttp://|\"\\\"Pouch\\");
+                               encodeRecodeTest(this, data);
+                       }
+               });
                addTest(new TestCase("Import/Export with nested objects forming a loop") {
                        @Override
                        public void test() throws Exception {
@@ -233,6 +246,20 @@ class SerialTest extends TestLauncher {
                }
        }
 
+       @SuppressWarnings("unused")
+       class DataString extends Data {
+               private String data;
+
+               @SuppressWarnings("synthetic-access")
+               private DataString() {
+               }
+
+               @SuppressWarnings("synthetic-access")
+               public DataString(String data) {
+                       this.data = data;
+               }
+       }
+
        @SuppressWarnings("unused")
        class DataLoop extends Data {
                public DataLoop next;