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(toBytes(data
, false));
27 byte[] reencoded
= toBytes(redata
, true);
29 // We suppose text mode
30 if (encoded
.length
< 256 && reencoded
.length
< 256) {
31 test
.assertEquals("Different data after encode/decode/encode",
32 new String(encoded
, "UTF-8"),
33 new String(reencoded
, "UTF-8"));
35 test
.assertEquals("Different data after encode/decode/encode",
36 true, Arrays
.equals(encoded
, reencoded
));
40 // try to remove pointer addresses
41 private byte[] toBytes(Object data
, boolean clearRefs
)
42 throws NotSerializableException
, IOException
{
43 ByteArrayOutputStream out
= new ByteArrayOutputStream();
44 new Exporter(out
).append(data
);
48 String tmp
= new String(out
.toByteArray(), "UTF-8");
49 tmp
= tmp
.replaceAll("@[0-9]*", "@REF");
50 return tmp
.getBytes("UTF-8");
53 return out
.toByteArray();
56 private Object
fromBytes(byte[] data
) throws NoSuchFieldException
,
57 NoSuchMethodException
, ClassNotFoundException
,
58 NullPointerException
, IOException
{
60 InputStream in
= new ByteArrayInputStream(data
);
62 return new Importer().read(in
).getValue();
68 public SerialTest(String
[] args
) {
69 super("Serial test", args
);
71 addTest(new TestCase("Simple class Import/Export") {
73 public void test() throws Exception
{
74 Data data
= new Data(42);
75 encodeRecodeTest(this, data
);
79 addTest(new TestCase() {
80 @SuppressWarnings("unused")
81 private TestCase me
= setName("Anonymous inner class");
84 public void test() throws Exception
{
85 Data data
= new Data() {
86 @SuppressWarnings("unused")
89 encodeRecodeTest(this, data
);
92 addTest(new TestCase() {
93 @SuppressWarnings("unused")
94 private TestCase me
= setName("Array of anonymous inner classes");
97 public void test() throws Exception
{
98 Data
[] data
= new Data
[] { new Data() {
99 @SuppressWarnings("unused")
103 byte[] encoded
= toBytes(data
, false);
104 Object redata
= fromBytes(encoded
);
106 // Comparing the 2 arrays won't be useful, because the @REFs
107 // will be ZIP-encoded; so we parse and re-encode each object
109 byte[] encoded1
= toBytes(data
[0], true);
110 byte[] reencoded1
= toBytes(((Object
[]) redata
)[0], true);
112 assertEquals("Different data after encode/decode/encode", true,
113 Arrays
.equals(encoded1
, reencoded1
));
116 addTest(new TestCase("URL Import/Export") {
118 public void test() throws Exception
{
119 URL data
= new URL("https://fanfan.be/");
120 encodeRecodeTest(this, data
);
123 addTest(new TestCase("URL-String Import/Export") {
125 public void test() throws Exception
{
126 String data
= new URL("https://fanfan.be/").toString();
127 encodeRecodeTest(this, data
);
130 addTest(new TestCase("URL/URL-String arrays Import/Export") {
132 public void test() throws Exception
{
133 final String url
= "https://fanfan.be/";
134 Object
[] data
= new Object
[] { new URL(url
), url
};
136 byte[] encoded
= toBytes(data
, false);
137 Object redata
= fromBytes(encoded
);
139 // Comparing the 2 arrays won't be useful, because the @REFs
140 // will be ZIP-encoded; so we parse and re-encode each object
142 byte[] encoded1
= toBytes(data
[0], true);
143 byte[] reencoded1
= toBytes(((Object
[]) redata
)[0], true);
144 byte[] encoded2
= toBytes(data
[1], true);
145 byte[] reencoded2
= toBytes(((Object
[]) redata
)[1], true);
147 assertEquals("Different data 1 after encode/decode/encode",
148 true, Arrays
.equals(encoded1
, reencoded1
));
149 assertEquals("Different data 2 after encode/decode/encode",
150 true, Arrays
.equals(encoded2
, reencoded2
));
153 addTest(new TestCase("Import/Export with nested objects") {
155 public void test() throws Exception
{
156 Data data
= new DataObject(new Data(21));
157 encodeRecodeTest(this, data
);
160 addTest(new TestCase("Import/Export String in object") {
162 public void test() throws Exception
{
163 Data data
= new DataString("fanfan");
164 encodeRecodeTest(this, data
);
165 data
= new DataString("http://example.com/query.html");
166 encodeRecodeTest(this, data
);
167 data
= new DataString("Test|Ché|http://|\"\\\"Pouch\\");
168 encodeRecodeTest(this, data
);
169 data
= new DataString("Test|Ché\\n|\nhttp://|\"\\\"Pouch\\");
170 encodeRecodeTest(this, data
);
173 addTest(new TestCase("Import/Export with nested objects forming a loop") {
175 public void test() throws Exception
{
176 DataLoop data
= new DataLoop("looping");
177 data
.next
= new DataLoop("level 2");
178 data
.next
.next
= data
;
179 encodeRecodeTest(this, data
);
182 addTest(new TestCase("Array in Object Import/Export") {
184 public void test() throws Exception
{
185 Object data
= new DataArray();// new String[] { "un", "deux" };
186 encodeRecodeTest(this, data
);
189 addTest(new TestCase("Array Import/Export") {
191 public void test() throws Exception
{
192 Object data
= new String
[] { "un", "deux" };
193 encodeRecodeTest(this, data
);
196 addTest(new TestCase("Enum Import/Export") {
198 public void test() throws Exception
{
199 Object data
= EnumToSend
.FANFAN
;
200 encodeRecodeTest(this, data
);
206 public String
[] data
= new String
[] { "un", "deux" };
215 public Data(int value
) {
220 public boolean equals(Object obj
) {
221 if (obj
instanceof Data
) {
222 Data other
= (Data
) obj
;
223 return other
.value
== this.value
;
230 public int hashCode() {
231 return new Integer(value
).hashCode();
235 @SuppressWarnings("unused")
236 class DataObject
extends Data
{
239 @SuppressWarnings("synthetic-access")
240 private DataObject() {
243 @SuppressWarnings("synthetic-access")
244 public DataObject(Data data
) {
249 @SuppressWarnings("unused")
250 class DataString
extends Data
{
253 @SuppressWarnings("synthetic-access")
254 private DataString() {
257 @SuppressWarnings("synthetic-access")
258 public DataString(String data
) {
263 @SuppressWarnings("unused")
264 class DataLoop
extends Data
{
265 public DataLoop next
;
266 private String value
;
268 @SuppressWarnings("synthetic-access")
272 @SuppressWarnings("synthetic-access")
273 public DataLoop(String value
) {