KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > testsuite > simple > DateTest


1 /*
2  Copyright (C) 2002-2004 MySQL AB
3
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of version 2 of the GNU General Public License as
6  published by the Free Software Foundation.
7
8  There are special exceptions to the terms and conditions of the GPL
9  as it is applied to this software. View the full text of the
10  exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
11  software distribution.
12
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22
23
24  */

25 package testsuite.simple;
26
27 import testsuite.BaseTestCase;
28
29 import java.sql.Connection JavaDoc;
30 import java.sql.Date JavaDoc;
31 import java.sql.PreparedStatement JavaDoc;
32 import java.sql.SQLException JavaDoc;
33 import java.sql.Statement JavaDoc;
34 import java.sql.Timestamp JavaDoc;
35
36 import java.text.DateFormat JavaDoc;
37 import java.text.SimpleDateFormat JavaDoc;
38
39 import java.util.Calendar JavaDoc;
40 import java.util.Properties JavaDoc;
41 import java.util.TimeZone JavaDoc;
42
43 import com.mysql.jdbc.SQLError;
44
45 /**
46  *
47  * @author Mark Matthews
48  * @version $Id: DateTest.java,v 1.1.2.2 2005/05/19 15:52:24 mmatthews Exp $
49  */

50 public class DateTest extends BaseTestCase {
51     // ~ Constructors
52
// -----------------------------------------------------------
53

54     /**
55      * Creates a new DateTest object.
56      *
57      * @param name
58      * DOCUMENT ME!
59      */

60     public DateTest(String JavaDoc name) {
61         super(name);
62     }
63
64     // ~ Methods
65
// ----------------------------------------------------------------
66

67     /**
68      * Runs all test cases in this test suite
69      *
70      * @param args
71      */

72     public static void main(String JavaDoc[] args) {
73         junit.textui.TestRunner.run(DateTest.class);
74     }
75
76     /**
77      * DOCUMENT ME!
78      *
79      * @throws Exception
80      * DOCUMENT ME!
81      */

82     public void setUp() throws Exception JavaDoc {
83         super.setUp();
84         createTestTable();
85     }
86
87     /**
88      * DOCUMENT ME!
89      *
90      * @throws SQLException
91      * DOCUMENT ME!
92      */

93     public void testTimestamp() throws SQLException JavaDoc {
94         this.pstmt = this.conn
95                 .prepareStatement("INSERT INTO DATETEST(tstamp, dt, dtime, tm) VALUES (?, ?, ?, ?)");
96
97         // TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
98
Calendar JavaDoc cal = Calendar.getInstance();
99         cal.set(Calendar.MONTH, 6);
100         cal.set(Calendar.DAY_OF_MONTH, 3);
101         cal.set(Calendar.YEAR, 2002);
102         cal.set(Calendar.HOUR, 7);
103         cal.set(Calendar.MINUTE, 0);
104         cal.set(Calendar.SECOND, 0);
105         cal.set(Calendar.MILLISECOND, 0);
106         cal.set(Calendar.AM_PM, Calendar.AM);
107         cal.getTime();
108         System.out.println(cal);
109
110         // DateFormat df = SimpleDateFormat.getInstance();
111
DateFormat JavaDoc df = new SimpleDateFormat JavaDoc("yyyy/MM/dd HH:mm:ss z");
112
113         Calendar JavaDoc calGMT = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
114         // df.setTimeZone(TimeZone.getTimeZone("GMT"));
115
Timestamp JavaDoc nowTstamp = new Timestamp JavaDoc(cal.getTime().getTime());
116         java.sql.Date JavaDoc nowDate = new java.sql.Date JavaDoc(cal.getTime().getTime());
117         Timestamp JavaDoc nowDatetime = new Timestamp JavaDoc(cal.getTime().getTime());
118         java.sql.Time JavaDoc nowTime = new java.sql.Time JavaDoc(cal.getTime().getTime());
119         System.out
120                 .println("** Times with given calendar (before storing) **\n");
121         System.out.println("TIMESTAMP:\t" + nowTstamp.getTime() + " -> "
122                 + df.format(nowTstamp));
123         System.out.println("DATE:\t\t" + nowDate.getTime() + " -> "
124                 + df.format(nowDate));
125         System.out.println("DATETIME:\t" + nowDatetime.getTime() + " -> "
126                 + df.format(nowDatetime));
127         System.out.println("DATE:\t\t" + nowDate.getTime() + " -> "
128                 + df.format(nowDate));
129         System.out.println("TIME:\t\t" + nowTime.getTime() + " -> "
130                 + df.format(nowTime));
131         System.out.println("\n");
132         this.pstmt.setTimestamp(1, nowTstamp, calGMT);
133         // have to use the same TimeZone as used to create or there will be
134
// shift
135
this.pstmt.setDate(2, nowDate, cal);
136         this.pstmt.setTimestamp(3, nowDatetime, calGMT);
137         // have to use the same TimeZone as used to create or there will be
138
// shift
139
this.pstmt.setTime(4, nowTime, cal);
140         this.pstmt.execute();
141
142         this.pstmt.getUpdateCount();
143         this.pstmt.clearParameters();
144         this.rs = this.stmt.executeQuery("SELECT * from DATETEST");
145
146         java.sql.Date JavaDoc thenDate = null;
147
148         while (this.rs.next()) {
149             Timestamp JavaDoc thenTstamp = this.rs.getTimestamp(1, calGMT);
150             thenDate = this.rs.getDate(2, cal);
151
152             java.sql.Timestamp JavaDoc thenDatetime = this.rs.getTimestamp(3, calGMT);
153
154             java.sql.Time JavaDoc thenTime = this.rs.getTime(4, cal);
155             System.out
156                     .println("** Times with given calendar (retrieved from database) **\n");
157             System.out.println("TIMESTAMP:\t" + thenTstamp.getTime() + " -> "
158                     + df.format(thenTstamp));
159             System.out.println("DATE:\t\t" + thenDate.getTime() + " -> "
160                     + df.format(thenDate));
161             System.out.println("DATETIME:\t" + thenDatetime.getTime() + " -> "
162                     + df.format(thenDatetime));
163             System.out.println("TIME:\t\t" + thenTime.getTime() + " -> "
164                     + df.format(thenTime));
165             System.out.println("\n");
166         }
167
168         this.rs.close();
169         this.rs = null;
170     }
171
172     public void testNanosParsing() throws SQLException JavaDoc {
173         try {
174             this.stmt.executeUpdate("DROP TABLE IF EXISTS testNanosParsing");
175             this.stmt
176                     .executeUpdate("CREATE TABLE testNanosParsing (dateIndex int, field1 VARCHAR(32))");
177             this.stmt
178                     .executeUpdate("INSERT INTO testNanosParsing VALUES (1, '1969-12-31 18:00:00.0'), "
179                             + "(2, '1969-12-31 18:00:00.90'), "
180                             + "(3, '1969-12-31 18:00:00.900'), "
181                             + "(4, '1969-12-31 18:00:00.9000'), "
182                             + "(5, '1969-12-31 18:00:00.90000'), "
183                             + "(6, '1969-12-31 18:00:00.900000'), "
184                             + "(7, '1969-12-31 18:00:00.')");
185
186             this.rs = this.stmt
187                     .executeQuery("SELECT field1 FROM testNanosParsing ORDER BY dateIndex ASC");
188             assertTrue(this.rs.next());
189             assertTrue(this.rs.getTimestamp(1).getNanos() == 0);
190             assertTrue(this.rs.next());
191             assertTrue(this.rs.getTimestamp(1).getNanos() + " != 90", this.rs
192                     .getTimestamp(1).getNanos() == 90);
193             assertTrue(this.rs.next());
194             assertTrue(this.rs.getTimestamp(1).getNanos() + " != 900", this.rs
195                     .getTimestamp(1).getNanos() == 900);
196             assertTrue(this.rs.next());
197             assertTrue(this.rs.getTimestamp(1).getNanos() + " != 9000", this.rs
198                     .getTimestamp(1).getNanos() == 9000);
199             assertTrue(this.rs.next());
200             assertTrue(this.rs.getTimestamp(1).getNanos() + " != 90000",
201                     this.rs.getTimestamp(1).getNanos() == 90000);
202             assertTrue(this.rs.next());
203             assertTrue(this.rs.getTimestamp(1).getNanos() + " != 900000",
204                     this.rs.getTimestamp(1).getNanos() == 900000);
205             assertTrue(this.rs.next());
206
207             try {
208                 this.rs.getTimestamp(1);
209             } catch (SQLException JavaDoc sqlEx) {
210                 assertTrue(SQLError.SQL_STATE_ILLEGAL_ARGUMENT.equals(sqlEx
211                         .getSQLState()));
212             }
213         } finally {
214             this.stmt.executeUpdate("DROP TABLE IF EXISTS testNanosParsing");
215         }
216     }
217
218     private void createTestTable() throws SQLException JavaDoc {
219         //
220
// Catch the error, the table might exist
221
//
222
try {
223             this.stmt.executeUpdate("DROP TABLE DATETEST");
224         } catch (SQLException JavaDoc SQLE) {
225             ;
226         }
227
228         this.stmt
229                 .executeUpdate("CREATE TABLE DATETEST (tstamp TIMESTAMP, dt DATE, dtime DATETIME, tm TIME)");
230     }
231
232     /**
233      * Tests the configurability of all-zero date/datetime/timestamp handling in
234      * the driver.
235      *
236      * @throws Exception
237      * if the test fails.
238      */

239     public void testZeroDateBehavior() throws Exception JavaDoc {
240         try {
241             this.stmt
242                     .executeUpdate("DROP TABLE IF EXISTS testZeroDateBehavior");
243             this.stmt
244                     .executeUpdate("CREATE TABLE testZeroDateBehavior(fieldAsString VARCHAR(32), fieldAsDateTime DATETIME)");
245             this.stmt
246                     .executeUpdate("INSERT INTO testZeroDateBehavior VALUES ('0000-00-00 00:00:00', '0000-00-00 00:00:00')");
247             Properties JavaDoc props = new Properties JavaDoc();
248             props.setProperty("zeroDateTimeBehavior", "round");
249             Connection JavaDoc roundConn = getConnectionWithProps(props);
250             Statement JavaDoc roundStmt = roundConn.createStatement();
251             this.rs = roundStmt
252                     .executeQuery("SELECT fieldAsString, fieldAsDateTime FROM testZeroDateBehavior");
253             this.rs.next();
254
255             assertTrue("0001-01-01".equals(this.rs.getDate(1).toString()));
256             assertTrue("0001-01-01 00:00:00.0".equals(this.rs.getTimestamp(1)
257                     .toString()));
258             assertTrue("0001-01-01".equals(this.rs.getDate(2).toString()));
259             assertTrue("0001-01-01 00:00:00.0".equals(this.rs.getTimestamp(2)
260                     .toString()));
261
262             PreparedStatement JavaDoc roundPrepStmt = roundConn
263                     .prepareStatement("SELECT fieldAsString, fieldAsDateTime FROM testZeroDateBehavior");
264             this.rs = roundPrepStmt.executeQuery();
265             this.rs.next();
266
267             assertTrue("0001-01-01".equals(this.rs.getDate(1).toString()));
268             assertTrue("0001-01-01 00:00:00.0".equals(this.rs.getTimestamp(1)
269                     .toString()));
270             assertTrue("0001-01-01".equals(this.rs.getDate(2).toString()));
271             assertTrue("0001-01-01 00:00:00.0".equals(this.rs.getTimestamp(2)
272                     .toString()));
273
274             props = new Properties JavaDoc();
275             props.setProperty("zeroDateTimeBehavior", "convertToNull");
276             Connection JavaDoc nullConn = getConnectionWithProps(props);
277             Statement JavaDoc nullStmt = nullConn.createStatement();
278             this.rs = nullStmt
279                     .executeQuery("SELECT fieldAsString, fieldAsDateTime FROM testZeroDateBehavior");
280
281             this.rs.next();
282
283             assertTrue(null == this.rs.getDate(1));
284             assertTrue(null == this.rs.getTimestamp(1));
285             assertTrue(null == this.rs.getDate(2));
286             assertTrue(null == this.rs.getTimestamp(2));
287
288             PreparedStatement JavaDoc nullPrepStmt = nullConn
289                     .prepareStatement("SELECT fieldAsString, fieldAsDateTime FROM testZeroDateBehavior");
290             this.rs = nullPrepStmt.executeQuery();
291
292             this.rs.next();
293
294             assertTrue(null == this.rs.getDate(1));
295             assertTrue(null == this.rs.getTimestamp(1));
296             assertTrue(null == this.rs.getDate(2));
297             assertTrue(null == this.rs.getTimestamp(2));
298             assertTrue(null == this.rs.getString(2));
299
300             props = new Properties JavaDoc();
301             props.setProperty("zeroDateTimeBehavior", "exception");
302             Connection JavaDoc exceptionConn = getConnectionWithProps(props);
303             Statement JavaDoc exceptionStmt = exceptionConn.createStatement();
304             this.rs = exceptionStmt
305                     .executeQuery("SELECT fieldAsString, fieldAsDateTime FROM testZeroDateBehavior");
306
307             this.rs.next();
308
309             try {
310                 this.rs.getDate(1);
311                 fail("Exception should have been thrown when trying to retrieve invalid date");
312             } catch (SQLException JavaDoc sqlEx) {
313                 assertTrue(SQLError.SQL_STATE_ILLEGAL_ARGUMENT.equals(sqlEx
314                         .getSQLState()));
315             }
316
317             try {
318                 this.rs.getTimestamp(1);
319                 fail("Exception should have been thrown when trying to retrieve invalid date");
320             } catch (SQLException JavaDoc sqlEx) {
321                 assertTrue(SQLError.SQL_STATE_ILLEGAL_ARGUMENT.equals(sqlEx
322                         .getSQLState()));
323             }
324
325             try {
326                 this.rs.getDate(2);
327                 fail("Exception should have been thrown when trying to retrieve invalid date");
328             } catch (SQLException JavaDoc sqlEx) {
329                 assertTrue(SQLError.SQL_STATE_ILLEGAL_ARGUMENT.equals(sqlEx
330                         .getSQLState()));
331             }
332
333             try {
334                 this.rs.getTimestamp(2);
335                 fail("Exception should have been thrown when trying to retrieve invalid date");
336             } catch (SQLException JavaDoc sqlEx) {
337                 assertTrue(SQLError.SQL_STATE_ILLEGAL_ARGUMENT.equals(sqlEx
338                         .getSQLState()));
339             }
340
341             PreparedStatement JavaDoc exceptionPrepStmt = exceptionConn
342                     .prepareStatement("SELECT fieldAsString, fieldAsDateTime FROM testZeroDateBehavior");
343
344             try {
345                 this.rs = exceptionPrepStmt.executeQuery();
346                 this.rs.next();
347                 this.rs.getDate(2);
348                 fail("Exception should have been thrown when trying to retrieve invalid date");
349             } catch (SQLException JavaDoc sqlEx) {
350                 assertTrue(SQLError.SQL_STATE_ILLEGAL_ARGUMENT.equals(sqlEx
351                         .getSQLState()));
352             }
353
354         } finally {
355             this.stmt
356                     .executeUpdate("DROP TABLE IF EXISTS testZeroDateBehavior");
357         }
358     }
359
360     public void testReggieBug() throws Exception JavaDoc {
361         try {
362             this.stmt.executeUpdate("DROP TABLE IF EXISTS testReggieBug");
363             this.stmt.executeUpdate("CREATE TABLE testReggieBug (field1 DATE)");
364
365             PreparedStatement JavaDoc pStmt = this.conn
366                     .prepareStatement("INSERT INTO testReggieBug VALUES (?)");
367             pStmt.setDate(1, new Date JavaDoc(2004 - 1900, 07, 28));
368             pStmt.executeUpdate();
369             this.rs = this.stmt.executeQuery("SELECT * FROM testReggieBug");
370             this.rs.next();
371             System.out.println(this.rs.getDate(1));
372             this.rs = this.conn.prepareStatement("SELECT * FROM testReggieBug")
373                     .executeQuery();
374             this.rs.next();
375             System.out.println(this.rs.getDate(1));
376
377         } finally {
378             this.stmt.executeUpdate("DROP TABLE IF EXISTS testReggieBug");
379         }
380     }
381 }
382
Popular Tags