CSVParser { protected DataSet load() throws Exception { InputStream is = getCSVInputStream(); try { BufferedReader br = new BufferedReader(new InputStreamReader(is)); CSVReader csvReader = new CSVReader(br, dataSetDef.getSeparatorChar(), dataSetDef.getQuoteChar(), dataSetDef.getEscapeChar()); String[] header = csvReader.readNext(); if (header == null) throw new IOException("CSV has no header: " + dataSetDef); String[] firstRow = csvReader.readNext(); if (firstRow != null && firstRow.length < header.length) { String exceptionMessage = String.format("CSV parse error : The first row has fewer columns (%d) than the header (%d)" , firstRow.length, header.length); throw new IllegalArgumentException(exceptionMessage); } List<Integer> _columnIdxs = new ArrayList<Integer>(); DataSet dataSet = DataSetFactory.newEmptyDataSet(); for (int i = 0; i < header.length; i++) { String columnId = header[i]; if (isColumnIncluded(columnId)) { ColumnType type = ColumnType.LABEL; if (firstRow != null) type = calculateType(columnId, firstRow[i]); dataSet.addColumn(columnId, type); _columnIdxs.add(i); } } if (firstRow != null) { Object[] _rowArray = new Object[dataSet.getColumns().size()]; _processLine(dataSet, _rowArray, firstRow, _columnIdxs); dataSet.setValuesAt(dataSet.getRowCount(), _rowArray); String[] _line = csvReader.readNext(); while (_line != null && _line.length == header.length) { _processLine(dataSet,_rowArray, _line, _columnIdxs); dataSet.setValuesAt(dataSet.getRowCount(), _rowArray); _line = csvReader.readNext(); } } return dataSet; } finally { is.close(); } } CSVParser(CSVDataSetDef def, CSVFileStorage csvFileStorage);  static final String DATE_FORMAT_EPOCH; }
@Test public void exceptionThrown_whenFirstLineHasLessFieldsThanHeader() throws Exception { final String CSV_DATA = "'Name','Age'\n'Jan''15'"; CSVFileStorage mockStorage = new MockCSVFileStorage(CSV_DATA); CSVParser testedParser = new CSVParser(csvDataSet, mockStorage); try { DataSet dataSetShouldNotLoad = testedParser.load(); Assert.fail("IllegalArgumentException should be thrown when 1st row of CSV data has less fields than the header"); } catch (IllegalArgumentException iae) { String expectedExceptionMessage = "CSV parse error : The first row has fewer columns (1) than the header (2)"; assertEquals(expectedExceptionMessage, iae.getMessage()); } }
@Test public void dataSetCreated_whenCsvDataValid() throws Exception { final String CSV_DATA = "'Name','Weight','Date of birth'\n'Jan','75.64','1950-01-20'"; CSVFileStorage mockStorage = new MockCSVFileStorage(CSV_DATA); CSVParser testedParser = new CSVParser(csvDataSet, mockStorage); DataSet dset = testedParser.load(); assertEquals(1, dset.getRowCount()); assertEquals(ColumnType.LABEL, dset.getColumnById("Name").getColumnType()); assertEquals(ColumnType.NUMBER, dset.getColumnById("Weight").getColumnType()); assertEquals(ColumnType.DATE, dset.getColumnById("Date of birth").getColumnType()); }
@Test public void stringFieldStartingWithNumber_shouldBeParsedAsLabel() throws Exception { final String CSV_DATA = "Age,Address\n" + "25,12 Downing street\n" + "75,White House 52"; CSVFileStorage mockStorage = new MockCSVFileStorage(CSV_DATA); CSVParser testedParser = new CSVParser(csvDataSet, mockStorage); DataSet dset = testedParser.load(); assertEquals(2, dset.getRowCount()); assertEquals(ColumnType.NUMBER, dset.getColumnById("Age").getColumnType()); assertEquals(ColumnType.LABEL, dset.getColumnById("Address").getColumnType()); }