ExcelRowAggregator implements RowAggregator<ExcelRow> { @Override public ExcelRow aggregate(ExcelRow item, Workbook workbook, int sheetIndex, int rowIndex) throws Exception { final Row row = workbook.getSheetAt(sheetIndex).getRow(rowIndex); BatchExcelRow previousRow = null; Language lang = Language.valueOf(language.toUpperCase()); if (row != null && row.getCell(2) != null) { try { previousRow = batchExcelRowMapper.mapRow(row, rowIndex); } catch (RowMappingException e) { previousRow = null; } if (BatchExcelRow.EMPTY.equals(previousRow)) { previousRow = null; } setDateFormat(row, 2, item.getDate()); setStringFormat(row, 12, getStationName(item.getDepartureStation(), lang)); setStringFormat(row, 18, getStationName(item.getArrivalStation(), lang)); setStringFormat(row, 24, getStationName(item.getLinkStation(), lang)); setHHFormat(row, 30, item.getExpectedDepartureTime()); setMMFormat(row, 32, item.getExpectedDepartureTime()); setHHFormat(row, 33, item.getExpectedArrivalTime()); setMMFormat(row, 35, item.getExpectedArrivalTime()); setNumericFormat(row, 36, getTrainName(item.getExpectedTrainLine1())); setNumericFormat(row, 39, getTrainName(item.getExpectedTrainLine2())); setHHFormat(row, 42, item.getEffectiveDepartureTime()); setMMFormat(row, 44, item.getEffectiveDepartureTime()); setHHFormat(row, 45, item.getEffectiveArrivalTime()); setMMFormat(row, 47, item.getEffectiveArrivalTime()); setNumericFormat(row, 48, getTrainName(item.getEffectiveTrainLine1())); setNumericFormat(row, 51, getTrainName(item.getEffectiveTrainLine2())); evaluateFormula(workbook, row, 56); evaluateFormula(workbook, row, 55); evaluateFormula(workbook, row, 54); } return previousRow; } ExcelRowAggregator(); @Override ExcelRow aggregate(ExcelRow item, Workbook workbook, int sheetIndex, int rowIndex); void setLanguage(String language);  }
@Test public void testAggregateRow() throws Exception { aggregator.aggregate(item, workbook, SHEET_INDEX, ROW_INDEX); Row row = workbook.getSheetAt(SHEET_INDEX).getRow(ROW_INDEX); Assert.assertNotNull(row); Assert.assertEquals(TRAIN1.toString(), row.getCell(36).getStringCellValue()); Assert.assertEquals(TRAIN1.toString(), row.getCell(48).getStringCellValue()); }
@Test public void testXSSFWorkbook() throws Exception { workbook = new XSSFWorkbook(); row = workbook.createSheet().createRow(ROW_INDEX); initRow(row); Assert.assertNotNull(aggregator.aggregate(item, workbook, SHEET_INDEX, ROW_INDEX)); Row row = workbook.getSheetAt(SHEET_INDEX).getRow(ROW_INDEX); Assert.assertNotNull(row); Assert.assertEquals(TRAIN1.toString(), row.getCell(36).getStringCellValue()); Assert.assertEquals(TRAIN1.toString(), row.getCell(48).getStringCellValue()); }
@Test public void testInvalidFormatException() throws Exception { workbook = new SXSSFWorkbook(); row = workbook.createSheet().createRow(ROW_INDEX); initRow(row); Assert.assertNotNull(aggregator.aggregate(item, workbook, SHEET_INDEX, ROW_INDEX)); Row row = workbook.getSheetAt(SHEET_INDEX).getRow(ROW_INDEX); Assert.assertNotNull(row); Assert.assertNotEquals(DELAY, row.getCell(54).getNumericCellValue()); }
@Test public void testInvalidNumericFormat() throws Exception { item = new BatchExcelRow .Builder(LocalDate.now(), Sens.DEPARTURE) .expectedTrain1(new TrainLine.Builder(466L).build()) .build(false); row.createCell(36).setCellValue(1717); Assert.assertNotNull(aggregator.aggregate(item, workbook, SHEET_INDEX, ROW_INDEX)); Row row = workbook.getSheetAt(SHEET_INDEX).getRow(ROW_INDEX); Assert.assertNotNull(row); Assert.assertNotEquals(1717, row.getCell(36).getNumericCellValue()); }
@Test public void testEmptyRow() throws Exception { workbook.createSheet().createRow(2); Assert.assertNull(aggregator.aggregate(item, workbook, SHEET_INDEX, 2)); }
@Test public void testCannotMapRowIndex() throws Exception { Assert.assertNull(aggregator.aggregate(item, workbook, SHEET_INDEX, 2)); Assert.assertNull(workbook.getSheetAt(SHEET_INDEX).getRow(2)); }
@Test public void testRoundTrip() throws Exception { BatchExcelRow batchExcelRow = new BatchExcelRowMapper().mapRow(workbook.getSheetAt(SHEET_INDEX).getRow(ROW_INDEX), ROW_INDEX); ExcelRow previousRow = aggregator.aggregate(item, workbook, SHEET_INDEX, ROW_INDEX); Assert.assertNotNull(previousRow); Assert.assertNotEquals(item, previousRow); Assert.assertNotNull(batchExcelRow); Assert.assertEquals(previousRow, batchExcelRow); }