View Javadoc
1   package org.csveed.bean.conversion;
2   
3   import static org.junit.jupiter.api.Assertions.assertEquals;
4   import static org.junit.jupiter.api.Assertions.assertTrue;
5   
6   import java.math.BigDecimal;
7   import java.math.BigInteger;
8   import java.nio.charset.Charset;
9   import java.util.Arrays;
10  import java.util.Currency;
11  import java.util.TimeZone;
12  import java.util.regex.Pattern;
13  
14  import org.csveed.bean.BeanInstructions;
15  import org.csveed.bean.BeanParser;
16  import org.csveed.bean.BeanProperties;
17  import org.csveed.common.Column;
18  import org.junit.jupiter.api.BeforeEach;
19  import org.junit.jupiter.api.Test;
20  
21  public class BeanWrapperTest {
22  
23      private BeanProperties properties = deriveProperties();
24  
25      private Bean bean;
26  
27      private DefaultConverters defaultConverters = new DefaultConverters();
28  
29      private BeanWrapper beanWrapper;
30  
31      @BeforeEach
32      public void init() {
33          bean = new Bean();
34          beanWrapper = new BeanWrapper(defaultConverters, bean);
35      }
36  
37      @Test
38      public void hitAllProperties() throws Exception {
39          beanWrapper.setProperty(properties.fromName(new Column("charset")),null);
40          beanWrapper.setProperty(properties.fromName(new Column("charset")),"");
41          beanWrapper.setProperty(properties.fromName(new Column("chars")),null);
42          beanWrapper.setProperty(properties.fromName(new Column("bytes")),null);
43          beanWrapper.setProperty(properties.fromName(new Column("booleanObject")),"");
44          beanWrapper.setProperty(properties.fromName(new Column("byteObject")),"");
45          beanWrapper.setProperty(properties.fromName(new Column("shortObject")),"");
46          beanWrapper.setProperty(properties.fromName(new Column("intObject")),"");
47          beanWrapper.setProperty(properties.fromName(new Column("longObject")),"");
48          beanWrapper.setProperty(properties.fromName(new Column("floatObject")),"");
49          beanWrapper.setProperty(properties.fromName(new Column("doubleObject")),"");
50          beanWrapper.setProperty(properties.fromName(new Column("bigDecimal")),"");
51          beanWrapper.setProperty(properties.fromName(new Column("bigInteger")),"");
52      }
53  
54      @Test
55      public void getCharset() throws Exception {
56          bean.setCharset(Charset.forName("US-ASCII"));
57          assertEquals("US-ASCII", beanWrapper.getProperty(properties.fromName(new Column("charset"))));
58      }
59  
60      @Test
61      public void setCharset() throws Exception {
62          beanWrapper.setProperty(properties.fromName(new Column("charset")),"US-ASCII");
63          assertEquals("US-ASCII", bean.getCharset().displayName());
64      }
65  
66      @Test
67      public void getCurrency() throws Exception {
68          bean.setCurrency(Currency.getInstance("USD"));
69          assertEquals("USD", beanWrapper.getProperty(properties.fromName(new Column("currency"))));
70      }
71  
72      @Test
73      public void setCurrency() throws Exception {
74          beanWrapper.setProperty(properties.fromName(new Column("currency")),"USD");
75          assertEquals("USD", bean.getCurrency().getCurrencyCode());
76      }
77  
78      @Test
79      public void getPattern() throws Exception {
80          bean.setPattern(Pattern.compile("[0-9]"));
81          assertEquals("[0-9]", beanWrapper.getProperty(properties.fromName(new Column("pattern"))));
82      }
83  
84      @Test
85      public void setPattern() throws Exception {
86          beanWrapper.setProperty(properties.fromName(new Column("pattern")),"[0-9]");
87          assertEquals("[0-9]", bean.getPattern().pattern());
88      }
89  
90      @Test
91      public void getTimeZone() throws Exception {
92          bean.setTimeZone(TimeZone.getTimeZone("GMT-8"));
93          assertEquals("GMT-08:00", beanWrapper.getProperty(properties.fromName(new Column("timeZone"))));
94      }
95  
96      @Test
97      public void setTimeZone() throws Exception {
98          beanWrapper.setProperty(properties.fromName(new Column("timeZone")),"GMT-8");
99          assertEquals("GMT-08:00", bean.getTimeZone().getDisplayName());
100     }
101 
102     @Test
103     public void getBytes() throws Exception {
104         bean.setBytes(new byte[]{65, 66, 67});
105         assertEquals("ABC", beanWrapper.getProperty(properties.fromName(new Column("bytes"))));
106     }
107 
108     @Test
109     public void setBytes() throws Exception {
110         beanWrapper.setProperty(properties.fromName(new Column("bytes")),"ABC");
111         assertTrue(Arrays.equals(new byte[]{65, 66, 67}, bean.getBytes()));
112     }
113 
114     @Test
115     public void getChars() throws Exception {
116         bean.setChars(new char[]{'A', 'B', 'C'});
117         assertEquals("ABC", beanWrapper.getProperty(properties.fromName(new Column("chars"))));
118     }
119 
120     @Test
121     public void setChars() throws Exception {
122         beanWrapper.setProperty(properties.fromName(new Column("chars")),"ABC");
123         assertTrue(Arrays.equals(new char[] { 'A', 'B', 'C' } , bean.getChars()));
124     }
125 
126     @Test
127     public void getCharPrimitive() throws Exception {
128         bean.setCharPrimitive('ü');
129         assertEquals("ü", beanWrapper.getProperty(properties.fromName(new Column("charPrimitive"))));
130     }
131 
132     @Test
133     public void setCharPrimitive() throws Exception {
134         beanWrapper.setProperty(properties.fromName(new Column("charPrimitive")),"ü");
135         assertEquals('ü', bean.getCharPrimitive());
136     }
137 
138     @Test
139     public void getCharacter() throws Exception {
140         bean.setCharacter('ü');
141         assertEquals("ü", beanWrapper.getProperty(properties.fromName(new Column("character"))));
142     }
143 
144     @Test
145     public void setCharacter() throws Exception {
146         beanWrapper.setProperty(properties.fromName(new Column("character")),"ü");
147         assertEquals('ü', (char)bean.getCharacter());
148     }
149 
150     @Test
151     public void getBooleanPrimitive() throws Exception {
152         bean.setBooleanPrimitive(true);
153         assertEquals("true", beanWrapper.getProperty(properties.fromName(new Column("booleanPrimitive"))));
154     }
155 
156     @Test
157     public void setBooleanPrimitive() throws Exception {
158         beanWrapper.setProperty(properties.fromName(new Column("booleanPrimitive")),"on");
159         assertEquals(true, bean.isBooleanPrimitive());
160     }
161 
162     @Test
163     public void getBooleanObject() throws Exception {
164         bean.setBooleanObject(Boolean.TRUE);
165         assertEquals("true", beanWrapper.getProperty(properties.fromName(new Column("booleanObject"))));
166     }
167 
168     @Test
169     public void setBooleanObject() throws Exception {
170         beanWrapper.setProperty(properties.fromName(new Column("booleanObject")),"on");
171         assertEquals(Boolean.TRUE, bean.getBooleanObject());
172     }
173 
174     @Test
175     public void getBytePrimitive() throws Exception {
176         bean.setBytePrimitive((byte) 17);
177         assertEquals("17", beanWrapper.getProperty(properties.fromName(new Column("bytePrimitive"))));
178     }
179 
180     @Test
181     public void setBytePrimitive() throws Exception {
182         beanWrapper.setProperty(properties.fromName(new Column("bytePrimitive")),"17");
183         assertEquals(17, bean.getBytePrimitive());
184     }
185 
186     @Test
187     public void getByteObject() throws Exception {
188         bean.setByteObject(Byte.valueOf("17"));
189         assertEquals("17", beanWrapper.getProperty(properties.fromName(new Column("byteObject"))));
190     }
191 
192     @Test
193     public void setByteObject() throws Exception {
194         beanWrapper.setProperty(properties.fromName(new Column("byteObject")),"17");
195         assertEquals(Byte.valueOf("17"),bean.getByteObject());
196     }
197 
198     @Test
199     public void getShortPrimitive() throws Exception {
200         bean.setShortPrimitive((short) 17);
201         assertEquals("17", beanWrapper.getProperty(properties.fromName(new Column("shortPrimitive"))));
202     }
203 
204     @Test
205     public void setShortPrimitive() throws Exception {
206         beanWrapper.setProperty(properties.fromName(new Column("shortPrimitive")),"17");
207         assertEquals(17, bean.getShortPrimitive());
208     }
209 
210     @Test
211     public void getShortObject() throws Exception {
212         bean.setShortObject(Short.valueOf("17"));
213         assertEquals("17", beanWrapper.getProperty(properties.fromName(new Column("shortObject"))));
214     }
215 
216     @Test
217     public void setShortObject() throws Exception {
218         beanWrapper.setProperty(properties.fromName(new Column("shortObject")),"17");
219         assertEquals(Short.valueOf("17"),bean.getShortObject());
220     }
221 
222     @Test
223     public void getIntPrimitive() throws Exception {
224         bean.setIntPrimitive(989);
225         assertEquals("989", beanWrapper.getProperty(properties.fromName(new Column("intPrimitive"))));
226     }
227 
228     @Test
229     public void setIntPrimitive() throws Exception {
230         beanWrapper.setProperty(properties.fromName(new Column("intPrimitive")),"1989");
231         assertEquals(1989, bean.getIntPrimitive());
232     }
233 
234     @Test
235     public void getIntObject() throws Exception {
236         bean.setIntObject(989);
237         assertEquals("989", beanWrapper.getProperty(properties.fromName(new Column("intObject"))));
238     }
239 
240     @Test
241     public void setIntObject() throws Exception {
242         beanWrapper.setProperty(properties.fromName(new Column("intObject")),"1989");
243         assertEquals((Integer)1989, bean.getIntObject());
244     }
245 
246     @Test
247     public void getLongPrimitive() throws Exception {
248         bean.setLongPrimitive(989);
249         assertEquals("989", beanWrapper.getProperty(properties.fromName(new Column("longPrimitive"))));
250     }
251 
252     @Test
253     public void setLongPrimitive() throws Exception {
254         beanWrapper.setProperty(properties.fromName(new Column("longPrimitive")),"1989");
255         assertEquals(1989, bean.getLongPrimitive());
256     }
257 
258     @Test
259     public void getLongObject() throws Exception {
260         bean.setLongObject(989L);
261         assertEquals("989", beanWrapper.getProperty(properties.fromName(new Column("longObject"))));
262     }
263 
264     @Test
265     public void setLongObject() throws Exception {
266         beanWrapper.setProperty(properties.fromName(new Column("longObject")),"1989");
267         assertEquals((Long)1989L, bean.getLongObject());
268     }
269 
270     @Test
271     public void getFloatPrimitive() throws Exception {
272         bean.setFloatPrimitive((float)42.42);
273         assertEquals("42.42", beanWrapper.getProperty(properties.fromName(new Column("floatPrimitive"))));
274     }
275 
276     @Test
277     public void setFloatPrimitive() throws Exception {
278         beanWrapper.setProperty(properties.fromName(new Column("floatPrimitive")),"42.42");
279         assertEquals((float)42.42, bean.getFloatPrimitive(), 1);
280     }
281 
282     @Test
283     public void getFloatObject() throws Exception {
284         bean.setFloatObject(Float.valueOf("42.42"));
285         assertEquals("42.42", beanWrapper.getProperty(properties.fromName(new Column("floatObject"))));
286     }
287 
288     @Test
289     public void setFloatObject() throws Exception {
290         beanWrapper.setProperty(properties.fromName(new Column("floatObject")),"42.42");
291         assertEquals(Float.valueOf("42.42"),bean.getFloatObject());
292     }
293 
294     @Test
295     public void getDoublePrimitive() throws Exception {
296         bean.setDoublePrimitive(42.42);
297         assertEquals("42.42", beanWrapper.getProperty(properties.fromName(new Column("doublePrimitive"))));
298     }
299 
300     @Test
301     public void setDoublePrimitive() throws Exception {
302         beanWrapper.setProperty(properties.fromName(new Column("doublePrimitive")),"42.42");
303         assertEquals(42.42, bean.getDoublePrimitive(), 1);
304     }
305 
306     @Test
307     public void getDoubleObject() throws Exception {
308         bean.setDoubleObject(Double.valueOf("42.42"));
309         assertEquals("42.42", beanWrapper.getProperty(properties.fromName(new Column("doubleObject"))));
310     }
311 
312     @Test
313     public void setDoubleObject() throws Exception {
314         beanWrapper.setProperty(properties.fromName(new Column("doubleObject")),"42.42");
315         assertEquals(Double.valueOf("42.42"),bean.getDoubleObject());
316     }
317 
318     @Test
319     public void getBigDecimal() throws Exception {
320         bean.setBigDecimal(new BigDecimal("42.123"));
321         assertEquals("42.123", beanWrapper.getProperty(properties.fromName(new Column("bigDecimal"))));
322     }
323 
324     @Test
325     public void setBigDecimal() throws Exception {
326         beanWrapper.setProperty(properties.fromName(new Column("bigDecimal")),"42.12345678901234567890");
327         assertEquals(new BigDecimal("42.12345678901234567890"),bean.getBigDecimal());
328     }
329 
330     @Test
331     public void getBigInteger() throws Exception {
332         bean.setBigInteger(new BigInteger("4212345678901234567890"));
333         assertEquals("4212345678901234567890", beanWrapper.getProperty(properties.fromName(new Column("bigInteger"))));
334     }
335 
336     @Test
337     public void setBigInteger() throws Exception {
338         beanWrapper.setProperty(properties.fromName(new Column("bigInteger")),"4212345678901234567890");
339         assertEquals(new BigInteger("4212345678901234567890"),bean.getBigInteger());
340     }
341 
342     protected BeanProperties deriveProperties() {
343         BeanInstructions instructions = new BeanParser().getBeanInstructions(Bean.class);
344         return instructions.getProperties();
345     }
346 }