Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helper methods to the TypesafeConfigReader. #440

Open
wants to merge 4 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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"));
}
}