001: package org.apache.turbine.util;
002:
003: /*
004: * Copyright 2001-2005 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License")
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.math.BigDecimal;
020:
021: import junit.framework.TestSuite;
022:
023: import org.apache.commons.configuration.BaseConfiguration;
024: import org.apache.commons.configuration.Configuration;
025:
026: import org.apache.turbine.services.ServiceManager;
027: import org.apache.turbine.services.TurbineServices;
028: import org.apache.turbine.test.BaseTestCase;
029: import org.apache.turbine.util.parser.BaseValueParser;
030: import org.apache.turbine.util.parser.ParserUtils;
031:
032: /**
033: * Testing of the BaseValueParser class
034: *
035: * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
036: * @version $Id: BaseValueParserTest.java 264148 2005-08-29 14:21:04Z henning $
037: */
038: public class BaseValueParserTest extends BaseTestCase {
039: private BaseValueParser parser;
040:
041: /**
042: * Constructor for test.
043: *
044: * @param testName name of the test being executed
045: */
046: public BaseValueParserTest(String testName) throws Exception {
047: super (testName);
048:
049: // Setup configuration
050: ServiceManager serviceManager = TurbineServices.getInstance();
051: serviceManager.setApplicationRoot(".");
052: Configuration cfg = new BaseConfiguration();
053: cfg.setProperty(ParserUtils.URL_CASE_FOLDING_KEY,
054: ParserUtils.URL_CASE_FOLDING_LOWER_VALUE);
055: serviceManager.setConfiguration(cfg);
056:
057: }
058:
059: /**
060: * Performs any initialization that must happen before each test is run.
061: */
062: protected void setUp() {
063: parser = new BaseValueParser();
064: }
065:
066: /**
067: * Clean up after each test is run.
068: */
069: protected void tearDown() {
070: parser = null;
071: }
072:
073: /**
074: * Factory method for creating a TestSuite for this class.
075: *
076: * @return the test suite
077: */
078: public static TestSuite suite() {
079: TestSuite suite = new TestSuite(BaseValueParserTest.class);
080: return suite;
081: }
082:
083: public void testGetByte() {
084: // no param
085: byte result = parser.getByte("invalid");
086: assertEquals(result, 0);
087:
088: // default
089: result = parser.getByte("default", (byte) 3);
090: assertEquals(result, 3);
091:
092: // param exists
093: parser.add("exists", "1");
094: result = parser.getByte("exists");
095: assertEquals(result, 1);
096:
097: // unparsable value
098: parser.add("unparsable", "a");
099: result = parser.getByte("unparsable");
100: assertEquals(result, 0);
101: }
102:
103: public void testGetByteObject() {
104: // no param
105: Byte result = parser.getByteObject("invalid");
106: assertNull(result);
107:
108: // default
109: result = parser.getByteObject("default", new Byte((byte) 3));
110: assertEquals(result, new Byte((byte) 3));
111:
112: // param exists
113: parser.add("exists", "1");
114: result = parser.getByteObject("exists");
115: assertEquals(result, new Byte((byte) 1));
116:
117: // unparsable value
118: parser.add("unparsable", "a");
119: result = parser.getByteObject("unparsable");
120: assertNull(result);
121: }
122:
123: public void testGetInt() {
124: // no param
125: int result = parser.getInt("invalid");
126: assertEquals(result, 0);
127:
128: // default
129: result = parser.getInt("default", 3);
130: assertEquals(result, 3);
131:
132: // param exists
133: parser.add("exists", "1");
134: result = parser.getInt("exists");
135: assertEquals(result, 1);
136:
137: // unparsable value
138: parser.add("unparsable", "a");
139: result = parser.getInt("unparsable");
140: assertEquals(result, 0);
141:
142: // array
143: parser.add("array", "1");
144: parser.add("array", "2");
145: parser.add("array", "3");
146: int arrayResult[] = parser.getInts("array");
147: int compare[] = { 1, 2, 3 };
148: assertEquals(arrayResult.length, compare.length);
149: for (int i = 0; i < compare.length; i++) {
150: assertEquals(compare[i], arrayResult[i]);
151: }
152:
153: // array w/ unparsable element
154: parser.add("array2", "1");
155: parser.add("array2", "a");
156: parser.add("array2", "3");
157: int arrayResult2[] = parser.getInts("array2");
158: int compare2[] = { 1, 0, 3 };
159: assertEquals(arrayResult2.length, compare2.length);
160: for (int i = 0; i < compare2.length; i++) {
161: assertEquals(compare2[i], arrayResult2[i]);
162: }
163: }
164:
165: public void testGetIntObject() {
166: // no param
167: Integer result = parser.getIntObject("invalid");
168: assertNull(result);
169:
170: // default
171: result = parser.getIntObject("default", new Integer(3));
172: assertEquals(result, new Integer(3));
173:
174: // param exists
175: parser.add("exists", "1");
176: result = parser.getIntObject("exists");
177: assertEquals(result, new Integer(1));
178:
179: // unparsable value
180: parser.add("unparsable", "a");
181: result = parser.getIntObject("unparsable");
182: assertNull(result);
183:
184: // array
185: parser.add("array", "1");
186: parser.add("array", "2");
187: parser.add("array", "3");
188: Integer arrayResult[] = parser.getIntObjects("array");
189: Integer compare[] = { new Integer(1), new Integer(2),
190: new Integer(3) };
191: assertEquals(arrayResult.length, compare.length);
192: for (int i = 0; i < compare.length; i++) {
193: assertEquals(compare[i], arrayResult[i]);
194: }
195:
196: // array w/ unparsable element
197: parser.add("array2", "1");
198: parser.add("array2", "a");
199: parser.add("array2", "3");
200: Integer arrayResult2[] = parser.getIntObjects("array2");
201: Integer compare2[] = { new Integer(1), null, new Integer(3) };
202: assertEquals(arrayResult2.length, compare2.length);
203: for (int i = 0; i < compare2.length; i++) {
204: assertEquals(compare2[i], arrayResult2[i]);
205: }
206: }
207:
208: public void testGetFloat() {
209: // no param
210: float result = parser.getFloat("invalid");
211: assertEquals(result, 0, 0);
212:
213: // default
214: result = parser.getFloat("default", 3);
215: assertEquals(result, 3, 0);
216:
217: // param exists
218: parser.add("exists", "1");
219: result = parser.getFloat("exists");
220: assertEquals(result, 1, 0);
221:
222: // unparsable value
223: parser.add("unparsable", "a");
224: result = parser.getFloat("unparsable");
225: assertEquals(result, 0, 0);
226:
227: // array
228: parser.add("array", "1");
229: parser.add("array", "2");
230: parser.add("array", "3");
231: float arrayResult[] = parser.getFloats("array");
232: float compare[] = { 1, 2, 3 };
233: assertEquals(arrayResult.length, compare.length);
234: for (int i = 0; i < compare.length; i++) {
235: assertEquals(compare[i], arrayResult[i], 0);
236: }
237:
238: // array w/ unparsable element
239: parser.add("array2", "1");
240: parser.add("array2", "a");
241: parser.add("array2", "3");
242: float arrayResult2[] = parser.getFloats("array2");
243: float compare2[] = { 1, 0, 3 };
244: assertEquals(arrayResult2.length, compare2.length);
245: for (int i = 0; i < compare2.length; i++) {
246: assertEquals(compare2[i], arrayResult2[i], 0);
247: }
248: }
249:
250: public void testGetFloatObject() {
251: // no param
252: Float result = parser.getFloatObject("invalid");
253: assertNull(result);
254:
255: // default
256: result = parser.getFloatObject("default", new Float(3));
257: assertEquals(result, new Float(3));
258:
259: // param exists
260: parser.add("exists", "1");
261: result = parser.getFloatObject("exists");
262: assertEquals(result, new Float(1));
263:
264: // unparsable value
265: parser.add("unparsable", "a");
266: result = parser.getFloatObject("unparsable");
267: assertNull(result);
268:
269: // array
270: parser.add("array", "1");
271: parser.add("array", "2");
272: parser.add("array", "3");
273: Float arrayResult[] = parser.getFloatObjects("array");
274: Float compare[] = { new Float(1), new Float(2), new Float(3) };
275: assertEquals(arrayResult.length, compare.length);
276: for (int i = 0; i < compare.length; i++) {
277: assertEquals(compare[i], arrayResult[i]);
278: }
279:
280: // array w/ unparsable element
281: parser.add("array2", "1");
282: parser.add("array2", "a");
283: parser.add("array2", "3");
284: Float arrayResult2[] = parser.getFloatObjects("array2");
285: Float compare2[] = { new Float(1), null, new Float(3) };
286: assertEquals(arrayResult2.length, compare2.length);
287: for (int i = 0; i < compare2.length; i++) {
288: assertEquals(compare2[i], arrayResult2[i]);
289: }
290: }
291:
292: public void testGetDouble() {
293: // no param
294: double result = parser.getDouble("invalid");
295: assertEquals(result, 0, 0);
296:
297: // default
298: result = parser.getDouble("default", 3);
299: assertEquals(result, 3, 0);
300:
301: // param exists
302: parser.add("exists", "1");
303: result = parser.getDouble("exists");
304: assertEquals(result, 1, 0);
305:
306: // unparsable value
307: parser.add("unparsable", "a");
308: result = parser.getDouble("unparsable");
309: assertEquals(result, 0, 0);
310:
311: // array
312: parser.add("array", "1");
313: parser.add("array", "2");
314: parser.add("array", "3");
315: double arrayResult[] = parser.getDoubles("array");
316: double compare[] = { 1, 2, 3 };
317: assertEquals(arrayResult.length, compare.length);
318: for (int i = 0; i < compare.length; i++) {
319: assertEquals(compare[i], arrayResult[i], 0);
320: }
321:
322: // array w/ unparsable element
323: parser.add("array2", "1");
324: parser.add("array2", "a");
325: parser.add("array2", "3");
326: double arrayResult2[] = parser.getDoubles("array2");
327: double compare2[] = { 1, 0, 3 };
328: assertEquals(arrayResult2.length, compare2.length);
329: for (int i = 0; i < compare2.length; i++) {
330: assertEquals(compare2[i], arrayResult2[i], 0);
331: }
332: }
333:
334: public void testGetDoubleObject() {
335: // no param
336: Double result = parser.getDoubleObject("invalid");
337: assertNull(result);
338:
339: // default
340: result = parser.getDoubleObject("default", new Double(3));
341: assertEquals(result, new Double(3));
342:
343: // param exists
344: parser.add("exists", "1");
345: result = parser.getDoubleObject("exists");
346: assertEquals(result, new Double(1));
347:
348: // unparsable value
349: parser.add("unparsable", "a");
350: result = parser.getDoubleObject("unparsable");
351: assertNull(result);
352:
353: // array
354: parser.add("array", "1");
355: parser.add("array", "2");
356: parser.add("array", "3");
357: Double arrayResult[] = parser.getDoubleObjects("array");
358: Double compare[] = { new Double(1), new Double(2),
359: new Double(3) };
360: assertEquals(arrayResult.length, compare.length);
361: for (int i = 0; i < compare.length; i++) {
362: assertEquals(compare[i], arrayResult[i]);
363: }
364:
365: // array w/ unparsable element
366: parser.add("array2", "1");
367: parser.add("array2", "a");
368: parser.add("array2", "3");
369: Double arrayResult2[] = parser.getDoubleObjects("array2");
370: Double compare2[] = { new Double(1), null, new Double(3) };
371: assertEquals(arrayResult2.length, compare2.length);
372: for (int i = 0; i < compare2.length; i++) {
373: assertEquals(compare2[i], arrayResult2[i]);
374: }
375: }
376:
377: public void testGetLong() {
378: // no param
379: long result = parser.getLong("invalid");
380: assertEquals(result, 0);
381:
382: // default
383: result = parser.getLong("default", 3);
384: assertEquals(result, 3);
385:
386: // param exists
387: parser.add("exists", "1");
388: result = parser.getLong("exists");
389: assertEquals(result, 1);
390:
391: // unparsable value
392: parser.add("unparsable", "a");
393: result = parser.getLong("unparsable");
394: assertEquals(result, 0);
395:
396: // array
397: parser.add("array", "1");
398: parser.add("array", "2");
399: parser.add("array", "3");
400: long arrayResult[] = parser.getLongs("array");
401: long compare[] = { 1, 2, 3 };
402: assertEquals(arrayResult.length, compare.length);
403: for (int i = 0; i < compare.length; i++) {
404: assertEquals(compare[i], arrayResult[i]);
405: }
406:
407: // array w/ unparsable element
408: parser.add("array2", "1");
409: parser.add("array2", "a");
410: parser.add("array2", "3");
411: long arrayResult2[] = parser.getLongs("array2");
412: long compare2[] = { 1, 0, 3 };
413: assertEquals(arrayResult2.length, compare2.length);
414: for (int i = 0; i < compare2.length; i++) {
415: assertEquals(compare2[i], arrayResult2[i]);
416: }
417: }
418:
419: public void testGetLongObject() {
420: // no param
421: Long result = parser.getLongObject("invalid");
422: assertNull(result);
423:
424: // default
425: result = parser.getLongObject("default", new Long(3));
426: assertEquals(result, new Long(3));
427:
428: // param exists
429: parser.add("exists", "1");
430: result = parser.getLongObject("exists");
431: assertEquals(result, new Long(1));
432:
433: // unparsable value
434: parser.add("unparsable", "a");
435: result = parser.getLongObject("unparsable");
436: assertNull(result);
437:
438: // array
439: parser.add("array", "1");
440: parser.add("array", "2");
441: parser.add("array", "3");
442: Long arrayResult[] = parser.getLongObjects("array");
443: Long compare[] = { new Long(1), new Long(2), new Long(3) };
444: assertEquals(arrayResult.length, compare.length);
445: for (int i = 0; i < compare.length; i++) {
446: assertEquals(compare[i], arrayResult[i]);
447: }
448:
449: // array w/ unparsable element
450: parser.add("array2", "1");
451: parser.add("array2", "a");
452: parser.add("array2", "3");
453: Long arrayResult2[] = parser.getLongObjects("array2");
454: Long compare2[] = { new Long(1), null, new Long(3) };
455: assertEquals(arrayResult2.length, compare2.length);
456: for (int i = 0; i < compare2.length; i++) {
457: assertEquals(compare2[i], arrayResult2[i]);
458: }
459: }
460:
461: public void testGetBoolean() {
462: // no param
463: boolean result = parser.getBoolean("invalid");
464: assertEquals(result, false);
465:
466: // default
467: result = parser.getBoolean("default", true);
468: assertEquals(result, true);
469:
470: // true values - Case is intentional
471: parser.add("true1", "trUe");
472: result = parser.getBoolean("true1");
473: assertEquals(result, true);
474: parser.add("true2", "yEs");
475: result = parser.getBoolean("true2");
476: assertEquals(result, true);
477: parser.add("true3", "1");
478: result = parser.getBoolean("true3");
479: assertEquals(result, true);
480: parser.add("true4", "oN");
481: result = parser.getBoolean("true4");
482: assertEquals(result, true);
483:
484: // unparsable value
485: parser.add("unparsable", "a");
486: result = parser.getBoolean("unparsable");
487: assertEquals(result, false);
488:
489: }
490:
491: public void testGetBooleanObject() {
492: // no param
493: Boolean result = parser.getBooleanObject("invalid");
494: assertNull(result);
495:
496: // default
497: result = parser.getBooleanObject("default", Boolean.TRUE);
498: assertEquals(result, Boolean.TRUE);
499:
500: // true values - Case is intentional
501: parser.add("true1", "trUe");
502: result = parser.getBooleanObject("true1");
503: assertEquals(result, Boolean.TRUE);
504: parser.add("true2", "yEs");
505: result = parser.getBooleanObject("true2");
506: assertEquals(result, Boolean.TRUE);
507: parser.add("true3", "1");
508: result = parser.getBooleanObject("true3");
509: assertEquals(result, Boolean.TRUE);
510: parser.add("true4", "oN");
511: result = parser.getBooleanObject("true4");
512: assertEquals(result, Boolean.TRUE);
513:
514: // false values - Case is intentional
515: parser.add("false1", "falSe");
516: result = parser.getBooleanObject("false1");
517: assertEquals(result, Boolean.FALSE);
518: parser.add("false2", "nO");
519: result = parser.getBooleanObject("false2");
520: assertEquals(result, Boolean.FALSE);
521: parser.add("false3", "0");
522: result = parser.getBooleanObject("false3");
523: assertEquals(result, Boolean.FALSE);
524: parser.add("false4", "oFf");
525: result = parser.getBooleanObject("false4");
526: assertEquals(result, Boolean.FALSE);
527:
528: // unparsable value
529: parser.add("unparsable", "a");
530: result = parser.getBooleanObject("unparsable");
531: assertNull(result);
532: }
533:
534: public void testGetBigDecimal() {
535: // no param
536: BigDecimal result = parser.getBigDecimal("invalid");
537: assertEquals(new BigDecimal(0), result);
538:
539: // default
540: result = parser.getBigDecimal("default", new BigDecimal(3));
541: assertEquals(result, new BigDecimal(3));
542:
543: // param exists
544: parser.add("exists", "1");
545: result = parser.getBigDecimal("exists");
546: assertEquals(result, new BigDecimal(1));
547:
548: // unparsable value
549: parser.add("unparsable", "a");
550: result = parser.getBigDecimal("unparsable");
551: assertEquals(new BigDecimal(0), result);
552:
553: // array
554: parser.add("array", "1");
555: parser.add("array", "2");
556: parser.add("array", "3");
557: BigDecimal arrayResult[] = parser.getBigDecimals("array");
558: BigDecimal compare[] = { new BigDecimal(1), new BigDecimal(2),
559: new BigDecimal(3) };
560: assertEquals(arrayResult.length, compare.length);
561: for (int i = 0; i < compare.length; i++) {
562: assertEquals(compare[i], arrayResult[i]);
563: }
564:
565: // array w/ unparsable element
566: parser.add("array2", "1");
567: parser.add("array2", "a");
568: parser.add("array2", "3");
569: BigDecimal arrayResult2[] = parser.getBigDecimals("array2");
570: BigDecimal compare2[] = { new BigDecimal(1), null,
571: new BigDecimal(3) };
572: assertEquals(arrayResult2.length, compare2.length);
573: for (int i = 0; i < compare2.length; i++) {
574: assertEquals(compare2[i], arrayResult2[i]);
575: }
576: }
577:
578: public void getString() {
579: // no param
580: String result = parser.getString("invalid");
581: assertNull(result);
582:
583: // default
584: result = parser.getString("default", "default");
585: assertEquals(result, "default");
586:
587: // null value
588: parser.add("null", "null");
589: assertNull(parser.getString("null"));
590:
591: // only return the first added
592: parser.add("multiple", "test");
593: parser.add("multiple", "test2");
594: assertEquals("test2", parser.getString("multiple"));
595:
596: // array
597: parser.add("array", "line1");
598: parser.add("array", "line2");
599: parser.add("array", "line3");
600: String arrayResult[] = parser.getStrings("array");
601: String compare[] = { "line1", "line2", "line3" };
602: assertEquals(arrayResult.length, compare.length);
603: for (int i = 0; i < compare.length; i++) {
604: assertEquals(compare[i], arrayResult[i]);
605: }
606:
607: }
608:
609: }
|