View Javadoc
1   /*
2    * CSVeed (https://github.com/42BV/CSVeed)
3    *
4    * Copyright 2013-2023 CSVeed.
5    *
6    * All rights reserved. This program and the accompanying materials
7    * are made available under the terms of The Apache Software License,
8    * Version 2.0 which accompanies this distribution, and is available at
9    * https://www.apache.org/licenses/LICENSE-2.0.txt
10   */
11  package org.csveed.row;
12  
13  import static org.junit.jupiter.api.Assertions.assertEquals;
14  import static org.junit.jupiter.api.Assertions.assertThrows;
15  
16  import java.io.IOException;
17  import java.io.Reader;
18  import java.io.StringReader;
19  import java.io.StringWriter;
20  import java.util.List;
21  
22  import org.csveed.api.Row;
23  import org.csveed.report.CsvException;
24  import org.junit.jupiter.api.Test;
25  
26  /**
27   * The Class RowWriterTest.
28   */
29  class RowWriterTest {
30  
31      /**
32       * Read and write.
33       *
34       * @throws IOException
35       *             Signals that an I/O exception has occurred.
36       */
37      @Test
38      void readAndWrite() throws IOException {
39          // First read...
40          Reader reader = new StringReader(
41                  "alpha;beta;gamma\n" + "\"row 1, cell 1\";\"row 1, cell 2\";\"row 1, cell 3\"\n"
42                          + "\"row 2, cell 1\";\"row 2, cell 2\";\"row 2, cell 3\"\n");
43          RowReader lineReader = new RowReaderImpl(reader);
44          List<Row> rows = lineReader.readRows();
45  
46          // ... then write
47          try (StringWriter writer = new StringWriter()) {
48              RowWriter rowWriter = new RowWriterImpl(writer);
49              rowWriter.writeHeader(rows.get(0).getHeader());
50              rowWriter.writeRows(rows);
51  
52              assertEquals(
53                      "\"alpha\";\"beta\";\"gamma\"\r\n" + "\"row 1, cell 1\";\"row 1, cell 2\";\"row 1, cell 3\"\r\n"
54                              + "\"row 2, cell 1\";\"row 2, cell 2\";\"row 2, cell 3\"\r\n",
55                      writer.getBuffer().toString());
56          }
57      }
58  
59      /**
60       * Write multiple rows.
61       *
62       * @throws IOException
63       *             Signals that an I/O exception has occurred.
64       */
65      @Test
66      void writeMultipleRows() throws IOException {
67          try (StringWriter writer = new StringWriter()) {
68              RowWriter rowWriter = new RowWriterImpl(writer);
69              rowWriter.writeHeader(new String[] { "desc1", "desc2", "desc3" });
70              rowWriter.writeRows(new String[][] { { "alpha", "beta", "gamma" }, { "delta", "epsilon", "phi" },
71                      { "b1", "b2", "b3" } });
72  
73              assertEquals(
74                      "\"desc1\";\"desc2\";\"desc3\"\r\n" + "\"alpha\";\"beta\";\"gamma\"\r\n"
75                              + "\"delta\";\"epsilon\";\"phi\"\r\n" + "\"b1\";\"b2\";\"b3\"\r\n",
76                      writer.getBuffer().toString());
77          }
78      }
79  
80      /**
81       * Write row with escape characters.
82       *
83       * @throws IOException
84       *             Signals that an I/O exception has occurred.
85       */
86      @Test
87      void writeRowWithEscapeCharacters() throws IOException {
88          try (StringWriter writer = new StringWriter()) {
89              RowInstructions instructions = new RowInstructionsImpl().setUseHeader(false).setEscape('\\');
90              RowWriter rowWriter = new RowWriterImpl(writer, instructions);
91              rowWriter.writeRow(new String[] { "\"tekst met \"quotes\"\"" });
92  
93              assertEquals("\"\\\"tekst met \\\"quotes\\\"\\\"\"\r\n", writer.getBuffer().toString());
94          }
95      }
96  
97      /**
98       * Write row.
99       *
100      * @throws IOException
101      *             Signals that an I/O exception has occurred.
102      */
103     @Test
104     void writeRow() throws IOException {
105         try (StringWriter writer = new StringWriter()) {
106             RowInstructions instructions = new RowInstructionsImpl().setUseHeader(false);
107             RowWriter rowWriter = new RowWriterImpl(writer, instructions);
108             rowWriter.writeRow(new String[] { "alpha", "beta", "gamma" });
109 
110             assertEquals("\"alpha\";\"beta\";\"gamma\"\r\n", writer.getBuffer().toString());
111         }
112     }
113 
114     /**
115      * Write row without quoting.
116      *
117      * @throws IOException
118      *             Signals that an I/O exception has occurred.
119      */
120     @Test
121     void writeRowWithoutQuoting() throws IOException {
122         try (StringWriter writer = new StringWriter()) {
123             RowInstructions instructions = new RowInstructionsImpl().setUseHeader(false).setQuotingEnabled(false);
124             RowWriter rowWriter = new RowWriterImpl(writer, instructions);
125             rowWriter.writeRow(new String[] { "alpha", "beta", "gamma" });
126 
127             assertEquals("alpha;beta;gamma\r\n", writer.getBuffer().toString());
128         }
129     }
130 
131     /**
132      * Write row without quoting and escaping.
133      *
134      * @throws IOException
135      *             Signals that an I/O exception has occurred.
136      */
137     @Test
138     void writeRowWithoutQuotingAndEscaping() throws IOException {
139         try (StringWriter writer = new StringWriter()) {
140             RowInstructions instructions = new RowInstructionsImpl().setUseHeader(false).setQuotingEnabled(false);
141             RowWriter rowWriter = new RowWriterImpl(writer, instructions);
142             rowWriter.writeRow(new String[] { "\"tekst met \"quotes\"\"" });
143 
144             assertEquals("\"tekst met \"quotes\"\"\r\n", writer.getBuffer().toString());
145         }
146     }
147 
148     /**
149      * Write row with null value.
150      *
151      * @throws IOException
152      *             Signals that an I/O exception has occurred.
153      */
154     @Test
155     void writeRowWithNullValue() throws IOException {
156         try (StringWriter writer = new StringWriter()) {
157             RowInstructions instructions = new RowInstructionsImpl().setUseHeader(false);
158             RowWriter rowWriter = new RowWriterImpl(writer, instructions);
159             rowWriter.writeRow(new String[] { "alpha", null, "gamma" });
160 
161             assertEquals("\"alpha\";\"\";\"gamma\"\r\n", writer.getBuffer().toString());
162         }
163     }
164 
165     /**
166      * Write row and header.
167      *
168      * @throws IOException
169      *             Signals that an I/O exception has occurred.
170      */
171     @Test
172     void writeRowAndHeader() throws IOException {
173         try (StringWriter writer = new StringWriter()) {
174             RowWriter rowWriter = new RowWriterImpl(writer);
175             rowWriter.writeHeader(new String[] { "desc1", "desc2", "desc3" });
176             rowWriter.writeRow(new String[] { "alpha", "beta", "gamma" });
177 
178             assertEquals("\"desc1\";\"desc2\";\"desc3\"\r\n" + "\"alpha\";\"beta\";\"gamma\"\r\n",
179                     writer.getBuffer().toString());
180         }
181     }
182 
183     /**
184      * No header written.
185      */
186     @Test
187     void noHeaderWritten() {
188         RowWriter rowWriter = new RowWriterImpl(new StringWriter());
189         assertThrows(CsvException.class, () -> rowWriter.writeRow(new String[] { "alpha", "beta", "gamma" }));
190     }
191 
192 }