java: add test methods for CollectionTemplate and change spec. of several test methods

This commit is contained in:
Muga Nishizawa
2010-12-12 00:38:13 +09:00
parent 4e4678edfa
commit f936a307c6
2 changed files with 811 additions and 300 deletions

View File

@@ -2,8 +2,12 @@ package org.msgpack.template;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.math.BigInteger; import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Random; import java.util.Random;
@@ -34,9 +38,9 @@ public class TestPackConvert extends TestCase {
static void _testInteger(Integer src) throws Exception { static void _testInteger(Integer src) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(src);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
Template tmpl = IntegerTemplate.getInstance(); Template tmpl = IntegerTemplate.getInstance();
tmpl.pack(new Packer(out), src);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
Integer dst = (Integer) tmpl.convert(obj, null); Integer dst = (Integer) tmpl.convert(obj, null);
assertEquals(src, dst); assertEquals(src, dst);
} }
@@ -44,20 +48,26 @@ public class TestPackConvert extends TestCase {
@Test @Test
public void testNullInteger() throws Exception { public void testNullInteger() throws Exception {
Integer src = null; Integer src = null;
Template tmpl = IntegerTemplate.getInstance();
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
Packer packer = new Packer(out);
try {
tmpl.pack(packer, src);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
new Packer(out).pack(src); new Packer(out).pack(src);
MessagePackObject obj = Util.unpackOne(out.toByteArray()); MessagePackObject obj = Util.unpackOne(out.toByteArray());
Template tmpl = IntegerTemplate.getInstance();
Integer dst = null;
try { try {
dst = (Integer) tmpl.convert(obj, null); tmpl.convert(obj, null);
fail(); fail();
} catch (Exception e) { } catch (Exception e) {
assertTrue(e instanceof MessageTypeException); assertTrue(e instanceof MessageTypeException);
} }
obj = Util.unpackOne(out.toByteArray()); obj = Util.unpackOne(out.toByteArray());
tmpl = new NullableTemplate(IntegerTemplate.getInstance()); tmpl = new NullableTemplate(IntegerTemplate.getInstance());
dst = (Integer) tmpl.convert(obj, null); Integer dst = (Integer) tmpl.convert(obj, null);
assertEquals(src, dst); assertEquals(src, dst);
} }
@@ -78,9 +88,9 @@ public class TestPackConvert extends TestCase {
public void _testLong(Long src) throws Exception { public void _testLong(Long src) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(src);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
Template tmpl = LongTemplate.getInstance(); Template tmpl = LongTemplate.getInstance();
tmpl.pack(new Packer(out), src);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
Long dst = (Long) tmpl.convert(obj, null); Long dst = (Long) tmpl.convert(obj, null);
assertEquals(src, dst); assertEquals(src, dst);
} }
@@ -88,20 +98,26 @@ public class TestPackConvert extends TestCase {
@Test @Test
public void testNullLong() throws Exception { public void testNullLong() throws Exception {
Long src = null; Long src = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(src);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
Template tmpl = LongTemplate.getInstance(); Template tmpl = LongTemplate.getInstance();
Long dst = null; ByteArrayOutputStream out = new ByteArrayOutputStream();
Packer packer = new Packer(out);
try { try {
dst = (Long) tmpl.convert(obj, null); tmpl.pack(packer, src);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
packer.pack(src);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
try {
tmpl.convert(obj, null);
fail(); fail();
} catch (Exception e) { } catch (Exception e) {
assertTrue(e instanceof MessageTypeException); assertTrue(e instanceof MessageTypeException);
} }
obj = Util.unpackOne(out.toByteArray()); obj = Util.unpackOne(out.toByteArray());
tmpl = new NullableTemplate(LongTemplate.getInstance()); tmpl = new NullableTemplate(LongTemplate.getInstance());
dst = (Long) tmpl.convert(obj, null); Long dst = (Long) tmpl.convert(obj, null);
assertEquals(src, dst); assertEquals(src, dst);
} }
@@ -118,16 +134,15 @@ public class TestPackConvert extends TestCase {
_testBigInteger(max); _testBigInteger(max);
Random rand = new Random(); Random rand = new Random();
for (int i = 0; i < 1000; i++) { for (int i = 0; i < 1000; i++) {
_testBigInteger(max.subtract(BigInteger.valueOf(Math.abs(rand _testBigInteger(max.subtract(BigInteger.valueOf(Math.abs(rand.nextLong()))));
.nextLong()))));
} }
} }
static void _testBigInteger(BigInteger src) throws Exception { static void _testBigInteger(BigInteger src) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(src);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
Template tmpl = BigIntegerTemplate.getInstance(); Template tmpl = BigIntegerTemplate.getInstance();
tmpl.pack(new Packer(out), src);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
BigInteger dst = (BigInteger) tmpl.convert(obj, null); BigInteger dst = (BigInteger) tmpl.convert(obj, null);
assertEquals(src, dst); assertEquals(src, dst);
} }
@@ -135,20 +150,25 @@ public class TestPackConvert extends TestCase {
@Test @Test
public void testNullBigInteger() throws Exception { public void testNullBigInteger() throws Exception {
Long src = null; Long src = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(src);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
Template tmpl = BigIntegerTemplate.getInstance(); Template tmpl = BigIntegerTemplate.getInstance();
BigInteger dst = null; ByteArrayOutputStream out = new ByteArrayOutputStream();
Packer packer = new Packer(out);
try { try {
dst = (BigInteger) tmpl.convert(obj, null); tmpl.pack(packer, src);
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
packer.pack(src);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
try {
tmpl.convert(obj, null);
fail(); fail();
} catch (Exception e) { } catch (Exception e) {
assertTrue(e instanceof MessageTypeException); assertTrue(e instanceof MessageTypeException);
} }
obj = Util.unpackOne(out.toByteArray()); obj = Util.unpackOne(out.toByteArray());
tmpl = new NullableTemplate(BigIntegerTemplate.getInstance()); tmpl = new NullableTemplate(BigIntegerTemplate.getInstance());
dst = (BigInteger) tmpl.convert(obj, null); BigInteger dst = (BigInteger) tmpl.convert(obj, null);
assertEquals(src, dst); assertEquals(src, dst);
} }
@@ -171,9 +191,9 @@ public class TestPackConvert extends TestCase {
static void _testFloat(Float src) throws Exception { static void _testFloat(Float src) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(src);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
Template tmpl = FloatTemplate.getInstance(); Template tmpl = FloatTemplate.getInstance();
tmpl.pack(new Packer(out), src);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
Float dst = (Float) tmpl.convert(obj, null); Float dst = (Float) tmpl.convert(obj, null);
assertEquals(src, dst, 10e-10); assertEquals(src, dst, 10e-10);
} }
@@ -181,20 +201,26 @@ public class TestPackConvert extends TestCase {
@Test @Test
public void testNullFloat() throws Exception { public void testNullFloat() throws Exception {
Long src = null; Long src = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(src);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
Template tmpl = FloatTemplate.getInstance(); Template tmpl = FloatTemplate.getInstance();
Float dst = null; ByteArrayOutputStream out = new ByteArrayOutputStream();
Packer packer = new Packer(out);
try { try {
dst = (Float) tmpl.convert(obj, null); tmpl.pack(packer, src);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
packer.pack(src);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
try {
tmpl.convert(obj, null);
fail(); fail();
} catch (Exception e) { } catch (Exception e) {
assertTrue(e instanceof MessageTypeException); assertTrue(e instanceof MessageTypeException);
} }
obj = Util.unpackOne(out.toByteArray()); obj = Util.unpackOne(out.toByteArray());
tmpl = new NullableTemplate(FloatTemplate.getInstance()); tmpl = new NullableTemplate(FloatTemplate.getInstance());
dst = (Float) tmpl.convert(obj, null); Float dst = (Float) tmpl.convert(obj, null);
assertEquals(src, dst); assertEquals(src, dst);
} }
@@ -217,9 +243,9 @@ public class TestPackConvert extends TestCase {
static void _testDouble(Double src) throws Exception { static void _testDouble(Double src) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(src);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
Template tmpl = DoubleTemplate.getInstance(); Template tmpl = DoubleTemplate.getInstance();
tmpl.pack(new Packer(out), src);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
Double dst = (Double) tmpl.convert(obj, null); Double dst = (Double) tmpl.convert(obj, null);
assertEquals(src, dst, 10e-10); assertEquals(src, dst, 10e-10);
} }
@@ -227,20 +253,26 @@ public class TestPackConvert extends TestCase {
@Test @Test
public void testNullDouble() throws Exception { public void testNullDouble() throws Exception {
Long src = null; Long src = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(src);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
Template tmpl = DoubleTemplate.getInstance(); Template tmpl = DoubleTemplate.getInstance();
Double dst = null; ByteArrayOutputStream out = new ByteArrayOutputStream();
Packer packer = new Packer(out);
try { try {
dst = (Double) tmpl.convert(obj, null); tmpl.pack(packer, src);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
packer.pack(src);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
try {
tmpl.convert(obj, null);
fail(); fail();
} catch (Exception e) { } catch (Exception e) {
assertTrue(e instanceof MessageTypeException); assertTrue(e instanceof MessageTypeException);
} }
obj = Util.unpackOne(out.toByteArray()); obj = Util.unpackOne(out.toByteArray());
tmpl = new NullableTemplate(DoubleTemplate.getInstance()); tmpl = new NullableTemplate(DoubleTemplate.getInstance());
dst = (Double) tmpl.convert(obj, null); Double dst = (Double) tmpl.convert(obj, null);
assertEquals(src, dst); assertEquals(src, dst);
} }
@@ -252,9 +284,9 @@ public class TestPackConvert extends TestCase {
static void _testBoolean(Boolean src) throws Exception { static void _testBoolean(Boolean src) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(src);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
Template tmpl = BooleanTemplate.getInstance(); Template tmpl = BooleanTemplate.getInstance();
tmpl.pack(new Packer(out), src);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
Boolean dst = (Boolean) tmpl.convert(obj, null); Boolean dst = (Boolean) tmpl.convert(obj, null);
assertEquals(src, dst); assertEquals(src, dst);
} }
@@ -262,20 +294,136 @@ public class TestPackConvert extends TestCase {
@Test @Test
public void testNullBoolean() throws Exception { public void testNullBoolean() throws Exception {
Long src = null; Long src = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(src);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
Template tmpl = BooleanTemplate.getInstance(); Template tmpl = BooleanTemplate.getInstance();
Boolean dst = null; ByteArrayOutputStream out = new ByteArrayOutputStream();
Packer packer = new Packer(out);
try { try {
dst = (Boolean) tmpl.convert(obj, null); tmpl.pack(packer, src);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
packer.pack(src);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
try {
tmpl.convert(obj, null);
fail(); fail();
} catch (Exception e) { } catch (Exception e) {
assertTrue(e instanceof MessageTypeException); assertTrue(e instanceof MessageTypeException);
} }
obj = Util.unpackOne(out.toByteArray()); obj = Util.unpackOne(out.toByteArray());
tmpl = new NullableTemplate(BooleanTemplate.getInstance()); tmpl = new NullableTemplate(BooleanTemplate.getInstance());
dst = (Boolean) tmpl.convert(obj, null); Boolean dst = (Boolean) tmpl.convert(obj, null);
assertEquals(src, dst);
}
@Test
public void testByteArray() throws Exception {
Random rand = new Random(System.currentTimeMillis());
byte[] b0 = new byte[0];
_testByteArray(b0);
byte[] b1 = new byte[10];
rand.nextBytes(b1);
_testByteArray(b1);
byte[] b2 = new byte[1024];
rand.nextBytes(b2);
_testByteArray(b2);
}
static void _testByteArray(byte[] src) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Template tmpl = ByteArrayTemplate.getInstance();
tmpl.pack(new Packer(out), src);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
byte[] dst = (byte[]) tmpl.convert(obj, null);
assertEquals(src.length, dst.length);
for (int i = 0; i < src.length; ++i) {
assertEquals(src[i], dst[i]);
}
}
@Test
public void testNullByteArray() throws Exception {
byte[] src = null;
Template tmpl = ByteArrayTemplate.getInstance();
ByteArrayOutputStream out = new ByteArrayOutputStream();
Packer packer = new Packer(out);
try {
tmpl.pack(packer, src);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
packer.pack(src);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
try {
tmpl.convert(obj, null);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
tmpl = new NullableTemplate(ByteArrayTemplate.getInstance());
obj = Util.unpackOne(out.toByteArray());
byte[] dst = (byte[]) tmpl.convert(obj, null);
assertEquals(null, dst);
}
@Test
public void testByteBuffer() throws Exception {// FIXME
Random rand = new Random(System.currentTimeMillis());
byte[] b0 = new byte[0];
ByteBuffer bb0 = ByteBuffer.wrap(b0);
_testByteBuffer(bb0);
bb0.clear();
byte[] b1 = new byte[10];
rand.nextBytes(b1);
ByteBuffer bb1 = ByteBuffer.wrap(b1);
_testByteBuffer(bb1);
bb1.clear();
byte[] b2 = new byte[2048];
rand.nextBytes(b2);
ByteBuffer bb2 = ByteBuffer.wrap(b2);
_testByteBuffer(bb2);
bb2.clear();
}
static void _testByteBuffer(ByteBuffer src) throws Exception {
Template tmpl = ByteBufferTemplate.getInstance();
ByteArrayOutputStream out = new ByteArrayOutputStream();
tmpl.pack(new Packer(out), src);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
ByteBuffer dst = (ByteBuffer) tmpl.convert(obj, null);
assertEquals(src.limit() - src.position(), dst.limit() - dst.position());
int dst_pos = dst.position();
for (int i = src.position(); i < src.limit(); ++i) {
assertEquals(src.get(i), dst.get(dst_pos));
dst_pos++;
}
}
@Test
public void testNullByteBuffer() throws Exception {
ByteBuffer src = null;
Template tmpl = ByteBufferTemplate.getInstance();
ByteArrayOutputStream out = new ByteArrayOutputStream();
Packer packer = new Packer(out);
try {
tmpl.pack(packer, src);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
packer.pack(src);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
try {
tmpl.convert(obj, null);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
obj = Util.unpackOne(out.toByteArray());
tmpl = new NullableTemplate(BooleanTemplate.getInstance());
ByteBuffer dst = (ByteBuffer) tmpl.convert(obj, null);
assertEquals(src, dst); assertEquals(src, dst);
} }
@@ -319,78 +467,76 @@ public class TestPackConvert extends TestCase {
static void _testString(String src) throws Exception { static void _testString(String src) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(src);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
Template tmpl = StringTemplate.getInstance(); Template tmpl = StringTemplate.getInstance();
tmpl.pack(new Packer(out), src);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
String dst = (String) tmpl.convert(obj, null); String dst = (String) tmpl.convert(obj, null);
assertEquals(src, dst); assertEquals(src, dst);
} }
@Test @Test
public void testNullString() throws Exception { public void testNullString() throws Exception {
Long src = null; String src = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(src);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
Template tmpl = StringTemplate.getInstance(); Template tmpl = StringTemplate.getInstance();
String dst = null; ByteArrayOutputStream out = new ByteArrayOutputStream();
Packer packer = new Packer(out);
try { try {
dst = (String) tmpl.convert(obj, null); tmpl.pack(packer, src);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
packer.pack(src);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
try {
tmpl.convert(obj, null);
fail(); fail();
} catch (Exception e) { } catch (Exception e) {
assertTrue(e instanceof MessageTypeException); assertTrue(e instanceof MessageTypeException);
} }
obj = Util.unpackOne(out.toByteArray()); obj = Util.unpackOne(out.toByteArray());
tmpl = new NullableTemplate(StringTemplate.getInstance()); tmpl = new NullableTemplate(StringTemplate.getInstance());
dst = (String) tmpl.convert(obj, null); String dst = (String) tmpl.convert(obj, null);
assertEquals(src, dst); assertEquals(src, dst);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Test @Test
public void testList() throws Exception { public void testList() throws Exception {
List<Integer> emptyList = new ArrayList<Integer>(); List<Integer> src = new ArrayList<Integer>();
Template tmpl = new ListTemplate(IntegerTemplate.getInstance());
// size is zero
{ {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(emptyList); tmpl.pack(new Packer(out), src);
MessagePackObject obj = Util.unpackOne(out.toByteArray()); MessagePackObject obj = Util.unpackOne(out.toByteArray());
Template tmpl = new ListTemplate(IntegerTemplate.getInstance());
List<Integer> dst = (List<Integer>) tmpl.convert(obj, null); List<Integer> dst = (List<Integer>) tmpl.convert(obj, null);
assertEquals(emptyList, dst); assertEquals(src.size(), dst.size());
Integer[] src_array = src.toArray(new Integer[0]);
Integer[] dst_array = dst.toArray(new Integer[0]);
for (int i = 0; i < src_array.length; ++i) {
assertEquals(src_array[i], dst_array[i]);
}
src.clear();
} }
for (int i = 0; i < 1000; i++) { // otherwise
List<Integer> l = new ArrayList<Integer>(); {
int len = (int) Math.random() % 1000 + 1; int len = (int) (Math.random() * 1000);
for (int j = 0; j < len; j++) { for (int i = 0; i < len; i++) {
l.add(j); src.add(i);
} }
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(l); tmpl.pack(new Packer(out), src);
MessagePackObject obj = Util.unpackOne(out.toByteArray()); MessagePackObject obj = Util.unpackOne(out.toByteArray());
Template tmpl = new ListTemplate(IntegerTemplate.getInstance());
List<Integer> dst = (List<Integer>) tmpl.convert(obj, null); List<Integer> dst = (List<Integer>) tmpl.convert(obj, null);
assertEquals(l.size(), dst.size()); assertEquals(src.size(), dst.size());
for (int j = 0; j < len; j++) { Integer[] src_array = src.toArray(new Integer[0]);
assertEquals(l.get(j), dst.get(j)); Integer[] dst_array = dst.toArray(new Integer[0]);
} for (int i = 0; i < src_array.length; ++i) {
} assertEquals(src_array[i], dst_array[i]);
for (int i = 0; i < 1000; i++) {
List<String> l = new ArrayList<String>();
int len = (int) Math.random() % 1000 + 1;
for (int j = 0; j < len; j++) {
l.add(Integer.toString(j));
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(l);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
Template tmpl = new ListTemplate(StringTemplate.getInstance());
List<String> dst = (List<String>) tmpl.convert(obj, null);
assertEquals(l.size(), dst.size());
for (int j = 0; j < len; j++) {
assertEquals(l.get(j), dst.get(j));
} }
src.clear();
} }
} }
@@ -398,74 +544,64 @@ public class TestPackConvert extends TestCase {
@Test @Test
public void testNullList() throws Exception { public void testNullList() throws Exception {
List<String> src = null; 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()); Template tmpl = new ListTemplate(StringTemplate.getInstance());
List<String> dst = null; ByteArrayOutputStream out = new ByteArrayOutputStream();
Packer packer = new Packer(out);
try { try {
dst = (List<String>) tmpl.convert(obj, null); tmpl.pack(packer, src);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
packer.pack(src);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
try {
tmpl.convert(obj, null);
fail(); fail();
} catch (Exception e) { } catch (Exception e) {
assertTrue(e instanceof MessageTypeException); assertTrue(e instanceof MessageTypeException);
} }
obj = Util.unpackOne(out.toByteArray()); obj = Util.unpackOne(out.toByteArray());
tmpl = new NullableTemplate(new ListTemplate(StringTemplate tmpl = new NullableTemplate(new ListTemplate(StringTemplate.getInstance()));
.getInstance())); List<String> dst = (List<String>) tmpl.convert(obj, null);
dst = (List<String>) tmpl.convert(obj, null);
assertEquals(src, dst); assertEquals(src, dst);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Test @Test
public void testMap() throws Exception { public void testMap() throws Exception {
Map<Integer, Integer> emptyMap = new HashMap<Integer, Integer>(); Map<Integer, Integer> src = new HashMap<Integer, Integer>();
Template tmpl = new MapTemplate(
IntegerTemplate.getInstance(),
IntegerTemplate.getInstance());
// size is zero
{ {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(emptyMap); tmpl.pack(new Packer(out), src);
MessagePackObject obj = Util.unpackOne(out.toByteArray()); MessagePackObject obj = Util.unpackOne(out.toByteArray());
Template tmpl = new MapTemplate(IntegerTemplate.getInstance(), Map<Integer, Integer> dst = (Map<Integer, Integer>) tmpl.convert(obj, null);
IntegerTemplate.getInstance()); assertEquals(src.size(), src.size());
Map<Integer, Integer> dst = (Map<Integer, Integer>) tmpl for (Map.Entry<Integer, Integer> pair : dst.entrySet()) {
.convert(obj, null); Integer val = src.get(pair.getKey());
assertEquals(emptyMap, dst);
}
for (int i = 0; i < 1000; i++) {
Map<Integer, Integer> m = new HashMap<Integer, Integer>();
int len = (int) Math.random() % 1000 + 1;
for (int j = 0; j < len; j++) {
m.put(j, j);
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(m);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
Template tmpl = new MapTemplate(IntegerTemplate.getInstance(),
IntegerTemplate.getInstance());
Map<Integer, Integer> map = (Map<Integer, Integer>) tmpl
.convert(obj, null);
assertEquals(m.size(), map.size());
for (Map.Entry<Integer, Integer> pair : map.entrySet()) {
Integer val = m.get(pair.getKey());
assertNotNull(val); assertNotNull(val);
assertEquals(val, pair.getValue()); assertEquals(val, pair.getValue());
} }
src.clear();
} }
for (int i = 0; i < 1000; i++) { // otherwise
Map<String, Integer> m = new HashMap<String, Integer>(); {
int len = (int) Math.random() % 1000 + 1; int len = (int) (Math.random() * 1000);
for (int j = 0; j < len; j++) for (int j = 0; j < len; j++) {
m.put(Integer.toString(j), j); src.put(j, j);
}
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(m); tmpl.pack(new Packer(out), src);
MessagePackObject obj = Util.unpackOne(out.toByteArray()); MessagePackObject obj = Util.unpackOne(out.toByteArray());
Template tmpl = new MapTemplate(StringTemplate.getInstance(), Map<Integer, Integer> dst = (Map<Integer, Integer>) tmpl.convert(obj, null);
IntegerTemplate.getInstance()); assertEquals(src.size(), dst.size());
Map<String, Integer> map = (Map<String, Integer>) tmpl.convert(obj, null); for (Map.Entry<Integer, Integer> pair : dst.entrySet()) {
assertEquals(m.size(), map.size()); Integer val = src.get(pair.getKey());
for (Map.Entry<String, Integer> pair : map.entrySet()) {
Integer val = m.get(pair.getKey());
assertNotNull(val); assertNotNull(val);
assertEquals(val, pair.getValue()); assertEquals(val, pair.getValue());
} }
@@ -476,22 +612,133 @@ public class TestPackConvert extends TestCase {
@Test @Test
public void testNullMap() throws Exception { public void testNullMap() throws Exception {
Map<String, String> src = null; Map<String, String> src = null;
ByteArrayOutputStream out = new ByteArrayOutputStream(); Template tmpl = new MapTemplate(
new Packer(out).pack(src); StringTemplate.getInstance(),
MessagePackObject obj = Util.unpackOne(out.toByteArray());
Template tmpl = new MapTemplate(StringTemplate.getInstance(),
StringTemplate.getInstance()); StringTemplate.getInstance());
Map<String, String> dst = null; ByteArrayOutputStream out = new ByteArrayOutputStream();
Packer packer = new Packer(out);
try { try {
dst = (Map<String, String>) tmpl.convert(obj, null); tmpl.pack(packer, src);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
packer.pack(src);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
try {
tmpl.convert(obj, null);
fail(); fail();
} catch (Exception e) { } catch (Exception e) {
assertTrue(e instanceof MessageTypeException); assertTrue(e instanceof MessageTypeException);
} }
obj = Util.unpackOne(out.toByteArray()); obj = Util.unpackOne(out.toByteArray());
tmpl = new NullableTemplate(new MapTemplate(StringTemplate tmpl = new NullableTemplate(new MapTemplate(
.getInstance(), StringTemplate.getInstance())); StringTemplate.getInstance(),
dst = (Map<String, String>) tmpl.convert(obj, null); StringTemplate.getInstance()));
Map<String, String> dst = (Map<String, String>) tmpl.convert(obj, null);
assertEquals(src, dst);
}
@SuppressWarnings("unchecked")
@Test
public void testCollectionLinkedList() throws Exception {
LinkedList<Integer> src = new LinkedList<Integer>();
Template tmpl = new CollectionTemplate(IntegerTemplate.getInstance());
// size is zero
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
tmpl.pack(new Packer(out), src);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
LinkedList<Integer> dst = (LinkedList<Integer>)
tmpl.convert(obj, new LinkedList<Integer>());
assertEquals(src.getClass(), dst.getClass());
assertEquals(src.size(), dst.size());
src.clear();
}
// otherwise
{
int len = (int) Math.random() % 1000 + 1;
for (int j = 0; j < len; j++) {
src.add(j);
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
tmpl.pack(new Packer(out), src);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
LinkedList<Integer> dst = (LinkedList<Integer>)
tmpl.convert(obj, new LinkedList<Integer>());
assertEquals(src.getClass(), dst.getClass());
assertEquals(src.size(), dst.size());
for (int j = 0; j < len; j++) {
assertEquals(src.get(j), dst.get(j));
}
src.clear();
}
}
@SuppressWarnings("unchecked")
@Test
public void testCollectionHashSet() throws Exception {
HashSet<Integer> src = new HashSet<Integer>();
Template tmpl = new CollectionTemplate(IntegerTemplate.getInstance());
// size is zero
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
tmpl.pack(new Packer(out), src);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
HashSet<Integer> dst = (HashSet<Integer>)
tmpl.convert(obj, new HashSet<Integer>());
assertEquals(src.getClass(), dst.getClass());
assertEquals(src.size(), dst.size());
src.clear();
}
// otherwise
{
int len = (int) Math.random() % 1000 + 1;
for (int j = 0; j < len; j++) {
src.add(j);
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
tmpl.pack(new Packer(out), src);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
HashSet<Integer> dst = (HashSet<Integer>)
tmpl.convert(obj, new HashSet<Integer>());
assertEquals(src.getClass(), dst.getClass());
assertEquals(src.size(), dst.size());
Integer[] src_array = src.toArray(new Integer[0]);
Integer[] dst_array = dst.toArray(new Integer[0]);
for (int j = 0; j < len; j++) {
assertEquals(src_array[j], dst_array[j]);
}
src.clear();
}
}
@SuppressWarnings("unchecked")
@Test
public void testNullCollection() throws Exception {
Collection<String> src = null;
Template tmpl = new CollectionTemplate(StringTemplate.getInstance());
ByteArrayOutputStream out = new ByteArrayOutputStream();
Packer packer = new Packer(out);
try {
tmpl.pack(packer, src);
fail();
} catch (Throwable t) {
assertTrue(t instanceof MessageTypeException);
}
packer.pack(src);
MessagePackObject obj = Util.unpackOne(out.toByteArray());
try {
tmpl.convert(obj, null);
fail();
} catch (Throwable t) {
assertTrue(t instanceof MessageTypeException);
}
obj = Util.unpackOne(out.toByteArray());
tmpl = new NullableTemplate(new CollectionTemplate(StringTemplate.getInstance()));
Collection<String> dst = (Collection<String>) tmpl.convert(obj, null);
assertEquals(src, dst); assertEquals(src, dst);
} }
} }

View File

@@ -3,8 +3,12 @@ package org.msgpack.template;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.math.BigInteger; import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Random; import java.util.Random;
@@ -33,33 +37,38 @@ public class TestPackUnpack extends TestCase {
static void _testInteger(Integer src) throws Exception { static void _testInteger(Integer src) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(src);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
Template tmpl = IntegerTemplate.getInstance(); Template tmpl = IntegerTemplate.getInstance();
tmpl.pack(new Packer(out), src);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
Integer dst = (Integer) tmpl.unpack(new Unpacker(in), null); Integer dst = (Integer) tmpl.unpack(new Unpacker(in), null);
assertEquals(src, dst); assertEquals(src, dst);
} }
@Test @Test
public void testNullInteger() throws Exception { public void testNullInteger() throws Exception {
Template tmpl = IntegerTemplate.getInstance();
Integer src = null; Integer src = null;
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(src); Packer packer = new Packer(out);
byte[] bytes = out.toByteArray(); try {
Template tmpl = null; tmpl.pack(packer, src);
Unpacker unpacker = new Unpacker(); fail();
Integer dst = null; } catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
packer.pack(src);
byte[] bytes = out.toByteArray();
Unpacker unpacker = new Unpacker();
try { try {
tmpl = IntegerTemplate.getInstance();
unpacker.wrap(bytes); unpacker.wrap(bytes);
dst = (Integer) tmpl.unpack(unpacker, null); tmpl.unpack(unpacker, null);
fail(); fail();
} catch (Exception e) { } catch (Exception e) {
assertTrue(e instanceof MessageTypeException); assertTrue(e instanceof MessageTypeException);
} }
unpacker.wrap(bytes); unpacker.wrap(bytes);
tmpl = new NullableTemplate(IntegerTemplate.getInstance()); tmpl = new NullableTemplate(IntegerTemplate.getInstance());
dst = (Integer) tmpl.unpack(unpacker, null); Integer dst = (Integer) tmpl.unpack(unpacker, null);
assertEquals(src, dst); assertEquals(src, dst);
} }
@@ -80,9 +89,9 @@ public class TestPackUnpack extends TestCase {
static void _testLong(Long src) throws Exception { static void _testLong(Long src) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(src);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
Template tmpl = LongTemplate.getInstance(); Template tmpl = LongTemplate.getInstance();
tmpl.pack(new Packer(out), src);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
Long dst = (Long) tmpl.unpack(new Unpacker(in), null); Long dst = (Long) tmpl.unpack(new Unpacker(in), null);
assertEquals(src, dst); assertEquals(src, dst);
} }
@@ -90,23 +99,28 @@ public class TestPackUnpack extends TestCase {
@Test @Test
public void testNullLong() throws Exception { public void testNullLong() throws Exception {
Long src = null; Long src = null;
Template tmpl = LongTemplate.getInstance();
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(src); Packer packer = new Packer(out);
byte[] bytes = out.toByteArray(); try {
Template tmpl = null; tmpl.pack(packer, src);
Unpacker unpacker = new Unpacker(); fail();
Long dst = null; } catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
packer.pack(src);
byte[] bytes = out.toByteArray();
Unpacker unpacker = new Unpacker();
try { try {
tmpl = LongTemplate.getInstance();
unpacker.wrap(bytes); unpacker.wrap(bytes);
dst = (Long) tmpl.unpack(unpacker, null); tmpl.unpack(unpacker, null);
fail(); fail();
} catch (Exception e) { } catch (Exception e) {
assertTrue(e instanceof MessageTypeException); assertTrue(e instanceof MessageTypeException);
} }
unpacker.wrap(bytes); unpacker.wrap(bytes);
tmpl = new NullableTemplate(LongTemplate.getInstance()); tmpl = new NullableTemplate(LongTemplate.getInstance());
dst = (Long) tmpl.unpack(unpacker, null); Long dst = (Long) tmpl.unpack(unpacker, null);
assertEquals(src, dst); assertEquals(src, dst);
} }
@@ -130,9 +144,9 @@ public class TestPackUnpack extends TestCase {
static void _testBigInteger(BigInteger src) throws Exception { static void _testBigInteger(BigInteger src) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack((Object) src);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
Template tmpl = BigIntegerTemplate.getInstance(); Template tmpl = BigIntegerTemplate.getInstance();
tmpl.pack(new Packer(out), src);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
BigInteger dst = (BigInteger) tmpl.unpack(new Unpacker(in), null); BigInteger dst = (BigInteger) tmpl.unpack(new Unpacker(in), null);
assertEquals(src, dst); assertEquals(src, dst);
} }
@@ -140,23 +154,28 @@ public class TestPackUnpack extends TestCase {
@Test @Test
public void testNullBigInteger() throws Exception { public void testNullBigInteger() throws Exception {
BigInteger src = null; BigInteger src = null;
Template tmpl = BigIntegerTemplate.getInstance();
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(src); Packer packer = new Packer(out);
byte[] bytes = out.toByteArray(); try {
Template tmpl = null; tmpl.pack(packer, src);
Unpacker unpacker = new Unpacker(); fail();
BigInteger dst = null; } catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
packer.pack(src);
byte[] bytes = out.toByteArray();
Unpacker unpacker = new Unpacker();
try { try {
tmpl = BigIntegerTemplate.getInstance();
unpacker.wrap(bytes); unpacker.wrap(bytes);
dst = (BigInteger) tmpl.unpack(unpacker, null); tmpl.unpack(unpacker, null);
fail(); fail();
} catch (Exception e) { } catch (Exception e) {
assertTrue(e instanceof MessageTypeException); assertTrue(e instanceof MessageTypeException);
} }
unpacker.wrap(bytes); unpacker.wrap(bytes);
tmpl = new NullableTemplate(BigIntegerTemplate.getInstance()); tmpl = new NullableTemplate(BigIntegerTemplate.getInstance());
dst = (BigInteger) tmpl.unpack(unpacker, null); BigInteger dst = (BigInteger) tmpl.unpack(unpacker, null);
assertEquals(src, dst); assertEquals(src, dst);
} }
@@ -179,13 +198,41 @@ public class TestPackUnpack extends TestCase {
static void _testFloat(Float src) throws Exception { static void _testFloat(Float src) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(src);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
Template tmpl = FloatTemplate.getInstance(); Template tmpl = FloatTemplate.getInstance();
tmpl.pack(new Packer(out), src);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
Float dst = (Float) tmpl.unpack(new Unpacker(in), null); Float dst = (Float) tmpl.unpack(new Unpacker(in), null);
assertEquals(src, dst, 10e-10); assertEquals(src, dst, 10e-10);
} }
@Test
public void testNullFloat() throws Exception {
Double src = null;
Template tmpl = FloatTemplate.getInstance();
ByteArrayOutputStream out = new ByteArrayOutputStream();
Packer packer = new Packer(out);
try {
tmpl.pack(packer, src);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
packer.pack(src);
byte[] bytes = out.toByteArray();
Unpacker unpacker = new Unpacker();
try {
unpacker.wrap(bytes);
tmpl.unpack(unpacker, null);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
unpacker.wrap(bytes);
tmpl = new NullableTemplate(FloatTemplate.getInstance());
Float dst = (Float) tmpl.unpack(unpacker, null);
assertEquals(src, dst);
}
@Test @Test
public void testDouble() throws Exception { public void testDouble() throws Exception {
_testDouble((double) 0.0); _testDouble((double) 0.0);
@@ -205,9 +252,9 @@ public class TestPackUnpack extends TestCase {
static void _testDouble(Double src) throws Exception { static void _testDouble(Double src) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(src);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
Template tmpl = DoubleTemplate.getInstance(); Template tmpl = DoubleTemplate.getInstance();
tmpl.pack(new Packer(out), src);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
Double dst = (Double) tmpl.unpack(new Unpacker(in), null); Double dst = (Double) tmpl.unpack(new Unpacker(in), null);
assertEquals(src, dst, 10e-10); assertEquals(src, dst, 10e-10);
} }
@@ -215,23 +262,28 @@ public class TestPackUnpack extends TestCase {
@Test @Test
public void testNullDouble() throws Exception { public void testNullDouble() throws Exception {
Double src = null; Double src = null;
Template tmpl = DoubleTemplate.getInstance();
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(src); Packer packer = new Packer(out);
byte[] bytes = out.toByteArray(); try {
Template tmpl = null; tmpl.pack(packer, src);
Unpacker unpacker = new Unpacker(); fail();
Double dst = null; } catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
packer.pack(src);
byte[] bytes = out.toByteArray();
Unpacker unpacker = new Unpacker();
try { try {
tmpl = DoubleTemplate.getInstance();
unpacker.wrap(bytes); unpacker.wrap(bytes);
dst = (Double) tmpl.unpack(unpacker, null); tmpl.unpack(unpacker, null);
fail(); fail();
} catch (Exception e) { } catch (Exception e) {
assertTrue(e instanceof MessageTypeException); assertTrue(e instanceof MessageTypeException);
} }
unpacker.wrap(bytes); unpacker.wrap(bytes);
tmpl = new NullableTemplate(DoubleTemplate.getInstance()); tmpl = new NullableTemplate(DoubleTemplate.getInstance());
dst = (Double) tmpl.unpack(unpacker, null); Double dst = (Double) tmpl.unpack(unpacker, null);
assertEquals(src, dst); assertEquals(src, dst);
} }
@@ -243,9 +295,10 @@ public class TestPackUnpack extends TestCase {
static void _testBoolean(Boolean src) throws Exception { static void _testBoolean(Boolean src) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(src);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
Template tmpl = BooleanTemplate.getInstance(); Template tmpl = BooleanTemplate.getInstance();
Packer packer = new Packer(out);
tmpl.pack(packer, src);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
Boolean dst = (Boolean) tmpl.unpack(new Unpacker(in), null); Boolean dst = (Boolean) tmpl.unpack(new Unpacker(in), null);
assertEquals(src, dst); assertEquals(src, dst);
} }
@@ -253,26 +306,145 @@ public class TestPackUnpack extends TestCase {
@Test @Test
public void testNullBoolean() throws Exception { public void testNullBoolean() throws Exception {
Boolean src = null; Boolean src = null;
Template tmpl = BooleanTemplate.getInstance();
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(src); Packer packer = new Packer(out);
byte[] bytes = out.toByteArray(); try {
Template tmpl = null; tmpl.pack(packer, src);
Unpacker unpacker = new Unpacker(); fail();
Boolean dst = null; } catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
packer.pack(src);
byte[] bytes = out.toByteArray();
Unpacker unpacker = new Unpacker();
try { try {
tmpl = BooleanTemplate.getInstance();
unpacker.wrap(bytes); unpacker.wrap(bytes);
dst = (Boolean) tmpl.unpack(unpacker, null); tmpl.unpack(unpacker, null);
fail(); fail();
} catch (Exception e) { } catch (Exception e) {
assertTrue(e instanceof MessageTypeException); assertTrue(e instanceof MessageTypeException);
} }
unpacker.wrap(bytes); unpacker.wrap(bytes);
tmpl = new NullableTemplate(BooleanTemplate.getInstance()); tmpl = new NullableTemplate(BooleanTemplate.getInstance());
dst = (Boolean) tmpl.unpack(unpacker, null); Boolean dst = (Boolean) tmpl.unpack(unpacker, null);
assertEquals(src, dst); assertEquals(src, dst);
} }
@Test
public void testByteArray() throws Exception {
Random rand = new Random(System.currentTimeMillis());
byte[] b0 = new byte[0];
_testByteArray(b0);
byte[] b1 = new byte[10];
rand.nextBytes(b1);
_testByteArray(b1);
byte[] b2 = new byte[1024];
rand.nextBytes(b2);
_testByteArray(b2);
}
static void _testByteArray(byte[] src) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Template tmpl = ByteArrayTemplate.getInstance();
tmpl.pack(new Packer(out), src);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
byte[] dst = (byte[]) tmpl.unpack(new Unpacker(in), null);
assertEquals(src.length, dst.length);
for (int i = 0; i < src.length; ++i) {
assertEquals(src[i], dst[i]);
}
}
@Test
public void testNullByteArray() throws Exception {
byte[] src = null;
Template tmpl = ByteArrayTemplate.getInstance();
ByteArrayOutputStream out = new ByteArrayOutputStream();
Packer packer = new Packer(out);
try {
tmpl.pack(packer, src);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
packer.pack(src);
byte[] bytes = out.toByteArray();
Unpacker unpacker = new Unpacker();
try {
unpacker.wrap(bytes);
tmpl.unpack(unpacker, null);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
unpacker.wrap(bytes);
tmpl = new NullableTemplate(BooleanTemplate.getInstance());
byte[] dst = (byte[]) tmpl.unpack(unpacker, null);
assertEquals(null, dst);
}
@Test
public void testByteBuffer() throws Exception {
Random rand = new Random(System.currentTimeMillis());
byte[] b0 = new byte[0];
ByteBuffer bb0 = ByteBuffer.wrap(b0);
_testByteBuffer(bb0);
bb0.clear();
byte[] b1 = new byte[10];
rand.nextBytes(b1);
ByteBuffer bb1 = ByteBuffer.wrap(b1);
_testByteBuffer(bb1);
bb1.clear();
byte[] b2 = new byte[2048];
rand.nextBytes(b2);
ByteBuffer bb2 = ByteBuffer.wrap(b2);
_testByteBuffer(bb2);
bb2.clear();
}
static void _testByteBuffer(ByteBuffer src) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Template tmpl = ByteBufferTemplate.getInstance();
tmpl.pack(new Packer(out), src);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
ByteBuffer dst = (ByteBuffer) tmpl.unpack(new Unpacker(in), null);
assertEquals(src.limit() - src.position(), dst.limit() - dst.position());
int dst_pos = dst.position();
for (int i = src.position(); i < src.limit(); ++i) {
assertEquals(src.get(i), dst.get(dst_pos));
dst_pos++;
}
}
@Test
public void testNullByteBuffer() throws Exception {
ByteBuffer src = null;
Template tmpl = ByteBufferTemplate.getInstance();
ByteArrayOutputStream out = new ByteArrayOutputStream();
Packer packer = new Packer(out);
try {
tmpl.pack(packer, src);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
packer.pack(src);
byte[] bytes = out.toByteArray();
Unpacker unpacker = new Unpacker();
try {
unpacker.wrap(bytes);
tmpl.unpack(unpacker, null);
fail();
} catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
unpacker.wrap(bytes);
tmpl = new NullableTemplate(BooleanTemplate.getInstance());
ByteBuffer dst = (ByteBuffer) tmpl.unpack(unpacker, null);
assertEquals(null, dst);
}
@Test @Test
public void testString() throws Exception { public void testString() throws Exception {
_testString(""); _testString("");
@@ -313,9 +485,9 @@ public class TestPackUnpack extends TestCase {
static void _testString(String src) throws Exception { static void _testString(String src) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(src);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
Template tmpl = StringTemplate.getInstance(); Template tmpl = StringTemplate.getInstance();
tmpl.pack(new Packer(out), src);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
String dst = (String) tmpl.unpack(new Unpacker(in), null); String dst = (String) tmpl.unpack(new Unpacker(in), null);
assertEquals(src, dst); assertEquals(src, dst);
} }
@@ -323,73 +495,68 @@ public class TestPackUnpack extends TestCase {
@Test @Test
public void testNullString() throws Exception { public void testNullString() throws Exception {
String src = null; String src = null;
Template tmpl = StringTemplate.getInstance();
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(src); Packer packer = new Packer(out);
byte[] bytes = out.toByteArray(); try {
Template tmpl = null; tmpl.pack(packer, src);
Unpacker unpacker = new Unpacker(); fail();
String dst = null; } catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
packer.pack(src);
byte[] bytes = out.toByteArray();
Unpacker unpacker = new Unpacker();
try { try {
tmpl = StringTemplate.getInstance();
unpacker.wrap(bytes); unpacker.wrap(bytes);
dst = (String) tmpl.unpack(unpacker, null); tmpl.unpack(unpacker, null);
fail(); fail();
} catch (Exception e) { } catch (Exception e) {
assertTrue(e instanceof MessageTypeException); assertTrue(e instanceof MessageTypeException);
} }
unpacker.wrap(bytes); unpacker.wrap(bytes);
tmpl = new NullableTemplate(StringTemplate.getInstance()); tmpl = new NullableTemplate(StringTemplate.getInstance());
dst = (String) tmpl.unpack(unpacker, null); String dst = (String) tmpl.unpack(unpacker, null);
assertEquals(src, dst); assertEquals(src, dst);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Test @Test
public void testList() throws Exception { public void testList() throws Exception {
List<Integer> emptyList = new ArrayList<Integer>(); List<Integer> src = new ArrayList<Integer>();
Template tmpl = new ListTemplate(IntegerTemplate.getInstance());
// size is zero
{ {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(emptyList); tmpl.pack(new Packer(out), src);
ByteArrayInputStream in = new ByteArrayInputStream(out ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
.toByteArray());
Template tmpl = new ListTemplate(IntegerTemplate.getInstance());
List<Integer> dst = (List<Integer>) tmpl.unpack(new Unpacker(in), null); List<Integer> dst = (List<Integer>) tmpl.unpack(new Unpacker(in), null);
assertEquals(emptyList, dst); assertEquals(src.size(), dst.size());
Integer[] src_array = src.toArray(new Integer[0]);
Integer[] dst_array = dst.toArray(new Integer[0]);
for (int i = 0; i < src_array.length; ++i) {
assertEquals(src_array[i], dst_array[i]);
}
src.clear();
} }
for (int i = 0; i < 1000; i++) { // otherwise
List<Integer> l = new ArrayList<Integer>(); {
int len = (int) Math.random() % 1000 + 1; int len = (int) (Math.random() * 1000);
for (int j = 0; j < len; j++) { for (int i = 0; i < len; i++) {
l.add(j); src.add(i);
} }
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(l); tmpl.pack(new Packer(out), src);
ByteArrayInputStream in = new ByteArrayInputStream(out ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
.toByteArray());
Template tmpl = new ListTemplate(IntegerTemplate.getInstance());
List<Integer> dst = (List<Integer>) tmpl.unpack(new Unpacker(in), null); List<Integer> dst = (List<Integer>) tmpl.unpack(new Unpacker(in), null);
assertEquals(len, dst.size()); assertEquals(src.size(), dst.size());
for (int j = 0; j < len; j++) { Integer[] src_array = src.toArray(new Integer[0]);
assertEquals(l.get(j), dst.get(j)); Integer[] dst_array = dst.toArray(new Integer[0]);
} for (int i = 0; i < src_array.length; ++i) {
} assertEquals(src_array[i], dst_array[i]);
for (int i = 0; i < 1000; i++) {
List<String> l = new ArrayList<String>();
int len = (int) Math.random() % 1000 + 1;
for (int j = 0; j < len; j++)
l.add(Integer.toString(j));
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(l);
ByteArrayInputStream in = new ByteArrayInputStream(out
.toByteArray());
Template tmpl = new ListTemplate(StringTemplate.getInstance());
List<String> dst = (List<String>) tmpl.unpack(new Unpacker(in), null);
assertEquals(len, dst.size());
for (int j = 0; j < len; j++) {
assertEquals(l.get(j), dst.get(j));
} }
src.clear();
} }
} }
@@ -397,82 +564,68 @@ public class TestPackUnpack extends TestCase {
@Test @Test
public void testNullList() throws Exception { public void testNullList() throws Exception {
List<String> src = null; List<String> src = null;
Template tmpl = new ListTemplate(StringTemplate.getInstance());
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(src); Packer packer = new Packer(out);
byte[] bytes = out.toByteArray(); try {
Template tmpl = null; tmpl.pack(packer, src);
Unpacker unpacker = new Unpacker(); fail();
List<String> dst = null; } catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
packer.pack(src);
byte[] bytes = out.toByteArray();
Unpacker unpacker = new Unpacker();
try { try {
tmpl = new ListTemplate(StringTemplate.getInstance());
unpacker.wrap(bytes); unpacker.wrap(bytes);
dst = (List<String>) tmpl.unpack(unpacker, null); tmpl.unpack(unpacker, null);
fail(); fail();
} catch (Exception e) { } catch (Exception e) {
assertTrue(e instanceof MessageTypeException); assertTrue(e instanceof MessageTypeException);
} }
unpacker.wrap(bytes); unpacker.wrap(bytes);
tmpl = new NullableTemplate(new ListTemplate(StringTemplate tmpl = new NullableTemplate(new ListTemplate(StringTemplate.getInstance()));
.getInstance())); List<String> dst = (List<String>) tmpl.unpack(unpacker, null);
dst = (List<String>) tmpl.unpack(unpacker, null);
assertEquals(src, dst); assertEquals(src, dst);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Test @Test
public void testMap() throws Exception { public void testMap() throws Exception {
Map<Integer, Integer> emptyMap = new HashMap<Integer, Integer>(); Map<Integer, Integer> src = new HashMap<Integer, Integer>();
Template tmpl = new MapTemplate(
IntegerTemplate.getInstance(),
IntegerTemplate.getInstance());
// size is zero
{ {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(emptyMap); tmpl.pack(new Packer(out), src);
ByteArrayInputStream in = new ByteArrayInputStream(out ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
.toByteArray()); Map<Integer, Integer> dst = (Map<Integer, Integer>)
Template tmpl = new MapTemplate(IntegerTemplate.getInstance(), tmpl.unpack(new Unpacker(in), null);
IntegerTemplate.getInstance()); assertEquals(src.size(), src.size());
Map<Integer, Integer> dst = (Map<Integer, Integer>) tmpl for (Map.Entry<Integer, Integer> pair : dst.entrySet()) {
.unpack(new Unpacker(in), null); Integer val = src.get(pair.getKey());
assertEquals(emptyMap, dst);
}
for (int i = 0; i < 1000; i++) {
Map<Integer, Integer> m = new HashMap<Integer, Integer>();
int len = (int) Math.random() % 1000 + 1;
for (int j = 0; j < len; j++) {
m.put(j, j);
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(m);
ByteArrayInputStream in = new ByteArrayInputStream(out
.toByteArray());
Template tmpl = new MapTemplate(IntegerTemplate.getInstance(),
IntegerTemplate.getInstance());
Map<Integer, Integer> map = (Map<Integer, Integer>) tmpl
.unpack(new Unpacker(in), null);
assertEquals(len, map.size());
for (Map.Entry<Integer, Integer> pair : map.entrySet()) {
Integer val = m.get(pair.getKey());
assertNotNull(val); assertNotNull(val);
assertEquals(val, pair.getValue()); assertEquals(val, pair.getValue());
} }
src.clear();
} }
for (int i = 0; i < 1000; i++) { // otherwise
Map<String, Integer> m = new HashMap<String, Integer>(); {
int len = (int) Math.random() % 1000 + 1; int len = (int) (Math.random() * 1000);
for (int j = 0; j < len; j++) { for (int j = 0; j < len; j++) {
m.put(Integer.toString(j), j); src.put(j, j);
} }
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(m); tmpl.pack(new Packer(out), src);
ByteArrayInputStream in = new ByteArrayInputStream(out ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
.toByteArray()); Map<Integer, Integer> dst = (Map<Integer, Integer>)
Template tmpl = new MapTemplate(StringTemplate.getInstance(), tmpl.unpack(new Unpacker(in), null);
IntegerTemplate.getInstance()); assertEquals(src.size(), dst.size());
Map<String, Integer> map = (Map<String, Integer>) tmpl for (Map.Entry<Integer, Integer> pair : dst.entrySet()) {
.unpack(new Unpacker(in), null); Integer val = src.get(pair.getKey());
assertEquals(m.size(), map.size());
for (Map.Entry<String, Integer> pair : map.entrySet()) {
Integer val = m.get(pair.getKey());
assertNotNull(val); assertNotNull(val);
assertEquals(val, pair.getValue()); assertEquals(val, pair.getValue());
} }
@@ -483,17 +636,23 @@ public class TestPackUnpack extends TestCase {
@Test @Test
public void testNullMap() throws Exception { public void testNullMap() throws Exception {
Map<String, String> src = null; Map<String, String> src = null;
Template tmpl = new MapTemplate(
StringTemplate.getInstance(),
StringTemplate.getInstance());
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(src); Packer packer = new Packer(out);
byte[] bytes = out.toByteArray(); try {
Template tmpl = null; tmpl.pack(packer, src);
Unpacker unpacker = new Unpacker(); fail();
Map<String, String> dst = null; } catch (Exception e) {
assertTrue(e instanceof MessageTypeException);
}
packer.pack(src);
byte[] bytes = out.toByteArray();
Unpacker unpacker = new Unpacker();
try { try {
tmpl = new MapTemplate(StringTemplate.getInstance(), StringTemplate
.getInstance());
unpacker.wrap(bytes); unpacker.wrap(bytes);
dst = (Map<String, String>) tmpl.unpack(unpacker, null); tmpl.unpack(unpacker, null);
fail(); fail();
} catch (Exception e) { } catch (Exception e) {
assertTrue(e instanceof MessageTypeException); assertTrue(e instanceof MessageTypeException);
@@ -501,7 +660,112 @@ public class TestPackUnpack extends TestCase {
unpacker.wrap(bytes); unpacker.wrap(bytes);
tmpl = new NullableTemplate(new MapTemplate(StringTemplate tmpl = new NullableTemplate(new MapTemplate(StringTemplate
.getInstance(), StringTemplate.getInstance())); .getInstance(), StringTemplate.getInstance()));
dst = (Map<String, String>) tmpl.unpack(unpacker, null); Map<String, String> dst = (Map<String, String>) tmpl.unpack(unpacker, null);
assertEquals(src, dst);
}
@SuppressWarnings("unchecked")
@Test
public void testCollectionLinkedList() throws Exception {
LinkedList<Integer> src = new LinkedList<Integer>();
Template tmpl = new CollectionTemplate(IntegerTemplate.getInstance());
// size is zero
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
tmpl.pack(new Packer(out), src);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
LinkedList<Integer> dst = (LinkedList<Integer>)
tmpl.unpack(new Unpacker(in), new LinkedList<Integer>());
assertEquals(src.getClass(), dst.getClass());
assertEquals(src.size(), dst.size());
src.clear();
}
// otherwise
{
int len = (int) Math.random() % 1000 + 1;
for (int j = 0; j < len; j++) {
src.add(j);
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
tmpl.pack(new Packer(out), src);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
LinkedList<Integer> dst = (LinkedList<Integer>)
tmpl.unpack(new Unpacker(in), new LinkedList<Integer>());
assertEquals(src.getClass(), dst.getClass());
assertEquals(src.size(), dst.size());
for (int j = 0; j < len; j++) {
assertEquals(src.get(j), dst.get(j));
}
src.clear();
}
}
@SuppressWarnings("unchecked")
@Test
public void testCollectionHashSet() throws Exception {
HashSet<Integer> src = new HashSet<Integer>();
Template tmpl = new CollectionTemplate(IntegerTemplate.getInstance());
// size is zero
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
tmpl.pack(new Packer(out), src);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
HashSet<Integer> dst = (HashSet<Integer>)
tmpl.unpack(new Unpacker(in), new HashSet<Integer>());
assertEquals(src.getClass(), dst.getClass());
assertEquals(src.size(), dst.size());
src.clear();
}
// otherwise
{
int len = (int) Math.random() % 1000 + 1;
for (int j = 0; j < len; j++) {
src.add(j);
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
tmpl.pack(new Packer(out), src);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
HashSet<Integer> dst = (HashSet<Integer>)
tmpl.unpack(new Unpacker(in), new HashSet<Integer>());
assertEquals(src.getClass(), dst.getClass());
assertEquals(src.size(), dst.size());
Integer[] src_array = src.toArray(new Integer[0]);
Integer[] dst_array = dst.toArray(new Integer[0]);
for (int j = 0; j < len; j++) {
assertEquals(src_array[j], dst_array[j]);
}
src.clear();
}
}
@SuppressWarnings("unchecked")
@Test
public void testNullCollection() throws Exception {
Collection<String> src = null;
Template tmpl = new CollectionTemplate(StringTemplate.getInstance());
ByteArrayOutputStream out = new ByteArrayOutputStream();
Packer packer = new Packer(out);
try {
tmpl.pack(packer, src);
fail();
} catch (Throwable t) {
assertTrue(t instanceof MessageTypeException);
}
packer.pack(src);
byte[] bytes = out.toByteArray();
Unpacker unpacker = new Unpacker();
unpacker.wrap(bytes);
try {
tmpl.unpack(unpacker, null);
fail();
} catch (Throwable t) {
assertTrue(t instanceof MessageTypeException);
}
unpacker.wrap(bytes);
tmpl = new NullableTemplate(new CollectionTemplate(StringTemplate.getInstance()));
Collection<String> dst = (Collection<String>) tmpl.unpack(unpacker, null);
assertEquals(src, dst); assertEquals(src, dst);
} }
} }