Skip to content

Commit

Permalink
Merge pull request #421 from twicksell/testConfigRulePolish
Browse files Browse the repository at this point in the history
Test config rule polish
  • Loading branch information
twicksell committed Jun 6, 2016
2 parents b7322b1 + 7184dcd commit a4345d2
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 96 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.netflix.archaius.test;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Iterator;
Expand All @@ -16,7 +15,8 @@
import com.netflix.archaius.api.ConfigListener;
import com.netflix.archaius.api.Decoder;
import com.netflix.archaius.api.StrInterpolator;
import com.netflix.archaius.config.MapConfig;
import com.netflix.archaius.api.config.SettableConfig;
import com.netflix.archaius.config.DefaultSettableConfig;

/***
* JUnit rule which builds an Archaius2 {@link Config} instance using
Expand All @@ -35,255 +35,238 @@
*
* See {@link TestPropertyOverride} for additional usage information.
*/
public class Archaius2TestConfig implements TestRule, Config {
private final ThreadLocal<Config> configHolder = new ThreadLocal<>();
public class Archaius2TestConfig implements TestRule, Config, SettableConfig {
private TestCompositeConfig testCompositeConfig;
private final TestPropertyOverrideAnnotationReader annotationReader = new TestPropertyOverrideAnnotationReader();

public Statement apply(Statement base, Description description) {
return statement(base, description);
}

private Statement statement(final Statement base, final Description description) {
public Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
try {
createConfig(description);
base.evaluate();
} finally {
cleanupConfig(description);
}
}
SettableConfig classLevelProperties = new DefaultSettableConfig();
SettableConfig methodLevelProperties = new DefaultSettableConfig();
for(Class<?> parentClass : ClassUtils.getAllSuperclasses(description.getTestClass())) {
classLevelProperties.setProperties(annotationReader.getPropertiesForAnnotation(parentClass.getAnnotation(TestPropertyOverride.class)));
}
classLevelProperties.setProperties(annotationReader.getPropertiesForAnnotation(description.getTestClass().getAnnotation(TestPropertyOverride.class)));
methodLevelProperties.setProperties(annotationReader.getPropertiesForAnnotation(description.getAnnotation(TestPropertyOverride.class)));
testCompositeConfig = new TestCompositeConfig(classLevelProperties, methodLevelProperties);
base.evaluate();
}
};
}

private void createConfig(Description description) {
Properties props = new Properties();
for(Class<?> parentClass : ClassUtils.getAllSuperclasses(description.getTestClass()))
{
addPropertiesUsingAnnotation(parentClass.getAnnotation(TestPropertyOverride.class), props);
}
addPropertiesUsingAnnotation(description.getTestClass().getAnnotation(TestPropertyOverride.class), props);
addPropertiesUsingAnnotation(description.getAnnotation(TestPropertyOverride.class), props);
configHolder.set(new MapConfig(props));
}

private void addPropertiesUsingAnnotation(TestPropertyOverride annotation, Properties properties) {
if (annotation == null) {
return;
}
for (String fileName : annotation.propertyFiles()) {
try {
properties.load(this.getClass().getClassLoader().getResourceAsStream(fileName));
} catch (IOException e) {
throw new TestConfigException("Failed to load property file from classpath", e);
}
}
for (String override : annotation.value()) {
String[] parts = override.split("=", 2);
if (parts.length < 2) {
throw new TestConfigException("Error parsing TestPropertyOverride for: " + annotation.value()
+ " Please ensure you are specifying overrides in the form \"key=value\"");
}
properties.put(parts[0], parts[1]);
}
}

private void cleanupConfig(Description description) {
configHolder.remove();
}
}

private String getKey(Description description) {
return description.getClassName() + description.getMethodName() + description.getDisplayName();
}

@Override
public void addListener(ConfigListener listener) {
configHolder.get().addListener(listener);
testCompositeConfig.addListener(listener);
}

@Override
public void removeListener(ConfigListener listener) {
configHolder.get().removeListener(listener);
testCompositeConfig.removeListener(listener);
}

@Override
public Object getRawProperty(String key) {
return configHolder.get().getRawProperty(key);
return testCompositeConfig.getRawProperty(key);
}

@Override
public Long getLong(String key) {
return configHolder.get().getLong(key);
return testCompositeConfig.getLong(key);
}

@Override
public Long getLong(String key, Long defaultValue) {
return configHolder.get().getLong(key, defaultValue);
return testCompositeConfig.getLong(key, defaultValue);
}

@Override
public String getString(String key) {
return configHolder.get().getString(key);
return testCompositeConfig.getString(key);
}

@Override
public String getString(String key, String defaultValue) {
return configHolder.get().getString(key, defaultValue);
return testCompositeConfig.getString(key, defaultValue);
}

@Override
public Double getDouble(String key) {
return configHolder.get().getDouble(key);
return testCompositeConfig.getDouble(key);
}

@Override
public Double getDouble(String key, Double defaultValue) {
return configHolder.get().getDouble(key, defaultValue);
return testCompositeConfig.getDouble(key, defaultValue);
}

@Override
public Integer getInteger(String key) {
return configHolder.get().getInteger(key);
return testCompositeConfig.getInteger(key);
}

@Override
public Integer getInteger(String key, Integer defaultValue) {
return configHolder.get().getInteger(key, defaultValue);
return testCompositeConfig.getInteger(key, defaultValue);
}

@Override
public Boolean getBoolean(String key) {
return configHolder.get().getBoolean(key);
return testCompositeConfig.getBoolean(key);
}

@Override
public Boolean getBoolean(String key, Boolean defaultValue) {
return configHolder.get().getBoolean(key, defaultValue);
return testCompositeConfig.getBoolean(key, defaultValue);
}

@Override
public Short getShort(String key) {
return configHolder.get().getShort(key);
return testCompositeConfig.getShort(key);
}

@Override
public Short getShort(String key, Short defaultValue) {
return configHolder.get().getShort(key, defaultValue);
return testCompositeConfig.getShort(key, defaultValue);
}

@Override
public BigInteger getBigInteger(String key) {
return configHolder.get().getBigInteger(key);
return testCompositeConfig.getBigInteger(key);
}

@Override
public BigInteger getBigInteger(String key, BigInteger defaultValue) {
return configHolder.get().getBigInteger(key, defaultValue);
return testCompositeConfig.getBigInteger(key, defaultValue);
}

@Override
public BigDecimal getBigDecimal(String key) {
return configHolder.get().getBigDecimal(key);
return testCompositeConfig.getBigDecimal(key);
}

@Override
public BigDecimal getBigDecimal(String key, BigDecimal defaultValue) {
return configHolder.get().getBigDecimal(key, defaultValue);
return testCompositeConfig.getBigDecimal(key, defaultValue);
}

@Override
public Float getFloat(String key) {
return configHolder.get().getFloat(key);
return testCompositeConfig.getFloat(key);
}

@Override
public Float getFloat(String key, Float defaultValue) {
return configHolder.get().getFloat(key, defaultValue);
return testCompositeConfig.getFloat(key, defaultValue);
}

@Override
public Byte getByte(String key) {
return configHolder.get().getByte(key);
return testCompositeConfig.getByte(key);
}

@Override
public Byte getByte(String key, Byte defaultValue) {
return configHolder.get().getByte(key, defaultValue);
return testCompositeConfig.getByte(key, defaultValue);
}

@Override
public List<?> getList(String key) {
return configHolder.get().getList(key);
return testCompositeConfig.getList(key);
}

@Override
public <T> List<T> getList(String key, Class<T> type) {
return configHolder.get().getList(key, type);
return testCompositeConfig.getList(key, type);
}

@Override
public List<?> getList(String key, List<?> defaultValue) {
return configHolder.get().getList(key, defaultValue);
return testCompositeConfig.getList(key, defaultValue);
}

@Override
public <T> T get(Class<T> type, String key) {
return configHolder.get().get(type, key);
return testCompositeConfig.get(type, key);
}

@Override
public <T> T get(Class<T> type, String key, T defaultValue) {
return configHolder.get().get(type, key, defaultValue);
return testCompositeConfig.get(type, key, defaultValue);
}

@Override
public boolean containsKey(String key) {
return configHolder.get().containsKey(key);
return testCompositeConfig.containsKey(key);
}

@Override
public boolean isEmpty() {
return configHolder.get().isEmpty();
return testCompositeConfig.isEmpty();
}

@Override
public Iterator<String> getKeys() {
return configHolder.get().getKeys();
return testCompositeConfig.getKeys();
}

@Override
public Iterator<String> getKeys(String prefix) {
return configHolder.get().getKeys(prefix);
return testCompositeConfig.getKeys(prefix);
}

@Override
public Config getPrefixedView(String prefix) {
return configHolder.get().getPrefixedView(prefix);
return testCompositeConfig.getPrefixedView(prefix);
}

@Override
public void setStrInterpolator(StrInterpolator interpolator) {
configHolder.get().setStrInterpolator(interpolator);
testCompositeConfig.setStrInterpolator(interpolator);
}

@Override
public StrInterpolator getStrInterpolator() {
return configHolder.get().getStrInterpolator();
return testCompositeConfig.getStrInterpolator();
}

@Override
public void setDecoder(Decoder decoder) {
configHolder.get().setDecoder(decoder);
testCompositeConfig.setDecoder(decoder);
}

@Override
public Decoder getDecoder() {
return configHolder.get().getDecoder();
return testCompositeConfig.getDecoder();
}

@Override
public <T> T accept(Visitor<T> visitor) {
return configHolder.get().accept(visitor);
return testCompositeConfig.accept(visitor);
}

@Override
public void setProperties(Config config) {
testCompositeConfig.setProperties(config);
}

@Override
public void setProperties(Properties properties) {
testCompositeConfig.setProperties(properties);
}

@Override
public <T> void setProperty(String propName, T propValue) {
testCompositeConfig.setProperty(propName, propValue);
}

@Override
public void clearProperty(String propName) {
testCompositeConfig.clearProperty(propName);
}
}
Loading

0 comments on commit a4345d2

Please sign in to comment.