BlinkCodec implements MsgCodec { @Override public Object decode(InputStream in) throws IOException { return decode(new InputStreamSource(in)); } BlinkCodec(Schema schema, Pool<byte[]> bufferPool,
            int maxBinarySize, int maxSequenceLength); @Override void encode(Object group, OutputStream out); @Override void encode(Object group, ByteSink out); @Override Object decode(InputStream in); @Override Object decode(ByteSource in);  }
@Test public void testBrokenHelloDecodeNull() throws Exception { Schema schema = new SchemaBuilder().build(Hello.class); MsgCodec codec = new BlinkCodecFactory(schema).createCodec(); byte[] dataNull = new byte[] { 0x0d, 0x01, (byte)0xc0 }; ByteArrayInputStream in = new ByteArrayInputStream(dataNull); DecodeException decodeError = null; try { codec.decode(in); } catch (DecodeException e) { decodeError = e; } assertNotNull("Decoding error", decodeError); }
@Test public void testBrokenHelloDecodeEof() throws Exception { Schema schema = new SchemaBuilder().build(Hello.class); MsgCodec codec = new BlinkCodecFactory(schema).createCodec(); byte[] dataNull = new byte[] { 0x0d, 0x01 }; ByteArrayInputStream in = new ByteArrayInputStream(dataNull); DecodeException decodeError = null; try { codec.decode(in); } catch (DecodeException e) { decodeError = e; } assertNotNull("Decoding error", decodeError); }
@Test(expected = DecodeException.class) public void testFailDecodeAbstractMessage() throws IOException { Schema schema = new SchemaBuilder().build(AbstractMessage.class); BlinkCodec codec = new BlinkCodecFactory(schema).createCodec(); byte[] blink = new byte[]{0x01, 0x05}; ByteArrayInputStream in = new ByteArrayInputStream(blink); try { codec.decode(in); } catch(Exception e) { e.printStackTrace(); throw e; } }
@Test public void testReadNonReqWithNonNullReference() throws IOException { Schema schema = new SchemaBuilder().build(SingleFieldEntity.class, IntMsg.class); BlinkCodec codec = new BlinkCodecFactory(schema).createCodec(); byte[] data = new byte[]{0x03, 0x09, 0x01, 0x37}; ByteArrayInputStream in = new ByteArrayInputStream(data); SingleFieldEntity entity = (SingleFieldEntity) codec.decode(in); assertNotNull(entity.optionalIntMessage); assertEquals(0x37, entity.optionalIntMessage.value); }
@Test public void testReadNonReqWithNullReference() throws IOException { Schema schema = new SchemaBuilder().build(SingleFieldEntity.class, IntMsg.class); BlinkCodec codec = new BlinkCodecFactory(schema).createCodec(); byte[] data = new byte[]{0x02, 0x09, (byte)0xc0}; ByteArrayInputStream in = new ByteArrayInputStream(data); SingleFieldEntity entity = (SingleFieldEntity) codec.decode(in); assertNull(entity.optionalIntMessage); }
@Test public void testReadNonReqWithCompatibilityNullReference() throws IOException { Schema schema = new SchemaBuilder().build(SingleFieldEntity.class, IntMsg.class); BlinkCodec codec = new BlinkCodecFactory(schema).createCodec(); byte[] data = new byte[]{0x02, 0x09, (byte)0x00}; ByteArrayInputStream in = new ByteArrayInputStream(data); SingleFieldEntity entity = (SingleFieldEntity) codec.decode(in); assertNull(entity.optionalIntMessage); }