1   package org.apache.turbine.util.parser;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.math.BigDecimal;
23  import java.util.Calendar;
24  import java.util.Locale;
25  
26  import junit.framework.TestSuite;
27  
28  import org.apache.turbine.TurbineConstants;
29  import org.apache.turbine.test.BaseTurbineTest;
30  import org.apache.turbine.util.DateSelector;
31  import org.apache.turbine.util.TimeSelector;
32  
33  /***
34   * test whether the Default parameter parser returns its uploaded file items
35   * in the keySet().
36   *
37   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
38   * @version $Id: BaseValueParserTest.java 644892 2008-04-04 20:35:16Z tv $
39   */
40  
41  public class BaseValueParserTest
42          extends BaseTurbineTest
43  {
44      public BaseValueParserTest(String name)
45              throws Exception
46      {
47          super(name, "conf/test/TurbineResources.properties");
48      }
49  
50      public static TestSuite suite()
51      {
52          return new TestSuite(BaseValueParserTest.class);
53      }
54  
55      public void testSetup()
56      {
57          BaseValueParser vp = new BaseValueParser();
58          assertFalse(vp.isDisposed());
59  
60          assertEquals("Wrong Character Encoding", TurbineConstants.PARAMETER_ENCODING_DEFAULT, vp.getCharacterEncoding());
61      }
62  
63      public void testSetupWithEncoding()
64      {
65          String encoding = "ISO-8859-2";
66  
67          BaseValueParser vp = new BaseValueParser(encoding);
68          assertFalse(vp.isDisposed());
69  
70          assertEquals("Wrong Character Encoding", encoding, vp.getCharacterEncoding());
71      }
72  
73      public void testChangeEncoding()
74      {
75          ValueParser vp = new BaseValueParser();
76  
77          assertEquals("Wrong Character Encoding", TurbineConstants.PARAMETER_ENCODING_DEFAULT, vp.getCharacterEncoding());
78  
79          String encoding = "ISO-8859-2";
80          vp.setCharacterEncoding(encoding);
81  
82          assertEquals("Wrong Character Encoding", encoding, vp.getCharacterEncoding());
83      }
84  
85      public void testClear()
86      {
87          ValueParser vp = new BaseValueParser();
88  
89          assertEquals("Wrong number of keys", 0, vp.keySet().size());
90  
91          vp.add("foo", "bar");
92  
93          assertEquals("Wrong number of keys", 1, vp.keySet().size());
94  
95          vp.clear();
96  
97          assertEquals("Wrong number of keys", 0, vp.keySet().size());
98      }
99  
100     public void testDispose()
101     {
102         BaseValueParser vp = new BaseValueParser();
103 
104         assertEquals("Wrong number of keys", 0, vp.keySet().size());
105 
106         vp.add("foo", "bar");
107 
108         assertEquals("Wrong number of keys", 1, vp.keySet().size());
109 
110         vp.dispose();
111 
112         assertEquals("Wrong number of keys", 0, vp.keySet().size());
113         assertTrue(vp.isDisposed());
114     }
115 
116     public void testKeyArray()
117     {
118         ValueParser vp = new BaseValueParser();
119 
120         assertEquals("Wrong number of keys", 0, vp.keySet().size());
121 
122         vp.add("foo", "bar");
123 
124         assertEquals("Wrong number of keys", 1, vp.keySet().size());
125 
126         vp.add("bar", "foo");
127 
128         assertEquals("Wrong number of keys", 2, vp.keySet().size());
129 
130         vp.add("bar", "baz");
131 
132         assertEquals("Wrong number of keys", 2, vp.keySet().size());
133     }
134 
135     public void testDoubleAdd()
136     {
137         ValueParser vp = new BaseValueParser();
138         vp.setLocale(Locale.US);
139 
140         assertEquals("Wrong number of keys", 0, vp.keySet().size());
141 
142         double testValue = 2.2;
143 
144         vp.add("foo", testValue);
145 
146         assertEquals("Wrong number of keys", 1, vp.keySet().size());
147 
148         assertEquals("Wrong string value", "2.2", vp.getString("foo"));
149         assertEquals("Wrong double value", testValue, vp.getDouble("foo"), 0.001);
150         assertEquals("Wrong Double value", testValue, vp.getDoubleObject("foo").doubleValue(), 0.001);
151 
152         double [] doubles = vp.getDoubles("foo");
153         assertEquals("Wrong Array Size", 1, doubles.length);
154 
155         assertEquals("Wrong double array value", testValue, doubles[0], 0.001);
156 
157         Double [] doubleObjs = vp.getDoubleObjects("foo");
158         assertEquals("Wrong Array Size", 1, doubleObjs.length);
159 
160         assertEquals("Wrong Double array value", testValue, doubleObjs[0].doubleValue(), 0.001);
161         
162         vp.clear();
163         vp.setLocale(Locale.GERMANY);
164         
165         String testDouble = "2,3";
166         vp.add("foo", testDouble);
167         assertEquals("Wrong double value", 2.3, vp.getDouble("foo"), 0.001);
168         
169         vp.add("unparsable2", "1a");
170         Double result = vp.getDoubleObject("unparsable2");
171         assertNull("Double object should be null", result);
172     }
173 
174     public void testIntAdd()
175     {
176         ValueParser vp = new BaseValueParser();
177 
178         assertEquals("Wrong number of keys", 0, vp.keySet().size());
179 
180         int testValue = 123;
181 
182         vp.add("foo", testValue);
183 
184         assertEquals("Wrong number of keys", 1, vp.keySet().size());
185 
186         assertEquals("Wrong string value", "123", vp.getString("foo"));
187         assertEquals("Wrong int value", (int) testValue, vp.getInt("foo"));
188         assertEquals("Wrong Int value", (int) testValue, vp.getIntObject("foo").intValue());
189 
190         int [] ints = vp.getInts("foo");
191         assertEquals("Wrong Array Size", 1, ints.length);
192 
193         assertEquals("Wrong int array value", testValue, ints[0]);
194 
195         Integer [] intObjs = vp.getIntObjects("foo");
196         assertEquals("Wrong Array Size", 1, intObjs.length);
197 
198         assertEquals("Wrong Int array value", testValue, intObjs[0].intValue());
199     }
200 
201     public void testIntegerAdd()
202     {
203         ValueParser vp = new BaseValueParser();
204 
205         assertEquals("Wrong number of keys", 0, vp.keySet().size());
206 
207         Integer testValue = new Integer(123);
208 
209         vp.add("foo", testValue);
210 
211         assertEquals("Wrong number of keys", 1, vp.keySet().size());
212 
213         assertEquals("Wrong string value", "123", vp.getString("foo"));
214         assertEquals("Wrong int value", (int) testValue.intValue(), vp.getInt("foo"));
215         assertEquals("Wrong Int value", (int) testValue.intValue(), vp.getIntObject("foo").intValue());
216 
217         int [] ints = vp.getInts("foo");
218         assertEquals("Wrong Array Size", 1, ints.length);
219 
220         assertEquals("Wrong int array value", testValue.intValue(), ints[0]);
221 
222         Integer [] intObjs = vp.getIntObjects("foo");
223         assertEquals("Wrong Array Size", 1, intObjs.length);
224 
225         assertEquals("Wrong Int array value", testValue.intValue(), intObjs[0].intValue());
226     }
227 
228     public void testLongAdd()
229     {
230         ValueParser vp = new BaseValueParser();
231 
232         assertEquals("Wrong number of keys", 0, vp.keySet().size());
233 
234         long testValue = 9223372036854775807l;
235 
236         vp.add("foo", testValue);
237 
238         assertEquals("Wrong number of keys", 1, vp.keySet().size());
239 
240         assertEquals("Wrong string value", "9223372036854775807", vp.getString("foo"));
241         assertEquals("Wrong long value", (long) testValue, vp.getLong("foo"));
242         assertEquals("Wrong Long value", (long) testValue, vp.getLongObject("foo").longValue());
243 
244         long [] longs = vp.getLongs("foo");
245         assertEquals("Wrong Array Size", 1, longs.length);
246 
247         assertEquals("Wrong long array value", testValue, longs[0]);
248 
249         Long [] longObjs = vp.getLongObjects("foo");
250         assertEquals("Wrong Array Size", 1, longObjs.length);
251 
252         assertEquals("Wrong Long array value", testValue, longObjs[0].longValue());
253     }
254 
255     public void testLongToInt()
256     {
257         ValueParser vp = new BaseValueParser();
258 
259         assertEquals("Wrong number of keys", 0, vp.keySet().size());
260 
261         long testValue = 1234l;
262 
263         vp.add("foo", testValue);
264 
265         assertEquals("Wrong number of keys", 1, vp.keySet().size());
266 
267         assertEquals("Wrong string value", "1234", vp.getString("foo"));
268         assertEquals("Wrong int value", (int) testValue, vp.getInt("foo"));
269         assertEquals("Wrong Int value", (int) testValue, vp.getIntObject("foo").intValue());
270 
271         int [] ints = vp.getInts("foo");
272         assertEquals("Wrong Array Size", 1, ints.length);
273 
274         assertEquals("Wrong int array value", testValue, ints[0]);
275 
276         Integer [] intObjs = vp.getIntObjects("foo");
277         assertEquals("Wrong Array Size", 1, intObjs.length);
278 
279         assertEquals("Wrong Int array value", testValue, intObjs[0].intValue());
280     }
281 
282     public void testIntToLong()
283     {
284         ValueParser vp = new BaseValueParser();
285 
286         assertEquals("Wrong number of keys", 0, vp.keySet().size());
287 
288         int testValue = 123;
289 
290         vp.add("foo", testValue);
291 
292         assertEquals("Wrong number of keys", 1, vp.keySet().size());
293 
294         assertEquals("Wrong string value", "123", vp.getString("foo"));
295         assertEquals("Wrong long value", (long) testValue, vp.getLong("foo"));
296         assertEquals("Wrong Long value", (long) testValue, vp.getLongObject("foo").longValue());
297 
298         long [] longs = vp.getLongs("foo");
299         assertEquals("Wrong Array Size", 1, longs.length);
300 
301         assertEquals("Wrong long array value", testValue, longs[0]);
302 
303         Long [] longObjs = vp.getLongObjects("foo");
304         assertEquals("Wrong Array Size", 1, longObjs.length);
305 
306         assertEquals("Wrong Long array value", testValue, longObjs[0].longValue());
307     }
308 
309     public void testIntToDouble()
310     {
311         ValueParser vp = new BaseValueParser();
312 
313         assertEquals("Wrong number of keys", 0, vp.keySet().size());
314 
315         int testValue = 123;
316 
317         vp.add("foo", testValue);
318 
319         assertEquals("Wrong number of keys", 1, vp.keySet().size());
320 
321         assertEquals("Wrong string value", "123", vp.getString("foo"));
322         assertEquals("Wrong double value", (double) testValue, vp.getDouble("foo"), 0.001);
323         assertEquals("Wrong Double value", (double) testValue, vp.getDoubleObject("foo").doubleValue(), 0.001);
324 
325         double [] doubles = vp.getDoubles("foo");
326         assertEquals("Wrong Array Size", 1, doubles.length);
327 
328         assertEquals("Wrong double array value", testValue, doubles[0], 0.001);
329 
330         Double [] doubleObjs = vp.getDoubleObjects("foo");
331         assertEquals("Wrong Array Size", 1, doubleObjs.length);
332 
333         assertEquals("Wrong Double array value", testValue, doubleObjs[0].doubleValue(), 0.001);
334     }
335 
336     public void testLongToDouble()
337     {
338         ValueParser vp = new BaseValueParser();
339 
340         assertEquals("Wrong number of keys", 0, vp.keySet().size());
341 
342         long testValue = 9223372036854775807l;
343 
344         vp.add("foo", testValue);
345 
346         assertEquals("Wrong number of keys", 1, vp.keySet().size());
347 
348         assertEquals("Wrong string value", "9223372036854775807", vp.getString("foo"));
349         assertEquals("Wrong double value", (double) testValue, vp.getDouble("foo"), 0.001);
350         assertEquals("Wrong Double value", (double) testValue, vp.getDoubleObject("foo").doubleValue(), 0.001);
351 
352         double [] doubles = vp.getDoubles("foo");
353         assertEquals("Wrong Array Size", 1, doubles.length);
354 
355         assertEquals("Wrong double array value", testValue, doubles[0], 0.001);
356 
357         Double [] doubleObjs = vp.getDoubleObjects("foo");
358         assertEquals("Wrong Array Size", 1, doubleObjs.length);
359 
360         assertEquals("Wrong Double array value", testValue, doubleObjs[0].doubleValue(), 0.001);
361     }
362 
363     public void testStringAdd()
364     {
365         ValueParser vp = new BaseValueParser();
366 
367         assertEquals("Wrong number of keys", 0, vp.keySet().size());
368 
369         String testValue = "the quick brown fox";
370 
371         vp.add("foo", testValue);
372 
373         assertEquals("Wrong number of keys", 1, vp.keySet().size());
374 
375         assertEquals("Wrong string value", testValue, vp.getString("foo"));
376 
377         String [] Strings = vp.getStrings("foo");
378         assertEquals("Wrong Array Size", 1, Strings.length);
379 
380         assertEquals("Wrong String array value", testValue, Strings[0]);
381     }
382 
383     public void testStringToInt()
384     {
385         ValueParser vp = new BaseValueParser();
386 
387         assertEquals("Wrong number of keys", 0, vp.keySet().size());
388 
389         String testValue = "123456";
390 
391         vp.add("foo", testValue);
392 
393         assertEquals("Wrong number of keys", 1, vp.keySet().size());
394 
395         assertEquals("Wrong string value", testValue, vp.getString("foo"));
396 
397         assertEquals("Wrong int value", Integer.parseInt(testValue), vp.getInt("foo"));
398         assertEquals("Wrong Int value", Integer.valueOf(testValue).intValue(), vp.getIntObject("foo").intValue());
399 
400         int [] ints = vp.getInts("foo");
401         assertEquals("Wrong Array Size", 1, ints.length);
402 
403         assertEquals("Wrong int array value", Integer.parseInt(testValue), ints[0]);
404 
405         Integer [] intObjs = vp.getIntObjects("foo");
406         assertEquals("Wrong Array Size", 1, intObjs.length);
407 
408         assertEquals("Wrong Int array value", Integer.valueOf(testValue).intValue(), intObjs[0].intValue());
409     }
410 
411     public void testStringToLong()
412     {
413         ValueParser vp = new BaseValueParser();
414 
415         assertEquals("Wrong number of keys", 0, vp.keySet().size());
416 
417         String testValue = "123456";
418 
419         vp.add("foo", testValue);
420 
421         assertEquals("Wrong number of keys", 1, vp.keySet().size());
422 
423         assertEquals("Wrong string value", testValue, vp.getString("foo"));
424 
425         assertEquals("Wrong long value", Long.parseLong(testValue), vp.getLong("foo"));
426         assertEquals("Wrong Long value", Long.valueOf(testValue).longValue(), vp.getLongObject("foo").longValue());
427 
428         long [] longs = vp.getLongs("foo");
429         assertEquals("Wrong Array Size", 1, longs.length);
430 
431         assertEquals("Wrong long array value", Long.parseLong(testValue), longs[0]);
432 
433         Long [] longObjs = vp.getLongObjects("foo");
434         assertEquals("Wrong Array Size", 1, longObjs.length);
435 
436         assertEquals("Wrong Long array value", Long.valueOf(testValue).longValue(), longObjs[0].longValue());
437     }
438 
439     public void testStringArray()
440     {
441         ValueParser vp = new BaseValueParser();
442 
443         assertEquals("Wrong number of keys", 0, vp.keySet().size());
444 
445         String [] testValue = new String [] {
446             "foo", "bar", "baz"
447         };
448 
449         vp.add("foo", testValue);
450 
451         assertEquals("Wrong number of keys", 1, vp.keySet().size());
452 
453         String [] res = vp.getStrings("foo");
454 
455         assertEquals("Wrong number of elements", 3, res.length);
456 
457         for (int i = 0; i < res.length; i++)
458         {
459             assertEquals("Wrong value", res[i], testValue[i]);
460         }
461 
462         assertEquals("Wrong element returned", testValue[0], vp.getString("foo"));
463 
464         vp.add("foo", "xxx");
465 
466         res = vp.getStrings("foo");
467 
468         assertEquals("Wrong number of elements", 4, res.length);
469 
470         for (int i = 0; i < 3; i++)
471         {
472             assertEquals("Wrong value", res[i], testValue[i]);
473         }
474 
475         assertEquals(res[3], "xxx");
476 
477         // should append at the end.
478         assertEquals("Wrong element returned", testValue[0], vp.getString("foo"));
479     }
480 
481     public void testRemove()
482     {
483         ValueParser vp = new BaseValueParser();
484 
485         assertEquals("Wrong number of keys", 0, vp.keySet().size());
486 
487         String testValue = "the quick brown fox";
488 
489         vp.add("foo", testValue);
490 
491         assertEquals("Wrong number of keys", 1, vp.keySet().size());
492 
493         assertEquals("Wrong string value", testValue, vp.getString("foo"));
494 
495         assertNotNull(vp.remove("foo"));
496 
497         assertEquals("Wrong number of keys", 0, vp.keySet().size());
498 
499         assertNull(vp.getString("foo"));
500 
501         // Test non-existing key
502         assertNull(vp.remove("baz"));
503 
504         // Test removing null value
505         assertNull(vp.remove(null));
506     }
507 
508     public void testRemoveArray()
509     {
510         ValueParser vp = new BaseValueParser();
511 
512         assertEquals("Wrong number of keys", 0, vp.keySet().size());
513 
514         String testValue = "the quick brown fox";
515 
516         vp.add("foo", testValue);
517 
518         assertEquals("Wrong number of keys", 1, vp.keySet().size());
519 
520         vp.add("foo", testValue);
521 
522         assertEquals("Wrong number of keys", 1, vp.keySet().size());
523 
524         assertEquals("Wrong string value", testValue, vp.getString("foo"));
525 
526         String [] res = vp.getStrings("foo");
527 
528         assertEquals("Wrong number of elements", 2, res.length);
529 
530         for (int i = 0; i < res.length; i++)
531         {
532             assertEquals("Wrong value", res[i], testValue);
533         }
534 
535         vp.remove("foo");
536 
537         assertEquals("Wrong number of keys", 0, vp.keySet().size());
538 
539         assertNull(vp.getString("foo"));
540     }
541 
542     public void testContainsKey()
543     {
544         ValueParser vp = new BaseValueParser();
545 
546         vp.add("foo", "bar");
547         vp.add("bar", new String [] { "foo", "bar" });
548 
549         assertTrue(vp.containsKey("foo"));
550         assertTrue(vp.containsKey("bar"));
551         assertFalse(vp.containsKey("baz"));
552     }
553 
554     public void testDateSelector()
555     {
556         BaseValueParser vp = new BaseValueParser();
557 
558         assertEquals("Wrong number of keys", 0, vp.keySet().size());
559         assertFalse(vp.containsDateSelectorKeys("foo"));
560 
561         vp.add("foo" + DateSelector.DAY_SUFFIX, "1");
562 
563         assertEquals("Wrong number of keys", 1, vp.keySet().size());
564         assertFalse(vp.containsDateSelectorKeys("foo"));
565 
566         vp.add("foo" + DateSelector.MONTH_SUFFIX, "1");
567 
568         assertEquals("Wrong number of keys", 2, vp.keySet().size());
569         assertFalse(vp.containsDateSelectorKeys("foo"));
570 
571         vp.add("foo" + DateSelector.YEAR_SUFFIX, "2005");
572 
573         assertEquals("Wrong number of keys", 3, vp.keySet().size());
574         assertTrue(vp.containsDateSelectorKeys("foo"));
575     }
576 
577     public void testTimeSelector()
578     {
579         BaseValueParser vp = new BaseValueParser();
580 
581         assertEquals("Wrong number of keys", 0, vp.keySet().size());
582         assertFalse(vp.containsTimeSelectorKeys("foo"));
583 
584         vp.add("foo" + TimeSelector.HOUR_SUFFIX, "22");
585 
586         assertEquals("Wrong number of keys", 1, vp.keySet().size());
587         assertFalse(vp.containsTimeSelectorKeys("foo"));
588 
589         vp.add("foo" + TimeSelector.MINUTE_SUFFIX, "58");
590 
591         assertEquals("Wrong number of keys", 2, vp.keySet().size());
592         assertFalse(vp.containsTimeSelectorKeys("foo"));
593 
594         vp.add("foo" + TimeSelector.SECOND_SUFFIX, "0");
595 
596         assertEquals("Wrong number of keys", 3, vp.keySet().size());
597         assertTrue(vp.containsTimeSelectorKeys("foo"));
598     }
599 
600     public void testDate()
601     {
602         BaseValueParser vp = new BaseValueParser();
603         vp.setLocale(Locale.US);
604 
605         assertEquals("Wrong number of keys", 0, vp.keySet().size());
606 
607         vp.add("foo", "03/21/2008");
608         
609         Calendar cal = Calendar.getInstance(Locale.US);
610         cal.clear();
611         cal.set(2008, 2, 21, 0, 0, 0);
612 
613         assertEquals("Wrong Date value (US)", cal.getTime(), vp.getDate("foo"));
614 
615         vp.clear();
616         vp.setLocale(Locale.GERMANY);
617 
618         vp.add("foo", "21.03.2008");
619         
620         cal = Calendar.getInstance(Locale.GERMANY);
621         cal.clear();
622         cal.set(2008, 2, 21, 0, 0, 0);
623 
624         assertEquals("Wrong Date value (German)", cal.getTime(), vp.getDate("foo"));
625     }
626 
627     public void testBooleanObject()
628     {
629         ValueParser vp = new BaseValueParser();
630 
631         vp.add("t1", "true");
632         vp.add("t2", "yes");
633         vp.add("t3", "on");
634         vp.add("t4", "1");
635         vp.add("t5", 1);
636 
637         vp.add("f1", "false");
638         vp.add("f2", "no");
639         vp.add("f3", "off");
640         vp.add("f4", "0");
641         vp.add("f5", 0);
642 
643         vp.add("e1", "nix");
644         vp.add("e2", "weg");
645         vp.add("e3", 200);
646         vp.add("e4", -2.5);
647 
648         assertEquals("Value is not true", Boolean.TRUE, vp.getBooleanObject("t1"));
649         assertEquals("Value is not true", Boolean.TRUE, vp.getBooleanObject("t2"));
650         assertEquals("Value is not true", Boolean.TRUE, vp.getBooleanObject("t3"));
651         assertEquals("Value is not true", Boolean.TRUE, vp.getBooleanObject("t4"));
652         assertEquals("Value is not true", Boolean.TRUE, vp.getBooleanObject("t5"));
653 
654         assertEquals("Value is not false", Boolean.FALSE, vp.getBooleanObject("f1"));
655         assertEquals("Value is not false", Boolean.FALSE, vp.getBooleanObject("f2"));
656         assertEquals("Value is not false", Boolean.FALSE, vp.getBooleanObject("f3"));
657         assertEquals("Value is not false", Boolean.FALSE, vp.getBooleanObject("f4"));
658         assertEquals("Value is not false", Boolean.FALSE, vp.getBooleanObject("f5"));
659 
660         assertNull(vp.getBooleanObject("e1"));
661         assertNull(vp.getBooleanObject("e2"));
662         assertNull(vp.getBooleanObject("e3"));
663         assertNull(vp.getBooleanObject("e4"));
664 
665         assertNull(vp.getBooleanObject("does-not-exist"));
666     }
667 
668     public void testBoolDefault()
669     {
670         ValueParser vp = new BaseValueParser();
671 
672         vp.add("t1", "true");
673         vp.add("f1", "false");
674 
675         assertTrue(vp.getBoolean("t1"));
676         assertFalse(vp.getBoolean("f1"));
677 
678         assertFalse(vp.getBoolean("does not exist"));
679 
680         assertTrue(vp.getBoolean("t1", false));
681         assertFalse(vp.getBoolean("f1", true));
682 
683         assertFalse(vp.getBoolean("does not exist", false));
684         assertTrue(vp.getBoolean("does not exist", true));
685     }
686 
687     public void testBooleanDefault()
688     {
689         ValueParser vp = new BaseValueParser();
690 
691         vp.add("t1", "true");
692         vp.add("f1", "false");
693 
694         assertEquals("Value is not true",  Boolean.TRUE, vp.getBooleanObject("t1"));
695         assertEquals("Value is not false", Boolean.FALSE, vp.getBooleanObject("f1"));
696 
697         assertNull(vp.getBooleanObject("does not exist"));
698 
699         assertEquals("Value is not true",  Boolean.TRUE, vp.getBooleanObject("t1", Boolean.FALSE));
700         assertEquals("Value is not true",  Boolean.TRUE, vp.getBooleanObject("t1", null));
701         assertEquals("Value is not false", Boolean.FALSE, vp.getBooleanObject("f1", Boolean.TRUE));
702         assertEquals("Value is not false", Boolean.FALSE, vp.getBooleanObject("f1", null));
703 
704         assertNull(vp.getBooleanObject("does not exist", null));
705     }
706 
707     public void testDoubleArray()
708     {
709         ValueParser vp = new BaseValueParser();
710 
711         assertEquals("Wrong number of keys", 0, vp.keySet().size());
712 
713         double [] testValue = {
714             1.0, 2.0, 3.0
715         };
716 
717         for (int i = 0; i < testValue.length; i++)
718         {
719             vp.add("foo", testValue[i]);
720 
721             String [] res = vp.getStrings("foo");
722             assertEquals("Wrong number of elements", res.length, i + 1);
723         }
724 
725         assertEquals("Wrong number of keys", 1, vp.keySet().size());
726 
727         double [] res = vp.getDoubles("foo");
728 
729         assertEquals("Wrong number of elements", 3, res.length);
730 
731         for (int i = 0; i < res.length; i++)
732         {
733             assertEquals("Wrong value", res[i], testValue[i], 0.001);
734         }
735 
736         Double [] resObj = vp.getDoubleObjects("foo");
737 
738         assertEquals("Wrong number of elements", 3, resObj.length);
739 
740         for (int i = 0; i < resObj.length; i++)
741         {
742             assertEquals("Wrong value", resObj[i].doubleValue(), testValue[i], 0.001);
743         }
744 
745         assertEquals("Wrong element returned", testValue[0], vp.getDoubleObject("foo").doubleValue(), 0.001);
746 
747         vp.add("foo", 4.0);
748 
749         res = vp.getDoubles("foo");
750 
751         assertEquals("Wrong number of elements", 4, res.length);
752 
753         for (int i = 0; i < 3; i++)
754         {
755             assertEquals("Wrong value", res[i], testValue[i], 0.001);
756         }
757 
758         assertEquals(res[3], 4.0, 0.001);
759 
760         resObj = vp.getDoubleObjects("foo");
761 
762         assertEquals("Wrong number of elements", 4, resObj.length);
763 
764         for (int i = 0; i < 3; i++)
765         {
766             assertEquals("Wrong value", resObj[i].doubleValue(), testValue[i], 0.001);
767         }
768 
769         assertEquals(resObj[3].doubleValue(), 4.0, 0.001);
770 
771         // should append at the end.
772         assertEquals("Wrong element returned", testValue[0], vp.getDouble("foo"), 0.001);
773     }
774 
775     public void testFloatArray()
776     {
777         ValueParser vp = new BaseValueParser();
778 
779         assertEquals("Wrong number of keys", 0, vp.keySet().size());
780 
781         float [] testValue = {
782             1.0f, 2.0f, 3.0f
783         };
784 
785         for (int i = 0; i < testValue.length; i++)
786         {
787             vp.add("foo", testValue[i]);
788 
789             String [] res = vp.getStrings("foo");
790             assertEquals("Wrong number of elements", res.length, i + 1);
791         }
792 
793         assertEquals("Wrong number of keys", 1, vp.keySet().size());
794 
795         float [] res = vp.getFloats("foo");
796 
797         assertEquals("Wrong number of elements", 3, res.length);
798 
799         for (int i = 0; i < res.length; i++)
800         {
801             assertEquals("Wrong value", res[i], testValue[i], 0.001f);
802         }
803 
804         Float [] resObj = vp.getFloatObjects("foo");
805 
806         assertEquals("Wrong number of elements", 3, resObj.length);
807 
808         for (int i = 0; i < resObj.length; i++)
809         {
810             assertEquals("Wrong value", resObj[i].floatValue(), testValue[i], 0.001f);
811         }
812 
813         assertEquals("Wrong element returned", testValue[0], vp.getFloatObject("foo").floatValue(), 0.001f);
814 
815         vp.add("foo", 4.0f);
816 
817         res = vp.getFloats("foo");
818 
819         assertEquals("Wrong number of elements", 4, res.length);
820 
821         for (int i = 0; i < 3; i++)
822         {
823             assertEquals("Wrong value", res[i], testValue[i], 0.001f);
824         }
825 
826         assertEquals(res[3], 4.0f, 0.001f);
827 
828         resObj = vp.getFloatObjects("foo");
829 
830         assertEquals("Wrong number of elements", 4, resObj.length);
831 
832         for (int i = 0; i < 3; i++)
833         {
834             assertEquals("Wrong value", resObj[i].floatValue(), testValue[i], 0.001f);
835         }
836 
837         assertEquals(resObj[3].floatValue(), 4.0f, 0.001f);
838 
839         // should append at the end.
840         assertEquals("Wrong element returned", testValue[0], vp.getFloat("foo"), 0.001f);
841     }
842 
843     public void testBigDecimalArray()
844     {
845         ValueParser vp = new BaseValueParser();
846 
847         assertEquals("Wrong number of keys", 0, vp.keySet().size());
848 
849         long [] testValue = {
850             12345678,87654321,1092837465,
851         };
852 
853         for (int i = 0; i < testValue.length; i++)
854         {
855             vp.add("foo", testValue[i]);
856 
857             String [] res = vp.getStrings("foo");
858             assertEquals("Wrong number of elements", res.length, i + 1);
859         }
860 
861         assertEquals("Wrong number of keys", 1, vp.keySet().size());
862 
863         BigDecimal [] res = vp.getBigDecimals("foo");
864 
865         assertEquals("Wrong number of elements", 3, res.length);
866 
867         for (int i = 0; i < res.length; i++)
868         {
869             assertEquals("Wrong value", res[i].longValue(), testValue[i]);
870         }
871 
872         assertEquals("Wrong element returned", testValue[0], vp.getBigDecimal("foo").longValue());
873 
874         vp.add("foo", 77777777);
875 
876         res = vp.getBigDecimals("foo");
877 
878         assertEquals("Wrong number of elements", 4, res.length);
879 
880         for (int i = 0; i < 3; i++)
881         {
882             assertEquals("Wrong value", res[i].longValue(), testValue[i], 0.001);
883         }
884 
885         assertEquals(res[3].longValue(), 77777777);
886 
887         // should append at the end.
888         assertEquals("Wrong element returned", testValue[0], vp.getBigDecimal("foo").longValue());
889     }
890 
891     public void testIntegerArray()
892     {
893         ValueParser vp = new BaseValueParser();
894 
895         assertEquals("Wrong number of keys", 0, vp.keySet().size());
896 
897         int [] testValue = {
898             1, 2, 3
899         };
900 
901         for (int i = 0; i < testValue.length; i++)
902         {
903             vp.add("foo", testValue[i]);
904 
905             String [] res = vp.getStrings("foo");
906             assertEquals("Wrong number of elements", res.length, i + 1);
907         }
908 
909         assertEquals("Wrong number of keys", 1, vp.keySet().size());
910 
911         int [] res = vp.getInts("foo");
912 
913         assertEquals("Wrong number of elements", 3, res.length);
914 
915         for (int i = 0; i < res.length; i++)
916         {
917             assertEquals("Wrong value", res[i], testValue[i]);
918         }
919 
920         Integer [] resObj = vp.getIntObjects("foo");
921 
922         assertEquals("Wrong number of elements", 3, resObj.length);
923 
924         for (int i = 0; i < resObj.length; i++)
925         {
926             assertEquals("Wrong value", resObj[i].intValue(), testValue[i]);
927         }
928 
929         assertEquals("Wrong element returned", testValue[0], vp.getIntObject("foo").intValue());
930 
931         vp.add("foo", 4);
932 
933         res = vp.getInts("foo");
934 
935         assertEquals("Wrong number of elements", 4, res.length);
936 
937         for (int i = 0; i < 3; i++)
938         {
939             assertEquals("Wrong value", res[i], testValue[i]);
940         }
941 
942         assertEquals(res[3], 4);
943 
944         resObj = vp.getIntObjects("foo");
945 
946         assertEquals("Wrong number of elements", 4, resObj.length);
947 
948         for (int i = 0; i < 3; i++)
949         {
950             assertEquals("Wrong value", resObj[i].intValue(), testValue[i]);
951         }
952 
953         assertEquals(resObj[3].intValue(), 4);
954 
955         // should append at the end.
956         assertEquals("Wrong element returned", testValue[0], vp.getInt("foo"));
957     }
958 
959     public void testLongArray()
960     {
961         ValueParser vp = new BaseValueParser();
962 
963         assertEquals("Wrong number of keys", 0, vp.keySet().size());
964 
965         long [] testValue = {
966             1l, 2l, 3l
967         };
968 
969         for (int i = 0; i < testValue.length; i++)
970         {
971             vp.add("foo", testValue[i]);
972 
973             String [] res = vp.getStrings("foo");
974             assertEquals("Wrong number of elements", res.length, i + 1);
975         }
976 
977         assertEquals("Wrong number of keys", 1, vp.keySet().size());
978 
979         long [] res = vp.getLongs("foo");
980 
981         assertEquals("Wrong number of elements", 3, res.length);
982 
983         for (int i = 0; i < res.length; i++)
984         {
985             assertEquals("Wrong value", res[i], testValue[i]);
986         }
987 
988         Long [] resObj = vp.getLongObjects("foo");
989 
990         assertEquals("Wrong number of elements", 3, resObj.length);
991 
992         for (int i = 0; i < resObj.length; i++)
993         {
994             assertEquals("Wrong value", resObj[i].longValue(), testValue[i]);
995         }
996 
997         assertEquals("Wrong element returned", testValue[0], vp.getLongObject("foo").longValue());
998 
999         vp.add("foo", 4);
1000 
1001         res = vp.getLongs("foo");
1002 
1003         assertEquals("Wrong number of elements", 4, res.length);
1004 
1005         for (int i = 0; i < 3; i++)
1006         {
1007             assertEquals("Wrong value", res[i], testValue[i]);
1008         }
1009 
1010         assertEquals(res[3], 4);
1011 
1012         resObj = vp.getLongObjects("foo");
1013 
1014         assertEquals("Wrong number of elements", 4, resObj.length);
1015 
1016         for (int i = 0; i < 3; i++)
1017         {
1018             assertEquals("Wrong value", resObj[i].longValue(), testValue[i]);
1019         }
1020 
1021         assertEquals(resObj[3].longValue(), 4);
1022 
1023         // should append at the end.
1024         assertEquals("Wrong element returned", testValue[0], vp.getLong("foo"));
1025     }
1026 
1027     public void testByteArray()
1028             throws Exception
1029     {
1030         ValueParser vp = new BaseValueParser();
1031 
1032         assertEquals("Wrong number of keys", 0, vp.keySet().size());
1033 
1034         String  testValue = "abcdefg";
1035 
1036         vp.add("foo", testValue);
1037 
1038         assertEquals("Wrong number of keys", 1, vp.keySet().size());
1039 
1040         byte [] res = vp.getBytes("foo");
1041 
1042         assertEquals("Wrong number of elements", 7, res.length);
1043 
1044         for (int i = 0; i < res.length; i++)
1045         {
1046             byte [] testByte = testValue.substring(i, i + 1).getBytes(vp.getCharacterEncoding());
1047             assertEquals("More than one byte for a char!", 1, testByte.length);
1048             assertEquals("Wrong value", res[i], testByte[0]);
1049         }
1050     }
1051 
1052     public void testByte()
1053     {
1054         ValueParser vp = new BaseValueParser();
1055 
1056         assertEquals("Wrong number of keys", 0, vp.keySet().size());
1057 
1058         String [] testValue = {
1059             "0", "127", "-1",
1060             "0", "-127", "100"
1061         };
1062 
1063 
1064         for (int i = 0; i < testValue.length; i++)
1065         {
1066             vp.add("foo" + i, testValue[i]);
1067         }
1068 
1069         assertEquals("Wrong number of keys", 6, vp.keySet().size());
1070 
1071         assertEquals("Wrong value", (byte) 0,    vp.getByte("foo0"));
1072         assertEquals("Wrong value", (byte) 127,  vp.getByte("foo1"));
1073         assertEquals("Wrong value", (byte) -1,   vp.getByte("foo2"));
1074         assertEquals("Wrong value", (byte) 0,    vp.getByte("foo3"));
1075         assertEquals("Wrong value", (byte) -127, vp.getByte("foo4"));
1076         assertEquals("Wrong value", (byte) 100,  vp.getByte("foo5"));
1077 
1078         assertEquals("Wrong value", new Byte((byte) 0),    vp.getByteObject("foo0"));
1079         assertEquals("Wrong value", new Byte((byte) 127),  vp.getByteObject("foo1"));
1080         assertEquals("Wrong value", new Byte((byte) -1),   vp.getByteObject("foo2"));
1081         assertEquals("Wrong value", new Byte((byte) 0),    vp.getByteObject("foo3"));
1082         assertEquals("Wrong value", new Byte((byte) -127), vp.getByteObject("foo4"));
1083         assertEquals("Wrong value", new Byte((byte) 100),  vp.getByteObject("foo5"));
1084 
1085     }
1086 
1087     public void testStringDefault()
1088     {
1089         ValueParser vp = new BaseValueParser();
1090 
1091         assertEquals("Wrong number of keys", 0, vp.keySet().size());
1092 
1093         vp.add("foo", "bar");
1094 
1095         assertEquals("Wrong value found", "bar", vp.getString("foo", "xxx"));
1096         assertEquals("Wrong value found", "bar", vp.getString("foo", null));
1097 
1098         assertEquals("Wrong value found", "baz", vp.getString("does-not-exist", "baz"));
1099         assertNull(vp.getString("does-not-exist", null));
1100     }
1101 
1102     public void testSetString()
1103     {
1104         ValueParser vp = new BaseValueParser();
1105 
1106         assertEquals("Wrong number of keys", 0, vp.keySet().size());
1107 
1108         vp.add("foo", "bar");
1109 
1110         assertEquals("Wrong number of keys", 1, vp.keySet().size());
1111 
1112         vp.add("bar", "foo");
1113 
1114         assertEquals("Wrong number of keys", 2, vp.keySet().size());
1115 
1116         vp.add("bar", "baz");
1117 
1118         assertEquals("Wrong number of keys", 2, vp.keySet().size());
1119 
1120         String [] res = vp.getStrings("bar");
1121         assertEquals("Wrong number of values", 2, res.length);
1122         assertEquals("Wrong value found", "foo", res[0]);
1123         assertEquals("Wrong value found", "baz", res[1]);
1124 
1125         vp.setString("bar", "xxx");
1126 
1127         assertEquals("Wrong number of keys", 2, vp.keySet().size());
1128 
1129         res = vp.getStrings("bar");
1130         assertEquals("Wrong number of values", 1, res.length);
1131         assertEquals("Wrong value found", "xxx", res[0]);
1132     }
1133 
1134     public void testSetStrings()
1135     {
1136         ValueParser vp = new BaseValueParser();
1137 
1138         assertEquals("Wrong number of keys", 0, vp.keySet().size());
1139 
1140         vp.add("foo", "bar");
1141 
1142         assertEquals("Wrong number of keys", 1, vp.keySet().size());
1143 
1144         vp.add("bar", "foo");
1145 
1146         assertEquals("Wrong number of keys", 2, vp.keySet().size());
1147 
1148         vp.add("bar", "baz");
1149 
1150         assertEquals("Wrong number of keys", 2, vp.keySet().size());
1151 
1152         String [] res = vp.getStrings("bar");
1153         assertEquals("Wrong number of values", 2, res.length);
1154         assertEquals("Wrong value found", "foo", res[0]);
1155         assertEquals("Wrong value found", "baz", res[1]);
1156 
1157         String [] newValues = new String [] { "aaa", "bbb", "ccc", "ddd" };
1158 
1159         vp.setStrings("bar", newValues);
1160 
1161         assertEquals("Wrong number of keys", 2, vp.keySet().size());
1162 
1163         res = vp.getStrings("bar");
1164         assertEquals("Wrong number of values", newValues.length, res.length);
1165 
1166         for (int i = 0 ; i < newValues.length; i++)
1167         {
1168             assertEquals("Wrong value found", newValues[i], res[i]);
1169         }
1170     }
1171 
1172     public void testSetProperties()
1173             throws Exception
1174     {
1175         ValueParser vp = new BaseValueParser();
1176 
1177         vp.add("longvalue", 12345l);
1178         vp.add("doublevalue", 2.0);
1179         vp.add("intValue", 200);
1180         vp.add("stringvalue", "foobar");
1181         vp.add("booleanvalue", "true");
1182 
1183         PropertyBean bp = new PropertyBean();
1184         bp.setDoNotTouchValue("abcdef");
1185 
1186         vp.setProperties(bp);
1187 
1188         assertEquals("Wrong value in bean", "abcdef", bp.getDoNotTouchValue());
1189         assertEquals("Wrong value in bean", "foobar", bp.getStringValue());
1190         assertEquals("Wrong value in bean", 200,      bp.getIntValue());
1191         assertEquals("Wrong value in bean", 2.0,      bp.getDoubleValue(), 0.001);
1192         assertEquals("Wrong value in bean", 12345l,   bp.getLongValue());
1193         assertEquals("Wrong value in bean", Boolean.TRUE, bp.getBooleanValue());
1194     }
1195 
1196     public void testAddNulls()
1197     {
1198         ValueParser vp = new BaseValueParser();
1199 
1200         assertEquals("Wrong number of keys", 0, vp.keySet().size());
1201 
1202         vp.add("foo", (Integer) null);
1203 
1204         assertEquals("Wrong number of keys", 0, vp.keySet().size());
1205 
1206         vp.add("foo", (String) null);
1207 
1208         assertEquals("Wrong number of keys", 0, vp.keySet().size());
1209 
1210         vp.add("bar", "null");
1211 
1212         assertEquals("Wrong number of keys", 1, vp.keySet().size());
1213 
1214     }
1215 
1216     public void testAddNullArrays()
1217     {
1218         String [] res = null;
1219 
1220         ValueParser vp = new BaseValueParser();
1221         assertEquals("Wrong number of keys", 0, vp.keySet().size());
1222 
1223         vp.add("foo", new String [] { "foo", "bar" });
1224         res = vp.getStrings("foo");
1225         assertEquals("Wrong number of keys", 1, vp.keySet().size());
1226         assertEquals("Wrong number of values", 2, res.length);
1227 
1228         // null value should not change contents
1229         vp.add("foo", (String) null);
1230         res = vp.getStrings("foo");
1231         assertEquals("Wrong number of keys", 1, vp.keySet().size());
1232         assertEquals("Wrong number of values", 2, res.length);
1233 
1234         // null value should not change contents
1235         vp.add("foo", (String []) null);
1236         res = vp.getStrings("foo");
1237         assertEquals("Wrong number of keys", 1, vp.keySet().size());
1238         assertEquals("Wrong number of values", 2, res.length);
1239 
1240         // empty String array should not change contents
1241         vp.add("foo", new String [0]);
1242         res = vp.getStrings("foo");
1243         assertEquals("Wrong number of keys", 1, vp.keySet().size());
1244         assertEquals("Wrong number of values", 2, res.length);
1245 
1246         // String array with null value should not change contents
1247         vp.add("foo", new String [] { null });
1248         res = vp.getStrings("foo");
1249         assertEquals("Wrong number of keys", 1, vp.keySet().size());
1250         assertEquals("Wrong number of values", 2, res.length);
1251 
1252         // String array with null value should only add non-null values
1253         vp.add("foo", new String [] { "bla", null, "foo" });
1254         res = vp.getStrings("foo");
1255         assertEquals("Wrong number of keys", 1, vp.keySet().size());
1256         assertEquals("Wrong number of values", 4, res.length);
1257     }
1258 
1259     public void testNonExistingResults()
1260     {
1261         ValueParser vp = new BaseValueParser();
1262         assertEquals("Wrong number of keys", 0, vp.keySet().size());
1263 
1264         assertEquals("Wrong value for non existing key", 0.0, vp.getDouble("foo"), 0.001);
1265         assertNull(vp.getDoubles("foo"));
1266         assertNull(vp.getDoubleObject("foo"));
1267         assertNull(vp.getDoubleObjects("foo"));
1268 
1269         assertEquals("Wrong number of keys", 0, vp.keySet().size());
1270 
1271         assertNull(vp.getString("foo"));
1272         assertNull(vp.getStrings("foo"));
1273 
1274         assertEquals("Wrong value for non existing key", 0.0f, vp.getFloat("foo"), 0.001);
1275         assertNull(vp.getFloats("foo"));
1276         assertNull(vp.getFloatObject("foo"));
1277         assertNull(vp.getFloatObjects("foo"));
1278 
1279         assertEquals("Wrong number of keys", 0, vp.keySet().size());
1280 
1281         assertNull(vp.getBigDecimal("foo"));
1282         assertNull(vp.getBigDecimals("foo"));
1283 
1284         assertEquals("Wrong number of keys", 0, vp.keySet().size());
1285 
1286         assertEquals("Wrong value for non existing key", 0, vp.getInt("foo"));
1287         assertNull(vp.getInts("foo"));
1288         assertNull(vp.getIntObject("foo"));
1289         assertNull(vp.getIntObjects("foo"));
1290 
1291         assertEquals("Wrong number of keys", 0, vp.keySet().size());
1292 
1293         assertEquals("Wrong value for non existing key", 0, vp.getLong("foo"));
1294         assertNull(vp.getLongs("foo"));
1295         assertNull(vp.getLongObject("foo"));
1296         assertNull(vp.getLongObjects("foo"));
1297 
1298         assertEquals("Wrong number of keys", 0, vp.keySet().size());
1299 
1300         assertEquals("Wrong value for non existing key", 0, vp.getByte("foo"));
1301         assertNull(vp.getByteObject("foo"));
1302 
1303         assertEquals("Wrong number of keys", 0, vp.keySet().size());
1304     }
1305 }
1306