1
2
3
4
5
6
7
8
9
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
28
29 class RowWriterTest {
30
31
32
33
34
35
36
37 @Test
38 void readAndWrite() throws IOException {
39
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
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
61
62
63
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
82
83
84
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
99
100
101
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
116
117
118
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
133
134
135
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
150
151
152
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
167
168
169
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
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 }