serial: fix b64 or not error
authorNiki Roo <niki@nikiroo.be>
Sun, 17 Mar 2019 13:59:57 +0000 (14:59 +0100)
committerNiki Roo <niki@nikiroo.be>
Sun, 17 Mar 2019 13:59:57 +0000 (14:59 +0100)
changelog.md
src/be/nikiroo/utils/serial/Exporter.java
src/be/nikiroo/utils/test/SerialTest.java
src/be/nikiroo/utils/test/Test.java
src/be/nikiroo/utils/test/TestCase.java

index 6bd05cc7706ad6e1d99c86f5e883f9ed62859d6e..2ad5d0354da23e8f102fd1ff224f5b384653d771 100644 (file)
@@ -1,5 +1,9 @@
 # nikiroo-utils
 
+## Version WIP
+
+- Serial: fix b64/not b64 error
+
 ## Version 4.5.1
 
 - Progress: fix deadlock in rare cases
index d5f3a5fdc72e47219f3a70a0fb15c0480bf72045..dc96d973226fbcaaf09331ae8f0b6ac3d68ada98 100644 (file)
@@ -74,7 +74,7 @@ public class Exporter {
                        b64 = false;
                }
 
-               if (b64 != null || b64) {
+               if (b64 == null || b64) {
                        try {
                                String zipped = StringUtils.base64(builder.toString(), zip);
                                if (b64 != null || zipped.length() < builder.length() - 4) {
index 8fb17220a2d549171193104b4bfd81e17a55a682..be8b3008ebec5c7528e7ebfa5158239e600c285f 100644 (file)
@@ -20,10 +20,11 @@ class SerialTest extends TestLauncher {
                        @Override
                        public void test() throws Exception {
                                Data data = new Data(42);
-                               String encoded = new Exporter().append(data).toString(false);
+                               String encoded = new Exporter().append(data).toString(false,
+                                               false);
                                Object redata = new Importer().read(encoded).getValue();
-                               String reencoded = new Exporter().append(redata)
-                                               .toString(false);
+                               String reencoded = new Exporter().append(redata).toString(
+                                               false, false);
 
                                assertEquals(encoded.replaceAll("@[0-9]*", "@REF"),
                                                reencoded.replaceAll("@[0-9]*", "@REF"));
@@ -37,13 +38,15 @@ class SerialTest extends TestLauncher {
                        @Override
                        public void test() throws Exception {
                                Data data = new Data() {
+                                       @SuppressWarnings("unused")
                                        int value = 42;
                                };
 
-                               String encoded = new Exporter().append(data).toString(false);
+                               String encoded = new Exporter().append(data).toString(false,
+                                               false);
                                Object redata = new Importer().read(encoded).getValue();
-                               String reencoded = new Exporter().append(redata)
-                                               .toString(false);
+                               String reencoded = new Exporter().append(redata).toString(
+                                               false, false);
 
                                assertEquals(encoded.replaceAll("@[0-9]*", "@REF"),
                                                reencoded.replaceAll("@[0-9]*", "@REF"));
@@ -57,20 +60,22 @@ class SerialTest extends TestLauncher {
                        @Override
                        public void test() throws Exception {
                                Data[] data = new Data[] { new Data() {
+                                       @SuppressWarnings("unused")
                                        int value = 42;
                                } };
 
-                               String encoded = new Exporter().append(data).toString(false);
+                               String encoded = new Exporter().append(data).toString(false,
+                                               false);
                                Object redata = new Importer().read(encoded).getValue();
-                               String reencoded = new Exporter().append(redata)
-                                               .toString(false);
+                               String reencoded = new Exporter().append(redata).toString(
+                                               false, false);
 
                                // Comparing the 2 strings won't be useful, because the @REFs
                                // will be ZIP-encoded; so we parse and re-encode the object
-                               encoded = new Exporter().append(data[0]).toString(false);
+                               encoded = new Exporter().append(data[0]).toString(false, false);
                                try {
                                        reencoded = new Exporter().append(((Data[]) redata)[0])
-                                                       .toString(false);
+                                                       .toString(false, false);
                                } catch (Exception e) {
                                        fail("Cannot cast the returned data into its original object",
                                                        e);
@@ -85,10 +90,11 @@ class SerialTest extends TestLauncher {
                        @Override
                        public void test() throws Exception {
                                URL data = new URL("https://fanfan.be/");
-                               String encoded = new Exporter().append(data).toString(false);
+                               String encoded = new Exporter().append(data).toString(false,
+                                               false);
                                Object redata = new Importer().read(encoded).getValue();
-                               String reencoded = new Exporter().append(redata)
-                                               .toString(false);
+                               String reencoded = new Exporter().append(redata).toString(
+                                               false, false);
 
                                assertEquals(encoded.replaceAll("@[0-9]*", "@REF"),
                                                reencoded.replaceAll("@[0-9]*", "@REF"));
@@ -99,10 +105,11 @@ class SerialTest extends TestLauncher {
                        @Override
                        public void test() throws Exception {
                                String data = new URL("https://fanfan.be/").toString();
-                               String encoded = new Exporter().append(data).toString(false);
+                               String encoded = new Exporter().append(data).toString(false,
+                                               false);
                                Object redata = new Importer().read(encoded).getValue();
-                               String reencoded = new Exporter().append(redata)
-                                               .toString(false);
+                               String reencoded = new Exporter().append(redata).toString(
+                                               false, false);
 
                                assertEquals(encoded.replaceAll("@[0-9]*", "@REF"),
                                                reencoded.replaceAll("@[0-9]*", "@REF"));
@@ -116,10 +123,11 @@ class SerialTest extends TestLauncher {
                                final String url = "https://fanfan.be/";
 
                                Object[] data = new Object[] { new URL(url), url };
-                               String encoded = new Exporter().append(data).toString(false);
+                               String encoded = new Exporter().append(data).toString(false,
+                                               false);
                                Object redata = new Importer().read(encoded).getValue();
-                               String reencoded = new Exporter().append(redata)
-                                               .toString(false);
+                               String reencoded = new Exporter().append(redata).toString(
+                                               false, false);
 
                                assertEquals(encoded.replaceAll("@[0-9]*", "@REF"),
                                                reencoded.replaceAll("@[0-9]*", "@REF"));
@@ -132,10 +140,11 @@ class SerialTest extends TestLauncher {
                        @Override
                        public void test() throws Exception {
                                Data data = new DataObject(new Data(21));
-                               String encoded = new Exporter().append(data).toString(false);
+                               String encoded = new Exporter().append(data).toString(false,
+                                               false);
                                Object redata = new Importer().read(encoded).getValue();
-                               String reencoded = new Exporter().append(redata)
-                                               .toString(false);
+                               String reencoded = new Exporter().append(redata).toString(
+                                               false, false);
 
                                assertEquals(encoded.replaceAll("@[0-9]*", "@REF"),
                                                reencoded.replaceAll("@[0-9]*", "@REF"));
@@ -149,10 +158,11 @@ class SerialTest extends TestLauncher {
                                data.next = new DataLoop("level 2");
                                data.next.next = data;
 
-                               String encoded = new Exporter().append(data).toString(false);
+                               String encoded = new Exporter().append(data).toString(false,
+                                               false);
                                Object redata = new Importer().read(encoded).getValue();
-                               String reencoded = new Exporter().append(redata)
-                                               .toString(false);
+                               String reencoded = new Exporter().append(redata).toString(
+                                               false, false);
 
                                assertEquals(encoded.replaceAll("@[0-9]*", "@REF"),
                                                reencoded.replaceAll("@[0-9]*", "@REF"));
@@ -163,10 +173,11 @@ class SerialTest extends TestLauncher {
                        @Override
                        public void test() throws Exception {
                                Object data = new DataArray();// new String[] { "un", "deux" };
-                               String encoded = new Exporter().append(data).toString(false);
+                               String encoded = new Exporter().append(data).toString(false,
+                                               false);
                                Object redata = new Importer().read(encoded).getValue();
-                               String reencoded = new Exporter().append(redata)
-                                               .toString(false);
+                               String reencoded = new Exporter().append(redata).toString(
+                                               false, false);
 
                                assertEquals(encoded.replaceAll("@[0-9]*", "@REF"),
                                                reencoded.replaceAll("@[0-9]*", "@REF"));
@@ -177,10 +188,11 @@ class SerialTest extends TestLauncher {
                        @Override
                        public void test() throws Exception {
                                Object data = new String[] { "un", "deux" };
-                               String encoded = new Exporter().append(data).toString(false);
+                               String encoded = new Exporter().append(data).toString(false,
+                                               false);
                                Object redata = new Importer().read(encoded).getValue();
-                               String reencoded = new Exporter().append(redata)
-                                               .toString(false);
+                               String reencoded = new Exporter().append(redata).toString(
+                                               false, false);
 
                                assertEquals(encoded.replaceAll("@[0-9]*", "@REF"),
                                                reencoded.replaceAll("@[0-9]*", "@REF"));
@@ -191,22 +203,80 @@ class SerialTest extends TestLauncher {
                        @Override
                        public void test() throws Exception {
                                Object data = EnumToSend.FANFAN;
-                               String encoded = new Exporter().append(data).toString(false);
+                               String encoded = new Exporter().append(data).toString(false,
+                                               false);
                                Object redata = new Importer().read(encoded).getValue();
-                               String reencoded = new Exporter().append(redata)
-                                               .toString(false);
+                               String reencoded = new Exporter().append(redata).toString(
+                                               false, false);
 
                                assertEquals(encoded.replaceAll("@[0-9]*", "@REF"),
                                                reencoded.replaceAll("@[0-9]*", "@REF"));
                        }
                });
+
+               addTest(new TestCase("B64 and ZIP String test") {
+                       @Override
+                       public void test() throws Exception {
+                               Object data = "Fanfan la tulipe";
+                               String encoded = new Exporter().append(data).toString(true,
+                                               false);
+                               String redata = (String) new Importer().read(encoded)
+                                               .getValue();
+
+                               assertEquals("Items not identical after B64", data, redata);
+
+                               encoded = new Exporter().append(data).toString(true, true);
+                               redata = (String) new Importer().read(encoded).getValue();
+
+                               assertEquals("Items not identical after ZIP", data, redata);
+                       }
+               });
+
+               addTest(new TestCase("B64 and ZIP Data test") {
+                       @Override
+                       public void test() throws Exception {
+                               Object data = new Data(55);
+                               String encoded = new Exporter().append(data).toString(true,
+                                               false);
+                               Data redata = (Data) new Importer().read(encoded).getValue();
+
+                               assertEquals("Items not identical after B64", data, redata);
+
+                               encoded = new Exporter().append(data).toString(true, true);
+                               redata = (Data) new Importer().read(encoded).getValue();
+
+                               assertEquals("Items not identical after ZIP", data, redata);
+                       }
+               });
+
+               addTest(new TestCase("B64 and ZIP 70000 chars test") {
+                       @Override
+                       public void test() throws Exception {
+                               StringBuilder builder = new StringBuilder();
+                               for (int i = 0; i < 7000; i++) {
+                                       builder.append("0123456789");
+                               }
+
+                               Object data = builder.toString();
+                               String encoded = new Exporter().append(data).toString(true,
+                                               false);
+                               String redata = (String) new Importer().read(encoded)
+                                               .getValue();
+
+                               assertEquals("Items not identical after B64", data, redata);
+
+                               encoded = new Exporter().append(data).toString(true, true);
+                               redata = (String) new Importer().read(encoded).getValue();
+
+                               assertEquals("Items not identical after ZIP", data, redata);
+                       }
+               });
        }
 
        class DataArray {
                public String[] data = new String[] { "un", "deux" };
        }
 
-       @SuppressWarnings("unused")
        class Data {
                private int value;
 
@@ -216,6 +286,21 @@ class SerialTest extends TestLauncher {
                public Data(int value) {
                        this.value = value;
                }
+
+               @Override
+               public boolean equals(Object obj) {
+                       if (obj instanceof Data) {
+                               Data other = (Data) obj;
+                               return other.value == this.value;
+                       }
+
+                       return false;
+               }
+
+               @Override
+               public int hashCode() {
+                       return Integer.hashCode(value);
+               }
        }
 
        @SuppressWarnings("unused")
index 14c7af3cc71f7ec93a7ff1277f02e8beaf189bd5..f49d03b983fdccf8d8ac249dcacb578a0a34a2cf 100644 (file)
@@ -22,15 +22,19 @@ public class Test extends TestLauncher {
        public Test(String[] args) {
                super("Nikiroo-utils", args);
 
+               /*
                addSeries(new ProgressTest(args));
                addSeries(new BundleTest(args));
                addSeries(new IOUtilsTest(args));
                addSeries(new VersionTest(args));
+               */
                addSeries(new SerialTest(args));
+               /*
                addSeries(new SerialServerTest(args));
                addSeries(new StringUtilsTest(args));
                addSeries(new TempFilesTest(args));
-
+               */
+               
                // TODO: test cache and downloader
                Cache cache = null;
                Downloader downloader = null;
index 0210e2fe0963c2b4e63a4b6056bd783f550243e9..fe7b9af5e141d5f750be96b5c692b51484158f4f 100644 (file)
@@ -509,7 +509,7 @@ abstract public class TestCase {
                return String.format("" //
                                + "Assertion failed!%n" //
                                + "Expected value: [%s]%n" //
-                               + "Actual value: [%s]", expected, actual);
+                               + "Actual value:   [%s]", expected, actual);
        }
 
        private static String list(List<?> items) {