Skip to content

Commit

Permalink
test: migrate tests to Junit4 (#883)
Browse files Browse the repository at this point in the history
Migrate XATestSuite, Jdbc2TestSuite, Jdbc4TestSuite, ExtensionsTestSuite test suites

see #205
  • Loading branch information
AlexElin authored and vlsi committed Jul 26, 2017
1 parent 2951a95 commit 5c12da1
Show file tree
Hide file tree
Showing 13 changed files with 229 additions and 312 deletions.
5 changes: 2 additions & 3 deletions pgjdbc/src/test/java/org/postgresql/test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,11 @@ In your test method you can use the fixture that is setup for it
by the test case.

If you decide to add a new test case, you should do two things:
1) Add a class that extends junit.framework.TestCase. It should
1) Add a test class. It should
contain setUp() and tearDown() methods that create and destroy
the fixture respectively.
2) Edit $JDBC_SRC/org/postgresql/test/jdbc2/Jdbc2TestSuite.java or
$JDBC_SRC/org/postgresql/test/jdbc3/Jdbc3TestSuite.java and add a
suite.addTestSuite() call for your class. This will make the test case
$JDBC_SRC/org/postgresql/test/jdbc3/Jdbc3TestSuite.java and add your class. This will make the test case
part of the test suite.

6 Guidelines for developing new tests
Expand Down
7 changes: 3 additions & 4 deletions pgjdbc/src/test/java/org/postgresql/test/TestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,7 @@ public static File getFile(String name) {
* @return connection using a priviliged user mostly for tests that the ability to load C
* functions now as of 4/14
*/
public static java.sql.Connection openPrivilegedDB() throws Exception {

public static Connection openPrivilegedDB() throws Exception {
initDriver();
Properties properties = new Properties();
properties.setProperty("user", getPrivilegedUser());
Expand All @@ -274,15 +273,15 @@ public static java.sql.Connection openPrivilegedDB() throws Exception {
*
* @return connection
*/
public static java.sql.Connection openDB() throws Exception {
public static 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 {
public static Connection openDB(Properties props) throws Exception {
initDriver();

// Allow properties to override the user name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,14 @@

package org.postgresql.test.extensions;

import org.postgresql.test.TestUtil;

import junit.framework.JUnit4TestAdapter;
import junit.framework.TestSuite;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;

/*
* Executes all known tests for PostgreSQL extensions supported by JDBC driver
*/
public class ExtensionsTestSuite extends TestSuite {

/*
* The main entry point for JUnit
*/
public static TestSuite suite() throws Exception {
TestSuite suite = new TestSuite();

Connection connection = TestUtil.openDB();
try {
if (isHStoreEnabled(connection)) {
suite.addTest(new JUnit4TestAdapter(HStoreTest.class));
}
} finally {
connection.close();
}

return suite;
}

/**
* Not all servers have hstore extensions installed.
*/
private static boolean isHStoreEnabled(Connection conn) {
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT 'a=>1'::hstore::text");
rs.close();
stmt.close();
return true;
} catch (SQLException sqle) {
return false;
}
}

@RunWith(Suite.class)
@Suite.SuiteClasses(HStoreTest.class)
public class ExtensionsTestSuite {
}

32 changes: 18 additions & 14 deletions pgjdbc/src/test/java/org/postgresql/test/extensions/HStoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import org.postgresql.core.ServerVersion;
import org.postgresql.jdbc.PreferQueryMode;
import org.postgresql.test.TestUtil;
import org.postgresql.test.jdbc2.BaseTest4;

import org.junit.Assume;
Expand All @@ -21,6 +20,7 @@
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -31,26 +31,30 @@

public class HStoreTest extends BaseTest4 {

private Connection _conn;

@Override
public void setUp() throws Exception {
super.setUp();
_conn = con;
Assume.assumeTrue("server has installed hstore", isHStoreEnabled(con));
Assume.assumeFalse("hstore is not supported in simple protocol only mode",
preferQueryMode == PreferQueryMode.SIMPLE);
Assume.assumeTrue("hstore requires PostgreSQL 8.3+",
TestUtil.haveMinimumServerVersion(con, ServerVersion.v8_3));
assumeMinimumServerVersion("hstore requires PostgreSQL 8.3+", ServerVersion.v8_3);
}

@Override
public void tearDown() throws SQLException {
TestUtil.closeDB(_conn);
private static boolean isHStoreEnabled(Connection conn) {
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT 'a=>1'::hstore::text");
rs.close();
stmt.close();
return true;
} catch (SQLException sqle) {
return false;
}
}

@Test
public void testHStoreSelect() throws SQLException {
PreparedStatement pstmt = _conn.prepareStatement("SELECT 'a=>1,b=>2'::hstore");
PreparedStatement pstmt = con.prepareStatement("SELECT 'a=>1,b=>2'::hstore");
ResultSet rs = pstmt.executeQuery();
assertEquals(Map.class.getName(), rs.getMetaData().getColumnClassName(1));
assertTrue(rs.next());
Expand All @@ -66,7 +70,7 @@ public void testHStoreSelect() throws SQLException {

@Test
public void testHStoreSelectNullValue() throws SQLException {
PreparedStatement pstmt = _conn.prepareStatement("SELECT 'a=>NULL'::hstore");
PreparedStatement pstmt = con.prepareStatement("SELECT 'a=>NULL'::hstore");
ResultSet rs = pstmt.executeQuery();
assertEquals(Map.class.getName(), rs.getMetaData().getColumnClassName(1));
assertTrue(rs.next());
Expand All @@ -78,7 +82,7 @@ public void testHStoreSelectNullValue() throws SQLException {
@Test
public void testHStoreSend() throws SQLException {
Map<String, Integer> correct = Collections.singletonMap("a", 1);
PreparedStatement pstmt = _conn.prepareStatement("SELECT ?::text");
PreparedStatement pstmt = con.prepareStatement("SELECT ?::text");
pstmt.setObject(1, correct);
ResultSet rs = pstmt.executeQuery();
assertEquals(String.class.getName(), rs.getMetaData().getColumnClassName(1));
Expand All @@ -89,7 +93,7 @@ public void testHStoreSend() throws SQLException {
@Test
public void testHStoreUsingPSSetObject4() throws SQLException {
Map<String, Integer> correct = Collections.singletonMap("a", 1);
PreparedStatement pstmt = _conn.prepareStatement("SELECT ?::text");
PreparedStatement pstmt = con.prepareStatement("SELECT ?::text");
pstmt.setObject(1, correct, Types.OTHER, -1);
ResultSet rs = pstmt.executeQuery();
assertEquals(String.class.getName(), rs.getMetaData().getColumnClassName(1));
Expand All @@ -100,7 +104,7 @@ public void testHStoreUsingPSSetObject4() throws SQLException {
@Test
public void testHStoreSendEscaped() throws SQLException {
Map<String, String> correct = Collections.singletonMap("a", "t'e\ns\"t");
PreparedStatement pstmt = _conn.prepareStatement("SELECT ?");
PreparedStatement pstmt = con.prepareStatement("SELECT ?");
pstmt.setObject(1, correct);
ResultSet rs = pstmt.executeQuery();
assertEquals(Map.class.getName(), rs.getMetaData().getColumnClassName(1));
Expand Down
16 changes: 16 additions & 0 deletions pgjdbc/src/test/java/org/postgresql/test/jdbc2/BaseTest4.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import org.postgresql.PGConnection;
import org.postgresql.PGProperty;
import org.postgresql.core.Version;
import org.postgresql.jdbc.PreferQueryMode;
import org.postgresql.test.TestUtil;

Expand Down Expand Up @@ -73,4 +74,19 @@ public void assumeCallableStatementsSupported() {
public void assumeBinaryModeRegular() {
Assume.assumeTrue(binaryMode == BinaryMode.REGULAR);
}

/**
* Shorthand for {@code Assume.assumeTrue(TestUtil.haveMinimumServerVersion(conn, version)}
*/
public void assumeMinimumServerVersion(String message, Version version) throws SQLException {
Assume.assumeTrue(message, TestUtil.haveMinimumServerVersion(con, version));
}

/**
* Shorthand for {@code Assume.assumeTrue(TestUtil.haveMinimumServerVersion(conn, version)}
*/
public void assumeMinimumServerVersion(Version version) throws SQLException {
Assume.assumeTrue(TestUtil.haveMinimumServerVersion(con, version));
}

}
Loading

0 comments on commit 5c12da1

Please sign in to comment.