KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > dataset > datatype > TimeDataTypeTest


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 JavaDoc;
28 import java.sql.Timestamp JavaDoc;
29 import java.sql.Types JavaDoc;
30
31 /**
32  * @author Manuel Laflamme
33  * @version $Revision: 1.12 $
34  */

35
36 public class TimeDataTypeTest extends AbstractDataTypeTest
37 {
38     private final static DataType THIS_TYPE = DataType.TIME;
39
40     public TimeDataTypeTest(String JavaDoc name)
41     {
42         super(name);
43     }
44
45     public void testToString() throws Exception JavaDoc
46     {
47         assertEquals("name", "TIME", THIS_TYPE.toString());
48     }
49
50     public void testGetTypeClass() throws Exception JavaDoc
51     {
52         assertEquals("class", Time JavaDoc.class, THIS_TYPE.getTypeClass());
53     }
54
55     public void testIsNumber() throws Exception JavaDoc
56     {
57         assertEquals("is number", false, THIS_TYPE.isNumber());
58     }
59
60     public void testIsDateTime() throws Exception JavaDoc
61     {
62         assertEquals("is date/time", true, THIS_TYPE.isDateTime());
63     }
64
65     public void testTypeCast() throws Exception JavaDoc
66     {
67         Object JavaDoc[] values = {
68             null,
69             new Time JavaDoc(1234),
70             new java.sql.Date JavaDoc(1234),
71             new Timestamp JavaDoc(1234),
72             new Time JavaDoc(1234).toString(),
73             new java.util.Date JavaDoc(1234),
74         };
75
76         java.sql.Time JavaDoc[] expected = {
77             null,
78             new Time JavaDoc(1234),
79             new Time JavaDoc(new java.sql.Date JavaDoc(1234).getTime()),
80             new Time JavaDoc(new Timestamp JavaDoc(1234).getTime()),
81             Time.valueOf(new Time JavaDoc(1234).toString()),
82             new Time JavaDoc(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 JavaDoc
95     {
96         assertEquals("typecast", null, THIS_TYPE.typeCast(ITable.NO_VALUE));
97     }
98
99     public void testTypeCastInvalid() throws Exception JavaDoc
100     {
101         Object JavaDoc[] values = {
102             new Integer JavaDoc(1234),
103             new Object JavaDoc(),
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 JavaDoc
122     {
123         Object JavaDoc[] values1 = {
124             null,
125             new Time JavaDoc(1234),
126             new java.sql.Date JavaDoc(1234),
127             new Timestamp JavaDoc(1234),
128             new Time JavaDoc(1234).toString(),
129             new java.util.Date JavaDoc(1234),
130             "00:01:02",
131         };
132
133         Object JavaDoc[] values2 = {
134             null,
135             new Time JavaDoc(1234),
136             new Time JavaDoc(new java.sql.Date JavaDoc(1234).getTime()),
137             new Time JavaDoc(new Timestamp JavaDoc(1234).getTime()),
138             Time.valueOf(new Time JavaDoc(1234).toString()),
139             new Time JavaDoc(1234),
140             new Time JavaDoc(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 JavaDoc
153     {
154         Object JavaDoc[] values1 = {
155             new Integer JavaDoc(1234),
156             new Object JavaDoc(),
157             "bla",
158             "2000.05.05",
159         };
160         Object JavaDoc[] 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 JavaDoc
192     {
193         Object JavaDoc[] less = {
194             null,
195             new java.sql.Time JavaDoc(0),
196             "08:00:00",
197             "08:00:00",
198         };
199
200         Object JavaDoc[] greater = {
201             new java.sql.Time JavaDoc(1234),
202             new java.sql.Time JavaDoc(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 JavaDoc
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 JavaDoc
227     {
228         assertEquals(THIS_TYPE, DataType.forObject(new Time JavaDoc(1234)));
229     }
230
231     public void testAsString() throws Exception JavaDoc
232     {
233         java.sql.Time JavaDoc[] values = {
234             new java.sql.Time JavaDoc(1234),
235         };
236
237         String JavaDoc[] expected = {
238             new java.sql.Time JavaDoc(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 JavaDoc
251     {
252         java.sql.Time JavaDoc[] expected = {
253             null,
254             new Time JavaDoc(1234),
255             new Time JavaDoc(new java.sql.Date JavaDoc(1234).getTime()),
256             new Time JavaDoc(new Timestamp JavaDoc(1234).getTime()),
257             Time.valueOf(new Time JavaDoc(1234).toString()),
258             new Time JavaDoc(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 JavaDoc expectedValue = expected[i];
267             Object JavaDoc actualValue = THIS_TYPE.getSqlValue(i + 1, resultSet);
268             assertEquals("value", expectedValue, actualValue);
269         }
270     }
271
272 }
273
Popular Tags