001    /*
002     * Copyright 2005,2009 Ivan SZKIBA
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.ini4j.spi;
017    
018    import org.ini4j.BasicOptionMapGate;
019    import org.ini4j.Ini4jCase;
020    
021    import org.ini4j.sample.Dwarf;
022    import org.ini4j.sample.DwarfBean;
023    
024    import org.ini4j.test.DwarfsData;
025    import org.ini4j.test.Helper;
026    
027    import static org.junit.Assert.assertArrayEquals;
028    import static org.junit.Assert.assertEquals;
029    import static org.junit.Assert.assertFalse;
030    import static org.junit.Assert.assertNotNull;
031    import static org.junit.Assert.assertNull;
032    import static org.junit.Assert.assertSame;
033    import static org.junit.Assert.fail;
034    
035    import org.junit.Before;
036    import org.junit.Test;
037    
038    import java.io.File;
039    import java.io.IOException;
040    
041    import java.net.URI;
042    import java.net.URL;
043    
044    import java.util.TimeZone;
045    
046    public class BeanToolTest extends Ini4jCase
047    {
048        protected BeanTool instance;
049    
050        @Before @Override public void setUp() throws Exception
051        {
052            super.setUp();
053            instance = BeanTool.getInstance();
054        }
055    
056        @Test public void testInject() throws Exception
057        {
058            testInject(null);
059            testInject("dummy");
060        }
061    
062        @Test public void testInjectIllegalArgument1() throws Exception
063        {
064            TestMap map = new TestMap();
065    
066            try
067            {
068                instance.inject(map.newBeanAccess(), new BadBean());
069                missing(IllegalArgumentException.class);
070            }
071            catch (IllegalArgumentException x)
072            {
073                //
074            }
075        }
076    
077        @Test public void testInjectIllegalArgument2() throws Exception
078        {
079            TestMap map = new TestMap();
080    
081            map.put("name", "bad");
082            try
083            {
084                instance.inject(new BadBean(), map.newBeanAccess());
085                missing(IllegalArgumentException.class);
086            }
087            catch (IllegalArgumentException x)
088            {
089                //
090            }
091        }
092    
093        @SuppressWarnings("empty-statement")
094        @Test public void testParse() throws Exception
095        {
096            String input = "6";
097            int value = 6;
098    
099            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    }