Skip to content

Commit

Permalink
Move to typesafe config (#26)
Browse files Browse the repository at this point in the history
* move to typesafe config
  • Loading branch information
iantmoore committed Dec 19, 2016
1 parent 0e1776d commit 3243645
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 118 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Requirements
* Fixed Issue #24 - ExecutionResultsCollector didn't work with forked mode
* Added 3rd colour to usage report pie charts to show not run state
* Corrected the path to the screenshots in the report data and the final report
* Replaced Apache config with [Typesafe Config](https://github.com/typesafehub/config) - similar functionality but provides better nesting of properties, variable substitution

1.0.3
-----
Expand Down
6 changes: 4 additions & 2 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
<name>SubSteps API</name>

<dependencies>

<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<groupId>com.typesafe</groupId>
<artifactId>config</artifactId>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,12 @@

package com.technophobia.substeps.model;

import com.technophobia.substeps.model.exception.SubstepsConfigurationException;
import org.apache.commons.configuration.CombinedConfiguration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.tree.OverrideCombiner;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URL;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
* @author ian
Expand All @@ -41,31 +35,12 @@ public enum Configuration {

private static final Logger logger = LoggerFactory.getLogger(Configuration.class);

private final CombinedConfiguration combinedConfig = new CombinedConfiguration(
new OverrideCombiner());
private final Config config;

private Configuration() {
initialise();
}

private void initialise() {

final String resourceBundleName = resourceBundleName();
config = ConfigFactory.load(resourceBundleName);

final URL customPropsUrl = Configuration.class.getResource(resourceBundleName);

if (customPropsUrl != null) {

try {
final PropertiesConfiguration customProps = new PropertiesConfiguration(
customPropsUrl);
combinedConfig.addConfiguration(customProps, "customProps");

} catch (final ConfigurationException e) {
logger.error("error loading custom properties", e);

}
}
}


Expand All @@ -76,77 +51,39 @@ private void initialise() {
* @param url to a properties file containing default values
* @param name to name of the properties file that is being added
*/
@Deprecated
public void addDefaultProperties(final URL url, final String name) {

if (url != null) {
try {

final PropertiesConfiguration defaultProps = new PropertiesConfiguration(url);

combinedConfig.addConfiguration(defaultProps, name);
} catch (final ConfigurationException e) {
logger.error("error loading default properties", e);
throw new SubstepsConfigurationException(e);
}
}
throw new IllegalArgumentException("method no longer supported, rename default substep library properties to reference.conf and they will be loaded by Typesafe config");
}


public void addDefaultProperty(final String key, final Object value) {
combinedConfig.addProperty(key, value);
}


public String getConfigurationInfo() {

final List<String> configurationNameList = combinedConfig.getConfigurationNameList();

final StringBuilder buf = new StringBuilder();

for (final String configurationName : configurationNameList) {

buf.append("In config: ").append(configurationName).append("\n");

final org.apache.commons.configuration.Configuration cfg = combinedConfig
.getConfiguration(configurationName);

final Iterator<String> keys = cfg.getKeys();

while (keys.hasNext()) {
final String key = keys.next();

final String val = cfg.getString(key);

buf.append("key: ").append(key).append("\tval: [").append(val).append("]\n");
}

buf.append("\n");
}
return buf.toString();
return config.root().render();
}


private static String resourceBundleName() {
return "/" + System.getProperty("environment", "localhost") + ".properties";
return System.getProperty("environment", "localhost") + ".properties";
}


public String getString(final String key) {
return combinedConfig.getString(key);
return config.getString(key);
}


public int getInt(final String key) {
return combinedConfig.getInt(key);
return config.getInt(key);
}


public long getLong(final String key) {
return combinedConfig.getLong(key);
return config.getLong(key);
}


public boolean getBoolean(final String key) {
return combinedConfig.getBoolean(key);
return config.getBoolean(key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,6 @@ public enum CoreSubstepsPropertiesConfiguration implements CoreSubstepsConfigura

private CoreSubstepsPropertiesConfiguration() {

final URL defaultURL = getClass().getResource("/default-core-substeps.properties");

if (defaultURL == null){
throw new IllegalStateException("Unable to find default core properties");
}

Configuration.INSTANCE.addDefaultProperties(defaultURL, "default-core-substeps");

stepDepthForDescription = Configuration.INSTANCE.getInt("step.depth.description");

logUncalledAndUnusedStepImpls = Configuration.INSTANCE.getBoolean("log.unused.uncalled");
Expand All @@ -57,7 +49,6 @@ private CoreSubstepsPropertiesConfiguration() {

reportDataBaseDir = Configuration.INSTANCE.getString("report.data.base.dir");


LOG.info("Using core properties:\n" + Configuration.INSTANCE.getConfigurationInfo());
}

Expand Down
12 changes: 12 additions & 0 deletions api/src/main/resources/reference.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

# depth limiter for the creation of step description, beyond 6 deep no descriptions
step.depth.description=6

# flag tp control whether unused calls are logged out, generally true for CI, complete executions, false for Intellij, junit tests etc
log.unused.uncalled=false

# flag to enable pretty printing of the json report data
report.data.pretty.print=false

# the base directory under which report data will be written
report.data.base.dir=target
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package com.technophobia.substeps.model;

import com.typesafe.config.ConfigException;
import org.junit.Assert;
import org.junit.Test;

Expand All @@ -38,8 +39,8 @@ public void testConfig() {
// set the custom props, then override with defaults
System.setProperty("environment", "custom");

final URL defaultPropsUrl = ConfigurationTest.class.getResource("/default.properties");
Configuration.INSTANCE.addDefaultProperties(defaultPropsUrl, "default");
// final URL defaultPropsUrl = ConfigurationTest.class.getResource("/default.properties");
// Configuration.INSTANCE.addDefaultProperties(defaultPropsUrl, "default");

// overridden
Assert.assertThat(Configuration.INSTANCE.getString("overridden.key"), is("overridden"));
Expand All @@ -50,7 +51,15 @@ public void testConfig() {
// custom
Assert.assertThat(Configuration.INSTANCE.getString("custom.key"), is("custom-key"));



}

@Test(expected = ConfigException.Missing.class)
public void negativeTest(){
System.setProperty("environment", "custom");
Assert.assertNull(Configuration.INSTANCE.getString("non-existant"));

}

}
2 changes: 2 additions & 0 deletions api/src/test/resources/reference.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
overridden.key=default-overridden
default.key=default-key
12 changes: 6 additions & 6 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@
<groupId>velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.5</version>
<exclusions>
<exclusion>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
</exclusion>
</exclusions>
<!--<exclusions>-->
<!--<exclusion>-->
<!--<groupId>commons-lang</groupId>-->
<!--<artifactId>commons-lang</artifactId>-->
<!--</exclusion>-->
<!--</exclusions>-->
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
package com.technophobia.substeps.model;

import com.google.common.base.Strings;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.Map.Entry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package com.technophobia.substeps.glossary;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang3.StringEscapeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -67,12 +67,12 @@ private void buildStepTagRows(final StringBuilder buf, final Collection<StepDesc
for (final StepDescriptor info : infos) {

log.debug("info non escaped: " + info.getExpression() + "\n\tescaped:\n"
+ StringEscapeUtils.escapeHtml(info.getExpression()));
+ StringEscapeUtils.escapeHtml4(info.getExpression()));

buf.append(
String.format(TABLE_ROW_FORMAT,
StringEscapeUtils.escapeHtml(info.getExpression()), info.getExample(),
StringEscapeUtils.escapeHtml(info.getDescription()))).append("\n");
StringEscapeUtils.escapeHtml4(info.getExpression()), info.getExample(),
StringEscapeUtils.escapeHtml4(info.getDescription()))).append("\n");

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package com.technophobia.substeps.glossary;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang3.StringEscapeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -65,13 +65,14 @@ private void buildStepTagRows(final StringBuilder buf, final Collection<StepDesc
for (final StepDescriptor info : infos) {

log.debug("info non escaped: " + info.getExpression() + "\n\tescaped:\n"
+ StringEscapeUtils.escapeHtml(info.getExpression()));
+ StringEscapeUtils.escapeHtml4(info.getExpression()));



buf.append(
String.format(TABLE_ROW_FORMAT,
StringEscapeUtils.escapeHtml(info.getExpression()), info.getExample().replaceAll("\n", " "),
StringEscapeUtils.escapeHtml(info.getDescription()).replaceAll("\n", " "))).append("\n");
StringEscapeUtils.escapeHtml4(info.getExpression()), info.getExample().replaceAll("\n", " "),
StringEscapeUtils.escapeHtml4(info.getDescription()).replaceAll("\n", " "))).append("\n");

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import com.technophobia.substeps.runner.FeatureFileComparator;
import com.technophobia.substeps.runner.FeatureFileParser;
import com.technophobia.substeps.runner.syntax.FileUtils;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang3.StringEscapeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -118,7 +118,7 @@ private void toHTML(final Scenario scenario, final StringBuilder buf) {
buf.append("<div style=\"position:relative;left:5em\">");
for (final Step step : scenario.getSteps()) {

buf.append(StringEscapeUtils.escapeHtml(step.getLine())).append("<br/>").append("\n");
buf.append(StringEscapeUtils.escapeHtml4(step.getLine())).append("<br/>").append("\n");
}
buf.append("</div>");
buf.append("<br/><br/>\n");
Expand Down
16 changes: 3 additions & 13 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -201,19 +201,9 @@
</dependency>

<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>1.10</version>

<!--
<exclusions>
<exclusion>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
</exclusion>
</exclusions>
-->

<groupId>com.typesafe</groupId>
<artifactId>config</artifactId>
<version>1.3.1</version>
</dependency>

<dependency>
Expand Down

0 comments on commit 3243645

Please sign in to comment.