Skip to content

Commit 7fad648

Browse files
fix: Correct off-by-one error in CsvRow#getCell(int)
1 parent 19ff542 commit 7fad648

File tree

4 files changed

+247
-2
lines changed

4 files changed

+247
-2
lines changed

fesod/src/main/java/org/apache/fesod/excel/metadata/csv/CsvRow.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ public int getRowNum() {
105105

106106
@Override
107107
public Cell getCell(int cellnum) {
108-
if (cellnum >= cellList.size()) {
108+
if (cellnum < 0 || cellnum >= cellList.size()) {
109109
return null;
110110
}
111-
return cellList.get(cellnum - 1);
111+
return cellList.get(cellnum);
112112
}
113113

114114
@Override
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.fesod.excel.csv;
21+
22+
import java.util.List;
23+
import lombok.RequiredArgsConstructor;
24+
import org.apache.fesod.excel.write.handler.WorkbookWriteHandler;
25+
import org.apache.fesod.excel.write.handler.context.WorkbookWriteHandlerContext;
26+
import org.apache.fesod.excel.write.metadata.holder.WriteWorkbookHolder;
27+
import org.apache.poi.ss.usermodel.Cell;
28+
import org.apache.poi.ss.usermodel.Row;
29+
import org.apache.poi.ss.usermodel.Sheet;
30+
import org.junit.jupiter.api.Assertions;
31+
32+
@RequiredArgsConstructor
33+
public class AssertCsvHeadDataWriteHandler implements WorkbookWriteHandler {
34+
35+
private final List<List<String>> head;
36+
private final List<List<String>> data;
37+
38+
@Override
39+
public void afterWorkbookDispose(WorkbookWriteHandlerContext context) {
40+
WriteWorkbookHolder workbook = context.getWriteWorkbookHolder();
41+
42+
Sheet sheet = workbook.getWorkbook().getSheetAt(0);
43+
44+
Row headRow = sheet.getRow(0);
45+
for (int i = 0; i < head.size(); i++) {
46+
Cell cell = headRow.getCell(i);
47+
48+
Assertions.assertEquals(head.get(i).get(0), cell.getStringCellValue());
49+
}
50+
51+
for (int i = 0; i < data.size(); i++) {
52+
Row dataRow = sheet.getRow(i + 1);
53+
54+
for (int j = 0; j < data.get(i).size(); j++) {
55+
Cell cell = dataRow.getCell(j);
56+
57+
Assertions.assertEquals(data.get(i).get(j), cell.getStringCellValue());
58+
}
59+
}
60+
}
61+
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.fesod.excel.csv;
21+
22+
import java.io.File;
23+
import java.util.ArrayList;
24+
import java.util.Arrays;
25+
import java.util.List;
26+
import org.apache.fesod.excel.FastExcel;
27+
import org.apache.fesod.excel.metadata.csv.CsvRow;
28+
import org.apache.fesod.excel.metadata.csv.CsvSheet;
29+
import org.apache.fesod.excel.metadata.csv.CsvWorkbook;
30+
import org.apache.fesod.excel.util.TestFileUtil;
31+
import org.apache.poi.ss.usermodel.Cell;
32+
import org.apache.poi.ss.usermodel.CellType;
33+
import org.junit.jupiter.api.Assertions;
34+
import org.junit.jupiter.api.BeforeEach;
35+
import org.junit.jupiter.api.Test;
36+
import org.junit.jupiter.api.extension.ExtendWith;
37+
import org.mockito.Mock;
38+
import org.mockito.junit.jupiter.MockitoExtension;
39+
40+
@ExtendWith(MockitoExtension.class)
41+
public class CsvRowTest {
42+
43+
@Mock
44+
private CsvWorkbook csvWorkbook;
45+
46+
@Mock
47+
private CsvSheet csvSheet;
48+
49+
private CsvRow csvRow;
50+
51+
private static File fileCsvNoModel;
52+
private static File fileCsvModel;
53+
54+
@BeforeEach
55+
void setUp() {
56+
csvRow = new CsvRow(csvWorkbook, csvSheet, 1);
57+
58+
Cell firstCell = csvRow.createCell(0, CellType.STRING);
59+
firstCell.setCellValue("No");
60+
Cell middleCell = csvRow.createCell(1, CellType.STRING);
61+
middleCell.setCellValue("Name");
62+
Cell lastCell = csvRow.createCell(2, CellType.STRING);
63+
lastCell.setCellValue("Age");
64+
65+
fileCsvNoModel = TestFileUtil.createNewFile("csv-no-model.csv");
66+
fileCsvModel = TestFileUtil.createNewFile("csv-model.csv");
67+
}
68+
69+
@Test
70+
void testGetCellWithFirstIndexShouldReturnFirstCell() {
71+
Cell actualCell = csvRow.getCell(0);
72+
Assertions.assertNotNull(actualCell);
73+
Assertions.assertEquals("No", actualCell.getStringCellValue());
74+
}
75+
76+
@Test
77+
void testGetCellWithMiddleIndexShouldReturnMiddleCell() {
78+
Cell actualCell = csvRow.getCell(1);
79+
Assertions.assertNotNull(actualCell);
80+
Assertions.assertEquals("Name", actualCell.getStringCellValue());
81+
}
82+
83+
@Test
84+
void testGetCellWithLastIndexShouldReturnLastCell() {
85+
Cell actualCell = csvRow.getCell(2);
86+
Assertions.assertNotNull(actualCell);
87+
Assertions.assertEquals("Age", actualCell.getStringCellValue());
88+
}
89+
90+
@Test
91+
void testGetCellWithOutOfBoundsIndexShouldReturnNull() {
92+
Cell actualCell1 = csvRow.getCell(3);
93+
Assertions.assertNull(actualCell1);
94+
95+
Cell actualCell2 = csvRow.getCell(-1);
96+
Assertions.assertNull(actualCell2);
97+
}
98+
99+
@Test
100+
void testCsvWriteWithOutModelShouldSuccess() {
101+
FastExcel.write(fileCsvNoModel)
102+
.head(head())
103+
.registerWriteHandler(new AssertCsvHeadDataWriteHandler(head(), data()))
104+
.csv()
105+
.doWrite(data());
106+
}
107+
108+
@Test
109+
void testCsvWriteWithModelShouldSuccess() {
110+
FastExcel.write(fileCsvModel)
111+
.head(SimpleCsvData.class)
112+
.registerWriteHandler(new AssertCsvHeadDataWriteHandler(head(), data()))
113+
.csv()
114+
.doWrite(modelData());
115+
}
116+
117+
private static List<SimpleCsvData> modelData() {
118+
List<SimpleCsvData> data = new ArrayList<>();
119+
data.add(new SimpleCsvData("1", "Jackson", "20"));
120+
data.add(new SimpleCsvData("2", "Tom", "21"));
121+
data.add(new SimpleCsvData("3", "Sophia", "20"));
122+
return data;
123+
}
124+
125+
private static List<List<String>> data() {
126+
List<List<String>> data = new ArrayList<>();
127+
data.add(Arrays.asList("1", "Jackson", "20"));
128+
data.add(Arrays.asList("2", "Tom", "21"));
129+
data.add(Arrays.asList("3", "Sophia", "20"));
130+
return data;
131+
}
132+
133+
private List<List<String>> head() {
134+
List<List<String>> head = new ArrayList<>();
135+
head.add(Arrays.asList("No"));
136+
head.add(Arrays.asList("Name"));
137+
head.add(Arrays.asList("Age"));
138+
return head;
139+
}
140+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.fesod.excel.csv;
21+
22+
import lombok.AllArgsConstructor;
23+
import lombok.EqualsAndHashCode;
24+
import lombok.Getter;
25+
import lombok.NoArgsConstructor;
26+
import lombok.Setter;
27+
import org.apache.fesod.excel.annotation.ExcelProperty;
28+
29+
@Getter
30+
@Setter
31+
@NoArgsConstructor
32+
@AllArgsConstructor
33+
@EqualsAndHashCode
34+
public class SimpleCsvData {
35+
36+
@ExcelProperty("No")
37+
private String no;
38+
39+
@ExcelProperty("Name")
40+
private String name;
41+
42+
@ExcelProperty("Age")
43+
private String age;
44+
}

0 commit comments

Comments
 (0)