~ubuntu-branches/ubuntu/wily/libpgjava/wily

« back to all changes in this revision

Viewing changes to src/interfaces/jdbc/org/postgresql/test/jdbc2/TimeTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Gybas
  • Date: 2002-02-06 23:43:06 UTC
  • Revision ID: james.westby@ubuntu.com-20020206234306-hsg7suqr8q56qg40
Tags: upstream-7.2
ImportĀ upstreamĀ versionĀ 7.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.postgresql.test.jdbc2;
 
2
 
 
3
import org.postgresql.test.JDBC2Tests;
 
4
import junit.framework.TestCase;
 
5
import java.sql.*;
 
6
 
 
7
/*
 
8
 * $Id: TimeTest.java,v 1.4 2001/11/19 22:33:39 momjian Exp $
 
9
 *
 
10
 * Some simple tests based on problems reported by users. Hopefully these will
 
11
 * help prevent previous problems from re-occuring ;-)
 
12
 *
 
13
 */
 
14
public class TimeTest extends TestCase
 
15
{
 
16
 
 
17
        private Connection con;
 
18
 
 
19
        public TimeTest(String name)
 
20
        {
 
21
                super(name);
 
22
        }
 
23
 
 
24
        protected void setUp() throws Exception
 
25
        {
 
26
                con = JDBC2Tests.openDB();
 
27
                JDBC2Tests.createTable(con, "testtime", "tm time");
 
28
        }
 
29
 
 
30
        protected void tearDown() throws Exception
 
31
        {
 
32
                JDBC2Tests.dropTable(con, "testtime");
 
33
                JDBC2Tests.closeDB(con);
 
34
        }
 
35
 
 
36
        /*
 
37
         * Tests the time methods in ResultSet
 
38
         */
 
39
        public void testGetTime()
 
40
        {
 
41
                try
 
42
                {
 
43
                        Statement stmt = con.createStatement();
 
44
 
 
45
                        assertEquals(1, stmt.executeUpdate(JDBC2Tests.insertSQL("testtime", "'01:02:03'")));
 
46
                        assertEquals(1, stmt.executeUpdate(JDBC2Tests.insertSQL("testtime", "'23:59:59'")));
 
47
 
 
48
                        // Fall through helper
 
49
                        timeTest();
 
50
 
 
51
                        assertEquals(2, stmt.executeUpdate("DELETE FROM testtime"));
 
52
                        stmt.close();
 
53
                }
 
54
                catch (Exception ex)
 
55
                {
 
56
                        fail(ex.getMessage());
 
57
                }
 
58
        }
 
59
 
 
60
        /*
 
61
         * Tests the time methods in PreparedStatement
 
62
         */
 
63
        public void testSetTime()
 
64
        {
 
65
                try
 
66
                {
 
67
                        PreparedStatement ps = con.prepareStatement(JDBC2Tests.insertSQL("testtime", "?"));
 
68
                        Statement stmt = con.createStatement();
 
69
 
 
70
                        ps.setTime(1, makeTime(1, 2, 3));
 
71
                        assertEquals(1, ps.executeUpdate());
 
72
 
 
73
                        ps.setTime(1, makeTime(23, 59, 59));
 
74
                        assertEquals(1, ps.executeUpdate());
 
75
 
 
76
                        // Fall through helper
 
77
                        timeTest();
 
78
 
 
79
                        assertEquals(2, stmt.executeUpdate("DELETE FROM testtime"));
 
80
                        stmt.close();
 
81
                        ps.close();
 
82
                }
 
83
                catch (Exception ex)
 
84
                {
 
85
                        fail(ex.getMessage());
 
86
                }
 
87
        }
 
88
 
 
89
        /*
 
90
         * Helper for the TimeTests. It tests what should be in the db
 
91
         */
 
92
        private void timeTest() throws SQLException
 
93
        {
 
94
                Statement st = con.createStatement();
 
95
                ResultSet rs;
 
96
                java.sql.Time t;
 
97
 
 
98
                rs = st.executeQuery(JDBC2Tests.selectSQL("testtime", "tm"));
 
99
                assertNotNull(rs);
 
100
 
 
101
                assertTrue(rs.next());
 
102
                t = rs.getTime(1);
 
103
                assertNotNull(t);
 
104
                assertEquals(makeTime(1, 2, 3), t);
 
105
 
 
106
                assertTrue(rs.next());
 
107
                t = rs.getTime(1);
 
108
                assertNotNull(t);
 
109
                assertEquals(makeTime(23, 59, 59), t);
 
110
 
 
111
                assertTrue(! rs.next());
 
112
 
 
113
                rs.close();
 
114
        }
 
115
 
 
116
        private java.sql.Time makeTime(int h, int m, int s)
 
117
        {
 
118
                return java.sql.Time.valueOf(JDBC2Tests.fix(h, 2) + ":" +
 
119
                                                                         JDBC2Tests.fix(m, 2) + ":" +
 
120
                                                                         JDBC2Tests.fix(s, 2));
 
121
        }
 
122
}