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