From c254a39ac2009d37b464bc7d56730c650061b658 Mon Sep 17 00:00:00 2001 From: PencilWarrior882 <184399684+PencilWarrior882@users.noreply.github.com> Date: Tue, 23 Jun 2026 04:57:18 +0600 Subject: [PATCH 1/2] test: cover CSVExporter row and column output Add focused CSVExporter tests that write row-oriented and column-oriented XY series data to temporary files and verify the exact CSV content using UTF-8 reads. These tests lock the current export format before refactoring the writer implementation, so the resource-handling cleanup can be shown to preserve behavior. --- .../org/knowm/xchart/CSVExporterTest.java | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 xchart/src/test/java/org/knowm/xchart/CSVExporterTest.java diff --git a/xchart/src/test/java/org/knowm/xchart/CSVExporterTest.java b/xchart/src/test/java/org/knowm/xchart/CSVExporterTest.java new file mode 100644 index 00000000..5543367d --- /dev/null +++ b/xchart/src/test/java/org/knowm/xchart/CSVExporterTest.java @@ -0,0 +1,51 @@ +package org.knowm.xchart; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.File; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +public class CSVExporterTest { + + @TempDir Path tempDir; + + @Test + public void writeCSVRowsExportsXAndYDataRows() throws Exception { + + XYChart chart = new XYChartBuilder().width(600).height(400).build(); + chart.addSeries("series1", new double[] {1.0, 2.0, 3.0}, new double[] {12.0, 34.0, 56.0}); + + CSVExporter.writeCSVRows(chart.getSeries("series1"), tempDir.toString() + File.separator); + + String csv = Files.readString(tempDir.resolve("series1.csv"), StandardCharsets.UTF_8); + assertThat(csv) + .isEqualTo( + "1.0,2.0,3.0" + + System.lineSeparator() + + "12.0,34.0,56.0" + + System.lineSeparator()); + } + + @Test + public void writeCSVColumnsExportsXAndYDataColumns() throws Exception { + + XYChart chart = new XYChartBuilder().width(600).height(400).build(); + chart.addSeries("series1", new double[] {1.0, 2.0, 3.0}, new double[] {12.0, 34.0, 56.0}); + + CSVExporter.writeCSVColumns(chart.getSeries("series1"), tempDir.toString() + File.separator); + + String csv = Files.readString(tempDir.resolve("series1.csv"), StandardCharsets.UTF_8); + assertThat(csv) + .isEqualTo( + "1.0,12.0" + + System.lineSeparator() + + "2.0,34.0" + + System.lineSeparator() + + "3.0,56.0" + + System.lineSeparator()); + } +} From 8fc50eac17d945723443c7967d35d61eab93cf06 Mon Sep 17 00:00:00 2001 From: PencilWarrior882 <184399684+PencilWarrior882@users.noreply.github.com> Date: Tue, 23 Jun 2026 04:58:56 +0600 Subject: [PATCH 2/2] refactor: simplify CSVExporter writer handling Replace manual CSV Writer lifecycle management with try-with-resources so each export path closes its output stream reliably without duplicated finally blocks. Use StandardCharsets.UTF_8 instead of the UTF8 string literal, centralize the platform line separator, and remove stale commented-out CSV construction code. Public CSVExporter APIs and output format remain unchanged. --- .../java/org/knowm/xchart/CSVExporter.java | 50 +++++++------------ 1 file changed, 18 insertions(+), 32 deletions(-) diff --git a/xchart/src/main/java/org/knowm/xchart/CSVExporter.java b/xchart/src/main/java/org/knowm/xchart/CSVExporter.java index f4218686..6540c1bd 100644 --- a/xchart/src/main/java/org/knowm/xchart/CSVExporter.java +++ b/xchart/src/main/java/org/knowm/xchart/CSVExporter.java @@ -1,6 +1,11 @@ package org.knowm.xchart; -import java.io.*; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileOutputStream; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.nio.charset.StandardCharsets; /** * This class is used to export Chart data to a folder containing one or more CSV files. The parent @@ -9,6 +14,8 @@ */ public class CSVExporter { + private static final String LINE_SEPARATOR = System.lineSeparator(); + /** * Export all XYChart series as rows in separate CSV files. * @@ -31,29 +38,20 @@ public static void writeCSVRows(XYChart chart, String path2Dir) { public static void writeCSVRows(XYSeries series, String path2Dir) { File newFile = new File(path2Dir + series.getName() + ".csv"); - Writer out = null; - try { + try (Writer out = + new BufferedWriter( + new OutputStreamWriter(new FileOutputStream(newFile), StandardCharsets.UTF_8))) { - out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(newFile), "UTF8")); - String csv = join(series.getXData(), ",") + System.getProperty("line.separator"); + String csv = join(series.getXData(), ",") + LINE_SEPARATOR; out.write(csv); - csv = join(series.getYData(), ",") + System.getProperty("line.separator"); + csv = join(series.getYData(), ",") + LINE_SEPARATOR; out.write(csv); if (series.getExtraValues() != null) { - csv = join(series.getExtraValues(), ",") + System.getProperty("line.separator"); + csv = join(series.getExtraValues(), ",") + LINE_SEPARATOR; out.write(csv); } } catch (Exception e) { e.printStackTrace(); - } finally { - if (out != null) { - try { - out.flush(); - out.close(); - } catch (IOException e) { - // NOP - } - } } } @@ -102,10 +100,10 @@ public static void writeCSVColumns(XYChart chart, String path2Dir) { public static void writeCSVColumns(XYSeries series, String path2Dir) { File newFile = new File(path2Dir + series.getName() + ".csv"); - Writer out = null; - try { + try (Writer out = + new BufferedWriter( + new OutputStreamWriter(new FileOutputStream(newFile), StandardCharsets.UTF_8))) { - out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(newFile), "UTF8")); double[] xData = series.getXData(); double[] yData = series.getYData(); double[] errorBarData = series.getExtraValues(); @@ -118,24 +116,12 @@ public static void writeCSVColumns(XYSeries series, String path2Dir) { sb.append(errorBarData[i]).append(","); } sb.setLength(sb.length() - 1); - sb.append(System.getProperty("line.separator")); + sb.append(LINE_SEPARATOR); - // String csv = xDataPoint + "," + yDataPoint + errorBarValue == null ? "" : ("," + - // errorBarValue) + System.getProperty("line.separator"); - // String csv = + yDataPoint + System.getProperty("line.separator"); out.write(sb.toString()); } } catch (Exception e) { e.printStackTrace(); - } finally { - if (out != null) { - try { - out.flush(); - out.close(); - } catch (IOException e) { - // NOP - } - } } } }