File: TestUtil.java

package info (click to toggle)
libpgjava 8.4-701-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,532 kB
  • ctags: 4,162
  • sloc: java: 33,948; xml: 3,158; makefile: 14; sh: 10
file content (393 lines) | stat: -rw-r--r-- 11,090 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*-------------------------------------------------------------------------
*
* Copyright (c) 2004-2008, PostgreSQL Global Development Group
*
* IDENTIFICATION
*   $PostgreSQL: pgjdbc/org/postgresql/test/TestUtil.java,v 1.23 2008/01/08 06:56:30 jurka Exp $
*
*-------------------------------------------------------------------------
*/
package org.postgresql.test;

import java.sql.*;
import java.util.Properties;

import org.postgresql.jdbc2.AbstractJdbc2Connection;

/**
 * Utility class for JDBC tests
 */
public class TestUtil
{
    /*
     * Returns the Test database JDBC URL
     */
    public static String getURL()
    {
        String protocolVersion = "";
        if (getProtocolVersion() != 0) {
            protocolVersion = "&protocolVersion=" + getProtocolVersion();
        }

        return "jdbc:postgresql://"
                                + getServer() + ":" 
                                + getPort() + "/" 
                                + getDatabase() 
                                + "?prepareThreshold=" + getPrepareThreshold()
                                + "&loglevel=" + getLogLevel()
                                + protocolVersion;
    }

    /*
     * Returns the Test server
     */
    public static String getServer()
    {
        return System.getProperty("server");
    }

    /*
     * Returns the Test port
     */
    public static int getPort()
    {
        return Integer.parseInt(System.getProperty("port"));
    }

    /*
     * Returns the server side prepared statement threshold.
     */
    public static int getPrepareThreshold()
    {
        return Integer.parseInt(System.getProperty("preparethreshold"));
    }

    public static int getProtocolVersion()
    {
        return Integer.parseInt(System.getProperty("protocolVersion"));
    }

    /*
     * Returns the Test database
     */
    public static String getDatabase()
    {
        return System.getProperty("database");
    }

    /*
     * Returns the Postgresql username
     */
    public static String getUser()
    {
        return System.getProperty("username");
    }

    /*
     * Returns the user's password
     */
    public static String getPassword()
    {
        return System.getProperty("password");
    }

    /*
     * Returns the log level to use
     */
    public static int getLogLevel()
    {
        return Integer.parseInt(System.getProperty("loglevel"));
    }

    private static boolean initialized = false;
    public static void initDriver() throws Exception
    {
        synchronized (TestUtil.class) {
            if (initialized)
                return;

            if (getLogLevel() > 0) { 
                // Ant's junit task likes to buffer stdout/stderr and tends to run out of memory.
                // So we put debugging output to a file instead.
                java.io.Writer output = new java.io.FileWriter("postgresql-jdbc-tests.debug.txt", true);
                java.sql.DriverManager.setLogWriter(new java.io.PrintWriter(output,true));
            }
            
            org.postgresql.Driver.setLogLevel(getLogLevel()); // Also loads and registers driver.
            initialized = true;
        }
    }        

    /*
     * Helper - opens a connection.
     */
    public static java.sql.Connection openDB() throws Exception
    {
        return openDB(new Properties());
    }

    /*
     * Helper - opens a connection with the allowance for passing
     * additional parameters, like "compatible".
     */
    public static java.sql.Connection openDB(Properties props) throws Exception
    {
        initDriver();

        props.setProperty("user", getUser());
        props.setProperty("password", getPassword());

        return DriverManager.getConnection(getURL(), props);
    }

    /*
     * Helper - closes an open connection.
     */
    public static void closeDB(Connection con) throws SQLException
    {
        if (con != null)
            con.close();
    }

    /*
     * Helper - creates a test table for use by a test
     */
    public static void createTable(Connection con,
                                   String table,
                                   String columns) throws SQLException
    {
        // by default we don't request oids.
        createTable(con,table,columns,false);
    }

    /*
     * Helper - creates a test table for use by a test
     */
    public static void createTable(Connection con,
                                   String table,
                                   String columns,
                                   boolean withOids) throws SQLException
    {
        Statement st = con.createStatement();
        try
        {
            // Drop the table
            dropTable(con, table);

            // Now create the table
            String sql = "CREATE TABLE " + table + " (" + columns + ") ";

            // Starting with 8.0 oids may be turned off by default.
            // Some tests need them, so they flag that here.
            if (withOids && haveMinimumServerVersion(con,"8.0")) {
                sql += " WITH OIDS";
            }
            st.executeUpdate(sql);
        }
        finally
        {
            st.close();
        }
    }

    /**
     * Helper creates a temporary table
     * @param con Connection
     * @param table String
     * @param columns String
     * @throws SQLException
     */

    public static void createTempTable( Connection con,
                                        String table,
                                        String columns) throws SQLException
    {
        Statement st = con.createStatement();
        try
        {
            // Drop the table
            dropTable(con, table);

            // Now create the table
            st.executeUpdate("create temp table " + table + " (" + columns + ")");
        }
        finally
        {
            st.close();
        }
    }

    /*
     * drop a sequence because older versions don't have dependency
     * information for serials
     */
    public static void dropSequence(Connection con, String sequence) throws SQLException
    {
        Statement stmt = con.createStatement();
        try
        {
            String sql = "DROP SEQUENCE " + sequence;
            stmt.executeUpdate(sql);
        }
        catch (SQLException sqle)
        {
            if (!con.getAutoCommit())
                throw sqle;
        }
    }

    /*
     * Helper - drops a table
     */
    public static void dropTable(Connection con, String table) throws SQLException
    {
        Statement stmt = con.createStatement();
        try
        {
            String sql = "DROP TABLE " + table;
            if (haveMinimumServerVersion(con, "7.3"))
            {
                sql += " CASCADE ";
            }
            stmt.executeUpdate(sql);
        }
        catch (SQLException ex)
        {
            // Since every create table issues a drop table
            // it's easy to get a table doesn't exist error.
            // we want to ignore these, but if we're in a
            // transaction then we've got trouble
            if (!con.getAutoCommit())
                throw ex;
        }
    }

    /*
     * Helper - generates INSERT SQL - very simple
     */
    public static String insertSQL(String table, String values)
    {
        return insertSQL(table, null, values);
    }

    public static String insertSQL(String table, String columns, String values)
    {
        String s = "INSERT INTO " + table;

        if (columns != null)
            s = s + " (" + columns + ")";

        return s + " VALUES (" + values + ")";
    }

    /*
     * Helper - generates SELECT SQL - very simple
     */
    public static String selectSQL(String table, String columns)
    {
        return selectSQL(table, columns, null, null);
    }

    public static String selectSQL(String table, String columns, String where)
    {
        return selectSQL(table, columns, where, null);
    }

    public static String selectSQL(String table, String columns, String where, String other)
    {
        String s = "SELECT " + columns + " FROM " + table;

        if (where != null)
            s = s + " WHERE " + where;
        if (other != null)
            s = s + " " + other;

        return s;
    }

    /*
     * Helper to prefix a number with leading zeros - ugly but it works...
     * @param v value to prefix
     * @param l number of digits (0-10)
     */
    public static String fix(int v, int l)
    {
        String s = "0000000000".substring(0, l) + Integer.toString(v);
        return s.substring(s.length() - l);
    }

    public static String escapeString(Connection con, String value) throws SQLException
    {
        if (con instanceof org.postgresql.jdbc2.AbstractJdbc2Connection)
        {
            return ((org.postgresql.jdbc2.AbstractJdbc2Connection)con).escapeString(value);
        }
        return value;
    }
    
    public static boolean getStandardConformingStrings(Connection con)
    {
        if (con instanceof org.postgresql.jdbc2.AbstractJdbc2Connection)
        {
            return ((org.postgresql.jdbc2.AbstractJdbc2Connection)con).getStandardConformingStrings();
        }
        return false;
    }
    
    /**
     * Determine if the given connection is connected to a server with
     * a version of at least the given version.
     * This is convenient because we are working with a java.sql.Connection,
     * not an Postgres connection.
     */
    public static boolean haveMinimumServerVersion(Connection con, String version) throws SQLException {
        if (con instanceof org.postgresql.jdbc2.AbstractJdbc2Connection)
        {
            return ((org.postgresql.jdbc2.AbstractJdbc2Connection)con).haveMinimumServerVersion(version);
        }
        return false;
    }

    public static boolean haveMinimumJVMVersion(String version) {
        String jvm = java.lang.System.getProperty("java.version");
        return (jvm.compareTo(version) >= 0);
    }

    public static boolean isProtocolVersion( Connection con, int version )
    {
        if ( con instanceof AbstractJdbc2Connection )
        {
            return (version == ((AbstractJdbc2Connection)con).getProtocolVersion());
          
        }
        return false;
    }
    /**
     * Print a ResultSet to System.out.
     * This is useful for debugging tests.
     */
    public static void printResultSet(ResultSet rs) throws SQLException {
        ResultSetMetaData rsmd = rs.getMetaData();
        for (int i = 1; i <= rsmd.getColumnCount(); i++)
        {
            if (i != 1)
            {
                System.out.print(", ");
            }
            System.out.print(rsmd.getColumnName(i));
        }
        System.out.println();
        while (rs.next())
        {
            for (int i = 1; i <= rsmd.getColumnCount(); i++)
            {
                if (i != 1)
                {
                    System.out.print(", ");
                }
                System.out.print(rs.getString(i));
            }
            System.out.println();
        }
    }
}