use of org.projectnessie.versioned.persist.adapter.DatabaseAdapterConfig in project nessie by projectnessie.
the class DatabaseAdapters method createDatabaseConnectionProvider.
public static DatabaseConnectionProvider<DatabaseConnectionConfig> createDatabaseConnectionProvider(String databaseAdapterName, Map<String, String> configuration) {
DatabaseAdapterFactory<DatabaseAdapterConfig, DatabaseAdapterConfig, DatabaseConnectionProvider<?>> factory = DatabaseAdapterFactory.loadFactoryByName(databaseAdapterName);
String providerSpec = databaseAdapterName.indexOf(':') == -1 ? null : databaseAdapterName.substring(databaseAdapterName.indexOf(':') + 1).toLowerCase(Locale.ROOT);
DatabaseAdapterFactory.Builder<DatabaseAdapterConfig, DatabaseAdapterConfig, DatabaseConnectionProvider<?>> builder = factory.newBuilder();
TestConnectionProviderSource<DatabaseConnectionConfig> providerSource = TestConnectionProviderSource.findCompatibleProviderSource(builder.getConfig(), factory, providerSpec);
providerSource.configureConnectionProviderConfigFromDefaults(config -> SystemPropertiesConfigurer.configureFromPropertiesGeneric(config, DatabaseConnectionConfig.class, prop -> configuration.getOrDefault(prop, System.getProperty(prop))));
try {
// createConnectionProvider method is protected, not defined on DatabaseConnectionProvider,
// but on AbstractTestConnectionProviderSource
Method createConnectionProviderMethod = providerSource.getClass().getMethod("createConnectionProvider");
@SuppressWarnings("unchecked") DatabaseConnectionProvider<DatabaseConnectionConfig> connectionProvider = (DatabaseConnectionProvider<DatabaseConnectionConfig>) createConnectionProviderMethod.invoke(providerSource);
connectionProvider.configure(providerSource.getConnectionProviderConfig());
connectionProvider.initialize();
return connectionProvider;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of org.projectnessie.versioned.persist.adapter.DatabaseAdapterConfig in project nessie by projectnessie.
the class TestTryLoopState method mockedConfig.
DatabaseAdapterConfig mockedConfig(int commitRetries, long commitTimeout, long lowerDefault, long upperDefault, long maxSleep) {
DatabaseAdapterConfig mock = mock(DatabaseAdapterConfig.class);
when(mock.getCommitRetries()).thenReturn(commitRetries);
when(mock.getCommitTimeout()).thenReturn(commitTimeout);
when(mock.getRetryInitialSleepMillisLower()).thenReturn(lowerDefault);
when(mock.getRetryInitialSleepMillisUpper()).thenReturn(upperDefault);
when(mock.getRetryMaxSleepMillis()).thenReturn(maxSleep);
return mock;
}
use of org.projectnessie.versioned.persist.adapter.DatabaseAdapterConfig in project nessie by projectnessie.
the class DatabaseAdapterExtension method extractCustomConfiguration.
private static Function<AdjustableDatabaseAdapterConfig, DatabaseAdapterConfig> extractCustomConfiguration(NessieDbAdapter adapterAnnotation, ExtensionContext context) {
Function<AdjustableDatabaseAdapterConfig, DatabaseAdapterConfig> applyCustomConfig = c -> c;
if (!adapterAnnotation.configMethod().isEmpty()) {
Method configMethod = findMethod(context.getRequiredTestClass(), adapterAnnotation.configMethod(), AdjustableDatabaseAdapterConfig.class).orElseThrow(() -> new IllegalArgumentException(String.format("%s.configMethod='%s' does not exist in %s", NessieDbAdapter.class.getSimpleName(), adapterAnnotation.configMethod(), context.getRequiredTestClass().getName())));
makeAccessible(configMethod);
if (!Modifier.isStatic(configMethod.getModifiers()) || Modifier.isPrivate(configMethod.getModifiers()) || !DatabaseAdapterConfig.class.isAssignableFrom(configMethod.getReturnType())) {
throw new IllegalArgumentException(String.format("%s.configMethod='%s' must have the signature 'static %s %s(%s)' in %s", NessieDbAdapter.class.getSimpleName(), adapterAnnotation.configMethod(), DatabaseAdapterConfig.class.getSimpleName(), adapterAnnotation.configMethod(), AdjustableDatabaseAdapterConfig.class.getSimpleName(), context.getRequiredTestClass().getName()));
}
applyCustomConfig = c -> {
try {
return (DatabaseAdapterConfig) configMethod.invoke(null, c);
} catch (InvocationTargetException | IllegalAccessException e) {
throw new RuntimeException(e);
}
};
}
return applyCustomConfig;
}
Aggregations