|
| 1 | +package cn.idev.excel.parameter; |
| 2 | + |
| 3 | +import cn.idev.excel.ExcelReader; |
| 4 | +import cn.idev.excel.ExcelWriter; |
| 5 | +import cn.idev.excel.FastExcel; |
| 6 | +import cn.idev.excel.context.AnalysisContext; |
| 7 | +import cn.idev.excel.event.AnalysisEventListener; |
| 8 | +import cn.idev.excel.read.metadata.ReadSheet; |
| 9 | +import cn.idev.excel.support.ExcelTypeEnum; |
| 10 | +import cn.idev.excel.util.ParameterUtil; |
| 11 | +import cn.idev.excel.util.SheetUtils; |
| 12 | +import cn.idev.excel.util.StringUtils; |
| 13 | +import cn.idev.excel.util.TestFileUtil; |
| 14 | +import cn.idev.excel.write.metadata.WriteSheet; |
| 15 | +import com.alibaba.fastjson2.JSON; |
| 16 | +import lombok.extern.slf4j.Slf4j; |
| 17 | +import org.junit.jupiter.api.Assertions; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | + |
| 20 | +import java.io.File; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +@Slf4j |
| 25 | +public class AutoStripParameterTest { |
| 26 | + |
| 27 | + private static File testFile; |
| 28 | + private static final String FW_SPACES = " "; |
| 29 | + private static final String SPACES = " "; |
| 30 | + |
| 31 | + @Test |
| 32 | + public void test03() { |
| 33 | + testAutoStripSheetNameInternal(ExcelTypeEnum.XLS, null, null); |
| 34 | + testAutoStripSheetNameInternal(ExcelTypeEnum.XLS, null, false); |
| 35 | + testAutoStripSheetNameInternal(ExcelTypeEnum.XLS, null, true); |
| 36 | + testAutoStripSheetNameInternal(ExcelTypeEnum.XLS, false, null); |
| 37 | + testAutoStripSheetNameInternal(ExcelTypeEnum.XLS, false, false); |
| 38 | + testAutoStripSheetNameInternal(ExcelTypeEnum.XLS, false, true); |
| 39 | + testAutoStripSheetNameInternal(ExcelTypeEnum.XLS, true, null); |
| 40 | + testAutoStripSheetNameInternal(ExcelTypeEnum.XLS, true, false); |
| 41 | + testAutoStripSheetNameInternal(ExcelTypeEnum.XLS, true, true); |
| 42 | + |
| 43 | + testAutoStripContentInternal(ExcelTypeEnum.XLS, null, null); |
| 44 | + testAutoStripContentInternal(ExcelTypeEnum.XLS, null, false); |
| 45 | + testAutoStripContentInternal(ExcelTypeEnum.XLS, null, true); |
| 46 | + testAutoStripContentInternal(ExcelTypeEnum.XLS, false, null); |
| 47 | + testAutoStripContentInternal(ExcelTypeEnum.XLS, false, false); |
| 48 | + testAutoStripContentInternal(ExcelTypeEnum.XLS, false, true); |
| 49 | + testAutoStripContentInternal(ExcelTypeEnum.XLS, true, null); |
| 50 | + testAutoStripContentInternal(ExcelTypeEnum.XLS, true, false); |
| 51 | + testAutoStripContentInternal(ExcelTypeEnum.XLS, true, true); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void test07() { |
| 56 | + testAutoStripSheetNameInternal(ExcelTypeEnum.XLSX, null, null); |
| 57 | + testAutoStripSheetNameInternal(ExcelTypeEnum.XLSX, null, false); |
| 58 | + testAutoStripSheetNameInternal(ExcelTypeEnum.XLSX, null, true); |
| 59 | + testAutoStripSheetNameInternal(ExcelTypeEnum.XLSX, false, null); |
| 60 | + testAutoStripSheetNameInternal(ExcelTypeEnum.XLSX, false, false); |
| 61 | + testAutoStripSheetNameInternal(ExcelTypeEnum.XLSX, false, true); |
| 62 | + testAutoStripSheetNameInternal(ExcelTypeEnum.XLSX, true, null); |
| 63 | + testAutoStripSheetNameInternal(ExcelTypeEnum.XLSX, true, false); |
| 64 | + testAutoStripSheetNameInternal(ExcelTypeEnum.XLSX, true, true); |
| 65 | + |
| 66 | + testAutoStripContentInternal(ExcelTypeEnum.XLSX, null, null); |
| 67 | + testAutoStripContentInternal(ExcelTypeEnum.XLSX, null, false); |
| 68 | + testAutoStripContentInternal(ExcelTypeEnum.XLSX, null, true); |
| 69 | + testAutoStripContentInternal(ExcelTypeEnum.XLSX, false, null); |
| 70 | + testAutoStripContentInternal(ExcelTypeEnum.XLSX, false, false); |
| 71 | + testAutoStripContentInternal(ExcelTypeEnum.XLSX, false, true); |
| 72 | + testAutoStripContentInternal(ExcelTypeEnum.XLSX, true, null); |
| 73 | + testAutoStripContentInternal(ExcelTypeEnum.XLSX, true, false); |
| 74 | + testAutoStripContentInternal(ExcelTypeEnum.XLSX, true, true); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testCSV() { |
| 79 | + testAutoStripContentInternal(ExcelTypeEnum.CSV, null, null); |
| 80 | + testAutoStripContentInternal(ExcelTypeEnum.CSV, null, false); |
| 81 | + testAutoStripContentInternal(ExcelTypeEnum.CSV, null, true); |
| 82 | + testAutoStripContentInternal(ExcelTypeEnum.CSV, false, null); |
| 83 | + testAutoStripContentInternal(ExcelTypeEnum.CSV, false, false); |
| 84 | + testAutoStripContentInternal(ExcelTypeEnum.CSV, false, true); |
| 85 | + testAutoStripContentInternal(ExcelTypeEnum.CSV, true, null); |
| 86 | + testAutoStripContentInternal(ExcelTypeEnum.CSV, true, false); |
| 87 | + testAutoStripContentInternal(ExcelTypeEnum.CSV, true, true); |
| 88 | + } |
| 89 | + |
| 90 | + private void testAutoStripSheetNameInternal( |
| 91 | + final ExcelTypeEnum excelType, final Boolean autoTrim, final Boolean autoStrip) { |
| 92 | + testFile = TestFileUtil.createNewFile("auto-strip-sheet-name" + excelType.getValue()); |
| 93 | + |
| 94 | + final String sheetNameSpaces = SPACES + "Sheet1" + SPACES; |
| 95 | + final String sheetNameFullWidthSpaces = FW_SPACES + "Sheet2" + FW_SPACES; |
| 96 | + |
| 97 | + List<ParameterData> demoList = new ArrayList<>(); |
| 98 | + ParameterData simpleData = new ParameterData(); |
| 99 | + simpleData.setName("string"); |
| 100 | + demoList.add(simpleData); |
| 101 | + |
| 102 | + try (ExcelWriter excelWriter = FastExcel.write(testFile, ParameterData.class) |
| 103 | + .excelType(excelType) |
| 104 | + .autoTrim(autoTrim) |
| 105 | + .autoStrip(autoStrip) |
| 106 | + .build()) { |
| 107 | + WriteSheet writeSheet = FastExcel.writerSheet(sheetNameSpaces).build(); |
| 108 | + excelWriter.write(demoList, writeSheet); |
| 109 | + writeSheet = FastExcel.writerSheet(sheetNameFullWidthSpaces).build(); |
| 110 | + excelWriter.write(demoList, writeSheet); |
| 111 | + } |
| 112 | + |
| 113 | + try (ExcelReader excelReader = FastExcel.read(testFile) |
| 114 | + .excelType(excelType) |
| 115 | + .head(ParameterData.class) |
| 116 | + .registerReadListenerIfNotNull(new AnalysisEventListener<ParameterData>() { |
| 117 | + @Override |
| 118 | + public void invoke(ParameterData data, AnalysisContext context) { |
| 119 | + log.info("Read one record: {}", JSON.toJSONString(data)); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public void doAfterAllAnalysed(AnalysisContext context) { |
| 124 | + // global configuration match |
| 125 | + Assertions.assertEquals( |
| 126 | + autoTrim == null ? Boolean.TRUE : autoTrim, |
| 127 | + ParameterUtil.getAutoTrimFlag( |
| 128 | + context.readSheetHolder().getReadSheet(), context)); |
| 129 | + Assertions.assertEquals( |
| 130 | + autoStrip == null ? Boolean.FALSE : autoStrip, |
| 131 | + ParameterUtil.getAutoStripFlag( |
| 132 | + context.readSheetHolder().getReadSheet(), context)); |
| 133 | + |
| 134 | + // sheet name match |
| 135 | + ReadSheet readSheet = context.readSheetHolder().getReadSheet(); |
| 136 | + Assertions.assertEquals(readSheet, SheetUtils.match(readSheet, context)); |
| 137 | + } |
| 138 | + }) |
| 139 | + .autoTrim(autoTrim) |
| 140 | + .autoStrip(autoStrip) |
| 141 | + .build()) { |
| 142 | + |
| 143 | + // set sheet name |
| 144 | + excelReader.read( |
| 145 | + FastExcel.readSheet(sheetNameSpaces).build(), |
| 146 | + FastExcel.readSheet(sheetNameFullWidthSpaces).build()); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + private void testAutoStripContentInternal( |
| 151 | + final ExcelTypeEnum excelType, final Boolean autoTrim, final Boolean autoStrip) { |
| 152 | + testFile = TestFileUtil.createNewFile("auto-strip-content" + excelType.getValue()); |
| 153 | + |
| 154 | + final String testContentSpaces = SPACES + "String Data1" + SPACES; |
| 155 | + final String testContentFullWidthSpaces = FW_SPACES + "String Data2" + FW_SPACES; |
| 156 | + |
| 157 | + List<ParameterData> demoList = new ArrayList<>(); |
| 158 | + ParameterData simpleData = new ParameterData(); |
| 159 | + // normal spaces |
| 160 | + simpleData.setName(testContentSpaces); |
| 161 | + demoList.add(simpleData); |
| 162 | + |
| 163 | + simpleData = new ParameterData(); |
| 164 | + // full-width spaces |
| 165 | + simpleData.setName(testContentFullWidthSpaces); |
| 166 | + demoList.add(simpleData); |
| 167 | + |
| 168 | + FastExcel.write(testFile, ParameterData.class) |
| 169 | + .excelType(excelType) |
| 170 | + .autoTrim(autoTrim) |
| 171 | + .autoStrip(autoStrip) |
| 172 | + .sheet() |
| 173 | + .doWrite(demoList); |
| 174 | + |
| 175 | + List<ParameterData> dataList = FastExcel.read(testFile) |
| 176 | + .excelType(excelType) |
| 177 | + .head(ParameterData.class) |
| 178 | + .autoTrim(autoTrim) |
| 179 | + .autoStrip(autoStrip) |
| 180 | + .sheet() |
| 181 | + .doReadSync(); |
| 182 | + |
| 183 | + log.info("Read records: {}", JSON.toJSONString(dataList)); |
| 184 | + Assertions.assertEquals(2, dataList.size()); |
| 185 | + if (Boolean.TRUE.equals(autoStrip)) { |
| 186 | + Assertions.assertEquals( |
| 187 | + StringUtils.strip(testContentSpaces), dataList.get(0).getName()); |
| 188 | + Assertions.assertEquals( |
| 189 | + StringUtils.strip(testContentFullWidthSpaces), |
| 190 | + dataList.get(1).getName()); |
| 191 | + } else if (autoTrim == null || autoTrim) { |
| 192 | + Assertions.assertEquals(testContentSpaces.trim(), dataList.get(0).getName()); |
| 193 | + Assertions.assertEquals( |
| 194 | + testContentFullWidthSpaces.trim(), dataList.get(1).getName()); |
| 195 | + } else { |
| 196 | + Assertions.assertEquals(testContentSpaces, dataList.get(0).getName()); |
| 197 | + Assertions.assertEquals(testContentFullWidthSpaces, dataList.get(1).getName()); |
| 198 | + } |
| 199 | + } |
| 200 | +} |
0 commit comments