diff --git a/README.adoc b/README.adoc index 01efc25..38329f9 100644 --- a/README.adoc +++ b/README.adoc @@ -2,8 +2,8 @@ :toclevels: 2 = Poiji -:version: v4.3.0 -:branch: 4.3.0 +:version: v4.4.0 +:branch: 4.4.0 image:https://github.com/ozlerhakan/poiji/actions/workflows/maven.yml/badge.svg["Build Status"] image:https://app.codacy.com/project/badge/Grade/64f7e2cb9e604807b62334a4cfc3952d["Codacy code quality",link="https://www.codacy.com/gh/ozlerhakan/poiji/dashboard?utm_source=github.com&utm_medium=referral&utm_content=ozlerhakan/poiji&utm_campaign=Badge_Grade"] image:https://codecov.io/gh/ozlerhakan/poiji/branch/{branch}/graph/badge.svg?token=MN6V6xOWBq["Codecov",link="https://codecov.io/gh/ozlerhakan/poiji"] image:https://img.shields.io/badge/apache.poi-5.2.3-brightgreen.svg[] image:https://app.fossa.com/api/projects/git%2Bgithub.com%2Fozlerhakan%2Fpoiji.svg?type=shield["FOSSA Status",link="https://app.fossa.com/projects/git%2Bgithub.com%2Fozlerhakan%2Fpoiji?ref=badge_shield"] @@ -25,7 +25,7 @@ In your Maven/Gradle project, first add the corresponding dependency: com.github.ozlerhakan poiji - 4.3.0 + 4.4.0 ---- diff --git a/pom.xml b/pom.xml index b1af229..b9992b7 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ com.github.ozlerhakan poiji - 4.3.0 + 4.4.0 jar poiji diff --git a/src/main/java/com/poiji/exception/HeaderMissingException.java b/src/main/java/com/poiji/exception/HeaderMissingException.java index 4445059..ce2a1aa 100644 --- a/src/main/java/com/poiji/exception/HeaderMissingException.java +++ b/src/main/java/com/poiji/exception/HeaderMissingException.java @@ -1,12 +1,29 @@ package com.poiji.exception; +import java.util.Set; + /** * Exception thrown if namedHeaderMandatory is set in the options, but a header specified in an @ExcelCellName * is missing in the sheet. */ @SuppressWarnings("serial") public class HeaderMissingException extends PoijiException { - public HeaderMissingException(String message) { + + private final Set missingExcelCellHeaders; + private final Set missingExcelCellNameHeaders; + + public HeaderMissingException(String message, Set missingExcelCellHeaders, + Set missingExcelCellNameHeaders) { super(message); + this.missingExcelCellHeaders = Set.copyOf(missingExcelCellHeaders); + this.missingExcelCellNameHeaders = Set.copyOf(missingExcelCellNameHeaders); + } + + public Set getMissingExcelCellHeaders() { + return missingExcelCellHeaders; + } + + public Set getMissingExcelCellNameHeaders() { + return missingExcelCellNameHeaders; } } diff --git a/src/main/java/com/poiji/exception/PoijiMultiRowException.java b/src/main/java/com/poiji/exception/PoijiMultiRowException.java index d4f984f..ffc3613 100644 --- a/src/main/java/com/poiji/exception/PoijiMultiRowException.java +++ b/src/main/java/com/poiji/exception/PoijiMultiRowException.java @@ -24,9 +24,28 @@ public List getErrors() { public static class PoijiRowSpecificException extends RuntimeException { + private final String columnName; + private final String fieldName; + private final Integer rowNum; + public PoijiRowSpecificException(String columnName, String fieldName, Integer rowNum) { super("Cell value of column '" + columnName + "' is null," + " so cannot be applied to mandatory field '" + fieldName + "'. ;Row " + rowNum); + this.columnName = columnName; + this.fieldName = fieldName; + this.rowNum = rowNum; + } + + public String getColumnName() { + return columnName; + } + + public String getFieldName() { + return fieldName; + } + + public Integer getRowNum() { + return rowNum; } } } diff --git a/src/main/java/com/poiji/util/AnnotationUtil.java b/src/main/java/com/poiji/util/AnnotationUtil.java index 5ff9226..71b1b15 100644 --- a/src/main/java/com/poiji/util/AnnotationUtil.java +++ b/src/main/java/com/poiji/util/AnnotationUtil.java @@ -77,7 +77,7 @@ public static void validateMandatoryNameColumns(PoijiOptions options, .forEach(missingMessage::append); message += missingMessage; } - throw new HeaderMissingException(message); + throw new HeaderMissingException(message, missingExcelCellHeaders, missingExcelCellNameHeaders); } } } diff --git a/src/test/java/com/poiji/deserialize/MandatoryCellsExceptionTest.java b/src/test/java/com/poiji/deserialize/MandatoryCellsExceptionTest.java index b0b1304..0571b9f 100644 --- a/src/test/java/com/poiji/deserialize/MandatoryCellsExceptionTest.java +++ b/src/test/java/com/poiji/deserialize/MandatoryCellsExceptionTest.java @@ -3,6 +3,7 @@ import com.poiji.bind.Poiji; import com.poiji.deserialize.model.byname.MandatoryMissingCells; import com.poiji.exception.PoijiMultiRowException; +import com.poiji.exception.PoijiMultiRowException.PoijiRowSpecificException; import com.poiji.option.PoijiOptions; import org.apache.poi.hssf.usermodel.HSSFWorkbook; @@ -13,14 +14,28 @@ import org.junit.Test; import java.io.IOException; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; public class MandatoryCellsExceptionTest { - @Test(expected = PoijiMultiRowException.class) + @Test public void testExcelMandatoryColumn() { - Poiji.fromExcel(createDummyExcel(), MandatoryMissingCells.class, PoijiOptions.PoijiOptionsBuilder - .settings() - .build()); + try { + Poiji.fromExcel(createDummyExcel(), 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() { diff --git a/src/test/java/com/poiji/deserialize/MandatoryNamedColumnsExceptionTest.java b/src/test/java/com/poiji/deserialize/MandatoryNamedColumnsExceptionTest.java index 7dd18ed..2d1b280 100644 --- a/src/test/java/com/poiji/deserialize/MandatoryNamedColumnsExceptionTest.java +++ b/src/test/java/com/poiji/deserialize/MandatoryNamedColumnsExceptionTest.java @@ -10,6 +10,10 @@ import java.io.File; import java.util.Arrays; +import java.util.Set; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; @RunWith(Parameterized.class) public class MandatoryNamedColumnsExceptionTest { @@ -28,10 +32,17 @@ public static Iterable queries() { }); } - @Test(expected = HeaderMissingException.class) + @Test public void testExcelMandatoryColumn() { - Poiji.fromExcel(new File(path), PersonByNameWithMissingColumn.class, PoijiOptions.PoijiOptionsBuilder - .settings() - .build()); + try { + Poiji.fromExcel(new File(path), PersonByNameWithMissingColumn.class, PoijiOptions.PoijiOptionsBuilder + .settings() + .build()); + } catch (HeaderMissingException e) { + assertEquals(Set.of(6), e.getMissingExcelCellHeaders()); + assertEquals(Set.of("This column will be missing"), e.getMissingExcelCellNameHeaders()); + return; + } + fail("Expected exception: " + HeaderMissingException.class.getName()); } }