001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.beanutils.locale;
019:
020: import java.sql.Date;
021: import java.sql.Time;
022: import java.sql.Timestamp;
023: import java.text.DecimalFormat;
024: import java.text.NumberFormat;
025:
026: import junit.framework.TestCase;
027: import junit.framework.Test;
028: import junit.framework.TestSuite;
029: import org.apache.commons.beanutils.ConversionException;
030:
031: /**
032: * <p>
033: * Test Case for the LocaleConvertUtils class.
034: * See unimplemented functionality of the convert utils in the method begining with fixme
035: * </p>
036: *
037: * @author Michael Szlapa
038: * @author Paul Hamamnt & Rune Johannesen (pairing) - patches.
039: * @version $Revision: 469743 $ $Date: 2006-11-01 01:27:40 +0000 (Wed, 01 Nov 2006) $
040: */
041:
042: public class LocaleConvertUtilsTestCase extends TestCase {
043:
044: // ---------------------------------------------------- Instance Variables
045:
046: private char m_decimalSeparator;
047:
048: // ---------------------------------------------------------- Constructors
049:
050: /**
051: * Construct a new instance of this test case.
052: *
053: * @param name Name of the test case
054: */
055: public LocaleConvertUtilsTestCase(String name) {
056: super (name);
057: }
058:
059: // -------------------------------------------------- Overall Test Methods
060:
061: /**
062: * Set up instance variables required by this test case.
063: */
064: public void setUp() {
065:
066: LocaleConvertUtils.deregister();
067:
068: NumberFormat nf = DecimalFormat.getNumberInstance();
069: String result = nf.format(1.1);
070:
071: // could be commas instead of stops in Europe.
072: m_decimalSeparator = result.charAt(1);
073:
074: }
075:
076: /**
077: * Return the tests included in this test suite.
078: */
079: public static Test suite() {
080: return (new TestSuite(LocaleConvertUtilsTestCase.class));
081: }
082:
083: /**
084: * Tear down instance variables required by this test case.
085: */
086: public void tearDown() {
087: // No action required
088: }
089:
090: // ------------------------------------------------ Individual Test Methods
091:
092: /**
093: * Negative String to primitive integer array tests.
094: */
095: public void fixmetestNegativeIntegerArray() {
096:
097: fail("Array conversions not implemented yet.");
098:
099: Object value = null;
100: int intArray[] = new int[0];
101:
102: value = LocaleConvertUtils.convert((String) null, intArray
103: .getClass());
104: checkIntegerArray(value, intArray);
105: value = LocaleConvertUtils.convert("a", intArray.getClass());
106: checkIntegerArray(value, intArray);
107: value = LocaleConvertUtils
108: .convert("{ a }", intArray.getClass());
109: checkIntegerArray(value, intArray);
110: value = LocaleConvertUtils.convert("1a3", intArray.getClass());
111: checkIntegerArray(value, intArray);
112: value = LocaleConvertUtils.convert("{ 1a3 }", intArray
113: .getClass());
114: checkIntegerArray(value, intArray);
115: value = LocaleConvertUtils
116: .convert("0,1a3", intArray.getClass());
117: checkIntegerArray(value, intArray);
118: value = LocaleConvertUtils.convert("{ 0, 1a3 }", intArray
119: .getClass());
120: checkIntegerArray(value, intArray);
121:
122: }
123:
124: /**
125: * Negative scalar conversion tests. These rely on the standard
126: * default value conversions in LocaleConvertUtils.
127: */
128: public void testNegativeScalar() {
129:
130: Object value = null;
131:
132: /* fixme Boolean converters not implemented at this point
133: value = LocaleConvertUtils.convert("foo", Boolean.TYPE);
134: ...
135:
136: value = LocaleConvertUtils.convert("foo", Boolean.class);
137: ...
138: */
139:
140: try {
141: value = LocaleConvertUtils.convert("foo", Byte.TYPE);
142: fail("Should have thrown conversion exception (1)");
143: } catch (ConversionException e) {
144: // Expected result
145: }
146:
147: try {
148: value = LocaleConvertUtils.convert("foo", Byte.class);
149: fail("Should have thrown conversion exception (2)");
150: } catch (ConversionException e) {
151: // Expected result
152: }
153:
154: /* fixme - not implemented
155: try {
156: value = LocaleConvertUtils.convert("org.apache.commons.beanutils.Undefined", Class.class);
157: fail("Should have thrown conversion exception");
158: } catch (ConversionException e) {
159: ; // Expected result
160: }
161: */
162:
163: try {
164: value = LocaleConvertUtils.convert("foo", Double.TYPE);
165: fail("Should have thrown conversion exception (3)");
166: } catch (ConversionException e) {
167: // Expected result
168: }
169:
170: try {
171: value = LocaleConvertUtils.convert("foo", Double.class);
172: fail("Should have thrown conversion exception (4)");
173: } catch (ConversionException e) {
174: // Expected result
175: }
176:
177: try {
178: value = LocaleConvertUtils.convert("foo", Float.TYPE);
179: fail("Should have thrown conversion exception (5)");
180: } catch (ConversionException e) {
181: // Expected result
182: }
183:
184: try {
185: value = LocaleConvertUtils.convert("foo", Float.class);
186: fail("Should have thrown conversion exception (6)");
187: } catch (ConversionException e) {
188: // Expected result
189: }
190:
191: try {
192: value = LocaleConvertUtils.convert("foo", Integer.TYPE);
193: fail("Should have thrown conversion exception (7)");
194: } catch (ConversionException e) {
195: // Expected result
196: }
197:
198: try {
199: value = LocaleConvertUtils.convert("foo", Integer.class);
200: fail("Should have thrown conversion exception (8)");
201: } catch (ConversionException e) {
202: // Expected result
203: }
204:
205: try {
206: value = LocaleConvertUtils.convert("foo", Byte.TYPE);
207: fail("Should have thrown conversion exception (9)");
208: } catch (ConversionException e) {
209: // Expected result
210: }
211:
212: try {
213: value = LocaleConvertUtils.convert("foo", Long.class);
214: fail("Should have thrown conversion exception (10)");
215: } catch (ConversionException e) {
216: // Expected result
217: }
218:
219: try {
220: value = LocaleConvertUtils.convert("foo", Short.TYPE);
221: fail("Should have thrown conversion exception (11)");
222: } catch (ConversionException e) {
223: // Expected result
224: }
225:
226: try {
227: value = LocaleConvertUtils.convert("foo", Short.class);
228: fail("Should have thrown conversion exception (12)");
229: } catch (ConversionException e) {
230: // Expected result
231: }
232:
233: }
234:
235: /**
236: * Negative String to String array tests.
237: */
238: public void fixmetestNegativeStringArray() {
239:
240: fail("Array conversions not implemented yet.");
241:
242: Object value = null;
243: String stringArray[] = new String[0];
244:
245: value = LocaleConvertUtils.convert((String) null, stringArray
246: .getClass());
247: checkStringArray(value, stringArray);
248: }
249:
250: /**
251: * Test conversion of object to string for arrays - .
252: */
253: public void fixmetestObjectToStringArray() {
254:
255: fail("Array conversions not implemented yet.");
256: int intArray0[] = new int[0];
257: int intArray1[] = { 123 };
258: int intArray2[] = { 123, 456 };
259: String stringArray0[] = new String[0];
260: String stringArray1[] = { "abc" };
261: String stringArray2[] = { "abc", "def" };
262:
263: assertEquals("intArray0", null, LocaleConvertUtils
264: .convert(intArray0));
265: assertEquals("intArray1", "123", LocaleConvertUtils
266: .convert(intArray1));
267: assertEquals("intArray2", "123", LocaleConvertUtils
268: .convert(intArray2));
269:
270: assertEquals("stringArray0", null, LocaleConvertUtils
271: .convert(stringArray0));
272: assertEquals("stringArray1", "abc", LocaleConvertUtils
273: .convert(stringArray1));
274: assertEquals("stringArray2", "abc", LocaleConvertUtils
275: .convert(stringArray2));
276:
277: }
278:
279: /**
280: * Test conversion of object to string for scalars.
281: */
282: public void testObjectToStringScalar() {
283:
284: assertEquals("Boolean->String", "false", LocaleConvertUtils
285: .convert(Boolean.FALSE));
286: assertEquals("Boolean->String", "true", LocaleConvertUtils
287: .convert(Boolean.TRUE));
288: assertEquals("Byte->String", "123", LocaleConvertUtils
289: .convert(new Byte((byte) 123)));
290: assertEquals("Character->String", "a", LocaleConvertUtils
291: .convert(new Character('a')));
292: assertEquals("Double->String",
293: "123" + m_decimalSeparator + "4", LocaleConvertUtils
294: .convert(new Double(123.4)));
295: assertEquals("Float->String", "123" + m_decimalSeparator + "4",
296: LocaleConvertUtils.convert(new Float((float) 123.4)));
297: assertEquals("Integer->String", "123", LocaleConvertUtils
298: .convert(new Integer(123)));
299: assertEquals("Long->String", "123", LocaleConvertUtils
300: .convert(new Long(123)));
301: assertEquals("Short->String", "123", LocaleConvertUtils
302: .convert(new Short((short) 123)));
303: assertEquals("String->String", "abc", LocaleConvertUtils
304: .convert("abc"));
305: assertEquals("String->String null", null, LocaleConvertUtils
306: .convert(null));
307:
308: }
309:
310: /**
311: * Positive array conversion tests.
312: */
313: public void fixmetestPositiveArray() {
314:
315: fail("Array conversions not implemented yet.");
316:
317: String values1[] = { "10", "20", "30" };
318: Object value = LocaleConvertUtils
319: .convert(values1, Integer.TYPE);
320: int shape[] = new int[0];
321: assertEquals(shape.getClass(), value.getClass());
322: int results1[] = (int[]) value;
323: assertEquals(results1[0], 10);
324: assertEquals(results1[1], 20);
325: assertEquals(results1[2], 30);
326:
327: String values2[] = { "100", "200", "300" };
328: value = LocaleConvertUtils.convert(values2, shape.getClass());
329: assertEquals(shape.getClass(), value.getClass());
330: int results2[] = (int[]) value;
331: assertEquals(results2[0], 100);
332: assertEquals(results2[1], 200);
333: assertEquals(results2[2], 300);
334: }
335:
336: /**
337: * Positive String to primitive integer array tests.
338: */
339: public void fixmetestPositiveIntegerArray() {
340:
341: fail("Array conversions not implemented yet.");
342:
343: Object value = null;
344: int intArray[] = new int[0];
345: int intArray1[] = new int[] { 0 };
346: int intArray2[] = new int[] { 0, 10 };
347:
348: value = LocaleConvertUtils.convert("{ }", intArray.getClass());
349: checkIntegerArray(value, intArray);
350:
351: value = LocaleConvertUtils.convert("0", intArray.getClass());
352: checkIntegerArray(value, intArray1);
353: value = LocaleConvertUtils.convert(" 0 ", intArray.getClass());
354: checkIntegerArray(value, intArray1);
355: value = LocaleConvertUtils
356: .convert("{ 0 }", intArray.getClass());
357: checkIntegerArray(value, intArray1);
358:
359: value = LocaleConvertUtils.convert("0,10", intArray.getClass());
360: checkIntegerArray(value, intArray2);
361: value = LocaleConvertUtils.convert("0 10", intArray.getClass());
362: checkIntegerArray(value, intArray2);
363: value = LocaleConvertUtils.convert("{0,10}", intArray
364: .getClass());
365: checkIntegerArray(value, intArray2);
366: value = LocaleConvertUtils.convert("{0 10}", intArray
367: .getClass());
368: checkIntegerArray(value, intArray2);
369: value = LocaleConvertUtils.convert("{ 0, 10 }", intArray
370: .getClass());
371: checkIntegerArray(value, intArray2);
372: value = LocaleConvertUtils.convert("{ 0 10 }", intArray
373: .getClass());
374: checkIntegerArray(value, intArray2);
375: }
376:
377: /**
378: * Positive scalar conversion tests.
379: */
380: public void testPositiveScalar() {
381: Object value = null;
382:
383: /* fixme Boolean converters not implemented
384: value = LocaleConvertUtils.convert("true", Boolean.TYPE);
385: assertTrue(value instanceof Boolean);
386: assertEquals(((Boolean) value).booleanValue(), true);
387:
388: value = LocaleConvertUtils.convert("true", Boolean.class);
389: assertTrue(value instanceof Boolean);
390: assertEquals(((Boolean) value).booleanValue(), true);
391:
392: value = LocaleConvertUtils.convert("yes", Boolean.TYPE);
393: assertTrue(value instanceof Boolean);
394: assertEquals(((Boolean) value).booleanValue(), true);
395:
396: value = LocaleConvertUtils.convert("yes", Boolean.class);
397: assertTrue(value instanceof Boolean);
398: assertEquals(((Boolean) value).booleanValue(), true);
399:
400: value = LocaleConvertUtils.convert("y", Boolean.TYPE);
401: assertTrue(value instanceof Boolean);
402: assertEquals(((Boolean) value).booleanValue(), true);
403:
404: value = LocaleConvertUtils.convert("y", Boolean.class);
405: assertTrue(value instanceof Boolean);
406: assertEquals(((Boolean) value).booleanValue(), true);
407:
408: value = LocaleConvertUtils.convert("on", Boolean.TYPE);
409: assertTrue(value instanceof Boolean);
410: assertEquals(((Boolean) value).booleanValue(), true);
411:
412: value = LocaleConvertUtils.convert("on", Boolean.class);
413: assertTrue(value instanceof Boolean);
414: assertEquals(((Boolean) value).booleanValue(), true);
415:
416: value = LocaleConvertUtils.convert("false", Boolean.TYPE);
417: assertTrue(value instanceof Boolean);
418: assertEquals(((Boolean) value).booleanValue(), false);
419:
420: value = LocaleConvertUtils.convert("false", Boolean.class);
421: assertTrue(value instanceof Boolean);
422: assertEquals(((Boolean) value).booleanValue(), false);
423:
424: value = LocaleConvertUtils.convert("no", Boolean.TYPE);
425: assertTrue(value instanceof Boolean);
426: assertEquals(((Boolean) value).booleanValue(), false);
427:
428: value = LocaleConvertUtils.convert("no", Boolean.class);
429: assertTrue(value instanceof Boolean);
430: assertEquals(((Boolean) value).booleanValue(), false);
431:
432: value = LocaleConvertUtils.convert("n", Boolean.TYPE);
433: assertTrue(value instanceof Boolean);
434: assertEquals(((Boolean) value).booleanValue(), false);
435:
436: value = LocaleConvertUtils.convert("n", Boolean.class);
437: assertTrue(value instanceof Boolean);
438: assertEquals(((Boolean) value).booleanValue(), false);
439:
440: value = LocaleConvertUtils.convert("off", Boolean.TYPE);
441: assertTrue(value instanceof Boolean);
442: assertEquals(((Boolean) value).booleanValue(), false);
443:
444: value = LocaleConvertUtils.convert("off", Boolean.class);
445: assertTrue(value instanceof Boolean);
446: assertEquals(((Boolean) value).booleanValue(), false);
447: */
448:
449: value = LocaleConvertUtils.convert("123", Byte.TYPE);
450: assertTrue(value instanceof Byte);
451: assertEquals(((Byte) value).byteValue(), (byte) 123);
452:
453: value = LocaleConvertUtils.convert("123", Byte.class);
454: assertTrue(value instanceof Byte);
455: assertEquals(((Byte) value).byteValue(), (byte) 123);
456:
457: /*fixme Character conversion not implemented yet
458: value = LocaleConvertUtils.convert("a", Character.TYPE);
459: assertTrue(value instanceof Character);
460: assertEquals(((Character) value).charValue(), 'a');
461:
462: value = LocaleConvertUtils.convert("a", Character.class);
463: assertTrue(value instanceof Character);
464: assertEquals(((Character) value).charValue(), 'a');
465: */
466: /* fixme - this is a discrepancy with standard converters ( probably not major issue )
467: value = LocaleConvertUtils.convert("java.lang.String", Class.class);
468: assertTrue(value instanceof Class);
469: assertEquals(String.class, (Class) value);
470: */
471:
472: value = LocaleConvertUtils.convert("123" + m_decimalSeparator
473: + "456", Double.TYPE);
474: assertTrue(value instanceof Double);
475: assertEquals(((Double) value).doubleValue(), 123.456, 0.005);
476:
477: value = LocaleConvertUtils.convert("123" + m_decimalSeparator
478: + "456", Double.class);
479: assertTrue(value instanceof Double);
480: assertEquals(((Double) value).doubleValue(), 123.456, 0.005);
481:
482: value = LocaleConvertUtils.convert("123" + m_decimalSeparator
483: + "456", Float.TYPE);
484: assertTrue(value instanceof Float);
485: assertEquals(((Float) value).floatValue(), (float) 123.456,
486: (float) 0.005);
487:
488: value = LocaleConvertUtils.convert("123" + m_decimalSeparator
489: + "456", Float.class);
490: assertTrue(value instanceof Float);
491: assertEquals(((Float) value).floatValue(), (float) 123.456,
492: (float) 0.005);
493:
494: value = LocaleConvertUtils.convert("123", Integer.TYPE);
495: assertTrue(value instanceof Integer);
496: assertEquals(((Integer) value).intValue(), 123);
497:
498: value = LocaleConvertUtils.convert("123", Integer.class);
499: assertTrue(value instanceof Integer);
500: assertEquals(((Integer) value).intValue(), 123);
501:
502: value = LocaleConvertUtils.convert("123", Long.TYPE);
503: assertTrue(value instanceof Long);
504: assertEquals(((Long) value).longValue(), 123);
505:
506: value = LocaleConvertUtils.convert("123456", Long.class);
507: assertTrue(value instanceof Long);
508: assertEquals(((Long) value).longValue(), 123456);
509:
510: /* fixme - Short conversion not implemented at this point
511: value = LocaleConvertUtils.convert("123", Short.TYPE);
512: assertTrue(value instanceof Short);
513: assertEquals(((Short) value).shortValue(), (short) 123);
514:
515: value = LocaleConvertUtils.convert("123", Short.class);
516: assertTrue(value instanceof Short);
517: assertEquals(((Short) value).shortValue(), (short) 123);
518: */
519:
520: String input = null;
521:
522: input = "2002-03-17";
523: value = LocaleConvertUtils.convert(input, Date.class);
524: assertTrue(value instanceof Date);
525: assertEquals(input, value.toString());
526:
527: input = "20:30:40";
528: value = LocaleConvertUtils.convert(input, Time.class);
529: assertTrue(value instanceof Time);
530: assertEquals(input, value.toString());
531:
532: input = "2002-03-17 20:30:40.0";
533: value = LocaleConvertUtils.convert(input, Timestamp.class);
534: assertTrue(value instanceof Timestamp);
535: assertEquals(input, value.toString());
536:
537: }
538:
539: /**
540: * Positive String to String array tests.
541: */
542: public void fixmetestPositiveStringArray() {
543:
544: fail("Array conversions not implemented yet.");
545:
546: Object value = null;
547: String stringArray[] = new String[0];
548: String stringArray1[] = new String[] { "abc" };
549: String stringArray2[] = new String[] { "abc", "de,f" };
550:
551: value = LocaleConvertUtils.convert("", stringArray.getClass());
552: checkStringArray(value, stringArray);
553: value = LocaleConvertUtils.convert(" ", stringArray.getClass());
554: checkStringArray(value, stringArray);
555: value = LocaleConvertUtils
556: .convert("{}", stringArray.getClass());
557: checkStringArray(value, stringArray);
558: value = LocaleConvertUtils.convert("{ }", stringArray
559: .getClass());
560: checkStringArray(value, stringArray);
561:
562: value = LocaleConvertUtils.convert("abc", stringArray
563: .getClass());
564: checkStringArray(value, stringArray1);
565: value = LocaleConvertUtils.convert("{abc}", stringArray
566: .getClass());
567: checkStringArray(value, stringArray1);
568: value = LocaleConvertUtils.convert("\"abc\"", stringArray
569: .getClass());
570: checkStringArray(value, stringArray1);
571: value = LocaleConvertUtils.convert("{\"abc\"}", stringArray
572: .getClass());
573: checkStringArray(value, stringArray1);
574: value = LocaleConvertUtils.convert("'abc'", stringArray
575: .getClass());
576: checkStringArray(value, stringArray1);
577: value = LocaleConvertUtils.convert("{'abc'}", stringArray
578: .getClass());
579: checkStringArray(value, stringArray1);
580:
581: value = LocaleConvertUtils.convert("abc 'de,f'", stringArray
582: .getClass());
583: checkStringArray(value, stringArray2);
584: value = LocaleConvertUtils.convert("{abc, 'de,f'}", stringArray
585: .getClass());
586: checkStringArray(value, stringArray2);
587: value = LocaleConvertUtils.convert("\"abc\",\"de,f\"",
588: stringArray.getClass());
589: checkStringArray(value, stringArray2);
590: value = LocaleConvertUtils.convert("{\"abc\" 'de,f'}",
591: stringArray.getClass());
592: checkStringArray(value, stringArray2);
593: value = LocaleConvertUtils.convert("'abc' 'de,f'", stringArray
594: .getClass());
595: checkStringArray(value, stringArray2);
596: value = LocaleConvertUtils.convert("{'abc', \"de,f\"}",
597: stringArray.getClass());
598: checkStringArray(value, stringArray2);
599:
600: }
601:
602: // -------------------------------------------------------- Private Methods
603:
604: private void checkIntegerArray(Object value, int intArray[]) {
605:
606: assertNotNull("Returned value is not null", value);
607: assertEquals("Returned value is int[]", intArray.getClass(),
608: value.getClass());
609: int results[] = (int[]) value;
610: assertEquals("Returned array length", intArray.length,
611: results.length);
612: for (int i = 0; i < intArray.length; i++) {
613: assertEquals("Returned array value " + i, intArray[i],
614: results[i]);
615: }
616:
617: }
618:
619: private void checkStringArray(Object value, String stringArray[]) {
620:
621: assertNotNull("Returned value is not null", value);
622: assertEquals("Returned value is String[]", stringArray
623: .getClass(), value.getClass());
624: String results[] = (String[]) value;
625: assertEquals("Returned array length", stringArray.length,
626: results.length);
627: for (int i = 0; i < stringArray.length; i++) {
628: assertEquals("Returned array value " + i, stringArray[i],
629: results[i]);
630: }
631:
632: }
633:
634: }
|