Skip to content

Commit cf4ac9c

Browse files
committed
#822 Improve schema test coverage (getPseudoColumns/getTables)
1 parent f598527 commit cf4ac9c

File tree

3 files changed

+198
-172
lines changed

3 files changed

+198
-172
lines changed

src/test/org/firebirdsql/common/FBTestProperties.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -383,15 +383,16 @@ public static <T> T ifSchemaElse(T forSchema, T withoutSchema) {
383383
}
384384

385385
/**
386-
* Helper method that replaces {@code "PUBLIC"} with {@code ""} if schemas are not supported.
386+
* Helper method that replaces {@code "PUBLIC"} or {@code "SYSTEM"} with {@code ""} if schemas are not supported.
387387
*
388388
* @param schemaName
389389
* schema name
390-
* @return {@code schemaName}, or &mdash; if {@code schemaName} is {@code "PUBLIC"} and schemas are not supported
391-
* &mdash; {@code ""}
390+
* @return {@code schemaName}, or &mdash; if {@code schemaName} is {@code "PUBLIC"} or {@code "SYSTEM"} and schemas
391+
* are not supported &mdash; {@code ""}
392392
*/
393393
public static String resolveSchema(String schemaName) {
394-
if (!getDefaultSupportInfo().supportsSchemas() && "PUBLIC".equals(schemaName)) {
394+
if (!getDefaultSupportInfo().supportsSchemas()
395+
&& ("PUBLIC".equals(schemaName) || "SYSTEM".equals(schemaName))) {
395396
return "";
396397
}
397398
return schemaName;

src/test/org/firebirdsql/jdbc/FBDatabaseMetaDataPseudoColumnsTest.java

Lines changed: 80 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,70 +3,73 @@
33
package org.firebirdsql.jdbc;
44

55
import org.firebirdsql.common.extension.UsesDatabaseExtension;
6+
import org.firebirdsql.util.FirebirdSupportInfo;
67
import org.junit.jupiter.api.AfterAll;
78
import org.junit.jupiter.api.BeforeAll;
89
import org.junit.jupiter.api.Test;
910
import org.junit.jupiter.api.extension.RegisterExtension;
11+
import org.junit.jupiter.params.ParameterizedTest;
12+
import org.junit.jupiter.params.provider.NullSource;
13+
import org.junit.jupiter.params.provider.ValueSource;
1014

1115
import java.sql.*;
1216
import java.util.*;
1317

1418
import static org.firebirdsql.common.FBTestProperties.getConnectionViaDriverManager;
1519
import static org.firebirdsql.common.FBTestProperties.getDefaultSupportInfo;
1620
import static org.firebirdsql.common.FBTestProperties.ifSchemaElse;
21+
import static org.firebirdsql.common.FBTestProperties.resolveSchema;
22+
import static org.firebirdsql.common.FbAssumptions.assumeFeature;
1723
import static org.firebirdsql.common.JdbcResourceHelper.closeQuietly;
1824
import static org.junit.jupiter.api.Assertions.*;
1925
import static org.junit.jupiter.api.Assumptions.assumeTrue;
2026

2127
class FBDatabaseMetaDataPseudoColumnsTest {
2228

23-
// TODO Add schema support: tests involving other schema
24-
25-
//@formatter:off
2629
private static final String NORMAL_TABLE_NAME = "NORMAL_TABLE";
27-
private static final String CREATE_NORMAL_TABLE = "create table " + NORMAL_TABLE_NAME + " ( "
28-
+ " ID integer primary key"
29-
+ ")";
30-
private static final String NORMAL_TABLE2_NAME = "NORMAL_TABLE2";
31-
private static final String CREATE_NORMAL_TABLE2 = "create table " + NORMAL_TABLE2_NAME + " ( "
32-
+ " ID integer primary key"
33-
+ ")";
30+
private static final String CREATE_NORMAL_TABLE = """
31+
create table NORMAL_TABLE (
32+
ID integer primary key
33+
)""";
34+
private static final String CREATE_NORMAL_TABLE2 = """
35+
create table NORMAL_TABLE2 (
36+
ID integer primary key
37+
)""";
3438
private static final String SINGLE_VIEW_NAME = "SINGLE_VIEW";
3539
private static final String CREATE_SINGLE_VIEW =
36-
"create view " + SINGLE_VIEW_NAME + " as select id from " + NORMAL_TABLE_NAME;
40+
"create view SINGLE_VIEW as select id from NORMAL_TABLE";
3741
private static final String MULTI_VIEW_NAME = "MULTI_VIEW";
38-
private static final String CREATE_MULTI_VIEW = "create view " + MULTI_VIEW_NAME + " as "
39-
+ "select a.id as id1, b.id as id2 "
40-
+ "from " + NORMAL_TABLE_NAME + " as a, " + NORMAL_TABLE2_NAME + " as b";
42+
private static final String CREATE_MULTI_VIEW =
43+
"create view MULTI_VIEW as select a.id as id1, b.id as id2 from NORMAL_TABLE as a, NORMAL_TABLE2 as b";
4144
private static final String EXTERNAL_TABLE_NAME = "EXTERNAL_TABLE";
42-
private static final String CREATE_EXTERNAL_TABLE = "create table " + EXTERNAL_TABLE_NAME
43-
+ " external file 'test_external_tbl.dat' ( "
44-
+ " ID integer not null"
45-
+ ")";
45+
private static final String CREATE_EXTERNAL_TABLE = """
46+
create table EXTERNAL_TABLE
47+
external file 'test_external_tbl.dat' (
48+
ID integer not null
49+
)""";
4650
private static final String GTT_PRESERVE_NAME = "GTT_PRESERVE";
47-
private static final String CREATE_GTT_PRESERVE = "create global temporary table " + GTT_PRESERVE_NAME + " ("
48-
+ " ID integer primary key"
49-
+ ") "
50-
+ " on commit preserve rows ";
51+
private static final String CREATE_GTT_PRESERVE = """
52+
create global temporary table GTT_PRESERVE (
53+
ID integer primary key
54+
) on commit preserve rows""";
5155
private static final String GTT_DELETE_NAME = "GTT_DELETE";
52-
private static final String CREATE_GTT_DELETE = "create global temporary table " + GTT_DELETE_NAME + " ("
53-
+ " ID integer primary key"
54-
+ ") "
55-
+ " on commit delete rows ";
56-
//@formatter:on
56+
private static final String CREATE_GTT_DELETE = """
57+
create global temporary table GTT_DELETE (
58+
ID integer primary key
59+
) on commit delete rows""";
60+
private static final String CREATE_OTHER_SCHEMA = "create schema OTHER_SCHEMA";
61+
private static final String NORMAL_TABLE3_NAME = "NORMAL_TABLE3";
62+
private static final String CREATE_OTHER_SCHEMA_NORMAL_TABLE3 = """
63+
create table OTHER_SCHEMA.NORMAL_TABLE3 (
64+
ID integer primary key
65+
)""";
5766

5867
private static final MetadataResultSetDefinition getPseudoColumnsDefinition =
5968
new MetadataResultSetDefinition(PseudoColumnMetaData.class);
6069

6170
@RegisterExtension
62-
static final UsesDatabaseExtension.UsesDatabaseForAll usesDatabase = UsesDatabaseExtension.usesDatabaseForAll(
63-
CREATE_NORMAL_TABLE,
64-
CREATE_NORMAL_TABLE2,
65-
CREATE_SINGLE_VIEW,
66-
CREATE_MULTI_VIEW,
67-
CREATE_EXTERNAL_TABLE,
68-
CREATE_GTT_PRESERVE,
69-
CREATE_GTT_DELETE);
71+
static final UsesDatabaseExtension.UsesDatabaseForAll usesDatabase =
72+
UsesDatabaseExtension.usesDatabaseForAll(getDbInitStatements());
7073

7174
private static final boolean supportsRecordVersion = getDefaultSupportInfo()
7275
.supportsRecordVersionPseudoColumn();
@@ -80,6 +83,24 @@ static void setupAll() throws SQLException {
8083
dbmd = con.getMetaData();
8184
}
8285

86+
private static List<String> getDbInitStatements() {
87+
var statements = new ArrayList<>(List.of(
88+
CREATE_NORMAL_TABLE,
89+
CREATE_NORMAL_TABLE2,
90+
CREATE_SINGLE_VIEW,
91+
CREATE_MULTI_VIEW,
92+
CREATE_EXTERNAL_TABLE,
93+
CREATE_GTT_PRESERVE,
94+
CREATE_GTT_DELETE));
95+
if (getDefaultSupportInfo().supportsSchemas()) {
96+
statements.addAll(List.of(
97+
CREATE_OTHER_SCHEMA,
98+
CREATE_OTHER_SCHEMA_NORMAL_TABLE3));
99+
}
100+
101+
return statements;
102+
}
103+
83104
@AfterAll
84105
static void tearDownAll() throws SQLException {
85106
try {
@@ -100,12 +121,14 @@ void testPseudoColumnsMetaDataColumns() throws Exception {
100121
}
101122
}
102123

103-
@Test
104-
void testNormalTable_allPseudoColumns() throws Exception {
124+
@ParameterizedTest
125+
@NullSource
126+
@ValueSource(strings = { "%", "PUBLIC" })
127+
void testNormalTable_allPseudoColumns(String schemaPattern) throws Exception {
105128
List<Map<PseudoColumnMetaData, Object>> validationRules =
106129
createStandardValidationRules(NORMAL_TABLE_NAME, "NO");
107130

108-
ResultSet pseudoColumns = dbmd.getPseudoColumns(null, null, NORMAL_TABLE_NAME, "%");
131+
ResultSet pseudoColumns = dbmd.getPseudoColumns(null, resolveSchema(schemaPattern), NORMAL_TABLE_NAME, "%");
109132
validate(pseudoColumns, validationRules);
110133
}
111134

@@ -253,8 +276,9 @@ void testPattern_wildCardTable() throws Exception {
253276
tableCount += 1;
254277
}
255278

256-
// System tables + the 7 tables created for this test
257-
assertEquals(getDefaultSupportInfo().getSystemTableCount() + 7, tableCount,
279+
// System tables + the tables created for this test
280+
int testTableCount = getDefaultSupportInfo().supportsSchemas() ? 8 : 7;
281+
assertEquals(getDefaultSupportInfo().getSystemTableCount() + testTableCount, tableCount,
258282
"Unexpected number of pseudo columns");
259283
}
260284
}
@@ -267,12 +291,25 @@ void testPattern_nullTable() throws Exception {
267291
tableCount += 1;
268292
}
269293

270-
// System tables + the 7 tables created for this test
271-
assertEquals(getDefaultSupportInfo().getSystemTableCount() + 7, tableCount,
294+
// System tables + the tables created for this test
295+
int testTableCount = getDefaultSupportInfo().supportsSchemas() ? 8 : 7;
296+
assertEquals(getDefaultSupportInfo().getSystemTableCount() + testTableCount, tableCount,
272297
"Unexpected number of pseudo columns");
273298
}
274299
}
275300

301+
@ParameterizedTest
302+
@NullSource
303+
@ValueSource(strings = { "%", "OTHER_SCHEMA", "OTHER\\_SCHEMA", "OTHER%" })
304+
void testOtherSchemaNormalTable2_allPseudoColumns(String schemaPattern) throws Exception {
305+
assumeFeature(FirebirdSupportInfo::supportsSchemas, "Test requires schema support");
306+
List<Map<PseudoColumnMetaData, Object>> validationRules =
307+
createStandardValidationRules("OTHER_SCHEMA", NORMAL_TABLE3_NAME, "NO");
308+
309+
ResultSet pseudoColumns = dbmd.getPseudoColumns(null, schemaPattern, NORMAL_TABLE3_NAME, "%");
310+
validate(pseudoColumns, validationRules);
311+
}
312+
276313
private List<Map<PseudoColumnMetaData, Object>> createStandardValidationRules(String tableName,
277314
String recordVersionNullable) {
278315
return createStandardValidationRules(ifSchemaElse("PUBLIC", null), tableName, recordVersionNullable);
@@ -342,6 +379,7 @@ private Map<PseudoColumnMetaData, Object> createDbkeyValidationRules(String sche
342379
return rules;
343380
}
344381

382+
@SuppressWarnings("SameParameterValue")
345383
private Map<PseudoColumnMetaData, Object> createRecordVersionValidationRules(String tableName, String nullable) {
346384
return createRecordVersionValidationRules(ifSchemaElse("PUBLIC", null), tableName, nullable);
347385
}

0 commit comments

Comments
 (0)