KickJava   Java API By Example, From Geeks To Geeks.

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


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.TestSuite;
20
21 import org.apache.commons.beanutils.Converter;
22
23
24 /**
25  * Test Case for the DoubleConverter class.
26  *
27  * @author Rodney Waldhoff
28  * @version $Revision: 1.4 $ $Date: 2004/02/28 13:18:37 $
29  */

30
31 public class DoubleConverterTestCase extends NumberConverterTestBase {
32
33     private Converter converter = null;
34
35     // ------------------------------------------------------------------------
36

37     public DoubleConverterTestCase(String JavaDoc name) {
38         super(name);
39     }
40     
41     // ------------------------------------------------------------------------
42

43     public void setUp() throws Exception JavaDoc {
44         converter = makeConverter();
45     }
46
47     public static TestSuite suite() {
48         return new TestSuite(DoubleConverterTestCase.class);
49     }
50
51     public void tearDown() throws Exception JavaDoc {
52         converter = null;
53     }
54
55     // ------------------------------------------------------------------------
56

57     protected Converter makeConverter() {
58         return new DoubleConverter();
59     }
60     
61     protected Class JavaDoc getExpectedType() {
62         return Double JavaDoc.class;
63     }
64
65     // ------------------------------------------------------------------------
66

67     public void testSimpleConversion() throws Exception JavaDoc {
68         String JavaDoc[] message= {
69             "from String",
70             "from String",
71             "from String",
72             "from String",
73             "from String",
74             "from String",
75             "from String",
76             "from Byte",
77             "from Short",
78             "from Integer",
79             "from Long",
80             "from Float",
81             "from Double"
82         };
83         
84         Object JavaDoc[] input = {
85             String.valueOf(Double.MIN_VALUE),
86             "-17.2",
87             "-1.1",
88             "0.0",
89             "1.1",
90             "17.2",
91             String.valueOf(Double.MAX_VALUE),
92             new Byte JavaDoc((byte)7),
93             new Short JavaDoc((short)8),
94             new Integer JavaDoc(9),
95             new Long JavaDoc(10),
96             new Float JavaDoc(11.1),
97             new Double JavaDoc(12.2)
98         };
99         
100         Double JavaDoc[] expected = {
101             new Double JavaDoc(Double.MIN_VALUE),
102             new Double JavaDoc(-17.2),
103             new Double JavaDoc(-1.1),
104             new Double JavaDoc(0.0),
105             new Double JavaDoc(1.1),
106             new Double JavaDoc(17.2),
107             new Double JavaDoc(Double.MAX_VALUE),
108             new Double JavaDoc(7),
109             new Double JavaDoc(8),
110             new Double JavaDoc(9),
111             new Double JavaDoc(10),
112             new Double JavaDoc(11.1),
113             new Double JavaDoc(12.2)
114         };
115         
116         for(int i=0;i<expected.length;i++) {
117             assertEquals(
118                 message[i] + " to Double",
119                 expected[i].doubleValue(),
120                 ((Double JavaDoc)(converter.convert(Double JavaDoc.class,input[i]))).doubleValue(),
121                 0.00001D);
122             assertEquals(
123                 message[i] + " to double",
124                 expected[i].doubleValue(),
125                 ((Double JavaDoc)(converter.convert(Double.TYPE,input[i]))).doubleValue(),
126                 0.00001D);
127             assertEquals(
128                 message[i] + " to null type",
129                 expected[i].doubleValue(),
130                 ((Double JavaDoc)(converter.convert(null,input[i]))).doubleValue(),
131                 0.00001D);
132         }
133     }
134     
135 }
136
137
Popular Tags