diff --git a/archaius2-typesafe/src/main/java/com/netflix/archaius/typesafe/TypesafeConfigReader.java b/archaius2-typesafe/src/main/java/com/netflix/archaius/typesafe/TypesafeConfigReader.java index 42452efee..d472ae740 100644 --- a/archaius2-typesafe/src/main/java/com/netflix/archaius/typesafe/TypesafeConfigReader.java +++ b/archaius2-typesafe/src/main/java/com/netflix/archaius/typesafe/TypesafeConfigReader.java @@ -15,6 +15,8 @@ */ package com.netflix.archaius.typesafe; +import java.io.File; +import java.io.Reader; import java.net.URL; import com.netflix.archaius.api.ConfigReader; @@ -31,12 +33,27 @@ public com.netflix.archaius.api.Config load(ClassLoader loader, String resourceN return new TypesafeConfig(config); } + public com.netflix.archaius.api.Config load(String resourceName) throws ConfigException { + Config config = ConfigFactory.parseResourcesAnySyntax(resourceName); + return new TypesafeConfig(config); + } + @Override public com.netflix.archaius.api.Config load(ClassLoader loader, URL url, StrInterpolator strInterpolator, StrInterpolator.Lookup lookup) throws ConfigException { Config config = ConfigFactory.parseURL(url, ConfigParseOptions.defaults().setClassLoader(loader)); return new TypesafeConfig(config); } + public com.netflix.archaius.api.Config load(File file) throws ConfigException { + Config config = ConfigFactory.parseFile(file); + return new TypesafeConfig(config); + } + + public com.netflix.archaius.api.Config load(Reader reader) throws ConfigException { + Config config = ConfigFactory.parseReader(reader); + return new TypesafeConfig(config); + } + @Override public boolean canLoad(ClassLoader loader, String name) { return true; diff --git a/archaius2-typesafe/src/test/java/com/netflix/archaius/typesafe/TypesafeConfigLoaderTest.java b/archaius2-typesafe/src/test/java/com/netflix/archaius/typesafe/TypesafeConfigLoaderTest.java index 41224757d..50e7051db 100644 --- a/archaius2-typesafe/src/test/java/com/netflix/archaius/typesafe/TypesafeConfigLoaderTest.java +++ b/archaius2-typesafe/src/test/java/com/netflix/archaius/typesafe/TypesafeConfigLoaderTest.java @@ -5,7 +5,7 @@ * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -15,37 +15,67 @@ */ package com.netflix.archaius.typesafe; +import com.netflix.archaius.DefaultConfigLoader; +import com.netflix.archaius.api.Config; +import com.netflix.archaius.api.config.CompositeConfig; +import com.netflix.archaius.api.exceptions.ConfigException; +import com.netflix.archaius.cascade.ConcatCascadeStrategy; import com.netflix.archaius.config.DefaultCompositeConfig; +import com.netflix.archaius.config.MapConfig; +import com.sun.org.apache.bcel.internal.util.ClassLoader; + import org.junit.Assert; import org.junit.Test; -import com.netflix.archaius.DefaultConfigLoader; -import com.netflix.archaius.cascade.ConcatCascadeStrategy; -import com.netflix.archaius.api.config.CompositeConfig; -import com.netflix.archaius.config.MapConfig; -import com.netflix.archaius.api.exceptions.ConfigException; +import java.io.File; +import java.io.InputStreamReader; +import java.io.Reader; +import java.net.URISyntaxException; public class TypesafeConfigLoaderTest { @Test public void test() throws ConfigException { CompositeConfig config = new DefaultCompositeConfig(); config.addConfig("prop", MapConfig.builder() - .put("env", "prod") + .put("env", "prod") .put("region", "us-east") .build()); - + DefaultConfigLoader loader = DefaultConfigLoader.builder() .withConfigReader(new TypesafeConfigReader()) .withStrLookup(config) .build(); - + config.replaceConfig("foo", loader.newLoader() - .withCascadeStrategy(ConcatCascadeStrategy.from("${env}", "${region}")) - .load("foo")); + .withCascadeStrategy(ConcatCascadeStrategy.from("${env}", "${region}")) + .load("foo")); Assert.assertEquals("prod", config.getString("@environment")); Assert.assertEquals("foo-prod", config.getString("foo.prop1")); Assert.assertEquals("foo", config.getString("foo.prop2")); } + @Test + public void testResourceLoad() throws ConfigException { + Config config = new TypesafeConfigReader().load("foo.properties"); + Assert.assertEquals("foo", config.getString("foo.prop2")); + } + + @Test + public void testFileLoad() throws ConfigException { + try { + File file = new File(ClassLoader.getSystemResource("foo.properties").toURI()); + Config config = new TypesafeConfigReader().load(file); + Assert.assertEquals("foo", config.getString("foo.prop2")); + } catch (URISyntaxException e) { + throw new ConfigException("", e); + } + } + + @Test + public void testReaderLoad() throws ConfigException { + Reader reader = new InputStreamReader(ClassLoader.getSystemResourceAsStream("foo.properties")); + Config config = new TypesafeConfigReader().load(reader); + Assert.assertEquals("foo", config.getString("foo.prop2")); + } }