SerialTest: OK
[nikiroo-utils.git] / src / be / nikiroo / utils / test_code / SerialTest.java
CommitLineData
fbcc2a2a 1package be.nikiroo.utils.test_code;
db31c358 2
564bbbdb
NR
3import java.io.ByteArrayInputStream;
4import java.io.ByteArrayOutputStream;
5import java.io.IOException;
6import java.io.InputStream;
7import java.io.NotSerializableException;
088c1d7a 8import java.net.URL;
564bbbdb 9import java.util.Arrays;
f4053377 10
db31c358
NR
11import be.nikiroo.utils.serial.Exporter;
12import be.nikiroo.utils.serial.Importer;
fbcc2a2a
NR
13import be.nikiroo.utils.test.TestCase;
14import be.nikiroo.utils.test.TestLauncher;
db31c358
NR
15
16class SerialTest extends TestLauncher {
452f38c8
NR
17 /**
18 * Required for Import/Export of objects.
19 */
20 public SerialTest() {
21 this(null);
22 }
23
6e56dcb6 24 private void encodeRecodeTest(TestCase test, Object data) throws Exception {
d8b7384a 25 byte[] encoded = toBytes(data, true);
cfb8e516 26 Object redata = fromBytes(toBytes(data, false));
d8b7384a 27 byte[] reencoded = toBytes(redata, true);
6e56dcb6 28
cfb8e516
NR
29 // We suppose text mode
30 if (encoded.length < 256 && reencoded.length < 256) {
31 test.assertEquals("Different data after encode/decode/encode",
32 new String(encoded, "UTF-8"),
33 new String(reencoded, "UTF-8"));
34 } else {
35 test.assertEquals("Different data after encode/decode/encode",
36 true, Arrays.equals(encoded, reencoded));
37 }
6e56dcb6
NR
38 }
39
564bbbdb 40 // try to remove pointer addresses
d8b7384a
NR
41 private byte[] toBytes(Object data, boolean clearRefs)
42 throws NotSerializableException, IOException {
564bbbdb
NR
43 ByteArrayOutputStream out = new ByteArrayOutputStream();
44 new Exporter(out).append(data);
45 out.flush();
d8b7384a
NR
46
47 if (clearRefs) {
48 String tmp = new String(out.toByteArray(), "UTF-8");
49 tmp = tmp.replaceAll("@[0-9]*", "@REF");
50 return tmp.getBytes("UTF-8");
4d319565 51 }
d8b7384a 52
4d319565 53 return out.toByteArray();
564bbbdb
NR
54 }
55
56 private Object fromBytes(byte[] data) throws NoSuchFieldException,
57 NoSuchMethodException, ClassNotFoundException,
58 NullPointerException, IOException {
59
60 InputStream in = new ByteArrayInputStream(data);
61 try {
62 return new Importer().read(in).getValue();
63 } finally {
64 in.close();
65 }
66 }
67
db31c358
NR
68 public SerialTest(String[] args) {
69 super("Serial test", args);
70
71 addTest(new TestCase("Simple class Import/Export") {
72 @Override
73 public void test() throws Exception {
74 Data data = new Data(42);
6e56dcb6 75 encodeRecodeTest(this, data);
3f277541
NR
76 }
77 });
088c1d7a
NR
78
79 addTest(new TestCase() {
80 @SuppressWarnings("unused")
81 private TestCase me = setName("Anonymous inner class");
82
83 @Override
84 public void test() throws Exception {
85 Data data = new Data() {
86 @SuppressWarnings("unused")
87 int value = 42;
88 };
6e56dcb6 89 encodeRecodeTest(this, data);
088c1d7a
NR
90 }
91 });
92 addTest(new TestCase() {
93 @SuppressWarnings("unused")
94 private TestCase me = setName("Array of anonymous inner classes");
95
96 @Override
97 public void test() throws Exception {
98 Data[] data = new Data[] { new Data() {
99 @SuppressWarnings("unused")
100 int value = 42;
d8b7384a
NR
101 } };
102
103 byte[] encoded = toBytes(data, false);
6e56dcb6 104 Object redata = fromBytes(encoded);
d8b7384a 105
6e56dcb6
NR
106 // Comparing the 2 arrays won't be useful, because the @REFs
107 // will be ZIP-encoded; so we parse and re-encode each object
d8b7384a
NR
108
109 byte[] encoded1 = toBytes(data[0], true);
110 byte[] reencoded1 = toBytes(((Object[]) redata)[0], true);
6e56dcb6
NR
111
112 assertEquals("Different data after encode/decode/encode", true,
113 Arrays.equals(encoded1, reencoded1));
088c1d7a
NR
114 }
115 });
116 addTest(new TestCase("URL Import/Export") {
117 @Override
118 public void test() throws Exception {
119 URL data = new URL("https://fanfan.be/");
d8b7384a 120 encodeRecodeTest(this, data);
088c1d7a
NR
121 }
122 });
123 addTest(new TestCase("URL-String Import/Export") {
124 @Override
125 public void test() throws Exception {
126 String data = new URL("https://fanfan.be/").toString();
d8b7384a 127 encodeRecodeTest(this, data);
088c1d7a
NR
128 }
129 });
130 addTest(new TestCase("URL/URL-String arrays Import/Export") {
131 @Override
132 public void test() throws Exception {
133 final String url = "https://fanfan.be/";
134 Object[] data = new Object[] { new URL(url), url };
d8b7384a
NR
135
136 byte[] encoded = toBytes(data, false);
137 Object redata = fromBytes(encoded);
138
139 // Comparing the 2 arrays won't be useful, because the @REFs
140 // will be ZIP-encoded; so we parse and re-encode each object
141
142 byte[] encoded1 = toBytes(data[0], true);
143 byte[] reencoded1 = toBytes(((Object[]) redata)[0], true);
cfb8e516 144 byte[] encoded2 = toBytes(data[1], true);
d8b7384a
NR
145 byte[] reencoded2 = toBytes(((Object[]) redata)[1], true);
146
147 assertEquals("Different data 1 after encode/decode/encode",
148 true, Arrays.equals(encoded1, reencoded1));
149 assertEquals("Different data 2 after encode/decode/encode",
150 true, Arrays.equals(encoded2, reencoded2));
088c1d7a
NR
151 }
152 });
153 addTest(new TestCase("Import/Export with nested objects") {
154 @Override
155 public void test() throws Exception {
156 Data data = new DataObject(new Data(21));
d8b7384a 157 encodeRecodeTest(this, data);
088c1d7a
NR
158 }
159 });
160 addTest(new TestCase("Import/Export with nested objects forming a loop") {
161 @Override
162 public void test() throws Exception {
163 DataLoop data = new DataLoop("looping");
164 data.next = new DataLoop("level 2");
165 data.next.next = data;
d8b7384a 166 encodeRecodeTest(this, data);
088c1d7a
NR
167 }
168 });
169 addTest(new TestCase("Array in Object Import/Export") {
170 @Override
171 public void test() throws Exception {
172 Object data = new DataArray();// new String[] { "un", "deux" };
d8b7384a 173 encodeRecodeTest(this, data);
088c1d7a
NR
174 }
175 });
176 addTest(new TestCase("Array Import/Export") {
177 @Override
178 public void test() throws Exception {
179 Object data = new String[] { "un", "deux" };
d8b7384a 180 encodeRecodeTest(this, data);
088c1d7a
NR
181 }
182 });
183 addTest(new TestCase("Enum Import/Export") {
184 @Override
185 public void test() throws Exception {
186 Object data = EnumToSend.FANFAN;
d8b7384a 187 encodeRecodeTest(this, data);
088c1d7a
NR
188 }
189 });
ce0974c4
NR
190 }
191
192 class DataArray {
193 public String[] data = new String[] { "un", "deux" };
db31c358
NR
194 }
195
db31c358
NR
196 class Data {
197 private int value;
198
199 private Data() {
200 }
201
202 public Data(int value) {
203 this.value = value;
204 }
3f277541
NR
205
206 @Override
207 public boolean equals(Object obj) {
208 if (obj instanceof Data) {
209 Data other = (Data) obj;
210 return other.value == this.value;
211 }
212
213 return false;
214 }
215
216 @Override
217 public int hashCode() {
b6d4ace8 218 return new Integer(value).hashCode();
3f277541 219 }
db31c358
NR
220 }
221
222 @SuppressWarnings("unused")
223 class DataObject extends Data {
224 private Data data;
225
226 @SuppressWarnings("synthetic-access")
227 private DataObject() {
228 }
229
230 @SuppressWarnings("synthetic-access")
231 public DataObject(Data data) {
232 this.data = data;
233 }
234 }
235
236 @SuppressWarnings("unused")
237 class DataLoop extends Data {
238 public DataLoop next;
239 private String value;
240
241 @SuppressWarnings("synthetic-access")
242 private DataLoop() {
243 }
244
245 @SuppressWarnings("synthetic-access")
246 public DataLoop(String value) {
247 this.value = value;
248 }
249 }
e570f7eb
NR
250
251 enum EnumToSend {
252 FANFAN, TULIPE,
253 }
db31c358 254}