Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions api/src/test/java/org/openmrs/test/BaseContextSensitiveTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,16 @@ public String getWebappName() {
* @return Properties runtime
*/
public Properties getRuntimeProperties() {

// cache the properties for subsequent calls
if (runtimeProperties == null)
runtimeProperties = TestUtil.getRuntimeProperties(getWebappName());

// ensure runtimeProperties is not null
if (runtimeProperties == null) {
// Fallback to an empty properties object to avoid NullPointerExceptions
runtimeProperties = new Properties();
}

// if we're using the in-memory hypersonic database, add those
// connection properties here to override what is in the runtime
Expand All @@ -335,17 +341,34 @@ public Properties getRuntimeProperties() {

// automatically create the tables defined in the hbm files
runtimeProperties.setProperty(Environment.HBM2DDL_AUTO, "create-drop");
}
else {

}else {
String url = System.getProperty("databaseUrl");
String username = System.getProperty("databaseUsername");
String password = System.getProperty("databasePassword");
String driver = System.getProperty("databaseDriver");
String dialect = System.getProperty("databaseDialect");

// Validate that all required system properties are present
if (url == null || username == null || password == null || driver == null || dialect == null) {
String msg = "Required database system properties are missing. When using -DuseInMemoryDatabase=false, "
+ "the following system properties must be set: databaseUrl, databaseUsername, databasePassword, "
+ "databaseDriver, and databaseDialect. "
+ "These should be set by Containers.ensureDatabaseRunning() or manually. "
+ "Example:\n"
+ "mvn -DuseInMemoryDatabase=false -Ddatabase=postgres -DdatabaseUrl=<jdbc> -DdatabaseUsername=<user> "
+ "-DdatabasePassword=<pass> -DdatabaseDriver=org.postgresql.Driver -DdatabaseDialect=org.hibernate.dialect.PostgreSQL95Dialect test";
log.error(msg);
throw new IllegalStateException(msg);
}


runtimeProperties.setProperty(Environment.URL, url);
runtimeProperties.setProperty(Environment.DRIVER, System.getProperty("databaseDriver"));
runtimeProperties.setProperty(Environment.DRIVER, driver);
runtimeProperties.setProperty(Environment.USER, username);
runtimeProperties.setProperty(Environment.PASS, password);
runtimeProperties.setProperty(Environment.DIALECT, System.getProperty("databaseDialect"));
runtimeProperties.setProperty(Environment.DIALECT, dialect);


// these properties need to be set in case the user has this exact
// phrasing in their runtime file.
Expand Down
52 changes: 42 additions & 10 deletions api/src/test/java/org/openmrs/test/Containers.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ public class Containers {
private static final String USERNAME = "test";
private static final String PASSWORD = "test";
private static final String DATABASE = "openmrs";

private static boolean schemaCreated = false;


public static void ensureDatabaseRunning() {
if (mysql != null || postgres != null) {

if(mysql != null || postgres != null) {
return;
}

Expand All @@ -51,9 +53,18 @@ private static void ensureMySQLRunning() {
}

if (!mysql.isRunning()) {

mysql.start();

try {
mysql.start();
} catch (Exception e) {
String msg = "Failed to start MySQL container. This usually means Docker is not available or not running. "
+ "Please ensure Docker is installed and running, or use -DuseInMemoryDatabase=true to use in-memory database. "
+ "Original error: " + e.getMessage();
throw new IllegalStateException(msg, e);
}
}

// Always set system properties if container is running, not just when starting
if (mysql.isRunning()) {
System.setProperty("databaseUrl", mysql.getJdbcUrl());
System.setProperty("databaseName", DATABASE);
System.setProperty("databaseUsername", USERNAME);
Expand All @@ -62,7 +73,13 @@ private static void ensureMySQLRunning() {
System.setProperty("databaseDialect", MySQLDialect.class.getName());
System.setProperty("database", "mysql");

createSchema();
// Only create schema once
if (!schemaCreated) {
createSchema();
schemaCreated = true;
}
} else {
throw new IllegalStateException("MySQL container failed to start. Please check Docker configuration.");
}
}

Expand Down Expand Up @@ -100,17 +117,32 @@ private static void ensurePostgreSQLRunning() {
}

if (!postgres.isRunning()) {

postgres.start();

try {
postgres.start();
} catch (Exception e) {
String msg = "Failed to start PostgreSQL container. This usually means Docker is not available or not running. "
+ "Please ensure Docker is installed and running, or use -DuseInMemoryDatabase=true to use in-memory database. "
+ "Original error: " + e.getMessage();
throw new IllegalStateException(msg, e);
}
}

// Always set system properties if container is running, not just when starting
if (postgres.isRunning()) {
System.setProperty("databaseUrl", postgres.getJdbcUrl());
System.setProperty("databaseName", DATABASE);
System.setProperty("databaseUsername", USERNAME);
System.setProperty("databasePassword", PASSWORD);
System.setProperty("databaseDriver", postgres.getDriverClassName());
System.setProperty("databaseDialect", PostgreSQLDialect.class.getName());

createSchema();
// Only create schema once
if (!schemaCreated) {
createSchema();
schemaCreated = true;
}
} else {
throw new IllegalStateException("PostgreSQL container failed to start. Please check Docker configuration.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,12 @@ public Properties getRuntimeProperties() {
if (runtimeProperties == null)
runtimeProperties = TestUtil.getRuntimeProperties(getWebappName());

// ensure runtimeProperties is not null
if (runtimeProperties == null) {
// Fallback to an empty properties object to avoid NullPointerExceptions
runtimeProperties = new Properties();
}

// if we're using the in-memory hypersonic database, add those
// connection properties here to override what is in the runtime
// properties
Expand All @@ -336,12 +342,27 @@ public Properties getRuntimeProperties() {
String url = System.getProperty("databaseUrl");
String username = System.getProperty("databaseUsername");
String password = System.getProperty("databasePassword");
String driver = System.getProperty("databaseDriver");
String dialect = System.getProperty("databaseDialect");

// Validate that all required system properties are present
if (url == null || username == null || password == null || driver == null || dialect == null) {
String msg = "Required database system properties are missing. When using -DuseInMemoryDatabase=false, "
+ "the following system properties must be set: databaseUrl, databaseUsername, databasePassword, "
+ "databaseDriver, and databaseDialect. "
+ "These should be set by Containers.ensureDatabaseRunning() or manually. "
+ "Example:\n"
+ "mvn -DuseInMemoryDatabase=false -Ddatabase=postgres -DdatabaseUrl=<jdbc> -DdatabaseUsername=<user> "
+ "-DdatabasePassword=<pass> -DdatabaseDriver=org.postgresql.Driver -DdatabaseDialect=org.hibernate.dialect.PostgreSQL95Dialect test";
log.error(msg);
throw new IllegalStateException(msg);
}

runtimeProperties.setProperty(Environment.URL, url);
runtimeProperties.setProperty(Environment.DRIVER, System.getProperty("databaseDriver"));
runtimeProperties.setProperty(Environment.DRIVER, driver);
runtimeProperties.setProperty(Environment.USER, username);
runtimeProperties.setProperty(Environment.PASS, password);
runtimeProperties.setProperty(Environment.DIALECT, System.getProperty("databaseDialect"));
runtimeProperties.setProperty(Environment.DIALECT, dialect);

// these properties need to be set in case the user has this exact
// phrasing in their runtime file.
Expand Down