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.assertNotNull;
15 import static org.junit.jupiter.api.Assertions.assertThrows;
16
17 import java.io.Reader;
18 import java.io.StringReader;
19 import java.util.List;
20
21 import org.csveed.api.Header;
22 import org.csveed.api.Row;
23 import org.csveed.common.Column;
24 import org.csveed.report.CsvException;
25 import org.csveed.report.RowReport;
26 import org.junit.jupiter.api.Test;
27
28
29
30
31 class RowReaderTest {
32
33
34
35
36 @Test
37 void columnIndexesSameOneBasedApproach() {
38 Reader reader = new StringReader(
39 "alpha;beta;gamma\n" + "\"row 1, cell 1\";\"row 1, cell 2\";\"row 1, cell 3\"\n");
40 RowReaderImpl lineReader = new RowReaderImpl(reader);
41 Row row = lineReader.readRow();
42 assertEquals("alpha", row.getColumnName(1));
43 assertEquals("row 1, cell 1", row.get(1));
44 }
45
46
47
48
49 @Test
50 void getByColumnName() {
51 Reader reader = new StringReader(
52 "alpha;beta;gamma\n" + "\"row 1, cell 1\";\"row 1, cell 2\";\"row 1, cell 3\"\n");
53 RowReaderImpl lineReader = new RowReaderImpl(reader);
54 Row row = lineReader.readRow();
55 assertEquals("row 1, cell 2", row.get("beta"));
56 }
57
58
59
60
61 @Test
62 void emptyLines() {
63 Reader reader = new StringReader(
64 "\n" + "\n" + "\n" + "alpha;beta;gamma\n" + "\"row 1, cell 1\";\"row 1, cell 2\";\"row 1, cell 3\"\n"
65 + "\n" + "\"row 1, cell 1\";\"row 1, cell 2\";\"row 1, cell 3\"");
66 RowReaderImpl lineReader = new RowReaderImpl(reader);
67 assertEquals(2, lineReader.readRows().size());
68 }
69
70
71
72
73 @Test
74 void doNotSkipEmptyLines() {
75 Reader reader = new StringReader("alpha\n" + "\n" + "word\n" + "\n" + "\n");
76 RowReaderImpl lineReader = new RowReaderImpl(reader, new RowInstructionsImpl().skipEmptyLines(false));
77 assertEquals(5, lineReader.readRows().size());
78 }
79
80
81
82
83 @Test
84 void getColumnIndexAt0() {
85 Reader reader = new StringReader("alpha;beta;gamma");
86 RowReaderImpl rowReader = new RowReaderImpl(reader);
87 Header header = rowReader.getHeader();
88 assertThrows(CsvException.class, () -> header.getName(0));
89 }
90
91
92
93
94 @Test
95 void commentLine() {
96 Reader reader = new StringReader("# lots of text\n" + "# bla...\n" + "# more bla...\n" + "alpha;beta;gamma\n"
97 + "\"row 1, cell 1\";\"row 1, cell 2\";\"row 1, cell 3\"\n" + "# this line must be ignored!\n"
98 + "\"row 1, cell 1\";\"row 1, cell 2\";\"row 1, cell 3\"");
99 RowReaderImpl rowReader = new RowReaderImpl(reader);
100 assertEquals(2, rowReader.readRows().size());
101 Header header = rowReader.getHeader();
102 assertEquals("alpha", header.getName(1));
103 }
104
105
106
107
108 @Test
109 void dissimilarNumberOfColumns() {
110 Reader reader = new StringReader("\"row 1, cell 1\";\"row 1, cell 2\";\"row 1, cell 3\"\n"
111 + "\"row 2, cell 1\";\"row 2, cell 2\";\"row 2, cell 3\"\n" + "\"row 3, cell 1\";\"row 3, cell 2\"");
112 RowReaderImpl lineReader = new RowReaderImpl(reader);
113 assertThrows(CsvException.class, () -> lineReader.readRows());
114 }
115
116
117
118
119 @Test
120 void readUnmapped() {
121 Reader reader = new StringReader(
122 "alpha;beta;gamma\n" + "\"row 1, cell 1\";\"row 1, cell 2\";\"row 1, cell 3\"\n"
123 + "\"row 2, cell 1\";\"row 2, cell 2\";\"row 2, cell 3\"\n"
124 + "\"row 3, cell 1\";\"row 3, cell 2\";\"row 3, cell 3\"");
125 RowReaderImpl lineReader = new RowReaderImpl(reader);
126 List<Row> rows = lineReader.readRows();
127 assertEquals(3, rows.size());
128 }
129
130
131
132
133 @Test
134 void nonContentBeforeLines() {
135 Reader reader = new StringReader("# line 1\n" + "# line 2\n" + "# line 3\n" + "alpha;beta;gamma\n"
136 + "\"row 1, cell 1\";\"row 1, cell 2\";\"row 1, cell 3\"\n"
137 + "\"row 2, cell 1\";\"row 2, cell 2\";\"row 2, cell 3\"\n");
138 RowReaderImpl lineReader = new RowReaderImpl(reader,
139 new RowInstructionsImpl().setStartRow(4).setUseHeader(true));
140 List<Row> rows = lineReader.readRows();
141 assertEquals(2, rows.size());
142 }
143
144
145
146
147 @Test
148 void roughRide() {
149 Reader reader = new StringReader(
150 "\"alpha\";\"\";;\"beta\";gamma;\"een \"\"echte\"\" test\";\"1\n2\n3\n\"\"regels\"\"\"");
151 RowReaderImpl lineReader = new RowReaderImpl(reader);
152 Line cells = lineReader.readBareLine();
153 assertEquals(7, cells.size());
154 assertEquals("alpha", cells.get(0));
155 assertEquals("", cells.get(1));
156 assertEquals("", cells.get(2));
157 assertEquals("beta", cells.get(3));
158 assertEquals("gamma", cells.get(4));
159 assertEquals("een \"echte\" test", cells.get(5));
160 assertEquals("1\n2\n3\n\"regels\"", cells.get(6));
161 }
162
163
164
165
166 @Test
167 void doubleQuotesAsEscape() {
168 Reader reader = new StringReader(
169 "\"\"\"very literal\"\"\";\"a\"\"b\"\"c\"\n\"abc\";\"first this, \"\"then that\"\"\"");
170 RowReaderImpl lineReader = new RowReaderImpl(reader, new RowInstructionsImpl().setUseHeader(false));
171 checkEscapedStrings(lineReader.readRows());
172 }
173
174
175
176
177 @Test
178 void backSlashesAsEscape() {
179 Reader reader = new StringReader(
180 "\"\\\"very literal\\\"\";\"a\\\"b\\\"c\"\n\"abc\";\"first this, \\\"then that\\\"\"");
181 RowReaderImpl lineReader = new RowReaderImpl(reader,
182 new RowInstructionsImpl().setUseHeader(false).setEscape('\\'));
183 checkEscapedStrings(lineReader.readRows());
184 }
185
186
187
188
189
190
191
192 private void checkEscapedStrings(List<Row> lines) {
193 Row row = lines.get(0);
194 assertEquals("\"very literal\"", row.get(1));
195 assertEquals("a\"b\"c", row.get(2));
196 row = lines.get(1);
197 assertEquals("abc", row.get(1));
198 assertEquals("first this, \"then that\"", row.get(2));
199 }
200
201
202
203
204 @Test
205 void readAllLines() {
206 Reader reader = new StringReader(";;;\n;;;\n;;;\n");
207 RowReaderImpl lineReader = new RowReaderImpl(reader, new RowInstructionsImpl().setUseHeader(false));
208 List<Row> allLines = lineReader.readRows();
209 assertEquals(3, allLines.size());
210 }
211
212
213
214
215 @Test
216 void allNumbers() {
217 Reader reader = new StringReader("17.51;23.19;-100.23;");
218 RowReaderImpl lineReader = new RowReaderImpl(reader);
219 Line row = lineReader.readBareLine();
220 assertEquals(4, row.size());
221 assertEquals("17.51", row.get(0));
222 assertEquals("23.19", row.get(1));
223 assertEquals("-100.23", row.get(2));
224 assertEquals("", row.get(3));
225 }
226
227
228
229
230 @Test
231 void spacesBeforeAndAfter() {
232 Reader reader = new StringReader(" \"alpha\" ; \"beta\" ; \"gamma\" ");
233 RowReaderImpl lineReader = new RowReaderImpl(reader);
234 Line row = lineReader.readBareLine();
235 assertEquals(3, row.size());
236 assertEquals("alpha", row.get(0));
237 assertEquals("beta", row.get(1));
238 assertEquals("gamma", row.get(2));
239 }
240
241
242
243
244 @Test
245 void spaceWithoutQuotesFields() {
246 Reader reader = new StringReader(" alpha one ; beta ; gamma ");
247 RowReaderImpl lineReader = new RowReaderImpl(reader);
248 Line row = lineReader.readBareLine();
249 assertEquals(3, row.size());
250 assertEquals("alpha one", row.get(0));
251 assertEquals("beta", row.get(1));
252 assertEquals("gamma", row.get(2));
253 }
254
255
256
257
258 @Test
259 void reportSimple() {
260 Reader reader = new StringReader("17.51;23.19;-100.23");
261 RowReaderImpl lineReader = new RowReaderImpl(reader);
262 Line row = lineReader.readBareLine();
263 RowReport report = row.reportOnColumn(new Column(3));
264 assertEquals("17.51;23.19;-100.23[EOF]", report.getPrintableLines().get(0));
265 assertEquals(" ^-----^ ", report.getPrintableLines().get(1));
266 }
267
268
269
270
271 @Test
272 void reportEscapingAndQuotes() {
273 Reader reader = new StringReader(
274 "\"alpha\";\"\";;\"b\te\tt\ta\";gamma;\"een \"\"echte\"\" test\";\"1\n2\n3\n\"\"regels\"\"\"");
275 RowReaderImpl lineReader = new RowReaderImpl(reader);
276 Line row = lineReader.readBareLine();
277 RowReport report = row.reportOnColumn(new Column(4));
278 assertEquals(
279 "\"alpha\";\"\";;\"b\\te\\tt\\ta\";gamma;\"een \"\"echte\"\" test\";\"1\\n2\\n3\\n\"\"regels\"\"\"[EOF]",
280 report.getPrintableLines().get(0));
281 assertEquals(" ^---------^ ",
282 report.getPrintableLines().get(1));
283 report = row.reportOnColumn(new Column(3));
284 assertEquals(" ^ ",
285 report.getPrintableLines().get(1));
286 }
287
288
289
290
291 @Test
292 void readHeader() {
293 Reader reader = new StringReader("alpha;beta;gamma");
294 RowReader rowReader = new RowReaderImpl(reader);
295 Header header = rowReader.readHeader();
296 assertNotNull(header);
297 assertEquals(3, header.size());
298 }
299
300
301
302
303 @Test
304 void readHeaderSecondLine() {
305 Reader reader = new StringReader("alpha;beta;gamma\nalpha2;beta2");
306 RowReader rowReader = new RowReaderImpl(reader, new RowInstructionsImpl().setStartRow(2));
307 Header header = rowReader.readHeader();
308 assertNotNull(header);
309 assertEquals(2, header.size());
310 }
311
312
313
314
315 @Test
316 void readHeaderWithoutUseHeader() {
317 Reader reader = new StringReader("alpha;beta;gamma");
318 RowReader rowReader = new RowReaderImpl(reader, new RowInstructionsImpl().setUseHeader(false));
319 Header header = rowReader.readHeader();
320 assertNotNull(header);
321 assertEquals(3, header.size());
322 }
323
324 }