@SuppressWarnings("unchecked") @Test public void testMap() throws Exception { Map<Integer, Integer> emptyMap = new HashMap<Integer, Integer>(); { ByteArrayOutputStream out = new ByteArrayOutputStream(); new Packer(out).pack(emptyMap); ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); Template tmpl = new MapTemplate(IntegerTemplate.getInstance(), IntegerTemplate.getInstance()); Map<Integer, Integer> dst = (Map<Integer, Integer>) tmpl.unpack(new Unpacker(in)); 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)); assertEquals(len, map.size()); for (Map.Entry<Integer, Integer> pair : map.entrySet()) { Integer val = m.get(pair.getKey()); assertNotNull(val); assertEquals(val, pair.getValue()); } } for (int i = 0; i < 1000; i++) { Map<String, Integer> m = new HashMap<String, Integer>(); int len = (int) Math.random() % 1000 + 1; for (int j = 0; j < len; j++) { m.put(Integer.toString(j), j); } ByteArrayOutputStream out = new ByteArrayOutputStream(); new Packer(out).pack(m); ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); Template tmpl = new MapTemplate(StringTemplate.getInstance(), IntegerTemplate.getInstance()); Map<String, Integer> map = (Map<String, Integer>) tmpl.unpack(new Unpacker(in)); assertEquals(m.size(), map.size()); for (Map.Entry<String, Integer> pair : map.entrySet()) { Integer val = m.get(pair.getKey()); assertNotNull(val); assertEquals(val, pair.getValue()); } } }
static void _testInteger(Integer src) throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); new Packer(out).pack(src); ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); Template tmpl = IntegerTemplate.getInstance(); Integer dst = (Integer) tmpl.unpack(new Unpacker(in)); assertEquals(src, dst); }
@SuppressWarnings("unchecked") @Test public void testList() throws Exception { List<Integer> emptyList = new ArrayList<Integer>(); { ByteArrayOutputStream out = new ByteArrayOutputStream(); new Packer(out).pack(emptyList); ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); Template tmpl = new ListTemplate(IntegerTemplate.getInstance()); List<Integer> dst = (List<Integer>) tmpl.unpack(new Unpacker(in)); assertEquals(emptyList, dst); } for (int i = 0; i < 1000; i++) { List<Integer> l = new ArrayList<Integer>(); int len = (int) Math.random() % 1000 + 1; for (int j = 0; j < len; j++) { l.add(j); } ByteArrayOutputStream out = new ByteArrayOutputStream(); new Packer(out).pack(l); ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); Template tmpl = new ListTemplate(IntegerTemplate.getInstance()); List<Integer> dst = (List<Integer>) tmpl.unpack(new Unpacker(in)); assertEquals(len, dst.size()); for (int j = 0; j < len; j++) { assertEquals(l.get(j), dst.get(j)); } } 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)); assertEquals(len, dst.size()); for (int j = 0; j < len; j++) { assertEquals(l.get(j), dst.get(j)); } } }
@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); }