KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > beanutils > converters > NumberConverterTestBase


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
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 JavaDoc name) {
37         super(name);
38     }
39     
40     // ------------------------------------------------------------------------
41

42     protected abstract Converter makeConverter();
43     protected abstract Class JavaDoc getExpectedType();
44
45     // ------------------------------------------------------------------------
46

47     /**
48      * Assumes ConversionException in response to covert(getExpectedType(),null).
49      */

50     public void testConvertNull() throws Exception JavaDoc {
51         try {
52             makeConverter().convert(getExpectedType(),null);
53             fail("Expected ConversionException");
54         } catch(ConversionException e) {
55             // expected
56
}
57     }
58
59     /**
60      * Assumes convert(getExpectedType(),Number) returns some non-null
61      * instance of getExpectedType().
62      */

63     public void testConvertNumber() throws Exception JavaDoc {
64         String JavaDoc[] message= {
65             "from Byte",
66             "from Short",
67             "from Integer",
68             "from Long",
69             "from Float",
70             "from Double"
71         };
72
73         Object JavaDoc[] number = {
74             new Byte JavaDoc((byte)7),
75             new Short JavaDoc((short)8),
76             new Integer JavaDoc(9),
77             new Long JavaDoc(10),
78             new Float JavaDoc(11.1),
79             new Double JavaDoc(12.2)
80         };
81
82         for(int i=0;i<number.length;i++) {
83             Object JavaDoc 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
92
Popular Tags