public final class Configuration extends Object
HOCON Configuration tools to support unit testing
HOCON configurations can be loaded and activated at any time :
Map<String, String> substitutions = new HashMap<>(); // example load / activate on this node // config = new Configuration("test1.conf").load().activate();
and subsequently deactivated and removed :
// example deactive / remove on this node // config.deactivate().remove();
Substitution parameters can be specified :
// example load / activate on this node // substitutions.put("param", "value"); config = new Configuration("test1.conf", substitutions).load().activate();
By default, the configuration is loaded on the local node only - to load on a different node or on the cluster, the serviceName can be specified :
// example load / activate on a specific node or cluster // config = new Configuration("A.test-framework", "test2.conf", substitutions).load().activate(); config.deactivate().remove(); config = new Configuration("test-framework", "test3.conf", substitutions).load().activate(); config.deactivate().remove();
Test can call the deactiveRemoveAll function to automatically deactivate and remove configurations that are still loaded.
// Deactivate configurations previous loaded // Configuration.deactiveAndRemoveAll();
Constructor and Description |
---|
Configuration(String resource)
Create a configuration instance
|
Configuration(String resource,
Map<String,String> substitutions)
Create a configuration instance
|
Configuration(String serviceName,
String resource,
Map<String,String> substitutions)
Create a configuration instance
|
Configuration(String serviceName,
String resource,
Map<String,String> substitutions,
String substitutionResource)
Create a configuration instance
|
Modifier and Type | Method and Description |
---|---|
Configuration |
activate()
Activate this configuration
|
Configuration |
deactivate()
Deactivate this configuration
|
static void |
deactiveAndRemoveAll()
Deactivate and remove configurations in reverse order
|
static Configuration |
forFile(String resource)
Create a configuration instance
|
static Configuration |
forFile(String resource,
Map<String,String> substitutions)
Create a configuration instance
|
static Configuration |
forFile(String serviceName,
String resource,
Map<String,String> substitutions)
Create a configuration instance
|
static Configuration |
forFile(String serviceName,
String resource,
Map<String,String> substitutions,
String substitutionResource)
Create a configuration instance
|
static List<Configuration> |
getLoadedConfigurations()
Get all loaded configurations
|
Optional<String> |
getName()
Get the currently loaded configuration name
|
Optional<String> |
getType()
Get the currently loaded configuration type
|
Optional<String> |
getVersion()
Get the currently loaded configuration version
|
boolean |
isActive()
Check if this configuration is activated
|
boolean |
isLoaded()
Check if this configuration is loaded
|
Configuration |
load()
Load this configuration
|
void |
remove()
Remove this configuration
|
String |
toString() |
public Configuration(String serviceName, String resource, Map<String,String> substitutions)
Create a configuration instance
serviceName
- service name, can not be nullresource
- HOCON configuration resource to load, can not be nullsubstitutions
- Substitution variables, can not be nullpublic Configuration(String serviceName, String resource, Map<String,String> substitutions, String substitutionResource)
Create a configuration instance
serviceName
- service name, can not be nullresource
- HOCON configuration resource to load, can not be nullsubstitutions
- Substitution variables, can not be nullsubstitutionResource
- Substitutions resource file, can not be nullpublic Configuration(String resource, Map<String,String> substitutions)
Create a configuration instance
resource
- HOCON configuration resource to load, can not be nullsubstitutions
- Substitution variables, can not be nullpublic Configuration(String resource)
Create a configuration instance
resource
- HOCON configuration resource to load, can not be nullpublic static Configuration forFile(String serviceName, String resource, Map<String,String> substitutions)
Create a configuration instance
serviceName
- service name, can not be nullresource
- HOCON configuration resource to load, can not be nullsubstitutions
- Substitution variables, can not be nullpublic static Configuration forFile(String serviceName, String resource, Map<String,String> substitutions, String substitutionResource)
Create a configuration instance
serviceName
- service name, can not be nullresource
- HOCON configuration resource to load, can not be nullsubstitutions
- Substitution variables, can not be nullsubstitutionResource
- Substitutions resource file, can not be nullpublic static Configuration forFile(String resource, Map<String,String> substitutions)
Create a configuration instance
resource
- HOCON configuration resource to load, can not be nullsubstitutions
- Substitution variables, can not be nullpublic static Configuration forFile(String resource)
Create a configuration instance
resource
- HOCON configuration resource to load, can not be nullpublic Configuration load() throws ConfigurationException
Load this configuration
This method can be run within or outside a transaction
ConfigurationException
- on configuration failure or already loadedpublic Configuration activate() throws ConfigurationException
Activate this configuration
Fails if configuration is not loaded
This method can be run within or outside a transaction
ConfigurationException
- on configuration failure or already activepublic Configuration deactivate() throws ConfigurationException
Deactivate this configuration
Fails if configuration is not active
This method can be run within or outside a transaction
ConfigurationException
- on configuration failure or not activepublic void remove() throws ConfigurationException
Remove this configuration
Fails if configuration is still active
This method can be run within or outside a transaction
ConfigurationException
- on configuration failure or still activepublic static List<Configuration> getLoadedConfigurations()
public boolean isActive()
Check if this configuration is activated
This method can be run within or outside a transaction
public boolean isLoaded()
Check if this configuration is loaded
This method can be run within or outside a transaction
public Optional<String> getVersion()
public static void deactiveAndRemoveAll()
Deactivate and remove configurations in reverse order
This method can be run within or outside a transaction
Errors are ignored
Copyright © 2015–2019 Cloud Software Group, Inc.. All rights reserved.