mirror of
https://github.com/msgpack/msgpack-python.git
synced 2026-02-08 02:40:09 +00:00
java: migrates from util.codegen to TemplateBuilder
This commit is contained in:
parent
78daac0f1b
commit
bd9a2c0d3a
52 changed files with 44 additions and 8371 deletions
|
|
@ -1,448 +0,0 @@
|
|||
package org.msgpack.packer;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.math.BigInteger;
|
||||
import java.util.Random;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.msgpack.MessagePackObject;
|
||||
import org.msgpack.MessagePacker;
|
||||
import org.msgpack.MessageTypeException;
|
||||
import org.msgpack.Packer;
|
||||
import org.msgpack.Template;
|
||||
import org.msgpack.Util;
|
||||
import org.msgpack.template.BigIntegerTemplate;
|
||||
import org.msgpack.template.BooleanTemplate;
|
||||
import org.msgpack.template.ByteTemplate;
|
||||
import org.msgpack.template.DoubleTemplate;
|
||||
import org.msgpack.template.FloatTemplate;
|
||||
import org.msgpack.template.IntegerTemplate;
|
||||
import org.msgpack.template.LongTemplate;
|
||||
import org.msgpack.template.NullableTemplate;
|
||||
import org.msgpack.template.ShortTemplate;
|
||||
import org.msgpack.template.StringTemplate;
|
||||
|
||||
public class TestPackConvert extends TestCase {
|
||||
|
||||
@Test
|
||||
public void testByte() throws Exception {
|
||||
_testByte((byte) 0);
|
||||
_testByte((byte) -1);
|
||||
_testByte((byte) 1);
|
||||
_testByte(Byte.MIN_VALUE);
|
||||
_testByte(Byte.MAX_VALUE);
|
||||
Random rand = new Random();
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
_testByte((byte) rand.nextInt());
|
||||
}
|
||||
}
|
||||
|
||||
static void _testByte(Byte src) throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = BytePacker.getInstance();
|
||||
packer.pack(new Packer(out), src);
|
||||
MessagePackObject obj = Util.unpackOne(out.toByteArray());
|
||||
assertEquals(src.byteValue(), obj.asByte());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullByte() throws Exception {
|
||||
Byte src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = new OptionalPacker(BytePacker.getInstance());
|
||||
packer.pack(new Packer(out), src);
|
||||
MessagePackObject obj = Util.unpackOne(out.toByteArray());
|
||||
Template tmpl = null;
|
||||
Byte dst = null;
|
||||
try {
|
||||
tmpl = ByteTemplate.getInstance();
|
||||
dst = (Byte) tmpl.convert(obj, null);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
obj = Util.unpackOne(out.toByteArray());
|
||||
tmpl = new NullableTemplate(ByteTemplate.getInstance());
|
||||
dst = (Byte) tmpl.convert(obj, null);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShort() throws Exception {
|
||||
_testShort((short) 0);
|
||||
_testShort((short) -1);
|
||||
_testShort((short) 1);
|
||||
_testShort(Short.MIN_VALUE);
|
||||
_testShort(Short.MAX_VALUE);
|
||||
Random rand = new Random();
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
_testShort((short) rand.nextInt());
|
||||
}
|
||||
}
|
||||
|
||||
static void _testShort(Short src) throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = ShortPacker.getInstance();
|
||||
packer.pack(new Packer(out), src);
|
||||
MessagePackObject obj = Util.unpackOne(out.toByteArray());
|
||||
assertEquals(src.shortValue(), obj.asShort());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullShort() throws Exception {
|
||||
Short src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = new OptionalPacker(ShortPacker.getInstance());
|
||||
packer.pack(new Packer(out), src);
|
||||
MessagePackObject obj = Util.unpackOne(out.toByteArray());
|
||||
Template tmpl = null;
|
||||
Short dst = null;
|
||||
try {
|
||||
tmpl = ShortTemplate.getInstance();
|
||||
dst = (Short) tmpl.convert(obj, null);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
obj = Util.unpackOne(out.toByteArray());
|
||||
tmpl = new NullableTemplate(ShortTemplate.getInstance());
|
||||
dst = (Short) tmpl.convert(obj, null);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInteger() throws Exception {
|
||||
_testInteger(0);
|
||||
_testInteger(-1);
|
||||
_testInteger(1);
|
||||
_testInteger(Integer.MIN_VALUE);
|
||||
_testInteger(Integer.MAX_VALUE);
|
||||
Random rand = new Random();
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
_testInteger(rand.nextInt());
|
||||
}
|
||||
}
|
||||
|
||||
static void _testInteger(Integer src) throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = IntegerPacker.getInstance();
|
||||
packer.pack(new Packer(out), src);
|
||||
MessagePackObject obj = Util.unpackOne(out.toByteArray());
|
||||
assertEquals(src.intValue(), obj.asInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullInteger() throws Exception {
|
||||
Integer src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = new OptionalPacker(IntegerPacker.getInstance());
|
||||
packer.pack(new Packer(out), src);
|
||||
MessagePackObject obj = Util.unpackOne(out.toByteArray());
|
||||
Template tmpl = null;
|
||||
Integer dst = null;
|
||||
try {
|
||||
tmpl = IntegerTemplate.getInstance();
|
||||
dst = (Integer) tmpl.convert(obj, null);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
obj = Util.unpackOne(out.toByteArray());
|
||||
tmpl = new NullableTemplate(IntegerTemplate.getInstance());
|
||||
dst = (Integer) tmpl.convert(obj, null);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLong() throws Exception {
|
||||
_testLong((long) 0);
|
||||
_testLong((long) -1);
|
||||
_testLong((long) 1);
|
||||
_testLong((long) Integer.MIN_VALUE);
|
||||
_testLong((long) Integer.MAX_VALUE);
|
||||
_testLong(Long.MIN_VALUE);
|
||||
_testLong(Long.MAX_VALUE);
|
||||
Random rand = new Random();
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
_testLong(rand.nextLong());
|
||||
}
|
||||
}
|
||||
|
||||
static void _testLong(Long src) throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = LongPacker.getInstance();
|
||||
packer.pack(new Packer(out), src);
|
||||
MessagePackObject obj = Util.unpackOne(out.toByteArray());
|
||||
assertEquals(src.longValue(), obj.asLong());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullLong() throws Exception {
|
||||
Long src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = new OptionalPacker(LongPacker.getInstance());
|
||||
packer.pack(new Packer(out), src);
|
||||
MessagePackObject obj = Util.unpackOne(out.toByteArray());
|
||||
Template tmpl = null;
|
||||
Long dst = null;
|
||||
try {
|
||||
tmpl = LongTemplate.getInstance();
|
||||
dst = (Long) tmpl.convert(obj, null);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
obj = Util.unpackOne(out.toByteArray());
|
||||
tmpl = new NullableTemplate(LongTemplate.getInstance());
|
||||
dst = (Long) tmpl.convert(obj, null);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBigInteger() throws Exception {
|
||||
_testBigInteger(BigInteger.valueOf(0));
|
||||
_testBigInteger(BigInteger.valueOf(-1));
|
||||
_testBigInteger(BigInteger.valueOf(1));
|
||||
_testBigInteger(BigInteger.valueOf(Integer.MIN_VALUE));
|
||||
_testBigInteger(BigInteger.valueOf(Integer.MAX_VALUE));
|
||||
_testBigInteger(BigInteger.valueOf(Long.MIN_VALUE));
|
||||
_testBigInteger(BigInteger.valueOf(Long.MAX_VALUE));
|
||||
BigInteger max = BigInteger.valueOf(Long.MAX_VALUE).setBit(63);
|
||||
_testBigInteger(max);
|
||||
Random rand = new Random();
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
_testBigInteger(max.subtract(BigInteger.valueOf(Math.abs(rand
|
||||
.nextLong()))));
|
||||
}
|
||||
}
|
||||
|
||||
static void _testBigInteger(BigInteger src) throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = BigIntegerPacker.getInstance();
|
||||
packer.pack(new Packer(out), src);
|
||||
MessagePackObject obj = Util.unpackOne(out.toByteArray());
|
||||
assertEquals(src, obj.asBigInteger());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullBigInteger() throws Exception {
|
||||
Long src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = new OptionalPacker(BigIntegerPacker
|
||||
.getInstance());
|
||||
packer.pack(new Packer(out), src);
|
||||
MessagePackObject obj = Util.unpackOne(out.toByteArray());
|
||||
Template tmpl = null;
|
||||
BigInteger dst = null;
|
||||
try {
|
||||
tmpl = BigIntegerTemplate.getInstance();
|
||||
dst = (BigInteger) tmpl.convert(obj, null);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
obj = Util.unpackOne(out.toByteArray());
|
||||
tmpl = new NullableTemplate(BigIntegerTemplate.getInstance());
|
||||
dst = (BigInteger) tmpl.convert(obj, null);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFloat() throws Exception {
|
||||
_testFloat((float) 0.0);
|
||||
_testFloat((float) -0.0);
|
||||
_testFloat((float) 1.0);
|
||||
_testFloat((float) -1.0);
|
||||
_testFloat((float) Float.MAX_VALUE);
|
||||
_testFloat((float) Float.MIN_VALUE);
|
||||
_testFloat((float) Float.NaN);
|
||||
_testFloat((float) Float.NEGATIVE_INFINITY);
|
||||
_testFloat((float) Float.POSITIVE_INFINITY);
|
||||
Random rand = new Random();
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
_testFloat(rand.nextFloat());
|
||||
}
|
||||
}
|
||||
|
||||
static void _testFloat(Float src) throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = FloatPacker.getInstance();
|
||||
packer.pack(new Packer(out), src);
|
||||
MessagePackObject obj = Util.unpackOne(out.toByteArray());
|
||||
assertEquals(src.floatValue(), obj.asFloat(), 10e-10);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullFloat() throws Exception {
|
||||
Long src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = new OptionalPacker(FloatPacker.getInstance());
|
||||
packer.pack(new Packer(out), src);
|
||||
MessagePackObject obj = Util.unpackOne(out.toByteArray());
|
||||
Template tmpl = null;
|
||||
Float dst = null;
|
||||
try {
|
||||
tmpl = FloatTemplate.getInstance();
|
||||
dst = (Float) tmpl.convert(obj, null);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
obj = Util.unpackOne(out.toByteArray());
|
||||
tmpl = new NullableTemplate(FloatTemplate.getInstance());
|
||||
dst = (Float) tmpl.convert(obj, null);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDouble() throws Exception {
|
||||
_testDouble((double) 0.0);
|
||||
_testDouble((double) -0.0);
|
||||
_testDouble((double) 1.0);
|
||||
_testDouble((double) -1.0);
|
||||
_testDouble((double) Double.MAX_VALUE);
|
||||
_testDouble((double) Double.MIN_VALUE);
|
||||
_testDouble((double) Double.NaN);
|
||||
_testDouble((double) Double.NEGATIVE_INFINITY);
|
||||
_testDouble((double) Double.POSITIVE_INFINITY);
|
||||
Random rand = new Random();
|
||||
for (int i = 0; i < 1000; i++)
|
||||
_testDouble(rand.nextDouble());
|
||||
}
|
||||
|
||||
static void _testDouble(Double src) throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DoublePacker.getInstance();
|
||||
packer.pack(new Packer(out), src);
|
||||
MessagePackObject obj = Util.unpackOne(out.toByteArray());
|
||||
assertEquals(src.doubleValue(), obj.asDouble(), 10e-10);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullDouble() throws Exception {
|
||||
Long src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = new OptionalPacker(DoublePacker.getInstance());
|
||||
packer.pack(new Packer(out), src);
|
||||
MessagePackObject obj = Util.unpackOne(out.toByteArray());
|
||||
Template tmpl = null;
|
||||
Double dst = null;
|
||||
try {
|
||||
tmpl = DoubleTemplate.getInstance();
|
||||
dst = (Double) tmpl.convert(obj, null);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
obj = Util.unpackOne(out.toByteArray());
|
||||
tmpl = new NullableTemplate(DoubleTemplate.getInstance());
|
||||
dst = (Double) tmpl.convert(obj, null);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBoolean() throws Exception {
|
||||
_testBoolean(false);
|
||||
_testBoolean(true);
|
||||
}
|
||||
|
||||
static void _testBoolean(Boolean src) throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = BooleanPacker.getInstance();
|
||||
packer.pack(new Packer(out), src);
|
||||
MessagePackObject obj = Util.unpackOne(out.toByteArray());
|
||||
assertEquals(src.booleanValue(), obj.asBoolean());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullBoolean() throws Exception {
|
||||
Long src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = new OptionalPacker(BooleanPacker.getInstance());
|
||||
packer.pack(new Packer(out), src);
|
||||
MessagePackObject obj = Util.unpackOne(out.toByteArray());
|
||||
Template tmpl = null;
|
||||
Boolean dst = null;
|
||||
try {
|
||||
tmpl = BooleanTemplate.getInstance();
|
||||
dst = (Boolean) tmpl.convert(obj, null);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
obj = Util.unpackOne(out.toByteArray());
|
||||
tmpl = new NullableTemplate(BooleanTemplate.getInstance());
|
||||
dst = (Boolean) tmpl.convert(obj, null);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testString() throws Exception {
|
||||
_testString("");
|
||||
_testString("a");
|
||||
_testString("ab");
|
||||
_testString("abc");
|
||||
|
||||
// small size string
|
||||
for (int i = 0; i < 100; i++) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int len = (int) Math.random() % 31 + 1;
|
||||
for (int j = 0; j < len; j++) {
|
||||
sb.append('a' + ((int) Math.random()) & 26);
|
||||
}
|
||||
_testString(sb.toString());
|
||||
}
|
||||
|
||||
// medium size string
|
||||
for (int i = 0; i < 100; i++) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int len = (int) Math.random() % 100 + (1 << 15);
|
||||
for (int j = 0; j < len; j++) {
|
||||
sb.append('a' + ((int) Math.random()) & 26);
|
||||
}
|
||||
_testString(sb.toString());
|
||||
}
|
||||
|
||||
// large size string
|
||||
for (int i = 0; i < 10; i++) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int len = (int) Math.random() % 100 + (1 << 31);
|
||||
for (int j = 0; j < len; j++) {
|
||||
sb.append('a' + ((int) Math.random()) & 26);
|
||||
}
|
||||
_testString(sb.toString());
|
||||
}
|
||||
}
|
||||
|
||||
static void _testString(String src) throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = StringPacker.getInstance();
|
||||
packer.pack(new Packer(out), src);
|
||||
MessagePackObject obj = Util.unpackOne(out.toByteArray());
|
||||
assertEquals(src, obj.asString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullString() throws Exception {
|
||||
String src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = new OptionalPacker(StringPacker.getInstance());
|
||||
packer.pack(new Packer(out), src);
|
||||
MessagePackObject obj = Util.unpackOne(out.toByteArray());
|
||||
Template tmpl = null;
|
||||
String dst = null;
|
||||
try {
|
||||
tmpl = StringTemplate.getInstance();
|
||||
dst = (String) tmpl.convert(obj, null);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
obj = Util.unpackOne(out.toByteArray());
|
||||
tmpl = new NullableTemplate(StringTemplate.getInstance());
|
||||
dst = (String) tmpl.convert(obj, null);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,475 +0,0 @@
|
|||
package org.msgpack.packer;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.math.BigInteger;
|
||||
import java.util.Random;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.msgpack.MessagePacker;
|
||||
import org.msgpack.MessageTypeException;
|
||||
import org.msgpack.Packer;
|
||||
import org.msgpack.Template;
|
||||
import org.msgpack.Unpacker;
|
||||
import org.msgpack.template.BigIntegerTemplate;
|
||||
import org.msgpack.template.BooleanTemplate;
|
||||
import org.msgpack.template.ByteTemplate;
|
||||
import org.msgpack.template.DoubleTemplate;
|
||||
import org.msgpack.template.FloatTemplate;
|
||||
import org.msgpack.template.IntegerTemplate;
|
||||
import org.msgpack.template.LongTemplate;
|
||||
import org.msgpack.template.NullableTemplate;
|
||||
import org.msgpack.template.ShortTemplate;
|
||||
import org.msgpack.template.StringTemplate;
|
||||
|
||||
public class TestPackUnpack extends TestCase {
|
||||
|
||||
@Test
|
||||
public void testByte() throws Exception {
|
||||
_testByte((byte) 0);
|
||||
_testByte((byte) -1);
|
||||
_testByte((byte) 1);
|
||||
_testByte(Byte.MIN_VALUE);
|
||||
_testByte(Byte.MAX_VALUE);
|
||||
Random rand = new Random();
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
_testByte((byte) rand.nextInt());
|
||||
}
|
||||
}
|
||||
|
||||
static void _testByte(Byte src) throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = BytePacker.getInstance();
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Unpacker unpacker = new Unpacker(in);
|
||||
assertEquals(src.byteValue(), unpacker.unpackByte());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullByte() throws Exception {
|
||||
Byte src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = new OptionalPacker(BytePacker.getInstance());
|
||||
packer.pack(new Packer(out), src);
|
||||
byte[] bytes = out.toByteArray();
|
||||
Template tmpl = null;
|
||||
Unpacker unpacker = new Unpacker();
|
||||
Byte dst = null;
|
||||
try {
|
||||
tmpl = ByteTemplate.getInstance();
|
||||
unpacker.wrap(bytes);
|
||||
dst = (Byte) tmpl.unpack(unpacker, null);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
unpacker.wrap(bytes);
|
||||
tmpl = new NullableTemplate(ByteTemplate.getInstance());
|
||||
dst = (Byte) tmpl.unpack(unpacker, null);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSort() throws Exception {
|
||||
_testShort((short) 0);
|
||||
_testShort((short) -1);
|
||||
_testShort((short) 1);
|
||||
_testShort(Short.MIN_VALUE);
|
||||
_testShort(Short.MAX_VALUE);
|
||||
Random rand = new Random();
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
_testShort((short) rand.nextInt());
|
||||
}
|
||||
}
|
||||
|
||||
static void _testShort(Short src) throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = ShortPacker.getInstance();
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Unpacker unpacker = new Unpacker(in);
|
||||
assertEquals(src.shortValue(), unpacker.unpackShort());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullShort() throws Exception {
|
||||
Short src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = new OptionalPacker(ShortPacker.getInstance());
|
||||
packer.pack(new Packer(out), src);
|
||||
byte[] bytes = out.toByteArray();
|
||||
Template tmpl = null;
|
||||
Unpacker unpacker = new Unpacker();
|
||||
Short dst = null;
|
||||
try {
|
||||
tmpl = ShortTemplate.getInstance();
|
||||
unpacker.wrap(bytes);
|
||||
dst = (Short) tmpl.unpack(unpacker, null);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
unpacker.wrap(bytes);
|
||||
tmpl = new NullableTemplate(ShortTemplate.getInstance());
|
||||
dst = (Short) tmpl.unpack(unpacker, null);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInteger() throws Exception {
|
||||
_testInteger(0);
|
||||
_testInteger(-1);
|
||||
_testInteger(1);
|
||||
_testInteger(Integer.MIN_VALUE);
|
||||
_testInteger(Integer.MAX_VALUE);
|
||||
Random rand = new Random();
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
_testInteger(rand.nextInt());
|
||||
}
|
||||
}
|
||||
|
||||
static void _testInteger(Integer src) throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = IntegerPacker.getInstance();
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Unpacker unpacker = new Unpacker(in);
|
||||
assertEquals(src.intValue(), unpacker.unpackInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullInteger() throws Exception {
|
||||
Integer src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = new OptionalPacker(IntegerPacker.getInstance());
|
||||
packer.pack(new Packer(out), src);
|
||||
byte[] bytes = out.toByteArray();
|
||||
Template tmpl = null;
|
||||
Unpacker unpacker = new Unpacker();
|
||||
Integer dst = null;
|
||||
try {
|
||||
tmpl = IntegerTemplate.getInstance();
|
||||
unpacker.wrap(bytes);
|
||||
dst = (Integer) tmpl.unpack(unpacker, null);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
unpacker.wrap(bytes);
|
||||
tmpl = new NullableTemplate(IntegerTemplate.getInstance());
|
||||
dst = (Integer) tmpl.unpack(unpacker, null);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLong() throws Exception {
|
||||
_testLong((long) 0);
|
||||
_testLong((long) -1);
|
||||
_testLong((long) 1);
|
||||
_testLong((long) Integer.MIN_VALUE);
|
||||
_testLong((long) Integer.MAX_VALUE);
|
||||
_testLong(Long.MIN_VALUE);
|
||||
_testLong(Long.MAX_VALUE);
|
||||
Random rand = new Random();
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
_testLong(rand.nextLong());
|
||||
}
|
||||
}
|
||||
|
||||
static void _testLong(Long src) throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = LongPacker.getInstance();
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Unpacker unpacker = new Unpacker(in);
|
||||
assertEquals(src.longValue(), unpacker.unpackLong());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullLong() throws Exception {
|
||||
Integer src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = new OptionalPacker(LongPacker.getInstance());
|
||||
packer.pack(new Packer(out), src);
|
||||
byte[] bytes = out.toByteArray();
|
||||
Template tmpl = null;
|
||||
Unpacker unpacker = new Unpacker();
|
||||
Long dst = null;
|
||||
try {
|
||||
tmpl = LongTemplate.getInstance();
|
||||
unpacker.wrap(bytes);
|
||||
dst = (Long) tmpl.unpack(unpacker, null);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
unpacker.wrap(bytes);
|
||||
tmpl = new NullableTemplate(LongTemplate.getInstance());
|
||||
dst = (Long) tmpl.unpack(unpacker, null);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBigInteger() throws Exception {
|
||||
_testBigInteger(BigInteger.valueOf(0));
|
||||
_testBigInteger(BigInteger.valueOf(-1));
|
||||
_testBigInteger(BigInteger.valueOf(1));
|
||||
_testBigInteger(BigInteger.valueOf(Integer.MIN_VALUE));
|
||||
_testBigInteger(BigInteger.valueOf(Integer.MAX_VALUE));
|
||||
_testBigInteger(BigInteger.valueOf(Long.MIN_VALUE));
|
||||
_testBigInteger(BigInteger.valueOf(Long.MAX_VALUE));
|
||||
BigInteger max = BigInteger.valueOf(Long.MAX_VALUE).setBit(63);
|
||||
_testBigInteger(max);
|
||||
Random rand = new Random();
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
_testBigInteger(max.subtract(BigInteger.valueOf(Math.abs(rand
|
||||
.nextLong()))));
|
||||
}
|
||||
}
|
||||
|
||||
static void _testBigInteger(BigInteger src) throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = BigIntegerPacker.getInstance();
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Unpacker unpacker = new Unpacker(in);
|
||||
assertEquals(src, unpacker.unpackBigInteger());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullBigInteger() throws Exception {
|
||||
BigInteger src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = new OptionalPacker(BigIntegerPacker
|
||||
.getInstance());
|
||||
packer.pack(new Packer(out), src);
|
||||
byte[] bytes = out.toByteArray();
|
||||
Template tmpl = null;
|
||||
Unpacker unpacker = new Unpacker();
|
||||
BigInteger dst = null;
|
||||
try {
|
||||
tmpl = BigIntegerTemplate.getInstance();
|
||||
unpacker.wrap(bytes);
|
||||
dst = (BigInteger) tmpl.unpack(unpacker, null);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
unpacker.wrap(bytes);
|
||||
tmpl = new NullableTemplate(BigIntegerTemplate.getInstance());
|
||||
dst = (BigInteger) tmpl.unpack(unpacker, null);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFloat() throws Exception {
|
||||
_testFloat((float) 0.0);
|
||||
_testFloat((float) -0.0);
|
||||
_testFloat((float) 1.0);
|
||||
_testFloat((float) -1.0);
|
||||
_testFloat((float) Float.MAX_VALUE);
|
||||
_testFloat((float) Float.MIN_VALUE);
|
||||
_testFloat((float) Float.NaN);
|
||||
_testFloat((float) Float.NEGATIVE_INFINITY);
|
||||
_testFloat((float) Float.POSITIVE_INFINITY);
|
||||
Random rand = new Random();
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
_testFloat(rand.nextFloat());
|
||||
}
|
||||
}
|
||||
|
||||
static void _testFloat(Float src) throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = FloatPacker.getInstance();
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Unpacker unpacker = new Unpacker(in);
|
||||
assertEquals(src.floatValue(), unpacker.unpackFloat(), 10e-10);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullFloat() throws Exception {
|
||||
Float src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = new OptionalPacker(FloatPacker.getInstance());
|
||||
packer.pack(new Packer(out), src);
|
||||
byte[] bytes = out.toByteArray();
|
||||
Template tmpl = null;
|
||||
Unpacker unpacker = new Unpacker();
|
||||
Float dst = null;
|
||||
try {
|
||||
tmpl = FloatTemplate.getInstance();
|
||||
unpacker.wrap(bytes);
|
||||
dst = (Float) tmpl.unpack(unpacker, null);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
unpacker.wrap(bytes);
|
||||
tmpl = new NullableTemplate(FloatTemplate.getInstance());
|
||||
dst = (Float) tmpl.unpack(unpacker, null);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDouble() throws Exception {
|
||||
_testDouble((double) 0.0);
|
||||
_testDouble((double) -0.0);
|
||||
_testDouble((double) 1.0);
|
||||
_testDouble((double) -1.0);
|
||||
_testDouble((double) Double.MAX_VALUE);
|
||||
_testDouble((double) Double.MIN_VALUE);
|
||||
_testDouble((double) Double.NaN);
|
||||
_testDouble((double) Double.NEGATIVE_INFINITY);
|
||||
_testDouble((double) Double.POSITIVE_INFINITY);
|
||||
Random rand = new Random();
|
||||
for (int i = 0; i < 1000; i++)
|
||||
_testDouble(rand.nextDouble());
|
||||
}
|
||||
|
||||
static void _testDouble(Double src) throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DoublePacker.getInstance();
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Unpacker unpacker = new Unpacker(in);
|
||||
assertEquals(src.doubleValue(), unpacker.unpackDouble(), 10e-10);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullDouble() throws Exception {
|
||||
Double src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = new OptionalPacker(DoublePacker.getInstance());
|
||||
packer.pack(new Packer(out), src);
|
||||
byte[] bytes = out.toByteArray();
|
||||
Template tmpl = null;
|
||||
Unpacker unpacker = new Unpacker();
|
||||
Double dst = null;
|
||||
try {
|
||||
tmpl = DoubleTemplate.getInstance();
|
||||
unpacker.wrap(bytes);
|
||||
dst = (Double) tmpl.unpack(unpacker, null);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
unpacker.wrap(bytes);
|
||||
tmpl = new NullableTemplate(DoubleTemplate.getInstance());
|
||||
dst = (Double) tmpl.unpack(unpacker, null);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBoolean() throws Exception {
|
||||
_testBoolean(false);
|
||||
_testBoolean(true);
|
||||
}
|
||||
|
||||
static void _testBoolean(Boolean src) throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = BooleanPacker.getInstance();
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Unpacker unpacker = new Unpacker(in);
|
||||
assertEquals(src.booleanValue(), unpacker.unpackBoolean());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullBoolean() throws Exception {
|
||||
Boolean src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = new OptionalPacker(BooleanPacker.getInstance());
|
||||
packer.pack(new Packer(out), src);
|
||||
byte[] bytes = out.toByteArray();
|
||||
Template tmpl = null;
|
||||
Unpacker unpacker = new Unpacker();
|
||||
Boolean dst = null;
|
||||
try {
|
||||
tmpl = BooleanTemplate.getInstance();
|
||||
unpacker.wrap(bytes);
|
||||
dst = (Boolean) tmpl.unpack(unpacker, null);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
unpacker.wrap(bytes);
|
||||
tmpl = new NullableTemplate(BooleanTemplate.getInstance());
|
||||
dst = (Boolean) tmpl.unpack(unpacker, null);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testString() throws Exception {
|
||||
_testString("");
|
||||
_testString("a");
|
||||
_testString("ab");
|
||||
_testString("abc");
|
||||
|
||||
// small size string
|
||||
for (int i = 0; i < 100; i++) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int len = (int) Math.random() % 31 + 1;
|
||||
for (int j = 0; j < len; j++) {
|
||||
sb.append('a' + ((int) Math.random()) & 26);
|
||||
}
|
||||
_testString(sb.toString());
|
||||
}
|
||||
|
||||
// medium size string
|
||||
for (int i = 0; i < 100; i++) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int len = (int) Math.random() % 100 + (1 << 15);
|
||||
for (int j = 0; j < len; j++) {
|
||||
sb.append('a' + ((int) Math.random()) & 26);
|
||||
}
|
||||
_testString(sb.toString());
|
||||
}
|
||||
|
||||
// large size string
|
||||
for (int i = 0; i < 10; i++) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int len = (int) Math.random() % 100 + (1 << 31);
|
||||
for (int j = 0; j < len; j++) {
|
||||
sb.append('a' + ((int) Math.random()) & 26);
|
||||
}
|
||||
_testString(sb.toString());
|
||||
}
|
||||
}
|
||||
|
||||
static void _testString(String src) throws Exception {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = StringPacker.getInstance();
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Unpacker unpacker = new Unpacker(in);
|
||||
assertEquals(src, unpacker.unpackString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullString() throws Exception {
|
||||
String src = null;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = new OptionalPacker(StringPacker.getInstance());
|
||||
packer.pack(new Packer(out), src);
|
||||
byte[] bytes = out.toByteArray();
|
||||
Template tmpl = null;
|
||||
Unpacker unpacker = new Unpacker();
|
||||
String dst = null;
|
||||
try {
|
||||
tmpl = StringTemplate.getInstance();
|
||||
unpacker.wrap(bytes);
|
||||
dst = (String) tmpl.unpack(unpacker, null);
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof MessageTypeException);
|
||||
}
|
||||
unpacker.wrap(bytes);
|
||||
tmpl = new NullableTemplate(StringTemplate.getInstance());
|
||||
dst = (String) tmpl.unpack(unpacker, null);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,218 +0,0 @@
|
|||
package org.msgpack.template;
|
||||
|
||||
import java.util.*;
|
||||
import java.io.*;
|
||||
import java.math.*;
|
||||
import org.msgpack.*;
|
||||
import org.msgpack.annotation.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class TestTemplateBuilder extends TestCase {
|
||||
public static class PrimitiveTypeFieldsClass {
|
||||
public byte f0;
|
||||
public short f1;
|
||||
public int f2;
|
||||
public long f3;
|
||||
public float f4;
|
||||
public double f5;
|
||||
public boolean f6;
|
||||
|
||||
public PrimitiveTypeFieldsClass() {
|
||||
}
|
||||
}
|
||||
|
||||
public static class GeneralReferenceTypeFieldsClass {
|
||||
public Byte f0;
|
||||
public Short f1;
|
||||
public Integer f2;
|
||||
public Long f3;
|
||||
public Float f4;
|
||||
public Double f5;
|
||||
public Boolean f6;
|
||||
public BigInteger f7;
|
||||
public String f8;
|
||||
public byte[] f9;
|
||||
|
||||
public GeneralReferenceTypeFieldsClass() {
|
||||
}
|
||||
}
|
||||
|
||||
public static class SampleListTypes {
|
||||
public List<Integer> f0;
|
||||
public List<Integer> f1;
|
||||
public List<String> f2;
|
||||
public List<List<String>> f3;
|
||||
public List<SampleListNestedType> f4;
|
||||
|
||||
public SampleListTypes() {
|
||||
}
|
||||
}
|
||||
|
||||
@MessagePackMessage
|
||||
public static class SampleListNestedType {
|
||||
public byte[] f0;
|
||||
public String f1;
|
||||
|
||||
public SampleListNestedType() {
|
||||
}
|
||||
}
|
||||
|
||||
public static class SampleMapTypes {
|
||||
public Map<Integer, Integer> f0;
|
||||
public Map<Integer, Integer> f1;
|
||||
public Map<String, Integer> f2;
|
||||
|
||||
public SampleMapTypes() {
|
||||
}
|
||||
}
|
||||
|
||||
static void buildAndRegisterTemplate(Class<?> targetClass) {
|
||||
MessagePack.register(targetClass,
|
||||
TemplateBuilder.build(targetClass));
|
||||
}
|
||||
|
||||
static {
|
||||
buildAndRegisterTemplate(PrimitiveTypeFieldsClass.class);
|
||||
buildAndRegisterTemplate(GeneralReferenceTypeFieldsClass.class);
|
||||
buildAndRegisterTemplate(SampleListNestedType.class);
|
||||
buildAndRegisterTemplate(SampleListTypes.class);
|
||||
buildAndRegisterTemplate(SampleMapTypes.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrimitiveTypeFieldsClass00() throws Exception {
|
||||
PrimitiveTypeFieldsClass src = new PrimitiveTypeFieldsClass();
|
||||
src.f0 = (byte) 0;
|
||||
src.f1 = 1;
|
||||
src.f2 = 2;
|
||||
src.f3 = 3;
|
||||
src.f4 = 4;
|
||||
src.f5 = 5;
|
||||
src.f6 = false;
|
||||
|
||||
byte[] raw = MessagePack.pack(src);
|
||||
|
||||
PrimitiveTypeFieldsClass dstu =
|
||||
MessagePack.unpack(raw, PrimitiveTypeFieldsClass.class);
|
||||
assertEquals(src.f0, dstu.f0);
|
||||
assertEquals(src.f1, dstu.f1);
|
||||
assertEquals(src.f2, dstu.f2);
|
||||
assertEquals(src.f3, dstu.f3);
|
||||
assertEquals(src.f4, dstu.f4);
|
||||
assertEquals(src.f5, dstu.f5);
|
||||
assertEquals(src.f6, dstu.f6);
|
||||
|
||||
MessagePackObject o = MessagePack.unpack(raw);
|
||||
PrimitiveTypeFieldsClass dsto =
|
||||
o.convert(PrimitiveTypeFieldsClass.class);
|
||||
|
||||
assertEquals(src.f0, dsto.f0);
|
||||
assertEquals(src.f1, dsto.f1);
|
||||
assertEquals(src.f2, dsto.f2);
|
||||
assertEquals(src.f3, dsto.f3);
|
||||
assertEquals(src.f4, dsto.f4);
|
||||
assertEquals(src.f5, dsto.f5);
|
||||
assertEquals(src.f6, dsto.f6);
|
||||
}
|
||||
|
||||
public void testGeneralReferenceTypeFieldsClass() throws Exception {
|
||||
GeneralReferenceTypeFieldsClass src = new GeneralReferenceTypeFieldsClass();
|
||||
src.f0 = 0;
|
||||
src.f1 = 1;
|
||||
src.f2 = 2;
|
||||
src.f3 = (long) 3;
|
||||
src.f4 = (float) 4;
|
||||
src.f5 = (double) 5;
|
||||
src.f6 = false;
|
||||
src.f7 = new BigInteger("7");
|
||||
src.f8 = "8";
|
||||
src.f9 = new byte[] { 0x01, 0x02 };
|
||||
|
||||
byte[] raw = MessagePack.pack(src);
|
||||
|
||||
GeneralReferenceTypeFieldsClass dstu =
|
||||
MessagePack.unpack(raw, GeneralReferenceTypeFieldsClass.class);
|
||||
assertEquals(src.f0, dstu.f0);
|
||||
assertEquals(src.f1, dstu.f1);
|
||||
assertEquals(src.f2, dstu.f2);
|
||||
assertEquals(src.f3, dstu.f3);
|
||||
assertEquals(src.f4, dstu.f4);
|
||||
assertEquals(src.f5, dstu.f5);
|
||||
assertEquals(src.f6, dstu.f6);
|
||||
assertEquals(src.f7, dstu.f7);
|
||||
assertEquals(src.f8, dstu.f8);
|
||||
assertEquals(src.f9[0], dstu.f9[0]);
|
||||
assertEquals(src.f9[1], dstu.f9[1]);
|
||||
|
||||
MessagePackObject o = MessagePack.unpack(raw);
|
||||
GeneralReferenceTypeFieldsClass dsto =
|
||||
o.convert(GeneralReferenceTypeFieldsClass.class);
|
||||
assertEquals(src.f0, dsto.f0);
|
||||
assertEquals(src.f1, dsto.f1);
|
||||
assertEquals(src.f2, dsto.f2);
|
||||
assertEquals(src.f3, dsto.f3);
|
||||
assertEquals(src.f4, dsto.f4);
|
||||
assertEquals(src.f5, dsto.f5);
|
||||
assertEquals(src.f6, dsto.f6);
|
||||
assertEquals(src.f7, dsto.f7);
|
||||
assertEquals(src.f8, dsto.f8);
|
||||
assertEquals(src.f9[0], dsto.f9[0]);
|
||||
assertEquals(src.f9[1], dsto.f9[1]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListTypes() throws Exception {
|
||||
SampleListTypes src = new SampleListTypes();
|
||||
src.f0 = new ArrayList<Integer>();
|
||||
src.f1 = new ArrayList<Integer>();
|
||||
src.f1.add(1);
|
||||
src.f1.add(2);
|
||||
src.f1.add(3);
|
||||
src.f2 = new ArrayList<String>();
|
||||
src.f2.add("e1");
|
||||
src.f2.add("e2");
|
||||
src.f2.add("e3");
|
||||
src.f3 = new ArrayList<List<String>>();
|
||||
src.f3.add(src.f2);
|
||||
src.f4 = new ArrayList<SampleListNestedType>();
|
||||
SampleListNestedType slnt = new SampleListNestedType();
|
||||
slnt.f0 = new byte[] { 0x01, 0x02 };
|
||||
slnt.f1 = "muga";
|
||||
src.f4.add(slnt);
|
||||
|
||||
byte[] raw = MessagePack.pack(src);
|
||||
|
||||
SampleListTypes dstu =
|
||||
MessagePack.unpack(raw, SampleListTypes.class);
|
||||
assertEquals(src.f0.size(), dstu.f0.size());
|
||||
assertEquals(src.f1.size(), dstu.f1.size());
|
||||
for (int i = 0; i < src.f1.size(); ++i) {
|
||||
assertEquals(src.f1.get(i), dstu.f1.get(i));
|
||||
}
|
||||
assertEquals(src.f2.size(), dstu.f2.size());
|
||||
for (int i = 0; i < src.f2.size(); ++i) {
|
||||
assertEquals(src.f2.get(i), dstu.f2.get(i));
|
||||
}
|
||||
assertEquals(src.f3.size(), dstu.f3.size());
|
||||
for (int i = 0; i < src.f3.size(); ++i) {
|
||||
List<String> srclist = src.f3.get(i);
|
||||
List<String> dstlist = dstu.f3.get(i);
|
||||
assertEquals(srclist.size(), dstlist.size());
|
||||
for (int j = 0; j < srclist.size(); ++j) {
|
||||
assertEquals(srclist.get(j), dstlist.get(j));
|
||||
}
|
||||
}
|
||||
assertEquals(src.f4.size(), dstu.f4.size());
|
||||
for (int i = 0; i < src.f4.size(); ++i) {
|
||||
SampleListNestedType s = src.f4.get(i);
|
||||
SampleListNestedType d = dstu.f4.get(i);
|
||||
assertEquals(s.f0[0], d.f0[0]);
|
||||
assertEquals(s.f0[1], d.f0[1]);
|
||||
assertEquals(s.f1, d.f1);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load diff
|
|
@ -1,554 +0,0 @@
|
|||
package org.msgpack.util.codegen;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.math.BigInteger;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.msgpack.MessagePackObject;
|
||||
import org.msgpack.MessagePacker;
|
||||
import org.msgpack.Packer;
|
||||
import org.msgpack.Template;
|
||||
import org.msgpack.Unpacker;
|
||||
import org.msgpack.annotation.MessagePackMessage;
|
||||
import org.msgpack.packer.OptionalPacker;
|
||||
import org.msgpack.template.ListTemplate;
|
||||
import org.msgpack.template.MapTemplate;
|
||||
import org.msgpack.template.OptionalTemplate;
|
||||
import org.msgpack.template.NullableTemplate;
|
||||
import static org.msgpack.Templates.tBigInteger;
|
||||
import static org.msgpack.Templates.tBoolean;
|
||||
import static org.msgpack.Templates.tByte;
|
||||
import static org.msgpack.Templates.tByteArray;
|
||||
import static org.msgpack.Templates.tDouble;
|
||||
import static org.msgpack.Templates.tFloat;
|
||||
import static org.msgpack.Templates.tInteger;
|
||||
import static org.msgpack.Templates.tLong;
|
||||
import static org.msgpack.Templates.tShort;
|
||||
import static org.msgpack.Templates.tString;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class TestPackConvertWithFieldOption extends TestCase {
|
||||
|
||||
@Test
|
||||
public void testPrimitiveTypeFieldsClass00() throws Exception {
|
||||
PrimitiveTypeFieldsClass src = new PrimitiveTypeFieldsClass();
|
||||
src.f0 = (byte) 0;
|
||||
src.f1 = 1;
|
||||
src.f2 = 2;
|
||||
src.f3 = 3;
|
||||
src.f4 = 4;
|
||||
src.f5 = 5;
|
||||
src.f6 = false;
|
||||
|
||||
FieldList opts = new FieldList();
|
||||
opts.add("f0");
|
||||
opts.add("f1");
|
||||
opts.add("f2");
|
||||
opts.add("f3");
|
||||
opts.add("f4");
|
||||
opts.add("f5");
|
||||
opts.add("f6");
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker.create(
|
||||
PrimitiveTypeFieldsClass.class, opts);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate.create(PrimitiveTypeFieldsClass.class, opts);
|
||||
Unpacker pac = new Unpacker(in);
|
||||
Iterator<MessagePackObject> it = pac.iterator();
|
||||
assertTrue(it.hasNext());
|
||||
MessagePackObject mpo = it.next();
|
||||
PrimitiveTypeFieldsClass dst = (PrimitiveTypeFieldsClass) tmpl.convert(mpo, null);
|
||||
assertEquals(src.f0, dst.f0);
|
||||
assertEquals(src.f1, dst.f1);
|
||||
assertEquals(src.f2, dst.f2);
|
||||
assertEquals(src.f3, dst.f3);
|
||||
assertEquals(src.f4, dst.f4);
|
||||
assertEquals(src.f5, dst.f5);
|
||||
assertEquals(src.f6, dst.f6);
|
||||
assertFalse(it.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrimitiveTypeFieldsClass01() throws Exception {
|
||||
PrimitiveTypeFieldsClass src = new PrimitiveTypeFieldsClass();
|
||||
|
||||
FieldList opts = new FieldList();
|
||||
opts.add("f0", FieldOption.OPTIONAL);
|
||||
opts.add("f1", FieldOption.OPTIONAL);
|
||||
opts.add("f2", FieldOption.OPTIONAL);
|
||||
opts.add("f3", FieldOption.OPTIONAL);
|
||||
opts.add("f4", FieldOption.OPTIONAL);
|
||||
opts.add("f5", FieldOption.OPTIONAL);
|
||||
opts.add("f6", FieldOption.OPTIONAL);
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker.create(
|
||||
PrimitiveTypeFieldsClass.class, opts);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate.create(PrimitiveTypeFieldsClass.class, opts);
|
||||
Unpacker pac = new Unpacker(in);
|
||||
Iterator<MessagePackObject> it = pac.iterator();
|
||||
assertTrue(it.hasNext());
|
||||
MessagePackObject mpo = it.next();
|
||||
PrimitiveTypeFieldsClass dst = (PrimitiveTypeFieldsClass) tmpl.convert(mpo, null);
|
||||
assertEquals(src.f0, dst.f0);
|
||||
assertEquals(src.f1, dst.f1);
|
||||
assertEquals(src.f2, dst.f2);
|
||||
assertEquals(src.f3, dst.f3);
|
||||
assertEquals(src.f4, dst.f4);
|
||||
assertEquals(src.f5, dst.f5);
|
||||
assertEquals(src.f6, dst.f6);
|
||||
assertFalse(it.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrimitiveTypeFieldsClass02() throws Exception {
|
||||
PrimitiveTypeFieldsClass src = null;
|
||||
|
||||
FieldList opts = new FieldList();
|
||||
opts.add("f0");
|
||||
opts.add("f1");
|
||||
opts.add("f2");
|
||||
opts.add("f3");
|
||||
opts.add("f4");
|
||||
opts.add("f5");
|
||||
opts.add("f6");
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = new OptionalPacker(DynamicPacker.create(
|
||||
PrimitiveTypeFieldsClass.class, opts));
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = new NullableTemplate(DynamicTemplate.create(PrimitiveTypeFieldsClass.class, opts));
|
||||
Unpacker pac = new Unpacker(in);
|
||||
Iterator<MessagePackObject> it = pac.iterator();
|
||||
assertTrue(it.hasNext());
|
||||
MessagePackObject mpo = it.next();
|
||||
PrimitiveTypeFieldsClass dst = (PrimitiveTypeFieldsClass) tmpl.convert(mpo, null);
|
||||
assertEquals(src, dst);
|
||||
assertFalse(it.hasNext());
|
||||
}
|
||||
|
||||
public static class PrimitiveTypeFieldsClass {
|
||||
public byte f0;
|
||||
public short f1;
|
||||
public int f2;
|
||||
public long f3;
|
||||
public float f4;
|
||||
public double f5;
|
||||
public boolean f6;
|
||||
|
||||
public PrimitiveTypeFieldsClass() {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGeneralReferenceTypeFieldsClass00() throws Exception {
|
||||
GeneralReferenceTypeFieldsClass src = new GeneralReferenceTypeFieldsClass();
|
||||
src.f0 = 0;
|
||||
src.f1 = 1;
|
||||
src.f2 = 2;
|
||||
src.f3 = (long) 3;
|
||||
src.f4 = (float) 4;
|
||||
src.f5 = (double) 5;
|
||||
src.f6 = false;
|
||||
src.f7 = new BigInteger("7");
|
||||
src.f8 = "8";
|
||||
src.f9 = new byte[] { 0x01, 0x02 };
|
||||
|
||||
FieldList opts = new FieldList();
|
||||
opts.add("f0");
|
||||
opts.add("f1");
|
||||
opts.add("f2");
|
||||
opts.add("f3");
|
||||
opts.add("f4");
|
||||
opts.add("f5");
|
||||
opts.add("f6");
|
||||
opts.add("f7");
|
||||
opts.add("f8");
|
||||
opts.add("f9");
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker
|
||||
.create(GeneralReferenceTypeFieldsClass.class, opts);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate.create(GeneralReferenceTypeFieldsClass.class, opts);
|
||||
Unpacker pac = new Unpacker(in);
|
||||
Iterator<MessagePackObject> it = pac.iterator();
|
||||
assertTrue(it.hasNext());
|
||||
MessagePackObject mpo = it.next();
|
||||
GeneralReferenceTypeFieldsClass dst = (GeneralReferenceTypeFieldsClass) tmpl.convert(mpo, null);
|
||||
assertEquals(src.f0, dst.f0);
|
||||
assertEquals(src.f1, dst.f1);
|
||||
assertEquals(src.f2, dst.f2);
|
||||
assertEquals(src.f3, dst.f3);
|
||||
assertEquals(src.f4, dst.f4);
|
||||
assertEquals(src.f5, dst.f5);
|
||||
assertEquals(src.f6, dst.f6);
|
||||
assertEquals(src.f7, dst.f7);
|
||||
assertEquals(src.f8, dst.f8);
|
||||
assertEquals(src.f9[0], dst.f9[0]);
|
||||
assertEquals(src.f9[1], dst.f9[1]);
|
||||
assertFalse(it.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGeneralReferenceTypeFieldsClass01() throws Exception {
|
||||
GeneralReferenceTypeFieldsClass src = new GeneralReferenceTypeFieldsClass();
|
||||
src.f0 = null;
|
||||
src.f1 = null;
|
||||
src.f2 = null;
|
||||
src.f3 = null;
|
||||
src.f4 = null;
|
||||
src.f5 = null;
|
||||
src.f6 = null;
|
||||
src.f7 = null;
|
||||
src.f8 = null;
|
||||
src.f9 = null;
|
||||
|
||||
FieldList opts = new FieldList();
|
||||
opts.add("f0", FieldOption.OPTIONAL);
|
||||
opts.add("f1", FieldOption.OPTIONAL);
|
||||
opts.add("f2", FieldOption.OPTIONAL);
|
||||
opts.add("f3", FieldOption.OPTIONAL);
|
||||
opts.add("f4", FieldOption.OPTIONAL);
|
||||
opts.add("f5", FieldOption.OPTIONAL);
|
||||
opts.add("f6", FieldOption.OPTIONAL);
|
||||
opts.add("f7", FieldOption.OPTIONAL);
|
||||
opts.add("f8", FieldOption.OPTIONAL);
|
||||
opts.add("f9", FieldOption.OPTIONAL);
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker
|
||||
.create(GeneralReferenceTypeFieldsClass.class, opts);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate.create(GeneralReferenceTypeFieldsClass.class, opts);
|
||||
Unpacker pac = new Unpacker(in);
|
||||
Iterator<MessagePackObject> it = pac.iterator();
|
||||
assertTrue(it.hasNext());
|
||||
MessagePackObject mpo = it.next();
|
||||
GeneralReferenceTypeFieldsClass dst = (GeneralReferenceTypeFieldsClass) tmpl.convert(mpo, null);
|
||||
assertEquals(src.f0, dst.f0);
|
||||
assertEquals(src.f1, dst.f1);
|
||||
assertEquals(src.f2, dst.f2);
|
||||
assertEquals(src.f3, dst.f3);
|
||||
assertEquals(src.f4, dst.f4);
|
||||
assertEquals(src.f5, dst.f5);
|
||||
assertEquals(src.f6, dst.f6);
|
||||
assertEquals(src.f7, dst.f7);
|
||||
assertEquals(src.f8, dst.f8);
|
||||
assertEquals(src.f9, dst.f9);
|
||||
assertFalse(it.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGeneralReferenceTypeFieldsClass02()
|
||||
throws Exception {
|
||||
GeneralReferenceTypeFieldsClass src = null;
|
||||
|
||||
FieldList opts = new FieldList();
|
||||
opts.add("f0", FieldOption.OPTIONAL);
|
||||
opts.add("f1", FieldOption.OPTIONAL);
|
||||
opts.add("f2", FieldOption.OPTIONAL);
|
||||
opts.add("f3", FieldOption.OPTIONAL);
|
||||
opts.add("f4", FieldOption.OPTIONAL);
|
||||
opts.add("f5", FieldOption.OPTIONAL);
|
||||
opts.add("f6", FieldOption.OPTIONAL);
|
||||
opts.add("f7", FieldOption.OPTIONAL);
|
||||
opts.add("f8", FieldOption.OPTIONAL);
|
||||
opts.add("f9", FieldOption.OPTIONAL);
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = new OptionalPacker(DynamicPacker
|
||||
.create(GeneralReferenceTypeFieldsClass.class, opts));
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = new NullableTemplate(DynamicTemplate
|
||||
.create(GeneralReferenceTypeFieldsClass.class, opts));
|
||||
Unpacker pac = new Unpacker(in);
|
||||
Iterator<MessagePackObject> it = pac.iterator();
|
||||
assertTrue(it.hasNext());
|
||||
MessagePackObject mpo = it.next();
|
||||
GeneralReferenceTypeFieldsClass dst = (GeneralReferenceTypeFieldsClass) tmpl.convert(mpo, null);
|
||||
assertEquals(src, dst);
|
||||
assertFalse(it.hasNext());
|
||||
}
|
||||
|
||||
public static class GeneralReferenceTypeFieldsClass {
|
||||
public Byte f0;
|
||||
public Short f1;
|
||||
public Integer f2;
|
||||
public Long f3;
|
||||
public Float f4;
|
||||
public Double f5;
|
||||
public Boolean f6;
|
||||
public BigInteger f7;
|
||||
public String f8;
|
||||
public byte[] f9;
|
||||
|
||||
public GeneralReferenceTypeFieldsClass() {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListTypes00() throws Exception {
|
||||
SampleListTypes src = new SampleListTypes();
|
||||
src.f0 = new ArrayList<Integer>();
|
||||
src.f1 = new ArrayList<Integer>();
|
||||
src.f1.add(1);
|
||||
src.f1.add(2);
|
||||
src.f1.add(3);
|
||||
src.f2 = new ArrayList<String>();
|
||||
src.f2.add("e1");
|
||||
src.f2.add("e2");
|
||||
src.f2.add("e3");
|
||||
src.f3 = new ArrayList<List<String>>();
|
||||
src.f3.add(src.f2);
|
||||
src.f4 = new ArrayList<SampleListNestedType>();
|
||||
SampleListNestedType slnt = new SampleListNestedType();
|
||||
slnt.f0 = new byte[] { 0x01, 0x02 };
|
||||
slnt.f1 = "muga";
|
||||
src.f4.add(slnt);
|
||||
|
||||
FieldList opts = new FieldList();
|
||||
opts.add("f0");
|
||||
opts.add("f1");
|
||||
opts.add("f2");
|
||||
opts.add("f3");
|
||||
opts.add("f4");
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker
|
||||
.create(SampleListTypes.class, opts);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate.create(SampleListTypes.class, opts);
|
||||
Unpacker pac = new Unpacker(in);
|
||||
Iterator<MessagePackObject> it = pac.iterator();
|
||||
assertTrue(it.hasNext());
|
||||
MessagePackObject mpo = it.next();
|
||||
SampleListTypes dst = (SampleListTypes) tmpl.convert(mpo, null);
|
||||
assertEquals(src.f0.size(), dst.f0.size());
|
||||
assertEquals(src.f1.size(), dst.f1.size());
|
||||
for (int i = 0; i < src.f1.size(); ++i) {
|
||||
assertEquals(src.f1.get(i), dst.f1.get(i));
|
||||
}
|
||||
assertEquals(src.f2.size(), dst.f2.size());
|
||||
for (int i = 0; i < src.f2.size(); ++i) {
|
||||
assertEquals(src.f2.get(i), dst.f2.get(i));
|
||||
}
|
||||
assertEquals(src.f3.size(), dst.f3.size());
|
||||
for (int i = 0; i < src.f3.size(); ++i) {
|
||||
List<String> srclist = src.f3.get(i);
|
||||
List<String> dstlist = dst.f3.get(i);
|
||||
assertEquals(srclist.size(), dstlist.size());
|
||||
for (int j = 0; j < srclist.size(); ++j) {
|
||||
assertEquals(srclist.get(j), dstlist.get(j));
|
||||
}
|
||||
}
|
||||
assertEquals(src.f4.size(), dst.f4.size());
|
||||
for (int i = 0; i < src.f4.size(); ++i) {
|
||||
SampleListNestedType s = src.f4.get(i);
|
||||
SampleListNestedType d = dst.f4.get(i);
|
||||
assertEquals(s.f0[0], d.f0[0]);
|
||||
assertEquals(s.f0[1], d.f0[1]);
|
||||
assertEquals(s.f1, d.f1);
|
||||
}
|
||||
assertFalse(it.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListTypes01() throws Exception {
|
||||
SampleListTypes src = new SampleListTypes();
|
||||
src.f0 = new ArrayList<Integer>();
|
||||
src.f1 = null;
|
||||
src.f2 = new ArrayList<String>();
|
||||
src.f3 = new ArrayList<List<String>>();
|
||||
src.f4 = null;
|
||||
|
||||
FieldList opts = new FieldList();
|
||||
opts.add("f0", FieldOption.OPTIONAL);
|
||||
opts.add("f1", FieldOption.OPTIONAL);
|
||||
opts.add("f2", FieldOption.OPTIONAL);
|
||||
opts.add("f3", FieldOption.OPTIONAL);
|
||||
opts.add("f4", FieldOption.OPTIONAL);
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker
|
||||
.create(SampleListTypes.class, opts);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate.create(SampleListTypes.class, opts);
|
||||
Unpacker pac = new Unpacker(in);
|
||||
Iterator<MessagePackObject> it = pac.iterator();
|
||||
assertTrue(it.hasNext());
|
||||
MessagePackObject mpo = it.next();
|
||||
SampleListTypes dst = (SampleListTypes) tmpl.convert(mpo, null);
|
||||
assertEquals(src.f0.size(), dst.f0.size());
|
||||
assertEquals(src.f1, dst.f1);
|
||||
assertEquals(src.f2.size(), dst.f2.size());
|
||||
assertEquals(src.f3.size(), dst.f3.size());
|
||||
assertEquals(src.f4, dst.f4);
|
||||
assertFalse(it.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListTypes02() throws Exception {
|
||||
SampleListTypes src = null;
|
||||
|
||||
FieldList opts = new FieldList();
|
||||
opts.add("f0", FieldOption.OPTIONAL);
|
||||
opts.add("f1", FieldOption.OPTIONAL);
|
||||
opts.add("f2", FieldOption.OPTIONAL);
|
||||
opts.add("f3", FieldOption.OPTIONAL);
|
||||
opts.add("f4", FieldOption.OPTIONAL);
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = new OptionalPacker(DynamicPacker
|
||||
.create(SampleListTypes.class));
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = new NullableTemplate(DynamicTemplate
|
||||
.create(SampleListTypes.class, opts));
|
||||
Unpacker pac = new Unpacker(in);
|
||||
Iterator<MessagePackObject> it = pac.iterator();
|
||||
assertTrue(it.hasNext());
|
||||
MessagePackObject mpo = it.next();
|
||||
SampleListTypes dst = (SampleListTypes) tmpl.convert(mpo, null);
|
||||
assertEquals(src, dst);
|
||||
assertFalse(it.hasNext());
|
||||
}
|
||||
|
||||
public static class SampleListTypes {
|
||||
public List<Integer> f0;
|
||||
public List<Integer> f1;
|
||||
public List<String> f2;
|
||||
public List<List<String>> f3;
|
||||
public List<SampleListNestedType> f4;
|
||||
|
||||
public SampleListTypes() {
|
||||
}
|
||||
}
|
||||
|
||||
@MessagePackMessage
|
||||
public static class SampleListNestedType {
|
||||
public byte[] f0;
|
||||
public String f1;
|
||||
|
||||
public SampleListNestedType() {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapTypes00() throws Exception {
|
||||
SampleMapTypes src = new SampleMapTypes();
|
||||
src.f0 = new HashMap<Integer, Integer>();
|
||||
src.f1 = new HashMap<Integer, Integer>();
|
||||
src.f1.put(1, 1);
|
||||
src.f1.put(2, 2);
|
||||
src.f1.put(3, 3);
|
||||
src.f2 = new HashMap<String, Integer>();
|
||||
src.f2.put("k1", 1);
|
||||
src.f2.put("k2", 2);
|
||||
src.f2.put("k3", 3);
|
||||
|
||||
FieldList opts = new FieldList();
|
||||
opts.add("f0");
|
||||
opts.add("f1");
|
||||
opts.add("f2");
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker
|
||||
.create(SampleMapTypes.class, opts);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate.create(SampleMapTypes.class, opts);
|
||||
Unpacker pac = new Unpacker(in);
|
||||
Iterator<MessagePackObject> it = pac.iterator();
|
||||
assertTrue(it.hasNext());
|
||||
MessagePackObject mpo = it.next();
|
||||
SampleMapTypes dst = (SampleMapTypes) tmpl.convert(mpo, null);
|
||||
assertEquals(src.f0.size(), dst.f0.size());
|
||||
assertEquals(src.f1.size(), dst.f1.size());
|
||||
Iterator<Integer> srcf1 = src.f1.keySet().iterator();
|
||||
Iterator<Integer> dstf1 = dst.f1.keySet().iterator();
|
||||
while (srcf1.hasNext()) {
|
||||
Integer s1 = srcf1.next();
|
||||
Integer d1 = dstf1.next();
|
||||
assertEquals(s1, d1);
|
||||
assertEquals(src.f1.get(s1), dst.f1.get(d1));
|
||||
}
|
||||
assertEquals(src.f2.size(), dst.f2.size());
|
||||
Iterator<String> srcf2 = src.f2.keySet().iterator();
|
||||
Iterator<String> dstf2 = dst.f2.keySet().iterator();
|
||||
while (srcf2.hasNext()) {
|
||||
String s2 = srcf2.next();
|
||||
String d2 = dstf2.next();
|
||||
assertEquals(s2, d2);
|
||||
assertEquals(src.f2.get(s2), dst.f2.get(d2));
|
||||
}
|
||||
assertFalse(it.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapTypes01() throws Exception {
|
||||
SampleMapTypes src = new SampleMapTypes();
|
||||
src.f0 = new HashMap<Integer, Integer>();
|
||||
src.f1 = null;
|
||||
src.f2 = new HashMap<String, Integer>();
|
||||
|
||||
FieldList opts = new FieldList();
|
||||
opts.add("f0", FieldOption.OPTIONAL);
|
||||
opts.add("f1", FieldOption.OPTIONAL);
|
||||
opts.add("f2", FieldOption.OPTIONAL);
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker
|
||||
.create(SampleMapTypes.class, opts);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate.create(SampleMapTypes.class, opts);
|
||||
Unpacker pac = new Unpacker(in);
|
||||
Iterator<MessagePackObject> it = pac.iterator();
|
||||
assertTrue(it.hasNext());
|
||||
MessagePackObject mpo = it.next();
|
||||
SampleMapTypes dst = (SampleMapTypes) tmpl.convert(mpo, null);
|
||||
assertEquals(src.f0.size(), dst.f0.size());
|
||||
assertEquals(src.f1, dst.f1);
|
||||
assertEquals(src.f2.size(), dst.f2.size());
|
||||
assertFalse(it.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapTypes02() throws Exception {
|
||||
SampleMapTypes src = null;
|
||||
|
||||
FieldList opts = new FieldList();
|
||||
opts.add("f0", FieldOption.OPTIONAL);
|
||||
opts.add("f1", FieldOption.OPTIONAL);
|
||||
opts.add("f2", FieldOption.OPTIONAL);
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = new OptionalPacker(DynamicPacker
|
||||
.create(SampleMapTypes.class, opts));
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = new NullableTemplate(DynamicTemplate.create(SampleMapTypes.class, opts));
|
||||
Unpacker pac = new Unpacker(in);
|
||||
Iterator<MessagePackObject> it = pac.iterator();
|
||||
assertTrue(it.hasNext());
|
||||
MessagePackObject mpo = it.next();
|
||||
SampleMapTypes dst = (SampleMapTypes) tmpl.convert(mpo, null);
|
||||
assertEquals(src, dst);
|
||||
assertFalse(it.hasNext());
|
||||
}
|
||||
|
||||
public static class SampleMapTypes {
|
||||
public Map<Integer, Integer> f0;
|
||||
public Map<Integer, Integer> f1;
|
||||
public Map<String, Integer> f2;
|
||||
|
||||
public SampleMapTypes() {
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load diff
|
|
@ -1,510 +0,0 @@
|
|||
package org.msgpack.util.codegen;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.math.BigInteger;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.msgpack.MessagePacker;
|
||||
import org.msgpack.Packer;
|
||||
import org.msgpack.Template;
|
||||
import org.msgpack.Unpacker;
|
||||
import org.msgpack.annotation.MessagePackMessage;
|
||||
import org.msgpack.packer.OptionalPacker;
|
||||
import org.msgpack.template.ListTemplate;
|
||||
import org.msgpack.template.MapTemplate;
|
||||
import org.msgpack.template.OptionalTemplate;
|
||||
import org.msgpack.template.NullableTemplate;
|
||||
import static org.msgpack.Templates.tBigInteger;
|
||||
import static org.msgpack.Templates.tBoolean;
|
||||
import static org.msgpack.Templates.tByte;
|
||||
import static org.msgpack.Templates.tByteArray;
|
||||
import static org.msgpack.Templates.tDouble;
|
||||
import static org.msgpack.Templates.tFloat;
|
||||
import static org.msgpack.Templates.tInteger;
|
||||
import static org.msgpack.Templates.tLong;
|
||||
import static org.msgpack.Templates.tShort;
|
||||
import static org.msgpack.Templates.tString;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class TestPackUnpackWithFieldOption extends TestCase {
|
||||
|
||||
@Test
|
||||
public void testPrimitiveTypeFieldsClass00() throws Exception {
|
||||
PrimitiveTypeFieldsClass src = new PrimitiveTypeFieldsClass();
|
||||
src.f0 = (byte) 0;
|
||||
src.f1 = 1;
|
||||
src.f2 = 2;
|
||||
src.f3 = 3;
|
||||
src.f4 = 4;
|
||||
src.f5 = 5;
|
||||
src.f6 = false;
|
||||
|
||||
FieldList opts = new FieldList();
|
||||
opts.add("f0");
|
||||
opts.add("f1");
|
||||
opts.add("f2");
|
||||
opts.add("f3");
|
||||
opts.add("f4");
|
||||
opts.add("f5");
|
||||
opts.add("f6");
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker.create(
|
||||
PrimitiveTypeFieldsClass.class, opts);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate.create(PrimitiveTypeFieldsClass.class,
|
||||
opts);
|
||||
PrimitiveTypeFieldsClass dst = (PrimitiveTypeFieldsClass) tmpl
|
||||
.unpack(new Unpacker(in), null);
|
||||
assertEquals(src.f0, dst.f0);
|
||||
assertEquals(src.f1, dst.f1);
|
||||
assertEquals(src.f2, dst.f2);
|
||||
assertEquals(src.f3, dst.f3);
|
||||
assertEquals(src.f4, dst.f4);
|
||||
assertEquals(src.f5, dst.f5);
|
||||
assertEquals(src.f6, dst.f6);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrimitiveTypeFieldsClass01() throws Exception {
|
||||
PrimitiveTypeFieldsClass src = new PrimitiveTypeFieldsClass();
|
||||
|
||||
FieldList opts = new FieldList();
|
||||
opts.add("f0", FieldOption.OPTIONAL);
|
||||
opts.add("f1", FieldOption.OPTIONAL);
|
||||
opts.add("f2", FieldOption.OPTIONAL);
|
||||
opts.add("f3", FieldOption.OPTIONAL);
|
||||
opts.add("f4", FieldOption.OPTIONAL);
|
||||
opts.add("f5", FieldOption.OPTIONAL);
|
||||
opts.add("f6", FieldOption.OPTIONAL);
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker.create(
|
||||
PrimitiveTypeFieldsClass.class, opts);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate.create(PrimitiveTypeFieldsClass.class,
|
||||
opts);
|
||||
PrimitiveTypeFieldsClass dst = (PrimitiveTypeFieldsClass) tmpl
|
||||
.unpack(new Unpacker(in), null);
|
||||
assertEquals(src.f0, dst.f0);
|
||||
assertEquals(src.f1, dst.f1);
|
||||
assertEquals(src.f2, dst.f2);
|
||||
assertEquals(src.f3, dst.f3);
|
||||
assertEquals(src.f4, dst.f4);
|
||||
assertEquals(src.f5, dst.f5);
|
||||
assertEquals(src.f6, dst.f6);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrimitiveTypeFieldsClass02() throws Exception {
|
||||
PrimitiveTypeFieldsClass src = null;
|
||||
|
||||
FieldList opts = new FieldList();
|
||||
opts.add("f0");
|
||||
opts.add("f1");
|
||||
opts.add("f2");
|
||||
opts.add("f3");
|
||||
opts.add("f4");
|
||||
opts.add("f5");
|
||||
opts.add("f6");
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = new OptionalPacker(DynamicPacker.create(
|
||||
PrimitiveTypeFieldsClass.class, opts));
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = new NullableTemplate(DynamicTemplate.create(
|
||||
PrimitiveTypeFieldsClass.class, opts));
|
||||
PrimitiveTypeFieldsClass dst = (PrimitiveTypeFieldsClass) tmpl
|
||||
.unpack(new Unpacker(in), null);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
public static class PrimitiveTypeFieldsClass {
|
||||
public byte f0;
|
||||
public short f1;
|
||||
public int f2;
|
||||
public long f3;
|
||||
public float f4;
|
||||
public double f5;
|
||||
public boolean f6;
|
||||
|
||||
public PrimitiveTypeFieldsClass() {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGeneralReferenceTypeFieldsClass00() throws Exception {
|
||||
GeneralReferenceTypeFieldsClass src = new GeneralReferenceTypeFieldsClass();
|
||||
src.f0 = 0;
|
||||
src.f1 = 1;
|
||||
src.f2 = 2;
|
||||
src.f3 = (long) 3;
|
||||
src.f4 = (float) 4;
|
||||
src.f5 = (double) 5;
|
||||
src.f6 = false;
|
||||
src.f7 = new BigInteger("7");
|
||||
src.f8 = "8";
|
||||
src.f9 = new byte[] { 0x01, 0x02 };
|
||||
|
||||
FieldList opts = new FieldList();
|
||||
opts.add("f0");
|
||||
opts.add("f1");
|
||||
opts.add("f2");
|
||||
opts.add("f3");
|
||||
opts.add("f4");
|
||||
opts.add("f5");
|
||||
opts.add("f6");
|
||||
opts.add("f7");
|
||||
opts.add("f8");
|
||||
opts.add("f9");
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker
|
||||
.create(GeneralReferenceTypeFieldsClass.class, opts);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate
|
||||
.create(GeneralReferenceTypeFieldsClass.class, opts);
|
||||
GeneralReferenceTypeFieldsClass dst = (GeneralReferenceTypeFieldsClass) tmpl
|
||||
.unpack(new Unpacker(in), null);
|
||||
assertEquals(src.f0, dst.f0);
|
||||
assertEquals(src.f1, dst.f1);
|
||||
assertEquals(src.f2, dst.f2);
|
||||
assertEquals(src.f3, dst.f3);
|
||||
assertEquals(src.f4, dst.f4);
|
||||
assertEquals(src.f5, dst.f5);
|
||||
assertEquals(src.f6, dst.f6);
|
||||
assertEquals(src.f7, dst.f7);
|
||||
assertEquals(src.f8, dst.f8);
|
||||
assertEquals(src.f9[0], dst.f9[0]);
|
||||
assertEquals(src.f9[1], dst.f9[1]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGeneralReferenceTypeFieldsClass01() throws Exception {
|
||||
GeneralReferenceTypeFieldsClass src = new GeneralReferenceTypeFieldsClass();
|
||||
src.f0 = null;
|
||||
src.f1 = null;
|
||||
src.f2 = null;
|
||||
src.f3 = null;
|
||||
src.f4 = null;
|
||||
src.f5 = null;
|
||||
src.f6 = null;
|
||||
src.f7 = null;
|
||||
src.f8 = null;
|
||||
src.f9 = null;
|
||||
|
||||
FieldList opts = new FieldList();
|
||||
opts.add("f0", FieldOption.OPTIONAL);
|
||||
opts.add("f1", FieldOption.OPTIONAL);
|
||||
opts.add("f2", FieldOption.OPTIONAL);
|
||||
opts.add("f3", FieldOption.OPTIONAL);
|
||||
opts.add("f4", FieldOption.OPTIONAL);
|
||||
opts.add("f5", FieldOption.OPTIONAL);
|
||||
opts.add("f6", FieldOption.OPTIONAL);
|
||||
opts.add("f7", FieldOption.OPTIONAL);
|
||||
opts.add("f8", FieldOption.OPTIONAL);
|
||||
opts.add("f9", FieldOption.OPTIONAL);
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker
|
||||
.create(GeneralReferenceTypeFieldsClass.class, opts);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate
|
||||
.create(GeneralReferenceTypeFieldsClass.class, opts);
|
||||
GeneralReferenceTypeFieldsClass dst = (GeneralReferenceTypeFieldsClass) tmpl
|
||||
.unpack(new Unpacker(in), null);
|
||||
assertEquals(src.f0, dst.f0);
|
||||
assertEquals(src.f1, dst.f1);
|
||||
assertEquals(src.f2, dst.f2);
|
||||
assertEquals(src.f3, dst.f3);
|
||||
assertEquals(src.f4, dst.f4);
|
||||
assertEquals(src.f5, dst.f5);
|
||||
assertEquals(src.f6, dst.f6);
|
||||
assertEquals(src.f7, dst.f7);
|
||||
assertEquals(src.f8, dst.f8);
|
||||
assertEquals(src.f9, dst.f9);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGeneralReferenceTypeFieldsClass02()
|
||||
throws Exception {
|
||||
GeneralReferenceTypeFieldsClass src = null;
|
||||
|
||||
FieldList opts = new FieldList();
|
||||
opts.add("f0", FieldOption.OPTIONAL);
|
||||
opts.add("f1", FieldOption.OPTIONAL);
|
||||
opts.add("f2", FieldOption.OPTIONAL);
|
||||
opts.add("f3", FieldOption.OPTIONAL);
|
||||
opts.add("f4", FieldOption.OPTIONAL);
|
||||
opts.add("f5", FieldOption.OPTIONAL);
|
||||
opts.add("f6", FieldOption.OPTIONAL);
|
||||
opts.add("f7", FieldOption.OPTIONAL);
|
||||
opts.add("f8", FieldOption.OPTIONAL);
|
||||
opts.add("f9", FieldOption.OPTIONAL);
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = new OptionalPacker(DynamicPacker
|
||||
.create(GeneralReferenceTypeFieldsClass.class, opts));
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = new NullableTemplate(DynamicTemplate
|
||||
.create(GeneralReferenceTypeFieldsClass.class, opts));
|
||||
GeneralReferenceTypeFieldsClass dst = (GeneralReferenceTypeFieldsClass) tmpl
|
||||
.unpack(new Unpacker(in), null);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
public static class GeneralReferenceTypeFieldsClass {
|
||||
public Byte f0;
|
||||
public Short f1;
|
||||
public Integer f2;
|
||||
public Long f3;
|
||||
public Float f4;
|
||||
public Double f5;
|
||||
public Boolean f6;
|
||||
public BigInteger f7;
|
||||
public String f8;
|
||||
public byte[] f9;
|
||||
|
||||
public GeneralReferenceTypeFieldsClass() {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListTypes00() throws Exception {
|
||||
SampleListTypes src = new SampleListTypes();
|
||||
src.f0 = new ArrayList<Integer>();
|
||||
src.f1 = new ArrayList<Integer>();
|
||||
src.f1.add(1);
|
||||
src.f1.add(2);
|
||||
src.f1.add(3);
|
||||
src.f2 = new ArrayList<String>();
|
||||
src.f2.add("e1");
|
||||
src.f2.add("e2");
|
||||
src.f2.add("e3");
|
||||
src.f3 = new ArrayList<List<String>>();
|
||||
src.f3.add(src.f2);
|
||||
src.f4 = new ArrayList<SampleListNestedType>();
|
||||
SampleListNestedType slnt = new SampleListNestedType();
|
||||
slnt.f0 = new byte[] { 0x01, 0x02 };
|
||||
slnt.f1 = "muga";
|
||||
src.f4.add(slnt);
|
||||
|
||||
FieldList opts = new FieldList();
|
||||
opts.add("f0", FieldOption.OPTIONAL);
|
||||
opts.add("f1", FieldOption.OPTIONAL);
|
||||
opts.add("f2", FieldOption.OPTIONAL);
|
||||
opts.add("f3", FieldOption.OPTIONAL);
|
||||
opts.add("f4", FieldOption.OPTIONAL);
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker
|
||||
.create(SampleListTypes.class, opts);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate.create(SampleListTypes.class, opts);
|
||||
SampleListTypes dst = (SampleListTypes) tmpl
|
||||
.unpack(new Unpacker(in), null);
|
||||
assertEquals(src.f0.size(), dst.f0.size());
|
||||
assertEquals(src.f1.size(), dst.f1.size());
|
||||
for (int i = 0; i < src.f1.size(); ++i) {
|
||||
assertEquals(src.f1.get(i), dst.f1.get(i));
|
||||
}
|
||||
assertEquals(src.f2.size(), dst.f2.size());
|
||||
for (int i = 0; i < src.f2.size(); ++i) {
|
||||
assertEquals(src.f2.get(i), dst.f2.get(i));
|
||||
}
|
||||
assertEquals(src.f3.size(), dst.f3.size());
|
||||
for (int i = 0; i < src.f3.size(); ++i) {
|
||||
List<String> srclist = src.f3.get(i);
|
||||
List<String> dstlist = dst.f3.get(i);
|
||||
assertEquals(srclist.size(), dstlist.size());
|
||||
for (int j = 0; j < srclist.size(); ++j) {
|
||||
assertEquals(srclist.get(j), dstlist.get(j));
|
||||
}
|
||||
}
|
||||
assertEquals(src.f4.size(), dst.f4.size());
|
||||
for (int i = 0; i < src.f4.size(); ++i) {
|
||||
SampleListNestedType s = src.f4.get(i);
|
||||
SampleListNestedType d = dst.f4.get(i);
|
||||
assertEquals(s.f0[0], d.f0[0]);
|
||||
assertEquals(s.f0[1], d.f0[1]);
|
||||
assertEquals(s.f1, d.f1);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListTypes01() throws Exception {
|
||||
SampleListTypes src = new SampleListTypes();
|
||||
src.f0 = new ArrayList<Integer>();
|
||||
src.f1 = null;
|
||||
src.f2 = new ArrayList<String>();
|
||||
src.f3 = new ArrayList<List<String>>();
|
||||
src.f4 = null;
|
||||
|
||||
FieldList opts = new FieldList();
|
||||
opts.add("f0", FieldOption.OPTIONAL);
|
||||
opts.add("f1", FieldOption.OPTIONAL);
|
||||
opts.add("f2", FieldOption.OPTIONAL);
|
||||
opts.add("f3", FieldOption.OPTIONAL);
|
||||
opts.add("f4", FieldOption.OPTIONAL);
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker
|
||||
.create(SampleListTypes.class, opts);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate.create(SampleListTypes.class, opts);
|
||||
SampleListTypes dst = (SampleListTypes) tmpl
|
||||
.unpack(new Unpacker(in), null);
|
||||
assertEquals(src.f0.size(), dst.f0.size());
|
||||
assertEquals(src.f1, dst.f1);
|
||||
assertEquals(src.f2.size(), dst.f2.size());
|
||||
assertEquals(src.f3.size(), dst.f3.size());
|
||||
assertEquals(src.f4, dst.f4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListTypes02() throws Exception {
|
||||
SampleListTypes src = null;
|
||||
|
||||
FieldList opts = new FieldList();
|
||||
opts.add("f0", FieldOption.OPTIONAL);
|
||||
opts.add("f1", FieldOption.OPTIONAL);
|
||||
opts.add("f2", FieldOption.OPTIONAL);
|
||||
opts.add("f3", FieldOption.OPTIONAL);
|
||||
opts.add("f4", FieldOption.OPTIONAL);
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = new OptionalPacker(DynamicPacker
|
||||
.create(SampleListTypes.class));
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = new NullableTemplate(DynamicTemplate
|
||||
.create(SampleListTypes.class));
|
||||
SampleListTypes dst = (SampleListTypes) tmpl.unpack(new Unpacker(in), null);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
public static class SampleListTypes {
|
||||
public List<Integer> f0;
|
||||
public List<Integer> f1;
|
||||
public List<String> f2;
|
||||
public List<List<String>> f3;
|
||||
public List<SampleListNestedType> f4;
|
||||
|
||||
public SampleListTypes() {
|
||||
}
|
||||
}
|
||||
|
||||
@MessagePackMessage
|
||||
public static class SampleListNestedType {
|
||||
public byte[] f0;
|
||||
public String f1;
|
||||
|
||||
public SampleListNestedType() {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapTypes00() throws Exception {
|
||||
SampleMapTypes src = new SampleMapTypes();
|
||||
src.f0 = new HashMap<Integer, Integer>();
|
||||
src.f1 = new HashMap<Integer, Integer>();
|
||||
src.f1.put(1, 1);
|
||||
src.f1.put(2, 2);
|
||||
src.f1.put(3, 3);
|
||||
src.f2 = new HashMap<String, Integer>();
|
||||
src.f2.put("k1", 1);
|
||||
src.f2.put("k2", 2);
|
||||
src.f2.put("k3", 3);
|
||||
|
||||
FieldList opts = new FieldList();
|
||||
opts.add("f0");
|
||||
opts.add("f1");
|
||||
opts.add("f2");
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker
|
||||
.create(SampleMapTypes.class, opts);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate.create(SampleMapTypes.class, opts);
|
||||
SampleMapTypes dst = (SampleMapTypes) tmpl
|
||||
.unpack(new Unpacker(in), null);
|
||||
assertEquals(src.f0.size(), dst.f0.size());
|
||||
assertEquals(src.f1.size(), dst.f1.size());
|
||||
Iterator<Integer> srcf1 = src.f1.keySet().iterator();
|
||||
Iterator<Integer> dstf1 = dst.f1.keySet().iterator();
|
||||
while (srcf1.hasNext()) {
|
||||
Integer s1 = srcf1.next();
|
||||
Integer d1 = dstf1.next();
|
||||
assertEquals(s1, d1);
|
||||
assertEquals(src.f1.get(s1), dst.f1.get(d1));
|
||||
}
|
||||
assertEquals(src.f2.size(), dst.f2.size());
|
||||
Iterator<String> srcf2 = src.f2.keySet().iterator();
|
||||
Iterator<String> dstf2 = dst.f2.keySet().iterator();
|
||||
while (srcf2.hasNext()) {
|
||||
String s2 = srcf2.next();
|
||||
String d2 = dstf2.next();
|
||||
assertEquals(s2, d2);
|
||||
assertEquals(src.f2.get(s2), dst.f2.get(d2));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapTypes01() throws Exception {
|
||||
SampleMapTypes src = new SampleMapTypes();
|
||||
src.f0 = new HashMap<Integer, Integer>();
|
||||
src.f1 = null;
|
||||
src.f2 = new HashMap<String, Integer>();
|
||||
|
||||
FieldList opts = new FieldList();
|
||||
opts.add("f0", FieldOption.OPTIONAL);
|
||||
opts.add("f1", FieldOption.OPTIONAL);
|
||||
opts.add("f2", FieldOption.OPTIONAL);
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = DynamicPacker
|
||||
.create(SampleMapTypes.class, opts);
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = DynamicTemplate.create(SampleMapTypes.class, opts);
|
||||
SampleMapTypes dst = (SampleMapTypes) tmpl
|
||||
.unpack(new Unpacker(in), null);
|
||||
assertEquals(src.f0.size(), dst.f0.size());
|
||||
assertEquals(src.f1, dst.f1);
|
||||
assertEquals(src.f2.size(), dst.f2.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapTypes02() throws Exception {
|
||||
SampleMapTypes src = null;
|
||||
|
||||
FieldList opts = new FieldList();
|
||||
opts.add("f0", FieldOption.OPTIONAL);
|
||||
opts.add("f1", FieldOption.OPTIONAL);
|
||||
opts.add("f2", FieldOption.OPTIONAL);
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
MessagePacker packer = new OptionalPacker(DynamicPacker
|
||||
.create(SampleMapTypes.class, opts));
|
||||
packer.pack(new Packer(out), src);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||
Template tmpl = new NullableTemplate(DynamicTemplate
|
||||
.create(SampleMapTypes.class, opts));
|
||||
SampleMapTypes dst = (SampleMapTypes) tmpl
|
||||
.unpack(new Unpacker(in), null);
|
||||
assertEquals(src, dst);
|
||||
}
|
||||
|
||||
public static class SampleMapTypes {
|
||||
public Map<Integer, Integer> f0;
|
||||
public Map<Integer, Integer> f1;
|
||||
public Map<String, Integer> f2;
|
||||
|
||||
public SampleMapTypes() {
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue