View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.beanutils.converters;
19  
20  import junit.framework.TestSuite;
21  
22  import org.apache.commons.beanutils.Converter;
23  
24  
25  /**
26   * Test Case for the DoubleConverter class.
27   *
28   * @version $Id$
29   */
30  
31  public class DoubleConverterTestCase extends NumberConverterTestBase {
32  
33      private Converter converter = null;
34  
35      // ------------------------------------------------------------------------
36  
37      public DoubleConverterTestCase(final String name) {
38          super(name);
39      }
40  
41      // ------------------------------------------------------------------------
42  
43      @Override
44      public void setUp() throws Exception {
45          converter = makeConverter();
46          numbers[0] = new Double("-12");
47          numbers[1] = new Double("13");
48          numbers[2] = new Double("-22");
49          numbers[3] = new Double("23");
50      }
51  
52      public static TestSuite suite() {
53          return new TestSuite(DoubleConverterTestCase.class);
54      }
55  
56      @Override
57      public void tearDown() throws Exception {
58          converter = null;
59      }
60  
61      // ------------------------------------------------------------------------
62  
63      @Override
64      protected NumberConverter makeConverter() {
65          return new DoubleConverter();
66      }
67  
68      @Override
69      protected NumberConverter makeConverter(final Object defaultValue) {
70          return new DoubleConverter(defaultValue);
71      }
72  
73      @Override
74      protected Class<?> getExpectedType() {
75          return Double.class;
76      }
77  
78      // ------------------------------------------------------------------------
79  
80      public void testSimpleConversion() throws Exception {
81          final String[] message= {
82              "from String",
83              "from String",
84              "from String",
85              "from String",
86              "from String",
87              "from String",
88              "from String",
89              "from Byte",
90              "from Short",
91              "from Integer",
92              "from Long",
93              "from Float",
94              "from Double"
95          };
96  
97          final Object[] input = {
98              String.valueOf(Double.MIN_VALUE),
99              "-17.2",
100             "-1.1",
101             "0.0",
102             "1.1",
103             "17.2",
104             String.valueOf(Double.MAX_VALUE),
105             new Byte((byte)7),
106             new Short((short)8),
107             new Integer(9),
108             new Long(10),
109             new Float(11.1),
110             new Double(12.2)
111         };
112 
113         final Double[] expected = {
114             new Double(Double.MIN_VALUE),
115             new Double(-17.2),
116             new Double(-1.1),
117             new Double(0.0),
118             new Double(1.1),
119             new Double(17.2),
120             new Double(Double.MAX_VALUE),
121             new Double(7),
122             new Double(8),
123             new Double(9),
124             new Double(10),
125             new Double(11.1),
126             new Double(12.2)
127         };
128 
129         for(int i=0;i<expected.length;i++) {
130             assertEquals(
131                 message[i] + " to Double",
132                 expected[i].doubleValue(),
133                 (converter.convert(Double.class,input[i])).doubleValue(),
134                 0.00001D);
135             assertEquals(
136                 message[i] + " to double",
137                 expected[i].doubleValue(),
138                 (converter.convert(Double.TYPE,input[i])).doubleValue(),
139                 0.00001D);
140             assertEquals(
141                 message[i] + " to null type",
142                 expected[i].doubleValue(),
143                 ((Double)(converter.convert(null,input[i]))).doubleValue(),
144                 0.00001D);
145         }
146     }
147 
148 }
149