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 java.math.BigDecimal;
25  import java.math.BigInteger;
26  import java.sql.Types;
27  
28  import org.dbunit.database.ExtendedMockSingleRowResultSet;
29  import org.dbunit.dataset.ITable;
30  
31  /**
32   * @author Manuel Laflamme
33   * @version $Revision: 1032 $
34   */
35  
36  public class BigIntegerDataTypeTest extends AbstractDataTypeTest
37  {
38      private final static DataType THIS_TYPE = DataType.BIGINT;
39  
40      public BigIntegerDataTypeTest(String name)
41      {
42          super(name);
43      }
44  
45      /**
46       *
47       */
48      public void testToString() throws Exception
49      {
50          assertEquals("name", "BIGINT", THIS_TYPE.toString());
51      }
52  
53      /**
54       *
55       */
56      public void testGetTypeClass() throws Exception
57      {
58          assertEquals("class", BigInteger.class, THIS_TYPE.getTypeClass());
59      }
60  
61      /**
62       *
63       */
64      public void testIsNumber() throws Exception
65      {
66          assertEquals("is number", true, THIS_TYPE.isNumber());
67      }
68  
69      public void testIsDateTime() throws Exception
70      {
71          assertEquals("is date/time", false, THIS_TYPE.isDateTime());
72      }
73  
74      public void testTypeCast() throws Exception
75      {
76          Object[] values = {
77              null,
78              "5",
79              new Long(1234),
80              new Float(Long.MAX_VALUE),
81              new Float(Long.MIN_VALUE),
82              "-7500",
83              new Double(Long.MAX_VALUE),
84              new Double(Long.MIN_VALUE),
85              new Float(0.666),
86              new Double(0.666),
87              new Double(5.49),
88              "-99.9",
89              new Double(1.5E6),
90              new BigDecimal((double)1234),
91              "17446744073709551630", // a value that is too long for a usual java.lang.Long
92              new BigDecimal("17446744073709551630"), // a value that is too long for a usual java.lang.Long
93          };
94  
95          BigInteger[] expected = {
96              null,
97              new BigInteger("5"),
98              new BigInteger("1234"),
99              new BigInteger(""+Long.MAX_VALUE),
100             new BigInteger(""+Long.MIN_VALUE),
101             new BigInteger("-7500"),
102             new BigInteger(""+Long.MAX_VALUE),
103             new BigInteger(""+Long.MIN_VALUE),
104             new BigInteger("0"),
105             new BigInteger("0"),
106             new BigInteger("5"),
107             new BigInteger("-99"),
108             new BigInteger("1500000"),
109             new BigInteger("1234"),
110             new BigInteger("17446744073709551630"),
111             new BigInteger("17446744073709551630"),
112         };
113 
114         assertEquals("actual vs expected count", values.length, expected.length);
115 
116         for (int i = 0; i < values.length; i++)
117         {
118             assertEquals("typecast " + i, expected[i],
119                     THIS_TYPE.typeCast(values[i]));
120         }
121     }
122 
123     public void testTypeCastNone() throws Exception
124     {
125         assertEquals("typecast", null, THIS_TYPE.typeCast(ITable.NO_VALUE));
126     }
127 
128     public void testTypeCastInvalid() throws Exception
129     {
130         Object[] values = {new Object(), "bla", new java.util.Date()};
131 
132         for (int i = 0; i < values.length; i++)
133         {
134             try
135             {
136                 THIS_TYPE.typeCast(values[i]);
137                 fail("Should throw TypeCastException");
138             }
139             catch (TypeCastException e)
140             {
141             }
142         }
143     }
144 
145     public void testCompareEquals() throws Exception
146     {
147         Object[] values1 = {
148             null,
149             "5",
150             new Long(1234),
151             new Float(Long.MAX_VALUE),
152             new Float(Long.MIN_VALUE),
153             "-7500",
154             new Double(Long.MAX_VALUE),
155             new Double(Long.MIN_VALUE),
156             new Float(0.666),
157             new Double(0.666),
158             new Double(5.49),
159             "-99.9",
160             new Double(1.5E6),
161             new BigDecimal((double)1234),
162         };
163 
164         Object[] values2 = {
165             null,
166             new Long(5),
167             new Long(1234),
168             new Long(Long.MAX_VALUE),
169             new Long(Long.MIN_VALUE),
170             new Long(-7500),
171             new Long(Long.MAX_VALUE),
172             new Long(Long.MIN_VALUE),
173             new Long(0),
174             new Long(0),
175             new Long(5),
176             new Long(-99),
177             new Long(1500000),
178             new Long(1234),
179         };
180 
181         assertEquals("values count", values1.length, values2.length);
182 
183         for (int i = 0; i < values1.length; i++)
184         {
185             assertEquals("compare1 " + i, 0, THIS_TYPE.compare(values1[i], values2[i]));
186             assertEquals("compare2 " + i, 0, THIS_TYPE.compare(values2[i], values1[i]));
187         }
188     }
189 
190     public void testCompareInvalid() throws Exception
191     {
192         Object[] values1 = {
193             new Object(),
194             "bla",
195             new java.util.Date()
196         };
197         Object[] values2 = {
198             null,
199             null,
200             null
201         };
202 
203         assertEquals("values count", values1.length, values2.length);
204 
205         for (int i = 0; i < values1.length; i++)
206         {
207             try
208             {
209                 THIS_TYPE.compare(values1[i], values2[i]);
210                 fail("Should throw TypeCastException");
211             }
212             catch (TypeCastException e)
213             {
214             }
215 
216             try
217             {
218                 THIS_TYPE.compare(values2[i], values1[i]);
219                 fail("Should throw TypeCastException");
220             }
221             catch (TypeCastException e)
222             {
223             }
224         }
225     }
226 
227     public void testCompareDifferent() throws Exception
228     {
229         Object[] less = {
230             null,
231             null,
232             "-7500",
233         };
234 
235         Object[] greater = {
236             "0",
237             new Long(-5),
238             new Long(5),
239         };
240 
241         assertEquals("values count", less.length, greater.length);
242 
243         for (int i = 0; i < less.length; i++)
244         {
245             assertTrue("less " + i, THIS_TYPE.compare(less[i], greater[i]) < 0);
246             assertTrue("greater " + i, THIS_TYPE.compare(greater[i], less[i]) > 0);
247         }
248     }
249 
250     public void testSqlType() throws Exception
251     {
252         assertEquals(THIS_TYPE, DataType.forSqlType(Types.BIGINT));
253         assertEquals("forSqlTypeName", THIS_TYPE, DataType.forSqlTypeName(THIS_TYPE.toString()));
254         assertEquals(Types.BIGINT, THIS_TYPE.getSqlType());
255     }
256 
257     public void testForObject() throws Exception
258     {
259         assertEquals(THIS_TYPE, DataType.forObject(new BigInteger("1234")));
260     }
261 
262     public void testAsString() throws Exception
263     {
264         Long[] values = {
265             new Long(1234),
266         };
267 
268         String[] expected = {
269             "1234",
270         };
271 
272 
273         assertEquals("actual vs expected count", values.length, expected.length);
274 
275         for (int i = 0; i < values.length; i++)
276         {
277             assertEquals("asString " + i, expected[i], DataType.asString(values[i]));
278         }
279     }
280 
281     public void testGetSqlValue() throws Exception
282     {
283         BigInteger[] expected = {
284             null,
285             new BigInteger("5"),
286             new BigInteger("1234"),
287             new BigInteger(""+Long.MAX_VALUE),
288             new BigInteger(""+Long.MIN_VALUE),
289             new BigInteger("-7500"),
290             new BigInteger("0"),
291         };
292         
293         // Internally BigIntegerDataType uses resultSet.getBigDecimal() on the JDBC API because
294         // there is no resultSet.getBigInteger().
295         BigDecimal[] expectedForMock = new BigDecimal[expected.length];
296         for (int i = 0; i < expectedForMock.length; i++) {
297             if(expected[i]==null){
298                 expectedForMock[i]=null;
299             }
300             else{
301                 expectedForMock[i] = new BigDecimal(expected[i]);
302             }
303         }
304         
305 
306         ExtendedMockSingleRowResultSet resultSet = new ExtendedMockSingleRowResultSet();
307         resultSet.addExpectedIndexedValues(expectedForMock);
308 
309         for (int i = 0; i < expected.length; i++)
310         {
311             Object expectedValue = expected[i];
312             Object actualValue = THIS_TYPE.getSqlValue(i + 1, resultSet);
313             if(expectedValue != null && actualValue!=null)
314                 assertEquals("type mismatch", expectedValue.getClass(), actualValue.getClass());
315             
316             assertEquals("value", expectedValue, actualValue);
317         }
318     }
319 
320 }