View Javadoc
1   package org.csveed.api;
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   import static org.junit.jupiter.api.Assertions.assertTrue;
7   
8   import java.io.IOException;
9   import java.io.Reader;
10  import java.io.StringReader;
11  import java.io.StringWriter;
12  import java.util.ArrayList;
13  import java.util.List;
14  import java.util.Locale;
15  
16  import org.csveed.bean.BeanInstructionsImpl;
17  import org.csveed.bean.ColumnNameMapper;
18  import org.csveed.report.CsvException;
19  import org.csveed.test.converters.BeanSimpleConverter;
20  import org.csveed.test.model.BeanCustomComments;
21  import org.csveed.test.model.BeanSimple;
22  import org.csveed.test.model.BeanVariousNotAnnotated;
23  import org.csveed.test.model.BeanWithCustomNumber;
24  import org.csveed.test.model.BeanWithMultipleStrings;
25  import org.junit.jupiter.api.Test;
26  
27  public class CsvClientTest {
28  
29      @Test
30      public void writeBeansBasedOnClass() throws IOException {
31          try (StringWriter writer = new StringWriter()) {
32              List<BeanWithMultipleStrings> beans = new ArrayList<>();
33              beans.add(createBean("row 1, cell 3", "row 1, cell 2", "row 1, cell 1"));
34              beans.add(createBean("row 2, cell 3", "row 2, cell 2", "row 2, cell 1"));
35              beans.add(createBean("row 3, cell 3", "row 3, cell 2", "row 3, cell 1"));
36              CsvClient<BeanWithMultipleStrings> client = new CsvClientImpl<>(
37                      writer, BeanWithMultipleStrings.class
38              );
39              client.writeBeans(beans);
40          
41              assertEquals(
42                  "\"gamma\";\"beta\";\"alpha\"\r\n"+
43                  "\"row 1, cell 1\";\"row 1, cell 2\";\"row 1, cell 3\"\r\n"+
44                  "\"row 2, cell 1\";\"row 2, cell 2\";\"row 2, cell 3\"\r\n"+
45                  "\"row 3, cell 1\";\"row 3, cell 2\";\"row 3, cell 3\"\r\n",
46                  writer.getBuffer().toString());
47          }
48      }
49  
50      @Test
51      public void writeBeansBasedOnInstructions() throws IOException {
52          try (StringWriter writer = new StringWriter()) {
53              List<BeanWithMultipleStrings> beans = new ArrayList<>();
54              beans.add(createBean("row 1, cell 3", "row 1, cell 2", "row 1, cell 1"));
55              beans.add(createBean("row 2, cell 3", "row 2, cell 2", "row 2, cell 1"));
56              beans.add(createBean("row 3, cell 3", "row 3, cell 2", "row 3, cell 1"));
57              CsvClient<BeanWithMultipleStrings> client = new CsvClientImpl<>(
58                  writer, new BeanInstructionsImpl(BeanWithMultipleStrings.class)
59                      .mapColumnNameToProperty("alpha", "alpha")
60                      .mapColumnNameToProperty("beta", "beta")
61                      .ignoreProperty("gamma")
62                      );
63              client.writeBeans(beans);
64          
65              assertEquals(
66                  "\"beta\";\"alpha\"\r\n"+
67                  "\"row 1, cell 2\";\"row 1, cell 3\"\r\n"+
68                  "\"row 2, cell 2\";\"row 2, cell 3\"\r\n"+
69                  "\"row 3, cell 2\";\"row 3, cell 3\"\r\n",
70                  writer.getBuffer().toString());
71          }
72      }
73  
74      private BeanWithMultipleStrings createBean(String alpha, String beta, String gamma) {
75          BeanWithMultipleStringshtml#BeanWithMultipleStrings">BeanWithMultipleStrings bean = new BeanWithMultipleStrings();
76          bean.setAlpha(alpha);
77          bean.setBeta(beta);
78          bean.setGamma(gamma);
79          return bean;
80      }
81  
82      @Test
83      public void readAndWriteRows() throws IOException {
84          Reader reader = new StringReader(
85                  "alpha;beta;gamma\n"+
86                  "\"row 1, cell 1\";\"row 1, cell 2\";\"row 1, cell 3\"\n"+
87                  "\"row 2, cell 1\";\"row 2, cell 2\";\"row 2, cell 3\"\n"
88          );
89          CsvClient<Reader> csvReader = new CsvClientImpl<>(reader);
90          List<Row> rows = csvReader.readRows();
91  
92          try (StringWriter writer = new StringWriter()) {
93              CsvClient<StringWriter> csvWriter = new CsvClientImpl<>(writer);
94              csvWriter.writeHeader(rows.get(0).getHeader());
95              csvWriter.writeRows(rows);
96  
97              assertEquals(
98                  "\"alpha\";\"beta\";\"gamma\"\r\n"+
99                  "\"row 1, cell 1\";\"row 1, cell 2\";\"row 1, cell 3\"\r\n"+
100                 "\"row 2, cell 1\";\"row 2, cell 2\";\"row 2, cell 3\"\r\n",
101                 writer.getBuffer().toString());
102         }
103     }
104 
105     @Test
106     public void writeRow() throws IOException {
107         try (StringWriter writer = new StringWriter()) {
108             CsvClient<StringWriter> csvClient = new CsvClientImpl<StringWriter>(writer)
109                 .setUseHeader(false);
110             csvClient.writeRow(new String[] { "alpha", "beta", "gamma" } );
111             
112             assertEquals("\"alpha\";\"beta\";\"gamma\"\r\n", writer.getBuffer().toString());
113         }
114     }
115 
116     @Test
117     public void writeRowsLF() throws IOException {
118         writeRows("\n");
119     }
120 
121     @Test
122     public void writeRowsCRLF() throws IOException {
123         writeRows("\r\n");
124     }
125 
126     private void writeRows(String lineTerminators) throws IOException {
127         try (StringWriter writer = new StringWriter()) {
128             CsvClient<StringWriter> csvClient = new CsvClientImpl<StringWriter>(writer)
129                 .setUseHeader(false)
130                 .setEndOfLine(lineTerminators.toCharArray());
131             csvClient.writeHeader(new String[] {
132                 "h1", "h2", "h3"
133             } );
134             csvClient.writeRows(new String[][] {
135                 { "l1c1", "l1c2", "l1c3" },
136                 { "l2c1", "l2c2", "l2c3" },
137                 { "l3c1", "l3c2", "l3c3" }
138             } );
139         
140             assertEquals(
141                 "\"h1\";\"h2\";\"h3\"" + lineTerminators +
142                         "\"l1c1\";\"l1c2\";\"l1c3\"" + lineTerminators +
143                         "\"l2c1\";\"l2c2\";\"l2c3\"" + lineTerminators +
144                         "\"l3c1\";\"l3c2\";\"l3c3\"" + lineTerminators,
145                 writer.getBuffer().toString());
146         }
147     }
148 
149     @Test
150     public void windowsCRLF0x0d0x0a() {
151         char[] file = new char[] {
152             'n', 'a', 'm', 'e', 0x0d, 0x0a,
153             'A', 'l', 'p', 'h', 'a', 0x0d, 0x0a,
154             'B', 'e', 't', 'a', 0x0d, 0x0a,
155             'G', 'a', 'm', 'm', 'a'
156         };
157         String fileText = new String(file);
158         Reader reader = new StringReader(fileText);
159         CsvClient<BeanSimple> csvClient = new CsvClientImpl<>(reader, BeanSimple.class);
160         final List<BeanSimple> beans = csvClient.readBeans();
161         assertEquals(3, beans.size());
162     }
163 
164     @Test
165     public void doNotSkipCommentLineMustCauseColumnCheckToFail() {
166         Reader reader = new StringReader(
167                 "name;name 2;name 3\n"+
168                 "# ignore me!\n"
169         );
170         CsvClient<StringWriter> csvClient = new CsvClientImpl<StringWriter>(reader)
171                 .skipCommentLines(false);
172         assertThrows(CsvException.class, () ->  {
173             csvClient.readRows();
174         });
175     }
176 
177     @Test
178     public void customComments() {
179         Reader reader = new StringReader(
180                 "name\n"+
181                 "% ignore me!\n"+
182                 "some name\n"
183         );
184         CsvClient<BeanCustomComments> csvClient = new CsvClientImpl<>(reader, BeanCustomComments.class);
185         List<BeanCustomComments> beans = csvClient.readBeans();
186         assertEquals(1, beans.size());
187     }
188 
189     @Test
190     public void callBeanMethodOnNonBeanReaderFacade() {
191         Reader reader = new StringReader("");
192         CsvClient<StringWriter> csvClient = new CsvClientImpl<>(reader);
193         assertThrows(CsvException.class, () ->  {
194             csvClient.readBean();
195         });
196     }
197 
198     @Test
199     public void customNumberConversion() {
200         Reader reader = new StringReader(
201                 "money\n"+
202                 "11.398,22"
203         );
204         CsvClient<BeanWithCustomNumber> beanReader = new CsvClientImpl<>(reader, BeanWithCustomNumber.class)
205                 .setLocalizedNumber("number", Locale.GERMANY);
206         BeanWithCustomNumber bean = beanReader.readBean();
207         assertEquals(Double.valueOf(11398.22), bean.getNumber());
208     }
209 
210     @Test
211     public void readLinesLF() {
212         readLines("\n");
213     }
214 
215     @Test
216     public void readLinesCRLF() {
217         readLines("\r\n");
218     }
219 
220     private void readLines(String lineTerminators) {
221         Reader reader = new StringReader("text,year,number,date,lines,year and month" + lineTerminators + "'a bit of text',1983,42.42,1972-01-13,'line 1',2013-04" + lineTerminators
222                 + "'more text',1984,42.42,1972-01-14,'line 1" + lineTerminators + "line 2',2014-04" + lineTerminators + "# please ignore me"
223                 + lineTerminators + "'and yet more text',1985,42.42,1972-01-15,'line 1" + lineTerminators + "line 2" + lineTerminators
224                 + "line 3',2015-04" + lineTerminators);
225 
226         CsvClient<BeanVariousNotAnnotated> csvClient =
227                 new CsvClientImpl<>(reader, BeanVariousNotAnnotated.class)
228                         .setEscape('\\')
229                         .setQuote('\'')
230                         .setComment('#')
231                         .setEndOfLine(new char[]{'\n'})
232                         .setSeparator(',')
233                         .setStartRow(1)
234                         .setUseHeader(true)
235                         .setMapper(ColumnNameMapper.class)
236                         .ignoreProperty("ignoreMe")
237                         .mapColumnNameToProperty("text", "txt")
238                         .setRequired("txt", true)
239                         .mapColumnNameToProperty("year", "year")
240                         .mapColumnNameToProperty("number", "number")
241                         .mapColumnNameToProperty("date", "date")
242                         .setDate("date", "yyyy-MM-dd")
243                         .mapColumnNameToProperty("year and month", "yearMonth")
244                         .setDate("yearMonth", "yyyy-MM")
245                         .mapColumnNameToProperty("lines", "simple")
246                         .setConverter("simple", new BeanSimpleConverter())
247                 ;
248 
249         List<BeanVariousNotAnnotated> beans = csvClient.readBeans();
250         assertTrue(csvClient.isFinished());
251         assertEquals(6, csvClient.getCurrentLine());
252         assertEquals(3, beans.size());
253     }
254 
255     @Test
256     public void multipleHeaderReads() {
257         Reader reader = new StringReader(
258                 "text;year;number;date;lines;year and month\n"+
259                 "\"a bit of text\";1983;42.42;1972-01-13;\"line 1\";2013-04\n"+
260                 "\"more text\";1984;42.42;1972-01-14;\"line 1\nline 2\";2014-04\n"+
261                 "\"and yet more text\";1985;42.42;1972-01-15;\"line 1\nline 2\nline 3\";2015-04\n"
262         );
263         CsvClient<BeanVariousNotAnnotated> csvClient =
264                 new CsvClientImpl<>(reader, BeanVariousNotAnnotated.class);
265 
266         assertNotNull(csvClient.readHeader());
267         assertNotNull(csvClient.readHeader());
268     }
269 
270     @Test
271     public void requiredField() {
272         Reader reader = new StringReader(
273                 "alpha;beta;gamma\n"+
274                 "\"l1c1\";\"l1c2\";\"l1c3\"\n"+
275                 "\"l2c1\";\"l2c2\";\"l2c3\"\n"+
276                 "\"l3c1\";\"l3c2\";"
277         );
278         CsvClient<BeanWithMultipleStrings> csvClient =
279                 new CsvClientImpl<>(reader, BeanWithMultipleStrings.class)
280                 .setMapper(ColumnNameMapper.class)
281                 .setRequired("gamma", true);
282         assertThrows(CsvException.class, () ->  {
283             csvClient.readBeans();
284         });
285     }
286 
287     @Test
288     public void startAtLaterLine() {
289         Reader reader = new StringReader(
290                 "-- ignore line 1\n"+
291                 "-- ignore line 2\n"+
292                 "-- ignore line 3\n"+
293                 "text;year;number;date;lines;year and month\n"+
294                 "\"a bit of text\";1983;42.42;1972-01-13;\"line 1\";2013-04\n"+
295                 "\"more text\";1984;42.42;1972-01-14;\"line 1\nline 2\";2014-04\n"+
296                 "\"and yet more text\";1985;42.42;1972-01-15;\"line 1\nline 2\nline 3\";2015-04\n"
297         );
298         CsvClient<BeanVariousNotAnnotated> csvClient =
299                 new CsvClientImpl<>(reader, BeanVariousNotAnnotated.class)
300                 .setStartRow(4);
301         List<Row> rows = csvClient.readRows();
302         assertEquals(3, rows.size());
303         assertEquals(8, csvClient.getCurrentLine());
304     }
305 
306     @Test
307     public void commentLinesNotSkipped() {
308         Reader reader = new StringReader(
309             "Issue ID;Submitter\n"+
310             "#1;Bill\n"+
311             "#2;Mary\n"+
312             "#3;Jane\n"+
313             "#4;Will"
314         );
315         CsvClient<BeanSimple> csvClient = new CsvClientImpl<>(reader, BeanSimple.class)
316                 .skipCommentLines(false);
317         List<Row> rows = csvClient.readRows();
318         assertEquals(4, rows.size());
319     }
320 
321     @Test
322     public void headerNotWrittenForOtherwiseEmptyCsv() throws IOException {
323         try (StringWriter writer = new StringWriter()) {
324             new CsvClientImpl<>(
325                 writer, BeanWithMultipleStrings.class
326             );
327         
328             assertEquals("", writer.getBuffer().toString());
329         }
330     }
331 
332     @Test
333     public void writeHeaderBasedOnBeanProperties() throws IOException {
334         try (StringWriter writer = new StringWriter()) {
335             CsvClient<BeanWithMultipleStrings> client = new CsvClientImpl<>(
336                 writer, BeanWithMultipleStrings.class
337                     );
338             client.writeHeader();
339         
340             assertEquals("\"gamma\";\"beta\";\"alpha\"\r\n", writer.getBuffer().toString());
341         }
342     }
343 }