Skip to content

Commit c69196e

Browse files
cbot59Marten Deinum
authored andcommitted
fix: avoid ArrayIndexOutOfBoundsException when currentRow length more than column names length
Signed-off-by: Rivaldi <[email protected]>
1 parent b1e195b commit c69196e

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

spring-batch-excel/src/main/java/org/springframework/batch/extensions/excel/support/rowset/DefaultRowSet.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ public Properties getProperties() {
8080
for (int i = 0; i < this.currentRow.length; i++) {
8181
String value = this.currentRow[i];
8282
if (value != null) {
83+
if (i > names.length - 1) {
84+
break;
85+
}
8386
props.setProperty(names[i], value);
8487
}
8588
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.springframework.batch.extensions.excel.support.rowset;
2+
3+
import org.junit.jupiter.api.BeforeEach;
4+
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.api.extension.ExtendWith;
6+
import org.mockito.Mockito;
7+
import org.springframework.batch.extensions.excel.MockSheet;
8+
import org.springframework.batch.extensions.excel.Sheet;
9+
10+
import java.util.ArrayList;
11+
import java.util.Arrays;
12+
import java.util.List;
13+
import java.util.Properties;
14+
15+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
16+
import static org.junit.jupiter.api.Assertions.*;
17+
import static org.mockito.BDDMockito.given;
18+
19+
class DefaultRowSetTest {
20+
21+
private DefaultRowSet rowSet;
22+
23+
@BeforeEach
24+
void setUp() {
25+
rowSet = new DefaultRowSet(new MockSheet(
26+
"Sheet1",
27+
Arrays.asList("col1a,col1b,col1c".split(","), "col2a,col2b,col2c".split(","), "col3a,col3b,col3c".split(","))
28+
), new RowSetMetaData() {
29+
@Override
30+
public String[] getColumnNames() {
31+
return new String[]{ "cola", "colb"};
32+
}
33+
34+
@Override
35+
public String getSheetName() {
36+
return "Sheet1";
37+
}
38+
});
39+
}
40+
41+
@Test
42+
void shouldReturnPropsSizeEqualsToMetadataColumns() {
43+
rowSet.next();
44+
var properties = rowSet.getProperties();
45+
46+
assertThat(properties.size()).isEqualTo(2);
47+
assertThat(properties.getProperty("cola")).isEqualTo("col1a");
48+
assertThat(properties.getProperty("colb")).isEqualTo("col1b");
49+
assertThat(properties.getProperty("colc")).isNull();
50+
}
51+
}

0 commit comments

Comments
 (0)