Bin2Json { public JsonObject convert(String hexString) throws ConversionException { try { return convert(HexUtils.hexToBytes(hexString)); } catch (IllegalArgumentException e) { throw new ConversionException(String.format("Illegal hexstring , reason: %s", e.getMessage(), e)); } } Bin2Json(String parserRule); JsonObject convert(String hexString); JsonObject convert(byte[] data); JsonObject convert(InputStream inputStream);  }
@Test public void testParserRuleError() throws ConversionException { assertThrows(ConversionException.class, () -> new Bin2Json("byte a byte b ubyte c;").convert(new byte[] { 3, 34, (byte) 255 })); }
@Test public void testHexStringData() throws ConversionException { JsonObject json = new Bin2Json("byte a; byte b; ubyte c;").convert("03FAFF"); assertEquals("{\"a\":3,\"b\":-6,\"c\":255}", json.toString()); }
@Test public void testHexStringDataError() throws ConversionException { assertThrows(ConversionException.class, () -> new Bin2Json("byte a; byte b; ubyte c;").convert("0322F")); }
@Test public void testByteArrayData() throws ConversionException { JsonObject json = new Bin2Json("ubyte length; ubyte[length] data;") .convert(new byte[] { 4, 8, 33, 1, 2, 3, 4 }); assertEquals("{\"length\":4,\"data\":[8,33,1,2]}", json.toString()); }
@Test public void testByteArrayDataError() throws ConversionException { assertThrows(ConversionException.class, () -> new Bin2Json("byte a; byte b; ubyte c;").convert(new byte[] { 3, 34 })); }
@Test public void testInputStreamData() throws ConversionException, IOException { InputStream inputStream = new ByteArrayInputStream(new byte[] { 4, 8, 33, 1, 2, 3, 4 }); JsonObject json = new Bin2Json("ubyte length; ubyte[length] data;").convert(inputStream); assertEquals("{\"length\":4,\"data\":[8,33,1,2]}", json.toString()); }
@Test public void testInputStreamDataError() throws ConversionException { InputStream inputStream = new ByteArrayInputStream(new byte[] { 4, 8, 33 }); assertThrows(ConversionException.class, () -> new Bin2Json("ubyte length; ubyte[length] data;").convert(inputStream)); }