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() {
42 String encoded
= new Exporter().append(data
).toString(false);
43 Object redata
= new Importer().read(encoded
).getValue();
44 String reencoded
= new Exporter().append(redata
)
47 assertEquals(encoded
.replaceAll("@[0-9]*", "@REF"),
48 reencoded
.replaceAll("@[0-9]*", "@REF"));
52 addTest(new TestCase() {
53 @SuppressWarnings("unused")
54 private TestCase me
= setName("Array of anonymous inner classes");
57 public void test() throws Exception
{
58 Data
[] data
= new Data
[] { new Data() {
61 String encoded
= new Exporter().append(data
).toString(false);
62 Object redata
= new Importer().read(encoded
).getValue();
63 String reencoded
= new Exporter().append(redata
)
66 // Comparing the 2 strings won't be useful, because the @REFs
67 // will be ZIP-encoded; so we parse and re-encode the object
68 encoded
= new Exporter().append(data
[0]).toString(false);
70 reencoded
= new Exporter().append(((Data
[]) redata
)[0])
72 } catch (Exception e
) {
73 fail("Cannot cast the returned data into its original object",
77 assertEquals(encoded
.replaceAll("@[0-9]*", "@REF"),
78 reencoded
.replaceAll("@[0-9]*", "@REF"));
82 addTest(new TestCase("URL Import/Export") {
84 public void test() throws Exception
{
85 URL data
= new URL("https://fanfan.be/");
86 String encoded
= new Exporter().append(data
).toString(false);
87 Object redata
= new Importer().read(encoded
).getValue();
88 String reencoded
= new Exporter().append(redata
)
91 assertEquals(encoded
.replaceAll("@[0-9]*", "@REF"),
92 reencoded
.replaceAll("@[0-9]*", "@REF"));
96 addTest(new TestCase("URL-String Import/Export") {
98 public void test() throws Exception
{
99 String data
= new URL("https://fanfan.be/").toString();
100 String encoded
= new Exporter().append(data
).toString(false);
101 Object redata
= new Importer().read(encoded
).getValue();
102 String reencoded
= new Exporter().append(redata
)
105 assertEquals(encoded
.replaceAll("@[0-9]*", "@REF"),
106 reencoded
.replaceAll("@[0-9]*", "@REF"));
107 assertEquals(data
, redata
);
111 addTest(new TestCase("URL/URL-String arrays Import/Export") {
113 public void test() throws Exception
{
114 final String url
= "https://fanfan.be/";
116 Object
[] data
= new Object
[] { new URL(url
), url
};
117 String encoded
= new Exporter().append(data
).toString(false);
118 Object redata
= new Importer().read(encoded
).getValue();
119 String reencoded
= new Exporter().append(redata
)
122 assertEquals(encoded
.replaceAll("@[0-9]*", "@REF"),
123 reencoded
.replaceAll("@[0-9]*", "@REF"));
124 assertEquals(data
[0], ((Object
[]) redata
)[0]);
125 assertEquals(data
[1], ((Object
[]) redata
)[1]);
129 addTest(new TestCase("Import/Export with nested objects") {
131 public void test() throws Exception
{
132 Data data
= new DataObject(new Data(21));
133 String encoded
= new Exporter().append(data
).toString(false);
134 Object redata
= new Importer().read(encoded
).getValue();
135 String reencoded
= new Exporter().append(redata
)
138 assertEquals(encoded
.replaceAll("@[0-9]*", "@REF"),
139 reencoded
.replaceAll("@[0-9]*", "@REF"));
143 addTest(new TestCase("Import/Export with nested objects forming a loop") {
145 public void test() throws Exception
{
146 DataLoop data
= new DataLoop("looping");
147 data
.next
= new DataLoop("level 2");
148 data
.next
.next
= data
;
150 String encoded
= new Exporter().append(data
).toString(false);
151 Object redata
= new Importer().read(encoded
).getValue();
152 String reencoded
= new Exporter().append(redata
)
155 assertEquals(encoded
.replaceAll("@[0-9]*", "@REF"),
156 reencoded
.replaceAll("@[0-9]*", "@REF"));
160 addTest(new TestCase("Array in Object Import/Export") {
162 public void test() throws Exception
{
163 Object data
= new DataArray();// new String[] { "un", "deux" };
164 String encoded
= new Exporter().append(data
).toString(false);
165 Object redata
= new Importer().read(encoded
).getValue();
166 String reencoded
= new Exporter().append(redata
)
169 assertEquals(encoded
.replaceAll("@[0-9]*", "@REF"),
170 reencoded
.replaceAll("@[0-9]*", "@REF"));
174 addTest(new TestCase("Array Import/Export") {
176 public void test() throws Exception
{
177 Object data
= new String
[] { "un", "deux" };
178 String encoded
= new Exporter().append(data
).toString(false);
179 Object redata
= new Importer().read(encoded
).getValue();
180 String reencoded
= new Exporter().append(redata
)
183 assertEquals(encoded
.replaceAll("@[0-9]*", "@REF"),
184 reencoded
.replaceAll("@[0-9]*", "@REF"));
188 addTest(new TestCase("Enum Import/Export") {
190 public void test() throws Exception
{
191 Object data
= EnumToSend
.FANFAN
;
192 String encoded
= new Exporter().append(data
).toString(false);
193 Object redata
= new Importer().read(encoded
).getValue();
194 String reencoded
= new Exporter().append(redata
)
197 assertEquals(encoded
.replaceAll("@[0-9]*", "@REF"),
198 reencoded
.replaceAll("@[0-9]*", "@REF"));
204 public String
[] data
= new String
[] { "un", "deux" };
207 @SuppressWarnings("unused")
214 public Data(int value
) {
219 @SuppressWarnings("unused")
220 class DataObject
extends Data
{
223 @SuppressWarnings("synthetic-access")
224 private DataObject() {
227 @SuppressWarnings("synthetic-access")
228 public DataObject(Data data
) {
233 @SuppressWarnings("unused")
234 class DataLoop
extends Data
{
235 public DataLoop next
;
236 private String value
;
238 @SuppressWarnings("synthetic-access")
242 @SuppressWarnings("synthetic-access")
243 public DataLoop(String value
) {