1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  package org.ini4j;
17  
18  import org.ini4j.sample.Dwarf;
19  import org.ini4j.sample.DwarfBean;
20  
21  import org.ini4j.test.DwarfsData;
22  import org.ini4j.test.DwarfsData.DwarfData;
23  import org.ini4j.test.Helper;
24  
25  import static org.junit.Assert.assertArrayEquals;
26  import static org.junit.Assert.assertEquals;
27  import static org.junit.Assert.assertNotNull;
28  import static org.junit.Assert.assertNull;
29  import static org.junit.Assert.assertTrue;
30  
31  import org.junit.Test;
32  
33  import java.net.URI;
34  
35  public class BasicOptionMapTest extends Ini4jCase
36  {
37      private static final String FOO = "foo";
38      private static BasicOptionMap _map;
39  
40      static
41      {
42          _map = new BasicOptionMap();
43          _map.putAll(Helper.newDwarfsOpt());
44      }
45  
46      @Test public void test_bug_2817403() throws Exception
47      {
48          OptionMap map = new BasicOptionMap();
49  
50          map.add("player.name", "Joe");
51          map.add("player.greeting", "Hi ${player.name}!");
52          map.add("player.domain", "foo.bar");
53          map.add("player.email", "${player.name}@${player.domain}");
54  
55          
56          assertEquals("Joe", map.fetch("player.name"));
57          assertEquals("Hi Joe!", map.fetch("player.greeting"));
58          assertEquals("foo.bar", map.fetch("player.domain"));
59          assertEquals("Joe@foo.bar", map.fetch("player.email"));
60      }
61  
62      @Test public void testAddPutNullAndString()
63      {
64          OptionMap map = new BasicOptionMap();
65          Object o;
66  
67          
68          o = null;
69          map.add(Dwarf.PROP_AGE, o);
70          assertNull(map.get(Dwarf.PROP_AGE));
71          map.put(Dwarf.PROP_AGE, new Integer(DwarfsData.doc.age));
72          assertNotNull(map.get(Dwarf.PROP_AGE));
73          map.add(Dwarf.PROP_AGE, o, 0);
74          assertNull(map.get(Dwarf.PROP_AGE, 0));
75          map.put(Dwarf.PROP_AGE, new Integer(DwarfsData.doc.age), 0);
76          assertNotNull(map.get(Dwarf.PROP_AGE, 0));
77          map.put(Dwarf.PROP_AGE, o, 0);
78          assertNull(map.get(Dwarf.PROP_AGE, 0));
79          map.remove(Dwarf.PROP_AGE);
80          map.put(Dwarf.PROP_AGE, o);
81          assertNull(map.get(Dwarf.PROP_AGE));
82  
83          
84          map.remove(Dwarf.PROP_AGE);
85          o = String.valueOf(DwarfsData.doc.age);
86          map.add(Dwarf.PROP_AGE, o);
87          assertEquals(o, map.get(Dwarf.PROP_AGE));
88          map.remove(Dwarf.PROP_AGE);
89          map.put(Dwarf.PROP_AGE, o);
90          assertEquals(o, map.get(Dwarf.PROP_AGE));
91          o = String.valueOf(DwarfsData.happy.age);
92          map.add(Dwarf.PROP_AGE, o, 0);
93          assertEquals(new Integer(DwarfsData.happy.age), (Integer) map.get(Dwarf.PROP_AGE, 0, int.class));
94          o = String.valueOf(DwarfsData.doc.age);
95          map.put(Dwarf.PROP_AGE, o, 0);
96          assertEquals(DwarfsData.doc.age, (int) map.get(Dwarf.PROP_AGE, 0, int.class));
97      }
98  
99      @Test public void testFetch()
100     {
101         OptionMap map = new BasicOptionMap();
102 
103         Helper.addDwarf(map, DwarfsData.dopey, false);
104         Helper.addDwarf(map, DwarfsData.bashful);
105         Helper.addDwarf(map, DwarfsData.doc);
106 
107         
108         assertEquals(DwarfsData.dopey.weight, map.fetch(Dwarf.PROP_WEIGHT, double.class), Helper.DELTA);
109         map.add(Dwarf.PROP_HEIGHT, map.get(Dwarf.PROP_HEIGHT));
110         assertEquals(DwarfsData.dopey.height, map.fetch(Dwarf.PROP_HEIGHT, 1, double.class), Helper.DELTA);
111 
112         
113         map.clear();
114         Helper.addDwarf(map, DwarfsData.happy);
115         Helper.addDwarf(map, DwarfsData.sneezy, false);
116         assertEquals(DwarfsData.sneezy.homePage, map.fetch(Dwarf.PROP_HOME_PAGE, URI.class));
117 
118         
119         map = new BasicOptionMap();
120         map.add(Dwarf.PROP_AGE, null);
121         assertNull(map.fetch(Dwarf.PROP_AGE, 0));
122     }
123 
124     @Test public void testFetchAllException()
125     {
126         OptionMap map = new BasicOptionMap();
127 
128         try
129         {
130             map.fetchAll(Dwarf.PROP_FORTUNE_NUMBER, String.class);
131             missing(IllegalArgumentException.class);
132         }
133         catch (IllegalArgumentException x)
134         {
135             
136         }
137     }
138 
139     @Test public void testFromToAs() throws Exception
140     {
141         DwarfBean bean = new DwarfBean();
142 
143         _map.to(bean);
144         Helper.assertEquals(DwarfsData.dopey, bean);
145         OptionMap map = new BasicOptionMap();
146 
147         map.from(bean);
148         bean = new DwarfBean();
149         map.to(bean);
150         Helper.assertEquals(DwarfsData.dopey, bean);
151         Dwarf proxy = map.as(Dwarf.class);
152 
153         Helper.assertEquals(DwarfsData.dopey, proxy);
154         map.clear();
155         _map.to(proxy);
156         Helper.assertEquals(DwarfsData.dopey, proxy);
157     }
158 
159     @Test public void testFromToAsPrefixed() throws Exception
160     {
161         fromToAs(DwarfsData.bashful);
162         fromToAs(DwarfsData.doc);
163         fromToAs(DwarfsData.dopey);
164         fromToAs(DwarfsData.grumpy);
165         fromToAs(DwarfsData.happy);
166         fromToAs(DwarfsData.sleepy);
167         fromToAs(DwarfsData.sneezy);
168     }
169 
170     @Test public void testGet()
171     {
172         OptionMap map = new BasicOptionMap();
173 
174         
175         Helper.addDwarf(map, DwarfsData.bashful, false);
176         assertEquals(DwarfsData.bashful.weight, map.get(Dwarf.PROP_WEIGHT, double.class), Helper.DELTA);
177         map.add(Dwarf.PROP_HEIGHT, map.get(Dwarf.PROP_HEIGHT));
178         assertEquals(DwarfsData.bashful.height, map.get(Dwarf.PROP_HEIGHT, 1, double.class), Helper.DELTA);
179         assertEquals(DwarfsData.bashful.homePage, map.fetch(Dwarf.PROP_HOME_PAGE, URI.class));
180     }
181 
182     @Test public void testGetAllException()
183     {
184         OptionMap map = new BasicOptionMap();
185 
186         try
187         {
188             map.getAll(Dwarf.PROP_FORTUNE_NUMBER, String.class);
189             missing(IllegalArgumentException.class);
190         }
191         catch (IllegalArgumentException x)
192         {
193             
194         }
195     }
196 
197     @Test public void testGetAndFetchDefaultValue()
198     {
199         OptionMap map = new BasicOptionMap();
200 
201         Helper.addDwarf(map, DwarfsData.dopey, false);
202         Helper.addDwarf(map, DwarfsData.bashful);
203         Helper.addDwarf(map, DwarfsData.doc);
204 
205         
206         assertEquals(DwarfsData.dopey.weight, map.fetch(Dwarf.PROP_WEIGHT, double.class), Helper.DELTA);
207         assertEquals(DwarfsData.dopey.weight, map.fetch(Dwarf.PROP_WEIGHT, double.class, 1.2), Helper.DELTA);
208         map.remove(Dwarf.PROP_WEIGHT);
209         assertEquals(1.2, map.fetch(Dwarf.PROP_WEIGHT, double.class, 1.2), Helper.DELTA);
210 
211         
212         assertEquals(DwarfsData.dopey.age, (int) map.get(Dwarf.PROP_AGE, int.class));
213         assertEquals(DwarfsData.dopey.age, (int) map.get(Dwarf.PROP_AGE, int.class, 11));
214         map.remove(Dwarf.PROP_AGE);
215         assertEquals(11, (int) map.get(Dwarf.PROP_AGE, int.class, 11));
216 
217         
218         assertEquals(DwarfsData.dopey.homePage.toString(), map.fetch(Dwarf.PROP_HOME_PAGE, FOO));
219         assertEquals(DwarfsData.dopey.homePage.toString(), map.get(Dwarf.PROP_HOME_PAGE, FOO));
220         map.remove(Dwarf.PROP_HOME_PAGE);
221         assertEquals(FOO, map.fetch(Dwarf.PROP_HOME_PAGE, FOO));
222         assertEquals(FOO, map.get(Dwarf.PROP_HOME_PAGE, FOO));
223     }
224 
225     @Test public void testPropertyFirstUpper()
226     {
227         DwarfBean bean;
228         OptionMap map = new BasicOptionMap(true);
229 
230         map.from(DwarfsData.bashful);
231         assertTrue(map.containsKey("Age"));
232         assertTrue(map.containsKey("Height"));
233         assertTrue(map.containsKey("Weight"));
234         assertTrue(map.containsKey("HomePage"));
235         assertTrue(map.containsKey("HomeDir"));
236         bean = new DwarfBean();
237         map.to(bean);
238         Helper.assertEquals(DwarfsData.bashful, bean);
239         Helper.assertEquals(DwarfsData.bashful, map.as(Dwarf.class));
240     }
241 
242     @Test public void testPut()
243     {
244         OptionMap map = new BasicOptionMap();
245 
246         map.add(Dwarf.PROP_AGE, new Integer(DwarfsData.sneezy.age));
247         map.put(Dwarf.PROP_HEIGHT, new Double(DwarfsData.sneezy.height));
248         map.add(Dwarf.PROP_HOME_DIR, DwarfsData.sneezy.homeDir);
249         map.add(Dwarf.PROP_WEIGHT, new Double(DwarfsData.sneezy.weight), 0);
250         map.put(Dwarf.PROP_HOME_PAGE, null);
251         map.put(Dwarf.PROP_HOME_PAGE, DwarfsData.sneezy.homePage);
252         map.add(Dwarf.PROP_FORTUNE_NUMBER, new Integer(DwarfsData.sneezy.fortuneNumber[1]));
253         map.add(Dwarf.PROP_FORTUNE_NUMBER, new Integer(DwarfsData.sneezy.fortuneNumber[2]));
254         map.add(Dwarf.PROP_FORTUNE_NUMBER, new Integer(0));
255         map.put(Dwarf.PROP_FORTUNE_NUMBER, new Integer(DwarfsData.sneezy.fortuneNumber[3]), 2);
256         map.add(Dwarf.PROP_FORTUNE_NUMBER, new Integer(DwarfsData.sneezy.fortuneNumber[0]), 0);
257         Helper.assertEquals(DwarfsData.sneezy, map.as(Dwarf.class));
258     }
259 
260     @Test public void testPutAllException()
261     {
262         OptionMap map = new BasicOptionMap();
263 
264         try
265         {
266             map.putAll(Dwarf.PROP_FORTUNE_NUMBER, new Integer(0));
267             missing(IllegalArgumentException.class);
268         }
269         catch (IllegalArgumentException x)
270         {
271             
272         }
273     }
274 
275     @Test public void testPutGetFetchAll()
276     {
277         OptionMap map = new BasicOptionMap();
278 
279         map.putAll(Dwarf.PROP_FORTUNE_NUMBER, DwarfsData.sneezy.fortuneNumber);
280         assertEquals(DwarfsData.sneezy.fortuneNumber.length, map.length(Dwarf.PROP_FORTUNE_NUMBER));
281         assertArrayEquals(DwarfsData.sneezy.fortuneNumber, map.getAll(Dwarf.PROP_FORTUNE_NUMBER, int[].class));
282         assertArrayEquals(DwarfsData.sneezy.fortuneNumber, map.fetchAll(Dwarf.PROP_FORTUNE_NUMBER, int[].class));
283         map.putAll(Dwarf.PROP_FORTUNE_NUMBER, (int[]) null);
284         assertEquals(0, map.length(Dwarf.PROP_FORTUNE_NUMBER));
285         assertEquals(0, map.getAll(Dwarf.PROP_FORTUNE_NUMBER, int[].class).length);
286         assertEquals(0, map.fetchAll(Dwarf.PROP_FORTUNE_NUMBER, int[].class).length);
287     }
288 
289     @Test public void testResolve() throws Exception
290     {
291         StringBuilder buffer;
292         String input;
293 
294         
295         input = "${height}";
296         buffer = new StringBuilder(input);
297 
298         _map.resolve(buffer);
299         assertEquals("" + DwarfsData.dopey.getHeight(), buffer.toString());
300 
301         
302         input = "${@prop/user.home}";
303         buffer = new StringBuilder(input);
304 
305         _map.resolve(buffer);
306         assertEquals(System.getProperty("user.home"), buffer.toString());
307 
308         
309         input = "${@env/PATH}";
310         buffer = new StringBuilder(input);
311         try
312         {
313             _map.resolve(buffer);
314             assertEquals(System.getenv("PATH"), buffer.toString());
315         }
316         catch (Error e)
317         {
318             
319         }
320 
321         
322         input = "${no such name}";
323         buffer = new StringBuilder(input);
324 
325         _map.resolve(buffer);
326         assertEquals(input, buffer.toString());
327 
328         
329         input = "${";
330         buffer = new StringBuilder(input);
331 
332         _map.resolve(buffer);
333         assertEquals(input, buffer.toString());
334 
335         
336         input = "${weight";
337         buffer = new StringBuilder(input);
338 
339         _map.resolve(buffer);
340         assertEquals(input, buffer.toString());
341 
342         
343         input = "jim${}";
344         buffer = new StringBuilder(input);
345 
346         _map.resolve(buffer);
347         assertEquals(input, buffer.toString());
348 
349         
350         input = "${weight}";
351         buffer = new StringBuilder(input);
352 
353         _map.resolve(buffer);
354         assertEquals("" + DwarfsData.dopey.getWeight(), buffer.toString());
355         input = "\\" + input;
356         buffer = new StringBuilder(input);
357 
358         assertEquals(input, buffer.toString());
359     }
360 
361     private void fromToAs(DwarfData dwarf)
362     {
363         String prefix = dwarf.name + '.';
364         DwarfBean bean = new DwarfBean();
365 
366         _map.to(bean, prefix);
367         Helper.assertEquals(dwarf, bean);
368         OptionMap map = new BasicOptionMap();
369 
370         map.from(bean, prefix);
371         bean = new DwarfBean();
372         map.to(bean, prefix);
373         Helper.assertEquals(dwarf, bean);
374         Dwarf proxy = map.as(Dwarf.class, prefix);
375 
376         Helper.assertEquals(dwarf, proxy);
377         map.clear();
378         _map.to(proxy, prefix);
379         Helper.assertEquals(dwarf, proxy);
380     }
381 }