@Override public Integer convertToAvro(String value) { // String to Integer (datum type), then Integer (datum type to Integer (Avro-compatible types) return StringUtils.isEmpty(value) ? null : ParserUtils .parseToShort(ParserUtils.transformNumberString(value, thousandsSepChar, decimalSepChar), isDecode()) .intValue(); } }
@Test public void testParseToShort() throws Exception { assertNull(ParserUtils.parseToShort(null, false)); assertEquals(Short.valueOf("10"), ParserUtils.parseToShort("010", false)); assertEquals(Short.valueOf("8"), ParserUtils.parseToShort("010", true)); assertEquals(Short.valueOf("10"), ParserUtils.parseToShort("10", false)); assertEquals(Short.valueOf("10"), ParserUtils.parseToShort("10", true)); try { ParserUtils.parseToShort("0X10", false); fail("Except get NumberFormatException"); } catch (Exception e) { assertEquals(NumberFormatException.class, e.getClass()); LOG.debug("Except exception:" + e.getMessage()); } assertEquals(Short.valueOf("16"), ParserUtils.parseToShort("0X10", true)); try { ParserUtils.parseToShort("0X12345", true); } catch (Exception e) { assertEquals(NumberFormatException.class, e.getClass()); LOG.debug("Except exception:" + e.getMessage()); } }