Skip to content

Commit

Permalink
Improve blank mandatory cell handling for XLS files and Sheet instances
Browse files Browse the repository at this point in the history
  • Loading branch information
virtual-machinist committed Mar 24, 2024
1 parent 0ee9a3f commit 11f44d8
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/com/poiji/bind/mapping/HSSFUnmarshallerStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,7 +24,7 @@
public class MandatoryCellsExceptionTest {

@Test
public void testExcelMandatoryColumn() {
public void shouldThrowExceptionForMissingCell() {
try {
Poiji.fromExcel(createDummyExcel(), MandatoryMissingCells.class, PoijiOptions.PoijiOptionsBuilder
.settings()
Expand All @@ -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<PoijiRowSpecificException> 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<PoijiRowSpecificException> 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<PoijiRowSpecificException> 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();
Expand Down
Binary file added src/test/resources/blank-cell.xls
Binary file not shown.
Binary file added src/test/resources/blank-cell.xlsx
Binary file not shown.

0 comments on commit 11f44d8

Please sign in to comment.