@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()); } }
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 { 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 { 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); }
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);  }
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; }