1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.beanutils.converters;
18
19 import junit.framework.TestCase;
20
21 import org.apache.commons.beanutils.ConversionException;
22 import org.apache.commons.beanutils.Converter;
23
24
25 /**
26 * Abstract base for <Number>Converter classes.
27 *
28 * @author Rodney Waldhoff
29 * @version $Revision: 1.4 $ $Date: 2004/02/28 13:18:37 $
30 */
31
32 public abstract class NumberConverterTestBase extends TestCase {
33
34
35
36 public NumberConverterTestBase(String name) {
37 super(name);
38 }
39
40
41
42 protected abstract Converter makeConverter();
43 protected abstract Class getExpectedType();
44
45
46
47 /**
48 * Assumes ConversionException in response to covert(getExpectedType(),null).
49 */
50 public void testConvertNull() throws Exception {
51 try {
52 makeConverter().convert(getExpectedType(),null);
53 fail("Expected ConversionException");
54 } catch(ConversionException e) {
55
56 }
57 }
58
59 /**
60 * Assumes convert(getExpectedType(),Number) returns some non-null
61 * instance of getExpectedType().
62 */
63 public void testConvertNumber() throws Exception {
64 String[] message= {
65 "from Byte",
66 "from Short",
67 "from Integer",
68 "from Long",
69 "from Float",
70 "from Double"
71 };
72
73 Object[] number = {
74 new Byte((byte)7),
75 new Short((short)8),
76 new Integer(9),
77 new Long(10),
78 new Float(11.1),
79 new Double(12.2)
80 };
81
82 for(int i=0;i<number.length;i++) {
83 Object val = makeConverter().convert(getExpectedType(),number[i]);
84 assertNotNull("Convert " + message[i] + " should not be null",val);
85 assertTrue(
86 "Convert " + message[i] + " should return a " + getExpectedType().getName(),
87 getExpectedType().isInstance(val));
88 }
89 }
90 }
91