From 37def774504467df8b5d61227ff1fe7894595e21 Mon Sep 17 00:00:00 2001 From: Peter van 't Hof <p.j.van_t_hof@lumc.nl> Date: Fri, 4 Sep 2015 21:15:39 +0200 Subject: [PATCH] Make config map immutable from outside the class --- .../lumc/sasc/biopet/core/config/Config.scala | 24 ++++++++++--------- .../sasc/biopet/extensions/GsnapTest.scala | 20 +++++----------- 2 files changed, 19 insertions(+), 25 deletions(-) diff --git a/public/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/core/config/Config.scala b/public/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/core/config/Config.scala index c477a4aaa..d9f0f650e 100644 --- a/public/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/core/config/Config.scala +++ b/public/biopet-framework/src/main/scala/nl/lumc/sasc/biopet/core/config/Config.scala @@ -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 diff --git a/public/biopet-framework/src/test/scala/nl/lumc/sasc/biopet/extensions/GsnapTest.scala b/public/biopet-framework/src/test/scala/nl/lumc/sasc/biopet/extensions/GsnapTest.scala index 1ffd257e0..d155e9744 100644 --- a/public/biopet-framework/src/test/scala/nl/lumc/sasc/biopet/extensions/GsnapTest.scala +++ b/public/biopet-framework/src/test/scala/nl/lumc/sasc/biopet/extensions/GsnapTest.scala @@ -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" } } -- GitLab