From 5c8d7f8a6c4b1d486256a952d74409ee0761d80b Mon Sep 17 00:00:00 2001 From: Greg Orzell Date: Tue, 2 Feb 2016 12:59:21 -0800 Subject: [PATCH 1/2] Move to the current version of type safe config and make the versioning more flexible for minor releases. --- archaius2-typesafe/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/archaius2-typesafe/build.gradle b/archaius2-typesafe/build.gradle index f0176d0aa..13e264434 100644 --- a/archaius2-typesafe/build.gradle +++ b/archaius2-typesafe/build.gradle @@ -18,7 +18,7 @@ apply plugin: 'java' dependencies { compile project(':archaius2-core') - compile 'com.typesafe:config:1.2.1' + compile 'com.typesafe:config:1.3.+' } eclipse { From 1ee1c219703b5fc1273b0a24d9ee878ce87efc0b Mon Sep 17 00:00:00 2001 From: Greg Orzell Date: Fri, 2 Sep 2016 11:54:08 +0200 Subject: [PATCH 2/2] Add helper methods for loading other types of resources and corresponding tests. --- .../typesafe/TypesafeConfigReader.java | 17 ++++++ .../typesafe/TypesafeConfigLoaderTest.java | 52 +++++++++++++++---- 2 files changed, 58 insertions(+), 11 deletions(-) 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")); + } }