View Javadoc
1   /*
2    * CSVeed (https://github.com/42BV/CSVeed)
3    *
4    * Copyright 2013-2023 CSVeed.
5    *
6    * All rights reserved. This program and the accompanying materials
7    * are made available under the terms of The Apache Software License,
8    * Version 2.0 which accompanies this distribution, and is available at
9    * https://www.apache.org/licenses/LICENSE-2.0.txt
10   */
11  package org.csveed.bean.conversion;
12  
13  import static org.junit.jupiter.api.Assertions.assertEquals;
14  import static org.junit.jupiter.api.Assertions.assertTrue;
15  
16  import java.math.BigDecimal;
17  import java.math.BigInteger;
18  import java.nio.charset.Charset;
19  import java.util.Arrays;
20  import java.util.Currency;
21  import java.util.TimeZone;
22  import java.util.regex.Pattern;
23  
24  import org.csveed.bean.BeanInstructions;
25  import org.csveed.bean.BeanParser;
26  import org.csveed.bean.BeanProperties;
27  import org.csveed.common.Column;
28  import org.junit.jupiter.api.BeforeEach;
29  import org.junit.jupiter.api.Test;
30  
31  /**
32   * The Class BeanWrapperTest.
33   */
34  class BeanWrapperTest {
35  
36      /** The properties. */
37      private BeanProperties properties = deriveProperties();
38  
39      /** The bean. */
40      private Bean bean;
41  
42      /** The default converters. */
43      private DefaultConverters defaultConverters = new DefaultConverters();
44  
45      /** The bean wrapper. */
46      private BeanWrapper beanWrapper;
47  
48      /**
49       * Inits the.
50       */
51      @BeforeEach
52      void init() {
53          bean = new Bean();
54          beanWrapper = new BeanWrapper(defaultConverters, bean);
55      }
56  
57      /**
58       * Hit all properties.
59       *
60       * @throws Exception
61       *             the exception
62       */
63      @Test
64      void hitAllProperties() throws Exception {
65          beanWrapper.setProperty(properties.fromName(new Column("charset")), null);
66          beanWrapper.setProperty(properties.fromName(new Column("charset")), "");
67          beanWrapper.setProperty(properties.fromName(new Column("chars")), null);
68          beanWrapper.setProperty(properties.fromName(new Column("bytes")), null);
69          beanWrapper.setProperty(properties.fromName(new Column("booleanObject")), "");
70          beanWrapper.setProperty(properties.fromName(new Column("byteObject")), "");
71          beanWrapper.setProperty(properties.fromName(new Column("shortObject")), "");
72          beanWrapper.setProperty(properties.fromName(new Column("intObject")), "");
73          beanWrapper.setProperty(properties.fromName(new Column("longObject")), "");
74          beanWrapper.setProperty(properties.fromName(new Column("floatObject")), "");
75          beanWrapper.setProperty(properties.fromName(new Column("doubleObject")), "");
76          beanWrapper.setProperty(properties.fromName(new Column("bigDecimal")), "");
77          beanWrapper.setProperty(properties.fromName(new Column("bigInteger")), "");
78      }
79  
80      /**
81       * Gets the charset.
82       *
83       * @throws Exception
84       *             the exception
85       */
86      @Test
87      void getCharset() throws Exception {
88          bean.setCharset(Charset.forName("US-ASCII"));
89          assertEquals("US-ASCII", beanWrapper.getProperty(properties.fromName(new Column("charset"))));
90      }
91  
92      /**
93       * Sets the charset.
94       *
95       * @throws Exception
96       *             the exception
97       */
98      @Test
99      void setCharset() throws Exception {
100         beanWrapper.setProperty(properties.fromName(new Column("charset")), "US-ASCII");
101         assertEquals("US-ASCII", bean.getCharset().displayName());
102     }
103 
104     /**
105      * Gets the currency.
106      *
107      * @throws Exception
108      *             the exception
109      */
110     @Test
111     void getCurrency() throws Exception {
112         bean.setCurrency(Currency.getInstance("USD"));
113         assertEquals("USD", beanWrapper.getProperty(properties.fromName(new Column("currency"))));
114     }
115 
116     /**
117      * Sets the currency.
118      *
119      * @throws Exception
120      *             the exception
121      */
122     @Test
123     void setCurrency() throws Exception {
124         beanWrapper.setProperty(properties.fromName(new Column("currency")), "USD");
125         assertEquals("USD", bean.getCurrency().getCurrencyCode());
126     }
127 
128     /**
129      * Gets the pattern.
130      *
131      * @throws Exception
132      *             the exception
133      */
134     @Test
135     void getPattern() throws Exception {
136         bean.setPattern(Pattern.compile("[0-9]"));
137         assertEquals("[0-9]", beanWrapper.getProperty(properties.fromName(new Column("pattern"))));
138     }
139 
140     /**
141      * Sets the pattern.
142      *
143      * @throws Exception
144      *             the exception
145      */
146     @Test
147     void setPattern() throws Exception {
148         beanWrapper.setProperty(properties.fromName(new Column("pattern")), "[0-9]");
149         assertEquals("[0-9]", bean.getPattern().pattern());
150     }
151 
152     /**
153      * Gets the time zone.
154      *
155      * @throws Exception
156      *             the exception
157      */
158     @Test
159     void getTimeZone() throws Exception {
160         bean.setTimeZone(TimeZone.getTimeZone("GMT-8"));
161         assertEquals("GMT-08:00", beanWrapper.getProperty(properties.fromName(new Column("timeZone"))));
162     }
163 
164     /**
165      * Sets the time zone.
166      *
167      * @throws Exception
168      *             the exception
169      */
170     @Test
171     void setTimeZone() throws Exception {
172         beanWrapper.setProperty(properties.fromName(new Column("timeZone")), "GMT-8");
173         assertEquals("GMT-08:00", bean.getTimeZone().getDisplayName());
174     }
175 
176     /**
177      * Gets the bytes.
178      *
179      * @throws Exception
180      *             the exception
181      */
182     @Test
183     void getBytes() throws Exception {
184         bean.setBytes(new byte[] { 65, 66, 67 });
185         assertEquals("ABC", beanWrapper.getProperty(properties.fromName(new Column("bytes"))));
186     }
187 
188     /**
189      * Sets the bytes.
190      *
191      * @throws Exception
192      *             the exception
193      */
194     @Test
195     void setBytes() throws Exception {
196         beanWrapper.setProperty(properties.fromName(new Column("bytes")), "ABC");
197         assertTrue(Arrays.equals(new byte[] { 65, 66, 67 }, bean.getBytes()));
198     }
199 
200     /**
201      * Gets the chars.
202      *
203      * @throws Exception
204      *             the exception
205      */
206     @Test
207     void getChars() throws Exception {
208         bean.setChars(new char[] { 'A', 'B', 'C' });
209         assertEquals("ABC", beanWrapper.getProperty(properties.fromName(new Column("chars"))));
210     }
211 
212     /**
213      * Sets the chars.
214      *
215      * @throws Exception
216      *             the exception
217      */
218     @Test
219     void setChars() throws Exception {
220         beanWrapper.setProperty(properties.fromName(new Column("chars")), "ABC");
221         assertTrue(Arrays.equals(new char[] { 'A', 'B', 'C' }, bean.getChars()));
222     }
223 
224     /**
225      * Gets the char primitive.
226      *
227      * @throws Exception
228      *             the exception
229      */
230     @Test
231     void getCharPrimitive() throws Exception {
232         bean.setCharPrimitive('ü');
233         assertEquals("ü", beanWrapper.getProperty(properties.fromName(new Column("charPrimitive"))));
234     }
235 
236     /**
237      * Sets the char primitive.
238      *
239      * @throws Exception
240      *             the exception
241      */
242     @Test
243     void setCharPrimitive() throws Exception {
244         beanWrapper.setProperty(properties.fromName(new Column("charPrimitive")), "ü");
245         assertEquals('ü', bean.getCharPrimitive());
246     }
247 
248     /**
249      * Gets the character.
250      *
251      * @throws Exception
252      *             the exception
253      */
254     @Test
255     void getCharacter() throws Exception {
256         bean.setCharacter('ü');
257         assertEquals("ü", beanWrapper.getProperty(properties.fromName(new Column("character"))));
258     }
259 
260     /**
261      * Sets the character.
262      *
263      * @throws Exception
264      *             the exception
265      */
266     @Test
267     void setCharacter() throws Exception {
268         beanWrapper.setProperty(properties.fromName(new Column("character")), "ü");
269         assertEquals('ü', (char) bean.getCharacter());
270     }
271 
272     /**
273      * Gets the boolean primitive.
274      *
275      * @throws Exception
276      *             the exception
277      */
278     @Test
279     void getBooleanPrimitive() throws Exception {
280         bean.setBooleanPrimitive(true);
281         assertEquals("true", beanWrapper.getProperty(properties.fromName(new Column("booleanPrimitive"))));
282     }
283 
284     /**
285      * Sets the boolean primitive.
286      *
287      * @throws Exception
288      *             the exception
289      */
290     @Test
291     void setBooleanPrimitive() throws Exception {
292         beanWrapper.setProperty(properties.fromName(new Column("booleanPrimitive")), "on");
293         assertEquals(true, bean.isBooleanPrimitive());
294     }
295 
296     /**
297      * Gets the boolean object.
298      *
299      * @throws Exception
300      *             the exception
301      */
302     @Test
303     void getBooleanObject() throws Exception {
304         bean.setBooleanObject(Boolean.TRUE);
305         assertEquals("true", beanWrapper.getProperty(properties.fromName(new Column("booleanObject"))));
306     }
307 
308     /**
309      * Sets the boolean object.
310      *
311      * @throws Exception
312      *             the exception
313      */
314     @Test
315     void setBooleanObject() throws Exception {
316         beanWrapper.setProperty(properties.fromName(new Column("booleanObject")), "on");
317         assertEquals(Boolean.TRUE, bean.getBooleanObject());
318     }
319 
320     /**
321      * Gets the byte primitive.
322      *
323      * @throws Exception
324      *             the exception
325      */
326     @Test
327     void getBytePrimitive() throws Exception {
328         bean.setBytePrimitive((byte) 17);
329         assertEquals("17", beanWrapper.getProperty(properties.fromName(new Column("bytePrimitive"))));
330     }
331 
332     /**
333      * Sets the byte primitive.
334      *
335      * @throws Exception
336      *             the exception
337      */
338     @Test
339     void setBytePrimitive() throws Exception {
340         beanWrapper.setProperty(properties.fromName(new Column("bytePrimitive")), "17");
341         assertEquals(17, bean.getBytePrimitive());
342     }
343 
344     /**
345      * Gets the byte object.
346      *
347      * @throws Exception
348      *             the exception
349      */
350     @Test
351     void getByteObject() throws Exception {
352         bean.setByteObject(Byte.valueOf("17"));
353         assertEquals("17", beanWrapper.getProperty(properties.fromName(new Column("byteObject"))));
354     }
355 
356     /**
357      * Sets the byte object.
358      *
359      * @throws Exception
360      *             the exception
361      */
362     @Test
363     void setByteObject() throws Exception {
364         beanWrapper.setProperty(properties.fromName(new Column("byteObject")), "17");
365         assertEquals(Byte.valueOf("17"), bean.getByteObject());
366     }
367 
368     /**
369      * Gets the short primitive.
370      *
371      * @throws Exception
372      *             the exception
373      */
374     @Test
375     void getShortPrimitive() throws Exception {
376         bean.setShortPrimitive((short) 17);
377         assertEquals("17", beanWrapper.getProperty(properties.fromName(new Column("shortPrimitive"))));
378     }
379 
380     /**
381      * Sets the short primitive.
382      *
383      * @throws Exception
384      *             the exception
385      */
386     @Test
387     void setShortPrimitive() throws Exception {
388         beanWrapper.setProperty(properties.fromName(new Column("shortPrimitive")), "17");
389         assertEquals(17, bean.getShortPrimitive());
390     }
391 
392     /**
393      * Gets the short object.
394      *
395      * @throws Exception
396      *             the exception
397      */
398     @Test
399     void getShortObject() throws Exception {
400         bean.setShortObject(Short.valueOf("17"));
401         assertEquals("17", beanWrapper.getProperty(properties.fromName(new Column("shortObject"))));
402     }
403 
404     /**
405      * Sets the short object.
406      *
407      * @throws Exception
408      *             the exception
409      */
410     @Test
411     void setShortObject() throws Exception {
412         beanWrapper.setProperty(properties.fromName(new Column("shortObject")), "17");
413         assertEquals(Short.valueOf("17"), bean.getShortObject());
414     }
415 
416     /**
417      * Gets the int primitive.
418      *
419      * @throws Exception
420      *             the exception
421      */
422     @Test
423     void getIntPrimitive() throws Exception {
424         bean.setIntPrimitive(989);
425         assertEquals("989", beanWrapper.getProperty(properties.fromName(new Column("intPrimitive"))));
426     }
427 
428     /**
429      * Sets the int primitive.
430      *
431      * @throws Exception
432      *             the exception
433      */
434     @Test
435     void setIntPrimitive() throws Exception {
436         beanWrapper.setProperty(properties.fromName(new Column("intPrimitive")), "1989");
437         assertEquals(1989, bean.getIntPrimitive());
438     }
439 
440     /**
441      * Gets the int object.
442      *
443      * @throws Exception
444      *             the exception
445      */
446     @Test
447     void getIntObject() throws Exception {
448         bean.setIntObject(989);
449         assertEquals("989", beanWrapper.getProperty(properties.fromName(new Column("intObject"))));
450     }
451 
452     /**
453      * Sets the int object.
454      *
455      * @throws Exception
456      *             the exception
457      */
458     @Test
459     void setIntObject() throws Exception {
460         beanWrapper.setProperty(properties.fromName(new Column("intObject")), "1989");
461         assertEquals((Integer) 1989, bean.getIntObject());
462     }
463 
464     /**
465      * Gets the long primitive.
466      *
467      * @throws Exception
468      *             the exception
469      */
470     @Test
471     void getLongPrimitive() throws Exception {
472         bean.setLongPrimitive(989);
473         assertEquals("989", beanWrapper.getProperty(properties.fromName(new Column("longPrimitive"))));
474     }
475 
476     /**
477      * Sets the long primitive.
478      *
479      * @throws Exception
480      *             the exception
481      */
482     @Test
483     void setLongPrimitive() throws Exception {
484         beanWrapper.setProperty(properties.fromName(new Column("longPrimitive")), "1989");
485         assertEquals(1989, bean.getLongPrimitive());
486     }
487 
488     /**
489      * Gets the long object.
490      *
491      * @throws Exception
492      *             the exception
493      */
494     @Test
495     void getLongObject() throws Exception {
496         bean.setLongObject(989L);
497         assertEquals("989", beanWrapper.getProperty(properties.fromName(new Column("longObject"))));
498     }
499 
500     /**
501      * Sets the long object.
502      *
503      * @throws Exception
504      *             the exception
505      */
506     @Test
507     void setLongObject() throws Exception {
508         beanWrapper.setProperty(properties.fromName(new Column("longObject")), "1989");
509         assertEquals((Long) 1989L, bean.getLongObject());
510     }
511 
512     /**
513      * Gets the float primitive.
514      *
515      * @throws Exception
516      *             the exception
517      */
518     @Test
519     void getFloatPrimitive() throws Exception {
520         bean.setFloatPrimitive((float) 42.42);
521         assertEquals("42.42", beanWrapper.getProperty(properties.fromName(new Column("floatPrimitive"))));
522     }
523 
524     /**
525      * Sets the float primitive.
526      *
527      * @throws Exception
528      *             the exception
529      */
530     @Test
531     void setFloatPrimitive() throws Exception {
532         beanWrapper.setProperty(properties.fromName(new Column("floatPrimitive")), "42.42");
533         assertEquals((float) 42.42, bean.getFloatPrimitive(), 1);
534     }
535 
536     /**
537      * Gets the float object.
538      *
539      * @throws Exception
540      *             the exception
541      */
542     @Test
543     void getFloatObject() throws Exception {
544         bean.setFloatObject(Float.valueOf("42.42"));
545         assertEquals("42.42", beanWrapper.getProperty(properties.fromName(new Column("floatObject"))));
546     }
547 
548     /**
549      * Sets the float object.
550      *
551      * @throws Exception
552      *             the exception
553      */
554     @Test
555     void setFloatObject() throws Exception {
556         beanWrapper.setProperty(properties.fromName(new Column("floatObject")), "42.42");
557         assertEquals(Float.valueOf("42.42"), bean.getFloatObject());
558     }
559 
560     /**
561      * Gets the double primitive.
562      *
563      * @throws Exception
564      *             the exception
565      */
566     @Test
567     void getDoublePrimitive() throws Exception {
568         bean.setDoublePrimitive(42.42);
569         assertEquals("42.42", beanWrapper.getProperty(properties.fromName(new Column("doublePrimitive"))));
570     }
571 
572     /**
573      * Sets the double primitive.
574      *
575      * @throws Exception
576      *             the exception
577      */
578     @Test
579     void setDoublePrimitive() throws Exception {
580         beanWrapper.setProperty(properties.fromName(new Column("doublePrimitive")), "42.42");
581         assertEquals(42.42, bean.getDoublePrimitive(), 1);
582     }
583 
584     /**
585      * Gets the double object.
586      *
587      * @throws Exception
588      *             the exception
589      */
590     @Test
591     void getDoubleObject() throws Exception {
592         bean.setDoubleObject(Double.valueOf("42.42"));
593         assertEquals("42.42", beanWrapper.getProperty(properties.fromName(new Column("doubleObject"))));
594     }
595 
596     /**
597      * Sets the double object.
598      *
599      * @throws Exception
600      *             the exception
601      */
602     @Test
603     void setDoubleObject() throws Exception {
604         beanWrapper.setProperty(properties.fromName(new Column("doubleObject")), "42.42");
605         assertEquals(Double.valueOf("42.42"), bean.getDoubleObject());
606     }
607 
608     /**
609      * Gets the big decimal.
610      *
611      * @throws Exception
612      *             the exception
613      */
614     @Test
615     void getBigDecimal() throws Exception {
616         bean.setBigDecimal(new BigDecimal("42.123"));
617         assertEquals("42.123", beanWrapper.getProperty(properties.fromName(new Column("bigDecimal"))));
618     }
619 
620     /**
621      * Sets the big decimal.
622      *
623      * @throws Exception
624      *             the exception
625      */
626     @Test
627     void setBigDecimal() throws Exception {
628         beanWrapper.setProperty(properties.fromName(new Column("bigDecimal")), "42.12345678901234567890");
629         assertEquals(new BigDecimal("42.12345678901234567890"), bean.getBigDecimal());
630     }
631 
632     /**
633      * Gets the big integer.
634      *
635      * @throws Exception
636      *             the exception
637      */
638     @Test
639     void getBigInteger() throws Exception {
640         bean.setBigInteger(new BigInteger("4212345678901234567890"));
641         assertEquals("4212345678901234567890", beanWrapper.getProperty(properties.fromName(new Column("bigInteger"))));
642     }
643 
644     /**
645      * Sets the big integer.
646      *
647      * @throws Exception
648      *             the exception
649      */
650     @Test
651     void setBigInteger() throws Exception {
652         beanWrapper.setProperty(properties.fromName(new Column("bigInteger")), "4212345678901234567890");
653         assertEquals(new BigInteger("4212345678901234567890"), bean.getBigInteger());
654     }
655 
656     /**
657      * Derive properties.
658      *
659      * @return the bean properties
660      */
661     protected BeanProperties deriveProperties() {
662         BeanInstructions instructions = new BeanParser().getBeanInstructions(Bean.class);
663         return instructions.getProperties();
664     }
665 }