Skip to content
Snippets Groups Projects
Commit 37def774 authored by Peter van 't Hof's avatar Peter van 't Hof
Browse files

Make config map immutable from outside the class

parent ec418017
No related branches found
No related tags found
No related merge requests found
...@@ -23,10 +23,10 @@ import nl.lumc.sasc.biopet.utils.ConfigUtils._ ...@@ -23,10 +23,10 @@ import nl.lumc.sasc.biopet.utils.ConfigUtils._
/** /**
* This class can store nested config values * This class can store nested config values
* @param map Map with value for new config * @param _map Map with value for new config
* @constructor Load config with existing map * @constructor Load config with existing map
*/ */
class Config(var map: Map[String, Any], class Config(protected var _map: Map[String, Any],
protected[core] var defaults: Map[String, Any] = Map()) extends Logging { protected[core] var defaults: Map[String, Any] = Map()) extends Logging {
logger.debug("Init phase of config") logger.debug("Init phase of config")
...@@ -36,6 +36,8 @@ class Config(var map: Map[String, Any], ...@@ -36,6 +36,8 @@ class Config(var map: Map[String, Any],
loadDefaultConfig() loadDefaultConfig()
} }
def map = _map
/** /**
* Loading a environmental variable as location of config files to merge into the config * Loading a environmental variable as location of config files to merge into the config
* @param valueName Name of value * @param valueName Name of value
...@@ -71,9 +73,9 @@ class Config(var map: Map[String, Any], ...@@ -71,9 +73,9 @@ class Config(var map: Map[String, Any],
else defaults = mergeMaps(configMap, defaults) else defaults = mergeMaps(configMap, defaults)
logger.debug("New defaults: " + defaults) logger.debug("New defaults: " + defaults)
} else { } else {
if (map.isEmpty) map = configMap if (_map.isEmpty) _map = configMap
else map = mergeMaps(configMap, map) else _map = mergeMaps(configMap, _map)
logger.debug("New config: " + map) logger.debug("New config: " + _map)
} }
} }
...@@ -87,7 +89,7 @@ class Config(var map: Map[String, Any], ...@@ -87,7 +89,7 @@ class Config(var map: Map[String, Any],
def addValue(key: String, value: Any, path: List[String] = Nil, default: Boolean = false): Unit = { def addValue(key: String, value: Any, path: List[String] = Nil, default: Boolean = false): Unit = {
val valueMap = path.foldRight(Map(key -> value))((a, b) => Map(a -> b)) val valueMap = path.foldRight(Map(key -> value))((a, b) => Map(a -> b))
if (default) defaults = mergeMaps(valueMap, defaults) if (default) defaults = mergeMaps(valueMap, defaults)
else map = mergeMaps(valueMap, map) else _map = mergeMaps(valueMap, _map)
} }
protected[config] var notFoundCache: List[ConfigValueIndex] = List() protected[config] var notFoundCache: List[ConfigValueIndex] = List()
...@@ -106,7 +108,7 @@ class Config(var map: Map[String, Any], ...@@ -106,7 +108,7 @@ class Config(var map: Map[String, Any],
* @param s key * @param s key
* @return True if exist * @return True if exist
*/ */
def contains(s: String): Boolean = map.contains(s) def contains(s: String): Boolean = _map.contains(s)
/** /**
* Checks if value exist in config * Checks if value exist in config
...@@ -131,7 +133,7 @@ class Config(var map: Map[String, Any], ...@@ -131,7 +133,7 @@ class Config(var map: Map[String, Any],
fixedCache += (requestedIndex -> fixedValue.get) fixedCache += (requestedIndex -> fixedValue.get)
true true
} else { } else {
val value = Config.getValueFromMap(map, requestedIndex) val value = Config.getValueFromMap(_map, requestedIndex)
if (value.isDefined && value.get.value != None) { if (value.isDefined && value.get.value != None) {
foundCache += (requestedIndex -> value.get) foundCache += (requestedIndex -> value.get)
true true
...@@ -215,7 +217,7 @@ class Config(var map: Map[String, Any], ...@@ -215,7 +217,7 @@ class Config(var map: Map[String, Any],
val fullEffective = ConfigUtils.mergeMaps(effectiveFound, effectiveDefaultFound) val fullEffective = ConfigUtils.mergeMaps(effectiveFound, effectiveDefaultFound)
val fullEffectiveWithNotFound = ConfigUtils.mergeMaps(fullEffective, notFound) val fullEffectiveWithNotFound = ConfigUtils.mergeMaps(fullEffective, notFound)
writeMapToJsonFile(this.map, "input") writeMapToJsonFile(this._map, "input")
writeMapToJsonFile(found, "found") writeMapToJsonFile(found, "found")
writeMapToJsonFile(effectiveFound, "effective.found") writeMapToJsonFile(effectiveFound, "effective.found")
writeMapToJsonFile(effectiveDefaultFound, "effective.defaults") writeMapToJsonFile(effectiveDefaultFound, "effective.defaults")
...@@ -224,7 +226,7 @@ class Config(var map: Map[String, Any], ...@@ -224,7 +226,7 @@ class Config(var map: Map[String, Any],
writeMapToJsonFile(fullEffectiveWithNotFound, "effective.full.notfound") writeMapToJsonFile(fullEffectiveWithNotFound, "effective.full.notfound")
} }
override def toString: String = map.toString() override def toString: String = _map.toString()
} }
object Config extends Logging { object Config extends Logging {
...@@ -236,7 +238,7 @@ object Config extends Logging { ...@@ -236,7 +238,7 @@ object Config extends Logging {
* @param config2 Low prio map * @param config2 Low prio map
* @return Merged config * @return Merged config
*/ */
def mergeConfigs(config1: Config, config2: Config): Config = new Config(mergeMaps(config1.map, config2.map)) def mergeConfigs(config1: Config, config2: Config): Config = new Config(mergeMaps(config1._map, config2._map))
/** /**
* Search for value in index position in a map * Search for value in index position in a map
......
...@@ -25,17 +25,10 @@ import scala.sys.process.{ Process, ProcessLogger } ...@@ -25,17 +25,10 @@ import scala.sys.process.{ Process, ProcessLogger }
class GsnapTest extends TestNGSuite with Matchers { class GsnapTest extends TestNGSuite with Matchers {
private def setConfig(key: String, value: String): Map[String, Any] = {
val oldMap: Map[String, Any] = Config.global.map
Config.global.map += (key -> value)
oldMap
}
private def restoreConfig(oldMap: Map[String, Any]): Unit = Config.global.map = oldMap
@BeforeClass def checkExecutable() = { @BeforeClass def checkExecutable() = {
val oldMap = setConfig("db", "mock") val wrapper = new Gsnap(null) {
val wrapper = new Gsnap(null) override def globalConfig = new Config(Map("db" -> "mock"))
}
val proc = Process(wrapper.versionCommand) val proc = Process(wrapper.versionCommand)
val exitCode = val exitCode =
try { try {
...@@ -47,13 +40,12 @@ class GsnapTest extends TestNGSuite with Matchers { ...@@ -47,13 +40,12 @@ class GsnapTest extends TestNGSuite with Matchers {
} }
if (exitCode != 0) if (exitCode != 0)
throw new SkipException("Skipping GSNAP test because the executable can not be found") throw new SkipException("Skipping GSNAP test because the executable can not be found")
restoreConfig(oldMap)
} }
@Test(description = "GSNAP version number capture from executable") @Test(description = "GSNAP version number capture from executable")
def testVersion() = { def testVersion() = {
val oldMap = setConfig("db", "mock") new Gsnap(null) {
new Gsnap(null).getVersion should not be "N/A" override def globalConfig = new Config(Map("db" -> "mock"))
restoreConfig(oldMap) }.getVersion should not be "N/A"
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment