Version 3.1.0: ServerBridge
[nikiroo-utils.git] / src / be / nikiroo / utils / test / SerialTest.java
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 public SerialTest(String[] args) {
8 super("Serial test", args);
9
10 addTest(new TestCase("Simple class Import/Export") {
11 @Override
12 public void test() throws Exception {
13 Data data = new Data(42);
14 String encoded = new Exporter().append(data).toString(false);
15 Object redata = new Importer().read(encoded).getValue();
16 String reencoded = new Exporter().append(redata)
17 .toString(false);
18
19 assertEquals(encoded.replaceAll("@[0-9]*", "@REF"),
20 reencoded.replaceAll("@[0-9]*", "@REF"));
21 }
22 });
23
24 addTest(new TestCase("Import/Export with nested objects") {
25 @Override
26 public void test() throws Exception {
27 Data data = new DataObject(new Data(21));
28 String encoded = new Exporter().append(data).toString(false);
29 Object redata = new Importer().read(encoded).getValue();
30 String reencoded = new Exporter().append(redata)
31 .toString(false);
32
33 assertEquals(encoded.replaceAll("@[0-9]*", "@REF"),
34 reencoded.replaceAll("@[0-9]*", "@REF"));
35 }
36 });
37
38 addTest(new TestCase("Import/Export with nested objects forming a loop") {
39 @Override
40 public void test() throws Exception {
41 DataLoop data = new DataLoop("looping");
42 data.next = new DataLoop("level 2");
43 data.next.next = data;
44
45 String encoded = new Exporter().append(data).toString(false);
46 Object redata = new Importer().read(encoded).getValue();
47 String reencoded = new Exporter().append(redata)
48 .toString(false);
49
50 assertEquals(encoded.replaceAll("@[0-9]*", "@REF"),
51 reencoded.replaceAll("@[0-9]*", "@REF"));
52 }
53 });
54
55 addTest(new TestCase("Array in Object Import/Export") {
56 @Override
57 public void test() throws Exception {
58 Object data = new DataArray();// new String[] { "un", "deux" };
59 String encoded = new Exporter().append(data).toString(false);
60 Object redata = new Importer().read(encoded).getValue();
61 String reencoded = new Exporter().append(redata)
62 .toString(false);
63
64 assertEquals(encoded.replaceAll("@[0-9]*", "@REF"),
65 reencoded.replaceAll("@[0-9]*", "@REF"));
66 }
67 });
68
69 addTest(new TestCase("Array Import/Export") {
70 @Override
71 public void test() throws Exception {
72 Object data = new String[] { "un", "deux" };
73 String encoded = new Exporter().append(data).toString(false);
74 Object redata = new Importer().read(encoded).getValue();
75 String reencoded = new Exporter().append(redata)
76 .toString(false);
77
78 assertEquals(encoded.replaceAll("@[0-9]*", "@REF"),
79 reencoded.replaceAll("@[0-9]*", "@REF"));
80 }
81 });
82
83 addTest(new TestCase("Enum Import/Export") {
84 @Override
85 public void test() throws Exception {
86 Object data = EnumToSend.FANFAN;
87 String encoded = new Exporter().append(data).toString(false);
88 Object redata = new Importer().read(encoded).getValue();
89 String reencoded = new Exporter().append(redata)
90 .toString(false);
91
92 assertEquals(encoded.replaceAll("@[0-9]*", "@REF"),
93 reencoded.replaceAll("@[0-9]*", "@REF"));
94 }
95 });
96 }
97
98 class DataArray {
99 public String[] data = new String[] { "un", "deux" };
100 }
101
102 @SuppressWarnings("unused")
103 class Data {
104 private int value;
105
106 private Data() {
107 }
108
109 public Data(int value) {
110 this.value = value;
111 }
112 }
113
114 @SuppressWarnings("unused")
115 class DataObject extends Data {
116 private Data data;
117
118 @SuppressWarnings("synthetic-access")
119 private DataObject() {
120 }
121
122 @SuppressWarnings("synthetic-access")
123 public DataObject(Data data) {
124 this.data = data;
125 }
126 }
127
128 @SuppressWarnings("unused")
129 class DataLoop extends Data {
130 public DataLoop next;
131 private String value;
132
133 @SuppressWarnings("synthetic-access")
134 private DataLoop() {
135 }
136
137 @SuppressWarnings("synthetic-access")
138 public DataLoop(String value) {
139 this.value = value;
140 }
141 }
142
143 enum EnumToSend {
144 FANFAN, TULIPE,
145 }
146 }