Skip to content

Commit

Permalink
Check for blank cell type in addition to null check for mandatory cells
Browse files Browse the repository at this point in the history
  • Loading branch information
virtual-machinist committed Mar 18, 2024
1 parent 0ee9a3f commit 63d9c09
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/poiji/bind/mapping/HSSFUnmarshaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ private <T> void constructTypeValue(Row currentRow, T instance, Field field,
FieldAnnotationDetail annotationDetail) {
Cell cell = currentRow.getCell(annotationDetail.getColumn());

if (cell != null) {
if (cell != null && cell.getCellType() != CellType.BLANK) {
if (annotationDetail.isDisabledCellFormat()) {
cell.setCellStyle(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,33 @@
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.io.IOException;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

@RunWith(Parameterized.class)
public class MandatoryCellsExceptionTest {

private final Sheet sheet;

public MandatoryCellsExceptionTest(Sheet sheet) {
this.sheet = sheet;
}

@Parameterized.Parameters
public static List<Sheet> sheets() {
return List.of(createDummyExcel(true), createDummyExcel(false));
}

@Test
public void testExcelMandatoryColumn() {
try {
Poiji.fromExcel(createDummyExcel(), MandatoryMissingCells.class, PoijiOptions.PoijiOptionsBuilder
Poiji.fromExcel(sheet, MandatoryMissingCells.class, PoijiOptions.PoijiOptionsBuilder
.settings()
.build());
} catch (PoijiMultiRowException e) {
Expand All @@ -38,7 +52,7 @@ public void testExcelMandatoryColumn() {
fail("Expected exception: " + PoijiMultiRowException.class.getName());
}

private Sheet createDummyExcel() {
private static Sheet createDummyExcel(boolean addBlank) {

Workbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet("Example");
Expand All @@ -52,6 +66,10 @@ private Sheet createDummyExcel() {
Row dataRow = sheet.createRow(1);
Cell dataCell1 = dataRow.createCell(0);
dataCell1.setCellValue("Paul");
if (addBlank) {
Cell dataCell2 = dataRow.createCell(1);
dataCell2.setBlank();
}

try {
workbook.close();
Expand Down

0 comments on commit 63d9c09

Please sign in to comment.