View Javadoc
1   package com.opencsv.bean;
2   
3   import com.opencsv.bean.mocks.profile.*;
4   import com.opencsv.exceptions.CsvBadConverterException;
5   import com.opencsv.exceptions.CsvDataTypeMismatchException;
6   import com.opencsv.exceptions.CsvRequiredFieldEmptyException;
7   import org.apache.commons.collections4.MultiValuedMap;
8   import org.apache.commons.collections4.multimap.ArrayListValuedHashMap;
9   import org.apache.commons.lang3.StringUtils;
10  import org.junit.jupiter.api.AfterEach;
11  import org.junit.jupiter.api.BeforeAll;
12  import org.junit.jupiter.api.BeforeEach;
13  import org.junit.jupiter.api.Test;
14  
15  import java.io.FileNotFoundException;
16  import java.io.FileReader;
17  import java.io.StringWriter;
18  import java.time.LocalDate;
19  import java.util.*;
20  
21  import static org.junit.jupiter.api.Assertions.*;
22  
23  /**
24   * Tests profile functionality.
25   *
26   * @author Andrew Rucker Jones
27   */
28  public class ProfileTest {
29  
30      private static Locale systemLocale;
31  
32      @BeforeAll
33      public static void storeSystemLocale() {
34          systemLocale = Locale.getDefault();
35      }
36  
37      @BeforeEach
38      public void setSystemLocaleToValueNotGerman() {
39          Locale.setDefault(Locale.US);
40      }
41  
42      @AfterEach
43      public void setSystemLocaleBackToDefault() {
44          Locale.setDefault(systemLocale);
45      }
46  
47      /**
48       * Tests that reading with profiles by header name works.
49       * <p>Also incidentally tests:
50       * <ul>
51       *     <li>Use of CsvBindByName with non-default profiles</li>
52       *     <li>Use of CsvCustomBindByName with non-default profiles</li>
53       *     <li>Use of CsvBindAndSplitByName with non-default profiles</li>
54       *     <li>Use of CsvBindAndJoinByName with non-default profiles</li>
55       *     <li>Use of @CsvNumber with non-default profiles</li>
56       *     <li>Use of @CsvDate with non-default profiles</li>
57       *     <li>One explicit profile in the profile list</li>
58       *     <li>Multiple profiles in the profile list</li>
59       *     <li>With the explicit default profile</li>
60       *     <li>Multiple annotations with no enclosing annotation</li>
61       *     <li>Multiple annotations with an enclosing annotation</li>
62       *     <li>Multiple annotations, mixed not enclosed / enclosed</li>
63       *     <li>Request of a non-existent profile name with the standard profile present in annotations</li>
64       *     <li>Request of a non-existent profile name with no standard profile present in annotations</li>
65       *     <li>Ignoring one profile</li>
66       *     <li>Ignoring all profiles with names profiles specified in binding annotations</li>
67       * </ul></p>
68       * @throws FileNotFoundException Never
69       */
70      @Test
71      public void testReadingByName() throws FileNotFoundException {
72          ProfileNameMock b;
73          List<ProfileNameMock> beans;
74          List<Float> floats;
75          Collection<LocalDate> dates;
76  
77          //Default profile (not explicitly stated)
78          beans = new CsvToBeanBuilder<ProfileNameMock>(new FileReader("src/test/resources/testNameProfileDefault.csv"))
79                  .withType(ProfileNameMock.class)
80                  .build()
81                  .parse();
82          assertNotNull(beans);
83          assertEquals(1, beans.size());
84          b = beans.get(0);
85          assertEquals(12, b.getInt1());
86          assertTrue(b.isBool1());
87          floats = b.getFloats();
88          assertNotNull(floats);
89          assertTrue(floats instanceof ArrayList);
90          assertEquals(2, floats.size());
91          assertEquals(1.2f, floats.get(0));
92          assertEquals(2.3f, floats.get(1));
93          dates = b.getDates().values();
94          assertNotNull(dates);
95          assertEquals(2, dates.size());
96          assertTrue(dates.contains(LocalDate.of(1978,1,15)));
97          assertTrue(dates.contains(LocalDate.of(1974,2,27)));
98          assertNull(b.getString1());
99          assertNull(b.getString2());
100 
101         //Default profile (empty string)
102         beans = new CsvToBeanBuilder<ProfileNameMock>(new FileReader("src/test/resources/testNameProfileDefault.csv"))
103                 .withType(ProfileNameMock.class)
104                 .withProfile(StringUtils.EMPTY)
105                 .build()
106                 .parse();
107         assertNotNull(beans);
108         assertEquals(1, beans.size());
109         b = beans.get(0);
110         assertEquals(12, b.getInt1());
111         assertTrue(b.isBool1());
112         floats = b.getFloats();
113         assertNotNull(floats);
114         assertTrue(floats instanceof ArrayList);
115         assertEquals(2, floats.size());
116         assertEquals(1.2f, floats.get(0));
117         assertEquals(2.3f, floats.get(1));
118         dates = b.getDates().values();
119         assertNotNull(dates);
120         assertEquals(2, dates.size());
121         assertTrue(dates.contains(LocalDate.of(1978,1,15)));
122         assertTrue(dates.contains(LocalDate.of(1974,2,27)));
123         assertNull(b.getString1());
124         assertNull(b.getString2());
125 
126         //Default profile (null)
127         beans = new CsvToBeanBuilder<ProfileNameMock>(new FileReader("src/test/resources/testNameProfileDefault.csv"))
128                 .withType(ProfileNameMock.class)
129                 .withProfile(null)
130                 .build()
131                 .parse();
132         assertNotNull(beans);
133         assertEquals(1, beans.size());
134         b = beans.get(0);
135         assertEquals(12, b.getInt1());
136         assertTrue(b.isBool1());
137         floats = b.getFloats();
138         assertNotNull(floats);
139         assertTrue(floats instanceof ArrayList);
140         assertEquals(2, floats.size());
141         assertEquals(1.2f, floats.get(0));
142         assertEquals(2.3f, floats.get(1));
143         dates = b.getDates().values();
144         assertNotNull(dates);
145         assertEquals(2, dates.size());
146         assertTrue(dates.contains(LocalDate.of(1978,1,15)));
147         assertTrue(dates.contains(LocalDate.of(1974,2,27)));
148         assertNull(b.getString1());
149         assertNull(b.getString2());
150 
151         //Profile 1
152         beans = new CsvToBeanBuilder<ProfileNameMock>(new FileReader("src/test/resources/testNameProfile1.csv"))
153                 .withType(ProfileNameMock.class)
154                 .withProfile("profile 1")
155                 .build()
156                 .parse();
157         assertNotNull(beans);
158         assertEquals(1, beans.size());
159         b = beans.get(0);
160         assertEquals(12, b.getInt1());
161         assertTrue(b.isBool1());
162         floats = b.getFloats();
163         assertNotNull(floats);
164         assertTrue(floats instanceof LinkedList);
165         assertEquals(2, floats.size());
166         assertEquals(1.234f, floats.get(0));
167         assertEquals(2.345f, floats.get(1));
168         dates = b.getDates().values();
169         assertNotNull(dates);
170         assertEquals(2, dates.size());
171         assertTrue(dates.contains(LocalDate.of(1978,1,15)));
172         assertTrue(dates.contains(LocalDate.of(1974,2,27)));
173         assertNull(b.getString1());
174         assertNull(b.getString2());
175 
176         //Profile 2
177         beans = new CsvToBeanBuilder<ProfileNameMock>(new FileReader("src/test/resources/testNameProfile2.csv"))
178                 .withType(ProfileNameMock.class)
179                 .withProfile("profile 2")
180                 .build()
181                 .parse();
182         assertNotNull(beans);
183         assertEquals(1, beans.size());
184         b = beans.get(0);
185         assertEquals(12, b.getInt1());
186         assertTrue(b.isBool1());
187         floats = b.getFloats();
188         assertNotNull(floats);
189         assertTrue(floats instanceof Stack);
190         assertEquals(2, floats.size());
191         assertEquals(12.34f, floats.get(0));
192         assertEquals(23.45f, floats.get(1));
193         dates = b.getDates().values();
194         assertNotNull(dates);
195         assertEquals(2, dates.size());
196         assertTrue(dates.contains(LocalDate.of(1978,1,15)));
197         assertTrue(dates.contains(LocalDate.of(1974,2,27)));
198         assertNull(b.getString1());
199         assertNull(b.getString2());
200 
201         //Profile 3
202         beans = new CsvToBeanBuilder<ProfileNameMock>(new FileReader("src/test/resources/testNameProfile3.csv"))
203                 .withType(ProfileNameMock.class)
204                 .withProfile("profile 3")
205                 .build()
206                 .parse();
207         assertNotNull(beans);
208         assertEquals(1, beans.size());
209         b = beans.get(0);
210         assertEquals(12, b.getInt1());
211         assertTrue(b.isBool1());
212         floats = b.getFloats();
213         assertNotNull(floats);
214         assertTrue(floats instanceof ArrayList);
215         assertEquals(2, floats.size());
216         assertEquals(1.2f, floats.get(0));
217         assertEquals(2.3f, floats.get(1));
218         dates = b.getDates().values();
219         assertNotNull(dates);
220         assertEquals(2, dates.size());
221         assertTrue(dates.contains(LocalDate.of(1978,1,15)));
222         assertTrue(dates.contains(LocalDate.of(1974,2,27)));
223         assertEquals("test string", b.getString1());
224         assertNull(b.getString2());
225 
226         //Profile 4
227         beans = new CsvToBeanBuilder<ProfileNameMock>(new FileReader("src/test/resources/testNameProfile4.csv"))
228                 .withType(ProfileNameMock.class)
229                 .withProfile("profile 4")
230                 .build()
231                 .parse();
232         assertNotNull(beans);
233         assertEquals(1, beans.size());
234         b = beans.get(0);
235         assertEquals(12, b.getInt1());
236         assertTrue(b.isBool1());
237         floats = b.getFloats();
238         assertNotNull(floats);
239         assertTrue(floats instanceof ArrayList);
240         assertEquals(2, floats.size());
241         assertEquals(1.2f, floats.get(0));
242         assertEquals(2.3f, floats.get(1));
243         dates = b.getDates().values();
244         assertNotNull(dates);
245         assertEquals(2, dates.size());
246         assertTrue(dates.contains(LocalDate.of(1978,1,15)));
247         assertTrue(dates.contains(LocalDate.of(1974,2,27)));
248         assertNull(b.getString1());
249         assertNull(b.getString2());
250     }
251 
252     /**
253      * Tests that reading with profiles by header name works.
254      * <p>Also incidentally tests:
255      * <ul>
256      *     <li>Use of CsvBindByPosition with non-default profiles</li>
257      *     <li>Use of CsvCustomBindByPosition with non-default profiles</li>
258      *     <li>Use of CsvBindAndSplitByPosition with non-default profiles</li>
259      *     <li>Use of CsvBindAndJoinByPosition with non-default profiles</li>
260      *     <li>Use of @CsvNumber with non-default profiles</li>
261      *     <li>Use of @CsvDate with non-default profiles</li>
262      *     <li>One explicit profile in the profile list</li>
263      *     <li>Multiple profiles in the profile list</li>
264      *     <li>With the explicit default profile</li>
265      *     <li>Multiple annotations with no enclosing annotation</li>
266      *     <li>Multiple annotations with an enclosing annotation</li>
267      *     <li>Multiple annotations, mixed not enclosed / enclosed</li>
268      *     <li>Request of a non-existent profile name with the standard profile present in annotations</li>
269      *     <li>Request of a non-existent profile name with no standard profile present in annotations</li>
270      *     <li>Ignoring one profile</li>
271      *     <li>Ignoring all profiles with names profiles specified in binding annotations</li>
272      * </ul></p>
273      * @throws FileNotFoundException Never
274      */
275     @Test
276     public void testReadingByPosition() throws FileNotFoundException {
277         ProfilePositionMock b;
278         List<ProfilePositionMock> beans;
279         List<Float> floats;
280         Collection<LocalDate> dates;
281 
282         //Default profile (not explicitly stated)
283         beans = new CsvToBeanBuilder<ProfilePositionMock>(new FileReader("src/test/resources/testPositionProfileDefault.csv"))
284                 .withType(ProfilePositionMock.class)
285                 .build()
286                 .parse();
287         assertNotNull(beans);
288         assertEquals(1, beans.size());
289         b = beans.get(0);
290         assertEquals(12, b.getInt1());
291         assertTrue(b.isBool1());
292         floats = b.getFloats();
293         assertNotNull(floats);
294         assertTrue(floats instanceof ArrayList);
295         assertEquals(2, floats.size());
296         assertEquals(1.2f, floats.get(0));
297         assertEquals(2.3f, floats.get(1));
298         dates = b.getDates().values();
299         assertNotNull(dates);
300         assertEquals(2, dates.size());
301         assertTrue(dates.contains(LocalDate.of(1978,1,15)));
302         assertTrue(dates.contains(LocalDate.of(1974,2,27)));
303         assertNull(b.getString1());
304         assertNull(b.getString2());
305 
306         //Default profile (empty string)
307         beans = new CsvToBeanBuilder<ProfilePositionMock>(new FileReader("src/test/resources/testPositionProfileDefault.csv"))
308                 .withType(ProfilePositionMock.class)
309                 .withProfile(StringUtils.EMPTY)
310                 .build()
311                 .parse();
312         assertNotNull(beans);
313         assertEquals(1, beans.size());
314         b = beans.get(0);
315         assertEquals(12, b.getInt1());
316         assertTrue(b.isBool1());
317         floats = b.getFloats();
318         assertNotNull(floats);
319         assertTrue(floats instanceof ArrayList);
320         assertEquals(2, floats.size());
321         assertEquals(1.2f, floats.get(0));
322         assertEquals(2.3f, floats.get(1));
323         dates = b.getDates().values();
324         assertNotNull(dates);
325         assertEquals(2, dates.size());
326         assertTrue(dates.contains(LocalDate.of(1978,1,15)));
327         assertTrue(dates.contains(LocalDate.of(1974,2,27)));
328         assertNull(b.getString1());
329         assertNull(b.getString2());
330 
331         //Default profile (null)
332         beans = new CsvToBeanBuilder<ProfilePositionMock>(new FileReader("src/test/resources/testPositionProfileDefault.csv"))
333                 .withType(ProfilePositionMock.class)
334                 .withProfile(null)
335                 .build()
336                 .parse();
337         assertNotNull(beans);
338         assertEquals(1, beans.size());
339         b = beans.get(0);
340         assertEquals(12, b.getInt1());
341         assertTrue(b.isBool1());
342         floats = b.getFloats();
343         assertNotNull(floats);
344         assertTrue(floats instanceof ArrayList);
345         assertEquals(2, floats.size());
346         assertEquals(1.2f, floats.get(0));
347         assertEquals(2.3f, floats.get(1));
348         dates = b.getDates().values();
349         assertNotNull(dates);
350         assertEquals(2, dates.size());
351         assertTrue(dates.contains(LocalDate.of(1978,1,15)));
352         assertTrue(dates.contains(LocalDate.of(1974,2,27)));
353         assertNull(b.getString1());
354         assertNull(b.getString2());
355 
356         //Profile 1
357         beans = new CsvToBeanBuilder<ProfilePositionMock>(new FileReader("src/test/resources/testPositionProfile1.csv"))
358                 .withType(ProfilePositionMock.class)
359                 .withProfile("profile 1")
360                 .build()
361                 .parse();
362         assertNotNull(beans);
363         assertEquals(1, beans.size());
364         b = beans.get(0);
365         assertEquals(12, b.getInt1());
366         assertTrue(b.isBool1());
367         floats = b.getFloats();
368         assertNotNull(floats);
369         assertTrue(floats instanceof LinkedList);
370         assertEquals(2, floats.size());
371         assertEquals(1.234f, floats.get(0));
372         assertEquals(2.345f, floats.get(1));
373         dates = b.getDates().values();
374         assertNotNull(dates);
375         assertEquals(2, dates.size());
376         assertTrue(dates.contains(LocalDate.of(1978,1,15)));
377         assertTrue(dates.contains(LocalDate.of(1974,2,27)));
378         assertNull(b.getString1());
379         assertNull(b.getString2());
380 
381         //Profile 2
382         beans = new CsvToBeanBuilder<ProfilePositionMock>(new FileReader("src/test/resources/testPositionProfile2.csv"))
383                 .withType(ProfilePositionMock.class)
384                 .withProfile("profile 2")
385                 .build()
386                 .parse();
387         assertNotNull(beans);
388         assertEquals(1, beans.size());
389         b = beans.get(0);
390         assertEquals(12, b.getInt1());
391         assertTrue(b.isBool1());
392         floats = b.getFloats();
393         assertNotNull(floats);
394         assertTrue(floats instanceof Stack);
395         assertEquals(2, floats.size());
396         assertEquals(12.34f, floats.get(0));
397         assertEquals(23.45f, floats.get(1));
398         dates = b.getDates().values();
399         assertNotNull(dates);
400         assertEquals(2, dates.size());
401         assertTrue(dates.contains(LocalDate.of(1978,1,15)));
402         assertTrue(dates.contains(LocalDate.of(1974,2,27)));
403         assertNull(b.getString1());
404         assertNull(b.getString2());
405 
406         //Profile 3
407         beans = new CsvToBeanBuilder<ProfilePositionMock>(new FileReader("src/test/resources/testPositionProfile3.csv"))
408                 .withType(ProfilePositionMock.class)
409                 .withProfile("profile 3")
410                 .build()
411                 .parse();
412         assertNotNull(beans);
413         assertEquals(1, beans.size());
414         b = beans.get(0);
415         assertEquals(12, b.getInt1());
416         assertTrue(b.isBool1());
417         floats = b.getFloats();
418         assertNotNull(floats);
419         assertTrue(floats instanceof ArrayList);
420         assertEquals(2, floats.size());
421         assertEquals(1.2f, floats.get(0));
422         assertEquals(2.3f, floats.get(1));
423         dates = b.getDates().values();
424         assertNotNull(dates);
425         assertEquals(2, dates.size());
426         assertTrue(dates.contains(LocalDate.of(1978,1,15)));
427         assertTrue(dates.contains(LocalDate.of(1974,2,27)));
428         assertEquals("test string", b.getString1());
429         assertNull(b.getString2());
430 
431         //Profile 4
432         beans = new CsvToBeanBuilder<ProfilePositionMock>(new FileReader("src/test/resources/testPositionProfile4.csv"))
433                 .withType(ProfilePositionMock.class)
434                 .withProfile("profile 4")
435                 .build()
436                 .parse();
437         assertNotNull(beans);
438         assertEquals(1, beans.size());
439         b = beans.get(0);
440         assertEquals(12, b.getInt1());
441         assertTrue(b.isBool1());
442         floats = b.getFloats();
443         assertNotNull(floats);
444         assertTrue(floats instanceof ArrayList);
445         assertEquals(2, floats.size());
446         assertEquals(1.2f, floats.get(0));
447         assertEquals(2.3f, floats.get(1));
448         dates = b.getDates().values();
449         assertNotNull(dates);
450         assertEquals(2, dates.size());
451         assertTrue(dates.contains(LocalDate.of(1978,1,15)));
452         assertTrue(dates.contains(LocalDate.of(1974,2,27)));
453         assertNull(b.getString1());
454         assertNull(b.getString2());
455     }
456 
457     /**
458      * Tests that writing with profiles by header name works.
459      * <p>Also incidentally tests:
460      * <ul>
461      *     <li>Use of CsvBindByName with non-default profiles</li>
462      *     <li>Use of CsvCustomBindByName with non-default profiles</li>
463      *     <li>Use of CsvBindAndSplitByName with non-default profiles</li>
464      *     <li>Use of CsvBindAndJoinByName with non-default profiles</li>
465      *     <li>Use of @CsvNumber with non-default profiles</li>
466      *     <li>Use of @CsvDate with non-default profiles</li>
467      *     <li>One explicit profile in the profile list</li>
468      *     <li>Multiple profiles in the profile list</li>
469      *     <li>With the explicit default profile</li>
470      *     <li>Multiple annotations with no enclosing annotation</li>
471      *     <li>Multiple annotations with an enclosing annotation</li>
472      *     <li>Multiple annotations, mixed not enclosed / enclosed</li>
473      *     <li>Request of a non-existent profile name with the standard profile present in annotations</li>
474      *     <li>Request of a non-existent profile name with no standard profile present in annotations</li>
475      *     <li>Ignoring one profile</li>
476      *     <li>Ignoring all profiles with names profiles specified in binding annotations</li>
477      * </ul></p>
478      * @throws CsvDataTypeMismatchException Never
479      * @throws CsvRequiredFieldEmptyException Never
480      */
481     @Test
482     public void testWritingByName() throws CsvDataTypeMismatchException, CsvRequiredFieldEmptyException {
483         StringWriter w;
484         StatefulBeanToCsv<ProfileNameMock> beanToCsv;
485 
486         // Initiate bean
487         ProfileNameMock b = new ProfileNameMock();
488         b.setInt1(12);
489         b.setBool1(true);
490         b.setFloats(Arrays.asList(1.234f, 2.345f));
491         MultiValuedMap<String, LocalDate> mvm = new ArrayListValuedHashMap<>();
492         mvm.put("dates", LocalDate.of(1978,1,15));
493         mvm.put("dates", LocalDate.of(1974,2,27));
494         b.setDates(mvm);
495         b.setString1("test string 1");
496         b.setString2("test string 2");
497 
498         // Default profile (not explicitly stated)
499         w = new StringWriter();
500         beanToCsv = new StatefulBeanToCsvBuilder<ProfileNameMock>(w)
501                 .withApplyQuotesToAll(false)
502                 .build();
503         beanToCsv.write(b);
504         assertEquals("BOOL1,FLOATS,INT1,dates,dates\nvrai,1.23% 2.35%,12,01/15/1978,02/27/1974\n", w.toString());
505 
506         // Default profile (empty string)
507         w = new StringWriter();
508         beanToCsv = new StatefulBeanToCsvBuilder<ProfileNameMock>(w)
509                 .withProfile(StringUtils.EMPTY)
510                 .withApplyQuotesToAll(false)
511                 .build();
512         beanToCsv.write(b);
513         assertEquals("BOOL1,FLOATS,INT1,dates,dates\nvrai,1.23% 2.35%,12,01/15/1978,02/27/1974\n", w.toString());
514 
515         // Default profile (null)
516         w = new StringWriter();
517         beanToCsv = new StatefulBeanToCsvBuilder<ProfileNameMock>(w)
518                 .withProfile(null)
519                 .withApplyQuotesToAll(false)
520                 .build();
521         beanToCsv.write(b);
522         assertEquals("BOOL1,FLOATS,INT1,dates,dates\nvrai,1.23% 2.35%,12,01/15/1978,02/27/1974\n", w.toString());
523 
524         // Profile 1
525         w = new StringWriter();
526         beanToCsv = new StatefulBeanToCsvBuilder<ProfileNameMock>(w)
527                 .withProfile("profile 1")
528                 .withApplyQuotesToAll(false)
529                 .build();
530         beanToCsv.write(b);
531         assertEquals("BOOL1,FLOATS,INT1,dates,dates\nwahr,1.234 2.345,integer: 12,15. January 1978,27. February 1974\n", w.toString());
532 
533         // Profile 2
534         w = new StringWriter();
535         beanToCsv = new StatefulBeanToCsvBuilder<ProfileNameMock>(w)
536                 .withProfile("profile 2")
537                 .withApplyQuotesToAll(false)
538                 .build();
539         beanToCsv.write(b);
540         assertEquals("BOOL1,FLOATS,INT1,dates,dates\nwahr,1.23E0 2.35E0,int 12 value,15. January 1978,27. February 1974\n", w.toString());
541 
542         // Profile 3
543         w = new StringWriter();
544         beanToCsv = new StatefulBeanToCsvBuilder<ProfileNameMock>(w)
545                 .withProfile("profile 3")
546                 .withApplyQuotesToAll(false)
547                 .build();
548         beanToCsv.write(b);
549         assertEquals("BOOL1,FLOATS,INT1,STRING1,dates,dates\nvrai,1.23% 2.35%,12,test string 1,15. January 1978,27. February 1974\n", w.toString());
550 
551         // Profile 4
552         w = new StringWriter();
553         beanToCsv = new StatefulBeanToCsvBuilder<ProfileNameMock>(w)
554                 .withProfile("profile 4")
555                 .withApplyQuotesToAll(false)
556                 .build();
557         beanToCsv.write(b);
558         assertEquals("BOOL1,FLOATS,INT1,dates,dates\nvrai,1.23% 2.35%,12,01/15/1978,02/27/1974\n", w.toString());
559     }
560 
561     /**
562      * Tests that writing with profiles by header name works.
563      * <p>Also incidentally tests:
564      * <ul>
565      *     <li>Use of CsvBindByPosition with non-default profiles</li>
566      *     <li>Use of CsvCustomBindByPosition with non-default profiles</li>
567      *     <li>Use of CsvBindAndSplitByPosition with non-default profiles</li>
568      *     <li>Use of CsvBindAndJoinByPosition with non-default profiles</li>
569      *     <li>Use of @CsvNumber with non-default profiles</li>
570      *     <li>Use of @CsvDate with non-default profiles</li>
571      *     <li>One explicit profile in the profile list</li>
572      *     <li>Multiple profiles in the profile list</li>
573      *     <li>With the explicit default profile</li>
574      *     <li>Multiple annotations with no enclosing annotation</li>
575      *     <li>Multiple annotations with an enclosing annotation</li>
576      *     <li>Multiple annotations, mixed not enclosed / enclosed</li>
577      *     <li>Request of a non-existent profile name with the standard profile present in annotations</li>
578      *     <li>Request of a non-existent profile name with no standard profile present in annotations</li>
579      *     <li>Ignoring one profile</li>
580      *     <li>Ignoring all profiles with names profiles specified in binding annotations</li>
581      * </ul></p>
582      * @throws CsvDataTypeMismatchException Never
583      * @throws CsvRequiredFieldEmptyException Never
584      */
585     @Test
586     public void testWritingByPosition() throws CsvDataTypeMismatchException, CsvRequiredFieldEmptyException {
587         StringWriter w;
588         StatefulBeanToCsv<ProfilePositionMock> beanToCsv;
589 
590         // Initiate bean
591         ProfilePositionMock b = new ProfilePositionMock();
592         b.setInt1(12);
593         b.setBool1(true);
594         b.setFloats(Arrays.asList(1.234f, 2.345f));
595         MultiValuedMap<Integer, LocalDate> mvm = new ArrayListValuedHashMap<>();
596         mvm.put(3, LocalDate.of(1978,1,15));
597         mvm.put(4, LocalDate.of(1974,2,27));
598         b.setDates(mvm);
599         b.setString1("test string 1");
600         b.setString2("test string 2");
601 
602         // Default profile (not explicitly stated)
603         w = new StringWriter();
604         beanToCsv = new StatefulBeanToCsvBuilder<ProfilePositionMock>(w)
605                 .withApplyQuotesToAll(false)
606                 .build();
607         beanToCsv.write(b);
608         assertEquals("12,vrai,1.23% 2.35%,01/15/1978,02/27/1974\n", w.toString());
609 
610         // Default profile (empty string)
611         w = new StringWriter();
612         beanToCsv = new StatefulBeanToCsvBuilder<ProfilePositionMock>(w)
613                 .withProfile(StringUtils.EMPTY)
614                 .withApplyQuotesToAll(false)
615                 .build();
616         beanToCsv.write(b);
617         assertEquals("12,vrai,1.23% 2.35%,01/15/1978,02/27/1974\n", w.toString());
618 
619         // Default profile (null)
620         w = new StringWriter();
621         beanToCsv = new StatefulBeanToCsvBuilder<ProfilePositionMock>(w)
622                 .withProfile(null)
623                 .withApplyQuotesToAll(false)
624                 .build();
625         beanToCsv.write(b);
626         assertEquals("12,vrai,1.23% 2.35%,01/15/1978,02/27/1974\n", w.toString());
627 
628         // Profile 1
629         w = new StringWriter();
630         beanToCsv = new StatefulBeanToCsvBuilder<ProfilePositionMock>(w)
631                 .withProfile("profile 1")
632                 .withApplyQuotesToAll(false)
633                 .build();
634         beanToCsv.write(b);
635         assertEquals("integer: 12,wahr,1.234 2.345,15. January 1978,27. February 1974\n", w.toString());
636 
637         // Profile 2
638         w = new StringWriter();
639         beanToCsv = new StatefulBeanToCsvBuilder<ProfilePositionMock>(w)
640                 .withProfile("profile 2")
641                 .withApplyQuotesToAll(false)
642                 .build();
643         beanToCsv.write(b);
644         assertEquals("int 12 value,wahr,1.23E0 2.35E0,15. January 1978,27. February 1974\n", w.toString());
645 
646         // Profile 3
647         w = new StringWriter();
648         beanToCsv = new StatefulBeanToCsvBuilder<ProfilePositionMock>(w)
649                 .withProfile("profile 3")
650                 .withApplyQuotesToAll(false)
651                 .build();
652         beanToCsv.write(b);
653         assertEquals("12,vrai,1.23% 2.35%,15. January 1978,27. February 1974,test string 1\n", w.toString());
654 
655         // Profile 4
656         w = new StringWriter();
657         beanToCsv = new StatefulBeanToCsvBuilder<ProfilePositionMock>(w)
658                 .withProfile("profile 4")
659                 .withApplyQuotesToAll(false)
660                 .build();
661         beanToCsv.write(b);
662         assertEquals("12,vrai,1.23% 2.35%,01/15/1978,02/27/1974\n", w.toString());
663     }
664 
665     /**
666      * Tests that a profile valid for the binding annotation, but with no
667      * matching {@link CsvNumber} annotation throws an error.
668      *
669      * @throws FileNotFoundException Never
670      */
671     @Test
672     public void testCsvNumberMismatch() throws FileNotFoundException {
673         try {
674             // The file used is irrelevant, because the error is thrown
675             // before the file is read.
676             new CsvToBeanBuilder<ProfileMismatch>(new FileReader("src/test/resources/testNameProfileDefault.csv"))
677                     .withProfile("number")
678                     .withType(ProfileMismatch.class)
679                     .build();
680             fail("Exception should have been thrown.");
681         } catch (CsvBadConverterException e) {
682             assertEquals(CsvNumber.class, e.getConverterClass());
683         }
684     }
685 
686     /**
687      * Tests that a profile valid for the binding annotation, but with no
688      * matching {@link CsvDate} annotation throws an error.
689      *
690      * @throws FileNotFoundException Never
691      */
692     @Test
693     public void testCsvDateMismatch() throws FileNotFoundException {
694         try {
695             // The file used is irrelevant, because the error is thrown
696             // before the file is read.
697             new CsvToBeanBuilder<ProfileMismatch>(new FileReader("src/test/resources/testNameProfileDefault.csv"))
698                     .withProfile("date")
699                     .withType(ProfileMismatch.class)
700                     .build();
701         } catch (CsvBadConverterException e) {
702             assertEquals(CsvDate.class, e.getConverterClass());
703         }
704     }
705 
706     /**
707      * Tests all name-based annotations for exclusion based on profile.
708      * @throws FileNotFoundException Never
709      */
710     @Test
711     public void testNoDefaultsName() throws FileNotFoundException {
712         ProfileNameNoDefault b;
713         List<ProfileNameNoDefault> beans;
714         List<Float> floats;
715 
716         beans = new CsvToBeanBuilder<ProfileNameNoDefault>(new FileReader("src/test/resources/testNameProfile1.csv"))
717                 .withType(ProfileNameNoDefault.class)
718                 .withProfile("profile 1")
719                 .build()
720                 .parse();
721         assertNotNull(beans);
722         assertEquals(1, beans.size());
723         b = beans.get(0);
724         assertEquals(0, b.getInt1());
725         assertFalse(b.isBool1());
726         floats = b.getFloats();
727         assertNull(floats);
728         assertNull(b.getDates());
729     }
730 
731     /**
732      * Tests all position-based annotations for exclusion based on profile.
733      * @throws FileNotFoundException Never
734      */
735     @Test
736     public void testNoDefaultsPosition() throws FileNotFoundException {
737         ProfilePositionNoDefault b;
738         List<ProfilePositionNoDefault> beans;
739         List<Float> floats;
740 
741         beans = new CsvToBeanBuilder<ProfilePositionNoDefault>(new FileReader("src/test/resources/testPositionProfile1.csv"))
742                 .withType(ProfilePositionNoDefault.class)
743                 .withProfile("profile 1")
744                 .build()
745                 .parse();
746         assertNotNull(beans);
747         assertEquals(1, beans.size());
748         b = beans.get(0);
749         assertEquals(0, b.getInt1());
750         assertFalse(b.isBool1());
751         floats = b.getFloats();
752         assertNull(floats);
753         assertNull(b.getDates());
754     }
755 }