View Javadoc
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;
28  import java.sql.Types;
29  
30  /**
31   * @author Manuel Laflamme
32   * @version $Revision$
33   */
34  
35  public class IntegerDataTypeTest extends AbstractDataTypeTest
36  {
37      private final static DataType[] TYPES = {
38          DataType.TINYINT,
39          DataType.SMALLINT,
40          DataType.INTEGER,
41      };
42  
43      public IntegerDataTypeTest(String name)
44      {
45          super(name);
46      }
47  
48      /**
49       *
50       */
51      public void testToString() throws Exception
52      {
53          String[] expected = {
54              "TINYINT",
55              "SMALLINT",
56              "INTEGER",
57          };
58  
59          assertEquals("type count", expected.length, TYPES.length);
60          for (int i = 0; i < TYPES.length; i++)
61          {
62              assertEquals("name", expected[i], TYPES[i].toString());
63          }
64      }
65  
66      public void testGetTypeClass() throws Exception
67      {
68          for (int i = 0; i < TYPES.length; i++)
69          {
70              assertEquals("class", Integer.class, TYPES[i].getTypeClass());
71          }
72      }
73  
74      public void testIsNumber() throws Exception
75      {
76          for (int i = 0; i < TYPES.length; i++)
77          {
78              assertEquals("is number", true, TYPES[i].isNumber());
79          }
80      }
81  
82      public void testIsDateTime() throws Exception
83      {
84          for (int i = 0; i < TYPES.length; i++)
85          {
86              assertEquals("is date/time", false, TYPES[i].isDateTime());
87          }
88      }
89  
90      public void testTypeCast() throws Exception
91      {
92          Object[] values = {
93              null,
94              "5",
95              new Long(Integer.MAX_VALUE - 1),
96              new Double(Integer.MIN_VALUE + 1),
97              "-7500",
98              new Long(Integer.MAX_VALUE),
99              new Double(Integer.MIN_VALUE),
100             new Float(0.666),
101             new Double(0.666),
102             new Double(5.49),
103             "-99.9",
104             new Double(1.5E2),
105             new BigDecimal((double)1234),
106         };
107 
108         Integer[] expected = {
109             null,
110             new Integer(5),
111             new Integer(Integer.MAX_VALUE - 1),
112             new Integer(Integer.MIN_VALUE + 1),
113             new Integer(-7500),
114             new Integer(Integer.MAX_VALUE),
115             new Integer(Integer.MIN_VALUE),
116             new Integer(0),
117             new Integer(0),
118             new Integer(5),
119             new Integer(-99),
120             new Integer(150),
121             new Integer(1234),
122 
123         };
124 
125         assertEquals("actual vs expected count", values.length, expected.length);
126 
127         for (int i = 0; i < TYPES.length; i++)
128         {
129             for (int j = 0; j < values.length; j++)
130             {
131                 assertEquals("typecast " + j, expected[j],
132                         TYPES[i].typeCast(values[j]));
133             }
134         }
135     }
136 
137     /**
138      * Empty string should be treated the same as null
139      * @throws Exception
140      * @since 2.4.6
141      */
142     public void testTypeCastEmptyString() throws Exception
143     {
144         String emptyString = "";
145         for (int i = 0; i < TYPES.length; i++)
146         {
147             DataType type = TYPES[i];
148             assertEquals("typecast " + type, null, type.typeCast(emptyString));
149         }
150     }
151     
152     public void testTypeCastNone() throws Exception
153     {
154         for (int i = 0; i < TYPES.length; i++)
155         {
156             DataType type = TYPES[i];
157             assertEquals("typecast " + type, null, type.typeCast(ITable.NO_VALUE));
158         }
159     }
160 
161     public void testTypeCastBool() throws Exception
162     {
163         for (int i = 0; i < TYPES.length; i++)
164         {
165             DataType type = TYPES[i];
166             assertEquals("typecast " + type, 0, type.typeCast("false"));
167             assertEquals("typecast " + type, 0, type.typeCast("FALSE"));
168             assertEquals("typecast " + type, 1, type.typeCast("true"));
169             assertEquals("typecast " + type, 1, type.typeCast("TRUE"));
170         }
171     }
172 
173     public void testTypeCastInvalid() throws Exception
174     {
175         Object[] values = {
176             new Object(),
177             "bla",
178             new java.util.Date()
179         };
180 
181         for (int i = 0; i < TYPES.length; i++)
182         {
183             for (int j = 0; j < values.length; j++)
184             {
185                 try
186                 {
187                     TYPES[i].typeCast(values[j]);
188                     fail("Should throw TypeCastException");
189                 }
190                 catch (TypeCastException e)
191                 {
192                 }
193             }
194         }
195     }
196 
197     public void testCompareEquals() throws Exception
198     {
199         Object[] values1 = {
200             null,
201             "5",
202             new Long(Integer.MAX_VALUE - 1),
203             new Double(Integer.MIN_VALUE + 1),
204             "-7500",
205             new Long(Integer.MAX_VALUE),
206             new Double(Integer.MIN_VALUE),
207             new Float(0.666),
208             new Double(0.666),
209             new Double(5.49),
210             "-99.9",
211             new Double(1.5E2),
212             new BigDecimal((double)1234),
213         };
214 
215         Object[] values2 = {
216             null,
217             new Integer(5),
218             new Integer(Integer.MAX_VALUE - 1),
219             new Integer(Integer.MIN_VALUE + 1),
220             new Integer(-7500),
221             new Integer(Integer.MAX_VALUE),
222             new Integer(Integer.MIN_VALUE),
223             new Integer(0),
224             new Integer(0),
225             new Integer(5),
226             new Integer(-99),
227             new Integer(150),
228             new Integer(1234),
229         };
230 
231         assertEquals("values count", values1.length, values2.length);
232 
233         for (int i = 0; i < TYPES.length; i++)
234         {
235             for (int j = 0; j < values1.length; j++)
236             {
237                 assertEquals("compare1 " + j, 0, TYPES[i].compare(values1[j], values2[j]));
238                 assertEquals("compare2 " + j, 0, TYPES[i].compare(values2[j], values1[j]));
239             }
240         }
241     }
242 
243     public void testCompareInvalid() throws Exception
244     {
245         Object[] values1 = {
246             new Object(),
247             "bla",
248             new java.util.Date()
249         };
250         Object[] values2 = {
251             null,
252             null,
253             null
254         };
255 
256         assertEquals("values count", values1.length, values2.length);
257 
258         for (int i = 0; i < TYPES.length; i++)
259         {
260             for (int j = 0; j < values1.length; j++)
261             {
262                 try
263                 {
264                     TYPES[i].compare(values1[j], values2[j]);
265                     fail("Should throw TypeCastException");
266                 }
267                 catch (TypeCastException e)
268                 {
269                 }
270 
271                 try
272                 {
273                     TYPES[i].compare(values2[j], values1[j]);
274                     fail("Should throw TypeCastException");
275                 }
276                 catch (TypeCastException e)
277                 {
278                 }
279             }
280         }
281     }
282 
283     public void testCompareDifferent() throws Exception
284     {
285         Object[] less = {
286             null,
287             "-7500",
288             new Double(Float.MIN_VALUE),
289         };
290 
291         Object[] greater = {
292             "0",
293             "5.555",
294             new Float(Float.MAX_VALUE),
295         };
296 
297         assertEquals("values count", less.length, greater.length);
298 
299         for (int i = 0; i < TYPES.length; i++)
300         {
301             for (int j = 0; j < less.length; j++)
302             {
303                 assertTrue("less " + j, TYPES[i].compare(less[j], greater[j]) < 0);
304                 assertTrue("greater " + j, TYPES[i].compare(greater[j], less[j]) > 0);
305             }
306         }
307     }
308 
309     public void testSqlType() throws Exception
310     {
311         int[] sqlTypes = {
312             Types.TINYINT,
313             Types.SMALLINT,
314             Types.INTEGER,
315         };
316 
317         assertEquals("count", sqlTypes.length, TYPES.length);
318         for (int i = 0; i < TYPES.length; i++)
319         {
320             assertEquals("forSqlType", TYPES[i], DataType.forSqlType(sqlTypes[i]));
321             assertEquals("forSqlTypeName", TYPES[i], DataType.forSqlTypeName(TYPES[i].toString()));
322             assertEquals("getSqlType", sqlTypes[i], TYPES[i].getSqlType());
323         }
324     }
325 
326     /**
327      *
328      */
329     public void testForObject() throws Exception
330     {
331         assertEquals(DataType.INTEGER, DataType.forObject(new Integer(1234)));
332     }
333 
334     public void testAsString() throws Exception
335     {
336         Object[] values = {
337             new Integer("1234"),
338         };
339 
340         String[] expected = {
341             "1234",
342         };
343 
344         assertEquals("actual vs expected count", values.length, expected.length);
345 
346         for (int i = 0; i < values.length; i++)
347         {
348             assertEquals("asString " + i, expected[i], DataType.asString(values[i]));
349         }
350     }
351 
352     public void testGetSqlValue() throws Exception
353     {
354         Integer[] expected = {
355             null,
356             new Integer(5),
357             new Integer(Integer.MAX_VALUE - 1),
358             new Integer(Integer.MIN_VALUE + 1),
359             new Integer(-7500),
360         };
361 
362         ExtendedMockSingleRowResultSet resultSet = new ExtendedMockSingleRowResultSet();
363         resultSet.addExpectedIndexedValues(expected);
364 
365         for (int i = 0; i < expected.length; i++)
366         {
367             Object expectedValue = expected[i];
368 
369             for (int j = 0; j < TYPES.length; j++)
370             {
371                 DataType dataType = TYPES[j];
372                 Object actualValue = dataType.getSqlValue(i + 1, resultSet);
373                 assertEquals("value " + j, expectedValue, actualValue);
374             }
375         }
376     }
377 
378 }