JDBC2Tests.java 4.68 KB
Newer Older
1 2 3 4 5 6 7 8 9
package org.postgresql.test;

import junit.framework.TestSuite;
import junit.framework.TestCase;

import org.postgresql.test.jdbc2.*;
import java.sql.*;

/**
10
 * Executes all known tests for JDBC2 and includes some utility methods.
11
 */
12 13
public class JDBC2Tests extends TestSuite
{
14 15 16
	/**
	 * Returns the Test database JDBC URL
	 */
17 18
	public static String getURL()
	{
19 20 21 22 23 24
		return System.getProperty("database");
	}

	/**
	 * Returns the Postgresql username
	 */
25 26
	public static String getUser()
	{
27 28 29 30 31 32
		return System.getProperty("username");
	}

	/**
	 * Returns the user's password
	 */
33 34
	public static String getPassword()
	{
35 36 37 38 39 40
		return System.getProperty("password");
	}

	/**
	 * Helper - opens a connection.
	 */
41 42 43 44
	public static java.sql.Connection openDB()
	{
		try
		{
45
			Class.forName("org.postgresql.Driver");
46 47 48 49
			return java.sql.DriverManager.getConnection(JDBC2Tests.getURL(), JDBC2Tests.getUser(), JDBC2Tests.getPassword());
		}
		catch (ClassNotFoundException ex)
		{
50
			TestCase.fail(ex.getMessage());
51 52 53
		}
		catch (SQLException ex)
		{
54 55 56 57 58 59 60 61 62
			TestCase.fail(ex.getMessage());
		}
		return null;
	}

	/**
	 * Helper - closes an open connection. This rewrites SQLException to a failed
	 * assertion. It's static so other classes can use it.
	 */
63 64 65 66
	public static void closeDB(Connection con)
	{
		try
		{
67 68
			if (con != null)
				con.close();
69 70 71
		}
		catch (SQLException ex)
		{
72 73 74
			TestCase.fail(ex.getMessage());
		}
	}
75

76 77
	/**
	 * Helper - creates a test table for use by a test
78 79 80
	 */
	public static void createTable(Connection con,
								   String table,
81 82 83 84
								   String columns)
	{
		try
		{
85
			Statement st = con.createStatement();
86 87
			try
			{
88 89
				// Drop the table
				dropTable(con, table);
90 91

				// Now create the table
92
				st.executeUpdate("create table " + table + " (" + columns + ")");
93 94 95
			}
			finally
			{
96 97
				st.close();
			}
98 99 100
		}
		catch (SQLException ex)
		{
101 102 103 104 105 106 107
			TestCase.fail(ex.getMessage());
		}
	}

	/**
	 * Helper - drops a table
	 */
108 109 110 111
	public static void dropTable(Connection con, String table)
	{
		try
		{
112
			Statement stmt = con.createStatement();
113 114
			try
			{
115
				stmt.executeUpdate("DROP TABLE " + table);
116 117 118
			}
			catch (SQLException ex)
			{
119 120
				// ignore
			}
121 122 123
		}
		catch (SQLException ex)
		{
124
			TestCase.fail(ex.getMessage());
125 126 127
		}
	}

128 129 130
	/**
	 * Helper - generates INSERT SQL - very simple
	 */
131 132
	public static String insertSQL(String table, String values)
	{
133
		return insertSQL(table, null, values);
134
	}
135 136 137

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

140 141
		if (columns != null)
			s = s + " (" + columns + ")";
142

143 144 145 146 147 148
		return s + " VALUES (" + values + ")";
	}

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

154 155
	public static String selectSQL(String table, String columns, String where)
	{
156 157
		return selectSQL(table, columns, where, null);
	}
158 159 160

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

163 164 165 166
		if (where != null)
			s = s + " WHERE " + where;
		if (other != null)
			s = s + " " + other;
167

168 169
		return s;
	}
170

171 172 173 174 175
	/**
	 * Helper to prefix a number with leading zeros - ugly but it works...
	 * @param v value to prefix
	 * @param l number of digits (0-10)
	 */
176 177
	public static String fix(int v, int l)
	{
178 179 180 181 182 183 184
		String s = "0000000000".substring(0, l) + Integer.toString(v);
		return s.substring(s.length() - l);
	}

	/**
	 * The main entry point for JUnit
	 */
185 186 187
	public static TestSuite suite()
	{
		TestSuite suite = new TestSuite();
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
		//
		// Add one line per class in our test cases. These should be in order of
		// complexity.

		// ANTTest should be first as it ensures that test parameters are
		// being sent to the suite. It also initialises the database (if required)
		// with some simple global tables (will make each testcase use its own later).
		//
		suite.addTestSuite(ANTTest.class);

		// Basic Driver internals
		suite.addTestSuite(DriverTest.class);
		suite.addTestSuite(ConnectionTest.class);
		suite.addTestSuite(DatabaseMetaDataTest.class);
		suite.addTestSuite(EncodingTest.class);

		// Connectivity/Protocols

		// ResultSet

		// Time, Date, Timestamp
		suite.addTestSuite(DateTest.class);
		suite.addTestSuite(TimeTest.class);
		suite.addTestSuite(TimestampTest.class);

		// PreparedStatement

		// BatchExecute
		suite.addTestSuite(BatchExecuteTest.class);

		// MetaData

		// Other misc tests, based on previous problems users have had or specific
		// features some applications require.
		suite.addTestSuite(JBuilderTest.class);
		suite.addTestSuite(MiscTest.class);

		// Fastpath/LargeObject
		suite.addTestSuite(BlobTest.class);

		// That's all folks
		return suite;
	}
232
}