BaseXlsImporter extends BaseImporter<Row, Cell> { public Workbook createWorkbook(byte[] bytes) { Workbook workbook = null; try { workbook = new HSSFWorkbook(new ByteArrayInputStream(bytes)); } catch (OfficeXmlFileException ex) { try { workbook = new XSSFWorkbook(new ByteArrayInputStream(bytes)); } catch (IOException e) { throw new OCSRuntimeException(e.getMessage(), e); } } catch (IOException e) { throw new OCSRuntimeException(e.getMessage(), e); } return workbook; }  @Override int countRows(byte[] bytes, int sheetIndex); Workbook createReader(byte[] bytes, int cacheSize); Workbook createWorkbook(byte[] bytes); @Override Double getNumericValueWithDefault(Cell cell, ImportField field); @Override boolean isPercentageCorrectionSupported(); boolean isRowEmpty(Row row); T processRows(Sheet sheet, int firstRowIndex, int colIndex, Class<T> clazz);  }
@Test public void testCreateWorkbookBoolean() throws IOException { byte[] bytes = readFile("importertest_boolean.xlsx"); Workbook wb = importer.createWorkbook(bytes); Sheet sheet = wb.getSheetAt(0); PersonDTO dto = importer.processRow(0, sheet.getRow(0), PersonDTO.class); assertNotNull(dto); assertTrue(dto.getAbool()); dto = importer.processRow(1, sheet.getRow(1), PersonDTO.class); assertNotNull(dto); assertFalse(dto.getAbool()); dto = importer.processRow(6, sheet.getRow(6), PersonDTO.class); assertNotNull(dto); assertFalse(dto.getAbool()); }
@Test public void testCreateWorkbook() throws IOException { byte[] bytes = readFile("importertest.xlsx"); Workbook wb = importer.createWorkbook(bytes); Sheet sheet = wb.getSheetAt(0); PersonDTO dto = importer.processRow(0, sheet.getRow(0), PersonDTO.class); assertNotNull(dto); assertEquals(0, dto.getRowNum()); 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); assertTrue(dto.getAbool()); assertEquals(DateUtils.createLocalDate("04042014"), dto.getDate()); assertEquals(6.66, dto.getRating().doubleValue(), 0.001); dto = importer.processRow(1, sheet.getRow(1), PersonDTO.class); assertNotNull(dto); assertEquals(1, dto.getRowNum()); assertEquals("Unknown", dto.getName()); assertEquals(2, dto.getNumber().intValue()); assertEquals(1.0, dto.getFactor().doubleValue(), 0.001); assertFalse(dto.getAbool()); assertEquals(DateUtils.createLocalDate("05052015"), dto.getDate()); dto = importer.processRow(2, sheet.getRow(2), PersonDTO.class); assertNotNull(dto); assertEquals(2, dto.getRowNum()); assertEquals("Endy", dto.getName()); assertEquals(-3, dto.getNumber().intValue()); assertEquals(DateUtils.createLocalDate("01012015"), dto.getDate()); }
@Test public void testCreateWorkbook_WrongNumericValue() throws IOException { byte[] bytes = readFile("importertest_wrongnumeric.xlsx"); Workbook wb = importer.createWorkbook(bytes); Sheet sheet = wb.getSheetAt(0); try { importer.processRow(0, sheet.getRow(0), PersonDTO.class); } catch (OCSImportException ex) { assertEquals("Found an invalid numeric value: xyz", ex.getMessage()); } }
@Test public void testCreateWorkbook_NotEnoughRows() throws IOException { byte[] bytes = readFile("importertest _notenoughrows.xlsx"); Workbook wb = importer.createWorkbook(bytes); Sheet sheet = wb.getSheetAt(0); try { importer.processRow(0, sheet.getRow(0), PersonDTO.class); } catch (OCSImportException ex) { assertEquals("Row doesn't have enough columns", ex.getMessage()); } }
@Test public void testCreateWorkbook_RequiredValueMissing() throws IOException { byte[] bytes = readFile("importertest_required_missing.xlsx"); Workbook wb = importer.createWorkbook(bytes); Sheet sheet = wb.getSheetAt(0); try { importer.processRow(0, sheet.getRow(1), PersonDTO.class); } catch (OCSImportException ex) { assertEquals("Required value for field 'number' is missing", ex.getMessage()); } }
@Test public void testCreateWorkbook_CannotBeNegative() throws IOException { byte[] bytes = readFile("importertest_notnegative.xlsx"); Workbook wb = importer.createWorkbook(bytes); Sheet sheet = wb.getSheetAt(0); try { importer.processRow(0, sheet.getRow(1), PersonDTO.class); } catch (OCSImportException ex) { assertEquals("Negative value -1.2 found for field 'factor'", ex.getMessage()); } }