From cb0724c7d9711115749e476fab815ee6bad1ed40 Mon Sep 17 00:00:00 2001
From: lorenz <stu203404@mail.uni-kiel.de>
Date: Sun, 19 Jun 2022 15:55:02 +0200
Subject: [PATCH] Fix IOHandler.kt + IOHandlerTest.kt Now adds quotation marks
 around values if they contain commas

---
 .../kotlin/rocks/theodolite/core/IOHandler.kt | 16 +++++--
 .../rocks/theodolite/core/IOHandlerTest.kt    | 43 +++++++++++++++++++
 2 files changed, 55 insertions(+), 4 deletions(-)

diff --git a/theodolite/src/main/kotlin/rocks/theodolite/core/IOHandler.kt b/theodolite/src/main/kotlin/rocks/theodolite/core/IOHandler.kt
index 4d2cab0da..8721ea9d4 100644
--- a/theodolite/src/main/kotlin/rocks/theodolite/core/IOHandler.kt
+++ b/theodolite/src/main/kotlin/rocks/theodolite/core/IOHandler.kt
@@ -64,15 +64,23 @@ class IOHandler {
      * Write to CSV file
      *
      * @param fileURL the URL of the file
-     * @param data  the data to write in the file, as list of list, each subList corresponds to a row in the CSV file
-     * @param columns columns of the CSV file
+     * @param data the data to write in the csv, as list of list,
+     *             each sublist corresponds to a row in the CSV file
+     * @param columns name of the columns
      */
     fun writeToCSVFile(fileURL: String, data: List<List<String>>, columns: List<String>) {
         val outputFile = File("$fileURL.csv")
         PrintWriter(outputFile).use { pw ->
             pw.println(columns.joinToString(separator = ","))
-            data.forEach {
-                pw.println(it.joinToString(separator = ","))
+            data.forEach{ row ->
+                val writeRow = row.toMutableList()
+                // change entry if it contains a comma
+                writeRow.forEachIndexed { index, entry ->
+                    if (entry.contains(",")){
+                        writeRow[index] = "\"" + entry + "\""
+                    }
+                }
+                pw.println(writeRow.joinToString(separator = ","))
             }
         }
         logger.info { "Wrote CSV file: $fileURL to ${outputFile.absolutePath}." }
diff --git a/theodolite/src/test/kotlin/rocks/theodolite/core/IOHandlerTest.kt b/theodolite/src/test/kotlin/rocks/theodolite/core/IOHandlerTest.kt
index 65e84d7dd..acdf3aa6e 100644
--- a/theodolite/src/test/kotlin/rocks/theodolite/core/IOHandlerTest.kt
+++ b/theodolite/src/test/kotlin/rocks/theodolite/core/IOHandlerTest.kt
@@ -11,6 +11,7 @@ import org.junit.jupiter.api.Test
 import org.junit.rules.TemporaryFolder
 import org.junitpioneer.jupiter.ClearEnvironmentVariable
 import org.junitpioneer.jupiter.SetEnvironmentVariable
+import java.util.stream.Collectors
 
 const val FOLDER_URL = "Test-Folder"
 
@@ -65,6 +66,48 @@ internal class IOHandlerTest {
         )
     }
 
+    /**
+     * Tests if values with commas are surrounded with quotation marks.
+     */
+    @Test
+    fun testWriteToCSVFileWithComma() {
+        temporaryFolder.create()
+        val folder = temporaryFolder.newFolder(FOLDER_URL)
+
+        val columns = listOf("Fruit", "Color")
+
+        val testContent = listOf(
+            listOf("apples, paprika", "red"),
+            listOf("bananas, pineapple", "yellow"),
+            listOf("avocado, coconut", "brown")
+        )
+
+        val expectedContent = listOf(
+            listOf("\"apples, paprika\"", "red"),
+            listOf("\"bananas, pineapple\"", "yellow"),
+            listOf("\"avocado, coconut\"", "brown")
+        )
+
+        IOHandler().writeToCSVFile(
+            fileURL = "${folder.absolutePath}/test-file",
+            data = testContent,
+            columns = columns
+        )
+
+        // construct string from the columns
+        var expected = columns.stream().collect(Collectors.joining(","))
+
+        // add values from the expectedContent to expected string
+        expectedContent.forEach{
+            expected += "\n" + it.joinToString(separator = ",")
+        }
+
+        assertEquals(
+            expected,
+            IOHandler().readFileAsString("${folder.absolutePath}/test-file.csv")
+        )
+    }
+
     @Test
     fun testWriteToJSONFile() {
         temporaryFolder.create()
-- 
GitLab