2626import org .firebirdsql .gds .ng .FbConnectionProperties ;
2727import org .firebirdsql .gds .ng .FbDatabaseFactory ;
2828import 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 ;
2932import org .firebirdsql .jaybird .fb .constants .TpbItems ;
3033import org .firebirdsql .jaybird .props .AttachmentProperties ;
3134import org .firebirdsql .jaybird .props .DatabaseConnectionProperties ;
3235import org .firebirdsql .jaybird .props .ServiceConnectionProperties ;
3336import org .firebirdsql .jaybird .xca .FBManagedConnectionFactory ;
3437import org .firebirdsql .jdbc .FBDriver ;
3538import org .firebirdsql .jdbc .FirebirdConnection ;
39+ import org .firebirdsql .jna .fbclient .FbClientLibrary ;
3640import org .firebirdsql .management .FBManager ;
3741import org .firebirdsql .management .FBServiceManager ;
3842import org .firebirdsql .management .ServiceManager ;
3943import org .firebirdsql .util .FirebirdSupportInfo ;
4044
41- import java .io .File ;
45+ import java .lang .reflect .Method ;
46+ import java .nio .file .Paths ;
4247import java .sql .DriverManager ;
4348import java .sql .SQLException ;
4449import 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 }
0 commit comments