DelimitedRecordMapper extends AbstractRecordMapper<P> implements RecordMapper<String, P> { protected List<Field> parseRecord(final Record<String> record) throws Exception { String payload = record.getPayload(); String[] tokens = payload.split(delimiter, -1); setRecordExpectedLength(tokens); setFieldNames(tokens); checkRecordLength(tokens); checkQualifier(tokens); List<Field> fields = new ArrayList<>(); int index = 0; for (String token : tokens) { token = trimWhitespaces(token); token = removeQualifier(token); fields.add(new Field(index++, token)); } if (fieldsPositions != null) { filterFields(fields); } return fields; } DelimitedRecordMapper(final Class<P> recordClass);  DelimitedRecordMapper(final Class<P> recordClass, String... fieldNames);  DelimitedRecordMapper(final Class<P> recordClass, final String[] fieldNames, final int recordExpectedLength);  DelimitedRecordMapper(final Class<P> recordClass, final Integer... fieldsPositions);  DelimitedRecordMapper(final Class<P> recordClass, final Integer[] fieldsPositions, final String[] fieldNames);  DelimitedRecordMapper(final Class<P> recordClass, final Integer[] fieldsPositions, final String[] fieldNames, final int recordExpectedLength); @Override Record<P> processRecord(final Record<String> record); void setDelimiter(final String delimiter); void setTrimWhitespaces(final boolean trimWhitespaces); void setQualifier(final String qualifier); static final String DEFAULT_DELIMITER; static final String DEFAULT_QUALIFIER; static final boolean DEFAULT_WHITESPACE_TRIMMING; }
@Test(expected = Exception.class) public void testIllFormedRecord() throws Exception { when(record.getPayload()).thenReturn("foo,bar,30,1990-12-12"); delimitedRecordMapper.parseRecord(record); }
@Test public void testRecordSizeWithEmptyField() throws Exception { when(record.getPayload()).thenReturn("foo,bar,30,1990-12-12,"); List<Field> fields = delimitedRecordMapper.parseRecord(record); assertThat(fields.get(4).getRawContent()).isEmpty(); }
@Test public void testFieldSubsetMapping() throws Exception { delimitedRecordMapper = new DelimitedRecordMapper<>(Person.class, new Integer[]{0, 4}, new String[]{"firstName", "married"} ); List<Field> fields = delimitedRecordMapper.parseRecord(record); assertThat(fields).hasSize(2); assertThat(fields).extracting("rawContent") .containsExactly("foo", "true"); }