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.sql.Time;
28  import java.sql.Timestamp;
29  import java.sql.Types;
30  
31  /**
32   * @author Manuel Laflamme
33   * @version $Revision$
34   */
35  
36  public class TimeDataTypeTest extends AbstractDataTypeTest
37  {
38      private final static DataType THIS_TYPE = DataType.TIME;
39  
40      public TimeDataTypeTest(String name)
41      {
42          super(name);
43      }
44  
45      public void testToString() throws Exception
46      {
47          assertEquals("name", "TIME", THIS_TYPE.toString());
48      }
49  
50      public void testGetTypeClass() throws Exception
51      {
52          assertEquals("class", Time.class, THIS_TYPE.getTypeClass());
53      }
54  
55      public void testIsNumber() throws Exception
56      {
57          assertEquals("is number", false, THIS_TYPE.isNumber());
58      }
59  
60      public void testIsDateTime() throws Exception
61      {
62          assertEquals("is date/time", true, THIS_TYPE.isDateTime());
63      }
64  
65      public void testTypeCast() throws Exception
66      {
67          Object[] values = {
68              null,
69              new Time(1234),
70              new java.sql.Date(1234),
71              new Timestamp(1234),
72              new Time(1234).toString(),
73              new java.util.Date(1234),
74          };
75  
76          java.sql.Time[] expected = {
77              null,
78              new Time(1234),
79              new Time(new java.sql.Date(1234).getTime()),
80              new Time(new Timestamp(1234).getTime()),
81              Time.valueOf(new Time(1234).toString()),
82              new Time(1234),
83          };
84  
85          assertEquals("actual vs expected count", values.length, expected.length);
86  
87          for (int i = 0; i < values.length; i++)
88          {
89              assertEquals("typecast " + i, expected[i],
90                      THIS_TYPE.typeCast(values[i]));
91          }
92      }
93  
94      public void testTypeCastNone() throws Exception
95      {
96          assertEquals("typecast", null, THIS_TYPE.typeCast(ITable.NO_VALUE));
97      }
98  
99      public void testTypeCastInvalid() throws Exception
100     {
101         Object[] values = {
102             new Integer(1234),
103             new Object(),
104             "bla",
105             "2000.05.05",
106         };
107 
108         for (int i = 0; i < values.length; i++)
109         {
110             try
111             {
112                 THIS_TYPE.typeCast(values[i]);
113                 fail("Should throw TypeCastException - " + i);
114             }
115             catch (TypeCastException e)
116             {
117             }
118         }
119     }
120 
121     public void testCompareEquals() throws Exception
122     {
123         Object[] values1 = {
124             null,
125             new Time(1234),
126             new java.sql.Date(1234),
127             new Timestamp(1234),
128             new Time(1234).toString(),
129             new java.util.Date(1234),
130             "00:01:02",
131         };
132 
133         Object[] values2 = {
134             null,
135             new Time(1234),
136             new Time(new java.sql.Date(1234).getTime()),
137             new Time(new Timestamp(1234).getTime()),
138             Time.valueOf(new Time(1234).toString()),
139             new Time(1234),
140             new Time(0, 1, 2),
141         };
142 
143         assertEquals("values count", values1.length, values2.length);
144 
145         for (int i = 0; i < values1.length; i++)
146         {
147             assertEquals("compare1 " + i, 0, THIS_TYPE.compare(values1[i], values2[i]));
148             assertEquals("compare2 " + i, 0, THIS_TYPE.compare(values2[i], values1[i]));
149         }
150     }
151 
152     public void testCompareInvalid() throws Exception
153     {
154         Object[] values1 = {
155             new Integer(1234),
156             new Object(),
157             "bla",
158             "2000.05.05",
159         };
160         Object[] values2 = {
161             null,
162             null,
163             null,
164             null,
165         };
166 
167         assertEquals("values count", values1.length, values2.length);
168 
169         for (int i = 0; i < values1.length; i++)
170         {
171             try
172             {
173                 THIS_TYPE.compare(values1[i], values2[i]);
174                 fail("Should throw TypeCastException - " + i);
175             }
176             catch (TypeCastException e)
177             {
178             }
179 
180             try
181             {
182                 THIS_TYPE.compare(values1[i], values2[i]);
183                 fail("Should throw TypeCastException - " + i);
184             }
185             catch (TypeCastException e)
186             {
187             }
188         }
189     }
190 
191     public void testCompareDifferent() throws Exception
192     {
193         Object[] less = {
194             null,
195             new java.sql.Time(0),
196             "08:00:00",
197             "08:00:00",
198         };
199 
200         Object[] greater = {
201             new java.sql.Time(1234),
202             new java.sql.Time(System.currentTimeMillis()),
203             "20:00:00",
204             "08:00:01",
205         };
206 
207         assertEquals("values count", less.length, greater.length);
208 
209         for (int i = 0; i < less.length; i++)
210         {
211             assertTrue("less " + i, THIS_TYPE.compare(less[i], greater[i]) < 0);
212             assertTrue("greater " + i, THIS_TYPE.compare(greater[i], less[i]) > 0);
213         }
214     }
215 
216     public void testSqlType() throws Exception
217     {
218         assertEquals(THIS_TYPE, DataType.forSqlType(Types.TIME));
219         assertEquals("forSqlTypeName", THIS_TYPE, DataType.forSqlTypeName(THIS_TYPE.toString()));
220         assertEquals(Types.TIME, THIS_TYPE.getSqlType());
221     }
222 
223     /**
224      *
225      */
226     public void testForObject() throws Exception
227     {
228         assertEquals(THIS_TYPE, DataType.forObject(new Time(1234)));
229     }
230 
231     public void testAsString() throws Exception
232     {
233         java.sql.Time[] values = {
234             new java.sql.Time(1234),
235         };
236 
237         String[] expected = {
238             new java.sql.Time(1234).toString(),
239         };
240 
241 
242         assertEquals("actual vs expected count", values.length, expected.length);
243 
244         for (int i = 0; i < values.length; i++)
245         {
246             assertEquals("asString " + i, expected[i], DataType.asString(values[i]));
247         }
248     }
249 
250     public void testGetSqlValue() throws Exception
251     {
252         java.sql.Time[] expected = {
253             null,
254             new Time(1234),
255             new Time(new java.sql.Date(1234).getTime()),
256             new Time(new Timestamp(1234).getTime()),
257             Time.valueOf(new Time(1234).toString()),
258             new Time(1234),
259         };
260 
261         ExtendedMockSingleRowResultSet resultSet = new ExtendedMockSingleRowResultSet();
262         resultSet.addExpectedIndexedValues(expected);
263 
264         for (int i = 0; i < expected.length; i++)
265         {
266             Object expectedValue = expected[i];
267             Object actualValue = THIS_TYPE.getSqlValue(i + 1, resultSet);
268             assertEquals("value", expectedValue, actualValue);
269         }
270     }
271 
272 }