001 /*
002 * Copyright 2003-2004 The Apache Software Foundation
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.apache.commons.convert1.string;
017
018 import junit.framework.TestSuite;
019
020 import org.apache.commons.convert1.Converter;
021
022
023 /**
024 * Test Case for the DoubleConverter class.
025 *
026 * @author Rodney Waldhoff
027 * @version $Id: DoubleConverterTestCase.java 155441 2005-02-26 13:19:22Z dirkv $
028 */
029
030 public class DoubleConverterTestCase extends NumberConverterTestBase {
031
032 private Converter converter = null;
033
034 // ------------------------------------------------------------------------
035
036 public DoubleConverterTestCase(String name) {
037 super(name);
038 }
039
040 // ------------------------------------------------------------------------
041
042 public void setUp() throws Exception {
043 converter = makeConverter();
044 }
045
046 public static TestSuite suite() {
047 return new TestSuite(DoubleConverterTestCase.class);
048 }
049
050 public void tearDown() throws Exception {
051 converter = null;
052 }
053
054 // ------------------------------------------------------------------------
055
056 protected Converter makeConverter() {
057 return new DoubleConverter();
058 }
059
060 protected Class getExpectedType() {
061 return Double.class;
062 }
063
064 // ------------------------------------------------------------------------
065
066 public void testSimpleConversion() throws Exception {
067 String[] message= {
068 "from String",
069 "from String",
070 "from String",
071 "from String",
072 "from String",
073 "from String",
074 "from String",
075 "from Byte",
076 "from Short",
077 "from Integer",
078 "from Long",
079 "from Float",
080 "from Double"
081 };
082
083 Object[] input = {
084 String.valueOf(Double.MIN_VALUE),
085 "-17.2",
086 "-1.1",
087 "0.0",
088 "1.1",
089 "17.2",
090 String.valueOf(Double.MAX_VALUE),
091 new Byte((byte)7),
092 new Short((short)8),
093 new Integer(9),
094 new Long(10),
095 new Float(11.1),
096 new Double(12.2)
097 };
098
099 Double[] expected = {
100 new Double(Double.MIN_VALUE),
101 new Double(-17.2),
102 new Double(-1.1),
103 new Double(0.0),
104 new Double(1.1),
105 new Double(17.2),
106 new Double(Double.MAX_VALUE),
107 new Double(7),
108 new Double(8),
109 new Double(9),
110 new Double(10),
111 new Double(11.1),
112 new Double(12.2)
113 };
114
115 for(int i=0;i<expected.length;i++) {
116 assertEquals(
117 message[i] + " to Double",
118 expected[i].doubleValue(),
119 ((Double)(converter.convert(Double.class,input[i]))).doubleValue(),
120 0.00001D);
121 assertEquals(
122 message[i] + " to double",
123 expected[i].doubleValue(),
124 ((Double)(converter.convert(Double.TYPE,input[i]))).doubleValue(),
125 0.00001D);
126 assertEquals(
127 message[i] + " to null type",
128 expected[i].doubleValue(),
129 ((Double)(converter.convert(null,input[i]))).doubleValue(),
130 0.00001D);
131 }
132 }
133
134 }
135