Browse Source

修复测试案例异常

pull/2144/head
Jiaju Zhuang 3 years ago
parent
commit
66cb547277
  1. 10
      src/main/java/com/alibaba/excel/util/ClassUtils.java
  2. 2
      src/main/java/com/alibaba/excel/util/StyleUtil.java
  3. 1
      src/main/java/com/alibaba/excel/write/property/ExcelWriteHeadProperty.java
  4. 2
      src/test/java/com/alibaba/easyexcel/test/core/fill/FillDataTest.java
  5. 7
      src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleDataTest.java
  6. 43
      src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java
  7. 5
      src/test/java/com/alibaba/easyexcel/test/core/noncamel/UnCamelDataListener.java

10
src/main/java/com/alibaba/excel/util/ClassUtils.java

@ -16,14 +16,18 @@ import java.util.concurrent.ConcurrentHashMap;
import com.alibaba.excel.annotation.ExcelIgnore; import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated; import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.format.DateTimeFormat;
import com.alibaba.excel.annotation.format.NumberFormat;
import com.alibaba.excel.annotation.write.style.ContentFontStyle; import com.alibaba.excel.annotation.write.style.ContentFontStyle;
import com.alibaba.excel.annotation.write.style.ContentStyle; import com.alibaba.excel.annotation.write.style.ContentStyle;
import com.alibaba.excel.converters.AutoConverter; import com.alibaba.excel.converters.AutoConverter;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.exception.ExcelCommonException; import com.alibaba.excel.exception.ExcelCommonException;
import com.alibaba.excel.metadata.Holder; import com.alibaba.excel.metadata.Holder;
import com.alibaba.excel.metadata.property.DateTimeFormatProperty;
import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.metadata.property.FontProperty; import com.alibaba.excel.metadata.property.FontProperty;
import com.alibaba.excel.metadata.property.NumberFormatProperty;
import com.alibaba.excel.metadata.property.StyleProperty; import com.alibaba.excel.metadata.property.StyleProperty;
import com.alibaba.excel.write.metadata.holder.WriteHolder; import com.alibaba.excel.write.metadata.holder.WriteHolder;
@ -175,6 +179,12 @@ public class ClassUtils {
contentFontStyle = parentContentFontStyle; contentFontStyle = parentContentFontStyle;
} }
excelContentProperty.setContentFontProperty(FontProperty.build(contentFontStyle)); excelContentProperty.setContentFontProperty(FontProperty.build(contentFontStyle));
excelContentProperty.setDateTimeFormatProperty(
DateTimeFormatProperty.build(field.getAnnotation(DateTimeFormat.class)));
excelContentProperty.setNumberFormatProperty(
NumberFormatProperty.build(field.getAnnotation(NumberFormat.class)));
fieldContentMap.put(field.getName(), excelContentProperty); fieldContentMap.put(field.getName(), excelContentProperty);
} }
return fieldContentMap; return fieldContentMap;

2
src/main/java/com/alibaba/excel/util/StyleUtil.java

@ -142,7 +142,7 @@ public class StyleUtil {
return null; return null;
} }
Font font = createFont(workbook, originFont, writeFont); Font font = createFont(workbook, originFont, writeFont);
if (writeFont == null) { if (writeFont == null || font == null) {
return font; return font;
} }
if (writeFont.getFontName() != null) { if (writeFont.getFontName() != null) {

1
src/main/java/com/alibaba/excel/write/property/ExcelWriteHeadProperty.java

@ -54,7 +54,6 @@ public class ExcelWriteHeadProperty extends ExcelHeadProperty {
HeadFontStyle parentHeadFontStyle = headClazz.getAnnotation(HeadFontStyle.class); HeadFontStyle parentHeadFontStyle = headClazz.getAnnotation(HeadFontStyle.class);
for (Map.Entry<Integer, Head> entry : getHeadMap().entrySet()) { for (Map.Entry<Integer, Head> entry : getHeadMap().entrySet()) {
Integer index = entry.getKey();
Head headData = entry.getValue(); Head headData = entry.getValue();
if (headData == null) { if (headData == null) {
throw new IllegalArgumentException( throw new IllegalArgumentException(

2
src/test/java/com/alibaba/easyexcel/test/core/fill/FillDataTest.java

@ -91,7 +91,7 @@ public class FillDataTest {
public void t03FillCsv() { public void t03FillCsv() {
ExcelGenerateException excelGenerateException = Assert.assertThrows(ExcelGenerateException.class, ExcelGenerateException excelGenerateException = Assert.assertThrows(ExcelGenerateException.class,
() -> fill(fileCsv, simpleTemplateCsv)); () -> fill(fileCsv, simpleTemplateCsv));
Assert.assertEquals("Calling the 'fill' method must use a template.", excelGenerateException.getMessage()); Assert.assertEquals("csv cannot use template.", excelGenerateException.getMessage());
} }
@Test @Test

7
src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleDataTest.java

@ -4,7 +4,6 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.util.List; import java.util.List;
import com.alibaba.easyexcel.test.core.fill.FillData;
import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel; import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.Head;
@ -72,7 +71,7 @@ public class FillStyleDataTest {
Assert.assertTrue(cell0.getCellStyle().getFont().getBold()); Assert.assertTrue(cell0.getCellStyle().getFont().getBold());
XSSFCell cell1 = row.getCell(1); XSSFCell cell1 = row.getCell(1);
Assert.assertEquals("5.2", cell1.getStringCellValue()); Assert.assertEquals(5.2, cell1.getNumericCellValue(), 1);
Assert.assertEquals(0, cell1.getCellStyle().getDataFormat()); Assert.assertEquals(0, cell1.getCellStyle().getDataFormat());
Assert.assertEquals("FF92D050", cell1.getCellStyle().getFillForegroundColorColor().getARGBHex()); Assert.assertEquals("FF92D050", cell1.getCellStyle().getFillForegroundColorColor().getARGBHex());
Assert.assertEquals("FF4BACC6", cell1.getCellStyle().getFont().getXSSFColor().getARGBHex()); Assert.assertEquals("FF4BACC6", cell1.getCellStyle().getFont().getXSSFColor().getARGBHex());
@ -126,7 +125,7 @@ public class FillStyleDataTest {
Assert.assertTrue(cell0.getCellStyle().getFont(workbook).getBold()); Assert.assertTrue(cell0.getCellStyle().getFont(workbook).getBold());
HSSFCell cell1 = row.getCell(1); HSSFCell cell1 = row.getCell(1);
Assert.assertEquals("5.2", cell1.getStringCellValue()); Assert.assertEquals(5.2, cell1.getNumericCellValue(), 1);
Assert.assertEquals(0, cell1.getCellStyle().getDataFormat()); Assert.assertEquals(0, cell1.getCellStyle().getDataFormat());
Assert.assertEquals("9999:CCCC:0", cell1.getCellStyle().getFillForegroundColorColor().getHexString()); Assert.assertEquals("9999:CCCC:0", cell1.getCellStyle().getFillForegroundColorColor().getHexString());
Assert.assertEquals("0:8080:8080", cell1.getCellStyle().getFont(workbook).getHSSFColor(workbook) Assert.assertEquals("0:8080:8080", cell1.getCellStyle().getFont(workbook).getHSSFColor(workbook)
@ -166,7 +165,7 @@ public class FillStyleDataTest {
} }
private void fill(File file, File template) throws Exception { private void fill(File file, File template) throws Exception {
EasyExcel.write(file, FillData.class).withTemplate(template).sheet().doFill(data()); EasyExcel.write(file, FillStyleData.class).withTemplate(template).sheet().doFill(data());
} }
@Test @Test

43
src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java

@ -1,6 +1,7 @@
package com.alibaba.easyexcel.test.core.large; package com.alibaba.easyexcel.test.core.large;
import java.io.File; import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -9,6 +10,10 @@ import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet; import com.alibaba.excel.write.metadata.WriteSheet;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.FixMethodOrder; import org.junit.FixMethodOrder;
import org.junit.Test; import org.junit.Test;
@ -25,11 +30,16 @@ public class LargeDataTest {
private static File fileFill07; private static File fileFill07;
private static File template07; private static File template07;
private static File fileCsv; private static File fileCsv;
private static File fileWrite07;
private static File fileWritePoi07;
private int i = 0; private int i = 0;
@BeforeClass @BeforeClass
public static void init() { public static void init() {
fileFill07 = TestFileUtil.createNewFile("largefill07.xlsx"); fileFill07 = TestFileUtil.createNewFile("largefill07.xlsx");
fileWrite07 = TestFileUtil.createNewFile("large" + File.separator + "fileWrite07.xlsx");
fileWritePoi07 = TestFileUtil.createNewFile("large" + File.separator + "fileWritePoi07.xlsx");
template07 = TestFileUtil.readFile("large" + File.separator + "fill.xlsx"); template07 = TestFileUtil.readFile("large" + File.separator + "fill.xlsx");
fileCsv = TestFileUtil.createNewFile("largefileCsv.csv"); fileCsv = TestFileUtil.createNewFile("largefileCsv.csv");
} }
@ -72,6 +82,39 @@ public class LargeDataTest {
LOGGER.info("CSV large data total time spent:{}", System.currentTimeMillis() - start); LOGGER.info("CSV large data total time spent:{}", System.currentTimeMillis() - start);
} }
@Test
public void t04Write() throws Exception {
long start = System.currentTimeMillis();
ExcelWriter excelWriter = EasyExcel.write(fileWrite07).build();
WriteSheet writeSheet = EasyExcel.writerSheet().build();
for (int j = 0; j < 100; j++) {
excelWriter.write(data(), writeSheet);
LOGGER.info("{} write success.", j);
}
excelWriter.finish();
LOGGER.info("write cost:{}", System.currentTimeMillis() - start);
start = System.currentTimeMillis();
try (FileOutputStream fileOutputStream = new FileOutputStream(fileWritePoi07)) {
SXSSFWorkbook workbook = new SXSSFWorkbook();
SXSSFSheet sheet = workbook.createSheet("sheet1");
for (int i = 0; i < 100 * 5000; i++) {
SXSSFRow row = sheet.createRow(i);
for (int j = 0; j < 25; j++) {
SXSSFCell cell = row.createCell(j);
cell.setCellValue("str-" + j + "-" + i);
}
if (i % 5000 == 0) {
LOGGER.info("{} write success.", i);
}
}
workbook.write(fileOutputStream);
workbook.dispose();
workbook.close();
}
LOGGER.info("poi write cost:{}", System.currentTimeMillis() - start);
}
private List<LargeData> data() { private List<LargeData> data() {
List<LargeData> list = new ArrayList<>(); List<LargeData> list = new ArrayList<>();
int size = i + 5000; int size = i + 5000;

5
src/test/java/com/alibaba/easyexcel/test/core/noncamel/UnCamelDataListener.java

@ -22,11 +22,12 @@ public class UnCamelDataListener extends AnalysisEventListener<UnCamelData> {
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) { public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
log.debug("Head is:{}", JSON.toJSONString(headMap)); log.debug("Head is:{}", JSON.toJSONString(headMap));
Assert.assertEquals(headMap.get(0), "string1"); Assert.assertEquals(headMap.get(0), "string1");
Assert.assertEquals(headMap.get(1), "String2"); Assert.assertEquals(headMap.get(1), "string2");
Assert.assertEquals(headMap.get(2), "sTring3"); Assert.assertEquals(headMap.get(2), "STring3");
Assert.assertEquals(headMap.get(3), "STring4"); Assert.assertEquals(headMap.get(3), "STring4");
Assert.assertEquals(headMap.get(4), "STRING5"); Assert.assertEquals(headMap.get(4), "STRING5");
Assert.assertEquals(headMap.get(5), "STRing6"); Assert.assertEquals(headMap.get(5), "STRing6");
} }
@Override @Override

Loading…
Cancel
Save