View Javadoc

1   /*
2    * Copyright 2005,2009 Ivan SZKIBA
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.ini4j.spi;
17  
18  import org.ini4j.BasicOptionMapGate;
19  import org.ini4j.Ini4jCase;
20  
21  import org.ini4j.sample.Dwarf;
22  import org.ini4j.sample.DwarfBean;
23  
24  import org.ini4j.test.DwarfsData;
25  import org.ini4j.test.Helper;
26  
27  import static org.junit.Assert.assertArrayEquals;
28  import static org.junit.Assert.assertEquals;
29  import static org.junit.Assert.assertFalse;
30  import static org.junit.Assert.assertNotNull;
31  import static org.junit.Assert.assertNull;
32  import static org.junit.Assert.assertSame;
33  import static org.junit.Assert.fail;
34  
35  import org.junit.Before;
36  import org.junit.Test;
37  
38  import java.io.File;
39  import java.io.IOException;
40  
41  import java.net.URI;
42  import java.net.URL;
43  
44  import java.util.TimeZone;
45  
46  public class BeanToolTest extends Ini4jCase
47  {
48      protected BeanTool instance;
49  
50      @Before @Override public void setUp() throws Exception
51      {
52          super.setUp();
53          instance = BeanTool.getInstance();
54      }
55  
56      @Test public void testInject() throws Exception
57      {
58          testInject(null);
59          testInject("dummy");
60      }
61  
62      @Test public void testInjectIllegalArgument1() throws Exception
63      {
64          TestMap map = new TestMap();
65  
66          try
67          {
68              instance.inject(map.newBeanAccess(), new BadBean());
69              missing(IllegalArgumentException.class);
70          }
71          catch (IllegalArgumentException x)
72          {
73              //
74          }
75      }
76  
77      @Test public void testInjectIllegalArgument2() throws Exception
78      {
79          TestMap map = new TestMap();
80  
81          map.put("name", "bad");
82          try
83          {
84              instance.inject(new BadBean(), map.newBeanAccess());
85              missing(IllegalArgumentException.class);
86          }
87          catch (IllegalArgumentException x)
88          {
89              //
90          }
91      }
92  
93      @SuppressWarnings("empty-statement")
94      @Test public void testParse() throws Exception
95      {
96          String input = "6";
97          int value = 6;
98  
99          assertEquals(value, instance.parse(input, byte.class).byteValue());
100         assertEquals(value, instance.parse(input, short.class).shortValue());
101         assertEquals(value, instance.parse(input, int.class).intValue());
102         assertEquals(value, instance.parse(input, long.class).longValue());
103         assertEquals((float) value, instance.parse(input, float.class).floatValue(), Helper.DELTA);
104         assertEquals((double) value, instance.parse(input, double.class).doubleValue(), Helper.DELTA);
105         assertFalse(instance.parse(input, boolean.class));
106         assertEquals('6', instance.parse(input, char.class).charValue());
107 
108         // parse null mean zero
109         assertEquals(0, instance.parse(null, byte.class).byteValue());
110 
111         // parse to null class mean exception
112         try
113         {
114             instance.parse(input, null);
115             fail();
116         }
117         catch (IllegalArgumentException x)
118         {
119             ;
120         }
121 
122         // invalid primitive value mean exception
123         try
124         {
125             instance.parse("?", int.class);
126             fail();
127         }
128         catch (IllegalArgumentException x)
129         {
130             ;
131         }
132 
133         // standard, but not primitive types
134         assertSame(input, instance.parse(input, String.class));
135         assertEquals(new Character('6'), instance.parse(input, Character.class));
136         assertEquals(new Byte(input), instance.parse(input, Byte.class));
137 
138         // special values
139         input = "http://www.ini4j.org";
140         assertEquals(new URL(input), instance.parse(input, URL.class));
141         assertEquals(new URI(input), instance.parse(input, URI.class));
142         assertEquals(new File(input), instance.parse(input, File.class));
143         input = "Europe/Budapest";
144         assertEquals(input, instance.parse(input, TimeZone.class).getID());
145         input = "java.lang.String";
146         assertEquals(String.class, instance.parse(input, Class.class));
147 
148         // invalid value should throw IllegalArgumentException
149         try
150         {
151             instance.parse("", URL.class);
152         }
153         catch (IllegalArgumentException x)
154         {
155             ;
156         }
157     }
158 
159     @Test public void testSetGet() throws Exception
160     {
161         TestMap map = new TestMap();
162         Dwarf proxy = instance.proxy(Dwarf.class, map.newBeanAccess());
163 
164         assertNull(proxy.getHomeDir());
165         assertFalse(proxy.hasHomePage());
166         assertNull(proxy.getFortuneNumber());
167         proxy.setAge(DwarfsData.sneezy.age);
168         proxy.setHeight(DwarfsData.sneezy.height);
169         proxy.setWeight(DwarfsData.sneezy.weight);
170         proxy.setHomePage(DwarfsData.sneezy.homePage);
171         proxy.setHomeDir(DwarfsData.sneezy.homeDir);
172         proxy.setFortuneNumber(DwarfsData.sneezy.fortuneNumber);
173         Helper.assertEquals(DwarfsData.sneezy, proxy);
174         assertArrayEquals(DwarfsData.sneezy.fortuneNumber, proxy.getFortuneNumber());
175     }
176 
177     @Test public void testSingleton() throws Exception
178     {
179         assertEquals(BeanTool.class, BeanTool.getInstance().getClass());
180     }
181 
182     @Test public void testZero() throws Exception
183     {
184         assertEquals(null, instance.zero(Object.class));
185         assertEquals(0, instance.zero(byte.class).byteValue());
186         assertEquals(0, instance.zero(short.class).shortValue());
187         assertEquals(0, instance.zero(int.class).intValue());
188         assertEquals(0, instance.zero(long.class).longValue());
189         assertEquals(0.0f, instance.zero(float.class).floatValue(), Helper.DELTA);
190         assertEquals(0.0, instance.zero(double.class).doubleValue(), Helper.DELTA);
191         assertNotNull((instance.zero(boolean.class)));
192         assertFalse(instance.zero(boolean.class));
193         assertEquals('\0', instance.zero(char.class).charValue());
194     }
195 
196     protected void testInject(String prefix) throws Exception
197     {
198         String p = (prefix == null) ? "" : prefix;
199         Dwarf bean = new DwarfBean();
200 
201         bean.setAge(23);
202         bean.setHeight(5.3);
203         URI uri = new URI("http://www.ini4j.org");
204 
205         bean.setHomePage(uri);
206         String dir = "/home/happy";
207 
208         bean.setHomeDir(dir);
209         bean.setFortuneNumber(new int[] { 1, 2, 3 });
210         TestMap map = new TestMap();
211 
212         instance.inject(map.newBeanAccess(prefix), bean);
213         assertEquals(6, map.size());
214         assertEquals("23", map.get(p + Dwarf.PROP_AGE));
215         assertEquals("5.3", map.get(p + Dwarf.PROP_HEIGHT));
216         assertEquals(uri.toString(), map.get(p + Dwarf.PROP_HOME_PAGE));
217         assertEquals(dir, map.get(p + Dwarf.PROP_HOME_DIR));
218         assertEquals(3, map.length(p + Dwarf.PROP_FORTUNE_NUMBER));
219         assertEquals("1", map.get(p + Dwarf.PROP_FORTUNE_NUMBER, 0));
220         assertEquals("2", map.get(p + Dwarf.PROP_FORTUNE_NUMBER, 1));
221         assertEquals("3", map.get(p + Dwarf.PROP_FORTUNE_NUMBER, 2));
222         bean.setAge(0);
223         bean.setHeight(0);
224         bean.setHomePage(null);
225         instance.inject(bean, map.newBeanAccess(prefix));
226         assertEquals(23, bean.getAge());
227         assertEquals(5.3, bean.getHeight(), Helper.DELTA);
228         assertEquals(uri, bean.getHomePage());
229         assertEquals(dir, bean.getHomeDir());
230         assertArrayEquals(new int[] { 1, 2, 3 }, bean.getFortuneNumber());
231 
232         //
233         // bean interface
234         //
235         Dwarf proxy = instance.proxy(Dwarf.class, map.newBeanAccess(prefix));
236 
237         assertEquals(23, proxy.getAge());
238         assertEquals(5.3, proxy.getHeight(), Helper.DELTA);
239         assertEquals(uri, proxy.getHomePage());
240         assertEquals(dir, proxy.getHomeDir());
241         assertArrayEquals(new int[] { 1, 2, 3 }, proxy.getFortuneNumber());
242     }
243 
244     static class TestMap extends BasicOptionMapGate
245     {
246         private static final long serialVersionUID = 4818386732025655044L;
247     }
248 
249     private static class BadBean
250     {
251         public String getName() throws IOException
252         {
253             throw new IOException();
254         }
255 
256         public void setName(String value) throws IOException
257         {
258             throw new IOException();
259         }
260     }
261 }