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