New Server class to send/rec objects via network
[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 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
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 });
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 });
103 }
104
105 class DataArray {
106 public String[] data = new String[] { "un", "deux" };
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 }
149 }