Merge pull request #12456 from hkhan/JAVA-12757-update-opencsv-code
[JAVA-12757] Update code for OpenCSV article
This commit is contained in:
commit
0eb9cfbad8
|
@ -1,5 +1,6 @@
|
||||||
package com.baeldung.libraries.opencsv;
|
package com.baeldung.libraries.opencsv;
|
||||||
|
|
||||||
|
import com.baeldung.libraries.opencsv.beans.CsvBean;
|
||||||
import com.baeldung.libraries.opencsv.beans.NamedColumnBean;
|
import com.baeldung.libraries.opencsv.beans.NamedColumnBean;
|
||||||
import com.baeldung.libraries.opencsv.beans.SimplePositionBean;
|
import com.baeldung.libraries.opencsv.beans.SimplePositionBean;
|
||||||
import com.baeldung.libraries.opencsv.examples.sync.BeanExamples;
|
import com.baeldung.libraries.opencsv.examples.sync.BeanExamples;
|
||||||
|
@ -7,102 +8,60 @@ import com.baeldung.libraries.opencsv.examples.sync.CsvReaderExamples;
|
||||||
import com.baeldung.libraries.opencsv.examples.sync.CsvWriterExamples;
|
import com.baeldung.libraries.opencsv.examples.sync.CsvWriterExamples;
|
||||||
import com.baeldung.libraries.opencsv.helpers.Helpers;
|
import com.baeldung.libraries.opencsv.helpers.Helpers;
|
||||||
|
|
||||||
import java.io.Reader;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class Application {
|
public class Application {
|
||||||
|
|
||||||
/*
|
// CSV Reader Examples
|
||||||
* Bean Examples.
|
public static List<String[]> readLineByLineSyncExample() throws Exception {
|
||||||
*/
|
Path path = Helpers.twoColumnCsvPath();
|
||||||
|
return CsvReaderExamples.readLineByLine(path);
|
||||||
public static String simpleSyncPositionBeanExample() {
|
|
||||||
Path path = null;
|
|
||||||
try {
|
|
||||||
path = Helpers.twoColumnCsvPath();
|
|
||||||
} catch (Exception ex) {
|
|
||||||
Helpers.err(ex);
|
|
||||||
}
|
|
||||||
return BeanExamples.beanBuilderExample(path, SimplePositionBean.class).toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String namedSyncColumnBeanExample() {
|
public static List<String[]> readAllLinesSyncExample() throws Exception {
|
||||||
Path path = null;
|
Path path = Helpers.twoColumnCsvPath();
|
||||||
try {
|
return CsvReaderExamples.readAllLines(path);
|
||||||
path = Helpers.namedColumnCsvPath();
|
|
||||||
} catch (Exception ex) {
|
|
||||||
Helpers.err(ex);
|
|
||||||
}
|
|
||||||
return BeanExamples.beanBuilderExample(path, NamedColumnBean.class).toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String writeSyncCsvFromBeanExample() {
|
// CSV Writer Examples
|
||||||
Path path = null;
|
public static String writeLineByLineSyncExample() throws Exception {
|
||||||
try {
|
Path path = Helpers.fileOutOnePath();
|
||||||
path = Helpers.fileOutBeanPath();
|
return CsvWriterExamples.writeLineByLine(Helpers.fourColumnCsvString(), path);
|
||||||
} catch (Exception ex) {
|
|
||||||
Helpers.err(ex);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String writeAllLinesSyncExample() throws Exception {
|
||||||
|
Path path = Helpers.fileOutAllPath();
|
||||||
|
return CsvWriterExamples.writeAllLines(Helpers.fourColumnCsvString(), path);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bean Examples
|
||||||
|
public static List<CsvBean> simpleSyncPositionBeanExample() throws Exception {
|
||||||
|
Path path = Helpers.twoColumnCsvPath();
|
||||||
|
return BeanExamples.beanBuilderExample(path, SimplePositionBean.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<CsvBean> namedSyncColumnBeanExample() throws Exception {
|
||||||
|
Path path = Helpers.namedColumnCsvPath();
|
||||||
|
return BeanExamples.beanBuilderExample(path, NamedColumnBean.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String writeSyncCsvFromBeanExample() throws Exception {
|
||||||
|
Path path = Helpers.fileOutBeanPath();
|
||||||
return BeanExamples.writeCsvFromBean(path);
|
return BeanExamples.writeCsvFromBean(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* CSV Reader Examples.
|
|
||||||
*/
|
|
||||||
|
|
||||||
public static String oneByOneSyncExample() {
|
|
||||||
Reader reader = null;
|
|
||||||
try {
|
|
||||||
reader = Files.newBufferedReader(Helpers.twoColumnCsvPath());
|
|
||||||
} catch (Exception ex) {
|
|
||||||
Helpers.err(ex);
|
|
||||||
}
|
|
||||||
return CsvReaderExamples.oneByOne(reader).toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String readAllSyncExample() {
|
|
||||||
Reader reader = null;
|
|
||||||
try {
|
|
||||||
reader = Files.newBufferedReader(Helpers.twoColumnCsvPath());
|
|
||||||
} catch (Exception ex) {
|
|
||||||
Helpers.err(ex);
|
|
||||||
}
|
|
||||||
return CsvReaderExamples.readAll(reader).toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* CSV Writer Examples.
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
public static String csvWriterSyncOneByOne() {
|
|
||||||
Path path = null;
|
|
||||||
try {
|
|
||||||
path = Helpers.fileOutOnePath();
|
|
||||||
} catch (Exception ex) {
|
|
||||||
Helpers.err(ex);
|
|
||||||
}
|
|
||||||
return CsvWriterExamples.csvWriterOneByOne(Helpers.fourColumnCsvString(), path);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String csvWriterSyncAll() {
|
|
||||||
Path path = null;
|
|
||||||
try {
|
|
||||||
path = Helpers.fileOutAllPath();
|
|
||||||
} catch (Exception ex) {
|
|
||||||
Helpers.err(ex);
|
|
||||||
}
|
|
||||||
return CsvWriterExamples.csvWriterAll(Helpers.fourColumnCsvString(), path);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
try {
|
||||||
simpleSyncPositionBeanExample();
|
simpleSyncPositionBeanExample();
|
||||||
namedSyncColumnBeanExample();
|
namedSyncColumnBeanExample();
|
||||||
writeSyncCsvFromBeanExample();
|
writeSyncCsvFromBeanExample();
|
||||||
oneByOneSyncExample();
|
readLineByLineSyncExample();
|
||||||
readAllSyncExample();
|
readAllLinesSyncExample();
|
||||||
csvWriterSyncOneByOne();
|
writeLineByLineSyncExample();
|
||||||
csvWriterSyncAll();
|
writeAllLinesSyncExample();
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException("Error during csv processing", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,5 +27,10 @@ public class NamedColumnBean extends CsvBean {
|
||||||
this.age = age;
|
this.age = age;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return name + ',' + age;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,4 +26,8 @@ public class SimplePositionBean extends CsvBean {
|
||||||
this.exampleColTwo = exampleColTwo;
|
this.exampleColTwo = exampleColTwo;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return exampleColOne + ',' + exampleColTwo;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,4 +37,9 @@ public class WriteExampleBean extends CsvBean {
|
||||||
public void setColC(String colC) {
|
public void setColC(String colC) {
|
||||||
this.colC = colC;
|
this.colC = colC;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return colA + ',' + colB + "," + colC;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,60 +4,49 @@ import com.baeldung.libraries.opencsv.beans.CsvBean;
|
||||||
import com.baeldung.libraries.opencsv.beans.WriteExampleBean;
|
import com.baeldung.libraries.opencsv.beans.WriteExampleBean;
|
||||||
import com.baeldung.libraries.opencsv.helpers.Helpers;
|
import com.baeldung.libraries.opencsv.helpers.Helpers;
|
||||||
import com.baeldung.libraries.opencsv.pojos.CsvTransfer;
|
import com.baeldung.libraries.opencsv.pojos.CsvTransfer;
|
||||||
import com.opencsv.CSVWriter;
|
import com.opencsv.bean.CsvToBean;
|
||||||
import com.opencsv.bean.*;
|
import com.opencsv.bean.CsvToBeanBuilder;
|
||||||
|
import com.opencsv.bean.StatefulBeanToCsv;
|
||||||
|
import com.opencsv.bean.StatefulBeanToCsvBuilder;
|
||||||
|
|
||||||
import java.io.FileWriter;
|
import java.io.FileWriter;
|
||||||
import java.io.Reader;
|
import java.io.Reader;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.ArrayList;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class BeanExamples {
|
public class BeanExamples {
|
||||||
|
|
||||||
public static List<CsvBean> beanBuilderExample(Path path, Class clazz) {
|
public static List<CsvBean> beanBuilderExample(Path path, Class<? extends CsvBean> clazz) throws Exception {
|
||||||
ColumnPositionMappingStrategy ms = new ColumnPositionMappingStrategy();
|
|
||||||
return beanBuilderExample(path, clazz, ms);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static List<CsvBean> beanBuilderExample(Path path, Class clazz, MappingStrategy ms) {
|
|
||||||
CsvTransfer csvTransfer = new CsvTransfer();
|
CsvTransfer csvTransfer = new CsvTransfer();
|
||||||
try {
|
try (Reader reader = Files.newBufferedReader(path)) {
|
||||||
ms.setType(clazz);
|
CsvToBean<CsvBean> cb = new CsvToBeanBuilder<CsvBean>(reader)
|
||||||
|
.withType(clazz)
|
||||||
Reader reader = Files.newBufferedReader(path);
|
|
||||||
CsvToBean cb = new CsvToBeanBuilder(reader).withType(clazz)
|
|
||||||
.withMappingStrategy(ms)
|
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
csvTransfer.setCsvList(cb.parse());
|
csvTransfer.setCsvList(cb.parse());
|
||||||
reader.close();
|
|
||||||
|
|
||||||
} catch (Exception ex) {
|
|
||||||
Helpers.err(ex);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return csvTransfer.getCsvList();
|
return csvTransfer.getCsvList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String writeCsvFromBean(Path path) {
|
public static String writeCsvFromBean(Path path) throws Exception {
|
||||||
try {
|
|
||||||
Writer writer = new FileWriter(path.toString());
|
|
||||||
|
|
||||||
StatefulBeanToCsv sbc = new StatefulBeanToCsvBuilder(writer).withSeparator(CSVWriter.DEFAULT_SEPARATOR)
|
List<CsvBean> sampleData = Arrays.asList(
|
||||||
|
new WriteExampleBean("Test1", "sample", "data"),
|
||||||
|
new WriteExampleBean("Test2", "ipso", "facto")
|
||||||
|
);
|
||||||
|
|
||||||
|
try (Writer writer = new FileWriter(path.toString())) {
|
||||||
|
StatefulBeanToCsv<CsvBean> sbc = new StatefulBeanToCsvBuilder<CsvBean>(writer)
|
||||||
|
.withQuotechar('\'')
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
List<CsvBean> list = new ArrayList<>();
|
sbc.write(sampleData);
|
||||||
list.add(new WriteExampleBean("Test1", "sfdsf", "fdfd"));
|
|
||||||
list.add(new WriteExampleBean("Test2", "ipso", "facto"));
|
|
||||||
|
|
||||||
sbc.write(list);
|
|
||||||
writer.close();
|
|
||||||
|
|
||||||
} catch (Exception ex) {
|
|
||||||
Helpers.err(ex);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return Helpers.readFile(path);
|
return Helpers.readFile(path);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,61 +1,58 @@
|
||||||
package com.baeldung.libraries.opencsv.examples.sync;
|
package com.baeldung.libraries.opencsv.examples.sync;
|
||||||
|
|
||||||
import com.baeldung.libraries.opencsv.helpers.Helpers;
|
|
||||||
import com.opencsv.CSVParser;
|
import com.opencsv.CSVParser;
|
||||||
import com.opencsv.CSVParserBuilder;
|
import com.opencsv.CSVParserBuilder;
|
||||||
import com.opencsv.CSVReader;
|
import com.opencsv.CSVReader;
|
||||||
import com.opencsv.CSVReaderBuilder;
|
import com.opencsv.CSVReaderBuilder;
|
||||||
|
|
||||||
import java.io.Reader;
|
import java.io.Reader;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class CsvReaderExamples {
|
public class CsvReaderExamples {
|
||||||
|
|
||||||
public static List<String[]> readAll(Reader reader) {
|
public static List<String[]> readAllLines(Path filePath) throws Exception {
|
||||||
|
|
||||||
CSVParser parser = new CSVParserBuilder()
|
CSVParser parser = new CSVParserBuilder()
|
||||||
.withSeparator(',')
|
.withSeparator(',')
|
||||||
.withIgnoreQuotations(true)
|
.withIgnoreQuotations(true)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
CSVReader csvReader = new CSVReaderBuilder(reader)
|
try (Reader reader = Files.newBufferedReader(filePath)) {
|
||||||
|
CSVReaderBuilder csvReaderBuilder = new CSVReaderBuilder(reader)
|
||||||
.withSkipLines(0)
|
.withSkipLines(0)
|
||||||
.withCSVParser(parser)
|
.withCSVParser(parser);
|
||||||
.build();
|
|
||||||
|
|
||||||
List<String[]> list = new ArrayList<>();
|
try (CSVReader csvReader = csvReaderBuilder.build()) {
|
||||||
try {
|
return csvReader.readAll();
|
||||||
list = csvReader.readAll();
|
|
||||||
reader.close();
|
|
||||||
csvReader.close();
|
|
||||||
} catch (Exception ex) {
|
|
||||||
Helpers.err(ex);
|
|
||||||
}
|
}
|
||||||
return list;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<String[]> oneByOne(Reader reader) {
|
}
|
||||||
|
|
||||||
|
public static List<String[]> readLineByLine(Path filePath) throws Exception {
|
||||||
List<String[]> list = new ArrayList<>();
|
List<String[]> list = new ArrayList<>();
|
||||||
try {
|
|
||||||
CSVParser parser = new CSVParserBuilder()
|
CSVParser parser = new CSVParserBuilder()
|
||||||
.withSeparator(',')
|
.withSeparator(',')
|
||||||
.withIgnoreQuotations(true)
|
.withIgnoreQuotations(true)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
CSVReader csvReader = new CSVReaderBuilder(reader)
|
try (Reader reader = Files.newBufferedReader(filePath)) {
|
||||||
.withSkipLines(0)
|
|
||||||
.withCSVParser(parser)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
|
CSVReaderBuilder csvReaderBuilder = new CSVReaderBuilder(reader)
|
||||||
|
.withSkipLines(0)
|
||||||
|
.withCSVParser(parser);
|
||||||
|
|
||||||
|
try (CSVReader csvReader = csvReaderBuilder.build()) {
|
||||||
String[] line;
|
String[] line;
|
||||||
while ((line = csvReader.readNext()) != null) {
|
while ((line = csvReader.readNext()) != null) {
|
||||||
list.add(line);
|
list.add(line);
|
||||||
}
|
}
|
||||||
reader.close();
|
}
|
||||||
csvReader.close();
|
|
||||||
} catch (Exception ex) {
|
|
||||||
Helpers.err(ex);
|
|
||||||
}
|
}
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,26 +9,21 @@ import java.util.List;
|
||||||
|
|
||||||
public class CsvWriterExamples {
|
public class CsvWriterExamples {
|
||||||
|
|
||||||
public static String csvWriterOneByOne(List<String[]> stringArray, Path path) {
|
public static String writeLineByLine(List<String[]> lines, Path path) throws Exception {
|
||||||
try {
|
|
||||||
CSVWriter writer = new CSVWriter(new FileWriter(path.toString()));
|
try (CSVWriter writer = new CSVWriter(new FileWriter(path.toString()))) {
|
||||||
for (String[] array : stringArray) {
|
for (String[] line : lines) {
|
||||||
writer.writeNext(array);
|
writer.writeNext(line);
|
||||||
}
|
}
|
||||||
writer.close();
|
|
||||||
} catch (Exception ex) {
|
|
||||||
Helpers.err(ex);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return Helpers.readFile(path);
|
return Helpers.readFile(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String csvWriterAll(List<String[]> stringArray, Path path) {
|
public static String writeAllLines(List<String[]> lines, Path path) throws Exception {
|
||||||
try {
|
|
||||||
CSVWriter writer = new CSVWriter(new FileWriter(path.toString()));
|
try (CSVWriter writer = new CSVWriter(new FileWriter(path.toString()))) {
|
||||||
writer.writeAll(stringArray);
|
writer.writeAll(lines);
|
||||||
writer.close();
|
|
||||||
} catch (Exception ex) {
|
|
||||||
Helpers.err(ex);
|
|
||||||
}
|
}
|
||||||
return Helpers.readFile(path);
|
return Helpers.readFile(path);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
package com.baeldung.libraries.opencsv.helpers;
|
package com.baeldung.libraries.opencsv.helpers;
|
||||||
|
|
||||||
import com.baeldung.libraries.opencsv.Constants;
|
import com.baeldung.libraries.opencsv.Constants;
|
||||||
|
import org.apache.commons.io.IOUtils;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.IOException;
|
||||||
import java.io.FileReader;
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
@ -13,9 +13,7 @@ import java.util.List;
|
||||||
|
|
||||||
public class Helpers {
|
public class Helpers {
|
||||||
|
|
||||||
/**
|
// Write Files
|
||||||
* Write Files
|
|
||||||
*/
|
|
||||||
|
|
||||||
public static Path fileOutAllPath() throws URISyntaxException {
|
public static Path fileOutAllPath() throws URISyntaxException {
|
||||||
URI uri = ClassLoader.getSystemResource(Constants.CSV_All).toURI();
|
URI uri = ClassLoader.getSystemResource(Constants.CSV_All).toURI();
|
||||||
|
@ -32,9 +30,7 @@ public class Helpers {
|
||||||
return Paths.get(uri);
|
return Paths.get(uri);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// Read Files
|
||||||
* Read Files
|
|
||||||
*/
|
|
||||||
|
|
||||||
public static Path twoColumnCsvPath() throws URISyntaxException {
|
public static Path twoColumnCsvPath() throws URISyntaxException {
|
||||||
URI uri = ClassLoader.getSystemResource(Constants.TWO_COLUMN_CSV).toURI();
|
URI uri = ClassLoader.getSystemResource(Constants.TWO_COLUMN_CSV).toURI();
|
||||||
|
@ -51,33 +47,12 @@ public class Helpers {
|
||||||
return Paths.get(uri);
|
return Paths.get(uri);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public static String readFile(Path path) throws IOException {
|
||||||
* Simple File Reader
|
return IOUtils.toString(path.toUri());
|
||||||
*/
|
|
||||||
|
|
||||||
public static String readFile(Path path) {
|
|
||||||
String response = "";
|
|
||||||
try {
|
|
||||||
FileReader fr = new FileReader(path.toString());
|
|
||||||
BufferedReader br = new BufferedReader(fr);
|
|
||||||
String strLine;
|
|
||||||
StringBuffer sb = new StringBuffer();
|
|
||||||
while ((strLine = br.readLine()) != null) {
|
|
||||||
sb.append(strLine);
|
|
||||||
}
|
|
||||||
response = sb.toString();
|
|
||||||
System.out.println(response);
|
|
||||||
fr.close();
|
|
||||||
br.close();
|
|
||||||
} catch (Exception ex) {
|
|
||||||
Helpers.err(ex);
|
|
||||||
}
|
|
||||||
return response;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Dummy Data for Writing.
|
// Dummy Data for Writing
|
||||||
*/
|
|
||||||
|
|
||||||
public static List<String[]> twoColumnCsvString() {
|
public static List<String[]> twoColumnCsvString() {
|
||||||
List<String[]> list = new ArrayList<>();
|
List<String[]> list = new ArrayList<>();
|
||||||
|
@ -94,15 +69,4 @@ public class Helpers {
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Message Helpers
|
|
||||||
*/
|
|
||||||
|
|
||||||
public static void print(String msg) {
|
|
||||||
System.out.println(msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void err(Exception ex) {
|
|
||||||
System.out.println(Constants.GENERIC_EXCEPTION + " " + ex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
colA, ColB
|
colA,colB
|
||||||
A, B
|
A,B
|
||||||
C, D
|
C,D
|
||||||
G, G
|
G,G
|
||||||
G, F
|
G,F
|
|
|
@ -1,66 +1,107 @@
|
||||||
package com.baeldung.libraries.opencsv;
|
package com.baeldung.libraries.opencsv;
|
||||||
|
|
||||||
import com.baeldung.libraries.opencsv.helpers.Helpers;
|
import com.baeldung.libraries.opencsv.beans.CsvBean;
|
||||||
import org.junit.After;
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
public class OpenCsvIntegrationTest {
|
public class OpenCsvIntegrationTest {
|
||||||
|
|
||||||
private Object testReadCsv(Object result) {
|
private static final String NEW_LINE = System.lineSeparator();
|
||||||
assert (result != null);
|
|
||||||
assert (result instanceof String);
|
|
||||||
assert (!((String) result).isEmpty());
|
|
||||||
System.out.println(result);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Object testWriteCsv(Object result) {
|
@Test
|
||||||
assert (result instanceof String);
|
public void givenSampleData_whenReadUsingPosition_thenContentsRead() throws Exception {
|
||||||
assert (!((String) result).isEmpty());
|
List<CsvBean> values = Application.simpleSyncPositionBeanExample();
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Before
|
assertThat(values)
|
||||||
public void setup() {
|
.extracting(Object::toString)
|
||||||
|
.containsExactly(
|
||||||
|
"colA,colB",
|
||||||
|
"A,B",
|
||||||
|
"C,D",
|
||||||
|
"G,G",
|
||||||
|
"G,F"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void positionExampleTest() {
|
public void givenSampleData_whenReadUsingNamedColumn_thenContentsRead() throws Exception {
|
||||||
testReadCsv(Application.simpleSyncPositionBeanExample());
|
List<CsvBean> values = Application.namedSyncColumnBeanExample();
|
||||||
|
|
||||||
|
assertThat(values)
|
||||||
|
.extracting(Object::toString)
|
||||||
|
.containsExactly(
|
||||||
|
"Joe,27",
|
||||||
|
"Jane,32",
|
||||||
|
"Bob,53"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void namedColumnExampleTest() {
|
public void givenSampleData_whenReadLineByLine_thenContentsRead() throws Exception {
|
||||||
testReadCsv(Application.namedSyncColumnBeanExample());
|
List<String[]> lineByLineContents = Application.readLineByLineSyncExample();
|
||||||
|
|
||||||
|
assertThat(lineByLineContents)
|
||||||
|
.containsExactly(
|
||||||
|
new String[] {"colA", "colB"},
|
||||||
|
new String[] {"A", "B"},
|
||||||
|
new String[] {"C", "D"},
|
||||||
|
new String[] {"G", "G"},
|
||||||
|
new String[] {"G", "F"}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void writeCsvUsingBeanBuilderTest() {
|
public void givenSampleData_whenReadAllLines_thenContentsRead() throws Exception {
|
||||||
testWriteCsv(Application.writeSyncCsvFromBeanExample());
|
|
||||||
|
List<String[]> contents = Application.readAllLinesSyncExample();
|
||||||
|
|
||||||
|
assertThat(contents)
|
||||||
|
.containsExactly(
|
||||||
|
new String[] {"colA", "colB"},
|
||||||
|
new String[] {"A", "B"},
|
||||||
|
new String[] {"C", "D"},
|
||||||
|
new String[] {"G", "G"},
|
||||||
|
new String[] {"G", "F"}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void oneByOneExampleTest() {
|
public void givenSampleData_whenWriteCsvUsingBean_thenContentsWritten() throws Exception {
|
||||||
testReadCsv(Application.oneByOneSyncExample());
|
String contents = Application.writeSyncCsvFromBeanExample();
|
||||||
|
|
||||||
|
assertThat(contents.split(NEW_LINE))
|
||||||
|
.containsExactly(
|
||||||
|
"'colA','colB','colC'",
|
||||||
|
"'Test1','sample','data'",
|
||||||
|
"'Test2','ipso','facto'"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void readAllExampleTest() {
|
public void givenSampleData_whenWriteCsvLineByLine_thenContentsWritten() throws Exception {
|
||||||
testReadCsv(Application.readAllSyncExample());
|
String contents = Application.writeLineByLineSyncExample();
|
||||||
|
|
||||||
|
assertThat(contents.split(NEW_LINE))
|
||||||
|
.containsExactly(
|
||||||
|
"\"ColA\",\"ColB\",\"ColC\",\"ColD\"",
|
||||||
|
"\"A\",\"B\",\"A\",\"B\"",
|
||||||
|
"\"BB\",\"AB\",\"AA\",\"B\""
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void csvWriterOneByOneTest() {
|
public void givenSampleData_whenWriteCsvAllLines_thenContentsWritten() throws Exception {
|
||||||
testWriteCsv(Application.csvWriterSyncOneByOne());
|
String contents = Application.writeAllLinesSyncExample();
|
||||||
|
|
||||||
|
assertThat(contents.split(NEW_LINE))
|
||||||
|
.containsExactly(
|
||||||
|
"\"ColA\",\"ColB\",\"ColC\",\"ColD\"",
|
||||||
|
"\"A\",\"B\",\"A\",\"B\"",
|
||||||
|
"\"BB\",\"AB\",\"AA\",\"B\""
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void csvWriterAllTest() {
|
|
||||||
testWriteCsv(Application.csvWriterSyncAll());
|
|
||||||
}
|
|
||||||
|
|
||||||
@After
|
|
||||||
public void close() {
|
|
||||||
}
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue