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