1 package be
.nikiroo
.utils
.test_code
;
3 import java
.io
.ByteArrayInputStream
;
4 import java
.io
.ByteArrayOutputStream
;
5 import java
.io
.IOException
;
6 import java
.io
.InputStream
;
7 import java
.io
.NotSerializableException
;
9 import java
.util
.Arrays
;
11 import be
.nikiroo
.utils
.serial
.Exporter
;
12 import be
.nikiroo
.utils
.serial
.Importer
;
13 import be
.nikiroo
.utils
.test
.TestCase
;
14 import be
.nikiroo
.utils
.test
.TestLauncher
;
16 class SerialTest
extends TestLauncher
{
18 * Required for Import/Export of objects.
24 private void encodeRecodeTest(TestCase test
, Object data
) throws Exception
{
25 byte[] encoded
= toBytes(data
, true);
26 Object redata
= fromBytes(encoded
);
27 byte[] reencoded
= toBytes(redata
, true);
29 test
.assertEquals("Different data after encode/decode/encode", true,
30 Arrays
.equals(encoded
, reencoded
));
33 // try to remove pointer addresses
34 private byte[] toBytes(Object data
, boolean clearRefs
)
35 throws NotSerializableException
, IOException
{
36 ByteArrayOutputStream out
= new ByteArrayOutputStream();
37 new Exporter(out
).append(data
);
41 String tmp
= new String(out
.toByteArray(), "UTF-8");
42 tmp
= tmp
.replaceAll("@[0-9]*", "@REF");
43 return tmp
.getBytes("UTF-8");
46 return out
.toByteArray();
49 private Object
fromBytes(byte[] data
) throws NoSuchFieldException
,
50 NoSuchMethodException
, ClassNotFoundException
,
51 NullPointerException
, IOException
{
53 InputStream in
= new ByteArrayInputStream(data
);
55 return new Importer().read(in
).getValue();
61 public SerialTest(String
[] args
) {
62 super("Serial test", args
);
64 addTest(new TestCase("Simple class Import/Export") {
66 public void test() throws Exception
{
67 Data data
= new Data(42);
68 encodeRecodeTest(this, data
);
72 addTest(new TestCase() {
73 @SuppressWarnings("unused")
74 private TestCase me
= setName("Anonymous inner class");
77 public void test() throws Exception
{
78 Data data
= new Data() {
79 @SuppressWarnings("unused")
82 encodeRecodeTest(this, data
);
85 addTest(new TestCase() {
86 @SuppressWarnings("unused")
87 private TestCase me
= setName("Array of anonymous inner classes");
90 public void test() throws Exception
{
91 Data
[] data
= new Data
[] { new Data() {
92 @SuppressWarnings("unused")
96 byte[] encoded
= toBytes(data
, false);
97 Object redata
= fromBytes(encoded
);
99 // Comparing the 2 arrays won't be useful, because the @REFs
100 // will be ZIP-encoded; so we parse and re-encode each object
102 byte[] encoded1
= toBytes(data
[0], true);
103 byte[] reencoded1
= toBytes(((Object
[]) redata
)[0], true);
105 assertEquals("Different data after encode/decode/encode", true,
106 Arrays
.equals(encoded1
, reencoded1
));
109 addTest(new TestCase("URL Import/Export") {
111 public void test() throws Exception
{
112 URL data
= new URL("https://fanfan.be/");
113 encodeRecodeTest(this, data
);
116 addTest(new TestCase("URL-String Import/Export") {
118 public void test() throws Exception
{
119 String data
= new URL("https://fanfan.be/").toString();
120 encodeRecodeTest(this, data
);
123 addTest(new TestCase("URL/URL-String arrays Import/Export") {
125 public void test() throws Exception
{
126 final String url
= "https://fanfan.be/";
127 Object
[] data
= new Object
[] { new URL(url
), url
};
129 byte[] encoded
= toBytes(data
, false);
130 Object redata
= fromBytes(encoded
);
132 // Comparing the 2 arrays won't be useful, because the @REFs
133 // will be ZIP-encoded; so we parse and re-encode each object
135 byte[] encoded1
= toBytes(data
[0], true);
136 byte[] reencoded1
= toBytes(((Object
[]) redata
)[0], true);
137 byte[] encoded2
= toBytes(data
[0], true);
138 byte[] reencoded2
= toBytes(((Object
[]) redata
)[1], true);
140 assertEquals("Different data 1 after encode/decode/encode",
141 true, Arrays
.equals(encoded1
, reencoded1
));
142 assertEquals("Different data 2 after encode/decode/encode",
143 true, Arrays
.equals(encoded2
, reencoded2
));
146 addTest(new TestCase("Import/Export with nested objects") {
148 public void test() throws Exception
{
149 Data data
= new DataObject(new Data(21));
150 encodeRecodeTest(this, data
);
153 addTest(new TestCase("Import/Export with nested objects forming a loop") {
155 public void test() throws Exception
{
156 DataLoop data
= new DataLoop("looping");
157 data
.next
= new DataLoop("level 2");
158 data
.next
.next
= data
;
159 encodeRecodeTest(this, data
);
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 encodeRecodeTest(this, data
);
169 addTest(new TestCase("Array Import/Export") {
171 public void test() throws Exception
{
172 Object data
= new String
[] { "un", "deux" };
173 encodeRecodeTest(this, data
);
176 addTest(new TestCase("Enum Import/Export") {
178 public void test() throws Exception
{
179 Object data
= EnumToSend
.FANFAN
;
180 encodeRecodeTest(this, data
);
186 public String
[] data
= new String
[] { "un", "deux" };
195 public Data(int value
) {
200 public boolean equals(Object obj
) {
201 if (obj
instanceof Data
) {
202 Data other
= (Data
) obj
;
203 return other
.value
== this.value
;
210 public int hashCode() {
211 return new Integer(value
).hashCode();
215 @SuppressWarnings("unused")
216 class DataObject
extends Data
{
219 @SuppressWarnings("synthetic-access")
220 private DataObject() {
223 @SuppressWarnings("synthetic-access")
224 public DataObject(Data data
) {
229 @SuppressWarnings("unused")
230 class DataLoop
extends Data
{
231 public DataLoop next
;
232 private String value
;
234 @SuppressWarnings("synthetic-access")
238 @SuppressWarnings("synthetic-access")
239 public DataLoop(String value
) {