diff --git a/README.adoc b/README.adoc index 38329f9..2f446df 100644 --- a/README.adoc +++ b/README.adoc @@ -710,7 +710,7 @@ The `mandatoryHeader` field is compatible with XLS and XLSX files. [NOTE] ==== -The `mandatoryCell` field works **only** with XLS files. +The `mandatoryCell` field works **only** with XLS files and `Sheet` instances. XLS workbooks are opened with `RETURN_BLANK_AS_NULL` missing cell policy. If passing a `Sheet` instance it is up for the caller to make sure the missing cell policy of the parent workbook is set accordingly. ==== === Debug Cells Formats diff --git a/src/main/java/com/poiji/bind/mapping/HSSFUnmarshallerFile.java b/src/main/java/com/poiji/bind/mapping/HSSFUnmarshallerFile.java index 077eb8e..a30777d 100644 --- a/src/main/java/com/poiji/bind/mapping/HSSFUnmarshallerFile.java +++ b/src/main/java/com/poiji/bind/mapping/HSSFUnmarshallerFile.java @@ -3,6 +3,7 @@ import com.poiji.bind.PoijiFile; import com.poiji.exception.PoijiException; import com.poiji.option.PoijiOptions; +import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; @@ -24,7 +25,9 @@ final class HSSFUnmarshallerFile extends HSSFUnmarshaller { @Override protected Workbook workbook() { try { - return WorkbookFactory.create(poijiFile.file(), options.getPassword(), true); + Workbook workbook = WorkbookFactory.create(poijiFile.file(), options.getPassword(), true); + workbook.setMissingCellPolicy(Row.MissingCellPolicy.RETURN_BLANK_AS_NULL); + return workbook; } catch (IOException e) { throw new PoijiException("Problem occurred while creating HSSFWorkbook", e); } diff --git a/src/main/java/com/poiji/bind/mapping/HSSFUnmarshallerStream.java b/src/main/java/com/poiji/bind/mapping/HSSFUnmarshallerStream.java index 9683d7e..3ed33c7 100644 --- a/src/main/java/com/poiji/bind/mapping/HSSFUnmarshallerStream.java +++ b/src/main/java/com/poiji/bind/mapping/HSSFUnmarshallerStream.java @@ -3,6 +3,7 @@ import com.poiji.bind.PoijiInputStream; import com.poiji.exception.PoijiException; import com.poiji.option.PoijiOptions; +import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; @@ -23,12 +24,14 @@ final class HSSFUnmarshallerStream extends HSSFUnmarshaller { @Override protected Workbook workbook() { try { - + Workbook workbook; if (options.getPassword() != null) { - return WorkbookFactory.create(poijiInputStream.stream(), options.getPassword()); + workbook = WorkbookFactory.create(poijiInputStream.stream(), options.getPassword()); + } else { + workbook = WorkbookFactory.create(poijiInputStream.stream()); } - - return WorkbookFactory.create(poijiInputStream.stream()); + workbook.setMissingCellPolicy(Row.MissingCellPolicy.RETURN_BLANK_AS_NULL); + return workbook; } catch (IOException e) { throw new PoijiException("Problem occurred while creating HSSFWorkbook", e); } diff --git a/src/test/java/com/poiji/deserialize/MandatoryCellsExceptionTest.java b/src/test/java/com/poiji/deserialize/MandatoryCellsExceptionTest.java index 0571b9f..14be6e1 100644 --- a/src/test/java/com/poiji/deserialize/MandatoryCellsExceptionTest.java +++ b/src/test/java/com/poiji/deserialize/MandatoryCellsExceptionTest.java @@ -2,18 +2,20 @@ import com.poiji.bind.Poiji; import com.poiji.deserialize.model.byname.MandatoryMissingCells; +import com.poiji.exception.PoijiExcelType; import com.poiji.exception.PoijiMultiRowException; import com.poiji.exception.PoijiMultiRowException.PoijiRowSpecificException; import com.poiji.option.PoijiOptions; - import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.usermodel.XSSFSheet; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.junit.Test; -import java.io.IOException; +import java.io.*; import java.util.List; import static org.junit.Assert.assertEquals; @@ -22,7 +24,7 @@ public class MandatoryCellsExceptionTest { @Test - public void testExcelMandatoryColumn() { + public void shouldThrowExceptionForMissingCell() { try { Poiji.fromExcel(createDummyExcel(), MandatoryMissingCells.class, PoijiOptions.PoijiOptionsBuilder .settings() @@ -38,6 +40,58 @@ public void testExcelMandatoryColumn() { fail("Expected exception: " + PoijiMultiRowException.class.getName()); } + @Test + public void shouldThrowExceptionForBlankCellInSheet() throws IOException { + try (InputStream stream = new FileInputStream("src/test/resources/blank-cell.xlsx"); + XSSFWorkbook workbook = new XSSFWorkbook(stream)) { + workbook.setMissingCellPolicy(Row.MissingCellPolicy.RETURN_BLANK_AS_NULL); + XSSFSheet sheet = workbook.getSheetAt(0); + Poiji.fromExcel(sheet, MandatoryMissingCells.class, PoijiOptions.PoijiOptionsBuilder + .settings() + .build()); + } catch (PoijiMultiRowException e) { + List errors = e.getErrors(); + assertEquals(1, errors.size()); + assertEquals("Address", errors.get(0).getColumnName()); + assertEquals("address", errors.get(0).getFieldName()); + assertEquals((Integer) 1, errors.get(0).getRowNum()); + return; + } + fail("Expected exception: " + PoijiMultiRowException.class.getName()); + } + + @Test + public void shouldThrowExceptionForBlankCellInFile() { + try { + Poiji.fromExcel(new File("src/test/resources/blank-cell.xls"), MandatoryMissingCells.class, + PoijiOptions.PoijiOptionsBuilder.settings().build()); + } catch (PoijiMultiRowException e) { + List errors = e.getErrors(); + assertEquals(1, errors.size()); + assertEquals("Address", errors.get(0).getColumnName()); + assertEquals("address", errors.get(0).getFieldName()); + assertEquals((Integer) 1, errors.get(0).getRowNum()); + return; + } + fail("Expected exception: " + PoijiMultiRowException.class.getName()); + } + + @Test + public void shouldThrowExceptionForBlankCellInStream() throws IOException { + try (InputStream stream = new FileInputStream("src/test/resources/blank-cell.xls")) { + Poiji.fromExcel(stream, PoijiExcelType.XLS, MandatoryMissingCells.class, + PoijiOptions.PoijiOptionsBuilder.settings().build()); + } catch (PoijiMultiRowException e) { + List errors = e.getErrors(); + assertEquals(1, errors.size()); + assertEquals("Address", errors.get(0).getColumnName()); + assertEquals("address", errors.get(0).getFieldName()); + assertEquals((Integer) 1, errors.get(0).getRowNum()); + return; + } + fail("Expected exception: " + PoijiMultiRowException.class.getName()); + } + private Sheet createDummyExcel() { Workbook workbook = new HSSFWorkbook(); diff --git a/src/test/resources/blank-cell.xls b/src/test/resources/blank-cell.xls new file mode 100644 index 0000000..b11c0b3 Binary files /dev/null and b/src/test/resources/blank-cell.xls differ diff --git a/src/test/resources/blank-cell.xlsx b/src/test/resources/blank-cell.xlsx new file mode 100644 index 0000000..d89082f Binary files /dev/null and b/src/test/resources/blank-cell.xlsx differ