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