CsvParser implements ICsvParser<TEntity> { @Override public Stream<CsvMappingResult<TEntity>> parse(Stream<String> stream) { if(options.getParallel()) { stream = stream.parallel(); } return stream .skip(options.getSkipHeader() ? 1 : 0) .filter(s1 -> !StringUtils.isNullOrWhiteSpace(s1)) .map(s -> options.getTokenizer().tokenize(s)) .map(a -> mapping.map(a)); } CsvParser(CsvParserOptions options, CsvMapping<TEntity> mapping); @Override Stream<CsvMappingResult<TEntity>> parse(Stream<String> stream); Stream<CsvMappingResult<TEntity>> parse(Iterable<String> csvData); Stream<CsvMappingResult<TEntity>> readFromFile(Path path, Charset charset); Stream<CsvMappingResult<TEntity>> readFromString(String csvData, CsvReaderOptions options); @Override String toString();  }
@Test public void testParse() { CsvParserOptions options = new CsvParserOptions(false, ","); PersonMapping mapping = new PersonMapping(() -> new Person()); CsvParser<Person> parser = new CsvParser<>(options, mapping); ArrayList<String> csvData = new ArrayList<>(); csvData.add("Philipp,Wagner,1986-05-12"); csvData.add(""); csvData.add("Max,Musterman,2000-01-07"); List<CsvMappingResult<Person>> result = parser.parse(csvData) .collect(Collectors.toList()); Assert.assertNotNull(result); Assert.assertEquals(2, result.size()); Person person0 = result.get(0).getResult(); Assert.assertEquals("Philipp", person0.firstName); Assert.assertEquals("Wagner", person0.lastName); Assert.assertEquals(1986, person0.getBirthDate().getYear()); Assert.assertEquals(5, person0.getBirthDate().getMonthValue()); Assert.assertEquals(12, person0.getBirthDate().getDayOfMonth()); Person person1 = result.get(1).getResult(); Assert.assertEquals("Max", person1.firstName); Assert.assertEquals("Musterman", person1.lastName); Assert.assertEquals(2000, person1.getBirthDate().getYear()); Assert.assertEquals(1, person1.getBirthDate().getMonthValue()); Assert.assertEquals(7, person1.getBirthDate().getDayOfMonth()); }
@Test public void testParse_Parallel() { CsvParserOptions options = new CsvParserOptions(false, ",", true); PersonMapping mapping = new PersonMapping(() -> new Person()); CsvParser<Person> parser = new CsvParser<>(options, mapping); ArrayList<String> csvData = new ArrayList<>(); csvData.add("Philipp,Wagner,1986-05-12"); csvData.add(""); csvData.add("Max,Musterman,2000-01-07"); List<CsvMappingResult<Person>> result = parser.parse(csvData) .collect(Collectors.toList()); Assert.assertNotNull(result); Assert.assertEquals(2, result.size()); Person person0 = result.get(0).getResult(); Assert.assertEquals("Philipp", person0.firstName); Assert.assertEquals("Wagner", person0.lastName); Assert.assertEquals(1986, person0.getBirthDate().getYear()); Assert.assertEquals(5, person0.getBirthDate().getMonthValue()); Assert.assertEquals(12, person0.getBirthDate().getDayOfMonth()); Person person1 = result.get(1).getResult(); Assert.assertEquals("Max", person1.firstName); Assert.assertEquals("Musterman", person1.lastName); Assert.assertEquals(2000, person1.getBirthDate().getYear()); Assert.assertEquals(1, person1.getBirthDate().getMonthValue()); Assert.assertEquals(7, person1.getBirthDate().getDayOfMonth()); }
@Test public void testParse_WithError() { CsvParserOptions options = new CsvParserOptions(false, ","); PersonMapping mapping = new PersonMapping(() -> new Person()); CsvParser<Person> parser = new CsvParser<>(options, mapping); ArrayList<String> csvData = new ArrayList<>(); csvData.add("Philipp,Wagner,1986-05-12"); csvData.add(""); csvData.add("Max,Musterman,2000-"); List<CsvMappingResult<Person>> result = parser.parse(csvData) .collect(Collectors.toList()); Assert.assertNotNull(result); Assert.assertEquals(2, result.size()); Assert.assertEquals(true, result.get(0).isValid()); Assert.assertEquals(null, result.get(0).getError()); Person person0 = result.get(0).getResult(); Assert.assertEquals("Philipp", person0.firstName); Assert.assertEquals("Wagner", person0.lastName); Assert.assertEquals(1986, person0.getBirthDate().getYear()); Assert.assertEquals(5, person0.getBirthDate().getMonthValue()); Assert.assertEquals(12, person0.getBirthDate().getDayOfMonth()); Assert.assertEquals(false, result.get(1).isValid()); Assert.assertEquals(null, result.get(1).getResult()); CsvMappingError error = result.get(1).getError(); Assert.assertEquals("2000-", error.getValue()); Assert.assertEquals(2, error.getIndex()); Assert.assertEquals( "Conversion failed", error.getMessage()); }