Update to version 1.5.0 (breaking Bundle/Meta)
[nikiroo-utils.git] / src / be / nikiroo / utils / test / SerialTest.java
diff --git a/src/be/nikiroo/utils/test/SerialTest.java b/src/be/nikiroo/utils/test/SerialTest.java
new file mode 100644 (file)
index 0000000..f73c39e
--- /dev/null
@@ -0,0 +1,100 @@
+package be.nikiroo.utils.test;
+
+import be.nikiroo.utils.serial.Exporter;
+import be.nikiroo.utils.serial.Importer;
+
+class SerialTest extends TestLauncher {
+       private SerialTest() {
+               super("Serial test", null);
+       }
+
+       public SerialTest(String[] args) {
+               super("Serial test", args);
+
+               addTest(new TestCase("Simple class Import/Export") {
+                       @Override
+                       public void test() throws Exception {
+                               Data data = new Data(42);
+                               String encoded = new Exporter().append(data).toString(false);
+                               Object redata = new Importer().read(encoded).getValue();
+                               String reencoded = new Exporter().append(redata)
+                                               .toString(false);
+
+                               assertEquals(encoded.replaceAll("@[0-9]*", "@REF"),
+                                               reencoded.replaceAll("@[0-9]*", "@REF"));
+                       }
+               });
+
+               addTest(new TestCase("Import/Export with nested objects") {
+                       @Override
+                       public void test() throws Exception {
+                               Data data = new DataObject(new Data(21));
+                               String encoded = new Exporter().append(data).toString(false);
+                               Object redata = new Importer().read(encoded).getValue();
+                               String reencoded = new Exporter().append(redata)
+                                               .toString(false);
+
+                               assertEquals(encoded.replaceAll("@[0-9]*", "@REF"),
+                                               reencoded.replaceAll("@[0-9]*", "@REF"));
+                       }
+               });
+
+               addTest(new TestCase("Import/Export with nested objects forming a loop") {
+                       @Override
+                       public void test() throws Exception {
+                               DataLoop data = new DataLoop("looping");
+                               data.next = new DataLoop("level 2");
+                               data.next.next = data;
+
+                               String encoded = new Exporter().append(data).toString(false);
+                               Object redata = new Importer().read(encoded).getValue();
+                               String reencoded = new Exporter().append(redata)
+                                               .toString(false);
+
+                               assertEquals(encoded.replaceAll("@[0-9]*", "@REF"),
+                                               reencoded.replaceAll("@[0-9]*", "@REF"));
+                       }
+               });
+       }
+
+       @SuppressWarnings("unused")
+       class Data {
+               private int value;
+
+               private Data() {
+               }
+
+               public Data(int value) {
+                       this.value = value;
+               }
+       }
+
+       @SuppressWarnings("unused")
+       class DataObject extends Data {
+               private Data data;
+
+               @SuppressWarnings("synthetic-access")
+               private DataObject() {
+               }
+
+               @SuppressWarnings("synthetic-access")
+               public DataObject(Data data) {
+                       this.data = data;
+               }
+       }
+
+       @SuppressWarnings("unused")
+       class DataLoop extends Data {
+               public DataLoop next;
+               private String value;
+
+               @SuppressWarnings("synthetic-access")
+               private DataLoop() {
+               }
+
+               @SuppressWarnings("synthetic-access")
+               public DataLoop(String value) {
+                       this.value = value;
+               }
+       }
+}