Browse Source

完成csv 测试案例

developing
Jiaju Zhuang 3 years ago
parent
commit
fce94542a4
  1. 5
      src/main/java/com/alibaba/excel/util/WorkBookUtil.java
  2. 12
      src/test/java/com/alibaba/easyexcel/test/core/multiplesheets/MultipleSheetsDataTest.java
  3. 10
      src/test/java/com/alibaba/easyexcel/test/core/nomodel/NoModelDataTest.java
  4. 8
      src/test/java/com/alibaba/easyexcel/test/core/noncamel/UnCamelDataTest.java
  5. 70
      src/test/java/com/alibaba/easyexcel/test/core/parameter/ParameterDataTest.java
  6. 29
      src/test/java/com/alibaba/easyexcel/test/core/repetition/RepetitionDataTest.java
  7. 23
      src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataTest.java
  8. 23
      src/test/java/com/alibaba/easyexcel/test/core/skip/SkipDataTest.java
  9. 24
      src/test/java/com/alibaba/easyexcel/test/core/sort/SortDataTest.java
  10. 2
      src/test/java/com/alibaba/easyexcel/test/core/style/StyleDataTest.java
  11. 7
      src/test/java/com/alibaba/easyexcel/test/core/template/TemplateDataTest.java
  12. 5
      src/test/java/com/alibaba/easyexcel/test/temp/csv/CsvReadTest.java

5
src/main/java/com/alibaba/excel/util/WorkBookUtil.java

@ -2,6 +2,7 @@ package com.alibaba.excel.util;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import com.alibaba.excel.metadata.csv.CsvWorkbook;
import com.alibaba.excel.metadata.data.DataFormatData;
@ -67,8 +68,8 @@ public class WorkBookUtil {
}
return;
case CSV:
CsvWorkbook csvWorkbook = new CsvWorkbook(
new OutputStreamWriter(writeWorkbookHolder.getOutputStream(), writeWorkbookHolder.getCharset()),
CsvWorkbook csvWorkbook = new CsvWorkbook(new PrintWriter(
new OutputStreamWriter(writeWorkbookHolder.getOutputStream(), writeWorkbookHolder.getCharset())),
writeWorkbookHolder.getGlobalConfiguration().getLocale(),
writeWorkbookHolder.getGlobalConfiguration().getUse1904windowing(),
writeWorkbookHolder.getGlobalConfiguration().getUseScientificFormat());

12
src/test/java/com/alibaba/easyexcel/test/core/multiplesheets/MultipleSheetsDataTest.java

@ -3,17 +3,17 @@ package com.alibaba.easyexcel.test.core.multiplesheets;
import java.io.File;
import java.util.List;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
/**
*
* @author Jiaju Zhuang
@ -57,7 +57,7 @@ public class MultipleSheetsDataTest {
int count = 1;
for (ReadSheet readSheet : sheets) {
excelReader.read(readSheet);
Assert.assertEquals((long)multipleSheetsListener.getList().size(), (long)count);
Assert.assertEquals(multipleSheetsListener.getList().size(), count);
count++;
}
excelReader.finish();

10
src/test/java/com/alibaba/easyexcel/test/core/nomodel/NoModelDataTest.java

@ -23,15 +23,20 @@ public class NoModelDataTest {
private static File file07;
private static File file03;
private static File fileCsv;
private static File fileRepeat07;
private static File fileRepeat03;
private static File fileRepeatCsv;
@BeforeClass
public static void init() {
file07 = TestFileUtil.createNewFile("noModel07.xlsx");
file03 = TestFileUtil.createNewFile("noModel03.xls");
fileCsv = TestFileUtil.createNewFile("noModelCsv.csv");
fileRepeat07 = TestFileUtil.createNewFile("noModelRepeat07.xlsx");
fileRepeat03 = TestFileUtil.createNewFile("noModelRepeat03.xls");
fileRepeatCsv = TestFileUtil.createNewFile("noModelRepeatCsv.csv");
}
@Test
@ -44,6 +49,11 @@ public class NoModelDataTest {
readAndWrite(file03, fileRepeat03);
}
@Test
public void t03ReadAndWriteCsv() throws Exception {
readAndWrite(fileCsv, fileRepeatCsv);
}
private void readAndWrite(File file, File fileRepeat) throws Exception {
EasyExcel.write(file).sheet().doWrite(data());
List<Map<Integer, String>> result = EasyExcel.read(file).headRowNumber(0).sheet().doReadSync();

8
src/test/java/com/alibaba/easyexcel/test/core/noncamel/UnCamelDataTest.java

@ -20,11 +20,14 @@ public class UnCamelDataTest {
private static File file07;
private static File file03;
private static File fileCsv;
@BeforeClass
public static void init() {
file07 = TestFileUtil.createNewFile("unCame07.xlsx");
file03 = TestFileUtil.createNewFile("unCame03.xls");
fileCsv = TestFileUtil.createNewFile("unCameCsv.csv");
}
@Test
@ -37,6 +40,11 @@ public class UnCamelDataTest {
readAndWrite(file03);
}
@Test
public void t03ReadAndWriteCsv() {
readAndWrite(fileCsv);
}
private void readAndWrite(File file) {
EasyExcel.write(file, UnCamelData.class).sheet().doWrite(data());
EasyExcel.read(file, UnCamelData.class, new UnCamelDataListener()).sheet().doRead();

70
src/test/java/com/alibaba/easyexcel/test/core/parameter/ParameterDataTest.java

@ -5,11 +5,6 @@ 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.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
@ -17,59 +12,79 @@ import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.cache.MapCache;
import com.alibaba.excel.converters.string.StringStringConverter;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.WriteTable;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
/**
*
* @author Jiaju Zhuang
*/
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class ParameterDataTest {
private static File file;
private static File file07;
private static File fileCsv;
@BeforeClass
public static void init() {
file = TestFileUtil.createNewFile("parameter07.xlsx");
file07 = TestFileUtil.createNewFile("parameter07.xlsx");
fileCsv = TestFileUtil.createNewFile("parameterCsv.csv");
}
@Test
public void t01ReadAndWrite() throws Exception {
readAndWrite1();
readAndWrite2();
readAndWrite3();
readAndWrite4();
readAndWrite5();
readAndWrite6();
readAndWrite7();
readAndWrite1(file07, ExcelTypeEnum.XLSX);
readAndWrite2(file07, ExcelTypeEnum.XLSX);
readAndWrite3(file07, ExcelTypeEnum.XLSX);
readAndWrite4(file07, ExcelTypeEnum.XLSX);
readAndWrite5(file07, ExcelTypeEnum.XLSX);
readAndWrite6(file07, ExcelTypeEnum.XLSX);
readAndWrite7(file07, ExcelTypeEnum.XLSX);
}
private void readAndWrite1() {
@Test
public void t02ReadAndWrite() throws Exception {
readAndWrite1(fileCsv, ExcelTypeEnum.CSV);
readAndWrite2(fileCsv, ExcelTypeEnum.CSV);
readAndWrite3(fileCsv, ExcelTypeEnum.CSV);
readAndWrite4(fileCsv, ExcelTypeEnum.CSV);
readAndWrite5(fileCsv, ExcelTypeEnum.CSV);
readAndWrite6(fileCsv, ExcelTypeEnum.CSV);
readAndWrite7(fileCsv, ExcelTypeEnum.CSV);
}
private void readAndWrite1(File file, ExcelTypeEnum type) {
EasyExcel.write(file.getPath()).head(ParameterData.class).sheet().doWrite(data());
EasyExcel.read(file.getPath()).head(ParameterData.class).registerReadListener(new ParameterDataListener())
.sheet().doRead();
}
private void readAndWrite2() {
private void readAndWrite2(File file, ExcelTypeEnum type) {
EasyExcel.write(file.getPath(), ParameterData.class).sheet().doWrite(data());
EasyExcel.read(file.getPath(), ParameterData.class, new ParameterDataListener()).sheet().doRead();
}
private void readAndWrite3() throws Exception {
EasyExcel.write(new FileOutputStream(file)).head(ParameterData.class).sheet().doWrite(data());
private void readAndWrite3(File file, ExcelTypeEnum type) throws Exception {
EasyExcel.write(new FileOutputStream(file)).excelType(type).head(ParameterData.class).sheet()
.doWrite(data());
EasyExcel.read(file.getPath()).head(ParameterData.class).registerReadListener(new ParameterDataListener())
.sheet().doRead();
}
private void readAndWrite4() throws Exception {
EasyExcel.write(new FileOutputStream(file), ParameterData.class).sheet().doWrite(data());
private void readAndWrite4(File file, ExcelTypeEnum type) throws Exception {
EasyExcel.write(new FileOutputStream(file), ParameterData.class).excelType(type).sheet().doWrite(data());
EasyExcel.read(file.getPath(), new ParameterDataListener()).head(ParameterData.class).sheet().doRead();
}
private void readAndWrite5() throws Exception {
private void readAndWrite5(File file, ExcelTypeEnum type) throws Exception {
ExcelWriter excelWriter =
EasyExcel.write(new FileOutputStream(file)).head(ParameterData.class).relativeHeadRowIndex(0).build();
EasyExcel.write(new FileOutputStream(file)).excelType(type).head(ParameterData.class).relativeHeadRowIndex(
0).build();
WriteSheet writeSheet = EasyExcel.writerSheet(0).relativeHeadRowIndex(0).needHead(Boolean.FALSE).build();
WriteTable writeTable = EasyExcel.writerTable(0).relativeHeadRowIndex(0).needHead(Boolean.TRUE).build();
excelWriter.write(data(), writeSheet, writeTable);
@ -88,9 +103,10 @@ public class ParameterDataTest {
excelReader.finish();
}
private void readAndWrite6() throws Exception {
private void readAndWrite6(File file, ExcelTypeEnum type) throws Exception {
ExcelWriter excelWriter =
EasyExcel.write(new FileOutputStream(file)).head(ParameterData.class).relativeHeadRowIndex(0).build();
EasyExcel.write(new FileOutputStream(file)).excelType(type).head(ParameterData.class).relativeHeadRowIndex(
0).build();
WriteSheet writeSheet = EasyExcel.writerSheet(0).relativeHeadRowIndex(0).needHead(Boolean.FALSE).build();
WriteTable writeTable = EasyExcel.writerTable(0).registerConverter(new StringStringConverter())
.relativeHeadRowIndex(0).needHead(Boolean.TRUE).build();
@ -110,7 +126,7 @@ public class ParameterDataTest {
excelReader.finish();
}
private void readAndWrite7() {
private void readAndWrite7(File file, ExcelTypeEnum type) {
EasyExcel.write(file, ParameterData.class).registerConverter(new StringStringConverter()).sheet()
.registerConverter(new StringStringConverter()).needHead(Boolean.FALSE).table(0).needHead(Boolean.TRUE)
.doWrite(data());
@ -119,7 +135,7 @@ public class ParameterDataTest {
}
private List<ParameterData> data() {
List<ParameterData> list = new ArrayList<ParameterData>();
List<ParameterData> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
ParameterData simpleData = new ParameterData();
simpleData.setName("姓名" + i);

29
src/test/java/com/alibaba/easyexcel/test/core/repetition/RepetitionDataTest.java

@ -4,11 +4,6 @@ import java.io.File;
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.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
@ -17,8 +12,12 @@ import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.WriteTable;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
/**
*
* @author Jiaju Zhuang
*/
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@ -26,15 +25,19 @@ public class RepetitionDataTest {
private static File file07;
private static File file03;
private static File fileCsv;
private static File fileTable07;
private static File fileTable03;
private static File fileTableCsv;
@BeforeClass
public static void init() {
file07 = TestFileUtil.createNewFile("repetition07.xlsx");
file03 = TestFileUtil.createNewFile("repetition03.xls");
fileCsv = TestFileUtil.createNewFile("repetitionCsv.csv");
fileTable07 = TestFileUtil.createNewFile("repetitionTable07.xlsx");
fileTable03 = TestFileUtil.createNewFile("repetitionTable03.xls");
fileTableCsv = TestFileUtil.createNewFile("repetitionTableCsv.csv");
}
@Test
@ -47,6 +50,11 @@ public class RepetitionDataTest {
readAndWrite(file03);
}
@Test
public void t03ReadAndWriteCsv() {
readAndWrite(fileCsv);
}
private void readAndWrite(File file) {
ExcelWriter excelWriter = EasyExcel.write(file, RepetitionData.class).build();
WriteSheet writeSheet = EasyExcel.writerSheet(0).build();
@ -57,15 +65,20 @@ public class RepetitionDataTest {
}
@Test
public void t03ReadAndWriteTable07() {
public void t11ReadAndWriteTable07() {
readAndWriteTable(fileTable07);
}
@Test
public void t04ReadAndWriteTable03() {
public void t12ReadAndWriteTable03() {
readAndWriteTable(fileTable03);
}
@Test
public void t13ReadAndWriteTableCsv() {
readAndWriteTable(fileTableCsv);
}
private void readAndWriteTable(File file) {
ExcelWriter excelWriter = EasyExcel.write(file, RepetitionData.class).build();
WriteSheet writeSheet = EasyExcel.writerSheet(0).build();

23
src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataTest.java

@ -4,17 +4,16 @@ import java.io.File;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
/**
*
* @author Jiaju Zhuang
*/
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@ -22,11 +21,13 @@ public class SimpleDataTest {
private static File file07;
private static File file03;
private static File fileCsv;
@BeforeClass
public static void init() {
file07 = TestFileUtil.createNewFile("simple07.xlsx");
file03 = TestFileUtil.createNewFile("simple03.xls");
fileCsv = TestFileUtil.createNewFile("simpleCsv.csv");
}
@Test
@ -39,21 +40,31 @@ public class SimpleDataTest {
readAndWrite(file03);
}
@Test
public void t03ReadAndWriteCsv() {
readAndWrite(fileCsv);
}
private void readAndWrite(File file) {
EasyExcel.write(file, SimpleData.class).sheet().doWrite(data());
EasyExcel.read(file, SimpleData.class, new SimpleDataListener()).sheet().doRead();
}
@Test
public void t03SynchronousRead07() {
public void t11SynchronousRead07() {
synchronousRead(file07);
}
@Test
public void t04SynchronousRead03() {
public void t12SynchronousRead03() {
synchronousRead(file03);
}
@Test
public void t13SynchronousReadCsv() {
synchronousRead(fileCsv);
}
@Test
public void t05SheetNameRead07() {
EasyExcel.read(TestFileUtil.readFile("simple" + File.separator + "simple07.xlsx"), SimpleData.class,

23
src/test/java/com/alibaba/easyexcel/test/core/skip/SkipDataTest.java

@ -4,23 +4,22 @@ import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.junit.Assert;
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.core.simple.SimpleDataListener;
import com.alibaba.easyexcel.test.core.simple.SimpleDataSheetNameListener;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.event.SyncReadListener;
import com.alibaba.excel.exception.ExcelGenerateException;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.write.metadata.WriteSheet;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
/**
* @author Jiaju Zhuang
*/
@ -29,11 +28,13 @@ public class SkipDataTest {
private static File file07;
private static File file03;
private static File fileCsv;
@BeforeClass
public static void init() {
file07 = TestFileUtil.createNewFile("skip.xlsx");
file03 = TestFileUtil.createNewFile("skip.xls");
fileCsv = TestFileUtil.createNewFile("skip.csv");
}
@Test
@ -46,6 +47,11 @@ public class SkipDataTest {
readAndWrite(file03);
}
@Test
public void t03ReadAndWriteCsv() {
Assert.assertThrows(ExcelGenerateException.class, () -> readAndWrite(fileCsv));
}
private void readAndWrite(File file) {
ExcelWriter excelWriter = EasyExcel.write(file, SimpleData.class).build();
WriteSheet writeSheet0 = EasyExcel.writerSheet(0, "第一个").build();
@ -75,7 +81,6 @@ public class SkipDataTest {
excelReader.finish();
}
private List<SkipData> data(String name) {
List<SkipData> list = new ArrayList<SkipData>();
SkipData data = new SkipData();

24
src/test/java/com/alibaba/easyexcel/test/core/sort/SortDataTest.java

@ -6,15 +6,15 @@ import java.util.Collections;
import java.util.List;
import java.util.Map;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
/**
* @author Jiaju Zhuang
*/
@ -23,15 +23,19 @@ public class SortDataTest {
private static File file07;
private static File file03;
private static File fileCsv;
private static File sortNoHead07;
private static File sortNoHead03;
private static File sortNoHeadCsv;
@BeforeClass
public static void init() {
file07 = TestFileUtil.createNewFile("sort.xlsx");
file03 = TestFileUtil.createNewFile("sort.xls");
fileCsv = TestFileUtil.createNewFile("sort.csv");
sortNoHead07 = TestFileUtil.createNewFile("sortNoHead.xlsx");
sortNoHead03 = TestFileUtil.createNewFile("sortNoHead.xls");
sortNoHeadCsv = TestFileUtil.createNewFile("sortNoHead.csv");
}
@Test
@ -45,15 +49,24 @@ public class SortDataTest {
}
@Test
public void t03ReadAndWriteNoHead07() {
public void t03ReadAndWriteCsv() {
readAndWrite(fileCsv);
}
@Test
public void t11ReadAndWriteNoHead07() {
readAndWriteNoHead(sortNoHead07);
}
@Test
public void t04ReadAndWriteNoHead03() {
public void t12ReadAndWriteNoHead03() {
readAndWriteNoHead(sortNoHead03);
}
@Test
public void t13ReadAndWriteNoHeadCsv() {
readAndWriteNoHead(sortNoHeadCsv);
}
private void readAndWrite(File file) {
EasyExcel.write(file, SortData.class).sheet().doWrite(data());
@ -95,7 +108,6 @@ public class SortDataTest {
return head;
}
private List<SortData> data() {
List<SortData> list = new ArrayList<SortData>();
SortData sortData = new SortData();

2
src/test/java/com/alibaba/easyexcel/test/core/style/StyleDataTest.java

@ -40,7 +40,7 @@ public class StyleDataTest {
@BeforeClass
public static void init() {
file07 = TestFileUtil.createNewFile("style07.xlsx");
file03 = TestFileUtil.createNewFile("style03.xls");
file03 = TestFileUtil.createNewFile("style03.csv");
}
@Test

7
src/test/java/com/alibaba/easyexcel/test/core/template/TemplateDataTest.java

@ -4,14 +4,14 @@ import java.io.File;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
/**
*
* @author Jiaju Zhuang
@ -22,6 +22,7 @@ public class TemplateDataTest {
private static File file07;
private static File file03;
@BeforeClass
public static void init() {
file07 = TestFileUtil.createNewFile("template07.xlsx");

5
src/test/java/com/alibaba/easyexcel/test/temp/csv/CsvReadTest.java

@ -2,7 +2,9 @@ package com.alibaba.easyexcel.test.temp.csv;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
@ -25,7 +27,8 @@ public class CsvReadTest {
@Test
public void write() throws Exception {
Appendable out = new PrintWriter(TestFileUtil.createNewFile("csvWrite1.csv"));
Appendable out = new PrintWriter(
new OutputStreamWriter(new FileOutputStream(TestFileUtil.createNewFile("csvWrite1.csv"))));
CSVPrinter printer = CSVFormat.DEFAULT.withHeader("userId", "userName")
.print(out);
for (int i = 0; i < 10; i++) {

Loading…
Cancel
Save