Skip to content

Commit 818d7f1

Browse files
committed
Improve robustness of tests on Firebird 2.5, especially native tests with a Firebird 3.0+ fbclient
1 parent 35a9e80 commit 818d7f1

23 files changed

+275
-340
lines changed

src/jna-test/org/firebirdsql/gds/ng/jna/JnaServiceTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ void basicStatusVectorProcessing_wrongLogin() throws Exception {
105105

106106
@Test
107107
void testBasicStatusVectorProcessing_wrongService() throws Exception {
108+
// Test may also fail when using fbclient of Firebird 4.0 or higher for NATIVE tests
108109
assumeTrue(getDefaultSupportInfo().isVersionBelow(4, 0), "Incorrect service name ignored in Firebird 4+");
109110
// set invalid database
110111
final String invalidServiceName = "doesnotexist";

src/main/org/firebirdsql/management/FBServiceManager.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,9 @@ protected FbDatabase attachDatabase() throws SQLException {
319319
createDatabaseAttachInfo().copyTo(connectionProperties);
320320
connectionProperties.setUser(serviceProperties.getUser());
321321
connectionProperties.setPassword(serviceProperties.getPassword());
322+
connectionProperties.setRoleName(serviceProperties.getRoleName());
323+
connectionProperties.setAuthPlugins(serviceProperties.getAuthPlugins());
324+
connectionProperties.setWireCrypt(serviceProperties.getWireCrypt());
322325
FbDatabase fbDatabase = dbFactory.connect(connectionProperties);
323326
fbDatabase.attach();
324327
return fbDatabase;

src/main/org/firebirdsql/util/FirebirdSupportInfo.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,13 @@ public boolean supportsPartialIndices() {
688688
return isVersionEqualOrAbove(5, 0);
689689
}
690690

691+
/**
692+
* @return {@code true} if the nameless service manager is supported
693+
*/
694+
public boolean supportsNamelessServiceManager() {
695+
return isVersionEqualOrAbove(3, 0);
696+
}
697+
691698
/**
692699
* @param odsMajor
693700
* ODS major version

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

Lines changed: 80 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,33 @@
2626
import org.firebirdsql.gds.ng.FbConnectionProperties;
2727
import org.firebirdsql.gds.ng.FbDatabaseFactory;
2828
import org.firebirdsql.gds.ng.FbServiceProperties;
29+
import org.firebirdsql.gds.ng.jna.AbstractNativeDatabaseFactory;
30+
import org.firebirdsql.gds.ng.jna.FbClientFeature;
31+
import org.firebirdsql.gds.ng.jna.FbClientFeatureAccess;
2932
import org.firebirdsql.jaybird.fb.constants.TpbItems;
3033
import org.firebirdsql.jaybird.props.AttachmentProperties;
3134
import org.firebirdsql.jaybird.props.DatabaseConnectionProperties;
3235
import org.firebirdsql.jaybird.props.ServiceConnectionProperties;
3336
import org.firebirdsql.jaybird.xca.FBManagedConnectionFactory;
3437
import org.firebirdsql.jdbc.FBDriver;
3538
import org.firebirdsql.jdbc.FirebirdConnection;
39+
import org.firebirdsql.jna.fbclient.FbClientLibrary;
3640
import org.firebirdsql.management.FBManager;
3741
import org.firebirdsql.management.FBServiceManager;
3842
import org.firebirdsql.management.ServiceManager;
3943
import org.firebirdsql.util.FirebirdSupportInfo;
4044

41-
import java.io.File;
45+
import java.lang.reflect.Method;
46+
import java.nio.file.Paths;
4247
import java.sql.DriverManager;
4348
import java.sql.SQLException;
4449
import java.util.*;
4550

51+
import static org.firebirdsql.common.matchers.GdsTypeMatchers.isEmbeddedType;
52+
import static org.firebirdsql.common.matchers.GdsTypeMatchers.isOtherNativeType;
53+
import static org.firebirdsql.common.matchers.GdsTypeMatchers.isPureJavaType;
54+
import static org.hamcrest.Matchers.not;
55+
4656
/**
4757
* Helper class for test properties (database user, password, paths etc)
4858
*/
@@ -84,33 +94,40 @@ public static String getProperty(String property, String defaultValue) {
8494
public static final int DB_SERVER_PORT = Integer.parseInt(getProperty("test.db.port", "3050"));
8595
public static final String DB_LC_CTYPE = getProperty("test.db.lc_ctype", "NONE");
8696
public static final boolean DB_ON_DOCKER = Boolean.parseBoolean(getProperty("test.db_on_docker", "false"));
87-
public static final String DB_DATASOURCE_URL = getdbpath(DB_NAME);
8897
public static final String GDS_TYPE = getProperty("test.gds_type", "PURE_JAVA");
98+
public static final String DB_DATASOURCE_URL = getdbpath(DB_NAME);
8999
public static final boolean USE_FIREBIRD_AUTOCOMMIT =
90100
Boolean.parseBoolean(getProperty("test.use_firebird_autocommit", "false"));
101+
// Allows running native tests against Firebird 2.5 or older with a Firebird 3.0 or newer fbclient.
102+
private static final boolean NATIVE_LEGACY_AUTH_COMPAT =
103+
Boolean.parseBoolean(getProperty("test.native_legacy_auth_compat", "false"));
104+
private static final String NATIVE_LEGACY_AUTH_COMPAT_AUTH_PLUGINS = "Legacy_Auth";
105+
106+
public static boolean isLocalhost() {
107+
return "localhost".equals(DB_SERVER_URL) || "127.0.0.1".equals(DB_SERVER_URL);
108+
}
91109

92110
public static String getDatabasePath() {
93111
return getDatabasePath(DB_NAME);
94112
}
95113

96114
public static String getDatabasePath(String name) {
97-
if (!("127.0.0.1".equals(DB_SERVER_URL) || "localhost".equals(DB_SERVER_URL)) || DB_ON_DOCKER)
115+
if (not(isEmbeddedType()).matches(GDS_TYPE) && (!isLocalhost() || DB_ON_DOCKER)) {
98116
return DB_PATH + "/" + name;
99-
else
100-
return new File(DB_PATH, name).getAbsolutePath();
117+
} else {
118+
return Paths.get(DB_PATH, name).toAbsolutePath().toString();
119+
}
101120
}
102121

103122
/**
104-
* Builds a firebird database connection string for the supplied database
105-
* file.
123+
* Builds a firebird database connection string for the supplied database file.
106124
*
107125
* @param name Database name
108126
* @return URL or path for the gds type.
109127
*/
110128
public static String getdbpath(String name) {
111-
final String gdsType = getProperty("test.gds_type", null);
112-
if ("EMBEDDED".equalsIgnoreCase(gdsType)) {
113-
return new File(DB_PATH, name).getAbsolutePath();
129+
if (isEmbeddedType().matches(GDS_TYPE)) {
130+
return Paths.get(DB_PATH, name).toAbsolutePath().toString();
114131
} else {
115132
return DB_SERVER_URL + "/" + DB_SERVER_PORT + ":" + getDatabasePath(name);
116133
}
@@ -128,6 +145,9 @@ public static Properties getDefaultPropertiesForConnection() {
128145
if (USE_FIREBIRD_AUTOCOMMIT) {
129146
props.setProperty("useFirebirdAutocommit", "true");
130147
}
148+
if (isEnableNativeLegacyAuthCompat()) {
149+
props.setProperty("authPlugins", NATIVE_LEGACY_AUTH_COMPAT_AUTH_PLUGINS);
150+
}
131151

132152
return props;
133153
}
@@ -168,7 +188,7 @@ public static <T extends ServiceConnectionProperties> T configureDefaultServiceP
168188
}
169189

170190
public static <T extends AttachmentProperties> T configureDefaultAttachmentProperties(T connectionInfo) {
171-
if (getGdsType() != GDSType.getType("EMBEDDED")) {
191+
if (not(isEmbeddedType()).matches(GDS_TYPE)) {
172192
connectionInfo.setServerName(FBTestProperties.DB_SERVER_URL);
173193
connectionInfo.setPortNumber(FBTestProperties.DB_SERVER_PORT);
174194
}
@@ -179,6 +199,9 @@ public static <T extends AttachmentProperties> T configureDefaultAttachmentPrope
179199
connectionInfo.setUser(DB_USER);
180200
connectionInfo.setPassword(DB_PASSWORD);
181201
connectionInfo.setEncoding(DB_LC_CTYPE);
202+
if (isEnableNativeLegacyAuthCompat()) {
203+
connectionInfo.setAuthPlugins(NATIVE_LEGACY_AUTH_COMPAT_AUTH_PLUGINS);
204+
}
182205
return connectionInfo;
183206
}
184207

@@ -202,11 +225,7 @@ public static TransactionParameterBuffer getDefaultTpb() {
202225
* @param serviceManager Service manager to configure
203226
*/
204227
public static <T extends ServiceManager> T configureServiceManager(T serviceManager) {
205-
serviceManager.setServerName(DB_SERVER_URL);
206-
serviceManager.setPortNumber(DB_SERVER_PORT);
207-
serviceManager.setUser(DB_USER);
208-
serviceManager.setPassword(DB_PASSWORD);
209-
return serviceManager;
228+
return configureDefaultAttachmentProperties(serviceManager);
210229
}
211230

212231
/**
@@ -228,16 +247,7 @@ public static GDSType getGdsType() {
228247
public static FirebirdSupportInfo getDefaultSupportInfo() {
229248
try {
230249
if (firebirdSupportInfo == null) {
231-
final GDSType gdsType = getGdsType();
232-
final FBServiceManager fbServiceManager = new FBServiceManager(gdsType);
233-
if (gdsType == GDSType.getType("PURE_JAVA")
234-
|| gdsType == GDSType.getType("NATIVE")
235-
|| gdsType == GDSType.getType("OOREMOTE") ) {
236-
fbServiceManager.setServerName(DB_SERVER_URL);
237-
fbServiceManager.setPortNumber(DB_SERVER_PORT);
238-
}
239-
fbServiceManager.setUser(FBTestProperties.DB_USER);
240-
fbServiceManager.setPassword(FBTestProperties.DB_PASSWORD);
250+
FBServiceManager fbServiceManager = configureServiceManager(new FBServiceManager(getGdsType()));
241251
firebirdSupportInfo = FirebirdSupportInfo.supportInfoFor(fbServiceManager.getServerVersion());
242252
}
243253
return firebirdSupportInfo;
@@ -262,7 +272,7 @@ public static String getUrl() {
262272
* @return JDBC URL (without parameters) for this testrun
263273
*/
264274
public static String getUrl(String dbPath) {
265-
if ("EMBEDDED".equalsIgnoreCase(GDS_TYPE)) {
275+
if (isEmbeddedType().matches(GDS_TYPE)) {
266276
return getProtocolPrefix() + dbPath;
267277
} else {
268278
return getProtocolPrefix() + DB_SERVER_URL + "/" + DB_SERVER_PORT + ":" + dbPath;
@@ -292,6 +302,9 @@ public static FBManagedConnectionFactory createDefaultMcf(boolean shared) {
292302
mcf.setUser(DB_USER);
293303
mcf.setPassword(DB_PASSWORD);
294304
mcf.setEncoding(DB_LC_CTYPE);
305+
if (isEnableNativeLegacyAuthCompat()) {
306+
mcf.setAuthPlugins(NATIVE_LEGACY_AUTH_COMPAT_AUTH_PLUGINS);
307+
}
295308

296309
return mcf;
297310
}
@@ -325,18 +338,23 @@ public static FirebirdConnection getConnectionViaDriverManager(Map<String, Strin
325338
return getConnectionViaDriverManager(getPropertiesForConnection(additionalProperties));
326339
}
327340

328-
public static void configureFBManager(FBManager fbManager) throws Exception {
329-
final GDSType gdsType = getGdsType();
330-
if (gdsType == GDSType.getType("PURE_JAVA")
331-
|| gdsType == GDSType.getType("NATIVE")
332-
|| gdsType == GDSType.getType("OOREMOTE")) {
341+
public static <T extends FBManager> T configureFBManager(T fbManager) throws Exception {
342+
return configureFBManager(fbManager, true);
343+
}
344+
345+
public static <T extends FBManager> T configureFBManager(T fbManager, boolean start) throws Exception {
346+
if (not(isEmbeddedType()).matches(GDS_TYPE)) {
333347
fbManager.setServer(DB_SERVER_URL);
334348
fbManager.setPort(DB_SERVER_PORT);
335349
}
336-
fbManager.start();
350+
if (isEnableNativeLegacyAuthCompat()) {
351+
fbManager.setAuthPlugins(NATIVE_LEGACY_AUTH_COMPAT_AUTH_PLUGINS);
352+
}
353+
if (start) fbManager.start();
337354
fbManager.setForceCreate(true);
338355
// disable force write for minor increase in test throughput
339356
fbManager.setForceWrite(false);
357+
return fbManager;
340358
}
341359

342360
/**
@@ -365,6 +383,35 @@ public static void defaultDatabaseTearDown(FBManager fbManager) throws Exception
365383
}
366384
}
367385

386+
/**
387+
* @return {@code true} if modern URLs (e.g. inet:// ...) are supported, {@code false} otherwise (i.e. a native test
388+
* where a client library of Firebird 2.5 or older is used, or for pure Java)
389+
*/
390+
public static boolean supportsNativeModernUrls() {
391+
if (isPureJavaType().matches(GDS_TYPE)) {
392+
return false;
393+
} else {
394+
try {
395+
Method getClientLibrary = AbstractNativeDatabaseFactory.class.getDeclaredMethod("getClientLibrary");
396+
getClientLibrary.setAccessible(true);
397+
FbClientLibrary clientLibrary = (FbClientLibrary) getClientLibrary.invoke(
398+
FBTestProperties.getFbDatabaseFactory());
399+
if (clientLibrary instanceof FbClientFeatureAccess) {
400+
return ((FbClientFeatureAccess) clientLibrary).hasFeature(FbClientFeature.FB_PING);
401+
}
402+
return false;
403+
} catch (RuntimeException | Error e) {
404+
throw e;
405+
} catch (Throwable e) {
406+
throw new RuntimeException(e);
407+
}
408+
}
409+
}
410+
411+
private static boolean isEnableNativeLegacyAuthCompat() {
412+
return NATIVE_LEGACY_AUTH_COMPAT && isOtherNativeType().matches(GDS_TYPE);
413+
}
414+
368415
private FBTestProperties() {
369416
// No instantiation
370417
}

src/test/org/firebirdsql/ds/FBConnectionPoolTestBase.java

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,15 @@
1919
package org.firebirdsql.ds;
2020

2121
import org.firebirdsql.common.extension.UsesDatabaseExtension;
22-
import org.firebirdsql.gds.impl.GDSType;
2322
import org.junit.jupiter.api.AfterEach;
24-
import org.junit.jupiter.api.BeforeEach;
2523
import org.junit.jupiter.api.extension.RegisterExtension;
2624

2725
import javax.sql.PooledConnection;
2826
import java.sql.SQLException;
2927
import java.util.ArrayList;
3028
import java.util.List;
3129

32-
import static org.firebirdsql.common.FBTestProperties.*;
30+
import static org.firebirdsql.common.FBTestProperties.configureDefaultDbProperties;
3331
import static org.firebirdsql.common.JdbcResourceHelper.closeQuietly;
3432

3533
/**
@@ -43,24 +41,7 @@ abstract class FBConnectionPoolTestBase {
4341
static final UsesDatabaseExtension.UsesDatabaseForAll usesDatabase = UsesDatabaseExtension.usesDatabaseForAll();
4442

4543
private final List<PooledConnection> connections = new ArrayList<>();
46-
protected FBConnectionPoolDataSource ds;
47-
48-
@BeforeEach
49-
void setUp() {
50-
FBConnectionPoolDataSource newDs = new FBConnectionPoolDataSource();
51-
newDs.setType(getProperty("test.gds_type", null));
52-
if (getGdsType() == GDSType.getType("PURE_JAVA")
53-
|| getGdsType() == GDSType.getType("NATIVE")) {
54-
newDs.setServerName(DB_SERVER_URL);
55-
newDs.setPortNumber(DB_SERVER_PORT);
56-
}
57-
newDs.setDatabaseName(getDatabasePath());
58-
newDs.setUser(DB_USER);
59-
newDs.setPassword(DB_PASSWORD);
60-
newDs.setEncoding(DB_LC_CTYPE);
61-
62-
ds = newDs;
63-
}
44+
protected final FBConnectionPoolDataSource ds = configureDefaultDbProperties(new FBConnectionPoolDataSource());
6445

6546
@AfterEach
6647
void tearDown() {

src/test/org/firebirdsql/ds/FBSimpleDataSourceTest.java

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.sql.Connection;
3131
import java.sql.PreparedStatement;
3232

33+
import static org.firebirdsql.common.FBTestProperties.configureDefaultDbProperties;
3334
import static org.firebirdsql.common.FBTestProperties.getDefaultSupportInfo;
3435
import static org.firebirdsql.common.matchers.GdsTypeMatchers.isPureJavaType;
3536
import static org.firebirdsql.common.matchers.MatcherAssume.assumeThat;
@@ -52,18 +53,14 @@ class FBSimpleDataSourceTest {
5253
*/
5354
@Test
5455
void testJavaCharSetIsDefaultCharSet() {
55-
FBSimpleDataSource ds = new FBSimpleDataSource();
56-
ds.setDatabaseName(FBTestProperties.DB_DATASOURCE_URL);
57-
ds.setUser(FBTestProperties.DB_USER);
58-
ds.setPassword(FBTestProperties.DB_PASSWORD);
59-
ds.setType(FBTestProperties.getGdsType().toString());
56+
FBSimpleDataSource ds = configureDefaultDbProperties(new FBSimpleDataSource());
57+
ds.setEncoding(null);
6058
ds.setCharSet(System.getProperty("file.encoding"));
6159
try (Connection con = ds.getConnection()) {
6260
PreparedStatement ps = con.prepareStatement("SELECT * FROM RDB$DATABASE");
6361
JdbcResourceHelper.closeQuietly(ps);
6462
} catch (Exception e) {
65-
e.printStackTrace();
66-
fail("Preparing statement with property charSet equal to file.encoding should not fail");
63+
fail("Preparing statement with property charSet equal to file.encoding should not fail", e);
6764
}
6865
}
6966

@@ -106,11 +103,7 @@ void enableWireCompression() throws Exception {
106103

107104
@Test
108105
void canChangeConfigAfterConnectionCreation() throws Exception {
109-
FBSimpleDataSource ds = new FBSimpleDataSource();
110-
ds.setDatabaseName(FBTestProperties.DB_DATASOURCE_URL);
111-
ds.setUser(FBTestProperties.DB_USER);
112-
ds.setPassword(FBTestProperties.DB_PASSWORD);
113-
ds.setType(FBTestProperties.getGdsType().toString());
106+
FBSimpleDataSource ds = configureDefaultDbProperties(new FBSimpleDataSource());
114107

115108
// possible before connecting
116109
ds.setBlobBufferSize(1024);
@@ -126,11 +119,7 @@ void canChangeConfigAfterConnectionCreation() throws Exception {
126119
@Test
127120
void cannotChangeConfigAfterConnectionCreation_usingSharedMCF() throws Exception {
128121
FBManagedConnectionFactory mcf = new FBManagedConnectionFactory();
129-
FBSimpleDataSource ds = new FBSimpleDataSource(mcf);
130-
ds.setDatabaseName(FBTestProperties.DB_DATASOURCE_URL);
131-
ds.setUser(FBTestProperties.DB_USER);
132-
ds.setPassword(FBTestProperties.DB_PASSWORD);
133-
ds.setType(FBTestProperties.getGdsType().toString());
122+
FBSimpleDataSource ds = configureDefaultDbProperties(new FBSimpleDataSource(mcf));
134123

135124
// possible before connecting
136125
ds.setBlobBufferSize(1024);

0 commit comments

Comments
 (0)