KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > util > BaseValueParserTest


1 package org.apache.turbine.util;
2
3 /*
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License")
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.math.BigDecimal JavaDoc;
20
21 import junit.framework.TestSuite;
22
23 import org.apache.commons.configuration.BaseConfiguration;
24 import org.apache.commons.configuration.Configuration;
25
26 import org.apache.turbine.services.ServiceManager;
27 import org.apache.turbine.services.TurbineServices;
28 import org.apache.turbine.test.BaseTestCase;
29 import org.apache.turbine.util.parser.BaseValueParser;
30 import org.apache.turbine.util.parser.ParserUtils;
31
32 /**
33  * Testing of the BaseValueParser class
34  *
35  * @author <a HREF="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
36  * @version $Id: BaseValueParserTest.java,v 1.4.2.2 2004/05/20 03:33:11 seade Exp $
37  */

38 public class BaseValueParserTest extends BaseTestCase
39 {
40     private BaseValueParser parser;
41
42     /**
43      * Constructor for test.
44      *
45      * @param testName name of the test being executed
46      */

47     public BaseValueParserTest(String JavaDoc testName)
48             throws Exception JavaDoc
49     {
50         super(testName);
51
52         // Setup configuration
53
ServiceManager serviceManager = TurbineServices.getInstance();
54         serviceManager.setApplicationRoot(".");
55         Configuration cfg = new BaseConfiguration();
56         cfg.setProperty(ParserUtils.URL_CASE_FOLDING_KEY,
57                 ParserUtils.URL_CASE_FOLDING_LOWER_VALUE );
58         serviceManager.setConfiguration(cfg);
59
60     }
61
62     /**
63      * Performs any initialization that must happen before each test is run.
64      */

65     protected void setUp()
66     {
67         parser = new BaseValueParser();
68     }
69
70     /**
71      * Clean up after each test is run.
72      */

73     protected void tearDown()
74     {
75         parser = null;
76     }
77
78     /**
79      * Factory method for creating a TestSuite for this class.
80      *
81      * @return the test suite
82      */

83     public static TestSuite suite()
84     {
85         TestSuite suite = new TestSuite(BaseValueParserTest.class);
86         return suite;
87     }
88
89     public void testGetByte()
90     {
91         // no param
92
byte result = parser.getByte("invalid");
93         assertEquals(result, 0);
94
95         // default
96
result = parser.getByte("default", (byte)3);
97         assertEquals(result, 3);
98
99         // param exists
100
parser.add("exists", "1");
101         result = parser.getByte("exists");
102         assertEquals(result, 1);
103
104         // unparsable value
105
parser.add("unparsable", "a");
106         result = parser.getByte("unparsable");
107         assertEquals(result, 0);
108     }
109
110     public void testGetByteObject()
111     {
112         // no param
113
Byte JavaDoc result = parser.getByteObject("invalid");
114         assertNull(result);
115
116         // default
117
result = parser.getByteObject("default", new Byte JavaDoc((byte)3));
118         assertEquals(result, new Byte JavaDoc((byte)3));
119
120         // param exists
121
parser.add("exists", "1");
122         result = parser.getByteObject("exists");
123         assertEquals(result, new Byte JavaDoc((byte)1));
124
125         // unparsable value
126
parser.add("unparsable", "a");
127         result = parser.getByteObject("unparsable");
128         assertNull(result);
129     }
130
131     public void testGetInt()
132     {
133         // no param
134
int result = parser.getInt("invalid");
135         assertEquals(result, 0);
136
137         // default
138
result = parser.getInt("default", 3);
139         assertEquals(result, 3);
140
141         // param exists
142
parser.add("exists", "1");
143         result = parser.getInt("exists");
144         assertEquals(result, 1);
145
146         // unparsable value
147
parser.add("unparsable", "a");
148         result = parser.getInt("unparsable");
149         assertEquals(result, 0);
150
151         // array
152
parser.add("array", "1");
153         parser.add("array", "2");
154         parser.add("array", "3");
155         int arrayResult[] = parser.getInts("array");
156         int compare[] = {1,2,3};
157         assertEquals(arrayResult.length, compare.length);
158         for( int i=0; i<compare.length; i++)
159         {
160             assertEquals(compare[i], arrayResult[i]);
161         }
162
163         // array w/ unparsable element
164
parser.add("array2", "1");
165         parser.add("array2", "a");
166         parser.add("array2", "3");
167         int arrayResult2[] = parser.getInts("array2");
168         int compare2[] = {1,0,3};
169         assertEquals(arrayResult2.length, compare2.length);
170         for( int i=0; i<compare2.length; i++)
171         {
172             assertEquals(compare2[i], arrayResult2[i] );
173         }
174     }
175
176     public void testGetIntObject()
177     {
178         // no param
179
Integer JavaDoc result = parser.getIntObject("invalid");
180         assertNull(result);
181
182         // default
183
result = parser.getIntObject("default", new Integer JavaDoc(3));
184         assertEquals(result, new Integer JavaDoc(3));
185
186         // param exists
187
parser.add("exists", "1");
188         result = parser.getIntObject("exists");
189         assertEquals(result, new Integer JavaDoc(1));
190
191         // unparsable value
192
parser.add("unparsable", "a");
193         result = parser.getIntObject("unparsable");
194         assertNull(result);
195
196         // array
197
parser.add("array", "1");
198         parser.add("array", "2");
199         parser.add("array", "3");
200         Integer JavaDoc arrayResult[] = parser.getIntObjects("array");
201         Integer JavaDoc compare[] = {new Integer JavaDoc(1), new Integer JavaDoc(2), new Integer JavaDoc(3)};
202         assertEquals(arrayResult.length, compare.length);
203         for( int i=0; i<compare.length; i++)
204         {
205             assertEquals(compare[i], arrayResult[i]);
206         }
207
208         // array w/ unparsable element
209
parser.add("array2", "1");
210         parser.add("array2", "a");
211         parser.add("array2", "3");
212         Integer JavaDoc arrayResult2[] = parser.getIntObjects("array2");
213         Integer JavaDoc compare2[] = {new Integer JavaDoc(1), null, new Integer JavaDoc(3)};
214         assertEquals(arrayResult2.length, compare2.length);
215         for( int i=0; i<compare2.length; i++)
216         {
217             assertEquals(compare2[i], arrayResult2[i] );
218         }
219     }
220
221     public void testGetFloat()
222     {
223         // no param
224
float result = parser.getFloat("invalid");
225         assertEquals(result, 0, 0);
226
227         // default
228
result = parser.getFloat("default", 3);
229         assertEquals(result, 3, 0);
230
231         // param exists
232
parser.add("exists", "1");
233         result = parser.getFloat("exists");
234         assertEquals(result, 1, 0);
235
236         // unparsable value
237
parser.add("unparsable", "a");
238         result = parser.getFloat("unparsable");
239         assertEquals(result, 0, 0);
240
241         // array
242
parser.add("array", "1");
243         parser.add("array", "2");
244         parser.add("array", "3");
245         float arrayResult[] = parser.getFloats("array");
246         float compare[] = {1,2,3};
247         assertEquals(arrayResult.length, compare.length);
248         for( int i=0; i<compare.length; i++)
249         {
250             assertEquals(compare[i], arrayResult[i], 0);
251         }
252
253         // array w/ unparsable element
254
parser.add("array2", "1");
255         parser.add("array2", "a");
256         parser.add("array2", "3");
257         float arrayResult2[] = parser.getFloats("array2");
258         float compare2[] = {1,0,3};
259         assertEquals(arrayResult2.length, compare2.length);
260         for( int i=0; i<compare2.length; i++)
261         {
262             assertEquals(compare2[i], arrayResult2[i], 0);
263         }
264     }
265
266     public void testGetFloatObject()
267     {
268         // no param
269
Float JavaDoc result = parser.getFloatObject("invalid");
270         assertNull(result);
271
272         // default
273
result = parser.getFloatObject("default", new Float JavaDoc(3));
274         assertEquals(result, new Float JavaDoc(3));
275
276         // param exists
277
parser.add("exists", "1");
278         result = parser.getFloatObject("exists");
279         assertEquals(result, new Float JavaDoc(1));
280
281         // unparsable value
282
parser.add("unparsable", "a");
283         result = parser.getFloatObject("unparsable");
284         assertNull(result);
285
286         // array
287
parser.add("array", "1");
288         parser.add("array", "2");
289         parser.add("array", "3");
290         Float JavaDoc arrayResult[] = parser.getFloatObjects("array");
291         Float JavaDoc compare[] = {new Float JavaDoc(1), new Float JavaDoc(2), new Float JavaDoc(3)};
292         assertEquals(arrayResult.length, compare.length);
293         for( int i=0; i<compare.length; i++)
294         {
295             assertEquals(compare[i], arrayResult[i]);
296         }
297
298         // array w/ unparsable element
299
parser.add("array2", "1");
300         parser.add("array2", "a");
301         parser.add("array2", "3");
302         Float JavaDoc arrayResult2[] = parser.getFloatObjects("array2");
303         Float JavaDoc compare2[] = {new Float JavaDoc(1), null, new Float JavaDoc(3)};
304         assertEquals(arrayResult2.length, compare2.length);
305         for( int i=0; i<compare2.length; i++)
306         {
307             assertEquals(compare2[i], arrayResult2[i] );
308         }
309     }
310
311     public void testGetDouble()
312     {
313         // no param
314
double result = parser.getDouble("invalid");
315         assertEquals(result, 0, 0);
316
317         // default
318
result = parser.getDouble("default", 3);
319         assertEquals(result, 3, 0);
320
321         // param exists
322
parser.add("exists", "1");
323         result = parser.getDouble("exists");
324         assertEquals(result, 1, 0);
325
326         // unparsable value
327
parser.add("unparsable", "a");
328         result = parser.getDouble("unparsable");
329         assertEquals(result, 0, 0);
330
331         // array
332
parser.add("array", "1");
333         parser.add("array", "2");
334         parser.add("array", "3");
335         double arrayResult[] = parser.getDoubles("array");
336         double compare[] = {1,2,3};
337         assertEquals(arrayResult.length, compare.length);
338         for( int i=0; i<compare.length; i++)
339         {
340             assertEquals(compare[i], arrayResult[i], 0);
341         }
342
343         // array w/ unparsable element
344
parser.add("array2", "1");
345         parser.add("array2", "a");
346         parser.add("array2", "3");
347         double arrayResult2[] = parser.getDoubles("array2");
348         double compare2[] = {1,0,3};
349         assertEquals(arrayResult2.length, compare2.length);
350         for( int i=0; i<compare2.length; i++)
351         {
352             assertEquals(compare2[i], arrayResult2[i], 0);
353         }
354     }
355
356     public void testGetDoubleObject()
357     {
358         // no param
359
Double JavaDoc result = parser.getDoubleObject("invalid");
360         assertNull(result);
361
362         // default
363
result = parser.getDoubleObject("default", new Double JavaDoc(3));
364         assertEquals(result, new Double JavaDoc(3));
365
366         // param exists
367
parser.add("exists", "1");
368         result = parser.getDoubleObject("exists");
369         assertEquals(result, new Double JavaDoc(1));
370
371         // unparsable value
372
parser.add("unparsable", "a");
373         result = parser.getDoubleObject("unparsable");
374         assertNull(result);
375
376         // array
377
parser.add("array", "1");
378         parser.add("array", "2");
379         parser.add("array", "3");
380         Double JavaDoc arrayResult[] = parser.getDoubleObjects("array");
381         Double JavaDoc compare[] = {new Double JavaDoc(1), new Double JavaDoc(2), new Double JavaDoc(3)};
382         assertEquals(arrayResult.length, compare.length);
383         for( int i=0; i<compare.length; i++)
384         {
385             assertEquals(compare[i], arrayResult[i]);
386         }
387
388         // array w/ unparsable element
389
parser.add("array2", "1");
390         parser.add("array2", "a");
391         parser.add("array2", "3");
392         Double JavaDoc arrayResult2[] = parser.getDoubleObjects("array2");
393         Double JavaDoc compare2[] = {new Double JavaDoc(1), null, new Double JavaDoc(3)};
394         assertEquals(arrayResult2.length, compare2.length);
395         for( int i=0; i<compare2.length; i++)
396         {
397             assertEquals(compare2[i], arrayResult2[i] );
398         }
399     }
400
401     public void testGetLong()
402     {
403         // no param
404
long result = parser.getLong("invalid");
405         assertEquals(result, 0);
406
407         // default
408
result = parser.getLong("default", 3);
409         assertEquals(result, 3);
410
411         // param exists
412
parser.add("exists", "1");
413         result = parser.getLong("exists");
414         assertEquals(result, 1);
415
416         // unparsable value
417
parser.add("unparsable", "a");
418         result = parser.getLong("unparsable");
419         assertEquals(result, 0);
420
421         // array
422
parser.add("array", "1");
423         parser.add("array", "2");
424         parser.add("array", "3");
425         long arrayResult[] = parser.getLongs("array");
426         long compare[] = {1,2,3};
427         assertEquals(arrayResult.length, compare.length);
428         for( int i=0; i<compare.length; i++)
429         {
430             assertEquals(compare[i], arrayResult[i]);
431         }
432
433         // array w/ unparsable element
434
parser.add("array2", "1");
435         parser.add("array2", "a");
436         parser.add("array2", "3");
437         long arrayResult2[] = parser.getLongs("array2");
438         long compare2[] = {1,0,3};
439         assertEquals(arrayResult2.length, compare2.length);
440         for( int i=0; i<compare2.length; i++)
441         {
442             assertEquals(compare2[i], arrayResult2[i]);
443         }
444     }
445
446     public void testGetLongObject()
447     {
448         // no param
449
Long JavaDoc result = parser.getLongObject("invalid");
450         assertNull(result);
451
452         // default
453
result = parser.getLongObject("default", new Long JavaDoc(3));
454         assertEquals(result, new Long JavaDoc(3));
455
456         // param exists
457
parser.add("exists", "1");
458         result = parser.getLongObject("exists");
459         assertEquals(result, new Long JavaDoc(1));
460
461         // unparsable value
462
parser.add("unparsable", "a");
463         result = parser.getLongObject("unparsable");
464         assertNull(result);
465
466         // array
467
parser.add("array", "1");
468         parser.add("array", "2");
469         parser.add("array", "3");
470         Long JavaDoc arrayResult[] = parser.getLongObjects("array");
471         Long JavaDoc compare[] = {new Long JavaDoc(1), new Long JavaDoc(2), new Long JavaDoc(3)};
472         assertEquals(arrayResult.length, compare.length);
473         for( int i=0; i<compare.length; i++)
474         {
475             assertEquals(compare[i], arrayResult[i]);
476         }
477
478         // array w/ unparsable element
479
parser.add("array2", "1");
480         parser.add("array2", "a");
481         parser.add("array2", "3");
482         Long JavaDoc arrayResult2[] = parser.getLongObjects("array2");
483         Long JavaDoc compare2[] = {new Long JavaDoc(1), null, new Long JavaDoc(3)};
484         assertEquals(arrayResult2.length, compare2.length);
485         for( int i=0; i<compare2.length; i++)
486         {
487             assertEquals(compare2[i], arrayResult2[i] );
488         }
489     }
490
491     public void testGetBoolean()
492     {
493         // no param
494
boolean result = parser.getBoolean("invalid");
495         assertEquals(result, false);
496
497         // default
498
result = parser.getBoolean("default", true);
499         assertEquals(result, true);
500
501         // true values - Case is intentional
502
parser.add("true1", "trUe");
503         result = parser.getBoolean("true1");
504         assertEquals(result, true);
505         parser.add("true2", "yEs");
506         result = parser.getBoolean("true2");
507         assertEquals(result, true);
508         parser.add("true3", "1");
509         result = parser.getBoolean("true3");
510         assertEquals(result, true);
511         parser.add("true4", "oN");
512         result = parser.getBoolean("true4");
513         assertEquals(result, true);
514
515         // unparsable value
516
parser.add("unparsable", "a");
517         result = parser.getBoolean("unparsable");
518         assertEquals(result, false);
519
520     }
521
522     public void testGetBooleanObject()
523     {
524         // no param
525
Boolean JavaDoc result = parser.getBooleanObject("invalid");
526         assertNull(result);
527
528         // default
529
result = parser.getBooleanObject("default", Boolean.TRUE);
530         assertEquals(result, Boolean.TRUE);
531
532         // true values - Case is intentional
533
parser.add("true1", "trUe");
534         result = parser.getBooleanObject("true1");
535         assertEquals(result, Boolean.TRUE);
536         parser.add("true2", "yEs");
537         result = parser.getBooleanObject("true2");
538         assertEquals(result, Boolean.TRUE);
539         parser.add("true3", "1");
540         result = parser.getBooleanObject("true3");
541         assertEquals(result, Boolean.TRUE);
542         parser.add("true4", "oN");
543         result = parser.getBooleanObject("true4");
544         assertEquals(result, Boolean.TRUE);
545
546         // false values - Case is intentional
547
parser.add("false1", "falSe");
548         result = parser.getBooleanObject("false1");
549         assertEquals(result, Boolean.FALSE);
550         parser.add("false2", "nO");
551         result = parser.getBooleanObject("false2");
552         assertEquals(result, Boolean.FALSE);
553         parser.add("false3", "0");
554         result = parser.getBooleanObject("false3");
555         assertEquals(result, Boolean.FALSE);
556         parser.add("false4", "oFf");
557         result = parser.getBooleanObject("false4");
558         assertEquals(result, Boolean.FALSE);
559
560
561         // unparsable value
562
parser.add("unparsable", "a");
563         result = parser.getBooleanObject("unparsable");
564         assertNull(result);
565     }
566
567     public void testGetBigDecimal()
568     {
569         // no param
570
BigDecimal JavaDoc result = parser.getBigDecimal("invalid");
571         assertEquals(new BigDecimal JavaDoc(0), result);
572
573         // default
574
result = parser.getBigDecimal("default", new BigDecimal JavaDoc(3));
575         assertEquals(result, new BigDecimal JavaDoc(3));
576
577         // param exists
578
parser.add("exists", "1");
579         result = parser.getBigDecimal("exists");
580         assertEquals(result, new BigDecimal JavaDoc(1));
581
582         // unparsable value
583
parser.add("unparsable", "a");
584         result = parser.getBigDecimal("unparsable");
585         assertEquals(new BigDecimal JavaDoc(0), result);
586
587         // array
588
parser.add("array", "1");
589         parser.add("array", "2");
590         parser.add("array", "3");
591         BigDecimal JavaDoc arrayResult[] = parser.getBigDecimals("array");
592         BigDecimal JavaDoc compare[] = {new BigDecimal JavaDoc(1), new BigDecimal JavaDoc(2),
593                                 new BigDecimal JavaDoc(3)};
594         assertEquals(arrayResult.length, compare.length);
595         for( int i=0; i<compare.length; i++)
596         {
597             assertEquals(compare[i], arrayResult[i]);
598         }
599
600         // array w/ unparsable element
601
parser.add("array2", "1");
602         parser.add("array2", "a");
603         parser.add("array2", "3");
604         BigDecimal JavaDoc arrayResult2[] = parser.getBigDecimals("array2");
605         BigDecimal JavaDoc compare2[] = {new BigDecimal JavaDoc(1), null, new BigDecimal JavaDoc(3)};
606         assertEquals(arrayResult2.length, compare2.length);
607         for( int i=0; i<compare2.length; i++)
608         {
609             assertEquals(compare2[i], arrayResult2[i] );
610         }
611     }
612
613
614     public void getString()
615     {
616         // no param
617
String JavaDoc result = parser.getString("invalid");
618         assertNull(result);
619
620         // default
621
result = parser.getString("default", "default");
622         assertEquals(result, "default");
623
624         // null value
625
parser.add("null", "null");
626         assertNull( parser.getString("null"));
627
628         // only return the first added
629
parser.add("multiple", "test");
630         parser.add("multiple", "test2");
631         assertEquals("test2", parser.getString("multiple"));
632
633         // array
634
parser.add("array", "line1");
635         parser.add("array", "line2");
636         parser.add("array", "line3");
637         String JavaDoc arrayResult[] = parser.getStrings("array");
638         String JavaDoc compare[] = {"line1","line2","line3"};
639         assertEquals(arrayResult.length, compare.length);
640         for( int i=0; i<compare.length; i++)
641         {
642             assertEquals(compare[i], arrayResult[i]);
643         }
644
645     }
646
647 }
648
Popular Tags