|
| 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 | +} |
0 commit comments