XLSBatchEngineImportTaskItemReader implements BatchEngineImportTaskItemReader { @Override public Map<String, Object> read() throws Exception { if (!_iterator.hasNext()) { return null; } Row row = _iterator.next(); Map<String, Object> fieldNameValueMap = new HashMap<>(); int index = 0; for (Cell cell : row) { String fieldName = _fieldNames[index++]; if (fieldName == null) { continue; } if (CellType.BOOLEAN == cell.getCellType()) { fieldNameValueMap.put(fieldName, cell.getBooleanCellValue()); } else if (CellType.NUMERIC == cell.getCellType()) { if (DateUtil.isCellDateFormatted(cell)) { fieldNameValueMap.put(fieldName, cell.getDateCellValue()); } else { fieldNameValueMap.put( fieldName, cell.getNumericCellValue()); } } else { String value = cell.getStringCellValue(); value = value.trim(); if (value.isEmpty()) { value = null; } int lastDelimiterIndex = fieldName.lastIndexOf('_'); if (lastDelimiterIndex == -1) { fieldNameValueMap.put(fieldName, value); } else { BatchEngineImportTaskItemReaderUtil.handleMapField( fieldName, fieldNameValueMap, lastDelimiterIndex, value); } } } return fieldNameValueMap; } XLSBatchEngineImportTaskItemReader(InputStream inputStream); @Override void close(); @Override Map<String, Object> read();  }
@Test public void testColumnMapping() throws Exception { try (XLSBatchEngineImportTaskItemReader xlsBatchEngineImportTaskItemReader = _getXLSBatchEngineImportTaskItemReader( new String[] { "createDate1", "description1", "id1", "name1_en", "name1_hr" }, new Object[][] { { createDateString, "sample description", 1, "sample name", "naziv" } })) { validate( createDateString, "sample description", 1L, HashMapBuilder.put( "createDate1", "createDate" ).put( "description1", "description" ).put( "id1", "id" ).put( "name1", "name" ).build(), xlsBatchEngineImportTaskItemReader.read(), HashMapBuilder.put( "en", "sample name" ).put( "hr", "naziv" ).build()); } }
@Test public void testColumnMappingWitUndefinedColumn() throws Exception { try (XLSBatchEngineImportTaskItemReader xlsBatchEngineImportTaskItemReader = _getXLSBatchEngineImportTaskItemReader( new String[] { "createDate1", "description1", "id1", "name1_en", "name1_hr" }, new Object[][] { { createDateString, "sample description", 1, "sample name", "naziv" } })) { validate( createDateString, "sample description", 1L, HashMapBuilder.put( "createDate1", "createDate" ).put( "description1", "description" ).put( "id1", "id" ).build(), xlsBatchEngineImportTaskItemReader.read(), null); } }
@Test public void testColumnMappingWitUndefinedTargetColumn() throws Exception { try (XLSBatchEngineImportTaskItemReader xlsBatchEngineImportTaskItemReader = _getXLSBatchEngineImportTaskItemReader( new String[] { "createDate1", "description1", "id1", "name1_en", "name1_hr" }, new Object[][] { { createDateString, "sample description", 1, "sample name", "naziv" } })) { validate( createDateString, "sample description", 1L, new HashMap<String, String>() { { put("createDate1", "createDate"); put("description1", "description"); put("id1", "id"); put("name1", null); } }, xlsBatchEngineImportTaskItemReader.read(), null); } }
@Test public void testInvalidColumnMapping() throws Exception { try (XLSBatchEngineImportTaskItemReader xlsBatchEngineImportTaskItemReader = _getXLSBatchEngineImportTaskItemReader( new String[] { "createDate1", "description1", "id1", "name1_en", "name1_hr" }, new Object[][] { { createDateString, "sample description", 1, "sample name", "naziv" } })) { try { validate( createDateString, "sample description", null, HashMapBuilder.put( "createDate1", "description" ).put( "description1", "createDate" ).put( "id1", "id" ).put( "name1", "name" ).build(), xlsBatchEngineImportTaskItemReader.read(), HashMapBuilder.put( "en", "sample name" ).put( "hr", "naziv" ).build()); Assert.fail(); } catch (IllegalArgumentException illegalArgumentException) { } } }
@Test public void testReadInvalidRow() throws Exception { try (XLSBatchEngineImportTaskItemReader xlsBatchEngineImportTaskItemReader = _getXLSBatchEngineImportTaskItemReader( FIELD_NAMES, new Object[][] { { new Date(), "sample description", 1L, "sample name", "naziv", "unknown column" } })) { try { xlsBatchEngineImportTaskItemReader.read(); Assert.fail(); } catch (ArrayIndexOutOfBoundsException arrayIndexOutOfBoundsException) { } } }
@Test public void testReadMultipleRows() throws Exception { try (XLSBatchEngineImportTaskItemReader xlsBatchEngineImportTaskItemReader = _getXLSBatchEngineImportTaskItemReader( FIELD_NAMES, new Object[][] { { createDate, "sample description 1", 1L, "sample name 1", "naziv 1" }, { createDate, "sample description 2", 2L, "sample name 2", "naziv 2" } })) { for (int i = 1; i < 3; i++) { long rowCount = i; validate( createDateString, "sample description " + rowCount, rowCount, Collections.emptyMap(), xlsBatchEngineImportTaskItemReader.read(), HashMapBuilder.put( "en", "sample name " + rowCount ).put( "hr", "naziv " + rowCount ).build()); } } }
@Test public void testReadRowsWithCommaInsideQuotes() throws Exception { try (XLSBatchEngineImportTaskItemReader xlsBatchEngineImportTaskItemReader = _getXLSBatchEngineImportTaskItemReader( FIELD_NAMES, new Object[][] { { createDate, "hey, here is comma inside", 1L, "sample name", "naziv" } })) { validate( createDateString, "hey, here is comma inside", 1L, Collections.emptyMap(), xlsBatchEngineImportTaskItemReader.read(), HashMapBuilder.put( "en", "sample name" ).put( "hr", "naziv" ).build()); } }
@Test public void testReadRowsWithLessValues() throws Exception { try (XLSBatchEngineImportTaskItemReader xlsBatchEngineImportTaskItemReader = _getXLSBatchEngineImportTaskItemReader( FIELD_NAMES, new Object[][] {{null, null, 1}})) { validate( null, null, 1L, Collections.emptyMap(), xlsBatchEngineImportTaskItemReader.read(), null); } }
@Test public void testReadRowsWithNullValues() throws Exception { try (XLSBatchEngineImportTaskItemReader xlsBatchEngineImportTaskItemReader = _getXLSBatchEngineImportTaskItemReader( FIELD_NAMES, new Object[][] { {createDate, null, 1L, null, "naziv"}, { createDate, "sample description 2", 2L, "sample name 2", "naziv 2" } })) { validate( createDateString, null, 1L, Collections.emptyMap(), xlsBatchEngineImportTaskItemReader.read(), new HashMap<String, String>() { { put("en", null); put("hr", "naziv"); } }); validate( createDateString, "sample description 2", 2L, Collections.emptyMap(), xlsBatchEngineImportTaskItemReader.read(), HashMapBuilder.put( "en", "sample name 2" ).put( "hr", "naziv 2" ).build()); } }