|
| 1 | +PostgreSQL/JDBC Test Suite Howto |
| 2 | +================================ |
| 3 | +1 Introduction |
| 4 | +2 Installation |
| 5 | +3 Configuration |
| 6 | +4 Running the test suite |
| 7 | +5 Extending the test suite with new tests |
| 8 | +6 Guidelines for developing new tests |
| 9 | +7 Example |
| 10 | +8 Running the JDBC 2 test suite from Sun against PostgreSQL |
| 11 | +9 Credits, feedback |
| 12 | + |
| 13 | + |
| 14 | +1 Introduction |
| 15 | +-------------- |
| 16 | +The PostgreSQL source tree contains an automated test suite for |
| 17 | +the JDBC driver. This document explains how to install, |
| 18 | +configure and run this test suite. Furthermore, it offers |
| 19 | +guidelines and an example for developers to add new test cases. |
| 20 | + |
| 21 | +Sun provides two standard JDBC test suites that you may also |
| 22 | +find useful. |
| 23 | +http://java.sun.com/products/jdbc/download2.html (JDBC 1) |
| 24 | +http://java.sun.com/products/jdbc/jdbctestsuite-1_2_1.html (JDBC |
| 25 | +2, including J2EE requirements) |
| 26 | +The JDBC 2 test suite is covered in section 8 below. The JDBC 1 |
| 27 | +test suite is not currently covered in this document. |
| 28 | + |
| 29 | +2 Installation |
| 30 | +-------------- |
| 31 | +Of course, you need to have a Java 2 JDK or JRE installed. The |
| 32 | +standard JDK from Sun is OK. You can download it from |
| 33 | +http://java.sun.com/. |
| 34 | + |
| 35 | +You need to install the Ant build utility. You can download it |
| 36 | +from http://jakarta.apache.org/ant/. |
| 37 | + |
| 38 | +You also need to install the JUnit testing framework. You can |
| 39 | +download it from http://www.junit.org/. Add junit.jar to your |
| 40 | +CLASSPATH before you perform the following steps. Ant will |
| 41 | +dynamically detect that JUnit is present and then build the JDBC |
| 42 | +test suite. |
| 43 | + |
| 44 | +You need to install and build the PostgreSQL source tree. You |
| 45 | +can download it from http://www.postgresql.org/devel-corner/. |
| 46 | +See README and INSTALL in the top of the tree for more |
| 47 | +information. |
| 48 | + |
| 49 | +You should run ./configure with the command line option |
| 50 | +--with-java. You may also want to use --with-pgport to compile a |
| 51 | +non-standard default port number (e.g. 5433) into all |
| 52 | +components. This will cause the server to listen on this |
| 53 | +non-standard port and it will cause the JDBC driver to connect |
| 54 | +to this port by default. In this way your testing environment is |
| 55 | +easily separated from other PostgreSQL applications on the same |
| 56 | +system. |
| 57 | + |
| 58 | +In this Howto we'll use $JDBC_SRC to refer to the directory |
| 59 | +src/interfaces/jdbc of the PostgreSQL source tree in your |
| 60 | +environment. The test suite is located in the subdirectory |
| 61 | +$JDBC_SRC/org/postgresql/test. |
| 62 | + |
| 63 | +3 Configuration |
| 64 | +--------------- |
| 65 | +The test suite requires a PostgreSQL database to run the tests |
| 66 | +against and a user to login as. For a full regression test of |
| 67 | +the entire PostgreSQL system, you should run the test against a |
| 68 | +server built from the same source tree as the driver you're |
| 69 | +testing. The tests will create and drop many objects in this |
| 70 | +database, so it should not contain production tables to avoid |
| 71 | +loss of data. We recommend you assign the following names: |
| 72 | + |
| 73 | + database: test |
| 74 | + username: test |
| 75 | + password: password |
| 76 | + |
| 77 | +These names correspond with the default names set for the test |
| 78 | +suite in $JDBC_SRC/build.xml. If you have chosen other names you |
| 79 | +need to edit this file and change the properties "database", |
| 80 | +"username" and "password" accordingly. |
| 81 | + |
| 82 | +4 Running the test suite |
| 83 | +------------------------ |
| 84 | +%cd $JDBC_SRC |
| 85 | +%make |
| 86 | +%make check |
| 87 | + |
| 88 | +This will run the command line version of JUnit. If you'd like |
| 89 | +to see an animated coloured progress bar as the tests are |
| 90 | +executed, you may want to use one of the GUI versions of the |
| 91 | +test runner. See the JUnit documentation for more information. |
| 92 | + |
| 93 | +If the test suite reports errors or failures that you cannot |
| 94 | +explain, please post the relevant parts of the output to the |
| 95 | +mailing list pgsql-jdbc@postgresql.org. |
| 96 | + |
| 97 | +5 Extending the test suite with new tests |
| 98 | +----------------------------------------- |
| 99 | +If you're not familiar with JUnit, we recommend that you |
| 100 | +first read the introductory article "JUnit Test Infected: |
| 101 | +Programmers Love Writing Tests" on |
| 102 | +http://junit.sourceforge.net/doc/testinfected/testing.htm. |
| 103 | +Before continuing, you should ensure you understand the |
| 104 | +following concepts: test suite, test case, test, fixture, |
| 105 | +assertion, failure. |
| 106 | + |
| 107 | +The test suite consists of test cases, which consist of tests. |
| 108 | +A test case is a collection of tests that test a particular |
| 109 | +feature. The test suite is a collection of test cases that |
| 110 | +together test the driver - and to an extent the PostgreSQL |
| 111 | +backend - as a whole. |
| 112 | + |
| 113 | +If you decide to add a test to an existing test case, all you |
| 114 | +need to do is add a method with a name that begins with "test" |
| 115 | +and which takes no arguments. JUnit will dynamically find this |
| 116 | +method using reflection and run it when it runs the test case. |
| 117 | +In your test method you can use the fixture that is setup for it |
| 118 | +by the test case. |
| 119 | + |
| 120 | +If you decide to add a new test case, you should do two things: |
| 121 | +1) Add a class that extends junit.framework.TestCase. It should |
| 122 | +contain setUp() and tearDown() methods that create and destroy |
| 123 | +the fixture respectively. |
| 124 | +2) Edit $JDBC_SRC/org/postgresql/test/JDBC2Tests.java and add a |
| 125 | +suite.addTestSuite() call for your class. This will make the |
| 126 | +test case part of the test suite. |
| 127 | + |
| 128 | +6 Guidelines for developing new tests |
| 129 | +------------------------------------- |
| 130 | +Every test should create and drop its own tables. We suggest to |
| 131 | +consider database objects (e.g. tables) part of the fixture for |
| 132 | +the tests in the test case. The test should also succeed when a |
| 133 | +table by the same name already exists in the test database, e.g. |
| 134 | +by dropping the table before running the test (ignoring errors). |
| 135 | +The recommended pattern for creating and dropping tables can be |
| 136 | +found in the example in section 7 below. |
| 137 | + |
| 138 | +Please note that JUnit provides several convenience methods to |
| 139 | +check for conditions. See the TestCase class in the Javadoc |
| 140 | +documentation of JUnit, which is installed on your system. For |
| 141 | +example, you can compare two integers using |
| 142 | +TestCase.assertEquals(int expected, int actual). This method |
| 143 | +will print both values in case of a failure. |
| 144 | + |
| 145 | +To simply report a failure use TestCase.fail(). |
| 146 | + |
| 147 | +The JUnit FAQ explains how to test for a thrown exception. |
| 148 | + |
| 149 | +Avoid the use of the deprecated TestCase.assert(), since it will |
| 150 | +collide with the new assert keyword in the Java 2 platform |
| 151 | +version 1.4. |
| 152 | + |
| 153 | +As a rule, the test suite should succeed. Any errors or failures |
| 154 | +- which may be caused by bugs in the JDBC driver, the backend or |
| 155 | +the test suite - should be fixed ASAP. Don't let a test fail |
| 156 | +just to make it clear that something needs to be fixed somewhere. |
| 157 | +That's what the TODO lists are for. |
| 158 | + |
| 159 | +Add some comments to your tests to explain to others what it is |
| 160 | +you're testing. A long sequence of JDBC method calls and JUnit |
| 161 | +assertions can be hard to comprehend. |
| 162 | + |
| 163 | +For example, in the comments you can explain where a certain test |
| 164 | +condition originates from. Is it a JDBC requirement, PostgreSQL |
| 165 | +behaviour or the intended implementation of a feature? |
| 166 | + |
| 167 | +7 Example (incomplete) |
| 168 | +---------------------- |
| 169 | +package org.postgresql.test.jdbc2; |
| 170 | + |
| 171 | +import org.postgresql.test.JDBC2Tests; |
| 172 | +import junit.framework.TestCase; |
| 173 | +import java.sql.*; |
| 174 | + |
| 175 | +/** |
| 176 | + * Test case for ... |
| 177 | + */ |
| 178 | +public class FooTest extends TestCase { |
| 179 | + |
| 180 | + private Connection con; |
| 181 | + private Statement stmt; |
| 182 | + |
| 183 | + public FooTest(String name) { |
| 184 | + super(name); |
| 185 | + } |
| 186 | + |
| 187 | + protected void setUp() throws Exception { |
| 188 | + con = JDBC2Tests.openDB(); |
| 189 | + stmt = con.createStatement(); |
| 190 | + |
| 191 | + // Drop the test table if it already exists for some |
| 192 | + // reason. It is not an error if it doesn't exist. |
| 193 | + try { |
| 194 | + stmt.executeUpdate("DROP TABLE testfoo"); |
| 195 | + } catch (SQLException e) { |
| 196 | + // Intentionally ignore. We cannot distinguish |
| 197 | + // "table does not exist" from other errors, since |
| 198 | + // PostgreSQL doesn't support error codes yet. |
| 199 | + } |
| 200 | + |
| 201 | + stmt.executeUpdate( |
| 202 | + "CREATE TABLE testfoo(pk INTEGER, col1 INTEGER)"); |
| 203 | + stmt.executeUpdate("INSERT INTO testfoo VALUES(1, 0)"); |
| 204 | + |
| 205 | + // You may want to call con.setAutoCommit(false) at |
| 206 | + // this point, if most tests in this test case require |
| 207 | + // the use of transactions. |
| 208 | + } |
| 209 | + |
| 210 | + protected void tearDown() throws Exception { |
| 211 | + con.setAutoCommit(true); |
| 212 | + if (stmt != null) { |
| 213 | + stmt.executeUpdate("DROP TABLE testfoo"); |
| 214 | + stmt.close(); |
| 215 | + } |
| 216 | + if (con != null) { |
| 217 | + JDBC2Tests.closeDB(con); |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + public void testFoo() { |
| 222 | + // Use the assert methods in junit.framework.TestCase |
| 223 | + // for the actual tests |
| 224 | + |
| 225 | + // Just some silly examples |
| 226 | + assertNotNull(con); |
| 227 | + if (stmt == null) { |
| 228 | + fail("Where is my statement?"); |
| 229 | + } |
| 230 | + } |
| 231 | + |
| 232 | + public void testBar() { |
| 233 | + // Another test. |
| 234 | + } |
| 235 | +} |
| 236 | + |
| 237 | +8. Running the JDBC 2 test suite from Sun against PostgreSQL |
| 238 | +------------------------------------------------------------ |
| 239 | +Download the test suite from |
| 240 | +http://java.sun.com/products/jdbc/jdbctestsuite-1_2_1.html |
| 241 | +This is the JDBC 2 test suite that includes J2EE requirements. |
| 242 | + |
| 243 | +1. Configure PostgreSQL so that it accepts TCP/IP connections and |
| 244 | + start the server. Prepare PostgreSQL by creating two users (cts1 |
| 245 | + and cts2) and two databases (DB1 and DB2) in the cluster that is |
| 246 | + going to be used for JDBC testing. |
| 247 | + |
| 248 | +2. Download the latest release versions of the J2EE, J2SE, and JDBC |
| 249 | + test suite from Sun's Java site (http://java.sun.com), and install |
| 250 | + according to Sun's documentation. |
| 251 | + |
| 252 | +3. The following environment variables should be set: |
| 253 | + |
| 254 | + CTS_HOME=<path where JDBC test suite installed (eg: /usr/local/jdbccts)> |
| 255 | + J2EE_HOME=<path where J2EE installed (eg: /usr/local/j2sdkee1.2.1)> |
| 256 | + JAVA_HOME=<path where J2SE installed (eg: /usr/local/jdk1.3.1)> |
| 257 | + NO_JAVATEST=Y |
| 258 | + LOCAL_CLASSES=<path to PostgreSQL JDBC driver jar> |
| 259 | + |
| 260 | +4. In $J2EE_HOME/config/default.properties: |
| 261 | + |
| 262 | + jdbc.drivers=org.postgresql.Driver |
| 263 | + jdbc.datasources=jdbc/DB1|jdbc:postgresql://localhost:5432/DB1|jdbc/DB2|jdbc:postgresq://localhost:5432/DB2 |
| 264 | + |
| 265 | + Of course, if PostgreSQL is running on a computer different from |
| 266 | + the one running the application server, localhost should be changed |
| 267 | + to the proper host. Also, 5432 should be changed to whatever port |
| 268 | + PostgreSQL is listening on (5432 is the default). |
| 269 | + |
| 270 | + In $J2EE_HOME/bin/userconfig.sh: |
| 271 | + |
| 272 | + Add $CTS_HOME/lib/harness.jar, $CTS_HOME/lib/moo.jar, |
| 273 | + $CTS_HOME/lib/util.jar to J2EE_CLASSPATH. Also add the path to |
| 274 | + the PostgreSQL JDBC jar to J2EE_CLASSPATH. Set the JAVA_HOME |
| 275 | + variable to where you installed the J2SE. You should end up with |
| 276 | + something like this: |
| 277 | + |
| 278 | + CTS_HOME=/home/liams/linux/java/jdbccts |
| 279 | + J2EE_CLASSPATH=/home/liams/work/inst/postgresql-7.1.2/share/java/postgresql.jar:$CTS_HOME/lib/harness.jar:$CTS_HOME/lib/moo.jar:$CTS_HOME/lib/util.jar |
| 280 | + export J2EE_CLASSPATH |
| 281 | + |
| 282 | + JAVA_HOME=/home/liams/linux/java/jdk1.3.1 |
| 283 | + export JAVA_HOME |
| 284 | + |
| 285 | + In $CTS_HOME/bin/cts.jte: |
| 286 | + |
| 287 | + webServerHost=localhost |
| 288 | + webServerPort=8000 |
| 289 | + servletServerHost=localhost |
| 290 | + servletServerPort=8000 |
| 291 | + |
| 292 | +5. Start the application server (j2ee): |
| 293 | + |
| 294 | + $ cd $J2EE_HOME |
| 295 | + $ bin/j2ee -verbose |
| 296 | + |
| 297 | + The server can be stopped after the tests have finished: |
| 298 | + |
| 299 | + $ cd $J2EE_HOME |
| 300 | + $ bin/j2ee -stop |
| 301 | + |
| 302 | +6. Run the JDBC tests: |
| 303 | + |
| 304 | + $ cd $CTS_HOME/tests/jdbc/ee |
| 305 | + $ make jdbc-tests |
| 306 | + |
| 307 | +At the time of writing of this document, a great number of tests |
| 308 | +in this test suite fail. |
| 309 | + |
| 310 | +9 Credits, feedback |
| 311 | +------------------- |
| 312 | +The parts of this document describing the PostgreSQL test suite |
| 313 | +were originally written by Rene Pijlman. Liam Stewart contributed |
| 314 | +the section on the Sun JDBC 2 test suite. |
| 315 | + |
| 316 | +Please send your questions about the JDBC test suites or suggestions |
| 317 | +for improvement to the pgsql-jdbc@postgresql.org mailing list. |
| 318 | + |
| 319 | +The source of this document is maintained in |
| 320 | +src/interfaces/jdbc/org/postgresql/test/README in CVS. Patches for |
| 321 | +improvement can be send to the mailing list |
| 322 | +pgsql-patches@postgresql.org. |
0 commit comments