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.sql.Date;
25  import java.sql.Time;
26  import java.sql.Timestamp;
27  import java.sql.Types;
28  import java.util.Calendar;
29  import java.util.GregorianCalendar;
30  import java.util.TimeZone;
31  
32  import org.dbunit.database.ExtendedMockSingleRowResultSet;
33  import org.dbunit.dataset.ITable;
34  
35  /**
36   * @author Manuel Laflamme
37   * @version $Revision$
38   */
39  
40  public class TimestampDataTypeTest extends AbstractDataTypeTest
41  {
42      private final static DataType THIS_TYPE = DataType.TIMESTAMP;
43  
44      public TimestampDataTypeTest(String name)
45      {
46          super(name);
47      }
48  
49      /**
50       *
51       */
52      @Override
53      public void testToString() throws Exception
54      {
55          assertEquals("name", "TIMESTAMP", THIS_TYPE.toString());
56      }
57  
58      /**
59       *
60       */
61      @Override
62      public void testGetTypeClass() throws Exception
63      {
64          assertEquals("class", Timestamp.class, THIS_TYPE.getTypeClass());
65      }
66  
67      @Override
68      public void testIsNumber() throws Exception
69      {
70          assertEquals("is number", false, THIS_TYPE.isNumber());
71      }
72  
73      @Override
74      public void testIsDateTime() throws Exception
75      {
76          assertEquals("is date/time", true, THIS_TYPE.isDateTime());
77      }
78  
79      private static Timestamp makeTimestamp(int year, int month, int day,
80              int hour, int minute, int second, int millis, TimeZone timeZone)
81      {
82          Calendar cal = new GregorianCalendar(timeZone);
83          cal.clear();
84          cal.set(year, month, day, hour, minute, second);
85          cal.set(Calendar.MILLISECOND, millis);
86          return new Timestamp(cal.getTime().getTime());
87      }
88  
89      private static Timestamp makeTimestamp(int year, int month, int day,
90              int hour, int minute, int second, int millis, String timeZone)
91      {
92          return makeTimestamp(year, month, day, hour, minute, second, millis,
93                  TimeZone.getTimeZone(timeZone));
94      }
95  
96      private static Timestamp makeTimestamp(int year, int month, int day,
97              int hour, int minute, int second, int millis)
98      {
99          return makeTimestamp(year, month, day, hour, minute, second, millis,
100                 TimeZone.getDefault());
101     }
102 
103     private static Timestamp makeTimestamp(int year, int month, int day,
104             int hour, int minute, int second, String timeZone)
105     {
106         return makeTimestamp(year, month, day, hour, minute, second, 0,
107                 TimeZone.getTimeZone(timeZone));
108     }
109 
110     private static Timestamp makeTimestamp(int year, int month, int day,
111             int hour, int minute, int second)
112     {
113         return makeTimestamp(year, month, day, hour, minute, second, 0,
114                 TimeZone.getDefault());
115     }
116 
117     public void testWithTimezone_LocalTZ() throws Exception
118     {
119         Timestamp ts1 = makeTimestamp(2013, 0, 27, 1, 22, 41, 900, "GMT+1");
120         String ts2 = "2013-01-27 01:22:41.900 +0100";
121         assertEquals(ts1, THIS_TYPE.typeCast(ts2));
122     }
123 
124     public void testWithTimezone_GMT6() throws Exception
125     {
126         Timestamp ts1 = makeTimestamp(2013, 0, 27, 1, 22, 41, 900, "GMT+6");
127         String ts2 = "2013-01-27 01:22:41.900 +0600";
128         assertEquals(ts1, THIS_TYPE.typeCast(ts2));
129     }
130 
131     @Override
132     public void testTypeCast() throws Exception
133     {
134         // Useful when manually testing this for other timezones
135         // Default setting is to test from default timezone
136         // TimeZone testTimeZone = TimeZone.getTimeZone("America/New_York");
137         // TimeZone testTimeZone = TimeZone.getTimeZone("Europe/Berlin");
138         // TimeZone.setDefault(testTimeZone);
139 
140         // @formatter:off
141         Object[] values = {
142             null,
143             new Timestamp(1234),
144             new Date(1234),
145             new Time(1234),
146             new Timestamp(1234).toString(),
147             new Date(1234).toString(),
148             new java.util.Date(1234),
149             "1995-01-07 01:22:41.9 -0500",
150             "1995-01-07 01:22:41.923 -0500",
151             "1995-01-07 01:22:41.9",
152             "1995-01-07 01:22:41.923",
153             "1995-01-07 01:22:41 -0500",
154             "1995-01-07 01:22:41",
155             "2008-11-27 14:52:38 +0100"
156         };
157 
158         Timestamp[] expected = {
159             null,
160             new Timestamp(1234),
161             new Timestamp(new Date(1234).getTime()),
162             new Timestamp(new Time(1234).getTime()),
163             new Timestamp(1234),
164             new Timestamp(Date.valueOf((new Date(1234).toString())).getTime()),
165             new Timestamp(1234),
166             makeTimestamp(1995, 0, 7, 1, 22, 41, 900, "America/New_York"),
167             makeTimestamp(1995, 0, 7, 1, 22, 41, 923, "America/New_York"),
168             makeTimestamp(1995, 0, 7, 1, 22, 41, 900),
169             makeTimestamp(1995, 0, 7, 1, 22, 41, 923),
170             makeTimestamp(1995, 0, 7, 1, 22, 41, "America/New_York"),
171             makeTimestamp(1995, 0, 7, 1, 22, 41),
172             makeTimestamp(2008, 10, 27, 14, 52, 38, "Europe/Berlin")
173         };
174         // @formatter:on
175 
176         assertEquals("actual vs expected count", values.length,
177                 expected.length);
178 
179         for (int i = 0; i < values.length; i++)
180         {
181             assertEquals("typecast " + i, expected[i],
182                     THIS_TYPE.typeCast(values[i]));
183         }
184     }
185 
186     @Override
187     public void testTypeCastNone() throws Exception
188     {
189         assertEquals("typecast", null, THIS_TYPE.typeCast(ITable.NO_VALUE));
190     }
191 
192     @Override
193     public void testTypeCastInvalid() throws Exception
194     {
195         // @formatter:off
196         Object[] values = {
197             new Integer(1234),
198             new Object(),
199             "bla",
200             "2000.05.05",
201         };
202         // @formatter:on
203 
204         for (int i = 0; i < values.length; i++)
205         {
206             try
207             {
208                 THIS_TYPE.typeCast(values[i]);
209                 fail("Should throw TypeCastException - " + i);
210             } catch (TypeCastException e)
211             {
212             }
213         }
214     }
215 
216     @Override
217     public void testCompareEquals() throws Exception
218     {
219         // @formatter:off
220         Object[] values1 = {
221             null,
222             new Timestamp(1234),
223             new Date(1234),
224             new Time(1234),
225             new Timestamp(1234).toString(),
226             new java.util.Date(1234),
227             "1970-01-01 00:00:00.0",
228         };
229 
230         Timestamp[] values2 = {
231             null,
232             new Timestamp(1234),
233             new Timestamp(new Date(1234).getTime()),
234             new Timestamp(new Time(1234).getTime()),
235             Timestamp.valueOf(new Timestamp(1234).toString()),
236             new Timestamp(1234),
237             Timestamp.valueOf("1970-01-01 00:00:00.0"),
238         };
239 
240         assertEquals("values count", values1.length, values2.length);
241         // @formatter:on
242         for (int i = 0; i < values1.length; i++)
243         {
244             assertEquals("compare1 " + i, 0,
245                     THIS_TYPE.compare(values1[i], values2[i]));
246             assertEquals("compare2 " + i, 0,
247                     THIS_TYPE.compare(values2[i], values1[i]));
248         }
249     }
250 
251     @Override
252     public void testCompareInvalid() throws Exception
253     {
254         // @formatter:off
255         Object[] values1 = {
256             new Integer(1234),
257             new Object(),
258             "bla",
259             "2000.05.05",
260         };
261         Object[] values2 = {
262             null,
263             null,
264             null,
265             null,
266         };
267         // @formatter:on
268 
269         assertEquals("values count", values1.length, values2.length);
270 
271         for (int i = 0; i < values1.length; i++)
272         {
273             try
274             {
275                 THIS_TYPE.compare(values1[i], values2[i]);
276                 fail("Should throw TypeCastException - " + i);
277             } catch (TypeCastException e)
278             {
279             }
280 
281             try
282             {
283                 THIS_TYPE.compare(values1[i], values2[i]);
284                 fail("Should throw TypeCastException - " + i);
285             } catch (TypeCastException e)
286             {
287             }
288         }
289     }
290 
291     @Override
292     public void testCompareDifferent() throws Exception
293     {
294         // @formatter:off
295         Object[] less = {
296             null,
297             new java.sql.Date(0),
298             "1974-06-23 23:40:00.0"
299         };
300 
301         Object[] greater = {
302             new java.sql.Date(1234),
303             new java.sql.Date(System.currentTimeMillis()),
304             Timestamp.valueOf("2003-01-30 11:42:00.0"),
305         };
306         // @formatter:on
307 
308         assertEquals("values count", less.length, greater.length);
309 
310         for (int i = 0; i < less.length; i++)
311         {
312             assertTrue("less " + i, THIS_TYPE.compare(less[i], greater[i]) < 0);
313             assertTrue("greater " + i,
314                     THIS_TYPE.compare(greater[i], less[i]) > 0);
315         }
316     }
317 
318     @Override
319     public void testSqlType() throws Exception
320     {
321         assertEquals(THIS_TYPE, DataType.forSqlType(Types.TIMESTAMP));
322         assertEquals("forSqlTypeName", THIS_TYPE,
323                 DataType.forSqlTypeName(THIS_TYPE.toString()));
324         assertEquals(Types.TIMESTAMP, THIS_TYPE.getSqlType());
325     }
326 
327     @Override
328     public void testForObject() throws Exception
329     {
330         assertEquals(THIS_TYPE, DataType.forObject(new Timestamp(1234)));
331     }
332 
333     @Override
334     public void testAsString() throws Exception
335     {
336         // @formatter:off
337         java.sql.Timestamp[] values = {
338             new java.sql.Timestamp(1234),
339         };
340         // @formatter:on
341 
342         // @formatter:off
343         String[] expected = {
344             new java.sql.Timestamp(1234).toString(),
345         };
346         // @formatter:on
347 
348         assertEquals("actual vs expected count", values.length,
349                 expected.length);
350 
351         for (int i = 0; i < values.length; i++)
352         {
353             assertEquals("asString " + i, expected[i],
354                     DataType.asString(values[i]));
355         }
356     }
357 
358     @Override
359     public void testGetSqlValue() throws Exception
360     {
361         // @formatter:off
362         Timestamp[] expected = {
363             null,
364             new Timestamp(1234),
365         };
366         // @formatter:on
367 
368         ExtendedMockSingleRowResultSet resultSet =
369                 new ExtendedMockSingleRowResultSet();
370         resultSet.addExpectedIndexedValues(expected);
371 
372         for (int i = 0; i < expected.length; i++)
373         {
374             Object expectedValue = expected[i];
375             Object actualValue = THIS_TYPE.getSqlValue(i + 1, resultSet);
376             assertEquals("value", expectedValue, actualValue);
377         }
378     }
379 }