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: 1023 $
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 testTypeCastInvalid() throws Exception
162     {
163         Object[] values = {
164             new Object(),
165             "bla",
166             new java.util.Date()
167         };
168 
169         for (int i = 0; i < TYPES.length; i++)
170         {
171             for (int j = 0; j < values.length; j++)
172             {
173                 try
174                 {
175                     TYPES[i].typeCast(values[j]);
176                     fail("Should throw TypeCastException");
177                 }
178                 catch (TypeCastException e)
179                 {
180                 }
181             }
182         }
183     }
184 
185     public void testCompareEquals() throws Exception
186     {
187         Object[] values1 = {
188             null,
189             "5",
190             new Long(Integer.MAX_VALUE - 1),
191             new Double(Integer.MIN_VALUE + 1),
192             "-7500",
193             new Long(Integer.MAX_VALUE),
194             new Double(Integer.MIN_VALUE),
195             new Float(0.666),
196             new Double(0.666),
197             new Double(5.49),
198             "-99.9",
199             new Double(1.5E2),
200             new BigDecimal((double)1234),
201         };
202 
203         Object[] values2 = {
204             null,
205             new Integer(5),
206             new Integer(Integer.MAX_VALUE - 1),
207             new Integer(Integer.MIN_VALUE + 1),
208             new Integer(-7500),
209             new Integer(Integer.MAX_VALUE),
210             new Integer(Integer.MIN_VALUE),
211             new Integer(0),
212             new Integer(0),
213             new Integer(5),
214             new Integer(-99),
215             new Integer(150),
216             new Integer(1234),
217         };
218 
219         assertEquals("values count", values1.length, values2.length);
220 
221         for (int i = 0; i < TYPES.length; i++)
222         {
223             for (int j = 0; j < values1.length; j++)
224             {
225                 assertEquals("compare1 " + j, 0, TYPES[i].compare(values1[j], values2[j]));
226                 assertEquals("compare2 " + j, 0, TYPES[i].compare(values2[j], values1[j]));
227             }
228         }
229     }
230 
231     public void testCompareInvalid() throws Exception
232     {
233         Object[] values1 = {
234             new Object(),
235             "bla",
236             new java.util.Date()
237         };
238         Object[] values2 = {
239             null,
240             null,
241             null
242         };
243 
244         assertEquals("values count", values1.length, values2.length);
245 
246         for (int i = 0; i < TYPES.length; i++)
247         {
248             for (int j = 0; j < values1.length; j++)
249             {
250                 try
251                 {
252                     TYPES[i].compare(values1[j], values2[j]);
253                     fail("Should throw TypeCastException");
254                 }
255                 catch (TypeCastException e)
256                 {
257                 }
258 
259                 try
260                 {
261                     TYPES[i].compare(values2[j], values1[j]);
262                     fail("Should throw TypeCastException");
263                 }
264                 catch (TypeCastException e)
265                 {
266                 }
267             }
268         }
269     }
270 
271     public void testCompareDifferent() throws Exception
272     {
273         Object[] less = {
274             null,
275             "-7500",
276             new Double(Float.MIN_VALUE),
277         };
278 
279         Object[] greater = {
280             "0",
281             "5.555",
282             new Float(Float.MAX_VALUE),
283         };
284 
285         assertEquals("values count", less.length, greater.length);
286 
287         for (int i = 0; i < TYPES.length; i++)
288         {
289             for (int j = 0; j < less.length; j++)
290             {
291                 assertTrue("less " + j, TYPES[i].compare(less[j], greater[j]) < 0);
292                 assertTrue("greater " + j, TYPES[i].compare(greater[j], less[j]) > 0);
293             }
294         }
295     }
296 
297     public void testSqlType() throws Exception
298     {
299         int[] sqlTypes = {
300             Types.TINYINT,
301             Types.SMALLINT,
302             Types.INTEGER,
303         };
304 
305         assertEquals("count", sqlTypes.length, TYPES.length);
306         for (int i = 0; i < TYPES.length; i++)
307         {
308             assertEquals("forSqlType", TYPES[i], DataType.forSqlType(sqlTypes[i]));
309             assertEquals("forSqlTypeName", TYPES[i], DataType.forSqlTypeName(TYPES[i].toString()));
310             assertEquals("getSqlType", sqlTypes[i], TYPES[i].getSqlType());
311         }
312     }
313 
314     /**
315      *
316      */
317     public void testForObject() throws Exception
318     {
319         assertEquals(DataType.INTEGER, DataType.forObject(new Integer(1234)));
320     }
321 
322     public void testAsString() throws Exception
323     {
324         Object[] values = {
325             new Integer("1234"),
326         };
327 
328         String[] expected = {
329             "1234",
330         };
331 
332         assertEquals("actual vs expected count", values.length, expected.length);
333 
334         for (int i = 0; i < values.length; i++)
335         {
336             assertEquals("asString " + i, expected[i], DataType.asString(values[i]));
337         }
338     }
339 
340     public void testGetSqlValue() throws Exception
341     {
342         Integer[] expected = {
343             null,
344             new Integer(5),
345             new Integer(Integer.MAX_VALUE - 1),
346             new Integer(Integer.MIN_VALUE + 1),
347             new Integer(-7500),
348         };
349 
350         ExtendedMockSingleRowResultSet resultSet = new ExtendedMockSingleRowResultSet();
351         resultSet.addExpectedIndexedValues(expected);
352 
353         for (int i = 0; i < expected.length; i++)
354         {
355             Object expectedValue = expected[i];
356 
357             for (int j = 0; j < TYPES.length; j++)
358             {
359                 DataType dataType = TYPES[j];
360                 Object actualValue = dataType.getSqlValue(i + 1, resultSet);
361                 assertEquals("value " + j, expectedValue, actualValue);
362             }
363         }
364     }
365 
366 }