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

Allow ignorable rows between the header row and the data rows #47

Merged
merged 3 commits into from
Mar 21, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,11 @@ public void execute(Connection connection, BinderConfiguration configuration) th
* <ul>
* <li>{@code include([])}</li>
* <li>{@code exclude([])}</li>
* <li>{@code resolver(i -> i)}</li>
* <li>{@code left(0)}</li>
* <li>{@code top(0)}</li>
* <li>{@code margin(0, 0)}</li>
* <li>{@code skipAfterHeader(0)}</li>
* </ul>
*
* @author sciencesakura
Expand All @@ -185,6 +188,7 @@ public static final class Builder {
Function<String, String> resolver = Function.identity();
int left;
int top;
int skipAfterHeader;
final Map<String, Map<String, Object>> defaultValues = new HashMap<>();
final Map<String, Map<String, ValueGenerator<?>>> valueGenerators = new HashMap<>();
private boolean built;
Expand All @@ -197,6 +201,7 @@ private Builder(URL location) {
* Build a new {@code Import} instance.
*
* @return the new {@code Import} instance
* @throws IllegalStateException if this method was called more than once on the same instance
*/
public Import build() {
if (built) {
Expand Down Expand Up @@ -278,8 +283,7 @@ public Builder exclude(@NotNull Pattern... patterns) {
*/
public Builder resolver(@NotNull Map<String, String> resolver) {
requireNonNull(resolver, "resolver must not be null");
this.resolver = resolver::get;
return this;
return resolver(resolver::get);
}

/**
Expand All @@ -301,6 +305,7 @@ public Builder resolver(@NotNull Function<String, String> resolver) {
*
* @param left the 0-based column index, must be non-negative
* @return the reference to this object
* @throws IllegalArgumentException if the specified value is negative
*/
public Builder left(int left) {
if (left < 0) {
Expand All @@ -318,6 +323,7 @@ public Builder left(int left) {
*
* @param top the 0-based row index, must be non-negative
* @return the reference to this object
* @throws IllegalArgumentException if the specified value is negative
*/
public Builder top(int top) {
if (top < 0) {
Expand All @@ -336,11 +342,30 @@ public Builder top(int top) {
* @param left the 0-based column index, must be non-negative
* @param top the 0-based row index, must be non-negative
* @return the reference to this object
* @throws IllegalArgumentException if the specified value is negative
*/
public Builder margin(int left, int top) {
return left(left).top(top);
}

/**
* Specifies the number of rows to skip after the header row.
* <p>
* By default {@code 0} is used.
* </p>
*
* @param n the number of rows to skip after the header row, must be non-negative
* @return the reference to this object
* @throws IllegalArgumentException if the specified value is negative
*/
public Builder skipAfterHeader(int n) {
if (n < 0) {
throw new IllegalArgumentException("skipAfterHeader must be greater than or equal to 0");
}
this.skipAfterHeader = n;
return this;
}

/**
* Specifies the default value for the given pair of table and column.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ static Operation build(Import.Builder builder) {
.columns(columns(row, builder.left, width, evaluator));
setDefaultValues(ib, builder.defaultValues, tableName);
setValueGenerators(ib, builder.valueGenerators, tableName);
rowIndex += builder.skipAfterHeader;
while ((row = sheet.getRow(++rowIndex)) != null) {
ib.values(values(row, builder.left, width, evaluator));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ void import_with_default_settings() {
}

@Nested
@SuppressWarnings("ConstantConditions")
class ExcelFile {

@Test
Expand Down Expand Up @@ -181,6 +182,7 @@ void throw_dsre_if_header_row_contains_error() {
}

@Nested
@SuppressWarnings("ConstantConditions")
class TableNames {

@BeforeEach
Expand Down Expand Up @@ -408,6 +410,7 @@ void throws_npe_if_pattern_to_include_contains_null() {
}

@Nested
@SuppressWarnings("ConstantConditions")
class TableMapping {

@BeforeEach
Expand Down Expand Up @@ -610,6 +613,23 @@ void left_is_two_and_top_is_three() {
.value("name").isEqualTo("Bob");
}

@Test
void margin_between_header_and_data() {
var changes = new Changes(source).setStartPointNow();
var operation = excel("Margin/margin_between_header_and_data.xlsx").skipAfterHeader(1).build();
new DbSetup(destination, operation).launch();
assertThat(changes.setEndPointNow())
.hasNumberOfChanges(2)
.changeOfCreationOnTable("table_11")
.rowAtEndPoint()
.value("id").isEqualTo(1)
.value("name").isEqualTo("Alice")
.changeOfCreationOnTable("table_12")
.rowAtEndPoint()
.value("id").isEqualTo(2)
.value("name").isEqualTo("Bob");
}

@Test
void throw_dsre_if_header_row_is_not_found_1() {
assertThatThrownBy(() -> excel("Margin/no_margin.xlsx").top(8).build())
Expand Down Expand Up @@ -637,9 +657,17 @@ void throws_iae_if_top_is_negative() {
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("top must be greater than or equal to 0");
}

@Test
void throws_iae_if_margin_between_header_and_data_is_negative() {
assertThatThrownBy(() -> excel("Margin/no_margin.xlsx").skipAfterHeader(-1))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("skipAfterHeader must be greater than or equal to 0");
}
}

@Nested
@SuppressWarnings("ConstantConditions")
class WithDefaultValue {

@BeforeEach
Expand Down Expand Up @@ -709,6 +737,7 @@ void throws_npe_if_column_is_null() {
}

@Nested
@SuppressWarnings("ConstantConditions")
class WithGeneratedValue {

@BeforeEach
Expand Down
Binary file not shown.