Serial: support for anonymous inner classes
[nikiroo-utils.git] / src / be / nikiroo / utils / test / SerialTest.java
1 package be.nikiroo.utils.test;
2
3 import java.net.URL;
4
5 import be.nikiroo.utils.serial.Exporter;
6 import be.nikiroo.utils.serial.Importer;
7
8 class SerialTest extends TestLauncher {
9 /**
10 * Required for Import/Export of objects.
11 */
12 public SerialTest() {
13 this(null);
14 }
15
16 public SerialTest(String[] args) {
17 super("Serial test", args);
18
19 addTest(new TestCase("Simple class Import/Export") {
20 @Override
21 public void test() throws Exception {
22 Data data = new Data(42);
23 String encoded = new Exporter().append(data).toString(false);
24 Object redata = new Importer().read(encoded).getValue();
25 String reencoded = new Exporter().append(redata)
26 .toString(false);
27
28 assertEquals(encoded.replaceAll("@[0-9]*", "@REF"),
29 reencoded.replaceAll("@[0-9]*", "@REF"));
30 }
31 });
32
33 addTest(new TestCase() {
34 private TestCase me = setName("Anonymous inner class");
35
36 @Override
37 public void test() throws Exception {
38 Data data = new Data() {
39 };
40
41 String encoded = new Exporter().append(data).toString(false);
42 Object redata = new Importer().read(encoded).getValue();
43 String reencoded = new Exporter().append(redata)
44 .toString(false);
45
46 assertEquals(encoded.replaceAll("@[0-9]*", "@REF"),
47 reencoded.replaceAll("@[0-9]*", "@REF"));
48 }
49 });
50
51 addTest(new TestCase("URL Import/Export") {
52 @Override
53 public void test() throws Exception {
54 URL data = new URL("https://fanfan.be/");
55 String encoded = new Exporter().append(data).toString(false);
56 Object redata = new Importer().read(encoded).getValue();
57 String reencoded = new Exporter().append(redata)
58 .toString(false);
59
60 assertEquals(encoded.replaceAll("@[0-9]*", "@REF"),
61 reencoded.replaceAll("@[0-9]*", "@REF"));
62 }
63 });
64
65 addTest(new TestCase("URL-String Import/Export") {
66 @Override
67 public void test() throws Exception {
68 String data = new URL("https://fanfan.be/").toString();
69 String encoded = new Exporter().append(data).toString(false);
70 Object redata = new Importer().read(encoded).getValue();
71 String reencoded = new Exporter().append(redata)
72 .toString(false);
73
74 assertEquals(encoded.replaceAll("@[0-9]*", "@REF"),
75 reencoded.replaceAll("@[0-9]*", "@REF"));
76 assertEquals(data, redata);
77 }
78 });
79
80 addTest(new TestCase("URL/URL-String arrays Import/Export") {
81 @Override
82 public void test() throws Exception {
83 final String url = "https://fanfan.be/";
84
85 Object[] data = new Object[] { new URL(url), url };
86 String encoded = new Exporter().append(data).toString(false);
87 Object redata = new Importer().read(encoded).getValue();
88 String reencoded = new Exporter().append(redata)
89 .toString(false);
90
91 assertEquals(encoded.replaceAll("@[0-9]*", "@REF"),
92 reencoded.replaceAll("@[0-9]*", "@REF"));
93 assertEquals(data[0], ((Object[]) redata)[0]);
94 assertEquals(data[1], ((Object[]) redata)[1]);
95 }
96 });
97
98 addTest(new TestCase("Import/Export with nested objects") {
99 @Override
100 public void test() throws Exception {
101 Data data = new DataObject(new Data(21));
102 String encoded = new Exporter().append(data).toString(false);
103 Object redata = new Importer().read(encoded).getValue();
104 String reencoded = new Exporter().append(redata)
105 .toString(false);
106
107 assertEquals(encoded.replaceAll("@[0-9]*", "@REF"),
108 reencoded.replaceAll("@[0-9]*", "@REF"));
109 }
110 });
111
112 addTest(new TestCase("Import/Export with nested objects forming a loop") {
113 @Override
114 public void test() throws Exception {
115 DataLoop data = new DataLoop("looping");
116 data.next = new DataLoop("level 2");
117 data.next.next = data;
118
119 String encoded = new Exporter().append(data).toString(false);
120 Object redata = new Importer().read(encoded).getValue();
121 String reencoded = new Exporter().append(redata)
122 .toString(false);
123
124 assertEquals(encoded.replaceAll("@[0-9]*", "@REF"),
125 reencoded.replaceAll("@[0-9]*", "@REF"));
126 }
127 });
128
129 addTest(new TestCase("Array in Object Import/Export") {
130 @Override
131 public void test() throws Exception {
132 Object data = new DataArray();// new String[] { "un", "deux" };
133 String encoded = new Exporter().append(data).toString(false);
134 Object redata = new Importer().read(encoded).getValue();
135 String reencoded = new Exporter().append(redata)
136 .toString(false);
137
138 assertEquals(encoded.replaceAll("@[0-9]*", "@REF"),
139 reencoded.replaceAll("@[0-9]*", "@REF"));
140 }
141 });
142
143 addTest(new TestCase("Array Import/Export") {
144 @Override
145 public void test() throws Exception {
146 Object data = new String[] { "un", "deux" };
147 String encoded = new Exporter().append(data).toString(false);
148 Object redata = new Importer().read(encoded).getValue();
149 String reencoded = new Exporter().append(redata)
150 .toString(false);
151
152 assertEquals(encoded.replaceAll("@[0-9]*", "@REF"),
153 reencoded.replaceAll("@[0-9]*", "@REF"));
154 }
155 });
156
157 addTest(new TestCase("Enum Import/Export") {
158 @Override
159 public void test() throws Exception {
160 Object data = EnumToSend.FANFAN;
161 String encoded = new Exporter().append(data).toString(false);
162 Object redata = new Importer().read(encoded).getValue();
163 String reencoded = new Exporter().append(redata)
164 .toString(false);
165
166 assertEquals(encoded.replaceAll("@[0-9]*", "@REF"),
167 reencoded.replaceAll("@[0-9]*", "@REF"));
168 }
169 });
170 }
171
172 class DataArray {
173 public String[] data = new String[] { "un", "deux" };
174 }
175
176 @SuppressWarnings("unused")
177 class Data {
178 private int value;
179
180 private Data() {
181 }
182
183 public Data(int value) {
184 this.value = value;
185 }
186 }
187
188 @SuppressWarnings("unused")
189 class DataObject extends Data {
190 private Data data;
191
192 @SuppressWarnings("synthetic-access")
193 private DataObject() {
194 }
195
196 @SuppressWarnings("synthetic-access")
197 public DataObject(Data data) {
198 this.data = data;
199 }
200 }
201
202 @SuppressWarnings("unused")
203 class DataLoop extends Data {
204 public DataLoop next;
205 private String value;
206
207 @SuppressWarnings("synthetic-access")
208 private DataLoop() {
209 }
210
211 @SuppressWarnings("synthetic-access")
212 public DataLoop(String value) {
213 this.value = value;
214 }
215 }
216
217 enum EnumToSend {
218 FANFAN, TULIPE,
219 }
220 }