[New] Added environment variable to override DB host for tests

This commit is contained in:
Robert von Burg 2022-03-08 09:42:54 +01:00
parent 0d6da250bd
commit b4fb7438e7
4 changed files with 40 additions and 17 deletions

View File

@ -129,6 +129,10 @@ public abstract class DbConnectionBuilder {
String host = tmp.substring(0, tmp.indexOf('/')); String host = tmp.substring(0, tmp.indexOf('/'));
String dbName = tmp.substring(tmp.indexOf('/')); String dbName = tmp.substring(tmp.indexOf('/'));
String hostOverride = System.getProperty(PROP_DB_HOST_OVERRIDE); String hostOverride = System.getProperty(PROP_DB_HOST_OVERRIDE);
if (host.equals(hostOverride))
return dbUrl;
logger.warn("[" + realm + "] Replacing db host " + host + " with override " + hostOverride); logger.warn("[" + realm + "] Replacing db host " + host + " with override " + hostOverride);
dbUrl = "jdbc:postgresql://" + hostOverride + dbName; dbUrl = "jdbc:postgresql://" + hostOverride + dbName;
logger.warn("[" + realm + "] DB URL is now " + dbUrl); logger.warn("[" + realm + "] DB URL is now " + dbUrl);

View File

@ -16,10 +16,12 @@
package li.strolch.persistence.postgresql.dao.test; package li.strolch.persistence.postgresql.dao.test;
import static java.util.Comparator.comparing; import static java.util.Comparator.comparing;
import static li.strolch.db.DbConstants.PROP_DB_HOST_OVERRIDE;
import static li.strolch.persistence.postgresql.PostgreSqlPersistenceHandler.SCRIPT_PREFIX_ARCHIVE; import static li.strolch.persistence.postgresql.PostgreSqlPersistenceHandler.SCRIPT_PREFIX_ARCHIVE;
import static li.strolch.persistence.postgresql.PostgreSqlPersistenceHandler.SCRIPT_PREFIX_STROLCH; import static li.strolch.persistence.postgresql.PostgreSqlPersistenceHandler.SCRIPT_PREFIX_STROLCH;
import static li.strolch.persistence.postgresql.dao.test.CachedDaoTest.*; import static li.strolch.persistence.postgresql.dao.test.CachedDaoTest.*;
import static li.strolch.runtime.StrolchConstants.DEFAULT_REALM; import static li.strolch.runtime.StrolchConstants.DEFAULT_REALM;
import static li.strolch.runtime.configuration.DbConnectionBuilder.overridePostgresqlHost;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import java.io.File; import java.io.File;
@ -64,28 +66,32 @@ public class DbSchemaCreationTest {
logger.info("Trying to create DB schema for " + scriptPrefix); logger.info("Trying to create DB schema for " + scriptPrefix);
logger.info(""); logger.info("");
String dbUrl = DB_URL;
if (System.getProperties().containsKey(PROP_DB_HOST_OVERRIDE))
dbUrl = overridePostgresqlHost(DbSchemaCreationTest.class.getSimpleName(), dbUrl);
DbSchemaVersionCheck dbCheck = new DbSchemaVersionCheck(scriptPrefix, PostgreSqlPersistenceHandler.class, true, DbSchemaVersionCheck dbCheck = new DbSchemaVersionCheck(scriptPrefix, PostgreSqlPersistenceHandler.class, true,
true, true); true, true);
try (Connection con = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD)) { try (Connection con = DriverManager.getConnection(dbUrl, DB_USERNAME, DB_PASSWORD)) {
// CREATE 0.1.0 // CREATE 0.1.0
dbCheck.createSchema(con, DEFAULT_REALM, Version.valueOf("0.1.0")); dbCheck.createSchema(con, DEFAULT_REALM, Version.valueOf("0.1.0"));
File scriptsD = new File("src/main/resources"); File scriptsD = new File("src/main/resources");
File[] scriptFiles = scriptsD File[] scriptFiles = scriptsD.listFiles(
.listFiles(f -> f.getName().startsWith(scriptPrefix) && f.getName().endsWith("_initial.sql")); f -> f.getName().startsWith(scriptPrefix) && f.getName().endsWith("_initial.sql"));
assertNotNull(scriptFiles); assertNotNull(scriptFiles);
Arrays.sort(scriptFiles, comparing(File::getName)); Arrays.sort(scriptFiles, comparing(File::getName));
for (File scriptFile : scriptFiles) { for (File scriptFile : scriptFiles) {
String name = scriptFile.getName(); String name = scriptFile.getName();
String versionS = name String versionS = name.substring((scriptPrefix + "_db_schema_").length(),
.substring((scriptPrefix + "_db_schema_").length(), name.length() - "_initial.sql".length()); name.length() - "_initial.sql".length());
Version version = Version.valueOf(versionS); Version version = Version.valueOf(versionS);
logger.info("Creating Version " + version); logger.info("Creating Version " + version);
dropSchema(DbSchemaCreationTest.class.getSimpleName(), scriptPrefix, DB_URL, DB_USERNAME, DB_PASSWORD); dropSchema(DbSchemaCreationTest.class.getSimpleName(), scriptPrefix, dbUrl, DB_USERNAME, DB_PASSWORD);
// CREATE // CREATE
dbCheck.createSchema(con, DEFAULT_REALM, version); dbCheck.createSchema(con, DEFAULT_REALM, version);
@ -93,7 +99,7 @@ public class DbSchemaCreationTest {
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Failed to open DB connection to URL {0} due to: {1}"; //$NON-NLS-1$ String msg = "Failed to open DB connection to URL {0} due to: {1}"; //$NON-NLS-1$
msg = MessageFormat.format(msg, DB_URL, e.getMessage()); msg = MessageFormat.format(msg, dbUrl, e.getMessage());
throw new DbException(msg, e); throw new DbException(msg, e);
} }
} }

View File

@ -15,10 +15,12 @@
*/ */
package li.strolch.persistence.postgresql.dao.test; package li.strolch.persistence.postgresql.dao.test;
import static li.strolch.db.DbConstants.PROP_DB_HOST_OVERRIDE;
import static li.strolch.persistence.postgresql.PostgreSqlPersistenceHandler.SCRIPT_PREFIX_ARCHIVE; import static li.strolch.persistence.postgresql.PostgreSqlPersistenceHandler.SCRIPT_PREFIX_ARCHIVE;
import static li.strolch.persistence.postgresql.PostgreSqlPersistenceHandler.SCRIPT_PREFIX_STROLCH; import static li.strolch.persistence.postgresql.PostgreSqlPersistenceHandler.SCRIPT_PREFIX_STROLCH;
import static li.strolch.persistence.postgresql.dao.test.CachedDaoTest.*; import static li.strolch.persistence.postgresql.dao.test.CachedDaoTest.*;
import static li.strolch.runtime.StrolchConstants.DEFAULT_REALM; import static li.strolch.runtime.StrolchConstants.DEFAULT_REALM;
import static li.strolch.runtime.configuration.DbConnectionBuilder.overridePostgresqlHost;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
@ -43,8 +45,10 @@ public class DbSchemaMigrationTest {
@BeforeClass @BeforeClass
public static void beforeClass() throws Exception { public static void beforeClass() throws Exception {
dropSchema(DbSchemaMigrationTest.class.getSimpleName(), SCRIPT_PREFIX_ARCHIVE, DB_URL, DB_USERNAME, DB_PASSWORD); dropSchema(DbSchemaMigrationTest.class.getSimpleName(), SCRIPT_PREFIX_ARCHIVE, DB_URL, DB_USERNAME,
dropSchema(DbSchemaMigrationTest.class.getSimpleName(), SCRIPT_PREFIX_STROLCH, DB_URL, DB_USERNAME, DB_PASSWORD); DB_PASSWORD);
dropSchema(DbSchemaMigrationTest.class.getSimpleName(), SCRIPT_PREFIX_STROLCH, DB_URL, DB_USERNAME,
DB_PASSWORD);
} }
@Test @Test
@ -60,27 +64,31 @@ public class DbSchemaMigrationTest {
logger.info("Trying to migrate DB schema from 0.1.0 upwards..."); logger.info("Trying to migrate DB schema from 0.1.0 upwards...");
logger.info(""); logger.info("");
String dbUrl = DB_URL;
if (System.getProperties().containsKey(PROP_DB_HOST_OVERRIDE))
dbUrl = overridePostgresqlHost(DbSchemaCreationTest.class.getSimpleName(), dbUrl);
// first clear DB // first clear DB
dropSchema(DbSchemaMigrationTest.class.getSimpleName(), scriptPrefix, DB_URL, DB_USERNAME, DB_PASSWORD); dropSchema(DbSchemaMigrationTest.class.getSimpleName(), scriptPrefix, dbUrl, DB_USERNAME, DB_PASSWORD);
DbSchemaVersionCheck dbCheck = new DbSchemaVersionCheck(scriptPrefix, PostgreSqlPersistenceHandler.class, true, DbSchemaVersionCheck dbCheck = new DbSchemaVersionCheck(scriptPrefix, PostgreSqlPersistenceHandler.class, true,
true, true); true, true);
try (Connection con = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD)) { try (Connection con = DriverManager.getConnection(dbUrl, DB_USERNAME, DB_PASSWORD)) {
// CREATE 0.1.0 // CREATE 0.1.0
Version currentVersion = Version.valueOf("0.1.0"); Version currentVersion = Version.valueOf("0.1.0");
dbCheck.createSchema(con, DEFAULT_REALM, currentVersion); dbCheck.createSchema(con, DEFAULT_REALM, currentVersion);
Version expectedDbVersion = DbSchemaVersionCheck Version expectedDbVersion = DbSchemaVersionCheck.getExpectedDbVersion(scriptPrefix,
.getExpectedDbVersion(scriptPrefix, PostgreSqlPersistenceHandler.class); PostgreSqlPersistenceHandler.class);
// MIGRATE // MIGRATE
dbCheck.migrateSchema(con, DEFAULT_REALM, currentVersion, expectedDbVersion); dbCheck.migrateSchema(con, DEFAULT_REALM, currentVersion, expectedDbVersion);
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Failed to open DB connection to URL {0} due to: {1}"; //$NON-NLS-1$ String msg = "Failed to open DB connection to URL {0} due to: {1}"; //$NON-NLS-1$
msg = MessageFormat.format(msg, DB_URL, e.getMessage()); msg = MessageFormat.format(msg, dbUrl, e.getMessage());
throw new DbException(msg, e); throw new DbException(msg, e);
} }
} }

View File

@ -15,6 +15,8 @@
*/ */
package li.strolch.command; package li.strolch.command;
import static li.strolch.db.DbConstants.PROP_DB_HOST_OVERRIDE;
import static li.strolch.runtime.configuration.DbConnectionBuilder.overridePostgresqlHost;
import static li.strolch.service.test.AbstractRealmServiceTest.*; import static li.strolch.service.test.AbstractRealmServiceTest.*;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
@ -49,7 +51,11 @@ public abstract class AbstractRealmCommandTest {
@Before @Before
public void beforeClass() throws Exception { public void beforeClass() throws Exception {
dropSchema("jdbc:postgresql://localhost/cacheduserdb", "cacheduser", "test"); String dbUrl = "jdbc:postgresql://localhost/cacheduserdb";
if (System.getProperties().containsKey(PROP_DB_HOST_OVERRIDE))
dbUrl = overridePostgresqlHost(AbstractRealmCommandTest.class.getSimpleName(), dbUrl);
dropSchema(dbUrl, "cacheduser", "test");
File rootPath = new File(RUNTIME_PATH); File rootPath = new File(RUNTIME_PATH);
File configSrc = new File(CONFIG_SRC); File configSrc = new File(CONFIG_SRC);
@ -127,8 +133,7 @@ public abstract class AbstractRealmCommandTest {
private Command command; private Command command;
/** /**
* @param container *
* @param tx
*/ */
public FailCommandFacade(ComponentContainer container, StrolchTransaction tx, Command command) { public FailCommandFacade(ComponentContainer container, StrolchTransaction tx, Command command) {
super(tx); super(tx);