java: write test programs for OptionalTemplate.java

This commit is contained in:
Muga Nishizawa 2010-10-22 23:23:53 +09:00
parent 71ae75a5bf
commit 69e32d264c
19 changed files with 2925 additions and 623 deletions

View file

@ -9,13 +9,111 @@ 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.OptionalTemplate;
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);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
obj = Util.unpackOne(out.toByteArray());
tmpl = new OptionalTemplate(ByteTemplate.getInstance());
dst = (Byte) tmpl.convert(obj);
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);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
obj = Util.unpackOne(out.toByteArray());
tmpl = new OptionalTemplate(ShortTemplate.getInstance());
dst = (Short) tmpl.convert(obj);
assertEquals(src, dst);
}
@Test
public void testInteger() throws Exception {
// _testInteger(null); // FIXME
_testInteger(0);
_testInteger(-1);
_testInteger(1);
@ -35,9 +133,30 @@ public class TestPackConvert extends TestCase {
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);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
obj = Util.unpackOne(out.toByteArray());
tmpl = new OptionalTemplate(IntegerTemplate.getInstance());
dst = (Integer) tmpl.convert(obj);
assertEquals(src, dst);
}
@Test
public void testLong() throws Exception {
// _testLong((null); // FIXME
_testLong((long) 0);
_testLong((long) -1);
_testLong((long) 1);
@ -59,9 +178,30 @@ public class TestPackConvert extends TestCase {
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);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
obj = Util.unpackOne(out.toByteArray());
tmpl = new OptionalTemplate(LongTemplate.getInstance());
dst = (Long) tmpl.convert(obj);
assertEquals(src, dst);
}
@Test
public void testBigInteger() throws Exception {
// _testBigInteger(null); // FIXME
_testBigInteger(BigInteger.valueOf(0));
_testBigInteger(BigInteger.valueOf(-1));
_testBigInteger(BigInteger.valueOf(1));
@ -86,9 +226,31 @@ public class TestPackConvert extends TestCase {
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);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
obj = Util.unpackOne(out.toByteArray());
tmpl = new OptionalTemplate(BigIntegerTemplate.getInstance());
dst = (BigInteger) tmpl.convert(obj);
assertEquals(src, dst);
}
@Test
public void testFloat() throws Exception {
// _testFloat(null); // FIXME
_testFloat((float) 0.0);
_testFloat((float) -0.0);
_testFloat((float) 1.0);
@ -112,9 +274,30 @@ public class TestPackConvert extends TestCase {
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);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
obj = Util.unpackOne(out.toByteArray());
tmpl = new OptionalTemplate(FloatTemplate.getInstance());
dst = (Float) tmpl.convert(obj);
assertEquals(src, dst);
}
@Test
public void testDouble() throws Exception {
// _testDouble(null); // FIXME
_testDouble((double) 0.0);
_testDouble((double) -0.0);
_testDouble((double) 1.0);
@ -137,9 +320,30 @@ public class TestPackConvert extends TestCase {
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);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
obj = Util.unpackOne(out.toByteArray());
tmpl = new OptionalTemplate(DoubleTemplate.getInstance());
dst = (Double) tmpl.convert(obj);
assertEquals(src, dst);
}
@Test
public void testBoolean() throws Exception {
// _testBoolean(null); // FIXME
_testBoolean(false);
_testBoolean(true);
}
@ -152,9 +356,30 @@ public class TestPackConvert extends TestCase {
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);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
obj = Util.unpackOne(out.toByteArray());
tmpl = new OptionalTemplate(BooleanTemplate.getInstance());
dst = (Boolean) tmpl.convert(obj);
assertEquals(src, dst);
}
@Test
public void testString() throws Exception {
// _testString(null); // FIXME
_testString("");
_testString("a");
_testString("ab");
@ -198,4 +423,26 @@ public class TestPackConvert extends TestCase {
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);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
obj = Util.unpackOne(out.toByteArray());
tmpl = new OptionalTemplate(StringTemplate.getInstance());
dst = (String) tmpl.convert(obj);
assertEquals(src, dst);
}
}

View file

@ -9,13 +9,117 @@ 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.OptionalTemplate;
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);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
unpacker.wrap(bytes);
tmpl = new OptionalTemplate(ByteTemplate.getInstance());
dst = (Byte) tmpl.unpack(unpacker);
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);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
unpacker.wrap(bytes);
tmpl = new OptionalTemplate(ShortTemplate.getInstance());
dst = (Short) tmpl.unpack(unpacker);
assertEquals(src, dst);
}
@Test
public void testInteger() throws Exception {
// _testInteger(null); // FIXME
_testInteger(0);
_testInteger(-1);
_testInteger(1);
@ -36,9 +140,32 @@ public class TestPackUnpack extends TestCase {
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);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
unpacker.wrap(bytes);
tmpl = new OptionalTemplate(IntegerTemplate.getInstance());
dst = (Integer) tmpl.unpack(unpacker);
assertEquals(src, dst);
}
@Test
public void testLong() throws Exception {
// _testLong((null); // FIXME
_testLong((long) 0);
_testLong((long) -1);
_testLong((long) 1);
@ -61,9 +188,32 @@ public class TestPackUnpack extends TestCase {
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);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
unpacker.wrap(bytes);
tmpl = new OptionalTemplate(LongTemplate.getInstance());
dst = (Long) tmpl.unpack(unpacker);
assertEquals(src, dst);
}
@Test
public void testBigInteger() throws Exception {
// _testBigInteger(null); // FIXME
_testBigInteger(BigInteger.valueOf(0));
_testBigInteger(BigInteger.valueOf(-1));
_testBigInteger(BigInteger.valueOf(1));
@ -89,9 +239,33 @@ public class TestPackUnpack extends TestCase {
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);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
unpacker.wrap(bytes);
tmpl = new OptionalTemplate(BigIntegerTemplate.getInstance());
dst = (BigInteger) tmpl.unpack(unpacker);
assertEquals(src, dst);
}
@Test
public void testFloat() throws Exception {
// _testFloat(null); // FIXME
_testFloat((float) 0.0);
_testFloat((float) -0.0);
_testFloat((float) 1.0);
@ -116,9 +290,32 @@ public class TestPackUnpack extends TestCase {
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);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
unpacker.wrap(bytes);
tmpl = new OptionalTemplate(FloatTemplate.getInstance());
dst = (Float) tmpl.unpack(unpacker);
assertEquals(src, dst);
}
@Test
public void testDouble() throws Exception {
// _testDouble(null); // FIXME
_testDouble((double) 0.0);
_testDouble((double) -0.0);
_testDouble((double) 1.0);
@ -142,9 +339,32 @@ public class TestPackUnpack extends TestCase {
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);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
unpacker.wrap(bytes);
tmpl = new OptionalTemplate(DoubleTemplate.getInstance());
dst = (Double) tmpl.unpack(unpacker);
assertEquals(src, dst);
}
@Test
public void testBoolean() throws Exception {
// _testBoolean(null); // FIXME
_testBoolean(false);
_testBoolean(true);
}
@ -158,9 +378,32 @@ public class TestPackUnpack extends TestCase {
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);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
unpacker.wrap(bytes);
tmpl = new OptionalTemplate(BooleanTemplate.getInstance());
dst = (Boolean) tmpl.unpack(unpacker);
assertEquals(src, dst);
}
@Test
public void testString() throws Exception {
// _testString(null); // FIXME
_testString("");
_testString("a");
_testString("ab");
@ -205,4 +448,28 @@ public class TestPackUnpack extends TestCase {
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);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
unpacker.wrap(bytes);
tmpl = new OptionalTemplate(StringTemplate.getInstance());
dst = (String) tmpl.unpack(unpacker);
assertEquals(src, dst);
}
}

View file

@ -10,6 +10,7 @@ import java.util.Random;
import org.junit.Test;
import org.msgpack.MessagePackObject;
import org.msgpack.MessageTypeException;
import org.msgpack.Packer;
import org.msgpack.Template;
import org.msgpack.Util;
@ -20,7 +21,6 @@ public class TestPackConvert extends TestCase {
@Test
public void testInteger() throws Exception {
// _testInteger(null); // FIXME
_testInteger(0);
_testInteger(-1);
_testInteger(1);
@ -41,9 +41,28 @@ public class TestPackConvert extends TestCase {
assertEquals(src, dst);
}
@Test
public void testNullInteger() throws Exception {
Integer src = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(src);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
Template tmpl = IntegerTemplate.getInstance();
Integer dst = null;
try {
dst = (Integer) tmpl.convert(obj);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
obj = Util.unpackOne(out.toByteArray());
tmpl = new OptionalTemplate(IntegerTemplate.getInstance());
dst = (Integer) tmpl.convert(obj);
assertEquals(src, dst);
}
@Test
public void testLong() throws Exception {
// _testLong(null); // FIXME
_testLong((long) 0);
_testLong((long) -1);
_testLong((long) 1);
@ -66,9 +85,28 @@ public class TestPackConvert extends TestCase {
assertEquals(src, dst);
}
@Test
public void testNullLong() throws Exception {
Long src = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(src);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
Template tmpl = LongTemplate.getInstance();
Long dst = null;
try {
dst = (Long) tmpl.convert(obj);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
obj = Util.unpackOne(out.toByteArray());
tmpl = new OptionalTemplate(LongTemplate.getInstance());
dst = (Long) tmpl.convert(obj);
assertEquals(src, dst);
}
@Test
public void testBiginteger() throws Exception {
// _testBigInteger(null); // FIXME
_testBigInteger(BigInteger.valueOf(0));
_testBigInteger(BigInteger.valueOf(-1));
_testBigInteger(BigInteger.valueOf(1));
@ -94,9 +132,28 @@ public class TestPackConvert extends TestCase {
assertEquals(src, dst);
}
@Test
public void testNullBigInteger() throws Exception {
Long src = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(src);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
Template tmpl = BigIntegerTemplate.getInstance();
BigInteger dst = null;
try {
dst = (BigInteger) tmpl.convert(obj);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
obj = Util.unpackOne(out.toByteArray());
tmpl = new OptionalTemplate(BigIntegerTemplate.getInstance());
dst = (BigInteger) tmpl.convert(obj);
assertEquals(src, dst);
}
@Test
public void testFloat() throws Exception {
// _testFloat(null); // FIXME
_testFloat((float) 0.0);
_testFloat((float) -0.0);
_testFloat((float) 1.0);
@ -121,9 +178,28 @@ public class TestPackConvert extends TestCase {
assertEquals(src, dst, 10e-10);
}
@Test
public void testNullFloat() throws Exception {
Long src = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(src);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
Template tmpl = FloatTemplate.getInstance();
Float dst = null;
try {
dst = (Float) tmpl.convert(obj);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
obj = Util.unpackOne(out.toByteArray());
tmpl = new OptionalTemplate(FloatTemplate.getInstance());
dst = (Float) tmpl.convert(obj);
assertEquals(src, dst);
}
@Test
public void testDouble() throws Exception {
// _testDouble(null); // FIXME
_testDouble((double) 0.0);
_testDouble((double) -0.0);
_testDouble((double) 1.0);
@ -148,9 +224,28 @@ public class TestPackConvert extends TestCase {
assertEquals(src, dst, 10e-10);
}
@Test
public void testNullDouble() throws Exception {
Long src = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(src);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
Template tmpl = DoubleTemplate.getInstance();
Double dst = null;
try {
dst = (Double) tmpl.convert(obj);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
obj = Util.unpackOne(out.toByteArray());
tmpl = new OptionalTemplate(DoubleTemplate.getInstance());
dst = (Double) tmpl.convert(obj);
assertEquals(src, dst);
}
@Test
public void testBoolean() throws Exception {
// _testBoolean(null); // FIXME
_testBoolean(false);
_testBoolean(true);
}
@ -164,9 +259,28 @@ public class TestPackConvert extends TestCase {
assertEquals(src, dst);
}
@Test
public void testNullBoolean() throws Exception {
Long src = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(src);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
Template tmpl = BooleanTemplate.getInstance();
Boolean dst = null;
try {
dst = (Boolean) tmpl.convert(obj);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
obj = Util.unpackOne(out.toByteArray());
tmpl = new OptionalTemplate(BooleanTemplate.getInstance());
dst = (Boolean) tmpl.convert(obj);
assertEquals(src, dst);
}
@Test
public void testString() throws Exception {
// _testString(null); // FIXME
_testString("");
_testString("a");
_testString("ab");
@ -212,10 +326,29 @@ public class TestPackConvert extends TestCase {
assertEquals(src, dst);
}
@Test
public void testNullString() throws Exception {
Long src = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(src);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
Template tmpl = StringTemplate.getInstance();
String dst = null;
try {
dst = (String) tmpl.convert(obj);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
obj = Util.unpackOne(out.toByteArray());
tmpl = new OptionalTemplate(StringTemplate.getInstance());
dst = (String) tmpl.convert(obj);
assertEquals(src, dst);
}
@SuppressWarnings("unchecked")
@Test
public void testList() throws Exception {
// nullList // FIXME
List<Integer> emptyList = new ArrayList<Integer>();
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
@ -261,10 +394,31 @@ public class TestPackConvert extends TestCase {
}
}
@SuppressWarnings("unchecked")
@Test
public void testNullList() throws Exception {
List<String> src = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(src);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
Template tmpl = new ListTemplate(StringTemplate.getInstance());
List<String> dst = null;
try {
dst = (List<String>) tmpl.convert(obj);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
obj = Util.unpackOne(out.toByteArray());
tmpl = new OptionalTemplate(new ListTemplate(StringTemplate
.getInstance()));
dst = (List<String>) tmpl.convert(obj);
assertEquals(src, dst);
}
@SuppressWarnings("unchecked")
@Test
public void testMap() throws Exception {
// nullMap // FIXME
Map<Integer, Integer> emptyMap = new HashMap<Integer, Integer>();
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
@ -317,4 +471,27 @@ public class TestPackConvert extends TestCase {
}
}
}
@SuppressWarnings("unchecked")
@Test
public void testNullMap() throws Exception {
Map<String, String> src = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(src);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
Template tmpl = new MapTemplate(StringTemplate.getInstance(),
StringTemplate.getInstance());
Map<String, String> dst = null;
try {
dst = (Map<String, String>) tmpl.convert(obj);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
obj = Util.unpackOne(out.toByteArray());
tmpl = new OptionalTemplate(new MapTemplate(StringTemplate
.getInstance(), StringTemplate.getInstance()));
dst = (Map<String, String>) tmpl.convert(obj);
assertEquals(src, dst);
}
}

View file

@ -10,6 +10,7 @@ import java.util.Map;
import java.util.Random;
import org.junit.Test;
import org.msgpack.MessageTypeException;
import org.msgpack.Packer;
import org.msgpack.Template;
import org.msgpack.Unpacker;
@ -19,7 +20,6 @@ import junit.framework.TestCase;
public class TestPackUnpack extends TestCase {
@Test
public void testInteger() throws Exception {
// _testInteger(null); // FIXME
_testInteger(0);
_testInteger(-1);
_testInteger(1);
@ -40,9 +40,31 @@ public class TestPackUnpack extends TestCase {
assertEquals(src, dst);
}
@Test
public void testNullInteger() throws Exception {
Integer src = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(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);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
unpacker.wrap(bytes);
tmpl = new OptionalTemplate(IntegerTemplate.getInstance());
dst = (Integer) tmpl.unpack(unpacker);
assertEquals(src, dst);
}
@Test
public void testLong() throws Exception {
// _testLong(null); // FIXME
_testLong((long) 0);
_testLong((long) -1);
_testLong((long) 1);
@ -65,9 +87,31 @@ public class TestPackUnpack extends TestCase {
assertEquals(src, dst);
}
@Test
public void testNullLong() throws Exception {
Long src = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(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);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
unpacker.wrap(bytes);
tmpl = new OptionalTemplate(LongTemplate.getInstance());
dst = (Long) tmpl.unpack(unpacker);
assertEquals(src, dst);
}
@Test
public void testBigInteger() throws Exception {
// _testBigInteger(null); // FIXME
_testBigInteger(BigInteger.valueOf(0));
_testBigInteger(BigInteger.valueOf(-1));
_testBigInteger(BigInteger.valueOf(1));
@ -93,9 +137,31 @@ public class TestPackUnpack extends TestCase {
assertEquals(src, dst);
}
@Test
public void testNullBigInteger() throws Exception {
BigInteger src = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(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);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
unpacker.wrap(bytes);
tmpl = new OptionalTemplate(BigIntegerTemplate.getInstance());
dst = (BigInteger) tmpl.unpack(unpacker);
assertEquals(src, dst);
}
@Test
public void testFloat() throws Exception {
// _testFloat(null); // FIXME
_testFloat((float) 0.0);
_testFloat((float) -0.0);
_testFloat((float) 1.0);
@ -122,7 +188,6 @@ public class TestPackUnpack extends TestCase {
@Test
public void testDouble() throws Exception {
// _testDouble(null); // FIXME
_testDouble((double) 0.0);
_testDouble((double) -0.0);
_testDouble((double) 1.0);
@ -147,9 +212,31 @@ public class TestPackUnpack extends TestCase {
assertEquals(src, dst, 10e-10);
}
@Test
public void testNullDouble() throws Exception {
Double src = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(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);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
unpacker.wrap(bytes);
tmpl = new OptionalTemplate(DoubleTemplate.getInstance());
dst = (Double) tmpl.unpack(unpacker);
assertEquals(src, dst);
}
@Test
public void testBoolean() throws Exception {
// _testBoolean(null); // FIXME
_testBoolean(false);
_testBoolean(true);
}
@ -163,9 +250,31 @@ public class TestPackUnpack extends TestCase {
assertEquals(src, dst);
}
@Test
public void testNullBoolean() throws Exception {
Boolean src = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(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);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
unpacker.wrap(bytes);
tmpl = new OptionalTemplate(BooleanTemplate.getInstance());
dst = (Boolean) tmpl.unpack(unpacker);
assertEquals(src, dst);
}
@Test
public void testString() throws Exception {
// _testString(null); // FIXME
_testString("");
_testString("a");
_testString("ab");
@ -211,10 +320,32 @@ public class TestPackUnpack extends TestCase {
assertEquals(src, dst);
}
@Test
public void testNullString() throws Exception {
String src = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(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);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
unpacker.wrap(bytes);
tmpl = new OptionalTemplate(StringTemplate.getInstance());
dst = (String) tmpl.unpack(unpacker);
assertEquals(src, dst);
}
@SuppressWarnings("unchecked")
@Test
public void testList() throws Exception {
// nullList // FIXME
List<Integer> emptyList = new ArrayList<Integer>();
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
@ -262,10 +393,34 @@ public class TestPackUnpack extends TestCase {
}
}
@SuppressWarnings("unchecked")
@Test
public void testNullList() throws Exception {
List<String> src = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(src);
byte[] bytes = out.toByteArray();
Template tmpl = null;
Unpacker unpacker = new Unpacker();
List<String> dst = null;
try {
tmpl = new ListTemplate(StringTemplate.getInstance());
unpacker.wrap(bytes);
dst = (List<String>) tmpl.unpack(unpacker);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
unpacker.wrap(bytes);
tmpl = new OptionalTemplate(new ListTemplate(StringTemplate
.getInstance()));
dst = (List<String>) tmpl.unpack(unpacker);
assertEquals(src, dst);
}
@SuppressWarnings("unchecked")
@Test
public void testMap() throws Exception {
// nullMap // FIXME
Map<Integer, Integer> emptyMap = new HashMap<Integer, Integer>();
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
@ -323,4 +478,30 @@ public class TestPackUnpack extends TestCase {
}
}
}
@SuppressWarnings("unchecked")
@Test
public void testNullMap() throws Exception {
Map<String, String> src = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(src);
byte[] bytes = out.toByteArray();
Template tmpl = null;
Unpacker unpacker = new Unpacker();
Map<String, String> dst = null;
try {
tmpl = new MapTemplate(StringTemplate.getInstance(), StringTemplate
.getInstance());
unpacker.wrap(bytes);
dst = (Map<String, String>) tmpl.unpack(unpacker);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
unpacker.wrap(bytes);
tmpl = new OptionalTemplate(new MapTemplate(StringTemplate
.getInstance(), StringTemplate.getInstance()));
dst = (Map<String, String>) tmpl.unpack(unpacker);
assertEquals(src, dst);
}
}

File diff suppressed because it is too large Load diff