SerialTest (array of anonymous classes) + TestCas
[nikiroo-utils.git] / src / be / nikiroo / utils / test / SerialTest.java
CommitLineData
db31c358
NR
1package be.nikiroo.utils.test;
2
f4053377
NR
3import java.net.URL;
4
db31c358
NR
5import be.nikiroo.utils.serial.Exporter;
6import be.nikiroo.utils.serial.Importer;
7
8class SerialTest extends TestLauncher {
452f38c8
NR
9 /**
10 * Required for Import/Export of objects.
11 */
12 public SerialTest() {
13 this(null);
14 }
15
db31c358
NR
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();
72648e75
NR
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() {
edeff2ab 34 @SuppressWarnings("unused")
72648e75
NR
35 private TestCase me = setName("Anonymous inner class");
36
37 @Override
38 public void test() throws Exception {
39 Data data = new Data() {
40 };
41
42 String encoded = new Exporter().append(data).toString(false);
43 Object redata = new Importer().read(encoded).getValue();
db31c358
NR
44 String reencoded = new Exporter().append(redata)
45 .toString(false);
46
47 assertEquals(encoded.replaceAll("@[0-9]*", "@REF"),
48 reencoded.replaceAll("@[0-9]*", "@REF"));
49 }
50 });
51
edeff2ab
NR
52 addTest(new TestCase() {
53 @SuppressWarnings("unused")
54 private TestCase me = setName("Array of anonymous inner classes");
55
56 @Override
57 public void test() throws Exception {
58 Data[] data = new Data[] { new Data() {
59 } };
60
61 String encoded = new Exporter().append(data).toString(false);
62 Object redata = new Importer().read(encoded).getValue();
63 String reencoded = new Exporter().append(redata)
64 .toString(false);
65
66 // Comparing the 2 strings won't be useful, because the @REFs
67 // will be ZIP-encoded; so we parse and re-encode the object
68 encoded = new Exporter().append(data[0]).toString(false);
69 try {
70 reencoded = new Exporter().append(((Data[]) redata)[0])
71 .toString(false);
72 } catch (Exception e) {
73 fail("Cannot cast the returned data into its original object",
74 e);
75 }
76
77 assertEquals(encoded.replaceAll("@[0-9]*", "@REF"),
78 reencoded.replaceAll("@[0-9]*", "@REF"));
79 }
80 });
81
f4053377
NR
82 addTest(new TestCase("URL Import/Export") {
83 @Override
84 public void test() throws Exception {
85 URL data = new URL("https://fanfan.be/");
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 }
94 });
95
96 addTest(new TestCase("URL-String Import/Export") {
97 @Override
98 public void test() throws Exception {
99 String data = new URL("https://fanfan.be/").toString();
100 String encoded = new Exporter().append(data).toString(false);
101 Object redata = new Importer().read(encoded).getValue();
102 String reencoded = new Exporter().append(redata)
103 .toString(false);
104
105 assertEquals(encoded.replaceAll("@[0-9]*", "@REF"),
106 reencoded.replaceAll("@[0-9]*", "@REF"));
107 assertEquals(data, redata);
108 }
109 });
110
111 addTest(new TestCase("URL/URL-String arrays Import/Export") {
112 @Override
113 public void test() throws Exception {
114 final String url = "https://fanfan.be/";
115
116 Object[] data = new Object[] { new URL(url), url };
117 String encoded = new Exporter().append(data).toString(false);
118 Object redata = new Importer().read(encoded).getValue();
119 String reencoded = new Exporter().append(redata)
120 .toString(false);
121
122 assertEquals(encoded.replaceAll("@[0-9]*", "@REF"),
123 reencoded.replaceAll("@[0-9]*", "@REF"));
124 assertEquals(data[0], ((Object[]) redata)[0]);
125 assertEquals(data[1], ((Object[]) redata)[1]);
126 }
127 });
128
db31c358
NR
129 addTest(new TestCase("Import/Export with nested objects") {
130 @Override
131 public void test() throws Exception {
132 Data data = new DataObject(new Data(21));
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("Import/Export with nested objects forming a loop") {
144 @Override
145 public void test() throws Exception {
146 DataLoop data = new DataLoop("looping");
147 data.next = new DataLoop("level 2");
148 data.next.next = data;
149
150 String encoded = new Exporter().append(data).toString(false);
151 Object redata = new Importer().read(encoded).getValue();
152 String reencoded = new Exporter().append(redata)
153 .toString(false);
154
155 assertEquals(encoded.replaceAll("@[0-9]*", "@REF"),
156 reencoded.replaceAll("@[0-9]*", "@REF"));
157 }
158 });
ce0974c4
NR
159
160 addTest(new TestCase("Array in Object Import/Export") {
161 @Override
162 public void test() throws Exception {
163 Object data = new DataArray();// new String[] { "un", "deux" };
164 String encoded = new Exporter().append(data).toString(false);
165 Object redata = new Importer().read(encoded).getValue();
166 String reencoded = new Exporter().append(redata)
167 .toString(false);
168
169 assertEquals(encoded.replaceAll("@[0-9]*", "@REF"),
170 reencoded.replaceAll("@[0-9]*", "@REF"));
171 }
172 });
173
174 addTest(new TestCase("Array Import/Export") {
175 @Override
176 public void test() throws Exception {
177 Object data = new String[] { "un", "deux" };
178 String encoded = new Exporter().append(data).toString(false);
179 Object redata = new Importer().read(encoded).getValue();
180 String reencoded = new Exporter().append(redata)
181 .toString(false);
182
183 assertEquals(encoded.replaceAll("@[0-9]*", "@REF"),
184 reencoded.replaceAll("@[0-9]*", "@REF"));
185 }
186 });
e570f7eb
NR
187
188 addTest(new TestCase("Enum Import/Export") {
189 @Override
190 public void test() throws Exception {
191 Object data = EnumToSend.FANFAN;
192 String encoded = new Exporter().append(data).toString(false);
193 Object redata = new Importer().read(encoded).getValue();
194 String reencoded = new Exporter().append(redata)
195 .toString(false);
196
197 assertEquals(encoded.replaceAll("@[0-9]*", "@REF"),
198 reencoded.replaceAll("@[0-9]*", "@REF"));
199 }
200 });
ce0974c4
NR
201 }
202
203 class DataArray {
204 public String[] data = new String[] { "un", "deux" };
db31c358
NR
205 }
206
207 @SuppressWarnings("unused")
208 class Data {
209 private int value;
210
211 private Data() {
212 }
213
214 public Data(int value) {
215 this.value = value;
216 }
217 }
218
219 @SuppressWarnings("unused")
220 class DataObject extends Data {
221 private Data data;
222
223 @SuppressWarnings("synthetic-access")
224 private DataObject() {
225 }
226
227 @SuppressWarnings("synthetic-access")
228 public DataObject(Data data) {
229 this.data = data;
230 }
231 }
232
233 @SuppressWarnings("unused")
234 class DataLoop extends Data {
235 public DataLoop next;
236 private String value;
237
238 @SuppressWarnings("synthetic-access")
239 private DataLoop() {
240 }
241
242 @SuppressWarnings("synthetic-access")
243 public DataLoop(String value) {
244 this.value = value;
245 }
246 }
e570f7eb
NR
247
248 enum EnumToSend {
249 FANFAN, TULIPE,
250 }
db31c358 251}