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._
/**
* 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
*/
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 {
logger.debug("Init phase of config")
......@@ -36,6 +36,8 @@ class Config(var map: Map[String, Any],
loadDefaultConfig()
}
def map = _map
/**
* Loading a environmental variable as location of config files to merge into the config
* @param valueName Name of value
......@@ -71,9 +73,9 @@ class Config(var map: Map[String, Any],
else defaults = mergeMaps(configMap, defaults)
logger.debug("New defaults: " + defaults)
} else {
if (map.isEmpty) map = configMap
else map = mergeMaps(configMap, map)
logger.debug("New config: " + map)
if (_map.isEmpty) _map = configMap
else _map = mergeMaps(configMap, _map)
logger.debug("New config: " + _map)
}
}
......@@ -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 = {
val valueMap = path.foldRight(Map(key -> value))((a, b) => Map(a -> b))
if (default) defaults = mergeMaps(valueMap, defaults)
else map = mergeMaps(valueMap, map)
else _map = mergeMaps(valueMap, _map)
}
protected[config] var notFoundCache: List[ConfigValueIndex] = List()
......@@ -106,7 +108,7 @@ class Config(var map: Map[String, Any],
* @param s key
* @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
......@@ -131,7 +133,7 @@ class Config(var map: Map[String, Any],
fixedCache += (requestedIndex -> fixedValue.get)
true
} else {
val value = Config.getValueFromMap(map, requestedIndex)
val value = Config.getValueFromMap(_map, requestedIndex)
if (value.isDefined && value.get.value != None) {
foundCache += (requestedIndex -> value.get)
true
......@@ -215,7 +217,7 @@ class Config(var map: Map[String, Any],
val fullEffective = ConfigUtils.mergeMaps(effectiveFound, effectiveDefaultFound)
val fullEffectiveWithNotFound = ConfigUtils.mergeMaps(fullEffective, notFound)
writeMapToJsonFile(this.map, "input")
writeMapToJsonFile(this._map, "input")
writeMapToJsonFile(found, "found")
writeMapToJsonFile(effectiveFound, "effective.found")
writeMapToJsonFile(effectiveDefaultFound, "effective.defaults")
......@@ -224,7 +226,7 @@ class Config(var map: Map[String, Any],
writeMapToJsonFile(fullEffectiveWithNotFound, "effective.full.notfound")
}
override def toString: String = map.toString()
override def toString: String = _map.toString()
}
object Config extends Logging {
......@@ -236,7 +238,7 @@ object Config extends Logging {
* @param config2 Low prio map
* @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
......
......@@ -25,17 +25,10 @@ import scala.sys.process.{ Process, ProcessLogger }
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() = {
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 exitCode =
try {
......@@ -47,13 +40,12 @@ class GsnapTest extends TestNGSuite with Matchers {
}
if (exitCode != 0)
throw new SkipException("Skipping GSNAP test because the executable can not be found")
restoreConfig(oldMap)
}
@Test(description = "GSNAP version number capture from executable")
def testVersion() = {
val oldMap = setConfig("db", "mock")
new Gsnap(null).getVersion should not be "N/A"
restoreConfig(oldMap)
new Gsnap(null) {
override def globalConfig = new Config(Map("db" -> "mock"))
}.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