mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-10-21 15:51:44 +02:00
java: improves test case of the MessagePack class
This commit is contained in:
@@ -57,9 +57,112 @@ public class TestMessagePackStaticMethods extends TestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static {
|
static {
|
||||||
// provided classes needs registration
|
// provided classes need registration
|
||||||
MessagePack.register(ProvidedClass.class);
|
MessagePack.register(ProvidedClass.class);
|
||||||
// annotated classes doesn't need registration
|
// annotated classes don't need registration
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCheckedPackToByteArray() throws Exception {
|
||||||
|
byte[] a = MessagePack.pack("msgpack", TString);
|
||||||
|
byte[] b = MessagePack.pack((Object)1, TInteger);
|
||||||
|
byte[] c = MessagePack.pack((Object)null, TAny);
|
||||||
|
byte[] d = MessagePack.pack(createStringList(), tList(TString));
|
||||||
|
byte[] e = MessagePack.pack(createProvidedClass(), tClass(ProvidedClass.class));
|
||||||
|
byte[] f = MessagePack.pack(createUserDefinedClass(), tClass(UserDefinedClass.class));
|
||||||
|
|
||||||
|
{
|
||||||
|
String aobj = MessagePack.unpack(a, String.class);
|
||||||
|
Integer bobj = MessagePack.unpack(b, Integer.class);
|
||||||
|
Object cobj = MessagePack.unpack(c, Object.class);
|
||||||
|
ProvidedClass eobj = MessagePack.unpack(e, ProvidedClass.class);
|
||||||
|
UserDefinedClass fobj = MessagePack.unpack(f, UserDefinedClass.class);
|
||||||
|
|
||||||
|
assertEquals(aobj, "msgpack");
|
||||||
|
assertEquals(bobj, (Integer)1);
|
||||||
|
assertEquals(cobj, null);
|
||||||
|
assertEquals(eobj, createProvidedClass());
|
||||||
|
assertEquals(fobj, createUserDefinedClass());
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
Object aobj = MessagePack.unpack(a, TString);
|
||||||
|
Object bobj = MessagePack.unpack(b, TInteger);
|
||||||
|
Object cobj_any = MessagePack.unpack(c, TAny);
|
||||||
|
Object cobj_obj = MessagePack.unpack(c, tOptional(TAny));
|
||||||
|
Object dobj = MessagePack.unpack(d, tList(TString));
|
||||||
|
Object eobj = MessagePack.unpack(e, tClass(ProvidedClass.class));
|
||||||
|
Object fobj = MessagePack.unpack(f, tClass(UserDefinedClass.class));
|
||||||
|
|
||||||
|
assertEquals(aobj, "msgpack");
|
||||||
|
assertEquals(bobj, 1);
|
||||||
|
assertEquals(cobj_any, NilType.create());
|
||||||
|
assertEquals(cobj_obj, null);
|
||||||
|
assertEquals(dobj, createStringList());
|
||||||
|
assertEquals(eobj, createProvidedClass());
|
||||||
|
assertEquals(fobj, createUserDefinedClass());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCheckedPackToStream() throws Exception {
|
||||||
|
ByteArrayOutputStream aout = new ByteArrayOutputStream();
|
||||||
|
MessagePack.pack(aout, "msgpack");
|
||||||
|
ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
||||||
|
MessagePack.pack(bout, (Object)1);
|
||||||
|
ByteArrayOutputStream cout = new ByteArrayOutputStream();
|
||||||
|
MessagePack.pack(cout, (Object)null);
|
||||||
|
ByteArrayOutputStream dout = new ByteArrayOutputStream();
|
||||||
|
MessagePack.pack(dout, createStringList());
|
||||||
|
ByteArrayOutputStream eout = new ByteArrayOutputStream();
|
||||||
|
MessagePack.pack(eout, createProvidedClass());
|
||||||
|
ByteArrayOutputStream fout = new ByteArrayOutputStream();
|
||||||
|
MessagePack.pack(fout, createUserDefinedClass());
|
||||||
|
|
||||||
|
{
|
||||||
|
InputStream ain = new ByteArrayInputStream(aout.toByteArray());
|
||||||
|
String aobj = MessagePack.unpack(ain, String.class);
|
||||||
|
InputStream bin = new ByteArrayInputStream(bout.toByteArray());
|
||||||
|
Integer bobj = MessagePack.unpack(bin, Integer.class);
|
||||||
|
InputStream cin = new ByteArrayInputStream(cout.toByteArray());
|
||||||
|
Object cobj = MessagePack.unpack(cin, Object.class);
|
||||||
|
InputStream ein = new ByteArrayInputStream(eout.toByteArray());
|
||||||
|
ProvidedClass eobj = MessagePack.unpack(ein, ProvidedClass.class);
|
||||||
|
InputStream fin = new ByteArrayInputStream(fout.toByteArray());
|
||||||
|
UserDefinedClass fobj = MessagePack.unpack(fin, UserDefinedClass.class);
|
||||||
|
|
||||||
|
assertEquals(aobj, "msgpack");
|
||||||
|
assertEquals(bobj, (Integer)1);
|
||||||
|
assertEquals(cobj, null);
|
||||||
|
assertEquals(eobj, createProvidedClass());
|
||||||
|
assertEquals(fobj, createUserDefinedClass());
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
InputStream ain = new ByteArrayInputStream(aout.toByteArray());
|
||||||
|
Object aobj = MessagePack.unpack(ain, TString);
|
||||||
|
InputStream bin = new ByteArrayInputStream(bout.toByteArray());
|
||||||
|
Object bobj = MessagePack.unpack(bin, TInteger);
|
||||||
|
InputStream cin_any = new ByteArrayInputStream(cout.toByteArray());
|
||||||
|
Object cobj_any = MessagePack.unpack(cin_any, TAny);
|
||||||
|
InputStream cin_obj = new ByteArrayInputStream(cout.toByteArray());
|
||||||
|
Object cobj_obj = MessagePack.unpack(cin_obj, tOptional(TAny));
|
||||||
|
InputStream din = new ByteArrayInputStream(dout.toByteArray());
|
||||||
|
Object dobj = MessagePack.unpack(din, tList(TString));
|
||||||
|
InputStream ein = new ByteArrayInputStream(eout.toByteArray());
|
||||||
|
Object eobj = MessagePack.unpack(ein, tClass(ProvidedClass.class));
|
||||||
|
InputStream fin = new ByteArrayInputStream(fout.toByteArray());
|
||||||
|
Object fobj = MessagePack.unpack(fin, tClass(UserDefinedClass.class));
|
||||||
|
|
||||||
|
assertEquals(aobj, "msgpack");
|
||||||
|
assertEquals(bobj, 1);
|
||||||
|
assertEquals(cobj_any, NilType.create());
|
||||||
|
assertEquals(cobj_obj, null);
|
||||||
|
assertEquals(dobj, createStringList());
|
||||||
|
assertEquals(eobj, createProvidedClass());
|
||||||
|
assertEquals(fobj, createUserDefinedClass());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -126,108 +229,6 @@ public class TestMessagePackStaticMethods extends TestCase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testCheckedPackToByteArray() throws Exception {
|
|
||||||
byte[] a = MessagePack.pack("msgpack", TString);
|
|
||||||
byte[] b = MessagePack.pack((Object)1, TInteger);
|
|
||||||
byte[] c = MessagePack.pack((Object)null, TAny);
|
|
||||||
byte[] d = MessagePack.pack(createStringList(), tList(TString));
|
|
||||||
byte[] e = MessagePack.pack(createProvidedClass(), tClass(ProvidedClass.class));
|
|
||||||
byte[] f = MessagePack.pack(createUserDefinedClass(), tClass(UserDefinedClass.class));
|
|
||||||
|
|
||||||
{
|
|
||||||
Object aobj = MessagePack.unpack(a, TString);
|
|
||||||
Object bobj = MessagePack.unpack(b, TInteger);
|
|
||||||
Object cobj_any = MessagePack.unpack(c, TAny);
|
|
||||||
Object cobj_obj = MessagePack.unpack(c, tOptional(TAny));
|
|
||||||
Object dobj = MessagePack.unpack(d, tList(TString));
|
|
||||||
Object eobj = MessagePack.unpack(e, tClass(ProvidedClass.class));
|
|
||||||
Object fobj = MessagePack.unpack(f, tClass(UserDefinedClass.class));
|
|
||||||
|
|
||||||
assertEquals(aobj, "msgpack");
|
|
||||||
assertEquals(bobj, 1);
|
|
||||||
assertEquals(cobj_any, NilType.create());
|
|
||||||
assertEquals(cobj_obj, null);
|
|
||||||
assertEquals(dobj, createStringList());
|
|
||||||
assertEquals(eobj, createProvidedClass());
|
|
||||||
assertEquals(fobj, createUserDefinedClass());
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
String aobj = MessagePack.unpack(a, String.class);
|
|
||||||
Integer bobj = MessagePack.unpack(b, Integer.class);
|
|
||||||
Object cobj = MessagePack.unpack(c, Object.class);
|
|
||||||
ProvidedClass eobj = MessagePack.unpack(e, ProvidedClass.class);
|
|
||||||
UserDefinedClass fobj = MessagePack.unpack(f, UserDefinedClass.class);
|
|
||||||
|
|
||||||
assertEquals(aobj, "msgpack");
|
|
||||||
assertEquals(bobj, (Integer)1);
|
|
||||||
assertEquals(cobj, null);
|
|
||||||
assertEquals(eobj, createProvidedClass());
|
|
||||||
assertEquals(fobj, createUserDefinedClass());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testCheckedPackToStream() throws Exception {
|
|
||||||
ByteArrayOutputStream aout = new ByteArrayOutputStream();
|
|
||||||
MessagePack.pack(aout, "msgpack");
|
|
||||||
ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
|
||||||
MessagePack.pack(bout, (Object)1);
|
|
||||||
ByteArrayOutputStream cout = new ByteArrayOutputStream();
|
|
||||||
MessagePack.pack(cout, (Object)null);
|
|
||||||
ByteArrayOutputStream dout = new ByteArrayOutputStream();
|
|
||||||
MessagePack.pack(dout, createStringList());
|
|
||||||
ByteArrayOutputStream eout = new ByteArrayOutputStream();
|
|
||||||
MessagePack.pack(eout, createProvidedClass());
|
|
||||||
ByteArrayOutputStream fout = new ByteArrayOutputStream();
|
|
||||||
MessagePack.pack(fout, createUserDefinedClass());
|
|
||||||
|
|
||||||
{
|
|
||||||
InputStream ain = new ByteArrayInputStream(aout.toByteArray());
|
|
||||||
Object aobj = MessagePack.unpack(ain, TString);
|
|
||||||
InputStream bin = new ByteArrayInputStream(bout.toByteArray());
|
|
||||||
Object bobj = MessagePack.unpack(bin, TInteger);
|
|
||||||
InputStream cin_any = new ByteArrayInputStream(cout.toByteArray());
|
|
||||||
Object cobj_any = MessagePack.unpack(cin_any, TAny);
|
|
||||||
InputStream cin_obj = new ByteArrayInputStream(cout.toByteArray());
|
|
||||||
Object cobj_obj = MessagePack.unpack(cin_obj, tOptional(TAny));
|
|
||||||
InputStream din = new ByteArrayInputStream(dout.toByteArray());
|
|
||||||
Object dobj = MessagePack.unpack(din, tList(TString));
|
|
||||||
InputStream ein = new ByteArrayInputStream(eout.toByteArray());
|
|
||||||
Object eobj = MessagePack.unpack(ein, tClass(ProvidedClass.class));
|
|
||||||
InputStream fin = new ByteArrayInputStream(fout.toByteArray());
|
|
||||||
Object fobj = MessagePack.unpack(fin, tClass(UserDefinedClass.class));
|
|
||||||
|
|
||||||
assertEquals(aobj, "msgpack");
|
|
||||||
assertEquals(bobj, 1);
|
|
||||||
assertEquals(cobj_any, NilType.create());
|
|
||||||
assertEquals(cobj_obj, null);
|
|
||||||
assertEquals(dobj, createStringList());
|
|
||||||
assertEquals(eobj, createProvidedClass());
|
|
||||||
assertEquals(fobj, createUserDefinedClass());
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
InputStream ain = new ByteArrayInputStream(aout.toByteArray());
|
|
||||||
String aobj = MessagePack.unpack(ain, String.class);
|
|
||||||
InputStream bin = new ByteArrayInputStream(bout.toByteArray());
|
|
||||||
Integer bobj = MessagePack.unpack(bin, Integer.class);
|
|
||||||
InputStream cin = new ByteArrayInputStream(cout.toByteArray());
|
|
||||||
Object cobj = MessagePack.unpack(cin, Object.class);
|
|
||||||
InputStream ein = new ByteArrayInputStream(eout.toByteArray());
|
|
||||||
ProvidedClass eobj = MessagePack.unpack(ein, ProvidedClass.class);
|
|
||||||
InputStream fin = new ByteArrayInputStream(fout.toByteArray());
|
|
||||||
UserDefinedClass fobj = MessagePack.unpack(fin, UserDefinedClass.class);
|
|
||||||
|
|
||||||
assertEquals(aobj, "msgpack");
|
|
||||||
assertEquals(bobj, (Integer)1);
|
|
||||||
assertEquals(cobj, null);
|
|
||||||
assertEquals(eobj, createProvidedClass());
|
|
||||||
assertEquals(fobj, createUserDefinedClass());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private List<String> createStringList() {
|
private List<String> createStringList() {
|
||||||
List<String> list = new ArrayList<String>();
|
List<String> list = new ArrayList<String>();
|
||||||
|
Reference in New Issue
Block a user