mirror of
https://github.com/corda/corda.git
synced 2025-01-19 11:16:54 +00:00
Do not test java.util.TreeMap's serialization in the Serialize test
In the Android class path, TreeMap is implemented differently and as a consequence its serialization is incompatible with OpenJDK's. So let's test a private static class' serialization instead, to make sure that the wire protocol defined by the Java Language Specification is implemented. This addresses issue #123 reported by Joel Dice. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
This commit is contained in:
parent
39e3850ed8
commit
ddd057c53a
@ -1,9 +1,10 @@
|
|||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.ObjectInputStream;
|
import java.io.ObjectInputStream;
|
||||||
import java.io.ObjectOutputStream;
|
import java.io.ObjectOutputStream;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.TreeMap;
|
import java.util.Properties;
|
||||||
|
|
||||||
public class Serialize implements Serializable {
|
public class Serialize implements Serializable {
|
||||||
public static final long serialVersionUID = 1l;
|
public static final long serialVersionUID = 1l;
|
||||||
@ -72,6 +73,49 @@ public class Serialize implements Serializable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static class MyMap implements Serializable {
|
||||||
|
private transient Properties properties = new Properties();
|
||||||
|
|
||||||
|
public final static long serialVersionUID = 0x0cc1f63e2d256ae6l;
|
||||||
|
|
||||||
|
public int size() {
|
||||||
|
return properties.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void put(String key, String value) {
|
||||||
|
properties.put(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String get(String key) {
|
||||||
|
return properties.getProperty(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void writeObject(ObjectOutputStream out) throws IOException {
|
||||||
|
out.defaultWriteObject();
|
||||||
|
out.writeInt(size());
|
||||||
|
for (Object key : properties.keySet()) {
|
||||||
|
out.writeObject(key);
|
||||||
|
out.writeObject(properties.get(key));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void readObject(ObjectInputStream in) throws IOException {
|
||||||
|
try {
|
||||||
|
in.defaultReadObject();
|
||||||
|
} catch (Exception e) {
|
||||||
|
// OpenJDK's defaultReadObject() can throw a ClassNotFoundException
|
||||||
|
throw new IOException(e);
|
||||||
|
}
|
||||||
|
properties = new Properties();
|
||||||
|
int size = in.readInt();
|
||||||
|
for (int i = 0; i < size; i++) try {
|
||||||
|
properties.put(in.readObject(), in.readObject());
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
throw new IOException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
ObjectOutputStream out2 = new ObjectOutputStream(out);
|
ObjectOutputStream out2 = new ObjectOutputStream(out);
|
||||||
@ -145,7 +189,7 @@ public class Serialize implements Serializable {
|
|||||||
|
|
||||||
out.reset();
|
out.reset();
|
||||||
out2 = new ObjectOutputStream(out);
|
out2 = new ObjectOutputStream(out);
|
||||||
TreeMap map = new TreeMap();
|
MyMap map = new MyMap();
|
||||||
map.put("key", "value");
|
map.put("key", "value");
|
||||||
out2.writeObject(map);
|
out2.writeObject(map);
|
||||||
out2.close();
|
out2.close();
|
||||||
@ -157,34 +201,29 @@ public class Serialize implements Serializable {
|
|||||||
0x00, 0x05,
|
0x00, 0x05,
|
||||||
// object
|
// object
|
||||||
0x73,
|
0x73,
|
||||||
// class desc "java.util.TreeMap"
|
// class desc "Serialize$MyMap"
|
||||||
0x72, 0, 17, 'j', 'a', 'v', 'a', '.', 'u', 't', 'i', 'l', '.',
|
0x72, 0, 15, 'S', 'e', 'r', 'i', 'a', 'l', 'i', 'z', 'e', '$',
|
||||||
'T', 'r', 'e', 'e', 'M', 'a', 'p',
|
'M', 'y', 'M', 'a', 'p',
|
||||||
// serial version UID: 0x0cc1f64e2d266ae6
|
// serial version UID: 0x0cc1f64e2d266ae6
|
||||||
0x0c, 0xc1, 0xf6, 0x3e, 0x2d, 0x25, 0x6a, 0xe6,
|
0x0c, 0xc1, 0xf6, 0x3e, 0x2d, 0x25, 0x6a, 0xe6,
|
||||||
// flags: SC_SERIALIZABLE | SC_WRITE_METHOD
|
// flags: SC_SERIALIZABLE | SC_WRITE_METHOD
|
||||||
0x03,
|
0x03,
|
||||||
// 1 field: comparator
|
// no (non-transient) fields
|
||||||
0, 1, 'L', 0, 10, 'c', 'o', 'm', 'p', 'a', 'r', 'a', 't', 'o', 'r',
|
0, 0,
|
||||||
0x74, 0, 22, 'L', 'j', 'a', 'v', 'a', '/', 'u', 't', 'i', 'l', '/',
|
|
||||||
'C', 'o', 'm', 'p', 'a', 'r', 'a', 't', 'o', 'r', ';',
|
|
||||||
// class annotation
|
// class annotation
|
||||||
0x78,
|
0x78,
|
||||||
// super class desc
|
// super class desc
|
||||||
0x70,
|
0x70,
|
||||||
// classdata[]: NULL
|
// custom TreeMap data written by TreeMap#writeObject
|
||||||
0x70,
|
|
||||||
// custom TreeMap data writte by TreeMap#writeObject
|
|
||||||
0x77, 4, 0x00 , 0x00, 0x00, 0x01, // (int)1 (== map.size())
|
0x77, 4, 0x00 , 0x00, 0x00, 0x01, // (int)1 (== map.size())
|
||||||
0x74, 0, 3, 'k', 'e', 'y', // "key"
|
0x74, 0, 3, 'k', 'e', 'y', // "key"
|
||||||
0x74, 0, 5, 'v', 'a', 'l', 'u', 'e', // "value"
|
0x74, 0, 5, 'v', 'a', 'l', 'u', 'e', // "value"
|
||||||
// end block data
|
// end block data
|
||||||
0x78
|
0x78
|
||||||
});
|
});
|
||||||
map.put("Hello", "ween");
|
|
||||||
in = new ByteArrayInputStream(array);
|
in = new ByteArrayInputStream(array);
|
||||||
in2 = new ObjectInputStream(in);
|
in2 = new ObjectInputStream(in);
|
||||||
map = (TreeMap)in2.readObject();
|
map = (MyMap)in2.readObject();
|
||||||
in2.close();
|
in2.close();
|
||||||
expectEqual(1, map.size());
|
expectEqual(1, map.size());
|
||||||
expectEqual("value", (String)map.get("key"));
|
expectEqual("value", (String)map.get("key"));
|
||||||
|
Loading…
Reference in New Issue
Block a user