~ubuntu-branches/ubuntu/hardy/libpgjava/hardy

« back to all changes in this revision

Viewing changes to src/interfaces/jdbc/org/postgresql/test/JDBC2Tests.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;
 
2
 
 
3
import junit.framework.TestSuite;
 
4
import junit.framework.TestCase;
 
5
 
 
6
import org.postgresql.test.jdbc2.*;
 
7
import java.sql.*;
 
8
 
 
9
/*
 
10
 * Executes all known tests for JDBC2 and includes some utility methods.
 
11
 */
 
12
public class JDBC2Tests extends TestSuite
 
13
{
 
14
        /*
 
15
         * Returns the Test database JDBC URL
 
16
         */
 
17
        public static String getURL()
 
18
        {
 
19
                return System.getProperty("database");
 
20
        }
 
21
 
 
22
        /*
 
23
         * Returns the Postgresql username
 
24
         */
 
25
        public static String getUser()
 
26
        {
 
27
                return System.getProperty("username");
 
28
        }
 
29
 
 
30
        /*
 
31
         * Returns the user's password
 
32
         */
 
33
        public static String getPassword()
 
34
        {
 
35
                return System.getProperty("password");
 
36
        }
 
37
 
 
38
        /*
 
39
         * Helper - opens a connection.
 
40
         */
 
41
        public static java.sql.Connection openDB()
 
42
        {
 
43
                try
 
44
                {
 
45
                        Class.forName("org.postgresql.Driver");
 
46
                        return java.sql.DriverManager.getConnection(JDBC2Tests.getURL(), JDBC2Tests.getUser(), JDBC2Tests.getPassword());
 
47
                }
 
48
                catch (ClassNotFoundException ex)
 
49
                {
 
50
                        TestCase.fail(ex.getMessage());
 
51
                }
 
52
                catch (SQLException ex)
 
53
                {
 
54
                        TestCase.fail(ex.getMessage());
 
55
                }
 
56
                return null;
 
57
        }
 
58
 
 
59
        /*
 
60
         * Helper - closes an open connection. This rewrites SQLException to a failed
 
61
         * assertion. It's static so other classes can use it.
 
62
         */
 
63
        public static void closeDB(Connection con)
 
64
        {
 
65
                try
 
66
                {
 
67
                        if (con != null)
 
68
                                con.close();
 
69
                }
 
70
                catch (SQLException ex)
 
71
                {
 
72
                        TestCase.fail(ex.getMessage());
 
73
                }
 
74
        }
 
75
 
 
76
        /*
 
77
         * Helper - creates a test table for use by a test
 
78
         */
 
79
        public static void createTable(Connection con,
 
80
                                                                   String table,
 
81
                                                                   String columns)
 
82
        {
 
83
                try
 
84
                {
 
85
                        Statement st = con.createStatement();
 
86
                        try
 
87
                        {
 
88
                                // Drop the table
 
89
                                dropTable(con, table);
 
90
 
 
91
                                // Now create the table
 
92
                                st.executeUpdate("create table " + table + " (" + columns + ")");
 
93
                        }
 
94
                        finally
 
95
                        {
 
96
                                st.close();
 
97
                        }
 
98
                }
 
99
                catch (SQLException ex)
 
100
                {
 
101
                        TestCase.fail(ex.getMessage());
 
102
                }
 
103
        }
 
104
 
 
105
        /*
 
106
         * Helper - drops a table
 
107
         */
 
108
        public static void dropTable(Connection con, String table)
 
109
        {
 
110
                try
 
111
                {
 
112
                        Statement stmt = con.createStatement();
 
113
                        try
 
114
                        {
 
115
                                stmt.executeUpdate("DROP TABLE " + table);
 
116
                        }
 
117
                        catch (SQLException ex)
 
118
                        {
 
119
                                // ignore
 
120
                        }
 
121
                }
 
122
                catch (SQLException ex)
 
123
                {
 
124
                        TestCase.fail(ex.getMessage());
 
125
                }
 
126
        }
 
127
 
 
128
        /*
 
129
         * Helper - generates INSERT SQL - very simple
 
130
         */
 
131
        public static String insertSQL(String table, String values)
 
132
        {
 
133
                return insertSQL(table, null, values);
 
134
        }
 
135
 
 
136
        public static String insertSQL(String table, String columns, String values)
 
137
        {
 
138
                String s = "INSERT INTO " + table;
 
139
 
 
140
                if (columns != null)
 
141
                        s = s + " (" + columns + ")";
 
142
 
 
143
                return s + " VALUES (" + values + ")";
 
144
        }
 
145
 
 
146
        /*
 
147
         * Helper - generates SELECT SQL - very simple
 
148
         */
 
149
        public static String selectSQL(String table, String columns)
 
150
        {
 
151
                return selectSQL(table, columns, null, null);
 
152
        }
 
153
 
 
154
        public static String selectSQL(String table, String columns, String where)
 
155
        {
 
156
                return selectSQL(table, columns, where, null);
 
157
        }
 
158
 
 
159
        public static String selectSQL(String table, String columns, String where, String other)
 
160
        {
 
161
                String s = "SELECT " + columns + " FROM " + table;
 
162
 
 
163
                if (where != null)
 
164
                        s = s + " WHERE " + where;
 
165
                if (other != null)
 
166
                        s = s + " " + other;
 
167
 
 
168
                return s;
 
169
        }
 
170
 
 
171
        /*
 
172
         * Helper to prefix a number with leading zeros - ugly but it works...
 
173
         * @param v value to prefix
 
174
         * @param l number of digits (0-10)
 
175
         */
 
176
        public static String fix(int v, int l)
 
177
        {
 
178
                String s = "0000000000".substring(0, l) + Integer.toString(v);
 
179
                return s.substring(s.length() - l);
 
180
        }
 
181
 
 
182
        /*
 
183
         * The main entry point for JUnit
 
184
         */
 
185
        public static TestSuite suite()
 
186
        {
 
187
                TestSuite suite = new TestSuite();
 
188
 
 
189
                //
 
190
                // Add one line per class in our test cases. These should be in order of
 
191
                // complexity.
 
192
 
 
193
                // ANTTest should be first as it ensures that test parameters are
 
194
                // being sent to the suite. It also initialises the database (if required)
 
195
                // with some simple global tables (will make each testcase use its own later).
 
196
                //
 
197
                suite.addTestSuite(ANTTest.class);
 
198
 
 
199
                // Basic Driver internals
 
200
                suite.addTestSuite(DriverTest.class);
 
201
                suite.addTestSuite(ConnectionTest.class);
 
202
                suite.addTestSuite(DatabaseMetaDataTest.class);
 
203
                suite.addTestSuite(EncodingTest.class);
 
204
 
 
205
                // Connectivity/Protocols
 
206
 
 
207
                // ResultSet
 
208
                suite.addTestSuite(ResultSetTest.class);
 
209
 
 
210
                // Time, Date, Timestamp
 
211
                suite.addTestSuite(DateTest.class);
 
212
                suite.addTestSuite(TimeTest.class);
 
213
                suite.addTestSuite(TimestampTest.class);
 
214
 
 
215
                // PreparedStatement
 
216
 
 
217
                // BatchExecute
 
218
                suite.addTestSuite(BatchExecuteTest.class);
 
219
 
 
220
                // MetaData
 
221
 
 
222
                // Other misc tests, based on previous problems users have had or specific
 
223
                // features some applications require.
 
224
                suite.addTestSuite(JBuilderTest.class);
 
225
                suite.addTestSuite(MiscTest.class);
 
226
 
 
227
                // Fastpath/LargeObject
 
228
                suite.addTestSuite(BlobTest.class);
 
229
 
 
230
                // That's all folks
 
231
                return suite;
 
232
        }
 
233
}