diff --git a/config/build.sbt b/config/build.sbt index 7f0677400..7d3fbe218 100644 --- a/config/build.sbt +++ b/config/build.sbt @@ -18,6 +18,9 @@ fork in Test := true fork in run := true fork in run in Test := true +//env vars for tests +envVars in Test ++= Map("testList.0" -> "0", "testList.1" -> "1") + autoScalaLibrary := false crossPaths := false diff --git a/config/src/main/java/com/typesafe/config/ConfigFactory.java b/config/src/main/java/com/typesafe/config/ConfigFactory.java index 7538817d3..afc06d498 100644 --- a/config/src/main/java/com/typesafe/config/ConfigFactory.java +++ b/config/src/main/java/com/typesafe/config/ConfigFactory.java @@ -496,6 +496,7 @@ public static void invalidateCaches() { // We rely on this having the side effect that it drops // all caches ConfigImpl.reloadSystemPropertiesConfig(); + ConfigImpl.cc(); } /** diff --git a/config/src/main/java/com/typesafe/config/impl/ConfigImpl.java b/config/src/main/java/com/typesafe/config/impl/ConfigImpl.java index e441204ac..080ba1f7f 100644 --- a/config/src/main/java/com/typesafe/config/impl/ConfigImpl.java +++ b/config/src/main/java/com/typesafe/config/impl/ConfigImpl.java @@ -78,7 +78,7 @@ synchronized Config getOrElseUpdate(ClassLoader loader, String key, Callable env = System.getenv(); - Map m = new HashMap(); - for (Map.Entry entry : env.entrySet()) { - String key = entry.getKey(); - m.put(key, - new ConfigString.Quoted(SimpleConfigOrigin.newSimple("env var " + key), entry - .getValue())); - } - return new SimpleConfigObject(SimpleConfigOrigin.newSimple("env variables"), - m, ResolveStatus.RESOLVED, false /* ignoresFallbacks */); + return PropertiesParser.fromStringMap(newSimpleOrigin("env variables"), System.getenv()); } private static class EnvVariablesHolder { diff --git a/config/src/main/java/com/typesafe/config/impl/PropertiesParser.java b/config/src/main/java/com/typesafe/config/impl/PropertiesParser.java index 5bc630da2..ee26a393a 100644 --- a/config/src/main/java/com/typesafe/config/impl/PropertiesParser.java +++ b/config/src/main/java/com/typesafe/config/impl/PropertiesParser.java @@ -56,15 +56,28 @@ static Path pathFromPropertyKey(String key) { static AbstractConfigObject fromProperties(ConfigOrigin origin, Properties props) { + return fromEntrySet(origin, props.entrySet()); + } + + private static AbstractConfigObject fromEntrySet(ConfigOrigin origin, Set> entries) { + final Map pathMap = getPathMap(entries); + return fromPathMap(origin, pathMap, true /* from properties */); + } + + private static Map getPathMap(Set> entries) { Map pathMap = new HashMap(); - for (Map.Entry entry : props.entrySet()) { + for (Map.Entry entry : entries) { Object key = entry.getKey(); if (key instanceof String) { Path path = pathFromPropertyKey((String) key); pathMap.put(path, entry.getValue()); } } - return fromPathMap(origin, pathMap, true /* from properties */); + return pathMap; + } + + static AbstractConfigObject fromStringMap(ConfigOrigin origin, Map stringMap) { + return fromEntrySet(origin, stringMap.entrySet()); } static AbstractConfigObject fromPathMap(ConfigOrigin origin, diff --git a/config/src/test/scala/com/typesafe/config/impl/ConfigSubstitutionTest.scala b/config/src/test/scala/com/typesafe/config/impl/ConfigSubstitutionTest.scala index 7cc79e59d..712e1c438 100644 --- a/config/src/test/scala/com/typesafe/config/impl/ConfigSubstitutionTest.scala +++ b/config/src/test/scala/com/typesafe/config/impl/ConfigSubstitutionTest.scala @@ -10,6 +10,7 @@ import com.typesafe.config.ConfigException import com.typesafe.config.ConfigResolveOptions import com.typesafe.config.Config import com.typesafe.config.ConfigFactory +import scala.collection.JavaConverters._ class ConfigSubstitutionTest extends TestUtils { @@ -723,6 +724,35 @@ class ConfigSubstitutionTest extends TestUtils { checkNotSerializable(substComplexObject) } + @Test + def resolveListFromSystemProps() { + val props = parseObject( + """ + |"a": ${testList} + """.stripMargin) + + System.setProperty("testList.0", "0") + System.setProperty("testList.1", "1") + ConfigImpl.reloadSystemPropertiesConfig() + + val resolved = resolve(ConfigFactory.systemProperties().withFallback(props).root.asInstanceOf[AbstractConfigObject]) + + assertEquals(List("0", "1"), resolved.getList("a").unwrapped().asScala) + } + + @Test + def resolveListFromEnvVars() { + val props = parseObject( + """ + |"a": ${testList} + """.stripMargin) + + //"testList.0" and "testList.1" are defined as envVars in build.sbt + val resolved = resolve(props) + + assertEquals(List("0", "1"), resolved.getList("a").unwrapped().asScala) + } + // this is a weird test, it used to test fallback to system props which made more sense. // Now it just tests that if you override with system props, you can use system props // in substitutions. diff --git a/config/src/test/scala/com/typesafe/config/impl/PublicApiTest.scala b/config/src/test/scala/com/typesafe/config/impl/PublicApiTest.scala index 70870c663..debd3cd21 100644 --- a/config/src/test/scala/com/typesafe/config/impl/PublicApiTest.scala +++ b/config/src/test/scala/com/typesafe/config/impl/PublicApiTest.scala @@ -1016,8 +1016,8 @@ class PublicApiTest extends TestUtils { assertTrue("invalidate caches works on changed system props sys", sys2 ne sys3) assertTrue("invalidate caches works on changed system props conf", conf2 ne conf3) - assertTrue("invalidate caches doesn't change value if no system prop changes sys", sys1 == sys2) - assertTrue("invalidate caches doesn't change value if no system prop changes conf", conf1 == conf2) + assertEquals("invalidate caches doesn't change value if no system prop changes sys", sys1, sys2) + assertEquals("invalidate caches doesn't change value if no system prop changes conf", conf1, conf2) assertTrue("test system property is set sys", sys3.hasPath("invalidateCachesTest")) assertTrue("test system property is set conf", conf3.hasPath("invalidateCachesTest"))