Skip to content

Commit

Permalink
Expose details of HeaderMissingException as properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Volkov committed Mar 1, 2024
1 parent 8c8830e commit e4dae05
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
19 changes: 18 additions & 1 deletion src/main/java/com/poiji/exception/HeaderMissingException.java
Original file line number Diff line number Diff line change
@@ -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<Integer> missingExcelCellHeaders;
private final Set<String> missingExcelCellNameHeaders;

public HeaderMissingException(String message, Set<Integer> missingExcelCellHeaders,
Set<String> missingExcelCellNameHeaders) {
super(message);
this.missingExcelCellHeaders = Set.copyOf(missingExcelCellHeaders);
this.missingExcelCellNameHeaders = Set.copyOf(missingExcelCellNameHeaders);
}

public Set<Integer> getMissingExcelCellHeaders() {
return missingExcelCellHeaders;
}

public Set<String> getMissingExcelCellNameHeaders() {
return missingExcelCellNameHeaders;
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/poiji/util/AnnotationUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public static <T> void validateMandatoryNameColumns(PoijiOptions options,
.forEach(missingMessage::append);
message += missingMessage;
}
throw new HeaderMissingException(message);
throw new HeaderMissingException(message, missingExcelCellHeaders, missingExcelCellNameHeaders);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -28,10 +32,17 @@ public static Iterable<Object[]> 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());
}
}

0 comments on commit e4dae05

Please sign in to comment.