mirror of
https://github.com/msgpack/msgpack-python.git
synced 2026-02-07 18:29:53 +00:00
edit DynamicCodeGenPacker and DynamicCodeGenUnpacker class
This commit is contained in:
parent
29e99e229b
commit
bffe0443f9
9 changed files with 674 additions and 255 deletions
|
|
@ -1,50 +1,60 @@
|
|||
package org.msgpack.util.codegen;
|
||||
|
||||
import static org.msgpack.Templates.tString;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.msgpack.MessagePacker;
|
||||
import org.msgpack.MessageUnpacker;
|
||||
import org.msgpack.Packer;
|
||||
import org.msgpack.ReflectionPacker;
|
||||
import org.msgpack.ReflectionTemplate;
|
||||
import org.msgpack.Template;
|
||||
import org.msgpack.Unpacker;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
|
||||
public class TestDynamicCodeGenPacker extends TestCase {
|
||||
public static class StringFieldClass {
|
||||
public String s1;
|
||||
public String s2;
|
||||
public StringFieldClass() { }
|
||||
|
||||
public StringFieldClass() {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPackConvert() throws Exception {
|
||||
tString(); // FIXME link StringTemplate
|
||||
|
||||
public void testPackConvert01() throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicCodeGenPacker.create(StringFieldClass.class);
|
||||
MessagePacker packer = DynamicCodeGenPacker
|
||||
.create(StringFieldClass.class);
|
||||
MessageUnpacker unpacker = DynamicCodeGenUnpacker
|
||||
.create(StringFieldClass.class);
|
||||
|
||||
StringFieldClass src = new StringFieldClass();
|
||||
|
||||
src.s1 = "kumofs";
|
||||
src.s2 = "frsyuki";
|
||||
|
||||
src.s1 = "muga";
|
||||
src.s2 = "nishizawa";
|
||||
packer.pack(new Packer(out), src);
|
||||
|
||||
Template tmpl = ReflectionTemplate.create(StringFieldClass.class);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
StringFieldClass dst = (StringFieldClass) new Unpacker(in)
|
||||
.unpack(unpacker);
|
||||
assertEquals(src.s1, dst.s1);
|
||||
assertEquals(src.s2, dst.s2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPackConvert02() throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicCodeGenPacker
|
||||
.create(StringFieldClass.class);
|
||||
Template tmpl = DynamicCodeGenTemplate.create(StringFieldClass.class);
|
||||
|
||||
StringFieldClass src = new StringFieldClass();
|
||||
src.s1 = "muga";
|
||||
src.s2 = "nishizawa";
|
||||
packer.pack(new Packer(out), src);
|
||||
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
|
||||
Object obj = tmpl.unpack(new Unpacker(in));
|
||||
assertEquals(obj.getClass(), StringFieldClass.class);
|
||||
|
||||
StringFieldClass dst = (StringFieldClass)obj;
|
||||
StringFieldClass dst = (StringFieldClass) tmpl.unpack(new Unpacker(in));
|
||||
assertEquals(src.s1, dst.s1);
|
||||
assertEquals(src.s2, dst.s2);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue