X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Ftest%2FSerialTest.java;fp=src%2Fbe%2Fnikiroo%2Futils%2Ftest%2FSerialTest.java;h=f73c39ecfeaae78f7f368df1591a5769f4a22603;hb=db31c35860081535d6e7ddc83ab4af573bb0522e;hp=0000000000000000000000000000000000000000;hpb=767e0e016fc0d8dc0c2f86218c8308a040b84684;p=nikiroo-utils.git diff --git a/src/be/nikiroo/utils/test/SerialTest.java b/src/be/nikiroo/utils/test/SerialTest.java new file mode 100644 index 0000000..f73c39e --- /dev/null +++ b/src/be/nikiroo/utils/test/SerialTest.java @@ -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; + } + } +}