Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for blank cell type in addition to null check for mandatory cells #314

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading