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