Skip to content
Permalink
Browse files

Modified configuration loading mechanism.

Renamed *ConfigLoader to *PropertiesLoader.
Introduced PropertyLoader and DefaultPropertyLoader
Introduced ProxyConfig which contains just the proxy config settings
Renamed package to org.concordion.cubano.config
Made Config and WebDriverConfig final
Removed inheritance from Config and WebDriverConfig
Introduced singletons for DefaultPropertiesLoader, Config and WebDriverConfig to ensure that properties files are only read once, and make it easy to access Config and WebDriverConfig
Removed HttpEasyConfig, since this just uses ProxyConfig
Modified WebDriverConfig to mirror Config and delegate to PropertyLoader

Any specific AppConfig, it will no longer be able to inherit from Config or WebDriverConfig and will need to be modified to delegate to PropertyLoader. See WebDriverConfig for example code.
  • Loading branch information...
nigelcharman committed Oct 23, 2017
1 parent 5af705f commit cd6eb05c73a3cd24eecfd2e1b2c84927de8b57d3
Showing with 1,181 additions and 1,101 deletions.
  1. +1 −1 cubano-config/src/main/java/org/concordion/cubano/{utils → config}/CaselessProperties.java
  2. +93 −0 cubano-config/src/main/java/org/concordion/cubano/config/Config.java
  3. +73 −65 ...java/org/concordion/cubano/{utils/DefaultConfigLoader.java → config/DefaultPropertiesLoader.java}
  4. +192 −0 cubano-config/src/main/java/org/concordion/cubano/config/DefaultPropertyLoader.java
  5. +23 −23 ...nfig/src/main/java/org/concordion/cubano/{utils/ConfigLoader.java → config/PropertiesLoader.java}
  6. +61 −0 cubano-config/src/main/java/org/concordion/cubano/config/PropertyLoader.java
  7. +195 −0 cubano-config/src/main/java/org/concordion/cubano/config/ProxyConfig.java
  8. +0 −431 cubano-config/src/main/java/org/concordion/cubano/utils/Config.java
  9. +35 −35 cubano-config/src/test/java/org/concordion/cubano/{utils → config}/CaselessPropertiesTest.java
  10. +62 −62 cubano-config/src/test/java/org/concordion/cubano/{utils → config}/ConfigTest.java
  11. +0 −19 cubano-config/src/test/java/org/concordion/cubano/utils/ConfigMock.java
  12. +1 −1 cubano-core/src/main/java/org/concordion/cubano/{utils → config}/ActionTimer.java
  13. +320 −320 cubano-core/src/main/java/org/concordion/cubano/{utils → config}/ActionWait.java
  14. +1 −1 cubano-core/src/main/java/org/concordion/cubano/{utils → config}/Growl.java
  15. +1 −1 cubano-core/src/main/java/org/concordion/cubano/{utils → config}/ISODateTimeFormat.java
  16. +1 −1 cubano-core/src/main/java/org/concordion/cubano/{utils → config}/TimeoutException.java
  17. +1 −1 cubano-core/src/main/java/org/concordion/cubano/{utils → config}/XMLGregorianCalendarHelper.java
  18. +1 −1 cubano-core/src/main/java/org/concordion/cubano/{utils → config}/data/DataCleanup.java
  19. +1 −1 cubano-core/src/main/java/org/concordion/cubano/{utils → config}/data/DataCleanupHelper.java
  20. +2 −2 cubano-core/src/main/java/org/concordion/cubano/{utils → config}/data/EntityPool.java
  21. +1 −1 cubano-core/src/main/java/org/concordion/cubano/{utils → config}/package-info.java
  22. +1 −1 cubano-core/src/main/java/org/concordion/cubano/data/XMLGregorianCalendarConverter.java
  23. +3 −3 cubano-core/src/main/javadoc/overview.md
  24. +1 −1 cubano-core/src/test/java/org/concordion/cubano/{utils → config}/ISODateTimeFormatTest.java
  25. +6 −5 cubano-httpeasy/src/main/java/org/concordion/cubano/driver/http/HttpEasy.java
  26. +0 −44 cubano-httpeasy/src/main/java/org/concordion/cubano/driver/http/HttpEasyConfig.java
  27. +3 −3 cubano-webdriver-manager/config.properties
  28. +19 −16 ...bdriver-manager/src/main/java/org/concordion/cubano/driver/web/provider/LocalBrowserProvider.java
  29. +60 −24 cubano-webdriver/src/main/java/org/concordion/cubano/driver/web/config/WebDriverConfig.java
  30. +11 −9 cubano-webdriver/src/main/java/org/concordion/cubano/driver/web/provider/RemoteBrowserProvider.java
  31. +4 −2 cubano-webdriver/src/test/java/org/concordion/cubano/driver/web/config/WebDriverConfigTest.java
  32. +2 −1 gradle/wrapper/gradle-wrapper.properties
  33. +4 −24 structure101-settings.javax.hsp
  34. +2 −2 structure101-spec.xml
@@ -1,4 +1,4 @@
package org.concordion.cubano.utils;
package org.concordion.cubano.config;

import java.util.Map;
import java.util.Properties;
@@ -0,0 +1,93 @@
package org.concordion.cubano.config;

import java.util.Properties;

/**
* Reads and supplies properties from the <code>config.properties</code> file that are required by the framework.
* <p>
* An optional <code>user.properties</code> file can set user specific values and allow overriding of defaults.
* The <code>user.properties</code> file should NEVER be checked into source control.
*
* @author Andrew Sumner
*/
public final class Config {

private final DefaultPropertyLoader propertyLoader;
private final ProxyConfig proxyConfig = new ProxyConfig();
private String environment;

private static class ConfigHolder {
static final Config INSTANCE = new Config();
}

public static Config getInstance() {
return ConfigHolder.INSTANCE;
}

/**
* Uses DefaultPropertiesLoader to import the config and user properties files.
*/
protected Config() {
this(DefaultPropertiesLoader.getInstance());
}

/**
* Uses the supplied PropertiesLoader to import the config and user properties files.
*
* @param propertiesLoader Configuration loader
*/
protected Config(PropertiesLoader propertiesLoader) {
this(propertiesLoader.getProperties(), propertiesLoader.getUserProperties());
}

/**
* Allow injection of properties for testing purposes.
*
* @param properties Default properties
*/
protected Config(Properties properties) {
this(properties, null);
}

/**
* Load the properties for this configuration.
*
* @param properties Default properties
* @param userProperties User specific overrides
*/
protected Config(Properties properties, Properties userProperties) {
propertyLoader = new DefaultPropertyLoader(properties, userProperties);

// Try environment variable first
environment = System.getProperty("environment", "");

if (environment.isEmpty()) {
environment = propertyLoader.getProperty("environment");
}

propertyLoader.setEnvironment(environment);

proxyConfig.loadProxyProperties(this, propertyLoader);
}

/**
* @return Configured environment.
*/
public String getEnvironment() {
return environment;
}

/**
* @return Configuration for proxy.
*/
public ProxyConfig getProxyConfig() {
return proxyConfig;
}

/**
* @return a loader for loading properties across config.properties and user.properties, taking environment into account.
*/
public PropertyLoader getPropertyLoader() {
return propertyLoader;
}
}
@@ -1,65 +1,73 @@
package org.concordion.cubano.utils;

import java.io.File;
import java.io.StringReader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Properties;

/**
* Loads configuration from config.properties and user.properties located in projects root folder.
*
* It also ensures that any single slash character '\' does not require a double slash. This means that
* using \ to wrap long property values across multiple lines won't work.
*/
public class DefaultConfigLoader implements ConfigLoader {
private static final String CONFIG_FILE = "config.properties";
private static final String USER_CONFIG_FILE = "user.properties";

private final Properties properties;
private final Properties userProperties;

/** Ensure properties have been loaded before any property is used. */
public DefaultConfigLoader() {
synchronized (DefaultConfigLoader.class) {
properties = loadFile(CONFIG_FILE);

if (new File(USER_CONFIG_FILE).exists()) {
userProperties = loadFile(USER_CONFIG_FILE);
} else {
userProperties = null;
}
}
}

/**
* Read properties from file, will ignoring the case of properties.
*
* @param filename Name of file to read, expected that it will be located in the projects root folder
* @return {@link CaselessProperties}
*/
private Properties loadFile(final String filename) {
Properties prop = new CaselessProperties();

try {
String content = new String(Files.readAllBytes(Paths.get(filename)));

// By default property files treat \ as an escape character
prop.load(new StringReader(content.replace("\\", "\\\\")));
} catch (Exception e) {
throw new RuntimeException("Unable to read properties file.", e);
}

return prop;
}

@Override
public Properties getProperties() {
return properties;
}

@Override
public Properties getUserProperties() {
return userProperties;
}
}
package org.concordion.cubano.config;

import java.io.File;
import java.io.StringReader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Properties;

/**
* Loads configuration from config.properties and user.properties located in projects root folder.
*
* It also ensures that any single slash character '\' does not require a double slash. This means that
* using \ to wrap long property values across multiple lines won't work.
*/
public class DefaultPropertiesLoader implements PropertiesLoader {
private static final String CONFIG_FILE = "config.properties";
private static final String USER_CONFIG_FILE = "user.properties";

private final Properties properties;
private final Properties userProperties;

private static class DPHolder {
static final DefaultPropertiesLoader INSTANCE = new DefaultPropertiesLoader();
}

public static DefaultPropertiesLoader getInstance() {
return DPHolder.INSTANCE;
}

/** Ensure properties have been loaded before any property is used. */
private DefaultPropertiesLoader() {
synchronized (DefaultPropertiesLoader.class) {
properties = loadFile(CONFIG_FILE);

if (new File(USER_CONFIG_FILE).exists()) {
userProperties = loadFile(USER_CONFIG_FILE);
} else {
userProperties = null;
}
}
}

/**
* Read properties from file, will ignoring the case of properties.
*
* @param filename Name of file to read, expected that it will be located in the projects root folder
* @return {@link CaselessProperties}
*/
private Properties loadFile(final String filename) {
Properties prop = new CaselessProperties();

try {
String content = new String(Files.readAllBytes(Paths.get(filename)));

// By default property files treat \ as an escape character
prop.load(new StringReader(content.replace("\\", "\\\\")));
} catch (Exception e) {
throw new RuntimeException("Unable to read properties file.", e);
}

return prop;
}

@Override
public Properties getProperties() {
return properties;
}

@Override
public Properties getUserProperties() {
return userProperties;
}
}

0 comments on commit cd6eb05

Please sign in to comment.
You can’t perform that action at this time.