From 34cd600315dab2d1aa9f8ed8b6359ac3e8cae5cd Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Fri, 24 Apr 2020 23:16:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=8B=A6=E6=88=AA=E5=99=A8?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E6=A1=88=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test/core/handler/WriteHandler.java | 280 ++++++++++++++++++ .../test/core/handler/WriteHandlerData.java | 14 + .../test/core/handler/WriteHandlerTest.java | 96 ++++++ 3 files changed, 390 insertions(+) create mode 100644 src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandler.java create mode 100644 src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandlerData.java create mode 100644 src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandlerTest.java diff --git a/src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandler.java b/src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandler.java new file mode 100644 index 00000000..c762db79 --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandler.java @@ -0,0 +1,280 @@ +package com.alibaba.easyexcel.test.core.handler; + +import java.util.List; + +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.Row; +import org.junit.Assert; + +import com.alibaba.excel.metadata.CellData; +import com.alibaba.excel.metadata.Head; +import com.alibaba.excel.write.handler.CellWriteHandler; +import com.alibaba.excel.write.handler.RowWriteHandler; +import com.alibaba.excel.write.handler.SheetWriteHandler; +import com.alibaba.excel.write.handler.WorkbookWriteHandler; +import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; +import com.alibaba.excel.write.metadata.holder.WriteTableHolder; +import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder; + +/** + * + * + * @author JiaJu Zhuang + **/ +public class WriteHandler implements WorkbookWriteHandler, SheetWriteHandler, RowWriteHandler, CellWriteHandler { + + private long beforeCellCreate = 0L; + private long afterCellCreate = 0L; + private long afterCellDataConverted = 0L; + private long afterCellDispose = 0L; + private long beforeRowCreate = 0L; + private long afterRowCreate = 0L; + private long afterRowDispose = 0L; + private long beforeSheetCreate = 0L; + private long afterSheetCreate = 0L; + private long beforeWorkbookCreate = 0L; + private long afterWorkbookCreate = 0L; + private long afterWorkbookDispose = 0L; + + + @Override + public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, + Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) { + if (isHead) { + Assert.assertEquals(0L, beforeCellCreate); + Assert.assertEquals(0L, afterCellCreate); + Assert.assertEquals(0L, afterCellDataConverted); + Assert.assertEquals(0L, afterCellDispose); + Assert.assertEquals(1L, beforeRowCreate); + Assert.assertEquals(1L, afterRowCreate); + Assert.assertEquals(0L, afterRowDispose); + Assert.assertEquals(1L, beforeSheetCreate); + Assert.assertEquals(1L, afterSheetCreate); + Assert.assertEquals(1L, beforeWorkbookCreate); + Assert.assertEquals(1L, afterWorkbookCreate); + Assert.assertEquals(0L, afterWorkbookDispose); + beforeCellCreate++; + } + + } + + @Override + public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, + Head head, Integer relativeRowIndex, Boolean isHead) { + if (isHead) { + Assert.assertEquals(1L, beforeCellCreate); + Assert.assertEquals(0L, afterCellCreate); + Assert.assertEquals(0L, afterCellDataConverted); + Assert.assertEquals(0L, afterCellDispose); + Assert.assertEquals(1L, beforeRowCreate); + Assert.assertEquals(1L, afterRowCreate); + Assert.assertEquals(0L, afterRowDispose); + Assert.assertEquals(1L, beforeSheetCreate); + Assert.assertEquals(1L, afterSheetCreate); + Assert.assertEquals(1L, beforeWorkbookCreate); + Assert.assertEquals(1L, afterWorkbookCreate); + Assert.assertEquals(0L, afterWorkbookDispose); + afterCellCreate++; + } + } + + @Override + public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, + CellData cellData, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) { + Assert.assertEquals(1L, beforeCellCreate); + Assert.assertEquals(1L, afterCellCreate); + Assert.assertEquals(0L, afterCellDataConverted); + Assert.assertEquals(1, afterCellDispose); + Assert.assertEquals(1L, beforeRowCreate); + Assert.assertEquals(1L, afterRowCreate); + Assert.assertEquals(1L, afterRowDispose); + Assert.assertEquals(1L, beforeSheetCreate); + Assert.assertEquals(1L, afterSheetCreate); + Assert.assertEquals(1L, beforeWorkbookCreate); + Assert.assertEquals(1L, afterWorkbookCreate); + Assert.assertEquals(0L, afterWorkbookDispose); + afterCellDataConverted++; + } + + @Override + public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, + List cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) { + if (isHead) { + Assert.assertEquals(1L, beforeCellCreate); + Assert.assertEquals(1L, afterCellCreate); + Assert.assertEquals(0L, afterCellDataConverted); + Assert.assertEquals(0L, afterCellDispose); + Assert.assertEquals(1L, beforeRowCreate); + Assert.assertEquals(1L, afterRowCreate); + Assert.assertEquals(0L, afterRowDispose); + Assert.assertEquals(1L, beforeSheetCreate); + Assert.assertEquals(1L, afterSheetCreate); + Assert.assertEquals(1L, beforeWorkbookCreate); + Assert.assertEquals(1L, afterWorkbookCreate); + Assert.assertEquals(0L, afterWorkbookDispose); + afterCellDispose++; + } + } + + @Override + public void beforeRowCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Integer rowIndex, + Integer relativeRowIndex, Boolean isHead) { + if (isHead) { + Assert.assertEquals(0L, beforeCellCreate); + Assert.assertEquals(0L, afterCellCreate); + Assert.assertEquals(0L, afterCellDataConverted); + Assert.assertEquals(0L, afterCellDispose); + Assert.assertEquals(0L, beforeRowCreate); + Assert.assertEquals(0L, afterRowCreate); + Assert.assertEquals(0L, afterRowDispose); + Assert.assertEquals(1L, beforeSheetCreate); + Assert.assertEquals(1L, afterSheetCreate); + Assert.assertEquals(1L, beforeWorkbookCreate); + Assert.assertEquals(1L, afterWorkbookCreate); + Assert.assertEquals(0L, afterWorkbookDispose); + beforeRowCreate++; + } + + } + + @Override + public void afterRowCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, + Integer relativeRowIndex, Boolean isHead) { + if (isHead) { + Assert.assertEquals(0L, beforeCellCreate); + Assert.assertEquals(0L, afterCellCreate); + Assert.assertEquals(0L, afterCellDataConverted); + Assert.assertEquals(0L, afterCellDispose); + Assert.assertEquals(1L, beforeRowCreate); + Assert.assertEquals(0L, afterRowCreate); + Assert.assertEquals(0L, afterRowDispose); + Assert.assertEquals(1L, beforeSheetCreate); + Assert.assertEquals(1L, afterSheetCreate); + Assert.assertEquals(1L, beforeWorkbookCreate); + Assert.assertEquals(1L, afterWorkbookCreate); + Assert.assertEquals(0L, afterWorkbookDispose); + afterRowCreate++; + } + } + + @Override + public void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, + Integer relativeRowIndex, Boolean isHead) { + if (isHead) { + Assert.assertEquals(1L, beforeCellCreate); + Assert.assertEquals(1L, afterCellCreate); + Assert.assertEquals(0L, afterCellDataConverted); + Assert.assertEquals(1L, afterCellDispose); + Assert.assertEquals(1L, beforeRowCreate); + Assert.assertEquals(1L, afterRowCreate); + Assert.assertEquals(0L, afterRowDispose); + Assert.assertEquals(1L, beforeSheetCreate); + Assert.assertEquals(1L, afterSheetCreate); + Assert.assertEquals(1L, beforeWorkbookCreate); + Assert.assertEquals(1L, afterWorkbookCreate); + Assert.assertEquals(0L, afterWorkbookDispose); + afterRowDispose++; + } + } + + @Override + public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) { + Assert.assertEquals(0L, beforeCellCreate); + Assert.assertEquals(0L, afterCellCreate); + Assert.assertEquals(0L, afterCellDataConverted); + Assert.assertEquals(0L, afterCellDispose); + Assert.assertEquals(0L, beforeRowCreate); + Assert.assertEquals(0L, afterRowCreate); + Assert.assertEquals(0L, afterRowDispose); + Assert.assertEquals(0L, beforeSheetCreate); + Assert.assertEquals(0L, afterSheetCreate); + Assert.assertEquals(1L, beforeWorkbookCreate); + Assert.assertEquals(1L, afterWorkbookCreate); + Assert.assertEquals(0L, afterWorkbookDispose); + beforeSheetCreate++; + } + + @Override + public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) { + Assert.assertEquals(0L, beforeCellCreate); + Assert.assertEquals(0L, afterCellCreate); + Assert.assertEquals(0L, afterCellDataConverted); + Assert.assertEquals(0L, afterCellDispose); + Assert.assertEquals(0L, beforeRowCreate); + Assert.assertEquals(0L, afterRowCreate); + Assert.assertEquals(0L, afterRowDispose); + Assert.assertEquals(1L, beforeSheetCreate); + Assert.assertEquals(0L, afterSheetCreate); + Assert.assertEquals(1L, beforeWorkbookCreate); + Assert.assertEquals(1L, afterWorkbookCreate); + Assert.assertEquals(0L, afterWorkbookDispose); + afterSheetCreate++; + } + + @Override + public void beforeWorkbookCreate() { + Assert.assertEquals(0L, beforeCellCreate); + Assert.assertEquals(0L, afterCellCreate); + Assert.assertEquals(0L, afterCellDataConverted); + Assert.assertEquals(0L, afterCellDispose); + Assert.assertEquals(0L, beforeRowCreate); + Assert.assertEquals(0L, afterRowCreate); + Assert.assertEquals(0L, afterRowDispose); + Assert.assertEquals(0L, beforeSheetCreate); + Assert.assertEquals(0L, afterSheetCreate); + Assert.assertEquals(0L, beforeWorkbookCreate); + Assert.assertEquals(0L, afterWorkbookCreate); + Assert.assertEquals(0L, afterWorkbookDispose); + beforeWorkbookCreate++; + } + + @Override + public void afterWorkbookCreate(WriteWorkbookHolder writeWorkbookHolder) { + Assert.assertEquals(0L, beforeCellCreate); + Assert.assertEquals(0L, afterCellCreate); + Assert.assertEquals(0L, afterCellDataConverted); + Assert.assertEquals(0L, afterCellDispose); + Assert.assertEquals(0L, beforeRowCreate); + Assert.assertEquals(0L, afterRowCreate); + Assert.assertEquals(0L, afterRowDispose); + Assert.assertEquals(0L, beforeSheetCreate); + Assert.assertEquals(0L, afterSheetCreate); + Assert.assertEquals(1L, beforeWorkbookCreate); + Assert.assertEquals(0L, afterWorkbookCreate); + Assert.assertEquals(0L, afterWorkbookDispose); + afterWorkbookCreate++; + } + + @Override + public void afterWorkbookDispose(WriteWorkbookHolder writeWorkbookHolder) { + Assert.assertEquals(1L, beforeCellCreate); + Assert.assertEquals(1L, afterCellCreate); + Assert.assertEquals(1L, afterCellDataConverted); + Assert.assertEquals(1L, afterCellDispose); + Assert.assertEquals(1L, beforeRowCreate); + Assert.assertEquals(1L, afterRowCreate); + Assert.assertEquals(1L, afterRowDispose); + Assert.assertEquals(1L, beforeSheetCreate); + Assert.assertEquals(1L, afterSheetCreate); + Assert.assertEquals(1L, beforeWorkbookCreate); + Assert.assertEquals(1L, afterWorkbookCreate); + Assert.assertEquals(0L, afterWorkbookDispose); + afterWorkbookDispose++; + + } + + public void afterAll() { + Assert.assertEquals(1L, beforeCellCreate); + Assert.assertEquals(1L, afterCellCreate); + Assert.assertEquals(1L, afterCellDataConverted); + Assert.assertEquals(1L, afterCellDispose); + Assert.assertEquals(1L, beforeRowCreate); + Assert.assertEquals(1L, afterRowCreate); + Assert.assertEquals(1L, afterRowDispose); + Assert.assertEquals(1L, beforeSheetCreate); + Assert.assertEquals(1L, afterSheetCreate); + Assert.assertEquals(1L, beforeWorkbookCreate); + Assert.assertEquals(1L, afterWorkbookCreate); + Assert.assertEquals(1L, afterWorkbookDispose); + } +} diff --git a/src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandlerData.java b/src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandlerData.java new file mode 100644 index 00000000..ad2188aa --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandlerData.java @@ -0,0 +1,14 @@ +package com.alibaba.easyexcel.test.core.handler; + +import com.alibaba.excel.annotation.ExcelProperty; + +import lombok.Data; + +/** + * @author Jiaju Zhuang + */ +@Data +public class WriteHandlerData { + @ExcelProperty("姓名") + private String name; +} diff --git a/src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandlerTest.java b/src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandlerTest.java new file mode 100644 index 00000000..a96d133e --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandlerTest.java @@ -0,0 +1,96 @@ +package com.alibaba.easyexcel.test.core.handler; + +import java.io.File; +import java.text.ParseException; +import java.util.ArrayList; +import java.util.List; + +import org.junit.BeforeClass; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runners.MethodSorters; + +import com.alibaba.easyexcel.test.core.head.ListHeadDataListener; +import com.alibaba.easyexcel.test.core.simple.SimpleData; +import com.alibaba.easyexcel.test.util.TestFileUtil; +import com.alibaba.excel.EasyExcel; +import com.alibaba.excel.util.DateUtils; +import com.alibaba.excel.write.handler.WorkbookWriteHandler; + +/** + * + * @author Jiaju Zhuang + */ +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class WriteHandlerTest { + + private static File file07; + private static File file03; + + @BeforeClass + public static void init() { + file07 = TestFileUtil.createNewFile("writeHandler07.xlsx"); + file03 = TestFileUtil.createNewFile("writeHandler03.xls"); + } + + @Test + public void t01WorkbookWrite07() throws Exception { + workbookWrite(file07); + } + + @Test + public void t02WorkbookWrite03() throws Exception { + workbookWrite(file03); + } + + @Test + public void t03SheetWrite07() throws Exception { + sheetWrite(file07); + } + + @Test + public void t04SheetWrite03() throws Exception { + sheetWrite(file03); + } + + @Test + public void t05TableWrite07() throws Exception { + workbookWrite(file07); + tableWrite(file07); + } + + @Test + public void t06TableWrite03() throws Exception { + tableWrite(file03); + } + + + private void workbookWrite(File file) { + WriteHandler writeHandler = new WriteHandler(); + EasyExcel.write(file).head(WriteHandlerData.class).registerWriteHandler(writeHandler).sheet().doWrite(data()); + writeHandler.afterAll(); + } + + private void sheetWrite(File file) { + WriteHandler writeHandler = new WriteHandler(); + EasyExcel.write(file).head(WriteHandlerData.class).sheet().registerWriteHandler(writeHandler).doWrite(data()); + writeHandler.afterAll(); + } + + private void tableWrite(File file) { + WriteHandler writeHandler = new WriteHandler(); + EasyExcel.write(file).head(WriteHandlerData.class).sheet().table(0).registerWriteHandler(writeHandler) + .doWrite(data()); + writeHandler.afterAll(); + } + + private List data() { + List list = new ArrayList(); + for (int i = 0; i < 1; i++) { + WriteHandlerData data = new WriteHandlerData(); + data.setName("姓名" + i); + list.add(data); + } + return list; + } +}