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