Browse Source

完成转换测试

developing
Jiaju Zhuang 4 years ago
parent
commit
4d2b025592
  1. 4
      src/main/java/com/alibaba/excel/analysis/v07/handlers/AbstractCellValueTagHandler.java
  2. 8
      src/main/java/com/alibaba/excel/metadata/data/DataFormatData.java
  3. 7
      src/main/java/com/alibaba/excel/metadata/data/FormulaData.java
  4. 18
      src/main/java/com/alibaba/excel/metadata/data/ReadCellData.java
  5. 6
      src/main/java/com/alibaba/excel/support/ExcelTypeEnum.java
  6. 30
      src/main/java/com/alibaba/excel/util/ConverterUtils.java
  7. 10
      src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataDataListener.java
  8. 13
      src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataDataTest.java
  9. 18
      src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataReadData.java
  10. 10
      src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataWriteData.java
  11. 16
      src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataListener.java
  12. 34
      src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataTest.java
  13. 38
      src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterReadData.java
  14. 2
      src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterWriteData.java
  15. 12
      src/test/java/com/alibaba/easyexcel/test/core/encrypt/EncryptDataTest.java

4
src/main/java/com/alibaba/excel/analysis/v07/handlers/AbstractCellValueTagHandler.java

@ -4,7 +4,7 @@ import java.math.BigDecimal;
import com.alibaba.excel.context.xlsx.XlsxReadContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.data.CellData;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadSheetHolder;
import com.alibaba.excel.util.BooleanUtils;
import com.alibaba.excel.util.StringUtils;
@ -19,7 +19,7 @@ public abstract class AbstractCellValueTagHandler extends AbstractXlsxTagHandler
@Override
public void endElement(XlsxReadContext xlsxReadContext, String name) {
XlsxReadSheetHolder xlsxReadSheetHolder = xlsxReadContext.xlsxReadSheetHolder();
CellData tempCellData = xlsxReadSheetHolder.getTempCellData();
ReadCellData<?> tempCellData = xlsxReadSheetHolder.getTempCellData();
StringBuilder tempData = xlsxReadSheetHolder.getTempData();
String tempDataString = tempData.toString();
CellDataTypeEnum oldType = tempCellData.getType();

8
src/main/java/com/alibaba/excel/metadata/data/DataFormatData.java

@ -18,4 +18,12 @@ public class DataFormatData {
* format
*/
private String format;
@Override
public DataFormatData clone() {
DataFormatData dataFormatData = new DataFormatData();
dataFormatData.setIndex(getIndex());
dataFormatData.setFormat(getFormat());
return dataFormatData;
}
}

7
src/main/java/com/alibaba/excel/metadata/data/FormulaData.java

@ -13,4 +13,11 @@ public class FormulaData {
* formula
*/
private String formulaValue;
@Override
public FormulaData clone() {
FormulaData formulaData = new FormulaData();
formulaData.setFormulaValue(getFormulaValue());
return formulaData;
}
}

18
src/main/java/com/alibaba/excel/metadata/data/ReadCellData.java

@ -20,7 +20,6 @@ public class ReadCellData<T> extends CellData<T> {
*/
private DataFormatData dataFormatData;
public ReadCellData(CellDataTypeEnum type) {
super();
if (type == null) {
@ -104,10 +103,21 @@ public class ReadCellData<T> extends CellData<T> {
return cellData;
}
@Override
public ReadCellData<Object> clone(){
return new ReadCellData<>("");
public ReadCellData<Object> clone() {
ReadCellData<Object> readCellData = new ReadCellData<>();
readCellData.setType(getType());
readCellData.setNumberValue(getNumberValue());
readCellData.setStringValue(getStringValue());
readCellData.setBooleanValue(getBooleanValue());
readCellData.setData(getData());
if (getDataFormatData() != null) {
readCellData.setDataFormatData(getDataFormatData().clone());
}
if (getFormulaData() != null) {
readCellData.setFormulaData(getFormulaData().clone());
}
return readCellData;
}
}

6
src/main/java/com/alibaba/excel/support/ExcelTypeEnum.java

@ -5,13 +5,13 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import org.apache.poi.poifs.filesystem.FileMagic;
import com.alibaba.excel.exception.ExcelAnalysisException;
import com.alibaba.excel.exception.ExcelCommonException;
import com.alibaba.excel.read.metadata.ReadWorkbook;
import com.alibaba.excel.util.StringUtils;
import org.apache.poi.poifs.filesystem.FileMagic;
/**
* @author jipengfei
*/
@ -78,8 +78,6 @@ public enum ExcelTypeEnum {
return recognitionExcelType(inputStream);
} catch (ExcelCommonException e) {
throw e;
} catch (ExcelAnalysisException e) {
throw e;
} catch (Exception e) {
throw new ExcelCommonException(
"Convert excel format exception.You can try specifying the 'excelType' yourself", e);

30
src/main/java/com/alibaba/excel/util/ConverterUtils.java

@ -11,7 +11,6 @@ import com.alibaba.excel.converters.ConverterKeyBuild;
import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.exception.ExcelDataConvertException;
import com.alibaba.excel.metadata.data.CellData;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.read.metadata.holder.ReadSheetHolder;
@ -22,6 +21,7 @@ import com.alibaba.excel.read.metadata.holder.ReadSheetHolder;
* @author Jiaju Zhuang
**/
public class ConverterUtils {
public static Class<?> defaultClassGeneric = String.class;
private ConverterUtils() {}
@ -87,16 +87,8 @@ public class ConverterUtils {
} else {
clazz = field.getType();
}
if (clazz == CellData.class) {
Type type = field.getGenericType();
Class<?> classGeneric;
if (type instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType)type;
classGeneric = (Class<?>)parameterizedType.getActualTypeArguments()[0];
} else {
classGeneric = String.class;
}
if (clazz == ReadCellData.class) {
Class<?> classGeneric = getClassGeneric(field.getGenericType());
ReadCellData<Object> cellDataReturn = cellData.clone();
cellDataReturn.setData(doConvertToJavaObject(cellData, classGeneric, contentProperty, converterMap,
context, rowIndex, columnIndex));
@ -106,6 +98,22 @@ public class ConverterUtils {
columnIndex);
}
private static Class<?> getClassGeneric(Type type) {
if (!(type instanceof ParameterizedType)) {
return defaultClassGeneric;
}
ParameterizedType parameterizedType = (ParameterizedType)type;
Type[] types = parameterizedType.getActualTypeArguments();
if (types == null || types.length == 0) {
return defaultClassGeneric;
}
Type actualType = types[0];
if (!(actualType instanceof Class<?>)) {
return defaultClassGeneric;
}
return (Class<?>)actualType;
}
/**
* @param cellData
* @param clazz

10
src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataDataListener.java

@ -14,21 +14,21 @@ import org.slf4j.LoggerFactory;
/**
* @author Jiaju Zhuang
*/
public class CellDataDataListener extends AnalysisEventListener<CellDataData> {
public class CellDataDataListener extends AnalysisEventListener<CellDataReadData> {
private static final Logger LOGGER = LoggerFactory.getLogger(CellDataDataListener.class);
List<CellDataData> list = new ArrayList<CellDataData>();
List<CellDataReadData> list = new ArrayList<>();
@Override
public void invoke(CellDataData data, AnalysisContext context) {
public void invoke(CellDataReadData data, AnalysisContext context) {
list.add(data);
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
Assert.assertEquals(list.size(), 1);
CellDataData cellDataData = list.get(0);
CellDataReadData cellDataData = list.get(0);
Assert.assertEquals(cellDataData.getDate().getStringValue(), "2020年01月01日");
Assert.assertEquals(cellDataData.getDate().getData(), "2020年01月01日");
Assert.assertEquals((long)cellDataData.getInteger1().getData(), 2L);
Assert.assertEquals((long)cellDataData.getInteger2(), 2L);
Assert.assertEquals(cellDataData.getFormulaValue().getFormulaData().getFormulaValue(), "B2+C2");

13
src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataDataTest.java

@ -8,7 +8,6 @@ import java.util.List;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.data.CellData;
import com.alibaba.excel.metadata.data.FormulaData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.util.DateUtils;
@ -44,15 +43,15 @@ public class CellDataDataTest {
}
private void readAndWrite(File file) throws Exception {
EasyExcel.write(file, CellDataData.class).sheet().doWrite(data());
EasyExcel.read(file, CellDataData.class, new CellDataDataListener()).sheet().doRead();
EasyExcel.write(file, CellDataWriteData.class).sheet().doWrite(data());
EasyExcel.read(file, CellDataReadData.class, new CellDataDataListener()).sheet().doRead();
}
private List<CellDataData> data() throws Exception {
List<CellDataData> list = new ArrayList<CellDataData>();
CellDataData cellDataData = new CellDataData();
private List<CellDataWriteData> data() throws Exception {
List<CellDataWriteData> list = new ArrayList<>();
CellDataWriteData cellDataData = new CellDataWriteData();
cellDataData.setDate(new WriteCellData<>(DateUtils.parseDate("2020-01-01 01:01:01")));
CellData<Integer> integer1 = new CellData<Integer>();
WriteCellData<Integer> integer1 = new WriteCellData<>();
integer1.setType(CellDataTypeEnum.NUMBER);
integer1.setNumberValue(BigDecimal.valueOf(2L));
cellDataData.setInteger1(integer1);

18
src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataReadData.java

@ -0,0 +1,18 @@
package com.alibaba.easyexcel.test.core.celldata;
import com.alibaba.excel.annotation.format.DateTimeFormat;
import com.alibaba.excel.metadata.data.ReadCellData;
import lombok.Data;
/**
* @author Jiaju Zhuang
*/
@Data
public class CellDataReadData {
@DateTimeFormat("yyyy年MM月dd日")
private ReadCellData<String> date;
private ReadCellData<Integer> integer1;
private Integer integer2;
private ReadCellData<?> formulaValue;
}

10
src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataData.java → src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataWriteData.java

@ -3,7 +3,7 @@ package com.alibaba.easyexcel.test.core.celldata;
import java.util.Date;
import com.alibaba.excel.annotation.format.DateTimeFormat;
import com.alibaba.excel.metadata.data.CellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import lombok.Data;
@ -11,10 +11,10 @@ import lombok.Data;
* @author Jiaju Zhuang
*/
@Data
public class CellDataData {
public class CellDataWriteData {
@DateTimeFormat("yyyy年MM月dd日")
private CellData<Date> date;
private CellData<Integer> integer1;
private WriteCellData<Date> date;
private WriteCellData<Integer> integer1;
private Integer integer2;
private CellData<?> formulaValue;
private WriteCellData<?> formulaValue;
}

16
src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataListener.java

@ -5,32 +5,32 @@ import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import org.junit.Assert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.exception.ExcelCommonException;
import com.alibaba.excel.util.DateUtils;
import com.alibaba.fastjson.JSON;
import org.junit.Assert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Jiaju Zhuang
*/
public class ConverterDataListener extends AnalysisEventListener<ConverterData> {
public class ConverterDataListener extends AnalysisEventListener<ConverterReadData> {
private static final Logger LOGGER = LoggerFactory.getLogger(ConverterDataListener.class);
List<ConverterData> list = new ArrayList<ConverterData>();
private List<ConverterReadData> list = new ArrayList<>();
@Override
public void invoke(ConverterData data, AnalysisContext context) {
public void invoke(ConverterReadData data, AnalysisContext context) {
list.add(data);
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
Assert.assertEquals(list.size(), 1);
ConverterData data = list.get(0);
ConverterReadData data = list.get(0);
try {
Assert.assertEquals(DateUtils.parseDate("2020-01-01 01:01:01"), data.getDate());
} catch (ParseException e) {

34
src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataTest.java

@ -48,8 +48,8 @@ public class ConverterDataTest {
}
private void readAndWrite(File file) throws Exception {
EasyExcel.write(file, ConverterData.class).sheet().doWrite(data());
EasyExcel.read(file, ConverterData.class, new ConverterDataListener()).sheet().doRead();
EasyExcel.write(file, ConverterWriteData.class).sheet().doWrite(data());
EasyExcel.read(file, ConverterReadData.class, new ConverterDataListener()).sheet().doRead();
}
@Test
@ -97,21 +97,21 @@ public class ConverterDataTest {
.sheet().doRead();
}
private List<ConverterData> data() throws Exception {
List<ConverterData> list = new ArrayList<ConverterData>();
ConverterData converterData = new ConverterData();
converterData.setDate(DateUtils.parseDate("2020-01-01 01:01:01"));
converterData.setBooleanData(Boolean.TRUE);
converterData.setBigDecimal(BigDecimal.ONE);
converterData.setLongData(1L);
converterData.setIntegerData(1);
converterData.setShortData((short)1);
converterData.setByteData((byte)1);
converterData.setDoubleData(1.0);
converterData.setFloatData((float)1.0);
converterData.setString("测试");
converterData.setCellData(new WriteCellData<>("自定义"));
list.add(converterData);
private List<ConverterWriteData> data() throws Exception {
List<ConverterWriteData> list = new ArrayList<ConverterWriteData>();
ConverterWriteData converterWriteData = new ConverterWriteData();
converterWriteData.setDate(DateUtils.parseDate("2020-01-01 01:01:01"));
converterWriteData.setBooleanData(Boolean.TRUE);
converterWriteData.setBigDecimal(BigDecimal.ONE);
converterWriteData.setLongData(1L);
converterWriteData.setIntegerData(1);
converterWriteData.setShortData((short)1);
converterWriteData.setByteData((byte)1);
converterWriteData.setDoubleData(1.0);
converterWriteData.setFloatData((float)1.0);
converterWriteData.setString("测试");
converterWriteData.setCellData(new WriteCellData<>("自定义"));
list.add(converterWriteData);
return list;
}
}

38
src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterReadData.java

@ -0,0 +1,38 @@
package com.alibaba.easyexcel.test.core.converter;
import java.math.BigDecimal;
import java.util.Date;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.metadata.data.ReadCellData;
import lombok.Data;
/**
* @author Jiaju Zhuang
*/
@Data
public class ConverterReadData {
@ExcelProperty("日期")
private Date date;
@ExcelProperty("布尔")
private Boolean booleanData;
@ExcelProperty("大数")
private BigDecimal bigDecimal;
@ExcelProperty("长整型")
private long longData;
@ExcelProperty("整型")
private Integer integerData;
@ExcelProperty("短整型")
private Short shortData;
@ExcelProperty("字节型")
private Byte byteData;
@ExcelProperty("双精度浮点型")
private double doubleData;
@ExcelProperty("浮点型")
private Float floatData;
@ExcelProperty("字符串")
private String string;
@ExcelProperty("自定义")
private ReadCellData<?> cellData;
}

2
src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterData.java → src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterWriteData.java

@ -12,7 +12,7 @@ import lombok.Data;
* @author Jiaju Zhuang
*/
@Data
public class ConverterData {
public class ConverterWriteData {
@ExcelProperty("日期")
private Date date;
@ExcelProperty("布尔")

12
src/test/java/com/alibaba/easyexcel/test/core/encrypt/EncryptDataTest.java

@ -6,16 +6,16 @@ import java.io.FileOutputStream;
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.simple.SimpleData;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
/**
*
* @author Jiaju Zhuang
@ -30,7 +30,7 @@ public class EncryptDataTest {
@BeforeClass
public static void init() {
file07 = TestFileUtil.createNewFile("encrypt07.xlsx");
file07 = TestFileUtil.createNewFile("encrypt07111.xlsx");
file03 = TestFileUtil.createNewFile("encrypt03.xls");
file07OutputStream = TestFileUtil.createNewFile("encryptOutputStream07.xlsx");
file03OutputStream = TestFileUtil.createNewFile("encryptOutputStream03.xls");

Loading…
Cancel
Save