java: adds a test program of the DynamicCodeGen class for packing and unpacking an object that has a field anntated by @MessagePackMessage

This commit is contained in:
Muga Nishizawa 2010-09-30 00:55:56 +09:00
parent 92ddb37ed3
commit 7f7f5253f2
4 changed files with 129 additions and 73 deletions

View file

@ -20,6 +20,7 @@ import org.msgpack.MessagePacker;
import org.msgpack.Packer;
import org.msgpack.Template;
import org.msgpack.Unpacker;
import org.msgpack.annotation.MessagePackMessage;
public class TestDynamicCodeGenPackerConverter extends TestCase {
@ -534,6 +535,44 @@ public class TestDynamicCodeGenPackerConverter extends TestCase {
}
}
@Test
public void testMessagePackMessageFieldClass() throws Exception {
BaseClass2 src = new BaseClass2();
MessagePackMessageClass2 src2 = new MessagePackMessageClass2();
src.f0 = 0;
src2.f2 = 2;
src.f1 = src2;
ByteArrayOutputStream out = new ByteArrayOutputStream();
MessagePacker packer = DynamicCodeGenPacker.create(BaseClass2.class);
packer.pack(new Packer(out), src);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
Unpacker pac = new Unpacker(in);
Iterator<MessagePackObject> it = pac.iterator();
assertTrue(it.hasNext());
MessagePackObject mpo = it.next();
Template tmpl = DynamicCodeGenTemplate.create(BaseClass2.class);
BaseClass2 dst = (BaseClass2) tmpl.convert(mpo);
assertTrue(src.f0 == dst.f0);
assertTrue(src.f1.f2 == dst.f1.f2);
assertFalse(it.hasNext());
}
public static class BaseClass2 {
public int f0;
public MessagePackMessageClass2 f1;
public BaseClass2() {
}
}
@MessagePackMessage
public static class MessagePackMessageClass2 {
public int f2;
public MessagePackMessageClass2() {
}
}
@Test
public void testExtendedClass() throws Exception {
SampleSubClass src = new SampleSubClass();
@ -581,6 +620,7 @@ public class TestDynamicCodeGenPackerConverter extends TestCase {
public static class SampleSuperClass {
public int f5;
public final int f6 = 2;
@SuppressWarnings("unused")
private int f7;
protected int f8;
int f9;

View file

@ -17,6 +17,7 @@ import org.msgpack.MessagePacker;
import org.msgpack.Packer;
import org.msgpack.Template;
import org.msgpack.Unpacker;
import org.msgpack.annotation.MessagePackMessage;
import junit.framework.TestCase;
@ -504,6 +505,39 @@ public class TestDynamicCodeGenPackerUnpacker extends TestCase {
}
}
@Test
public void testMessagePackMessageFieldClass() throws Exception {
BaseClass2 src = new BaseClass2();
MessagePackMessageClass2 src2 = new MessagePackMessageClass2();
src.f0 = 0;
src2.f2 = 2;
src.f1 = src2;
ByteArrayOutputStream out = new ByteArrayOutputStream();
MessagePacker packer = DynamicCodeGenPacker.create(BaseClass2.class);
packer.pack(new Packer(out), src);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
Template tmpl = DynamicCodeGenTemplate.create(BaseClass2.class);
BaseClass2 dst = (BaseClass2) tmpl.unpack(new Unpacker(in));
assertTrue(src.f0 == dst.f0);
assertTrue(src.f1.f2 == dst.f1.f2);
}
public static class BaseClass2 {
public int f0;
public MessagePackMessageClass2 f1;
public BaseClass2() {
}
}
@MessagePackMessage
public static class MessagePackMessageClass2 {
public int f2;
public MessagePackMessageClass2() {
}
}
@Test
public void testExtendedClass() throws Exception {
SampleSubClass src = new SampleSubClass();
@ -546,6 +580,7 @@ public class TestDynamicCodeGenPackerUnpacker extends TestCase {
public static class SampleSuperClass {
public int f5;
public final int f6 = 2;
@SuppressWarnings("unused")
private int f7;
protected int f8;
int f9;