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 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              // 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 {
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