This is an automated email from the ASF dual-hosted git repository.
delei pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/fesod.git
The following commit(s) were added to refs/heads/main by this push:
new c44cc05e fix: Correct off-by-one error in CsvRow#getCell(int) (#677)
c44cc05e is described below
commit c44cc05e8e88f5d4640b2221ff9e68d40c4953c4
Author: Bengbengbalabalabeng
<[email protected]>
AuthorDate: Tue May 5 23:12:34 2026 +0800
fix: Correct off-by-one error in CsvRow#getCell(int) (#677)
* fix: Correct off-by-one error in CsvRow#getCell(int)
* fix: resolve incorrect package/import references.
---------
Co-authored-by: Shuxin Pan <[email protected]>
Co-authored-by: DeleiGuo <[email protected]>
---
.../apache/fesod/sheet/metadata/csv/CsvRow.java | 4 +-
.../sheet/csv/AssertCsvHeadDataWriteHandler.java | 61 +++++++++
.../org/apache/fesod/sheet/csv/CsvRowTest.java | 140 +++++++++++++++++++++
.../org/apache/fesod/sheet/csv/SimpleCsvData.java | 44 +++++++
4 files changed, 247 insertions(+), 2 deletions(-)
diff --git
a/fesod-sheet/src/main/java/org/apache/fesod/sheet/metadata/csv/CsvRow.java
b/fesod-sheet/src/main/java/org/apache/fesod/sheet/metadata/csv/CsvRow.java
index 1d11ad30..f4e600ab 100644
--- a/fesod-sheet/src/main/java/org/apache/fesod/sheet/metadata/csv/CsvRow.java
+++ b/fesod-sheet/src/main/java/org/apache/fesod/sheet/metadata/csv/CsvRow.java
@@ -111,10 +111,10 @@ public class CsvRow implements Row {
@Override
public Cell getCell(int cellnum) {
- if (cellnum >= cellList.size()) {
+ if (cellnum < 0 || cellnum >= cellList.size()) {
return null;
}
- return cellList.get(cellnum - 1);
+ return cellList.get(cellnum);
}
@Override
diff --git
a/fesod-sheet/src/test/java/org/apache/fesod/sheet/csv/AssertCsvHeadDataWriteHandler.java
b/fesod-sheet/src/test/java/org/apache/fesod/sheet/csv/AssertCsvHeadDataWriteHandler.java
new file mode 100644
index 00000000..9aa7abf2
--- /dev/null
+++
b/fesod-sheet/src/test/java/org/apache/fesod/sheet/csv/AssertCsvHeadDataWriteHandler.java
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.fesod.sheet.csv;
+
+import java.util.List;
+import lombok.RequiredArgsConstructor;
+import org.apache.fesod.sheet.write.handler.WorkbookWriteHandler;
+import
org.apache.fesod.sheet.write.handler.context.WorkbookWriteHandlerContext;
+import org.apache.fesod.sheet.write.metadata.holder.WriteWorkbookHolder;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.junit.jupiter.api.Assertions;
+
+@RequiredArgsConstructor
+public class AssertCsvHeadDataWriteHandler implements WorkbookWriteHandler {
+
+ private final List<List<String>> head;
+ private final List<List<String>> data;
+
+ @Override
+ public void afterWorkbookDispose(WorkbookWriteHandlerContext context) {
+ WriteWorkbookHolder workbook = context.getWriteWorkbookHolder();
+
+ Sheet sheet = workbook.getWorkbook().getSheetAt(0);
+
+ Row headRow = sheet.getRow(0);
+ for (int i = 0; i < head.size(); i++) {
+ Cell cell = headRow.getCell(i);
+
+ Assertions.assertEquals(head.get(i).get(0),
cell.getStringCellValue());
+ }
+
+ for (int i = 0; i < data.size(); i++) {
+ Row dataRow = sheet.getRow(i + 1);
+
+ for (int j = 0; j < data.get(i).size(); j++) {
+ Cell cell = dataRow.getCell(j);
+
+ Assertions.assertEquals(data.get(i).get(j),
cell.getStringCellValue());
+ }
+ }
+ }
+}
diff --git
a/fesod-sheet/src/test/java/org/apache/fesod/sheet/csv/CsvRowTest.java
b/fesod-sheet/src/test/java/org/apache/fesod/sheet/csv/CsvRowTest.java
new file mode 100644
index 00000000..23817f44
--- /dev/null
+++ b/fesod-sheet/src/test/java/org/apache/fesod/sheet/csv/CsvRowTest.java
@@ -0,0 +1,140 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.fesod.sheet.csv;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import org.apache.fesod.sheet.FastExcel;
+import org.apache.fesod.sheet.metadata.csv.CsvRow;
+import org.apache.fesod.sheet.metadata.csv.CsvSheet;
+import org.apache.fesod.sheet.metadata.csv.CsvWorkbook;
+import org.apache.fesod.sheet.util.TestFileUtil;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.CellType;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+@ExtendWith(MockitoExtension.class)
+public class CsvRowTest {
+
+ @Mock
+ private CsvWorkbook csvWorkbook;
+
+ @Mock
+ private CsvSheet csvSheet;
+
+ private CsvRow csvRow;
+
+ private static File fileCsvNoModel;
+ private static File fileCsvModel;
+
+ @BeforeEach
+ void setUp() {
+ csvRow = new CsvRow(csvWorkbook, csvSheet, 1);
+
+ Cell firstCell = csvRow.createCell(0, CellType.STRING);
+ firstCell.setCellValue("No");
+ Cell middleCell = csvRow.createCell(1, CellType.STRING);
+ middleCell.setCellValue("Name");
+ Cell lastCell = csvRow.createCell(2, CellType.STRING);
+ lastCell.setCellValue("Age");
+
+ fileCsvNoModel = TestFileUtil.createNewFile("csv-no-model.csv");
+ fileCsvModel = TestFileUtil.createNewFile("csv-model.csv");
+ }
+
+ @Test
+ void testGetCellWithFirstIndexShouldReturnFirstCell() {
+ Cell actualCell = csvRow.getCell(0);
+ Assertions.assertNotNull(actualCell);
+ Assertions.assertEquals("No", actualCell.getStringCellValue());
+ }
+
+ @Test
+ void testGetCellWithMiddleIndexShouldReturnMiddleCell() {
+ Cell actualCell = csvRow.getCell(1);
+ Assertions.assertNotNull(actualCell);
+ Assertions.assertEquals("Name", actualCell.getStringCellValue());
+ }
+
+ @Test
+ void testGetCellWithLastIndexShouldReturnLastCell() {
+ Cell actualCell = csvRow.getCell(2);
+ Assertions.assertNotNull(actualCell);
+ Assertions.assertEquals("Age", actualCell.getStringCellValue());
+ }
+
+ @Test
+ void testGetCellWithOutOfBoundsIndexShouldReturnNull() {
+ Cell actualCell1 = csvRow.getCell(3);
+ Assertions.assertNull(actualCell1);
+
+ Cell actualCell2 = csvRow.getCell(-1);
+ Assertions.assertNull(actualCell2);
+ }
+
+ @Test
+ void testCsvWriteWithOutModelShouldSuccess() {
+ FastExcel.write(fileCsvNoModel)
+ .head(head())
+ .registerWriteHandler(new
AssertCsvHeadDataWriteHandler(head(), data()))
+ .csv()
+ .doWrite(data());
+ }
+
+ @Test
+ void testCsvWriteWithModelShouldSuccess() {
+ FastExcel.write(fileCsvModel)
+ .head(SimpleCsvData.class)
+ .registerWriteHandler(new
AssertCsvHeadDataWriteHandler(head(), data()))
+ .csv()
+ .doWrite(modelData());
+ }
+
+ private static List<SimpleCsvData> modelData() {
+ List<SimpleCsvData> data = new ArrayList<>();
+ data.add(new SimpleCsvData("1", "Jackson", "20"));
+ data.add(new SimpleCsvData("2", "Tom", "21"));
+ data.add(new SimpleCsvData("3", "Sophia", "20"));
+ return data;
+ }
+
+ private static List<List<String>> data() {
+ List<List<String>> data = new ArrayList<>();
+ data.add(Arrays.asList("1", "Jackson", "20"));
+ data.add(Arrays.asList("2", "Tom", "21"));
+ data.add(Arrays.asList("3", "Sophia", "20"));
+ return data;
+ }
+
+ private List<List<String>> head() {
+ List<List<String>> head = new ArrayList<>();
+ head.add(Arrays.asList("No"));
+ head.add(Arrays.asList("Name"));
+ head.add(Arrays.asList("Age"));
+ return head;
+ }
+}
diff --git
a/fesod-sheet/src/test/java/org/apache/fesod/sheet/csv/SimpleCsvData.java
b/fesod-sheet/src/test/java/org/apache/fesod/sheet/csv/SimpleCsvData.java
new file mode 100644
index 00000000..36378af7
--- /dev/null
+++ b/fesod-sheet/src/test/java/org/apache/fesod/sheet/csv/SimpleCsvData.java
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.fesod.sheet.csv;
+
+import lombok.AllArgsConstructor;
+import lombok.EqualsAndHashCode;
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+import lombok.Setter;
+import org.apache.fesod.sheet.annotation.ExcelProperty;
+
+@Getter
+@Setter
+@NoArgsConstructor
+@AllArgsConstructor
+@EqualsAndHashCode
+public class SimpleCsvData {
+
+ @ExcelProperty("No")
+ private String no;
+
+ @ExcelProperty("Name")
+ private String name;
+
+ @ExcelProperty("Age")
+ private String age;
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]