Created
February 13, 2021 19:38
-
-
Save PramodKumarYadav/b4405dfaaf0c9c48e18767e39e496936 to your computer and use it in GitHub Desktop.
A solution to load config properties based on a chosen test environment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.powertester.testconfig; | |
import com.typesafe.config.Config; | |
import com.typesafe.config.ConfigFactory; | |
import lombok.extern.slf4j.Slf4j; | |
import org.apache.commons.lang3.StringUtils; | |
@Slf4j | |
public class TestConfig { | |
public static Config getConfig() { | |
Config appConfig = ConfigFactory.load(); | |
// See if any hostName is passed from command line | |
String hostName = System.getProperty("host"); | |
log.debug("hostName passed from command line: {}", hostName); | |
// If host name is not passed from command line, choose it from application.config | |
if (StringUtils.isEmpty(hostName)) { | |
hostName = appConfig.getString("host"); | |
} | |
log.debug("hostName used to load config: {}", hostName); | |
// Load properties specific for chosen host. | |
Config hostConfig = ConfigFactory.load(hostName); | |
// Merge properties from common properties (application.conf) and host specific properties (from host file) | |
Config mergedConfig = hostConfig.withFallback(appConfig); | |
return mergedConfig; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment