| 1 | package be.nikiroo.utils.test; |
| 2 | |
| 3 | import be.nikiroo.utils.serial.Exporter; |
| 4 | import be.nikiroo.utils.serial.Importer; |
| 5 | |
| 6 | class SerialTest extends TestLauncher { |
| 7 | private SerialTest() { |
| 8 | super("Serial test", null); |
| 9 | } |
| 10 | |
| 11 | public SerialTest(String[] args) { |
| 12 | super("Serial test", args); |
| 13 | |
| 14 | addTest(new TestCase("Simple class Import/Export") { |
| 15 | @Override |
| 16 | public void test() throws Exception { |
| 17 | Data data = new Data(42); |
| 18 | String encoded = new Exporter().append(data).toString(false); |
| 19 | Object redata = new Importer().read(encoded).getValue(); |
| 20 | String reencoded = new Exporter().append(redata) |
| 21 | .toString(false); |
| 22 | |
| 23 | assertEquals(encoded.replaceAll("@[0-9]*", "@REF"), |
| 24 | reencoded.replaceAll("@[0-9]*", "@REF")); |
| 25 | } |
| 26 | }); |
| 27 | |
| 28 | addTest(new TestCase("Import/Export with nested objects") { |
| 29 | @Override |
| 30 | public void test() throws Exception { |
| 31 | Data data = new DataObject(new Data(21)); |
| 32 | String encoded = new Exporter().append(data).toString(false); |
| 33 | Object redata = new Importer().read(encoded).getValue(); |
| 34 | String reencoded = new Exporter().append(redata) |
| 35 | .toString(false); |
| 36 | |
| 37 | assertEquals(encoded.replaceAll("@[0-9]*", "@REF"), |
| 38 | reencoded.replaceAll("@[0-9]*", "@REF")); |
| 39 | } |
| 40 | }); |
| 41 | |
| 42 | addTest(new TestCase("Import/Export with nested objects forming a loop") { |
| 43 | @Override |
| 44 | public void test() throws Exception { |
| 45 | DataLoop data = new DataLoop("looping"); |
| 46 | data.next = new DataLoop("level 2"); |
| 47 | data.next.next = data; |
| 48 | |
| 49 | String encoded = new Exporter().append(data).toString(false); |
| 50 | Object redata = new Importer().read(encoded).getValue(); |
| 51 | String reencoded = new Exporter().append(redata) |
| 52 | .toString(false); |
| 53 | |
| 54 | assertEquals(encoded.replaceAll("@[0-9]*", "@REF"), |
| 55 | reencoded.replaceAll("@[0-9]*", "@REF")); |
| 56 | } |
| 57 | }); |
| 58 | } |
| 59 | |
| 60 | @SuppressWarnings("unused") |
| 61 | class Data { |
| 62 | private int value; |
| 63 | |
| 64 | private Data() { |
| 65 | } |
| 66 | |
| 67 | public Data(int value) { |
| 68 | this.value = value; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | @SuppressWarnings("unused") |
| 73 | class DataObject extends Data { |
| 74 | private Data data; |
| 75 | |
| 76 | @SuppressWarnings("synthetic-access") |
| 77 | private DataObject() { |
| 78 | } |
| 79 | |
| 80 | @SuppressWarnings("synthetic-access") |
| 81 | public DataObject(Data data) { |
| 82 | this.data = data; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | @SuppressWarnings("unused") |
| 87 | class DataLoop extends Data { |
| 88 | public DataLoop next; |
| 89 | private String value; |
| 90 | |
| 91 | @SuppressWarnings("synthetic-access") |
| 92 | private DataLoop() { |
| 93 | } |
| 94 | |
| 95 | @SuppressWarnings("synthetic-access") |
| 96 | public DataLoop(String value) { |
| 97 | this.value = value; |
| 98 | } |
| 99 | } |
| 100 | } |