8fb17220a2d549171193104b4bfd81e17a55a682
1 package be
.nikiroo
.utils
.test
;
5 import be
.nikiroo
.utils
.serial
.Exporter
;
6 import be
.nikiroo
.utils
.serial
.Importer
;
8 class SerialTest
extends TestLauncher
{
10 * Required for Import/Export of objects.
16 public SerialTest(String
[] args
) {
17 super("Serial test", args
);
19 addTest(new TestCase("Simple class Import/Export") {
21 public void test() throws Exception
{
22 Data data
= new Data(42);
23 String encoded
= new Exporter().append(data
).toString(false);
24 Object redata
= new Importer().read(encoded
).getValue();
25 String reencoded
= new Exporter().append(redata
)
28 assertEquals(encoded
.replaceAll("@[0-9]*", "@REF"),
29 reencoded
.replaceAll("@[0-9]*", "@REF"));
33 addTest(new TestCase() {
34 @SuppressWarnings("unused")
35 private TestCase me
= setName("Anonymous inner class");
38 public void test() throws Exception
{
39 Data data
= new Data() {
43 String encoded
= new Exporter().append(data
).toString(false);
44 Object redata
= new Importer().read(encoded
).getValue();
45 String reencoded
= new Exporter().append(redata
)
48 assertEquals(encoded
.replaceAll("@[0-9]*", "@REF"),
49 reencoded
.replaceAll("@[0-9]*", "@REF"));
53 addTest(new TestCase() {
54 @SuppressWarnings("unused")
55 private TestCase me
= setName("Array of anonymous inner classes");
58 public void test() throws Exception
{
59 Data
[] data
= new Data
[] { new Data() {
63 String encoded
= new Exporter().append(data
).toString(false);
64 Object redata
= new Importer().read(encoded
).getValue();
65 String reencoded
= new Exporter().append(redata
)
68 // Comparing the 2 strings won't be useful, because the @REFs
69 // will be ZIP-encoded; so we parse and re-encode the object
70 encoded
= new Exporter().append(data
[0]).toString(false);
72 reencoded
= new Exporter().append(((Data
[]) redata
)[0])
74 } catch (Exception e
) {
75 fail("Cannot cast the returned data into its original object",
79 assertEquals(encoded
.replaceAll("@[0-9]*", "@REF"),
80 reencoded
.replaceAll("@[0-9]*", "@REF"));
84 addTest(new TestCase("URL Import/Export") {
86 public void test() throws Exception
{
87 URL data
= new URL("https://fanfan.be/");
88 String encoded
= new Exporter().append(data
).toString(false);
89 Object redata
= new Importer().read(encoded
).getValue();
90 String reencoded
= new Exporter().append(redata
)
93 assertEquals(encoded
.replaceAll("@[0-9]*", "@REF"),
94 reencoded
.replaceAll("@[0-9]*", "@REF"));
98 addTest(new TestCase("URL-String Import/Export") {
100 public void test() throws Exception
{
101 String data
= new URL("https://fanfan.be/").toString();
102 String encoded
= new Exporter().append(data
).toString(false);
103 Object redata
= new Importer().read(encoded
).getValue();
104 String reencoded
= new Exporter().append(redata
)
107 assertEquals(encoded
.replaceAll("@[0-9]*", "@REF"),
108 reencoded
.replaceAll("@[0-9]*", "@REF"));
109 assertEquals(data
, redata
);
113 addTest(new TestCase("URL/URL-String arrays Import/Export") {
115 public void test() throws Exception
{
116 final String url
= "https://fanfan.be/";
118 Object
[] data
= new Object
[] { new URL(url
), url
};
119 String encoded
= new Exporter().append(data
).toString(false);
120 Object redata
= new Importer().read(encoded
).getValue();
121 String reencoded
= new Exporter().append(redata
)
124 assertEquals(encoded
.replaceAll("@[0-9]*", "@REF"),
125 reencoded
.replaceAll("@[0-9]*", "@REF"));
126 assertEquals(data
[0], ((Object
[]) redata
)[0]);
127 assertEquals(data
[1], ((Object
[]) redata
)[1]);
131 addTest(new TestCase("Import/Export with nested objects") {
133 public void test() throws Exception
{
134 Data data
= new DataObject(new Data(21));
135 String encoded
= new Exporter().append(data
).toString(false);
136 Object redata
= new Importer().read(encoded
).getValue();
137 String reencoded
= new Exporter().append(redata
)
140 assertEquals(encoded
.replaceAll("@[0-9]*", "@REF"),
141 reencoded
.replaceAll("@[0-9]*", "@REF"));
145 addTest(new TestCase("Import/Export with nested objects forming a loop") {
147 public void test() throws Exception
{
148 DataLoop data
= new DataLoop("looping");
149 data
.next
= new DataLoop("level 2");
150 data
.next
.next
= data
;
152 String encoded
= new Exporter().append(data
).toString(false);
153 Object redata
= new Importer().read(encoded
).getValue();
154 String reencoded
= new Exporter().append(redata
)
157 assertEquals(encoded
.replaceAll("@[0-9]*", "@REF"),
158 reencoded
.replaceAll("@[0-9]*", "@REF"));
162 addTest(new TestCase("Array in Object Import/Export") {
164 public void test() throws Exception
{
165 Object data
= new DataArray();// new String[] { "un", "deux" };
166 String encoded
= new Exporter().append(data
).toString(false);
167 Object redata
= new Importer().read(encoded
).getValue();
168 String reencoded
= new Exporter().append(redata
)
171 assertEquals(encoded
.replaceAll("@[0-9]*", "@REF"),
172 reencoded
.replaceAll("@[0-9]*", "@REF"));
176 addTest(new TestCase("Array Import/Export") {
178 public void test() throws Exception
{
179 Object data
= new String
[] { "un", "deux" };
180 String encoded
= new Exporter().append(data
).toString(false);
181 Object redata
= new Importer().read(encoded
).getValue();
182 String reencoded
= new Exporter().append(redata
)
185 assertEquals(encoded
.replaceAll("@[0-9]*", "@REF"),
186 reencoded
.replaceAll("@[0-9]*", "@REF"));
190 addTest(new TestCase("Enum Import/Export") {
192 public void test() throws Exception
{
193 Object data
= EnumToSend
.FANFAN
;
194 String encoded
= new Exporter().append(data
).toString(false);
195 Object redata
= new Importer().read(encoded
).getValue();
196 String reencoded
= new Exporter().append(redata
)
199 assertEquals(encoded
.replaceAll("@[0-9]*", "@REF"),
200 reencoded
.replaceAll("@[0-9]*", "@REF"));
206 public String
[] data
= new String
[] { "un", "deux" };
209 @SuppressWarnings("unused")
216 public Data(int value
) {
221 @SuppressWarnings("unused")
222 class DataObject
extends Data
{
225 @SuppressWarnings("synthetic-access")
226 private DataObject() {
229 @SuppressWarnings("synthetic-access")
230 public DataObject(Data data
) {
235 @SuppressWarnings("unused")
236 class DataLoop
extends Data
{
237 public DataLoop next
;
238 private String value
;
240 @SuppressWarnings("synthetic-access")
244 @SuppressWarnings("synthetic-access")
245 public DataLoop(String value
) {