BaseCsvImporter extends BaseTextImporter { protected List<String[]> readCsvFile(byte[] bytes, String separator, String quote) { try (CharSequenceReader seq = new CharSequenceReader(new String(bytes, StandardCharsets.UTF_8)); CSVReader reader = new CSVReaderBuilder(seq) .withCSVParser(new CSVParserBuilder().withSeparator(separator.charAt(0)).withQuoteChar(quote.charAt(0)).build()) .build()) { return reader.readAll(); } catch (IOException | CsvException ex) { throw new OCSImportException(ex.getMessage(), ex); } }  @Override int countRows(byte[] bytes, int sheetIndex);  }
@Test public void testReadFile() throws IOException { byte[] bytes = readFile("importertest.csv"); List<String[]> lines = importer.readCsvFile(bytes, ";", "'"); PersonDTO dto = importer.processRow(0, lines.get(0), PersonDTO.class); assertNotNull(dto); assertEquals("Bas", dto.getName()); assertEquals(1, dto.getNumber().intValue()); assertEquals(2.4, dto.getFactor().doubleValue(), 0.001); assertEquals("abc", dto.getRandom()); assertEquals(Gender.M, dto.getGender()); assertEquals(1.50, dto.getPercentage().doubleValue(), 0.001); assertEquals(6.66, dto.getRating().doubleValue(), 0.001); dto = importer.processRow(1, lines.get(1), PersonDTO.class); assertNotNull(dto); assertEquals("Unknown", dto.getName()); assertEquals(2, dto.getNumber().intValue()); assertEquals(1.0, dto.getFactor().doubleValue(), 0.001); dto = importer.processRow(1, lines.get(2), PersonDTO.class); assertNotNull(dto); assertEquals("Endy", dto.getName()); assertEquals(-3, dto.getNumber().intValue()); assertEquals(DateUtils.createLocalDate("01012015"), dto.getDate()); }
@Test public void testReadFile_NotNumeric() throws IOException { try { byte[] bytes = readFile("importertest_wrongnumeric.csv"); List<String[]> lines = importer.readCsvFile(bytes, ";", "'"); importer.processRow(0, lines.get(0), PersonDTO.class); fail(); } catch (OCSImportException ex) { assertEquals("abc cannot be converted to a number", ex.getMessage()); } }