Ejemplo n.º 1
0
 @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);
 }
Ejemplo n.º 2
0
 static void _testLong(Long src) throws Exception {
   ByteArrayOutputStream out = new ByteArrayOutputStream();
   new Packer(out).pack(src);
   ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
   Template tmpl = LongTemplate.getInstance();
   Long dst = (Long) tmpl.unpack(new Unpacker(in));
   assertEquals(src, dst);
 }
Ejemplo n.º 3
0
 static void _testBoolean(Boolean src) throws Exception {
   ByteArrayOutputStream out = new ByteArrayOutputStream();
   new Packer(out).pack(src);
   ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
   Template tmpl = BooleanTemplate.getInstance();
   Boolean dst = (Boolean) tmpl.unpack(new Unpacker(in));
   assertEquals(src, dst);
 }
Ejemplo n.º 4
0
 static void _testDouble(Double src) throws Exception {
   ByteArrayOutputStream out = new ByteArrayOutputStream();
   new Packer(out).pack(src);
   ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
   Template tmpl = DoubleTemplate.getInstance();
   Double dst = (Double) tmpl.unpack(new Unpacker(in));
   assertEquals(src, dst, 10e-10);
 }
Ejemplo n.º 5
0
 static void _testBigInteger(BigInteger src) throws Exception {
   ByteArrayOutputStream out = new ByteArrayOutputStream();
   new Packer(out).pack((Object) src);
   ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
   Template tmpl = BigIntegerTemplate.getInstance();
   BigInteger dst = (BigInteger) tmpl.unpack(new Unpacker(in));
   assertEquals(src, dst);
 }
Ejemplo n.º 6
0
 public void pack(Packer pk, Object target) throws IOException {
   if (messagePackable) {
     if (target == null) {
       throw new MessageTypeException("target is null.");
     }
     ((MessagePackable) target).messagePack(pk);
     return;
   }
   Template tmpl = TemplateRegistry.tryLookup(lookupType);
   if (tmpl == this || tmpl == null) {
     throw new MessageTypeException("Template lookup fail: " + lookupType);
   }
   tmpl.pack(pk, target);
 }
Ejemplo n.º 7
0
 public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
   if (messageConvertable) {
     if (to == null) {
       try {
         to = targetClass.newInstance();
       } catch (Exception e) {
         throw new MessageTypeException(e);
       }
     }
     ((MessageConvertable) to).messageConvert(from);
     return from;
   }
   Template tmpl = TemplateRegistry.tryLookup(lookupType);
   if (tmpl == this || tmpl == null) {
     throw new MessageTypeException("Template lookup fail: " + lookupType);
   }
   return tmpl.convert(from, to);
 }
Ejemplo n.º 8
0
 public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
   if (messageUnpackable) {
     if (to == null) {
       try {
         to = targetClass.newInstance();
       } catch (Exception e) {
         throw new MessageTypeException(e);
       }
     }
     ((MessageUnpackable) to).messageUnpack(pac);
     return to;
   }
   Template tmpl = TemplateRegistry.tryLookup(lookupType);
   if (tmpl == this || tmpl == null) {
     throw new MessageTypeException("Template lookup fail: " + lookupType);
   }
   return tmpl.unpack(pac, to);
 }
Ejemplo n.º 9
0
 @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);
 }
Ejemplo n.º 10
0
  @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());
      }
    }
  }
Ejemplo n.º 11
0
  @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));
      }
    }
  }