mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-03-21 15:10:01 +01:00
java: improves test case of the MessagePack class
This commit is contained in:
parent
7ac4ad3e38
commit
1b3231e617
@ -2,6 +2,7 @@ package org.msgpack;
|
|||||||
|
|
||||||
import org.msgpack.*;
|
import org.msgpack.*;
|
||||||
import org.msgpack.object.*;
|
import org.msgpack.object.*;
|
||||||
|
import org.msgpack.annotation.*;
|
||||||
import static org.msgpack.Templates.*;
|
import static org.msgpack.Templates.*;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
@ -12,7 +13,7 @@ import org.junit.Test;
|
|||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
public class TestMessagePackStaticMethods extends TestCase {
|
public class TestMessagePackStaticMethods extends TestCase {
|
||||||
public static class CustomClass {
|
public static class ProvidedClass {
|
||||||
public boolean bool;
|
public boolean bool;
|
||||||
public String str;
|
public String str;
|
||||||
public List<Integer> list;
|
public List<Integer> list;
|
||||||
@ -21,20 +22,44 @@ public class TestMessagePackStaticMethods extends TestCase {
|
|||||||
if (obj == this) {
|
if (obj == this) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (!(obj instanceof CustomClass)) {
|
if (!(obj instanceof ProvidedClass)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
CustomClass o = (CustomClass)obj;
|
ProvidedClass o = (ProvidedClass)obj;
|
||||||
return bool == o.bool && str.equals(o.str) && list.equals(o.list);
|
return bool == o.bool && str.equals(o.str) && list.equals(o.list);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "CustomClass<bool:"+bool+" str:"+str+" list:"+list+">";
|
return "ProvidedClass<bool:"+bool+" str:"+str+" list:"+list+">";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@MessagePackMessage
|
||||||
|
public static class UserDefinedClass {
|
||||||
|
public boolean bool;
|
||||||
|
public String str;
|
||||||
|
public List<Integer> list;
|
||||||
|
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (obj == this) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (!(obj instanceof UserDefinedClass)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
UserDefinedClass o = (UserDefinedClass)obj;
|
||||||
|
return bool == o.bool && str.equals(o.str) && list.equals(o.list);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return "UserDefinedClass<bool:"+bool+" str:"+str+" list:"+list+">";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static {
|
static {
|
||||||
MessagePack.register(CustomClass.class);
|
// provided classes needs registration
|
||||||
|
MessagePack.register(ProvidedClass.class);
|
||||||
|
// annotated classes doesn't need registration
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -43,7 +68,8 @@ public class TestMessagePackStaticMethods extends TestCase {
|
|||||||
byte[] b = MessagePack.pack((Object)1);
|
byte[] b = MessagePack.pack((Object)1);
|
||||||
byte[] c = MessagePack.pack((Object)null);
|
byte[] c = MessagePack.pack((Object)null);
|
||||||
byte[] d = MessagePack.pack(createStringList());
|
byte[] d = MessagePack.pack(createStringList());
|
||||||
byte[] e = MessagePack.pack(createCustomClass());
|
byte[] e = MessagePack.pack(createProvidedClass());
|
||||||
|
byte[] f = MessagePack.pack(createUserDefinedClass_dynamic());
|
||||||
|
|
||||||
{
|
{
|
||||||
MessagePackObject aobj = MessagePack.unpack(a);
|
MessagePackObject aobj = MessagePack.unpack(a);
|
||||||
@ -51,12 +77,14 @@ public class TestMessagePackStaticMethods extends TestCase {
|
|||||||
MessagePackObject cobj = MessagePack.unpack(c);
|
MessagePackObject cobj = MessagePack.unpack(c);
|
||||||
MessagePackObject dobj = MessagePack.unpack(d);
|
MessagePackObject dobj = MessagePack.unpack(d);
|
||||||
MessagePackObject eobj = MessagePack.unpack(e);
|
MessagePackObject eobj = MessagePack.unpack(e);
|
||||||
|
MessagePackObject fobj = MessagePack.unpack(f);
|
||||||
|
|
||||||
assertEquals(aobj, RawType.create("msgpack"));
|
assertEquals(aobj, RawType.create("msgpack"));
|
||||||
assertEquals(bobj, IntegerType.create(1));
|
assertEquals(bobj, IntegerType.create(1));
|
||||||
assertEquals(cobj, NilType.create());
|
assertEquals(cobj, NilType.create());
|
||||||
assertEquals(dobj, createStringList_dynamic());
|
assertEquals(dobj, createStringList_dynamic());
|
||||||
assertEquals(eobj, createCustomClass_dynamic());
|
assertEquals(eobj, createProvidedClass_dynamic());
|
||||||
|
assertEquals(fobj, createUserDefinedClass_dynamic());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,7 +99,9 @@ public class TestMessagePackStaticMethods extends TestCase {
|
|||||||
ByteArrayOutputStream dout = new ByteArrayOutputStream();
|
ByteArrayOutputStream dout = new ByteArrayOutputStream();
|
||||||
MessagePack.pack(dout, createStringList());
|
MessagePack.pack(dout, createStringList());
|
||||||
ByteArrayOutputStream eout = new ByteArrayOutputStream();
|
ByteArrayOutputStream eout = new ByteArrayOutputStream();
|
||||||
MessagePack.pack(eout, createCustomClass());
|
MessagePack.pack(eout, createProvidedClass());
|
||||||
|
ByteArrayOutputStream fout = new ByteArrayOutputStream();
|
||||||
|
MessagePack.pack(fout, createUserDefinedClass());
|
||||||
|
|
||||||
{
|
{
|
||||||
InputStream ain = new ByteArrayInputStream(aout.toByteArray());
|
InputStream ain = new ByteArrayInputStream(aout.toByteArray());
|
||||||
@ -84,12 +114,15 @@ public class TestMessagePackStaticMethods extends TestCase {
|
|||||||
MessagePackObject dobj = MessagePack.unpack(din);
|
MessagePackObject dobj = MessagePack.unpack(din);
|
||||||
InputStream ein = new ByteArrayInputStream(eout.toByteArray());
|
InputStream ein = new ByteArrayInputStream(eout.toByteArray());
|
||||||
MessagePackObject eobj = MessagePack.unpack(ein);
|
MessagePackObject eobj = MessagePack.unpack(ein);
|
||||||
|
InputStream fin = new ByteArrayInputStream(fout.toByteArray());
|
||||||
|
MessagePackObject fobj = MessagePack.unpack(fin);
|
||||||
|
|
||||||
assertEquals(aobj, RawType.create("msgpack"));
|
assertEquals(aobj, RawType.create("msgpack"));
|
||||||
assertEquals(bobj, IntegerType.create(1));
|
assertEquals(bobj, IntegerType.create(1));
|
||||||
assertEquals(cobj, NilType.create());
|
assertEquals(cobj, NilType.create());
|
||||||
assertEquals(dobj, createStringList_dynamic());
|
assertEquals(dobj, createStringList_dynamic());
|
||||||
assertEquals(eobj, createCustomClass_dynamic());
|
assertEquals(eobj, createProvidedClass_dynamic());
|
||||||
|
assertEquals(fobj, createUserDefinedClass_dynamic());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -99,7 +132,8 @@ public class TestMessagePackStaticMethods extends TestCase {
|
|||||||
byte[] b = MessagePack.pack((Object)1, TInteger);
|
byte[] b = MessagePack.pack((Object)1, TInteger);
|
||||||
byte[] c = MessagePack.pack((Object)null, TAny);
|
byte[] c = MessagePack.pack((Object)null, TAny);
|
||||||
byte[] d = MessagePack.pack(createStringList(), tList(TString));
|
byte[] d = MessagePack.pack(createStringList(), tList(TString));
|
||||||
byte[] e = MessagePack.pack(createCustomClass(), tClass(CustomClass.class));
|
byte[] e = MessagePack.pack(createProvidedClass(), tClass(ProvidedClass.class));
|
||||||
|
byte[] f = MessagePack.pack(createUserDefinedClass(), tClass(UserDefinedClass.class));
|
||||||
|
|
||||||
{
|
{
|
||||||
Object aobj = MessagePack.unpack(a, TString);
|
Object aobj = MessagePack.unpack(a, TString);
|
||||||
@ -107,26 +141,30 @@ public class TestMessagePackStaticMethods extends TestCase {
|
|||||||
Object cobj_any = MessagePack.unpack(c, TAny);
|
Object cobj_any = MessagePack.unpack(c, TAny);
|
||||||
Object cobj_obj = MessagePack.unpack(c, tOptional(TAny));
|
Object cobj_obj = MessagePack.unpack(c, tOptional(TAny));
|
||||||
Object dobj = MessagePack.unpack(d, tList(TString));
|
Object dobj = MessagePack.unpack(d, tList(TString));
|
||||||
Object eobj = MessagePack.unpack(e, tClass(CustomClass.class));
|
Object eobj = MessagePack.unpack(e, tClass(ProvidedClass.class));
|
||||||
|
Object fobj = MessagePack.unpack(f, tClass(UserDefinedClass.class));
|
||||||
|
|
||||||
assertEquals(aobj, "msgpack");
|
assertEquals(aobj, "msgpack");
|
||||||
assertEquals(bobj, 1);
|
assertEquals(bobj, 1);
|
||||||
assertEquals(cobj_any, NilType.create());
|
assertEquals(cobj_any, NilType.create());
|
||||||
assertEquals(cobj_obj, null);
|
assertEquals(cobj_obj, null);
|
||||||
assertEquals(dobj, createStringList());
|
assertEquals(dobj, createStringList());
|
||||||
assertEquals(eobj, createCustomClass());
|
assertEquals(eobj, createProvidedClass());
|
||||||
|
assertEquals(fobj, createUserDefinedClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
String aobj = MessagePack.unpack(a, String.class);
|
String aobj = MessagePack.unpack(a, String.class);
|
||||||
Integer bobj = MessagePack.unpack(b, Integer.class);
|
Integer bobj = MessagePack.unpack(b, Integer.class);
|
||||||
Object cobj = MessagePack.unpack(c, Object.class);
|
Object cobj = MessagePack.unpack(c, Object.class);
|
||||||
CustomClass eobj = MessagePack.unpack(e, CustomClass.class);
|
ProvidedClass eobj = MessagePack.unpack(e, ProvidedClass.class);
|
||||||
|
UserDefinedClass fobj = MessagePack.unpack(f, UserDefinedClass.class);
|
||||||
|
|
||||||
assertEquals(aobj, "msgpack");
|
assertEquals(aobj, "msgpack");
|
||||||
assertEquals(bobj, (Integer)1);
|
assertEquals(bobj, (Integer)1);
|
||||||
assertEquals(cobj, null);
|
assertEquals(cobj, null);
|
||||||
assertEquals(eobj, createCustomClass());
|
assertEquals(eobj, createProvidedClass());
|
||||||
|
assertEquals(fobj, createUserDefinedClass());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -141,7 +179,9 @@ public class TestMessagePackStaticMethods extends TestCase {
|
|||||||
ByteArrayOutputStream dout = new ByteArrayOutputStream();
|
ByteArrayOutputStream dout = new ByteArrayOutputStream();
|
||||||
MessagePack.pack(dout, createStringList());
|
MessagePack.pack(dout, createStringList());
|
||||||
ByteArrayOutputStream eout = new ByteArrayOutputStream();
|
ByteArrayOutputStream eout = new ByteArrayOutputStream();
|
||||||
MessagePack.pack(eout, createCustomClass());
|
MessagePack.pack(eout, createProvidedClass());
|
||||||
|
ByteArrayOutputStream fout = new ByteArrayOutputStream();
|
||||||
|
MessagePack.pack(fout, createUserDefinedClass());
|
||||||
|
|
||||||
{
|
{
|
||||||
InputStream ain = new ByteArrayInputStream(aout.toByteArray());
|
InputStream ain = new ByteArrayInputStream(aout.toByteArray());
|
||||||
@ -155,14 +195,17 @@ public class TestMessagePackStaticMethods extends TestCase {
|
|||||||
InputStream din = new ByteArrayInputStream(dout.toByteArray());
|
InputStream din = new ByteArrayInputStream(dout.toByteArray());
|
||||||
Object dobj = MessagePack.unpack(din, tList(TString));
|
Object dobj = MessagePack.unpack(din, tList(TString));
|
||||||
InputStream ein = new ByteArrayInputStream(eout.toByteArray());
|
InputStream ein = new ByteArrayInputStream(eout.toByteArray());
|
||||||
Object eobj = MessagePack.unpack(ein, tClass(CustomClass.class));
|
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(aobj, "msgpack");
|
||||||
assertEquals(bobj, 1);
|
assertEquals(bobj, 1);
|
||||||
assertEquals(cobj_any, NilType.create());
|
assertEquals(cobj_any, NilType.create());
|
||||||
assertEquals(cobj_obj, null);
|
assertEquals(cobj_obj, null);
|
||||||
assertEquals(dobj, createStringList());
|
assertEquals(dobj, createStringList());
|
||||||
assertEquals(eobj, createCustomClass());
|
assertEquals(eobj, createProvidedClass());
|
||||||
|
assertEquals(fobj, createUserDefinedClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
@ -173,12 +216,15 @@ public class TestMessagePackStaticMethods extends TestCase {
|
|||||||
InputStream cin = new ByteArrayInputStream(cout.toByteArray());
|
InputStream cin = new ByteArrayInputStream(cout.toByteArray());
|
||||||
Object cobj = MessagePack.unpack(cin, Object.class);
|
Object cobj = MessagePack.unpack(cin, Object.class);
|
||||||
InputStream ein = new ByteArrayInputStream(eout.toByteArray());
|
InputStream ein = new ByteArrayInputStream(eout.toByteArray());
|
||||||
Object eobj = MessagePack.unpack(ein, CustomClass.class);
|
ProvidedClass eobj = MessagePack.unpack(ein, ProvidedClass.class);
|
||||||
|
InputStream fin = new ByteArrayInputStream(fout.toByteArray());
|
||||||
|
UserDefinedClass fobj = MessagePack.unpack(fin, UserDefinedClass.class);
|
||||||
|
|
||||||
assertEquals(aobj, "msgpack");
|
assertEquals(aobj, "msgpack");
|
||||||
assertEquals(bobj, (Integer)1);
|
assertEquals(bobj, (Integer)1);
|
||||||
assertEquals(cobj, null);
|
assertEquals(cobj, null);
|
||||||
assertEquals(eobj, createCustomClass());
|
assertEquals(eobj, createProvidedClass());
|
||||||
|
assertEquals(fobj, createUserDefinedClass());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -200,8 +246,8 @@ public class TestMessagePackStaticMethods extends TestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private CustomClass createCustomClass() {
|
private ProvidedClass createProvidedClass() {
|
||||||
CustomClass obj = new CustomClass();
|
ProvidedClass obj = new ProvidedClass();
|
||||||
obj.bool = true;
|
obj.bool = true;
|
||||||
obj.str = "viver";
|
obj.str = "viver";
|
||||||
obj.list = new ArrayList<Integer>();
|
obj.list = new ArrayList<Integer>();
|
||||||
@ -211,7 +257,7 @@ public class TestMessagePackStaticMethods extends TestCase {
|
|||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
private MessagePackObject createCustomClass_dynamic() {
|
private MessagePackObject createProvidedClass_dynamic() {
|
||||||
MessagePackObject[] obj = new MessagePackObject[3];
|
MessagePackObject[] obj = new MessagePackObject[3];
|
||||||
obj[0] = BooleanType.create(true);
|
obj[0] = BooleanType.create(true);
|
||||||
obj[1] = RawType.create("viver");
|
obj[1] = RawType.create("viver");
|
||||||
@ -222,5 +268,29 @@ public class TestMessagePackStaticMethods extends TestCase {
|
|||||||
obj[2] = ArrayType.create(list);
|
obj[2] = ArrayType.create(list);
|
||||||
return ArrayType.create(obj);
|
return ArrayType.create(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private UserDefinedClass createUserDefinedClass() {
|
||||||
|
UserDefinedClass obj = new UserDefinedClass();
|
||||||
|
obj.bool = false;
|
||||||
|
obj.str = "muga";
|
||||||
|
obj.list = new ArrayList<Integer>();
|
||||||
|
obj.list.add(9);
|
||||||
|
obj.list.add(10);
|
||||||
|
obj.list.add(11);
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
private MessagePackObject createUserDefinedClass_dynamic() {
|
||||||
|
MessagePackObject[] obj = new MessagePackObject[3];
|
||||||
|
obj[0] = BooleanType.create(false);
|
||||||
|
obj[1] = RawType.create("muga");
|
||||||
|
MessagePackObject[] list = new MessagePackObject[3];
|
||||||
|
list[0] = IntegerType.create(9);
|
||||||
|
list[1] = IntegerType.create(10);
|
||||||
|
list[2] = IntegerType.create(11);
|
||||||
|
obj[2] = ArrayType.create(list);
|
||||||
|
return ArrayType.create(obj);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user