1   /*
2    *  Copyright 2003-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  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   * Abstract base for <Number>Converter classes.
26   *
27   * @author Rodney Waldhoff
28   * @version $Id: NumberConverterTestBase.java 155441 2005-02-26 13:19:22Z dirkv $
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       * Assumes ConversionException in response to covert(getExpectedType(),null).
48       */
49      public void testConvertNull() throws Exception {
50          try {
51              makeConverter().convert(getExpectedType(),null);
52              fail("Expected ConversionException");
53          } catch(ConversionException e) {
54              // expected
55          }
56      }
57  
58      /**
59       * Assumes convert(getExpectedType(),Number) returns some non-null
60       * instance of getExpectedType().
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