Commit | Line | Data |
---|---|---|
db31c358 NR |
1 | package be.nikiroo.utils.test; |
2 | ||
ce0974c4 NR |
3 | import be.nikiroo.utils.Version; |
4 | import be.nikiroo.utils.serial.ConnectActionServer; | |
db31c358 NR |
5 | import be.nikiroo.utils.serial.Exporter; |
6 | import be.nikiroo.utils.serial.Importer; | |
ce0974c4 | 7 | import be.nikiroo.utils.serial.Server; |
db31c358 NR |
8 | |
9 | class SerialTest extends TestLauncher { | |
ce0974c4 NR |
10 | private void not_used() { |
11 | // TODO: test Server ; but this will at least help dependency checking | |
12 | try { | |
13 | Server server = new Server(null, 0, false) { | |
14 | @Override | |
15 | protected Object onRequest(ConnectActionServer action, | |
16 | Version clientVersion, Object data) throws Exception { | |
17 | return null; | |
18 | } | |
19 | }; | |
20 | } catch (Exception e) { | |
21 | } | |
22 | } | |
23 | ||
db31c358 NR |
24 | private SerialTest() { |
25 | super("Serial test", null); | |
26 | } | |
27 | ||
28 | public SerialTest(String[] args) { | |
29 | super("Serial test", args); | |
30 | ||
31 | addTest(new TestCase("Simple class Import/Export") { | |
32 | @Override | |
33 | public void test() throws Exception { | |
34 | Data data = new Data(42); | |
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") { | |
46 | @Override | |
47 | public void test() throws Exception { | |
48 | Data data = new DataObject(new Data(21)); | |
49 | String encoded = new Exporter().append(data).toString(false); | |
50 | Object redata = new Importer().read(encoded).getValue(); | |
51 | String reencoded = new Exporter().append(redata) | |
52 | .toString(false); | |
53 | ||
54 | assertEquals(encoded.replaceAll("@[0-9]*", "@REF"), | |
55 | reencoded.replaceAll("@[0-9]*", "@REF")); | |
56 | } | |
57 | }); | |
58 | ||
59 | addTest(new TestCase("Import/Export with nested objects forming a loop") { | |
60 | @Override | |
61 | public void test() throws Exception { | |
62 | DataLoop data = new DataLoop("looping"); | |
63 | data.next = new DataLoop("level 2"); | |
64 | data.next.next = data; | |
65 | ||
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 | }); | |
ce0974c4 NR |
75 | |
76 | addTest(new TestCase("Array in Object Import/Export") { | |
77 | @Override | |
78 | public void test() throws Exception { | |
79 | Object data = new DataArray();// 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 | }); | |
89 | ||
90 | addTest(new TestCase("Array Import/Export") { | |
91 | @Override | |
92 | public void test() throws Exception { | |
93 | Object data = new String[] { "un", "deux" }; | |
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 | }); | |
e570f7eb NR |
103 | |
104 | addTest(new TestCase("Enum Import/Export") { | |
105 | @Override | |
106 | public void test() throws Exception { | |
107 | Object data = EnumToSend.FANFAN; | |
108 | String encoded = new Exporter().append(data).toString(false); | |
109 | Object redata = new Importer().read(encoded).getValue(); | |
110 | String reencoded = new Exporter().append(redata) | |
111 | .toString(false); | |
112 | ||
113 | assertEquals(encoded.replaceAll("@[0-9]*", "@REF"), | |
114 | reencoded.replaceAll("@[0-9]*", "@REF")); | |
115 | } | |
116 | }); | |
ce0974c4 NR |
117 | } |
118 | ||
119 | class DataArray { | |
120 | public String[] data = new String[] { "un", "deux" }; | |
db31c358 NR |
121 | } |
122 | ||
123 | @SuppressWarnings("unused") | |
124 | class Data { | |
125 | private int value; | |
126 | ||
127 | private Data() { | |
128 | } | |
129 | ||
130 | public Data(int value) { | |
131 | this.value = value; | |
132 | } | |
133 | } | |
134 | ||
135 | @SuppressWarnings("unused") | |
136 | class DataObject extends Data { | |
137 | private Data data; | |
138 | ||
139 | @SuppressWarnings("synthetic-access") | |
140 | private DataObject() { | |
141 | } | |
142 | ||
143 | @SuppressWarnings("synthetic-access") | |
144 | public DataObject(Data data) { | |
145 | this.data = data; | |
146 | } | |
147 | } | |
148 | ||
149 | @SuppressWarnings("unused") | |
150 | class DataLoop extends Data { | |
151 | public DataLoop next; | |
152 | private String value; | |
153 | ||
154 | @SuppressWarnings("synthetic-access") | |
155 | private DataLoop() { | |
156 | } | |
157 | ||
158 | @SuppressWarnings("synthetic-access") | |
159 | public DataLoop(String value) { | |
160 | this.value = value; | |
161 | } | |
162 | } | |
e570f7eb NR |
163 | |
164 | enum EnumToSend { | |
165 | FANFAN, TULIPE, | |
166 | } | |
db31c358 | 167 | } |