Browse Source

新增拦截器测试案例

developing
Jiaju Zhuang 5 years ago
parent
commit
34cd600315
  1. 280
      src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandler.java
  2. 14
      src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandlerData.java
  3. 96
      src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandlerTest.java

280
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<CellData> 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);
}
}

14
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;
}

96
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<WriteHandlerData> data() {
List<WriteHandlerData> list = new ArrayList<WriteHandlerData>();
for (int i = 0; i < 1; i++) {
WriteHandlerData data = new WriteHandlerData();
data.setName("姓名" + i);
list.add(data);
}
return list;
}
}
Loading…
Cancel
Save