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