From 0bc7e3e07ff41b71eb443ce00293d2068dc6a38f Mon Sep 17 00:00:00 2001 From: Michael-A-McMahon Date: Fri, 12 Mar 2021 18:05:01 -0800 Subject: [PATCH] Aborting test run if config is missing --- .../java/oracle/r2dbc/DatabaseConfig.java | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/test/java/oracle/r2dbc/DatabaseConfig.java b/src/test/java/oracle/r2dbc/DatabaseConfig.java index c827823..9701914 100644 --- a/src/test/java/oracle/r2dbc/DatabaseConfig.java +++ b/src/test/java/oracle/r2dbc/DatabaseConfig.java @@ -29,15 +29,12 @@ import org.reactivestreams.Publisher; import java.io.FileNotFoundException; -import java.io.IOException; import java.io.InputStream; import java.sql.DriverManager; import java.sql.SQLException; import java.time.Duration; import java.util.Properties; -import static org.junit.jupiter.api.Assertions.fail; - /** * Stores configuration used by integration tests that connect to a database. * The configuration is read from a resource file named "config.properties" @@ -194,12 +191,13 @@ public static int databaseVersion() { if (inputStream == null) { throw new FileNotFoundException( - "property file '" + CONFIG_FILE_NAME - + "' not found in the classpath"); + CONFIG_FILE_NAME + " resource not found. " + + "Check if it exists under src/test/resources/"); } Properties prop = new Properties(); prop.load(inputStream); + HOST = prop.getProperty("HOST"); PORT = Integer.parseInt(prop.getProperty("PORT")); SERVICE_NAME = prop.getProperty("DATABASE"); @@ -224,8 +222,17 @@ public static int databaseVersion() { CONNECTION_FACTORY.create(), CONNECTION_FACTORY.getMetadata()); } - catch (IOException loadFileException) { - throw new RuntimeException(loadFileException); + catch (Throwable initializationFailure) { + // Most test cases require a database connection; If it can't be + // configured, then the test run can not proceed. Print the failure and + // terminate the JVM: + initializationFailure.printStackTrace(); + System.exit(-1); + + // This throw is dead code; exit(-1) doesn't return. This throw helps + // javac to understand that the final fields are always initialized when + // this static initialization block returns successfully. + throw new RuntimeException(initializationFailure); } } }