001: package org.geotools.util;
002:
003: import java.math.BigDecimal;
004: import java.math.BigInteger;
005:
006: import junit.framework.TestCase;
007:
008: public class NumericConverterFactoryTest extends TestCase {
009:
010: NumericConverterFactory factory;
011:
012: protected void setUp() throws Exception {
013: factory = new NumericConverterFactory();
014: }
015:
016: public void testIntegral() throws Exception {
017:
018: //to byte
019: assertEquals(new Byte((byte) 127), convert(
020: new Byte((byte) 127), Byte.class));
021: assertEquals(new Byte((byte) 127), convert(new Short(
022: (short) 127), Byte.class));
023: assertEquals(new Byte((byte) 127), convert(new Integer(127),
024: Byte.class));
025: assertEquals(new Byte((byte) 127), convert(new Long(127),
026: Byte.class));
027: assertEquals(new Byte((byte) 127), convert(BigInteger
028: .valueOf(127), Byte.class));
029:
030: //to short
031: assertEquals(new Short((short) 127), convert(new Byte(
032: (byte) 127), Short.class));
033: assertEquals(new Short((short) 127), convert(new Short(
034: (short) 127), Short.class));
035: assertEquals(new Short((short) 127), convert(new Integer(127),
036: Short.class));
037: assertEquals(new Short((short) 127), convert(new Long(127),
038: Short.class));
039: assertEquals(new Short((short) 127), convert(BigInteger
040: .valueOf(127), Short.class));
041:
042: //to integer
043: assertEquals(new Integer(127), convert(new Byte((byte) 127),
044: Integer.class));
045: assertEquals(new Integer(127), convert(new Short((short) 127),
046: Integer.class));
047: assertEquals(new Integer(127), convert(new Integer(127),
048: Integer.class));
049: assertEquals(new Integer(127), convert(new Long(127),
050: Integer.class));
051: assertEquals(new Integer(127), convert(BigInteger.valueOf(127),
052: Integer.class));
053:
054: //to long
055: assertEquals(new Long(127), convert(new Byte((byte) 127),
056: Long.class));
057: assertEquals(new Long(127), convert(new Short((short) 127),
058: Long.class));
059: assertEquals(new Long(127), convert(new Integer(127),
060: Long.class));
061: assertEquals(new Long(127), convert(new Long(127), Long.class));
062: assertEquals(new Long(127), convert(BigInteger.valueOf(127),
063: Long.class));
064:
065: //to big integer
066: assertEquals(BigInteger.valueOf(127), convert(new Byte(
067: (byte) 127), BigInteger.class));
068: assertEquals(BigInteger.valueOf(127), convert(new Short(
069: (short) 127), BigInteger.class));
070: assertEquals(BigInteger.valueOf(127), convert(new Integer(127),
071: BigInteger.class));
072: assertEquals(BigInteger.valueOf(127), convert(new Long(127),
073: BigInteger.class));
074: assertEquals(BigInteger.valueOf(127), convert(BigInteger
075: .valueOf(127), BigInteger.class));
076: }
077:
078: public void testFloat() throws Exception {
079: //to float
080: assertEquals(new Float(127.127), convert(new Float(127.127),
081: Float.class));
082: assertEquals(new Float(127.127), convert(new Double(127.127),
083: Float.class));
084: assertEquals(new Float(127.127), convert(
085: new BigDecimal(127.127), Float.class));
086:
087: //to double
088: assertEquals(new Double(127.127).doubleValue(),
089: ((Double) convert(new Float(127.127), Double.class))
090: .doubleValue(), 0.001);
091: assertEquals(new Double(127.127), convert(new Double(127.127),
092: Double.class));
093: assertEquals(new Double(127.127), convert(new BigDecimal(
094: 127.127), Double.class));
095:
096: //to big decimal
097: assertEquals(new BigDecimal(127.127).doubleValue(),
098: ((BigDecimal) convert(new Float(127.127),
099: BigDecimal.class)).doubleValue(), 0.001);
100: assertEquals(new BigDecimal(127.127), convert(new Double(
101: 127.127), BigDecimal.class));
102: assertEquals(new BigDecimal(127.127), convert(new BigDecimal(
103: 127.127), BigDecimal.class));
104: }
105:
106: public void testIntegralToFloat() throws Exception {
107: assertEquals(new Float(127.0), convert(new Byte((byte) 127),
108: Float.class));
109: assertEquals(new Float(127.0), convert(new Short((short) 127),
110: Float.class));
111: assertEquals(new Float(127.0), convert(new Integer(127),
112: Float.class));
113: assertEquals(new Float(127.0), convert(new Long(127),
114: Float.class));
115: assertEquals(new Float(127.0), convert(BigInteger.valueOf(127),
116: Float.class));
117:
118: assertEquals(new Double(127.0), convert(new Byte((byte) 127),
119: Double.class));
120: assertEquals(new Double(127.0), convert(new Short((short) 127),
121: Double.class));
122: assertEquals(new Double(127.0), convert(new Integer(127),
123: Double.class));
124: assertEquals(new Double(127.0), convert(new Long(127),
125: Double.class));
126: assertEquals(new Double(127.0), convert(
127: BigInteger.valueOf(127), Double.class));
128:
129: assertEquals(new BigDecimal(127.0), convert(
130: new Byte((byte) 127), BigDecimal.class));
131: assertEquals(new BigDecimal(127.0), convert(new Short(
132: (short) 127), BigDecimal.class));
133: assertEquals(new BigDecimal(127.0), convert(new Integer(127),
134: BigDecimal.class));
135: assertEquals(new BigDecimal(127.0), convert(new Long(127),
136: BigDecimal.class));
137: assertEquals(new BigDecimal(127.0), convert(BigInteger
138: .valueOf(127), BigDecimal.class));
139: }
140:
141: public void testFloatToIntegral() throws Exception {
142: //to byte
143: assertEquals(new Byte((byte) 127), convert(new Float(127.127),
144: Byte.class));
145: assertEquals(new Byte((byte) 127), convert(new Double(127.127),
146: Byte.class));
147: assertEquals(new Byte((byte) 127), convert(new BigDecimal(
148: 127.127), Byte.class));
149:
150: //to short
151: assertEquals(new Short((short) 127), convert(
152: new Float(127.127), Short.class));
153: assertEquals(new Short((short) 127), convert(
154: new Double(127.127), Short.class));
155: assertEquals(new Short((short) 127), convert(new BigDecimal(
156: 127.127), Short.class));
157:
158: //to integer
159: assertEquals(new Integer(127), convert(new Float(127.127),
160: Integer.class));
161: assertEquals(new Integer(127), convert(new Double(127.127),
162: Integer.class));
163: assertEquals(new Integer(127), convert(new BigDecimal(127.127),
164: Integer.class));
165:
166: //to long
167: assertEquals(new Long(127), convert(new Float(127.127),
168: Long.class));
169: assertEquals(new Long(127), convert(new Double(127.127),
170: Long.class));
171: assertEquals(new Long(127), convert(new BigDecimal(127.127),
172: Long.class));
173:
174: //to big integer
175: assertEquals(BigInteger.valueOf(127), convert(
176: new Float(127.127), BigInteger.class));
177: assertEquals(BigInteger.valueOf(127), convert(new Double(
178: 127.127), BigInteger.class));
179: assertEquals(BigInteger.valueOf(127), convert(new BigDecimal(
180: 127.127), BigInteger.class));
181:
182: }
183:
184: Object convert(Object source, Class target) throws Exception {
185: return factory.createConverter(source.getClass(), target, null)
186: .convert(source, target);
187: }
188: }
|