From 9a3c2995b9ac08afe584696d033c385131d4b347 Mon Sep 17 00:00:00 2001 From: Vladimir Sitnikov Date: Wed, 24 Apr 2024 16:52:13 +0300 Subject: [PATCH] test: sue binaryMode when creating connections in DatabaseMetaDataTest https://github.com/pgjdbc/pgjdbc/issues/3229 --- pgjdbc/build.gradle.kts | 3 + .../test/jdbc2/DatabaseMetaDataTest.java | 65 +-------------- .../org.junit.jupiter.api.extension.Extension | 1 + .../impl/AfterBeforeParameterResolver.java | 79 +++++++++++++++++++ .../test/impl/MappedParameterContext.java | 58 ++++++++++++++ 5 files changed, 142 insertions(+), 64 deletions(-) create mode 100644 pgjdbc/src/test/resources/META-INF/services/org.junit.jupiter.api.extension.Extension create mode 100644 pgjdbc/src/testFixtures/java/org/postgresql/test/impl/AfterBeforeParameterResolver.java create mode 100644 pgjdbc/src/testFixtures/java/org/postgresql/test/impl/MappedParameterContext.java diff --git a/pgjdbc/build.gradle.kts b/pgjdbc/build.gradle.kts index f1820edaf7..4acada6321 100644 --- a/pgjdbc/build.gradle.kts +++ b/pgjdbc/build.gradle.kts @@ -90,6 +90,9 @@ dependencies { testImplementation("se.jiderhamn:classloader-leak-test-framework:1.1.2") testFixturesImplementation("junit:junit:4.13.2") testFixturesImplementation("org.junit.jupiter:junit-jupiter-api:5.10.2") + testFixturesApi("org.junit.jupiter:junit-jupiter-engine:5.10.2") { + because("We use BeforeEachMethodAdapter to add parameters to beforeeach and aftereach methods") + } testFixturesImplementation("org.checkerframework:checker-qual:3.42.0") } diff --git a/pgjdbc/src/test/java/org/postgresql/test/jdbc2/DatabaseMetaDataTest.java b/pgjdbc/src/test/java/org/postgresql/test/jdbc2/DatabaseMetaDataTest.java index 5c16faeab2..28c7f61634 100644 --- a/pgjdbc/src/test/java/org/postgresql/test/jdbc2/DatabaseMetaDataTest.java +++ b/pgjdbc/src/test/java/org/postgresql/test/jdbc2/DatabaseMetaDataTest.java @@ -49,11 +49,6 @@ */ public class DatabaseMetaDataTest { private Connection con; - private BinaryMode binaryMode; - - public void initDatabaseMetaDataTest(BinaryMode binaryMode) { - this.binaryMode = binaryMode; - } public static Iterable data() { Collection ids = new ArrayList<>(); @@ -64,7 +59,7 @@ public static Iterable data() { } @BeforeEach - void setUp() throws Exception { + void setUp(BinaryMode binaryMode) throws Exception { if (binaryMode == BinaryMode.FORCE) { final Properties props = new Properties(); PGProperty.PREPARE_THRESHOLD.set(props, -1); @@ -182,7 +177,6 @@ void tearDown() throws Exception { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void arrayTypeInfo(BinaryMode binaryMode) throws SQLException { - initDatabaseMetaDataTest(binaryMode); DatabaseMetaData dbmd = con.getMetaData(); ResultSet rs = dbmd.getColumns(null, null, "intarraytable", "a"); assertTrue(rs.next()); @@ -198,7 +192,6 @@ void arrayTypeInfo(BinaryMode binaryMode) throws SQLException { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void arrayInt4DoubleDim(BinaryMode binaryMode) throws SQLException { - initDatabaseMetaDataTest(binaryMode); DatabaseMetaData dbmd = con.getMetaData(); ResultSet rs = dbmd.getColumns(null, null, "intarraytable", "b"); assertTrue(rs.next()); @@ -212,7 +205,6 @@ void arrayInt4DoubleDim(BinaryMode binaryMode) throws SQLException { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void customArrayTypeInfo(BinaryMode binaryMode) throws SQLException { - initDatabaseMetaDataTest(binaryMode); DatabaseMetaData dbmd = con.getMetaData(); ResultSet res = dbmd.getColumns(null, null, "customtable", null); assertTrue(res.next()); @@ -250,7 +242,6 @@ void customArrayTypeInfo(BinaryMode binaryMode) throws SQLException { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void tables(BinaryMode binaryMode) throws Exception { - initDatabaseMetaDataTest(binaryMode); DatabaseMetaData dbmd = con.getMetaData(); assertNotNull(dbmd); @@ -292,7 +283,6 @@ void tables(BinaryMode binaryMode) throws Exception { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void crossReference(BinaryMode binaryMode) throws Exception { - initDatabaseMetaDataTest(binaryMode); Connection con1 = TestUtil.openDB(); TestUtil.createTable(con1, "vv", "a int not null, b int not null, constraint vv_pkey primary key ( a, b )"); @@ -342,7 +332,6 @@ void crossReference(BinaryMode binaryMode) throws Exception { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void foreignKeyActions(BinaryMode binaryMode) throws Exception { - initDatabaseMetaDataTest(binaryMode); Connection conn = TestUtil.openDB(); TestUtil.createTable(conn, "pkt", "id int primary key"); TestUtil.createTable(conn, "fkt1", @@ -372,7 +361,6 @@ void foreignKeyActions(BinaryMode binaryMode) throws Exception { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void foreignKeysToUniqueIndexes(BinaryMode binaryMode) throws Exception { - initDatabaseMetaDataTest(binaryMode); Connection con1 = TestUtil.openDB(); TestUtil.createTable(con1, "pkt", "a int not null, b int not null, CONSTRAINT pkt_pk_a PRIMARY KEY (a), CONSTRAINT pkt_un_b UNIQUE (b)"); @@ -398,7 +386,6 @@ void foreignKeysToUniqueIndexes(BinaryMode binaryMode) throws Exception { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void multiColumnForeignKeys(BinaryMode binaryMode) throws Exception { - initDatabaseMetaDataTest(binaryMode); Connection con1 = TestUtil.openDB(); TestUtil.createTable(con1, "pkt", "a int not null, b int not null, CONSTRAINT pkt_pk PRIMARY KEY (a,b)"); @@ -430,7 +417,6 @@ void multiColumnForeignKeys(BinaryMode binaryMode) throws Exception { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void sameTableForeignKeys(BinaryMode binaryMode) throws Exception { - initDatabaseMetaDataTest(binaryMode); Connection con1 = TestUtil.openDB(); TestUtil.createTable(con1, "person", @@ -495,7 +481,6 @@ void sameTableForeignKeys(BinaryMode binaryMode) throws Exception { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void foreignKeys(BinaryMode binaryMode) throws Exception { - initDatabaseMetaDataTest(binaryMode); Connection con1 = TestUtil.openDB(); TestUtil.createTable(con1, "people", "id int4 primary key, name text"); TestUtil.createTable(con1, "policy", "id int4 primary key, name text"); @@ -556,7 +541,6 @@ void foreignKeys(BinaryMode binaryMode) throws Exception { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void numericPrecision(BinaryMode binaryMode) throws SQLException { - initDatabaseMetaDataTest(binaryMode); DatabaseMetaData dbmd = con.getMetaData(); assertNotNull(dbmd); ResultSet rs = dbmd.getColumns(null, "public", "precision_test", "%"); @@ -568,7 +552,6 @@ void numericPrecision(BinaryMode binaryMode) throws SQLException { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void columns(BinaryMode binaryMode) throws SQLException { - initDatabaseMetaDataTest(binaryMode); // At the moment just test that no exceptions are thrown KJ String [] metadataColumns = {"TABLE_CAT", "TABLE_SCHEM", "TABLE_NAME", "COLUMN_NAME", "DATA_TYPE", "TYPE_NAME", "COLUMN_SIZE", "BUFFER_LENGTH", @@ -591,7 +574,6 @@ void columns(BinaryMode binaryMode) throws SQLException { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void droppedColumns(BinaryMode binaryMode) throws SQLException { - initDatabaseMetaDataTest(binaryMode); if (!TestUtil.haveMinimumServerVersion(con, ServerVersion.v8_4)) { return; } @@ -643,7 +625,6 @@ void droppedColumns(BinaryMode binaryMode) throws SQLException { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void serialColumns(BinaryMode binaryMode) throws SQLException { - initDatabaseMetaDataTest(binaryMode); DatabaseMetaData dbmd = con.getMetaData(); ResultSet rs = dbmd.getColumns(null, null, "sercoltest", null); int rownum = 0; @@ -670,7 +651,6 @@ void serialColumns(BinaryMode binaryMode) throws SQLException { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void columnPrivileges(BinaryMode binaryMode) throws SQLException { - initDatabaseMetaDataTest(binaryMode); // At the moment just test that no exceptions are thrown KJ DatabaseMetaData dbmd = con.getMetaData(); assertNotNull(dbmd); @@ -706,21 +686,18 @@ public void relationPrivilegesHelper(String relationName) throws SQLException { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void tablePrivileges(BinaryMode binaryMode) throws SQLException { - initDatabaseMetaDataTest(binaryMode); relationPrivilegesHelper("metadatatest"); } @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void viewPrivileges(BinaryMode binaryMode) throws SQLException { - initDatabaseMetaDataTest(binaryMode); relationPrivilegesHelper("viewtest"); } @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void materializedViewPrivileges(BinaryMode binaryMode) throws SQLException { - initDatabaseMetaDataTest(binaryMode); Assumptions.assumeTrue(TestUtil.haveMinimumServerVersion(con, ServerVersion.v9_3)); TestUtil.createMaterializedView(con, "matviewtest", "SELECT id, quest FROM metadatatest"); try { @@ -733,7 +710,6 @@ void materializedViewPrivileges(BinaryMode binaryMode) throws SQLException { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void noTablePrivileges(BinaryMode binaryMode) throws SQLException { - initDatabaseMetaDataTest(binaryMode); Statement stmt = con.createStatement(); stmt.execute("REVOKE ALL ON metadatatest FROM PUBLIC"); stmt.execute("REVOKE ALL ON metadatatest FROM " + TestUtil.getUser()); @@ -745,7 +721,6 @@ void noTablePrivileges(BinaryMode binaryMode) throws SQLException { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void primaryKeys(BinaryMode binaryMode) throws SQLException { - initDatabaseMetaDataTest(binaryMode); // At the moment just test that no exceptions are thrown KJ DatabaseMetaData dbmd = con.getMetaData(); assertNotNull(dbmd); @@ -756,7 +731,6 @@ void primaryKeys(BinaryMode binaryMode) throws SQLException { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void indexInfo(BinaryMode binaryMode) throws SQLException { - initDatabaseMetaDataTest(binaryMode); Statement stmt = con.createStatement(); stmt.execute("create index idx_id on metadatatest (id)"); stmt.execute("create index idx_func_single on metadatatest (upper(colour))"); @@ -817,7 +791,6 @@ void indexInfo(BinaryMode binaryMode) throws SQLException { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void indexInfoColumnOrder(BinaryMode binaryMode) throws SQLException { - initDatabaseMetaDataTest(binaryMode); DatabaseMetaData dbmd = con.getMetaData(); assertNotNull(dbmd); ResultSet rs = dbmd.getIndexInfo(null, null, "metadatatest", false, false); @@ -841,7 +814,6 @@ void indexInfoColumnOrder(BinaryMode binaryMode) throws SQLException { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void indexInfoColumnCase(BinaryMode binaryMode) throws SQLException { - initDatabaseMetaDataTest(binaryMode); DatabaseMetaData dbmd = con.getMetaData(); assertNotNull(dbmd); @@ -861,7 +833,6 @@ void indexInfoColumnCase(BinaryMode binaryMode) throws SQLException { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void notNullDomainColumn(BinaryMode binaryMode) throws SQLException { - initDatabaseMetaDataTest(binaryMode); DatabaseMetaData dbmd = con.getMetaData(); ResultSet rs = dbmd.getColumns("", "", "domaintable", ""); assertTrue(rs.next()); @@ -875,7 +846,6 @@ void notNullDomainColumn(BinaryMode binaryMode) throws SQLException { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void domainColumnSize(BinaryMode binaryMode) throws SQLException { - initDatabaseMetaDataTest(binaryMode); DatabaseMetaData dbmd = con.getMetaData(); ResultSet rs = dbmd.getColumns("", "", "domaintable", ""); assertTrue(rs.next()); @@ -894,7 +864,6 @@ void domainColumnSize(BinaryMode binaryMode) throws SQLException { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void ascDescIndexInfo(BinaryMode binaryMode) throws SQLException { - initDatabaseMetaDataTest(binaryMode); if (!TestUtil.haveMinimumServerVersion(con, ServerVersion.v8_3)) { return; } @@ -920,7 +889,6 @@ void ascDescIndexInfo(BinaryMode binaryMode) throws SQLException { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void partialIndexInfo(BinaryMode binaryMode) throws SQLException { - initDatabaseMetaDataTest(binaryMode); Statement stmt = con.createStatement(); stmt.execute("create index idx_p_name_id on metadatatest (name) where id > 5"); stmt.close(); @@ -941,7 +909,6 @@ void partialIndexInfo(BinaryMode binaryMode) throws SQLException { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void tableTypes(BinaryMode binaryMode) throws SQLException { - initDatabaseMetaDataTest(binaryMode); final List expectedTableTypes = new ArrayList<>(Arrays.asList("FOREIGN TABLE", "INDEX", "PARTITIONED INDEX", "MATERIALIZED VIEW", "PARTITIONED TABLE", "SEQUENCE", "SYSTEM INDEX", "SYSTEM TABLE", "SYSTEM TOAST INDEX", "SYSTEM TOAST TABLE", "SYSTEM VIEW", "TABLE", "TEMPORARY INDEX", "TEMPORARY SEQUENCE", "TEMPORARY TABLE", @@ -967,7 +934,6 @@ void tableTypes(BinaryMode binaryMode) throws SQLException { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void funcWithoutNames(BinaryMode binaryMode) throws SQLException { - initDatabaseMetaDataTest(binaryMode); DatabaseMetaData dbmd = con.getMetaData(); assertNotNull(dbmd); ResultSet rs = dbmd.getProcedureColumns(null, null, "f1", null); @@ -994,7 +960,6 @@ void funcWithoutNames(BinaryMode binaryMode) throws SQLException { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void funcWithNames(BinaryMode binaryMode) throws SQLException { - initDatabaseMetaDataTest(binaryMode); DatabaseMetaData dbmd = con.getMetaData(); ResultSet rs = dbmd.getProcedureColumns(null, null, "f2", null); @@ -1014,7 +979,6 @@ void funcWithNames(BinaryMode binaryMode) throws SQLException { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void funcWithDirection(BinaryMode binaryMode) throws SQLException { - initDatabaseMetaDataTest(binaryMode); DatabaseMetaData dbmd = con.getMetaData(); ResultSet rs = dbmd.getProcedureColumns(null, null, "f3", null); @@ -1039,7 +1003,6 @@ void funcWithDirection(BinaryMode binaryMode) throws SQLException { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void funcReturningComposite(BinaryMode binaryMode) throws SQLException { - initDatabaseMetaDataTest(binaryMode); DatabaseMetaData dbmd = con.getMetaData(); ResultSet rs = dbmd.getProcedureColumns(null, null, "f4", null); @@ -1080,7 +1043,6 @@ void funcReturningComposite(BinaryMode binaryMode) throws SQLException { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void funcReturningTable(BinaryMode binaryMode) throws Exception { - initDatabaseMetaDataTest(binaryMode); if (!TestUtil.haveMinimumServerVersion(con, ServerVersion.v8_4)) { return; } @@ -1101,7 +1063,6 @@ void funcReturningTable(BinaryMode binaryMode) throws Exception { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void versionColumns(BinaryMode binaryMode) throws SQLException { - initDatabaseMetaDataTest(binaryMode); // At the moment just test that no exceptions are thrown KJ DatabaseMetaData dbmd = con.getMetaData(); assertNotNull(dbmd); @@ -1112,7 +1073,6 @@ void versionColumns(BinaryMode binaryMode) throws SQLException { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void bestRowIdentifier(BinaryMode binaryMode) throws SQLException { - initDatabaseMetaDataTest(binaryMode); // At the moment just test that no exceptions are thrown KJ DatabaseMetaData dbmd = con.getMetaData(); assertNotNull(dbmd); @@ -1124,7 +1084,6 @@ void bestRowIdentifier(BinaryMode binaryMode) throws SQLException { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void procedures(BinaryMode binaryMode) throws SQLException { - initDatabaseMetaDataTest(binaryMode); // At the moment just test that no exceptions are thrown KJ DatabaseMetaData dbmd = con.getMetaData(); assertNotNull(dbmd); @@ -1135,7 +1094,6 @@ void procedures(BinaryMode binaryMode) throws SQLException { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void catalogs(BinaryMode binaryMode) throws SQLException { - initDatabaseMetaDataTest(binaryMode); DatabaseMetaData dbmd = con.getMetaData(); try (ResultSet rs = dbmd.getCatalogs()) { List catalogs = new ArrayList<>(); @@ -1159,7 +1117,6 @@ void catalogs(BinaryMode binaryMode) throws SQLException { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void schemas(BinaryMode binaryMode) throws Exception { - initDatabaseMetaDataTest(binaryMode); DatabaseMetaData dbmd = con.getMetaData(); assertNotNull(dbmd); @@ -1189,7 +1146,6 @@ void schemas(BinaryMode binaryMode) throws Exception { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void escaping(BinaryMode binaryMode) throws SQLException { - initDatabaseMetaDataTest(binaryMode); DatabaseMetaData dbmd = con.getMetaData(); ResultSet rs = dbmd.getTables(null, null, "a'", new String[]{"TABLE"}); assertTrue(rs.next()); @@ -1202,7 +1158,6 @@ void escaping(BinaryMode binaryMode) throws SQLException { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void searchStringEscape(BinaryMode binaryMode) throws Exception { - initDatabaseMetaDataTest(binaryMode); DatabaseMetaData dbmd = con.getMetaData(); String pattern = dbmd.getSearchStringEscape() + "_"; PreparedStatement pstmt = con.prepareStatement("SELECT 'a' LIKE ?, '_' LIKE ?"); @@ -1219,7 +1174,6 @@ void searchStringEscape(BinaryMode binaryMode) throws Exception { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void getUDTQualified(BinaryMode binaryMode) throws Exception { - initDatabaseMetaDataTest(binaryMode); Statement stmt = null; try { stmt = con.createStatement(); @@ -1275,7 +1229,6 @@ void getUDTQualified(BinaryMode binaryMode) throws Exception { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void getUDT1(BinaryMode binaryMode) throws Exception { - initDatabaseMetaDataTest(binaryMode); try { Statement stmt = con.createStatement(); stmt.execute("create domain testint8 as int8"); @@ -1308,7 +1261,6 @@ void getUDT1(BinaryMode binaryMode) throws Exception { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void getUDT2(BinaryMode binaryMode) throws Exception { - initDatabaseMetaDataTest(binaryMode); try { Statement stmt = con.createStatement(); stmt.execute("create domain testint8 as int8"); @@ -1342,7 +1294,6 @@ void getUDT2(BinaryMode binaryMode) throws Exception { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void getUDT3(BinaryMode binaryMode) throws Exception { - initDatabaseMetaDataTest(binaryMode); try { Statement stmt = con.createStatement(); stmt.execute("create domain testint8 as int8"); @@ -1375,7 +1326,6 @@ void getUDT3(BinaryMode binaryMode) throws Exception { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void getUDT4(BinaryMode binaryMode) throws Exception { - initDatabaseMetaDataTest(binaryMode); try { Statement stmt = con.createStatement(); stmt.execute("create type testint8 as (i int8)"); @@ -1406,7 +1356,6 @@ void getUDT4(BinaryMode binaryMode) throws Exception { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void types(BinaryMode binaryMode) throws SQLException { - initDatabaseMetaDataTest(binaryMode); // https://www.postgresql.org/docs/8.2/static/datatype.html List stringTypeList = new ArrayList<>(); stringTypeList.addAll(Arrays.asList("bit", @@ -1469,7 +1418,6 @@ void types(BinaryMode binaryMode) throws SQLException { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void typeInfoSigned(BinaryMode binaryMode) throws SQLException { - initDatabaseMetaDataTest(binaryMode); DatabaseMetaData dbmd = con.getMetaData(); ResultSet rs = dbmd.getTypeInfo(); while (rs.next()) { @@ -1486,7 +1434,6 @@ void typeInfoSigned(BinaryMode binaryMode) throws SQLException { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void typeInfoQuoting(BinaryMode binaryMode) throws SQLException { - initDatabaseMetaDataTest(binaryMode); DatabaseMetaData dbmd = con.getMetaData(); ResultSet rs = dbmd.getTypeInfo(); while (rs.next()) { @@ -1502,7 +1449,6 @@ void typeInfoQuoting(BinaryMode binaryMode) throws SQLException { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void informationAboutArrayTypes(BinaryMode binaryMode) throws SQLException { - initDatabaseMetaDataTest(binaryMode); DatabaseMetaData dbmd = con.getMetaData(); ResultSet rs = dbmd.getColumns("", "", "arraytable", ""); assertTrue(rs.next()); @@ -1518,7 +1464,6 @@ void informationAboutArrayTypes(BinaryMode binaryMode) throws SQLException { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void partitionedTablesIndex(BinaryMode binaryMode) throws SQLException { - initDatabaseMetaDataTest(binaryMode); if (TestUtil.haveMinimumServerVersion(con, ServerVersion.v11)) { Statement stmt = null; try { @@ -1543,7 +1488,6 @@ void partitionedTablesIndex(BinaryMode binaryMode) throws SQLException { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void partitionedTables(BinaryMode binaryMode) throws SQLException { - initDatabaseMetaDataTest(binaryMode); if (TestUtil.haveMinimumServerVersion(con, ServerVersion.v11)) { Statement stmt = null; try { @@ -1571,7 +1515,6 @@ void partitionedTables(BinaryMode binaryMode) throws SQLException { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void identityColumns(BinaryMode binaryMode) throws SQLException { - initDatabaseMetaDataTest(binaryMode); if ( TestUtil.haveMinimumServerVersion(con, ServerVersion.v10) ) { Statement stmt = null; try { @@ -1597,7 +1540,6 @@ void identityColumns(BinaryMode binaryMode) throws SQLException { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void generatedColumns(BinaryMode binaryMode) throws SQLException { - initDatabaseMetaDataTest(binaryMode); if ( TestUtil.haveMinimumServerVersion(con, ServerVersion.v12) ) { DatabaseMetaData dbmd = con.getMetaData(); ResultSet rs = dbmd.getColumns("", "", "employee", "gross_pay"); @@ -1610,7 +1552,6 @@ void generatedColumns(BinaryMode binaryMode) throws SQLException { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void getSQLKeywords(BinaryMode binaryMode) throws SQLException { - initDatabaseMetaDataTest(binaryMode); DatabaseMetaData dbmd = con.getMetaData(); String keywords = dbmd.getSQLKeywords(); @@ -1682,7 +1623,6 @@ void getSQLKeywords(BinaryMode binaryMode) throws SQLException { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void functionColumns(BinaryMode binaryMode) throws SQLException { - initDatabaseMetaDataTest(binaryMode); if (!TestUtil.haveMinimumServerVersion(con, ServerVersion.v8_4)) { return; } @@ -1748,7 +1688,6 @@ void functionColumns(BinaryMode binaryMode) throws SQLException { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void smallSerialColumns(BinaryMode binaryMode) throws SQLException { - initDatabaseMetaDataTest(binaryMode); Assumptions.assumeTrue(TestUtil.haveMinimumServerVersion(con, ServerVersion.v9_2)); TestUtil.createTable(con, "smallserial_test", "a smallserial"); @@ -1770,7 +1709,6 @@ void smallSerialColumns(BinaryMode binaryMode) throws SQLException { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void smallSerialSequenceLikeColumns(BinaryMode binaryMode) throws SQLException { - initDatabaseMetaDataTest(binaryMode); Statement stmt = con.createStatement(); // This is the equivalent of the smallserial, not the actual smallserial stmt.execute("CREATE SEQUENCE smallserial_test_a_seq;\n" @@ -1803,7 +1741,6 @@ void smallSerialSequenceLikeColumns(BinaryMode binaryMode) throws SQLException { @MethodSource("data") @ParameterizedTest(name = "binary = {0}") void upperCaseMetaDataLabels(BinaryMode binaryMode) throws SQLException { - initDatabaseMetaDataTest(binaryMode); ResultSet rs = con.getMetaData().getTables(null, null, null, null); ResultSetMetaData rsmd = rs.getMetaData(); diff --git a/pgjdbc/src/test/resources/META-INF/services/org.junit.jupiter.api.extension.Extension b/pgjdbc/src/test/resources/META-INF/services/org.junit.jupiter.api.extension.Extension new file mode 100644 index 0000000000..70d727d4c3 --- /dev/null +++ b/pgjdbc/src/test/resources/META-INF/services/org.junit.jupiter.api.extension.Extension @@ -0,0 +1 @@ +org.postgresql.test.impl.AfterBeforeParameterResolver diff --git a/pgjdbc/src/testFixtures/java/org/postgresql/test/impl/AfterBeforeParameterResolver.java b/pgjdbc/src/testFixtures/java/org/postgresql/test/impl/AfterBeforeParameterResolver.java new file mode 100644 index 0000000000..347fb08095 --- /dev/null +++ b/pgjdbc/src/testFixtures/java/org/postgresql/test/impl/AfterBeforeParameterResolver.java @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2024, PostgreSQL Global Development Group + * See the LICENSE file in the project root for more information. + */ + +package org.postgresql.test.impl; + +import org.checkerframework.checker.nullness.qual.Nullable; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.api.extension.ParameterContext; +import org.junit.jupiter.api.extension.ParameterResolutionException; +import org.junit.jupiter.api.extension.ParameterResolver; +import org.junit.jupiter.engine.execution.BeforeEachMethodAdapter; +import org.junit.jupiter.engine.extension.ExtensionRegistry; + +import java.lang.annotation.Annotation; +import java.util.Arrays; +import java.util.Optional; + +/** + * Passes JUnit5's {@code ParameterizedTest} parameters to {@code @BeforeEach} and {@code AfterEach} + * methods. + * + * @see Parameterized BeforeEach or + * AfterEach only + */ +public class AfterBeforeParameterResolver implements BeforeEachMethodAdapter, ParameterResolver { + private @Nullable ParameterResolver parameterisedTestParameterResolver; + + @Override + public void invokeBeforeEachMethod(ExtensionContext context, ExtensionRegistry registry) { + Optional resolverOptional = registry.getExtensions(ParameterResolver.class) + .stream() + .filter(parameterResolver -> parameterResolver.getClass().getName().contains( + "ParameterizedTestParameterResolver")) + .findFirst(); + if (!resolverOptional.isPresent()) { + throw new IllegalStateException("ParameterizedTestParameterResolver missed in the registry." + + " Probably it's not a Parameterized Test"); + } + parameterisedTestParameterResolver = resolverOptional.get(); + } + + @Override + public boolean supportsParameter(ParameterContext parameterContext, + ExtensionContext extensionContext) throws ParameterResolutionException { + if (isExecutedOnAfterOrBeforeMethod(parameterContext)) { + ParameterContext pContext = getMappedContext(parameterContext, extensionContext); + return parameterisedTestParameterResolver.supportsParameter(pContext, extensionContext); + } + return false; + } + + private MappedParameterContext getMappedContext(ParameterContext parameterContext, + ExtensionContext extensionContext) { + return new MappedParameterContext( + parameterContext.getIndex(), + extensionContext.getRequiredTestMethod().getParameters()[parameterContext.getIndex()], + Optional.of(parameterContext.getTarget())); + } + + private boolean isExecutedOnAfterOrBeforeMethod(ParameterContext parameterContext) { + return Arrays.stream(parameterContext.getDeclaringExecutable().getDeclaredAnnotations()) + .anyMatch(this::isAfterEachOrBeforeEachAnnotation); + } + + private boolean isAfterEachOrBeforeEachAnnotation(Annotation annotation) { + return annotation.annotationType() == BeforeEach.class || annotation.annotationType() == AfterEach.class; + } + + @Override + public Object resolveParameter(ParameterContext parameterContext, + ExtensionContext extensionContext) throws ParameterResolutionException { + return parameterisedTestParameterResolver.resolveParameter(getMappedContext(parameterContext, + extensionContext), extensionContext); + } +} diff --git a/pgjdbc/src/testFixtures/java/org/postgresql/test/impl/MappedParameterContext.java b/pgjdbc/src/testFixtures/java/org/postgresql/test/impl/MappedParameterContext.java new file mode 100644 index 0000000000..8cc0e97375 --- /dev/null +++ b/pgjdbc/src/testFixtures/java/org/postgresql/test/impl/MappedParameterContext.java @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2024, PostgreSQL Global Development Group + * See the LICENSE file in the project root for more information. + */ + +package org.postgresql.test.impl; + +import org.junit.jupiter.api.extension.ParameterContext; +import org.junit.platform.commons.util.AnnotationUtils; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Parameter; +import java.util.List; +import java.util.Optional; + +public class MappedParameterContext implements ParameterContext { + + private final int index; + private final Parameter parameter; + private final Optional target; + + public MappedParameterContext(int index, Parameter parameter, + Optional target) { + this.index = index; + this.parameter = parameter; + this.target = target; + } + + @Override + public boolean isAnnotated(Class annotationType) { + return AnnotationUtils.isAnnotated(parameter, annotationType); + } + + @Override + public Optional findAnnotation(Class annotationType) { + return Optional.empty(); + } + + @Override + public List findRepeatableAnnotations(Class annotationType) { + return null; + } + + @Override + public int getIndex() { + return index; + } + + @Override + public Parameter getParameter() { + return parameter; + } + + @Override + public Optional getTarget() { + return target; + } +}