View Javadoc
1   package org.csveed.bean;
2   
3   import static org.junit.jupiter.api.Assertions.assertEquals;
4   import static org.junit.jupiter.api.Assertions.assertNull;
5   import static org.junit.jupiter.api.Assertions.assertThrows;
6   
7   import java.io.Reader;
8   import java.io.StringReader;
9   import java.text.SimpleDateFormat;
10  import java.util.List;
11  
12  import org.csveed.report.CsvException;
13  import org.csveed.test.model.*;
14  import org.csveed.token.ParseState;
15  import org.junit.jupiter.api.Test;
16  
17  public class BeanReaderTest {
18  
19      @Test
20      public void dynamicColumns() {
21          Reader reader = new StringReader(
22                  "commodity;language;14-01;14-02;14-03\n"+
23                  "corn;NL;1;2;3\n"+
24                  "corn;BE;4;5;6\n");
25          BeanReader<BeanCommodity> beanReader = new BeanReaderImpl<>(reader, BeanCommodity.class);
26          List<BeanCommodity> commodities = beanReader.readBeans();
27          assertEquals(6, commodities.size());
28          assertBeanCommodity(commodities.get(0), "corn", "NL", "14-01", 1);
29          assertBeanCommodity(commodities.get(1), "corn", "NL", "14-02", 2);
30          assertBeanCommodity(commodities.get(2), "corn", "NL", "14-03", 3);
31          assertBeanCommodity(commodities.get(3), "corn", "BE", "14-01", 4);
32          assertBeanCommodity(commodities.get(4), "corn", "BE", "14-02", 5);
33          assertBeanCommodity(commodities.get(5), "corn", "BE", "14-03", 6);
34      }
35  
36      protected void assertBeanCommodity(BeanCommodity beanCommodity, String expectedCommodity, String expectedLanguage,
37                                         String expectedDay, int expectedAmount) {
38          assertEquals(expectedCommodity, beanCommodity.getCommodity());
39          assertEquals(expectedLanguage, beanCommodity.getLanguage());
40          assertEquals(expectedDay, beanCommodity.getDay());
41          assertEquals(expectedAmount, beanCommodity.getAmount());
42      }
43  
44      @Test
45      public void enumMayBeNull() {
46          Reader reader = new StringReader(
47                  "name;parseState\n"+
48                  "alpha;\"FIRST_CHAR_INSIDE_QUOTED_FIELD\"\n"+
49                  "beta;\n");
50          BeanReader<BeanWithEnumAndMore> beanReader = new BeanReaderImpl<>(reader, BeanWithEnumAndMore.class);
51          List<BeanWithEnumAndMore> beans = beanReader.readBeans();
52          assertEquals(2, beans.size());
53          assertEquals(null, beans.get(1).getParseState());
54      }
55  
56      @Test
57      public void convertToEnum() {
58          Reader reader = new StringReader(
59                  "parseState\n"+
60                  "\"FIRST_CHAR_INSIDE_QUOTED_FIELD\"");
61          BeanReader<BeanWithEnum> beanReader = new BeanReaderImpl<>(reader, BeanWithEnum.class);
62          BeanWithEnum bean = beanReader.readBean();
63          assertEquals(ParseState.FIRST_CHAR_INSIDE_QUOTED_FIELD, bean.getParseState());
64      }
65  
66      @Test
67      public void missingConverter() {
68          Reader reader = new StringReader(
69              "alpha\n"+
70              "\"row 1, cell 1\""
71          );
72          BeanReader<BeanWithNonStandardObject> beanReader = new BeanReaderImpl<>(reader, BeanWithNonStandardObject.class);
73          assertThrows(CsvException.class, () ->  {
74              beanReader.readBean();
75          });
76      }
77  
78      @Test
79      public void illegalColumnIndexMappingTooLow() {
80          assertThrows(CsvException.class, () ->  {
81              new BeanInstructionsImpl(BeanWithMultipleStrings.class)
82                  .setMapper(ColumnIndexMapper.class)
83                  .mapColumnIndexToProperty(-1, "alpha");
84          });
85      }
86  
87      @Test
88      public void illegalColumnIndexMappingTooHigh() {
89          checkIllegalMapping(
90              new BeanInstructionsImpl(BeanWithMultipleStrings.class)
91                  .setMapper(ColumnIndexMapper.class)
92                  .mapColumnIndexToProperty(99, "alpha")
93          );
94      }
95  
96      @Test
97      public void illegalColumnName() {
98          checkIllegalMapping(
99              new BeanInstructionsImpl(BeanWithMultipleStrings.class)
100                 .setMapper(ColumnNameMapper.class)
101                 .mapColumnNameToProperty("Alphabetical", "alpha")
102         );
103     }
104 
105     protected void checkIllegalMapping(BeanInstructions beanInstructions) {
106         Reader reader = new StringReader(
107             "alpha;beta;gamma\n"+
108             "\"row 1, cell 1\";\"row 1, cell 2\";\"row 1, cell 3\""
109         );
110         BeanReader<BeanWithMultipleStrings> beanReader = new BeanReaderImpl<>(reader, beanInstructions);
111         assertThrows(CsvException.class, () ->  {
112             beanReader.readBean();
113         });
114     }
115 
116     @Test
117     public void customNumberConversion() {
118         Reader reader = new StringReader(
119             "money\n"+
120             "11.398,22"
121         );
122         BeanReader<BeanWithCustomNumberAnnotated> beanReader = new BeanReaderImpl<>(reader, BeanWithCustomNumberAnnotated.class);
123         BeanWithCustomNumberAnnotated bean = beanReader.readBean();
124         assertEquals(Double.valueOf(11398.22), bean.getNumber());
125     }
126 
127     @Test
128     public void getBeansManualMapping() {
129         Reader reader = new StringReader(
130             "a;c;b\n"+
131             "\"row 1, cell 1\";\"row 1, cell 2\";\"row 1, cell 3\"\n"+
132             "\"row 2, cell 1\";\"row 2, cell 2\";\"row 2, cell 3\"\n"+
133             "\"row 3, cell 1\";\"row 3, cell 2\";\"row 3, cell 3\""
134         );
135         BeanReader<BeanWithMultipleStrings> beanReader = new BeanReaderImpl<>(
136                 reader,
137                 new BeanInstructionsImpl(BeanWithMultipleStrings.class)
138                 .setMapper(ColumnNameMapper.class)
139                 .mapColumnNameToProperty("a", "alpha")
140                 .mapColumnNameToProperty("b", "beta")
141                 .mapColumnNameToProperty("c", "gamma")
142         );
143         List<BeanWithMultipleStrings> beans = beanReader.readBeans();
144         assertEquals(3, beans.size());
145         BeanWithMultipleStrings bean = beans.get(0);
146         assertEquals("row 1, cell 1", bean.getAlpha());
147         assertEquals("row 1, cell 2", bean.getGamma());
148         assertEquals("row 1, cell 3", bean.getBeta());
149     }
150 
151     @Test
152     public void getBeans() {
153         Reader reader = new StringReader(
154             "alpha;beta;gamma\n"+
155             "\"row 1, cell 1\";\"row 1, cell 2\";\"row 1, cell 3\"\n"+
156             "\"row 2, cell 1\";\"row 2, cell 2\";\"row 2, cell 3\"\n"+
157             "\"row 3, cell 1\";\"row 3, cell 2\";\"row 3, cell 3\""
158         );
159         BeanReader<BeanWithMultipleStrings> beanReader =
160                 new BeanReaderImpl<>(reader, BeanWithMultipleStrings.class);
161         List<BeanWithMultipleStrings> beans = beanReader.readBeans();
162         assertEquals(3, beans.size());
163         BeanWithMultipleStrings bean = beans.get(0);
164         assertEquals("row 1, cell 1", bean.getGamma());
165         assertEquals("row 1, cell 2", bean.getBeta());
166         assertEquals("row 1, cell 3", bean.getAlpha());
167     }
168 
169     @Test
170     public void tabSeparated() {
171         Reader reader = new StringReader(
172             "alpha\tbeta\tgamma\r"+
173             "'\\'row\\' 1, cell 1'\t'row 1, cell 2'\t'row 1, cell 3'\r"+
174             "'\\'row\\' 2, cell 1'\t'row 2, cell 2'\t'row 2, cell 3'\r"+
175             "'\\'row\\' 3, cell 1'\t'row 3, cell 2'\t'row 3, cell 3'"
176         );
177         BeanReader<BeanWithAlienSettings> beanReader =
178                 new BeanReaderImpl<>(reader, BeanWithAlienSettings.class);
179         List<BeanWithAlienSettings> beans = beanReader.readBeans();
180         assertEquals(3, beans.size());
181         BeanWithAlienSettings bean = beans.get(0);
182         assertEquals("'row' 1, cell 1", bean.getGamma());
183         assertEquals("row 1, cell 2", bean.getBeta());
184         assertEquals("row 1, cell 3", bean.getAlpha());
185     }
186 
187     @Test
188     public void errorInDate() {
189         Reader reader = new StringReader(
190                 "text;year;number;date;year and month\n"+
191                 "\"a bit of text\";1984;42.42;1972-13-01;2013-04\n" // Month and day in reverse order
192         );
193         BeanReader<BeanWithVariousTypes> beanReader =
194                 new BeanReaderImpl<>(reader, BeanWithVariousTypes.class);
195         assertThrows(CsvException.class, () ->  {
196             beanReader.readBeans();
197         });
198     }
199 
200     @Test
201     public void variousDataTypes() {
202         Reader reader = new StringReader(
203             "text;year;number;date;year and month\n"+
204             "\"a bit of text\";1984;42.42;1972-01-13;2013-04\n"
205         );
206         BeanReader<BeanWithVariousTypes> beanReader =
207                 new BeanReaderImpl<>(reader, BeanWithVariousTypes.class);
208         List<BeanWithVariousTypes> beans = beanReader.readBeans();
209         assertEquals(1, beans.size());
210         BeanWithVariousTypes bean = beans.get(0);
211         assertEquals("a bit of text", bean.getText());
212         assertEquals((Integer)1984, bean.getYear());
213         assertEquals(Double.valueOf(42.42), bean.getNumber());
214         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
215         assertEquals("1972-01-13", formatter.format(bean.getDate()));
216         formatter = new SimpleDateFormat("yyyy-MM");
217         assertEquals("2013-04", formatter.format(bean.getYearMonth()));
218     }
219 
220     @Test
221     public void noHeader() {
222         Reader reader = new StringReader(
223             "\"a bit of text\";1984;42.42;1972-01-13;2013-04\n"
224         );
225         BeanReader<BeanWithoutHeader> beanReader =
226                 new BeanReaderImpl<>(reader, BeanWithoutHeader.class);
227         List<BeanWithoutHeader> beans = beanReader.readBeans();
228         assertEquals(1, beans.size());
229     }
230     
231     @Test
232     public void nameMatching() {
233         Reader reader = new StringReader(
234             "street;CITY;postal code;ignore this\n"+
235             "\"Some street\";\"Some city\";\"Some postal code\";\"Some ignoring\""
236         );
237         BeanReader<BeanWithNameMatching> beanReader = new BeanReaderImpl<>(reader, BeanWithNameMatching.class);
238         List<BeanWithNameMatching> beans = beanReader.readBeans();
239         assertEquals(1, beans.size());
240         BeanWithNameMatching bean = beans.get(0);
241         assertEquals("Some street", bean.getLine1());
242         assertEquals("Some city", bean.getLine2());
243         assertEquals("Some postal code", bean.getLine3());
244     }
245 
246     @Test
247     public void indexMatching() {
248         Reader reader = new StringReader(
249             "\"line-1\";\"line0\";\"line1\";\"line2\";\"line3\""
250         );
251         BeanReader<BeanWithCustomIndexes> beanReader = new BeanReaderImpl<>(reader, BeanWithCustomIndexes.class);
252         BeanWithCustomIndexes bean = beanReader.readBean();
253         assertEquals("line0", bean.getLine0());
254         assertEquals("line1", bean.getLine1());
255         assertEquals("line2", bean.getLine2());
256         assertEquals("line3", bean.getLine3());
257     }
258     
259     @Test
260     public void numberOfIgnores() {
261         Reader reader = new StringReader(
262             "14;28;42"
263         );
264         BeanReader<BeanLotsOfIgnores> beanReader = new BeanReaderImpl<>(reader, BeanLotsOfIgnores.class);
265         BeanLotsOfIgnores bean = beanReader.readBean();
266         assertEquals((Integer)14, bean.getTakeThis1());
267         assertEquals((Integer)28, bean.getPickThis1());
268         assertEquals((Integer)42, bean.getChooseThis1());
269         assertNull(bean.getDitchThat1());
270         assertNull(bean.getLeaveThat1());
271     }
272 
273     @Test
274     public void customPropertyEditor() {
275         Reader reader = new StringReader(
276             "\"some text\""
277         );
278         BeanReader<BeanWithConverter> beanReader = new BeanReaderImpl<>(reader, BeanWithConverter.class);
279         BeanWithConverter bean = beanReader.readBean();
280         assertEquals("some text", bean.getBean().getName());
281     }
282 
283     @Test
284     public void illegalToken() {
285         Reader reader = new StringReader(
286             "\"alpha\";\"beta\";\"gamma\"a\n"
287         );
288         BeanReader<BeanSimple> beanReader = new BeanReaderImpl<>(reader, BeanSimple.class);
289         assertThrows(CsvException.class, () ->  {
290             beanReader.readBeans();
291         });
292     }
293 
294     @Test
295     public void beanMappingError() {
296         Reader reader = new StringReader(
297             "text;year;number;date;year and month\n"+
298             "\"a bit of text\";UNEXPECTED TEXT!!!;42.42;1972-01-13;2013-04\n"
299         );
300         BeanReader<BeanWithVariousTypes> beanReader = new BeanReaderImpl<>(reader, BeanWithVariousTypes.class);
301         assertThrows(CsvException.class, () ->  {
302             beanReader.readBeans();
303         });
304     }
305 
306     @Test
307     public void cannotConvertToNonStandardObject() {
308         Reader reader = new StringReader(
309             "\"can I convert this to a simple bean?\""
310         );
311         BeanReader<BeanWithNonStandardObject> beanReader = new BeanReaderImpl<>(reader, BeanWithNonStandardObject.class);
312         assertThrows(CsvException.class, () ->  {
313             beanReader.readBeans();
314         });
315     }
316 
317     @Test
318     public void nonInstantiableBean() {
319         Reader reader = new StringReader(
320             "\"can I convert this to a simple bean?\""
321         );
322         BeanReader<BeanWithoutNoArgPublicConstructor> beanReader = new BeanReaderImpl<>(reader, BeanWithoutNoArgPublicConstructor.class);
323         assertThrows(CsvException.class, () ->  {
324             beanReader.readBeans();
325         });
326     }
327 
328 }