1
2
3
4
5
6
7
8
9
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
33
34 class BeanWrapperTest {
35
36
37 private BeanProperties properties = deriveProperties();
38
39
40 private Bean bean;
41
42
43 private DefaultConverters defaultConverters = new DefaultConverters();
44
45
46 private BeanWrapper beanWrapper;
47
48
49
50
51 @BeforeEach
52 void init() {
53 bean = new Bean();
54 beanWrapper = new BeanWrapper(defaultConverters, bean);
55 }
56
57
58
59
60
61
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
82
83
84
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
94
95
96
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
106
107
108
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
118
119
120
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
130
131
132
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
142
143
144
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
154
155
156
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
166
167
168
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
178
179
180
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
190
191
192
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
202
203
204
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
214
215
216
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
226
227
228
229
230 @Test
231 void getCharPrimitive() throws Exception {
232 bean.setCharPrimitive('ü');
233 assertEquals("ü", beanWrapper.getProperty(properties.fromName(new Column("charPrimitive"))));
234 }
235
236
237
238
239
240
241
242 @Test
243 void setCharPrimitive() throws Exception {
244 beanWrapper.setProperty(properties.fromName(new Column("charPrimitive")), "ü");
245 assertEquals('ü', bean.getCharPrimitive());
246 }
247
248
249
250
251
252
253
254 @Test
255 void getCharacter() throws Exception {
256 bean.setCharacter('ü');
257 assertEquals("ü", beanWrapper.getProperty(properties.fromName(new Column("character"))));
258 }
259
260
261
262
263
264
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
274
275
276
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
286
287
288
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
298
299
300
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
310
311
312
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
322
323
324
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
334
335
336
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
346
347
348
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
358
359
360
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
370
371
372
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
382
383
384
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
394
395
396
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
406
407
408
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
418
419
420
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
430
431
432
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
442
443
444
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
454
455
456
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
466
467
468
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
478
479
480
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
490
491
492
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
502
503
504
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
514
515
516
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
526
527
528
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
538
539
540
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
550
551
552
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
562
563
564
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
574
575
576
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
586
587
588
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
598
599
600
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
610
611
612
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
622
623
624
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
634
635
636
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
646
647
648
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
658
659
660
661 protected BeanProperties deriveProperties() {
662 BeanInstructions instructions = new BeanParser().getBeanInstructions(Bean.class);
663 return instructions.getProperties();
664 }
665 }