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