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("URL Import/Export") {
35 public void test() throws Exception
{
36 URL data
= new URL("https://fanfan.be/");
37 String encoded
= new Exporter().append(data
).toString(false);
38 Object redata
= new Importer().read(encoded
).getValue();
39 String reencoded
= new Exporter().append(redata
)
42 assertEquals(encoded
.replaceAll("@[0-9]*", "@REF"),
43 reencoded
.replaceAll("@[0-9]*", "@REF"));
47 addTest(new TestCase("URL-String Import/Export") {
49 public void test() throws Exception
{
50 String data
= new URL("https://fanfan.be/").toString();
51 String encoded
= new Exporter().append(data
).toString(false);
52 Object redata
= new Importer().read(encoded
).getValue();
53 String reencoded
= new Exporter().append(redata
)
56 assertEquals(encoded
.replaceAll("@[0-9]*", "@REF"),
57 reencoded
.replaceAll("@[0-9]*", "@REF"));
58 assertEquals(data
, redata
);
62 addTest(new TestCase("URL/URL-String arrays Import/Export") {
64 public void test() throws Exception
{
65 final String url
= "https://fanfan.be/";
67 Object
[] data
= new Object
[] { new URL(url
), url
};
68 String encoded
= new Exporter().append(data
).toString(false);
69 Object redata
= new Importer().read(encoded
).getValue();
70 String reencoded
= new Exporter().append(redata
)
73 assertEquals(encoded
.replaceAll("@[0-9]*", "@REF"),
74 reencoded
.replaceAll("@[0-9]*", "@REF"));
75 assertEquals(data
[0], ((Object
[]) redata
)[0]);
76 assertEquals(data
[1], ((Object
[]) redata
)[1]);
80 addTest(new TestCase("Import/Export with nested objects") {
82 public void test() throws Exception
{
83 Data data
= new DataObject(new Data(21));
84 String encoded
= new Exporter().append(data
).toString(false);
85 Object redata
= new Importer().read(encoded
).getValue();
86 String reencoded
= new Exporter().append(redata
)
89 assertEquals(encoded
.replaceAll("@[0-9]*", "@REF"),
90 reencoded
.replaceAll("@[0-9]*", "@REF"));
94 addTest(new TestCase("Import/Export with nested objects forming a loop") {
96 public void test() throws Exception
{
97 DataLoop data
= new DataLoop("looping");
98 data
.next
= new DataLoop("level 2");
99 data
.next
.next
= data
;
101 String encoded
= new Exporter().append(data
).toString(false);
102 Object redata
= new Importer().read(encoded
).getValue();
103 String reencoded
= new Exporter().append(redata
)
106 assertEquals(encoded
.replaceAll("@[0-9]*", "@REF"),
107 reencoded
.replaceAll("@[0-9]*", "@REF"));
111 addTest(new TestCase("Array in Object Import/Export") {
113 public void test() throws Exception
{
114 Object data
= new DataArray();// new String[] { "un", "deux" };
115 String encoded
= new Exporter().append(data
).toString(false);
116 Object redata
= new Importer().read(encoded
).getValue();
117 String reencoded
= new Exporter().append(redata
)
120 assertEquals(encoded
.replaceAll("@[0-9]*", "@REF"),
121 reencoded
.replaceAll("@[0-9]*", "@REF"));
125 addTest(new TestCase("Array Import/Export") {
127 public void test() throws Exception
{
128 Object data
= new String
[] { "un", "deux" };
129 String encoded
= new Exporter().append(data
).toString(false);
130 Object redata
= new Importer().read(encoded
).getValue();
131 String reencoded
= new Exporter().append(redata
)
134 assertEquals(encoded
.replaceAll("@[0-9]*", "@REF"),
135 reencoded
.replaceAll("@[0-9]*", "@REF"));
139 addTest(new TestCase("Enum Import/Export") {
141 public void test() throws Exception
{
142 Object data
= EnumToSend
.FANFAN
;
143 String encoded
= new Exporter().append(data
).toString(false);
144 Object redata
= new Importer().read(encoded
).getValue();
145 String reencoded
= new Exporter().append(redata
)
148 assertEquals(encoded
.replaceAll("@[0-9]*", "@REF"),
149 reencoded
.replaceAll("@[0-9]*", "@REF"));
155 public String
[] data
= new String
[] { "un", "deux" };
158 @SuppressWarnings("unused")
165 public Data(int value
) {
170 @SuppressWarnings("unused")
171 class DataObject
extends Data
{
174 @SuppressWarnings("synthetic-access")
175 private DataObject() {
178 @SuppressWarnings("synthetic-access")
179 public DataObject(Data data
) {
184 @SuppressWarnings("unused")
185 class DataLoop
extends Data
{
186 public DataLoop next
;
187 private String value
;
189 @SuppressWarnings("synthetic-access")
193 @SuppressWarnings("synthetic-access")
194 public DataLoop(String value
) {