KickJava   Java API By Example, From Geeks To Geeks.

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


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

36
37 public class TimestampDataTypeTest extends AbstractDataTypeTest
38 {
39     private final static DataType THIS_TYPE = DataType.TIMESTAMP;
40
41     public TimestampDataTypeTest(String JavaDoc name)
42     {
43         super(name);
44     }
45
46     /**
47      *
48      */

49     public void testToString() throws Exception JavaDoc
50     {
51         assertEquals("name", "TIMESTAMP", THIS_TYPE.toString());
52     }
53
54     /**
55      *
56      */

57     public void testGetTypeClass() throws Exception JavaDoc
58     {
59         assertEquals("class", Timestamp JavaDoc.class, THIS_TYPE.getTypeClass());
60     }
61
62     public void testIsNumber() throws Exception JavaDoc
63     {
64         assertEquals("is number", false, THIS_TYPE.isNumber());
65     }
66
67     public void testIsDateTime() throws Exception JavaDoc
68     {
69         assertEquals("is date/time", true, THIS_TYPE.isDateTime());
70     }
71
72     public void testTypeCast() throws Exception JavaDoc
73     {
74         Object JavaDoc[] values = {
75             null,
76             new Timestamp JavaDoc(1234),
77             new Date JavaDoc(1234),
78             new Time JavaDoc(1234),
79             new Timestamp JavaDoc(1234).toString(),
80             new Date JavaDoc(1234).toString(),
81             new java.util.Date JavaDoc(1234),
82         };
83
84         Timestamp JavaDoc[] expected = {
85             null,
86             new Timestamp JavaDoc(1234),
87             new Timestamp JavaDoc(new Date JavaDoc(1234).getTime()),
88             new Timestamp JavaDoc(new Time JavaDoc(1234).getTime()),
89             new Timestamp JavaDoc(1234),
90             new Timestamp JavaDoc(Date.valueOf((new Date JavaDoc(1234).toString())).getTime()),
91             new Timestamp JavaDoc(1234),
92         };
93
94         assertEquals("actual vs expected count", values.length, expected.length);
95
96         for (int i = 0; i < values.length; i++)
97         {
98             assertEquals("typecast " + i, expected[i],
99                     THIS_TYPE.typeCast(values[i]));
100         }
101     }
102
103     public void testTypeCastNone() throws Exception JavaDoc
104     {
105         assertEquals("typecast", null, THIS_TYPE.typeCast(ITable.NO_VALUE));
106     }
107
108     public void testTypeCastInvalid() throws Exception JavaDoc
109     {
110         Object JavaDoc[] values = {
111             new Integer JavaDoc(1234),
112             new Object JavaDoc(),
113             "bla",
114             "2000.05.05",
115         };
116
117         for (int i = 0; i < values.length; i++)
118         {
119             try
120             {
121                 THIS_TYPE.typeCast(values[i]);
122                 fail("Should throw TypeCastException - " + i);
123             }
124             catch (TypeCastException e)
125             {
126             }
127         }
128     }
129
130     public void testCompareEquals() throws Exception JavaDoc
131     {
132         Object JavaDoc[] values1 = {
133             null,
134             new Timestamp JavaDoc(1234),
135             new Date JavaDoc(1234),
136             new Time JavaDoc(1234),
137             new Timestamp JavaDoc(1234).toString(),
138             new java.util.Date JavaDoc(1234),
139             "1970-01-01 00:00:00.0",
140         };
141
142         Timestamp JavaDoc[] values2 = {
143             null,
144             new Timestamp JavaDoc(1234),
145             new Timestamp JavaDoc(new Date JavaDoc(1234).getTime()),
146             new Timestamp JavaDoc(new Time JavaDoc(1234).getTime()),
147             Timestamp.valueOf(new Timestamp JavaDoc(1234).toString()),
148             new Timestamp JavaDoc(1234),
149             Timestamp.valueOf("1970-01-01 00:00:00.0"),
150         };
151
152         assertEquals("values count", values1.length, values2.length);
153
154         for (int i = 0; i < values1.length; i++)
155         {
156             assertEquals("compare1 " + i, 0, THIS_TYPE.compare(values1[i], values2[i]));
157             assertEquals("compare2 " + i, 0, THIS_TYPE.compare(values2[i], values1[i]));
158         }
159     }
160
161     public void testCompareInvalid() throws Exception JavaDoc
162     {
163         Object JavaDoc[] values1 = {
164             new Integer JavaDoc(1234),
165             new Object JavaDoc(),
166             "bla",
167             "2000.05.05",
168         };
169         Object JavaDoc[] values2 = {
170             null,
171             null,
172             null,
173             null,
174         };
175
176         assertEquals("values count", values1.length, values2.length);
177
178         for (int i = 0; i < values1.length; i++)
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             try
190             {
191                 THIS_TYPE.compare(values1[i], values2[i]);
192                 fail("Should throw TypeCastException - " + i);
193             }
194             catch (TypeCastException e)
195             {
196             }
197         }
198     }
199
200     public void testCompareDifferent() throws Exception JavaDoc
201     {
202         Object JavaDoc[] less = {
203             null,
204             new java.sql.Date JavaDoc(0),
205             "1974-23-06 23:40:00.0"
206         };
207
208         Object JavaDoc[] greater = {
209             new java.sql.Date JavaDoc(1234),
210             new java.sql.Date JavaDoc(System.currentTimeMillis()),
211             Timestamp.valueOf("2003-01-30 11:42:00.0"),
212         };
213
214         assertEquals("values count", less.length, greater.length);
215
216         for (int i = 0; i < less.length; i++)
217         {
218             assertTrue("less " + i, THIS_TYPE.compare(less[i], greater[i]) < 0);
219             assertTrue("greater " + i, THIS_TYPE.compare(greater[i], less[i]) > 0);
220         }
221     }
222
223     public void testSqlType() throws Exception JavaDoc
224     {
225         assertEquals(THIS_TYPE, DataType.forSqlType(Types.TIMESTAMP));
226         assertEquals("forSqlTypeName", THIS_TYPE, DataType.forSqlTypeName(THIS_TYPE.toString()));
227         assertEquals(Types.TIMESTAMP, THIS_TYPE.getSqlType());
228     }
229
230     public void testForObject() throws Exception JavaDoc
231     {
232         assertEquals(THIS_TYPE, DataType.forObject(new Timestamp JavaDoc(1234)));
233     }
234
235     public void testAsString() throws Exception JavaDoc
236     {
237         java.sql.Timestamp JavaDoc[] values = {
238             new java.sql.Timestamp JavaDoc(1234),
239         };
240
241         String JavaDoc[] expected = {
242             new java.sql.Timestamp JavaDoc(1234).toString(),
243         };
244
245
246         assertEquals("actual vs expected count", values.length, expected.length);
247
248         for (int i = 0; i < values.length; i++)
249         {
250             assertEquals("asString " + i, expected[i], DataType.asString(values[i]));
251         }
252     }
253
254     public void testGetSqlValue() throws Exception JavaDoc
255     {
256         Timestamp JavaDoc[] expected = {
257             null,
258             new Timestamp 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
274
275
276
277
278
Popular Tags