branch feature/3798_new_export_API created (now 8324b6e)
This is an automated email from the git hooks/post-receive script. New change to branch feature/3798_new_export_API in repository nuiton-csv. See http://git.nuiton.org/nuiton-csv.git at 8324b6e Introduce new Exporter API (Refs #3798) This branch includes the following new commits: new 8324b6e Introduce new Exporter API (Refs #3798) The 1 revisions listed above as "new" are entirely new to this repository and will be described in separate emails. The revisions listed as "adds" were already present in the repository and have only been added to this reference. Detailed log of new commits: commit 8324b6ebda7ff5ae7743e3f29d2f02ed05d222f2 Author: Tony CHEMIT <chemit@codelutin.com> Date: Thu Dec 10 09:17:27 2015 +0100 Introduce new Exporter API (Refs #3798) -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
This is an automated email from the git hooks/post-receive script. New commit to branch feature/3798_new_export_API in repository nuiton-csv. See http://git.nuiton.org/nuiton-csv.git commit 8324b6ebda7ff5ae7743e3f29d2f02ed05d222f2 Author: Tony CHEMIT <chemit@codelutin.com> Date: Thu Dec 10 09:17:27 2015 +0100 Introduce new Exporter API (Refs #3798) --- src/main/java/org/nuiton/csv/Exporter.java | 569 +++++++++++++++++++++ src/main/java/org/nuiton/csv/ExporterBuilder.java | 97 ++++ .../java/org/nuiton/csv/ExporterConfiguration.java | 68 +++ src/main/java/org/nuiton/csv/package-info.java | 24 + src/test/java/org/nuiton/csv/ExporterTest.java | 233 +++++++++ 5 files changed, 991 insertions(+) diff --git a/src/main/java/org/nuiton/csv/Exporter.java b/src/main/java/org/nuiton/csv/Exporter.java new file mode 100644 index 0000000..17ba5b2 --- /dev/null +++ b/src/main/java/org/nuiton/csv/Exporter.java @@ -0,0 +1,569 @@ +package org.nuiton.csv; + +import com.google.common.base.Preconditions; +import org.nuiton.util.StringUtil; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.Reader; +import java.io.Writer; +import java.util.Iterator; + +/** + * Created on 09/12/15. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 3.0 + */ +public class Exporter<O> { + + /** + * Immutable exporter configuration. + */ + protected final ExporterConfiguration<O> configuration; + + /** + * Creates an exporter from the given model, and using default configuration values. + * + * @param model model of export + * @param <O> type of data to export + * @return the exporter ready to use + */ + public static <O> Exporter<O> of(ExportModel<O> model) { + return builder(model).build(); + } + + /** + * Creates an exporter from the given configuration. + * + * @param configuration export configuration + * @param <O> type of data to export + * @return the exporter ready to use + */ + public static <O> Exporter<O> of(ExporterConfiguration<O> configuration) { + return builder(configuration).build(); + } + + /** + * Creates a new exporter builder from the given model. + * + * @param model model of export + * @param <O> type of data to export + * @return the exporter builder + */ + public static <O> ExporterBuilder<O> builder(ExportModel<O> model) { + return new ExporterBuilder<>(model); + } + + /** + * Creates a new exporter builder from the given configuration. + * + * @param configuration configuration of export + * @param <O> type of data to export + * @return the exporter builder + */ + public static <O> ExporterBuilder<O> builder(ExporterConfiguration<O> configuration) { + return new ExporterBuilder<>(configuration); + } + + /** + * Creates a new exporter builder from this exporter (to reuse his configuration). + * + * @return the exporter builder + */ + public ExporterBuilder<O> builder() { + return new ExporterBuilder<>(configuration); + } + + /** + * Produces the cvs of the given {@code data} into the given {@code outputStream}. + * + * @param data data to export + * @param outputStream where to perform the export + * @throws IOException + */ + public void exportToOutputStream(Iterable<O> data, OutputStream outputStream) throws IOException { + exportToOutputStream(data, true, outputStream); + } + + /** + * Produces the cvs (without header) of the given {@code data} into the given {@code outputStream}. + * + * @param data data to export + * @param outputStream where to perform the export + * @throws IOException + */ + public void exportToOutputStreamWithoutHeader(Iterable<O> data, OutputStream outputStream) throws IOException { + exportToOutputStream(data, false, outputStream); + } + + /** + * Produces the cvs of the given {@code data} into the given {@code outputStream}. + * + * @param data data to export + * @param writeHeader write or not header (first line of the export) + * @param outputStream where to perform the export + * @throws IOException + */ + public void exportToOutputStream(Iterable<O> data, boolean writeHeader, OutputStream outputStream) throws IOException { + ExporterAction<O> action = ExporterAction.of(configuration, data, writeHeader); + action.export(outputStream); + } + + /** + * Produces the cvs of the given {@code data} into the given {@code writer}. + * + * @param data data to export + * @param writer where to perform the export + * @throws IOException + */ + public void exportToWriter(Iterable<O> data, Writer writer) throws IOException { + exportToWriter(data, true, writer); + } + + /** + * Produces the cvs (without header) of the given {@code data} into the given {@code writer}. + * + * @param data data to export + * @param writer where to perform the export + * @throws IOException + */ + public void exportToWriterWithoutHeader(Iterable<O> data, Writer writer) throws IOException { + exportToWriter(data, false, writer); + } + + /** + * Produces the cvs of the given {@code data} into the given {@code writer}. + * + * @param data data to export + * @param writeHeader write or not header (first line of the export) + * @param writer where to perform the export + * @throws IOException + */ + public void exportToWriter(Iterable<O> data, boolean writeHeader, Writer writer) throws IOException { + ExporterAction<O> action = ExporterAction.of(configuration, data, writeHeader); + action.export(writer); + } + + /** + * Produces the cvs of the given {@code data} into the given {@code file}. + * + * @param data data to export + * @param file where to perform the export + * @throws IOException + */ + public void exportToFile(Iterable<O> data, File file) throws IOException { + exportToFile(data, true, file); + } + + /** + * Produces the cvs (without header) of the given {@code data} into the given {@code file}. + * + * @param data data to export + * @param file where to perform the export + * @throws IOException + */ + public void exportToFileWithoutHeader(Iterable<O> data, File file) throws IOException { + exportToFile(data, false, file); + } + + /** + * Produces the cvs of the given {@code data} into the given {@code file}. + * + * @param data data to export + * @param writeHeader write or not header (first line of the export) + * @param file where to perform the export + * @throws IOException + */ + public void exportToFile(Iterable<O> data, boolean writeHeader, File file) throws IOException { + try (FileOutputStream outputStream = new FileOutputStream(file)) { + exportToOutputStream(data, writeHeader, outputStream); + } + } + + /** + * Produces the cvs of the given {@code data} and returns it as a {@link String}. + * + * @param data data to export + * @return the string of the csv + * @throws IOException + */ + public String exportToString(Iterable<O> data) throws IOException { + String result = exportToString(data, true); + return result; + } + + /** + * Produces the cvs (without header) of the given {@code data} and returns it as a {@link String}. + * + * @param data data to export + * @return the string of the csv + * @throws IOException + */ + public String exportToStringWithoutHeader(Iterable<O> data) throws IOException { + String result = exportToString(data, false); + return result; + } + + /** + * Produces the cvs of the given {@code data} and returns it as a {@link String}. + * + * @param data data to export + * @param writeHeader write or not header (first line of the export) + * @return the string of the csv + * @throws IOException + */ + public String exportToString(Iterable<O> data, boolean writeHeader) throws IOException { + + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { + + exportToOutputStream(data, writeHeader, outputStream); + + String result = new String(outputStream.toByteArray(), configuration.getCharset()); + return result; + + } + + } + + /** + * Produces the cvs of the given {@code data} and returns it as a {@link Reader}. + * + * @param data data to export + * @return the string of the csv + * @throws IOException + */ + public Reader exportToReader(Iterable<O> data) throws IOException { + Reader reader = exportToReader(data, true); + return reader; + } + + /** + * Produces the cvs (without header) of the given {@code data} and returns it as a {@link Reader}. + * + * @param data data to export + * @return the string of the csv + * @throws IOException + */ + public Reader exportToReaderWithoutHeader(Iterable<O> data) throws IOException { + Reader reader = exportToReader(data, false); + return reader; + } + + /** + * Produces the cvs (without header) of the given {@code data} and returns it as a {@link Reader}. + * + * @param data data to export + * @param writeHeader write or not header (first line of the export) + * @return the string of the csv + * @throws IOException + */ + public Reader exportToReader(Iterable<O> data, boolean writeHeader) throws IOException { + ExporterAction<O> action = ExporterAction.of(configuration, data, writeHeader); + Reader reader = action.toReader(); + return reader; + } + + /** + * Produces the cvs of the given {@code data} and returns it as a {@link InputStream}. + * + * @param data data to export + * @return the string of the csv + * @throws IOException + */ + public InputStream exportToInputStream(Iterable<O> data) throws IOException { + InputStream intputStream = exportToInputStream(data, true); + return intputStream; + } + + /** + * Produces the cvs (without header) of the given {@code data} and returns it as a {@link InputStream}. + * + * @param data data to export + * @return the string of the csv + * @throws IOException + */ + public InputStream exportToInputStreamWithoutHeader(Iterable<O> data) throws IOException { + InputStream intputStream = exportToInputStream(data, false); + return intputStream; + } + + /** + * Produces the cvs (without header) of the given {@code data} and returns it as a {@link InputStream}. + * + * @param data data to export + * @return the string of the csv + * @throws IOException + */ + public InputStream exportToInputStream(Iterable<O> data, boolean writeHeader) throws IOException { + ExporterAction<O> action = ExporterAction.of(configuration, data, writeHeader); + InputStream intputStream = action.toInputStream(); + return intputStream; + } + + protected Exporter(ExporterConfiguration<O> configuration) { + this.configuration = configuration; + } + + protected static class ExporterAction<E> { + + /** + * Data to export. + */ + protected final Iterator<E> dataIterator; + + /** + * Write header ? + */ + protected final boolean writeHeader; + + /** + * Exporter configuration. + */ + protected final ExporterConfiguration<E> configuration; + + /** + * Number of cells per row. + */ + protected final int nbCellsPerRow; + + /** + * Is the first row was consumed ? + */ + protected boolean firstRowConsumed; + + /** + * The current row to export. + */ + protected String currentRow; + + protected static <E> ExporterAction<E> of(ExporterConfiguration<E> configuration, Iterable<E> data, boolean writeHeader) { + return new ExporterAction<>(configuration, writeHeader, data); + } + + protected ExporterAction(ExporterConfiguration<E> configuration, boolean writeHeader, Iterable<E> data) { + this.configuration = configuration; + this.writeHeader = writeHeader; + this.dataIterator = data.iterator(); + this.nbCellsPerRow = configuration.getColumns().size(); + } + + public void export(OutputStream outputStream) throws IOException { + + for (String row = readFirstRowOrGetCurrentRow(); row != null; row = readNextRow()) { + outputStream.write(row.getBytes()); + } + + } + + public void export(Writer writer) throws IOException { + + for (String row = readFirstRowOrGetCurrentRow(); row != null; row = readNextRow()) { + writer.write(row); + } + + } + + public InputStream toInputStream() { + + return new InputStream() { + + @Override + public int read() throws IOException { + + String row = readFirstRowOrGetCurrentRow(); + + int result; + if (row == null) { + + // end of stream + result = -1; + + } else { + + int length = row.length(); + + result = getCharAt(0); + + updateCurrentRow(1, length == 1); + + } + + return result; + + } + + @Override + public void close() throws IOException { + // nothing to do + } + }; + } + + public Reader toReader() { + + return new Reader() { + + @Override + public int read(char[] cbuf, int off, int len) throws IOException { + String row = readFirstRowOrGetCurrentRow(); + + int result; + if (row == null) { + + // end of stream + result = -1; + + } else { + + int length = row.length(); + if (length >= len) { + + // enough in this row to fill buffer + fillBuffer(cbuf, off, len); + updateCurrentRow(len, false); + + result = len; + + } else { + + // fill with that we got + fillBuffer(cbuf, off, length); + row = updateCurrentRow(length, true); + if (row == null) { + + // no more stuff + result = length; + + } else { + + // ask for the rest + result = length + read(cbuf, off + length, len - length); + + } + + } + + } + + return result; + + } + + @Override + public void close() throws IOException { + // nothing to do + } + }; + + } + + protected String readFirstRowOrGetCurrentRow() { + if (!firstRowConsumed) { + firstRowConsumed = true; + if (writeHeader) { + currentRow = readHeader(); + } else { + currentRow = readNextRow(); + } + } + return currentRow; + } + + protected String readNextRow() { + + String nextRow = null; + if (dataIterator.hasNext()) { + E row = dataIterator.next(); + try { + nextRow = readRow(row); + } catch (Exception e) { + throw new ImportRuntimeException("Could not obtain row", e); + } + } + + return nextRow; + + } + + protected void fillBuffer(char[] cbuf, int off, int len) { + for (int i = 0; i < len; i++) { + char c = getCharAt(i); + cbuf[i + off] = c; + } + } + + protected char getCharAt(int index) { + char charAt = currentRow.charAt(index); + //FIXME-tc-2015-12-10 See if we can do better + char encodedChar = (char) configuration.getCharset().encode(String.valueOf(charAt)).get(); + return encodedChar; + } + + protected String updateCurrentRow(int len, boolean computeNext) { + + if (computeNext) { + currentRow = readNextRow(); + } else { + currentRow = currentRow.substring(len); + if (currentRow.isEmpty()) { + currentRow = null; + } + } + return currentRow; + + } + + protected String readHeader() { + + StringBuilder rowBuilder = new StringBuilder(); + int index = 0; + for (ExportableColumn<E, ?> column : configuration.getColumns()) { + + String formattedCell = column.getHeaderName(); + addCellToRowBuilder(rowBuilder, ++index, formattedCell); + + } + return toFormattedRow(rowBuilder); + + } + + protected String readRow(E row) throws Exception { + + StringBuilder rowBuilder = new StringBuilder(); + int index = 0; + for (ExportableColumn<E, Object> column : configuration.getColumns()) { + + Object cell = column.getValue(row); + String formattedCell = column.formatValue(cell); + Preconditions.checkNotNull(formattedCell, + String.format("column for header %s returned a null value.%s", + column.getHeaderName(), column.toString())); + addCellToRowBuilder(rowBuilder, ++index, formattedCell); + + } + return toFormattedRow(rowBuilder); + + } + + protected void addCellToRowBuilder(StringBuilder rowBuilder, int index, String formattedCell) { + String csvCell = StringUtil.escapeCsvValue(formattedCell, configuration.getCellSeparator()); + rowBuilder.append(csvCell); + if (index < nbCellsPerRow) { + rowBuilder.append(configuration.getCellSeparator()); + } + } + + protected String toFormattedRow(StringBuilder rowBuilder) { + rowBuilder.append(configuration.getEndOfLineSeparator()); + String formattedRow = rowBuilder.toString(); + return formattedRow; + } + + } + +} diff --git a/src/main/java/org/nuiton/csv/ExporterBuilder.java b/src/main/java/org/nuiton/csv/ExporterBuilder.java new file mode 100644 index 0000000..b55fe6c --- /dev/null +++ b/src/main/java/org/nuiton/csv/ExporterBuilder.java @@ -0,0 +1,97 @@ +package org.nuiton.csv; + +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableSet; + +import java.nio.charset.Charset; + +/** + * To build an {@link Exporter}. + * + * Created on 09/12/15. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 3.0 + */ +public class ExporterBuilder<O> { + + public static final String DEFAULT_CELL_SEPARATOR = ";"; + + public static final String DEFAULT_END_OF_LINE_SEPARATOR = "\n"; + + public static final Charset DEFAULT_CHARSET = Charset.defaultCharset(); + + protected final ExporterConfiguration<O> configuration; + + /** + * Set the {@link ExporterConfiguration#cellSeparator} and return the builder. + * + * @param cellSeparator new value for {@link ExporterConfiguration#cellSeparator}. + * @return the builder + */ + public ExporterBuilder<O> setCellSeparator(String cellSeparator) { + configuration.setCellSeparator(cellSeparator); + return this; + } + + /** + * Set the {@link ExporterConfiguration#endOfLineSeparator} and return the builder. + * + * @param endOfLineSeparator new value for {@link ExporterConfiguration#endOfLineSeparator}. + * @return the builder + */ + public ExporterBuilder<O> setEndOfLineSeparator(String endOfLineSeparator) { + configuration.setEndOfLineSeparator(endOfLineSeparator); + return this; + } + + /** + * Set the {@link ExporterConfiguration#charset} and return the builder. + * + * @param charset new value for {@link ExporterConfiguration#charset}. + * @return the builder + */ + public ExporterBuilder<O> setCharset(Charset charset) { + configuration.setCharset(charset); + return this; + } + + /** + * Sets the {@link ExporterConfiguration#columns} and return the builder. + * + * @param columns new value for {@link ExporterConfiguration#columns}. + * @return the builder + */ + public ExporterBuilder<O> setColumns(ImmutableSet<ExportableColumn<O, Object>> columns) { + configuration.setColumns(columns); + return this; + } + + public Exporter<O> build() { + + Preconditions.checkNotNull(configuration.getColumns(), "No columns defined."); + Preconditions.checkNotNull(configuration.getCellSeparator(), "No charset defined."); + Preconditions.checkNotNull(configuration.getCellSeparator(), "No cellSeparator defined."); + Preconditions.checkNotNull(configuration.getEndOfLineSeparator(), "No endOfLineSeparator defined."); + + return new Exporter<>(configuration); + + } + + public ExporterBuilder(ExportModel<O> model) { + this.configuration = new ExporterConfiguration<>(); + setColumns(ImmutableSet.copyOf(model.getColumnsForExport())); + setCharset(DEFAULT_CHARSET); + setCellSeparator(DEFAULT_CELL_SEPARATOR); + setEndOfLineSeparator(DEFAULT_END_OF_LINE_SEPARATOR); + } + + public ExporterBuilder(ExporterConfiguration<O> configuration) { + this.configuration = new ExporterConfiguration<>(); + setColumns(configuration.getColumns()); + setCharset(configuration.getCharset()); + setCellSeparator(configuration.getCellSeparator()); + setEndOfLineSeparator(configuration.getEndOfLineSeparator()); + } + +} diff --git a/src/main/java/org/nuiton/csv/ExporterConfiguration.java b/src/main/java/org/nuiton/csv/ExporterConfiguration.java new file mode 100644 index 0000000..4fea998 --- /dev/null +++ b/src/main/java/org/nuiton/csv/ExporterConfiguration.java @@ -0,0 +1,68 @@ +package org.nuiton.csv; + +import com.google.common.collect.ImmutableSet; + +import java.nio.charset.Charset; + +/** + * Configuration of an {@link Exporter}. + * + * Created on 09/12/15. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 3.0 + */ +public class ExporterConfiguration<O> { + + /** + * Columns export model. + */ + protected ImmutableSet<ExportableColumn<O, Object>> columns; + + /** + * Cell separator. + */ + protected String cellSeparator; + + /** + * Eof of line separator. + */ + protected String endOfLineSeparator; + + /** + * Charset used to perform the export. + */ + protected Charset charset; + + public String getCellSeparator() { + return cellSeparator; + } + + public String getEndOfLineSeparator() { + return endOfLineSeparator; + } + + public Charset getCharset() { + return charset; + } + + public ImmutableSet<ExportableColumn<O, Object>> getColumns() { + return columns; + } + + public void setCellSeparator(String cellSeparator) { + this.cellSeparator = cellSeparator; + } + + public void setCharset(Charset charset) { + this.charset = charset; + } + + public void setColumns(ImmutableSet<ExportableColumn<O, Object>> columns) { + this.columns = columns; + } + + public void setEndOfLineSeparator(String endOfLineSeparator) { + this.endOfLineSeparator = endOfLineSeparator; + } +} diff --git a/src/main/java/org/nuiton/csv/package-info.java b/src/main/java/org/nuiton/csv/package-info.java index 3742ca4..19bb57c 100644 --- a/src/main/java/org/nuiton/csv/package-info.java +++ b/src/main/java/org/nuiton/csv/package-info.java @@ -23,6 +23,30 @@ * This package contains a framework to import and export data from a * csv file using a model which permits us to validate what to do. * + * <h3>Exporter API (since 3.0)</h3> + * <h2>Steps to export</h2> + * <ul> + * <li>create export model (how objects should be tranformed)</li> + * <li>create exporter (and configure it)</li> + * <li>perform export</li> + * </ul> + * + * <h2>Example (with default configuration values)</h2> + * <pre> + * ExportModel<O> model = ...; + * Exporter<O> exporter = Exporter.of(model); + * Iterable<O> data = ...; + * String csv = exporter.writeToString(data); + * </pre> + * + * <h2>Example (with customized configuration values)</h2> + * <pre> + * ExportModel<O> model = ...; + * Exporter<O> exporter = Exporter.build(model).setCellSeparator(",").build(); + * Iterable<O> data = ...; + * String csv = exporter.writeToString(data); + * </pre> + * * @author Tony Chemit - chemit@codelutin.com * @since 2.4 */ diff --git a/src/test/java/org/nuiton/csv/ExporterTest.java b/src/test/java/org/nuiton/csv/ExporterTest.java new file mode 100644 index 0000000..a3768e8 --- /dev/null +++ b/src/test/java/org/nuiton/csv/ExporterTest.java @@ -0,0 +1,233 @@ +package org.nuiton.csv; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; +import com.google.common.io.Files; +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.nuiton.util.DateUtil; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.Reader; +import java.io.StringWriter; +import java.nio.charset.Charset; +import java.util.Collections; +import java.util.Iterator; +import java.util.Set; + +/** + * Created on 09/12/15. + * + * @author Tony Chemit - chemit@codelutin.com + */ +public class ExporterTest { + + public static final Charset CHARSET = Charset.forName("UTF-8"); + + public static final Iterable<String> RESULTS = ImmutableList.of( + "DATE~TITLE~NUMBER--\n", + "DATE~TITLE~NUMBER--\n" + + "01/12/2011~Batman~1--\n", + "DATE~TITLE~NUMBER--\n" + + "01/12/2011~Batman~1--\n" + + "02/12/2011~~7--\n", + "DATE~TITLE~NUMBER--\n" + + "01/12/2011~Batman~1--\n" + + "02/12/2011~~7--\n" + + "07/12/2011~~9--\n" + + "18/12/2011~~18--\n" + + "23/12/2011~~4--\n" + ); + + protected Iterable<Set<RowBean>> sets; + + protected ExportModel<RowBean> model = new RowBeanExportModel(); + + protected Exporter<RowBean> exporter; + + @Before + public void setUp() { + + ImmutableSet.Builder<RowBean> builder = ImmutableSet.builder(); + + ImmutableList.Builder<Set<RowBean>> setBuilder = ImmutableList.builder(); + + setBuilder.add(Collections.<RowBean>emptySet()); + + builder.add(new RowBean(DateUtil.createDate(1, 12, 2011), "Batman", 1, RowBeanEnum.ONE)); + setBuilder.add(builder.build()); + + builder.add(new RowBean(DateUtil.createDate(2, 12, 2011), "", 7, RowBeanEnum.TWO)); + setBuilder.add(builder.build()); + + builder.add((new RowBean(DateUtil.createDate(7, 12, 2011), "", 9, RowBeanEnum.ZERO))); + builder.add((new RowBean(DateUtil.createDate(18, 12, 2011), "", 18, RowBeanEnum.ONE))); + builder.add((new RowBean(DateUtil.createDate(23, 12, 2011), "", 4, RowBeanEnum.TWO))); + setBuilder.add(builder.build()); + + sets = setBuilder.build(); + + exporter = Exporter + .builder(model) + .setCellSeparator("~") + .setEndOfLineSeparator("--\n") + .setCharset(CHARSET) + .build(); + + } + + @Test + public void testExportToString() throws Exception { + + Iterator<String> resultIterator = RESULTS.iterator(); + for (Set<RowBean> set : sets) { + + String csv = exporter.exportToString(set); + + String csvWithoutHeader = exporter.exportToStringWithoutHeader(set); + + assertExportResultEquals(resultIterator, csv, csvWithoutHeader); + + } + + } + + @Test + public void testExportToWriter() throws Exception { + + Iterator<String> resultIterator = RESULTS.iterator(); + for (Set<RowBean> set : sets) { + + String csv; + try (StringWriter writer = new StringWriter()) { + exporter.exportToWriter(set, writer); + csv = writer.toString(); + } + + String csvWithoutHeader; + try (StringWriter writer = new StringWriter()) { + exporter.exportToWriterWithoutHeader(set, writer); + csvWithoutHeader = writer.toString(); + } + + assertExportResultEquals(resultIterator, csv, csvWithoutHeader); + + } + + } + + @Test + public void testExportToOuputStream() throws Exception { + + File parentFile = new File(FileUtils.getTempDirectory(), getClass().getName() + "-testExportToOuputStream"); + FileUtils.forceMkdir(parentFile); + + int index = 0; + Iterator<String> resultIterator = RESULTS.iterator(); + for (Set<RowBean> set : sets) { + + File exportFile = new File(parentFile, (index++) + ".csv"); + try (OutputStream outputStream = new FileOutputStream(exportFile)) { + exporter.exportToOutputStream(set, outputStream); + } + + File exportFileWithoutHeader = new File(parentFile, (index) + "-withoutHeader.csv"); + try (OutputStream outputStream = new FileOutputStream(exportFileWithoutHeader)) { + exporter.exportToOutputStreamWithoutHeader(set, outputStream); + } + + String csv = Files.toString(exportFile, CHARSET); + String csvWithoutHeader = Files.toString(exportFileWithoutHeader, CHARSET); + assertExportResultEquals(resultIterator, csv, csvWithoutHeader); + + } + + } + + @Test + public void testExportToFile() throws Exception { + + File parentFile = new File(FileUtils.getTempDirectory(), getClass().getName() + "-testExportToFile"); + FileUtils.forceMkdir(parentFile); + + Iterator<String> resultIterator = RESULTS.iterator(); + + int index = 0; + for (Set<RowBean> set : sets) { + + File exportFile = new File(parentFile, (index++) + ".csv"); + exporter.exportToFile(set, exportFile); + + String csv = Files.toString(exportFile, CHARSET); + + File exportFileWithoutHeader = new File(parentFile, (index) + "-withoutHeader.csv"); + exporter.exportToFileWithoutHeader(set, exportFileWithoutHeader); + + String csvWithoutHeader = Files.toString(exportFileWithoutHeader, CHARSET); + + assertExportResultEquals(resultIterator, csv, csvWithoutHeader); + + } + + } + + @Test + public void testExportToReader() throws Exception { + + Iterator<String> resultIterator = RESULTS.iterator(); + for (Set<RowBean> set : sets) { + + String csv; + try (Reader reader = exporter.exportToReader(set)) { + csv = IOUtils.toString(reader); + } + + String csvWithoutHeader; + try (Reader reader = exporter.exportToReaderWithoutHeader(set)) { + csvWithoutHeader = IOUtils.toString(reader); + } + + assertExportResultEquals(resultIterator, csv, csvWithoutHeader); + + } + + } + + @Test + public void testExportToInputStream() throws Exception { + + Iterator<String> resultIterator = RESULTS.iterator(); + for (Set<RowBean> set : sets) { + + String csv; + try (InputStream inputStream = exporter.exportToInputStream(set)) { + csv = IOUtils.toString(inputStream); + } + String csvWithoutHeader; + try (InputStream inputStream = exporter.exportToInputStreamWithoutHeader(set)) { + csvWithoutHeader = IOUtils.toString(inputStream); + } + assertExportResultEquals(resultIterator, csv, csvWithoutHeader); + + } + + } + + protected void assertExportResultEquals(Iterator<String> resultIterator, String actual, String actualWithoutHeader) { + + String expected = resultIterator.next(); + String expectedWithoutHeader = StringUtils.substringAfter(expected, "\n"); + + Assert.assertEquals(expected, actual); + Assert.assertEquals(expectedWithoutHeader, actualWithoutHeader); + + } + +} -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
participants (1)
-
nuiton.org scm