Changeset: cb8e8a8aaf0c for monetdb-java
URL: https://dev.monetdb.org/hg/monetdb-java/rev/cb8e8a8aaf0c
Modified Files:
        .gitignore
        src/test/java/org/monetdb/testinfra/Config.java
Branch: mvn
Log Message:

Also load test settings from test.properties in the current working dir


diffs (147 lines):

diff --git a/.gitignore b/.gitignore
--- a/.gitignore
+++ b/.gitignore
@@ -3,6 +3,9 @@ target
 jars
 build
 
+# local overrides while testing
+test.properties
+
 # files generated by various editors
 *.swp
 *~
diff --git a/src/test/java/org/monetdb/testinfra/Config.java 
b/src/test/java/org/monetdb/testinfra/Config.java
--- a/src/test/java/org/monetdb/testinfra/Config.java
+++ b/src/test/java/org/monetdb/testinfra/Config.java
@@ -11,8 +11,15 @@
  */
 package org.monetdb.testinfra;
 
+import java.io.IOException;
+import java.io.Reader;
 import java.net.URI;
 import java.net.URISyntaxException;
+import java.nio.file.Files;
+import java.nio.file.InvalidPathException;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Properties;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -22,9 +29,19 @@ import static org.junit.jupiter.api.Asse
 /**
  * Centralize the names of the system properties and environment variables 
used in the tests.
  *
- * This class also provides getters.
+ * This class provides a number of useful getters for test config info.
+
+ * First we look for a System property.
+ * Then an environment variable.
+ * Then for a property in Properties file System.getProperty("user.dir") / 
"test.properties"
+ * Then we return the default value
+ *
+ * We return the first result that's not null and not "".
+ * We only load the config file once.
  */
 public class Config {
+       public static final String CONFIG_FILE = "test.properties";
+
        public static final String SERVER_URL_PROPERTY = "test.url";
        public static final String SERVER_URL_ENVVAR = "MONETDB_TEST_URL";
        public static final String SERVER_URL_DEFAULT = 
"jdbc:monetdb:///testjdbc";
@@ -46,45 +63,65 @@ public class Config {
        public static final String SKIP_SLOW_PROPERTY = "test.skipslow";
        public static final String SKIP_SLOW_ENVVAR = "MONETDB_TEST_SKIP_SLOW";
 
+       // only access these while synchronized on cachedConfigFile:
+       private static final Properties cachedConfigFile = new Properties();
+       private static boolean configLoaded = false;
+
        private static String lookup(String propName, String envName, String 
defaultValue) {
-               String value = null;
+               String value;
+
+               value = lookupSystemProperty(propName);
+               if (value != null && !value.isEmpty())
+                       return value;
+
+               value = lookupEnvironmentVariable(envName);
+               if (value != null && !value.isEmpty())
+                       return value;
+
+               value = lookupConfigFile(propName);
+               if (value != null && !value.isEmpty())
+                       return value;
+
+               return defaultValue;
+       }
+
+       private static String lookupSystemProperty(String propName) {
                try {
                        if (propName != null)
-                               value = System.getProperty(propName);
-                       if (value != null && !value.equals(""))
-                               return value;
-               } catch (SecurityException ignored) {
-               }
+                               return System.getProperty(propName);
+               } catch (SecurityException ignored) {}
+               return null;
+       }
 
+       private static String lookupEnvironmentVariable(String envName) {
                try {
                        if (envName != null)
-                               value = System.getenv(envName);
-                       if (value != null)
-                               return value;
-               } catch (SecurityException ignored) {
-               }
-
-               if (defaultValue != null)
-                       return defaultValue;
-
-               if (propName != null && envName != null)
-                       return fail("Neither property '" + propName + "' nor 
environment variable '" + envName + "' is set");
-               else if (propName != null)
-                       return fail("Property '" + propName + "' is not set");
-               else if (envName != null)
-                       return fail("Environment variable '" + envName + "' is 
not set");
-               else
-                       return fail("propName and envName cannot both be null");
+                               return System.getenv(envName);
+               } catch (SecurityException ignored) {}
+               return null;
        }
 
-       public static String getRawServerURL() {
-               return lookup(SERVER_URL_PROPERTY, SERVER_URL_ENVVAR, 
SERVER_URL_DEFAULT);
+       private static String lookupConfigFile(String propName) {
+               if (propName == null)
+                       return null;
+               synchronized (cachedConfigFile) {
+                       if (!configLoaded) {
+                               configLoaded = true;
+                               try {
+                                       Path path = 
Paths.get(System.getProperty("user.dir"), CONFIG_FILE);
+                                       try (Reader rd = 
Files.newBufferedReader(path)) {
+                                               cachedConfigFile.load(rd);
+                                       }
+                               } catch (IOException | SecurityException | 
InvalidPathException ignored) {}
+                       }
+               }
+               return cachedConfigFile.getProperty(propName);
        }
 
        public static String getServerURL() {
-               String raw = getRawServerURL();
-               String fixed = addDefaultCredentials(raw);
-               return fixed;
+               String rawUrl = lookup(SERVER_URL_PROPERTY, SERVER_URL_ENVVAR, 
SERVER_URL_DEFAULT);
+               String jdbcUrl = prependJdbc(rawUrl);
+               return addDefaultCredentials(jdbcUrl);
        }
 
        public static boolean isSkipMalOutput() {
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to