ParserUtils { public static List<String> parseColumnDataType(String columnDataTypeString) { List<String> typePrecisionScale = new ArrayList<>(3); Pattern pattern = Pattern.compile(DATA_TYPE_REGEX); Matcher matcher = pattern.matcher(columnDataTypeString); if (matcher.find()) { typePrecisionScale.add(matcher.group(1)); typePrecisionScale.add(matcher.group(3)); typePrecisionScale.add(matcher.group(5)); }else{ typePrecisionScale.add(null); typePrecisionScale.add(null); typePrecisionScale.add(null); } return typePrecisionScale; }  static List<String> parseColumnDataType(String columnDataTypeString); static final String DATA_TYPE_REGEX; }
@Test public void parseColumnDataTypeDecimalTest(){ String columnDataTypeString = " decimal(10,2) "; List<String> list = ParserUtils.parseColumnDataType(columnDataTypeString); Assert.assertEquals("Must contain 3 elements : ", 3, list.size()); Assert.assertEquals("Failed to find datatype. ", "decimal", list.get(0)); Assert.assertEquals("Failed to find precision. ", "10", list.get(1)); Assert.assertEquals("Failed to find scale. ", "2", list.get(2)); }
@Test public void parseColumnDataTypeDecimalWithSpaceTest(){ String columnDataTypeString = " decimal ( 10 , 2 ) "; List<String> list = ParserUtils.parseColumnDataType(columnDataTypeString); Assert.assertEquals("Must contain 3 elements : ", 3, list.size()); Assert.assertEquals("Failed to find datatype. ", "decimal", list.get(0)); Assert.assertEquals("Failed to find precision. ", "10", list.get(1)); Assert.assertEquals("Failed to find scale. ", "2", list.get(2)); }
@Test public void parseColumnDataTypeVarcharTest(){ String columnDataTypeString = " VARCHAR( 10) "; List<String> list = ParserUtils.parseColumnDataType(columnDataTypeString); Assert.assertEquals("Must contain 2 elements : ", 3, list.size()); Assert.assertEquals("Failed to find datatype. ", "VARCHAR", list.get(0)); Assert.assertEquals("Failed to find precision. ", "10", list.get(1)); Assert.assertNull("Scale should be null. ", list.get(2)); }
@Test public void parseColumnDataTypeBooleanTest(){ String columnDataTypeString = " BOOLEAN "; List<String> list = ParserUtils.parseColumnDataType(columnDataTypeString); Assert.assertEquals("Must contain 1 elements : ", 3, list.size()); Assert.assertEquals("Failed to find datatype. ", "BOOLEAN", list.get(0)); Assert.assertNull("Precision should be null. ", list.get(1)); Assert.assertNull("Scale should be null. ", list.get(2)); }