001: /*-------------------------------------------------------------------------
002: *
003: * Copyright (c) 2004-2005, PostgreSQL Global Development Group
004: *
005: * IDENTIFICATION
006: * $PostgreSQL: pgjdbc/org/postgresql/test/TestUtil.java,v 1.22 2006/12/01 08:53:46 jurka Exp $
007: *
008: *-------------------------------------------------------------------------
009: */
010: package org.postgresql.test;
011:
012: import java.sql.*;
013: import java.util.Properties;
014:
015: import org.postgresql.jdbc2.AbstractJdbc2Connection;
016:
017: /**
018: * Utility class for JDBC tests
019: */
020: public class TestUtil {
021: /*
022: * Returns the Test database JDBC URL
023: */
024: public static String getURL() {
025: String protocolVersion = "";
026: if (getProtocolVersion() != 0) {
027: protocolVersion = "&protocolVersion="
028: + getProtocolVersion();
029: }
030:
031: return "jdbc:postgresql://" + getServer() + ":" + getPort()
032: + "/" + getDatabase() + "?prepareThreshold="
033: + getPrepareThreshold() + "&loglevel=" + getLogLevel()
034: + protocolVersion;
035: }
036:
037: /*
038: * Returns the Test server
039: */
040: public static String getServer() {
041: return System.getProperty("server");
042: }
043:
044: /*
045: * Returns the Test port
046: */
047: public static int getPort() {
048: return Integer.parseInt(System.getProperty("port"));
049: }
050:
051: /*
052: * Returns the server side prepared statement threshold.
053: */
054: public static int getPrepareThreshold() {
055: return Integer.parseInt(System.getProperty("preparethreshold"));
056: }
057:
058: public static int getProtocolVersion() {
059: return Integer.parseInt(System.getProperty("protocolVersion"));
060: }
061:
062: /*
063: * Returns the Test database
064: */
065: public static String getDatabase() {
066: return System.getProperty("database");
067: }
068:
069: /*
070: * Returns the Postgresql username
071: */
072: public static String getUser() {
073: return System.getProperty("username");
074: }
075:
076: /*
077: * Returns the user's password
078: */
079: public static String getPassword() {
080: return System.getProperty("password");
081: }
082:
083: /*
084: * Returns the log level to use
085: */
086: public static int getLogLevel() {
087: return Integer.parseInt(System.getProperty("loglevel"));
088: }
089:
090: private static boolean initialized = false;
091:
092: public static void initDriver() throws Exception {
093: synchronized (TestUtil.class) {
094: if (initialized)
095: return;
096:
097: if (getLogLevel() > 0) {
098: // Ant's junit task likes to buffer stdout/stderr and tends to run out of memory.
099: // So we put debugging output to a file instead.
100: java.io.Writer output = new java.io.FileWriter(
101: "postgresql-jdbc-tests.debug.txt", true);
102: java.sql.DriverManager
103: .setLogWriter(new java.io.PrintWriter(output,
104: true));
105: }
106:
107: org.postgresql.Driver.setLogLevel(getLogLevel()); // Also loads and registers driver.
108: initialized = true;
109: }
110: }
111:
112: /*
113: * Helper - opens a connection.
114: */
115: public static java.sql.Connection openDB() throws Exception {
116: return openDB(new Properties());
117: }
118:
119: /*
120: * Helper - opens a connection with the allowance for passing
121: * additional parameters, like "compatible".
122: */
123: public static java.sql.Connection openDB(Properties props)
124: throws Exception {
125: initDriver();
126:
127: props.setProperty("user", getUser());
128: props.setProperty("password", getPassword());
129:
130: return DriverManager.getConnection(getURL(), props);
131: }
132:
133: /*
134: * Helper - closes an open connection.
135: */
136: public static void closeDB(Connection con) throws SQLException {
137: if (con != null)
138: con.close();
139: }
140:
141: /*
142: * Helper - creates a test table for use by a test
143: */
144: public static void createTable(Connection con, String table,
145: String columns) throws SQLException {
146: // by default we don't request oids.
147: createTable(con, table, columns, false);
148: }
149:
150: /*
151: * Helper - creates a test table for use by a test
152: */
153: public static void createTable(Connection con, String table,
154: String columns, boolean withOids) throws SQLException {
155: Statement st = con.createStatement();
156: try {
157: // Drop the table
158: dropTable(con, table);
159:
160: // Now create the table
161: String sql = "CREATE TABLE " + table + " (" + columns
162: + ") ";
163:
164: // Starting with 8.0 oids may be turned off by default.
165: // Some tests need them, so they flag that here.
166: if (withOids && haveMinimumServerVersion(con, "8.0")) {
167: sql += " WITH OIDS";
168: }
169: st.executeUpdate(sql);
170: } finally {
171: st.close();
172: }
173: }
174:
175: /**
176: * Helper creates a temporary table
177: * @param con Connection
178: * @param table String
179: * @param columns String
180: * @throws SQLException
181: */
182:
183: public static void createTempTable(Connection con, String table,
184: String columns) throws SQLException {
185: Statement st = con.createStatement();
186: try {
187: // Drop the table
188: dropTable(con, table);
189:
190: // Now create the table
191: st.executeUpdate("create temp table " + table + " ("
192: + columns + ")");
193: } finally {
194: st.close();
195: }
196: }
197:
198: /*
199: * drop a sequence because older versions don't have dependency
200: * information for serials
201: */
202: public static void dropSequence(Connection con, String sequence)
203: throws SQLException {
204: Statement stmt = con.createStatement();
205: try {
206: String sql = "DROP SEQUENCE " + sequence;
207: stmt.executeUpdate(sql);
208: } catch (SQLException sqle) {
209: if (!con.getAutoCommit())
210: throw sqle;
211: }
212: }
213:
214: /*
215: * Helper - drops a table
216: */
217: public static void dropTable(Connection con, String table)
218: throws SQLException {
219: Statement stmt = con.createStatement();
220: try {
221: String sql = "DROP TABLE " + table;
222: if (haveMinimumServerVersion(con, "7.3")) {
223: sql += " CASCADE ";
224: }
225: stmt.executeUpdate(sql);
226: } catch (SQLException ex) {
227: // Since every create table issues a drop table
228: // it's easy to get a table doesn't exist error.
229: // we want to ignore these, but if we're in a
230: // transaction then we've got trouble
231: if (!con.getAutoCommit())
232: throw ex;
233: }
234: }
235:
236: /*
237: * Helper - generates INSERT SQL - very simple
238: */
239: public static String insertSQL(String table, String values) {
240: return insertSQL(table, null, values);
241: }
242:
243: public static String insertSQL(String table, String columns,
244: String values) {
245: String s = "INSERT INTO " + table;
246:
247: if (columns != null)
248: s = s + " (" + columns + ")";
249:
250: return s + " VALUES (" + values + ")";
251: }
252:
253: /*
254: * Helper - generates SELECT SQL - very simple
255: */
256: public static String selectSQL(String table, String columns) {
257: return selectSQL(table, columns, null, null);
258: }
259:
260: public static String selectSQL(String table, String columns,
261: String where) {
262: return selectSQL(table, columns, where, null);
263: }
264:
265: public static String selectSQL(String table, String columns,
266: String where, String other) {
267: String s = "SELECT " + columns + " FROM " + table;
268:
269: if (where != null)
270: s = s + " WHERE " + where;
271: if (other != null)
272: s = s + " " + other;
273:
274: return s;
275: }
276:
277: /*
278: * Helper to prefix a number with leading zeros - ugly but it works...
279: * @param v value to prefix
280: * @param l number of digits (0-10)
281: */
282: public static String fix(int v, int l) {
283: String s = "0000000000".substring(0, l) + Integer.toString(v);
284: return s.substring(s.length() - l);
285: }
286:
287: public static String escapeString(Connection con, String value)
288: throws SQLException {
289: if (con instanceof org.postgresql.jdbc2.AbstractJdbc2Connection) {
290: return ((org.postgresql.jdbc2.AbstractJdbc2Connection) con)
291: .escapeString(value);
292: }
293: return value;
294: }
295:
296: public static boolean getStandardConformingStrings(Connection con) {
297: if (con instanceof org.postgresql.jdbc2.AbstractJdbc2Connection) {
298: return ((org.postgresql.jdbc2.AbstractJdbc2Connection) con)
299: .getStandardConformingStrings();
300: }
301: return false;
302: }
303:
304: /**
305: * Determine if the given connection is connected to a server with
306: * a version of at least the given version.
307: * This is convenient because we are working with a java.sql.Connection,
308: * not an Postgres connection.
309: */
310: public static boolean haveMinimumServerVersion(Connection con,
311: String version) throws SQLException {
312: if (con instanceof org.postgresql.jdbc2.AbstractJdbc2Connection) {
313: return ((org.postgresql.jdbc2.AbstractJdbc2Connection) con)
314: .haveMinimumServerVersion(version);
315: }
316: return false;
317: }
318:
319: public static boolean haveMinimumJVMVersion(String version) {
320: String jvm = java.lang.System.getProperty("java.version");
321: return (jvm.compareTo(version) >= 0);
322: }
323:
324: public static boolean isProtocolVersion(Connection con, int version) {
325: if (con instanceof AbstractJdbc2Connection) {
326: return (version == ((AbstractJdbc2Connection) con)
327: .getProtocolVersion());
328:
329: }
330: return false;
331: }
332:
333: /**
334: * Print a ResultSet to System.out.
335: * This is useful for debugging tests.
336: */
337: public static void printResultSet(ResultSet rs) throws SQLException {
338: ResultSetMetaData rsmd = rs.getMetaData();
339: for (int i = 1; i <= rsmd.getColumnCount(); i++) {
340: if (i != 1) {
341: System.out.print(", ");
342: }
343: System.out.print(rsmd.getColumnName(i));
344: }
345: System.out.println();
346: while (rs.next()) {
347: for (int i = 1; i <= rsmd.getColumnCount(); i++) {
348: if (i != 1) {
349: System.out.print(", ");
350: }
351: System.out.print(rs.getString(i));
352: }
353: System.out.println();
354: }
355: }
356: }
|