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