Author: tchemit Date: 2014-08-03 12:43:58 +0200 (Sun, 03 Aug 2014) New Revision: 2627 Url: http://forge.nuiton.org/projects/nuiton-csv/repository/revisions/2627 Log: fixes #3407: Export#write(OutputStream) should NOT close stream Modified: trunk/src/main/java/org/nuiton/csv/Export.java trunk/src/test/java/org/nuiton/csv/ExportTest.java Modified: trunk/src/main/java/org/nuiton/csv/Export.java =================================================================== --- trunk/src/main/java/org/nuiton/csv/Export.java 2014-08-03 09:04:40 UTC (rev 2626) +++ trunk/src/main/java/org/nuiton/csv/Export.java 2014-08-03 10:43:58 UTC (rev 2627) @@ -221,11 +221,8 @@ public void write(OutputStream outputStream, Charset charset, boolean writeHeader) throws Exception { Writer writer = new OutputStreamWriter(outputStream, charset); - try { - write(writer, writeHeader); - } finally { - writer.close(); - } + write(writer, writeHeader); + writer.flush(); } public void write(OutputStream outputStream) throws Exception { Modified: trunk/src/test/java/org/nuiton/csv/ExportTest.java =================================================================== --- trunk/src/test/java/org/nuiton/csv/ExportTest.java 2014-08-03 09:04:40 UTC (rev 2626) +++ trunk/src/test/java/org/nuiton/csv/ExportTest.java 2014-08-03 10:43:58 UTC (rev 2627) @@ -23,6 +23,8 @@ */ package org.nuiton.csv; +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.junit.Assert; @@ -30,16 +32,23 @@ import org.junit.Test; import org.nuiton.util.DateUtil; +import java.io.File; +import java.io.FileOutputStream; import java.nio.charset.Charset; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; +import java.util.zip.ZipOutputStream; public class ExportTest { private static final Log log = LogFactory.getLog(ExportTest.class); + public static final Charset CHARSET = Charset.forName("UTF-8"); + protected Set<RowBean> oneSizedSet = new HashSet<RowBean>(); protected Set<RowBean> twoSizedSet = new HashSet<RowBean>(); @@ -51,7 +60,7 @@ protected ExportModel<RowBean> model = new RowBeanExportModel(); @Before - public void setUp() { + public void setUp() { oneSizedSet.add(new RowBean(DateUtil.createDate(1, 12, 2011), "Batman", 1, RowBeanEnum.ONE)); @@ -71,7 +80,7 @@ for (Set<RowBean> set : sets) { - String csv = Export.exportToString(model, set, Charset.forName("UTF-8")); + String csv = Export.exportToString(model, set, CHARSET); if (log.isDebugEnabled()) { log.debug("exported csv:\n" + csv); @@ -87,4 +96,44 @@ } + /** + * See https://forge.nuiton.org/issues/3407 + */ + @Test + public void testExportToOuputStream() throws Exception { + + File f = new File(FileUtils.getTempDirectory(), getClass().getName() + "-exportcsvtest.zip"); + + FileUtils.forceMkdir(f.getParentFile()); + + ZipOutputStream outputStream = new ZipOutputStream(new FileOutputStream(f)); + + for (Set<RowBean> set : sets) { + + outputStream.putNextEntry(new ZipEntry("/export" + (set.size()) + ".csv")); + Export.newExport(model, set).write(outputStream, CHARSET); + } + + outputStream.close(); + + ZipFile zipFile = new ZipFile(f); + + for (Set<RowBean> set : sets) { + + String csv = IOUtils.toString(zipFile.getInputStream(new ZipEntry("/export" + (set.size()) + ".csv"))); + + if (log.isDebugEnabled()) { + log.debug("exported csv:\n" + csv); + } + + // 1 header line + one line per RowBean instance + int expectedLineCount = 1 + set.size(); + // number of '\n' in csv + int actualLineCount = csv.split("\n").length; + Assert.assertEquals("exported CSV must have all lines", + expectedLineCount, actualLineCount); + } + + } + }