KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > dataset > datatype > NumberDataTypeTest


1 /*
2  *
3  * The DbUnit Database Testing Framework
4  * Copyright (C)2002-2004, DbUnit.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */

21
22 package org.dbunit.dataset.datatype;
23
24 import org.dbunit.database.ExtendedMockSingleRowResultSet;
25 import org.dbunit.dataset.ITable;
26
27 import java.math.BigDecimal JavaDoc;
28 import java.math.BigInteger JavaDoc;
29 import java.sql.Types JavaDoc;
30
31 /**
32  * @author Manuel Laflamme
33  * @version $Revision: 1.14 $
34  */

35
36 public class NumberDataTypeTest extends AbstractDataTypeTest
37 {
38     private final static DataType[] TYPES = {DataType.NUMERIC, DataType.DECIMAL};
39
40     public NumberDataTypeTest(String JavaDoc name)
41     {
42         super(name);
43     }
44
45     public void testToString() throws Exception JavaDoc
46     {
47         String JavaDoc[] expected = {"NUMERIC", "DECIMAL"};
48
49         assertEquals("type count", expected.length, TYPES.length);
50         for (int i = 0; i < TYPES.length; i++)
51         {
52             assertEquals("name", expected[i], TYPES[i].toString());
53         }
54     }
55
56     public void testGetTypeClass() throws Exception JavaDoc
57     {
58         for (int i = 0; i < TYPES.length; i++)
59         {
60             assertEquals("class", BigDecimal JavaDoc.class, TYPES[i].getTypeClass());
61         }
62     }
63
64     public void testIsNumber() throws Exception JavaDoc
65     {
66         for (int i = 0; i < TYPES.length; i++)
67         {
68             assertEquals("is number", true, TYPES[i].isNumber());
69         }
70     }
71
72     public void testIsDateTime() throws Exception JavaDoc
73     {
74         for (int i = 0; i < TYPES.length; i++)
75         {
76             assertEquals("is date/time", false, TYPES[i].isDateTime());
77         }
78     }
79
80     public void testTypeCast() throws Exception JavaDoc
81     {
82         Object JavaDoc[] values = {
83             null,
84             new BigDecimal JavaDoc(1234),
85             "1234",
86             "12.34",
87             Boolean.TRUE,
88             Boolean.FALSE,
89         };
90         BigDecimal JavaDoc[] expected = {
91             null,
92             new BigDecimal JavaDoc(1234),
93             new BigDecimal JavaDoc(1234),
94             new BigDecimal JavaDoc("12.34"),
95             new BigDecimal JavaDoc("1"),
96             new BigDecimal JavaDoc("0"),
97         };
98
99         assertEquals("actual vs expected count", values.length, expected.length);
100
101         for (int i = 0; i < TYPES.length; i++)
102         {
103             for (int j = 0; j < values.length; j++)
104             {
105                 assertEquals("typecast " + j, expected[j],
106                         TYPES[i].typeCast(values[j]));
107             }
108         }
109     }
110
111     public void testTypeCastNone() throws Exception JavaDoc
112     {
113         for (int i = 0; i < TYPES.length; i++)
114         {
115             DataType type = TYPES[i];
116             assertEquals("typecast " + type, null, type.typeCast(ITable.NO_VALUE));
117         }
118     }
119
120     public void testTypeCastInvalid() throws Exception JavaDoc
121     {
122         Object JavaDoc[] values = {new Object JavaDoc(), "bla"};
123
124         for (int i = 0; i < TYPES.length; i++)
125         {
126             for (int j = 0; j < values.length; j++)
127             {
128                 try
129                 {
130                     TYPES[i].typeCast(values[j]);
131                     fail("Should throw TypeCastException");
132                 }
133                 catch (TypeCastException e)
134                 {
135                 }
136             }
137         }
138     }
139
140     public void testCompareEquals() throws Exception JavaDoc
141     {
142         Object JavaDoc[] values1 = {
143             null,
144             new BigDecimal JavaDoc(1234),
145             "1234",
146             "12.34",
147             Boolean.TRUE,
148             Boolean.FALSE,
149             new BigDecimal JavaDoc(123.4),
150             "123",
151         };
152         Object JavaDoc[] values2 = {
153             null,
154             new BigDecimal JavaDoc(1234),
155             new BigDecimal JavaDoc(1234),
156             new BigDecimal JavaDoc("12.34"),
157             new BigDecimal JavaDoc("1"),
158             new BigDecimal JavaDoc("0"),
159             new BigDecimal JavaDoc(123.4000),
160             new BigDecimal JavaDoc("123.0"),
161         };
162
163         assertEquals("values count", values1.length, values2.length);
164
165         for (int i = 0; i < TYPES.length; i++)
166         {
167             for (int j = 0; j < values1.length; j++)
168             {
169                 assertEquals("compare1 " + j, 0, TYPES[i].compare(values1[j], values2[j]));
170                 assertEquals("compare2 " + j, 0, TYPES[i].compare(values2[j], values1[j]));
171             }
172         }
173     }
174
175     public void testCompareInvalid() throws Exception JavaDoc
176     {
177         Object JavaDoc[] values1 = {
178             new Object JavaDoc(),
179             "bla",
180             new java.util.Date JavaDoc()
181         };
182         Object JavaDoc[] values2 = {
183             null,
184             null,
185             null
186         };
187
188         assertEquals("values count", values1.length, values2.length);
189
190         for (int i = 0; i < TYPES.length; i++)
191         {
192             for (int j = 0; j < values1.length; j++)
193             {
194                 try
195                 {
196                     TYPES[i].compare(values1[j], values2[j]);
197                     fail("Should throw TypeCastException");
198                 }
199                 catch (TypeCastException e)
200                 {
201                 }
202
203                 try
204                 {
205                     TYPES[i].compare(values2[j], values1[j]);
206                     fail("Should throw TypeCastException");
207                 }
208                 catch (TypeCastException e)
209                 {
210                 }
211             }
212         }
213     }
214
215     public void testCompareDifferent() throws Exception JavaDoc
216     {
217         Object JavaDoc[] less = {
218             null,
219             "-7500",
220             new BigDecimal JavaDoc("-0.01"),
221             new BigInteger JavaDoc("1234"),
222         };
223
224         Object JavaDoc[] greater = {
225             "0",
226             "5.555",
227             new BigDecimal JavaDoc("0.01"),
228             new BigDecimal JavaDoc("1234.5"),
229         };
230
231         assertEquals("values count", less.length, greater.length);
232
233         for (int i = 0; i < TYPES.length; i++)
234         {
235             for (int j = 0; j < less.length; j++)
236             {
237                 assertTrue("less " + j, TYPES[i].compare(less[j], greater[j]) < 0);
238                 assertTrue("greater " + j, TYPES[i].compare(greater[j], less[j]) > 0);
239             }
240         }
241     }
242
243     public void testSqlType() throws Exception JavaDoc
244     {
245         int[] sqlTypes = {Types.NUMERIC, Types.DECIMAL};
246
247         assertEquals("count", sqlTypes.length, TYPES.length);
248         for (int i = 0; i < TYPES.length; i++)
249         {
250             assertEquals("forSqlType", TYPES[i], DataType.forSqlType(sqlTypes[i]));
251             assertEquals("forSqlTypeName", TYPES[i], DataType.forSqlTypeName(TYPES[i].toString()));
252             assertEquals("getSqlType", sqlTypes[i], TYPES[i].getSqlType());
253         }
254     }
255
256     public void testForObject() throws Exception JavaDoc
257     {
258         assertEquals(DataType.NUMERIC, DataType.forObject(new BigDecimal JavaDoc(1234)));
259     }
260
261     public void testAsString() throws Exception JavaDoc
262     {
263         BigDecimal JavaDoc[] values = {
264             new BigDecimal JavaDoc("1234"),
265         };
266
267         String JavaDoc[] expected = {
268             "1234",
269         };
270
271         assertEquals("actual vs expected count", values.length, expected.length);
272
273         for (int i = 0; i < values.length; i++)
274         {
275             assertEquals("asString " + i, expected[i], DataType.asString(values[i]));
276         }
277     }
278
279     public void testGetSqlValue() throws Exception JavaDoc
280     {
281         BigDecimal JavaDoc[] expected = {
282             null,
283             new BigDecimal JavaDoc("12.34"),
284         };
285
286         ExtendedMockSingleRowResultSet resultSet = new ExtendedMockSingleRowResultSet();
287         resultSet.addExpectedIndexedValues(expected);
288
289         for (int i = 0; i < expected.length; i++)
290         {
291             Object JavaDoc expectedValue = expected[i];
292
293             for (int j = 0; j < TYPES.length; j++)
294             {
295                 DataType dataType = TYPES[j];
296                 Object JavaDoc actualValue = dataType.getSqlValue(i + 1, resultSet);
297                 assertEquals("value " + j, expectedValue, actualValue);
298             }
299         }
300     }
301
302 }
303
Popular Tags