Skip to content

Commit

Permalink
Significant improvements to testing
Browse files Browse the repository at this point in the history
Added actual testing
Added test resource INI files
Expanded comments
Auto-generating docs during testing now, for completeness
  • Loading branch information
greg-hellings committed Sep 28, 2017
1 parent dc75e5a commit e0e0bb9
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 14 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
language: java
script:
- mvn test
- mvn javadoc:javadoc
- mvn javadoc:test-javadoc
cache:
directories:
- ~/.m2/repository
70 changes: 69 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,79 @@
<tagNameFormat>@{project.version}</tagNameFormat>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.4</version>
<executions>
<execution>
<id>test-docs</id>
<phase>test</phase>
<goals>
<goal>javadoc</goal>
<goal>test-javadoc</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.9</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
<goal>check</goal>
</goals>
<configuration>
<!-- Sets the path to the file which contains the execution data. -->
<dataFile>target/jacoco.exec</dataFile>
<!-- Sets the output directory for the code coverage report. -->
<outputDirectory>target/jacoco-ut</outputDirectory>
<rules>
<rule implementation="org.jacoco.maven.RuleConfiguration">
<element>BUNDLE</element>
<excludes>
<exclude>*Test</exclude>
</excludes>
<limits>
<limit implementation="org.jacoco.report.check.Limit">
<counter>INSTRUCTION</counter>
<value>COVEREDRATIO</value>
<minimum>0.80</minimum>
</limit>
<limit implementation="org.jacoco.report.check.Limit">
<counter>CLASS</counter>
<value>MISSEDCOUNT</value>
<maximum>0</maximum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
<configuration>
<systemPropertyVariables>
<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
Expand Down
53 changes: 42 additions & 11 deletions src/main/java/com/thehellings/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,22 @@
/**
* Wraps access to the configuration ini file to allow defaults to be processed.
* <p>
* Pass the constructor the name of the environment/section that is used. It will handle reding the values from the
* Pass the constructor the name of the environment/section that is used. It will handle reading the values from the
* named section in an ini file. If no such value is found in that section, it will test for a section named
* "defaults" and look there for the value instead.
* </p>
* <p>
* If there is no section called "defaults", then the fallback functionality is ignored, and only values specified
* in the configured section are read. This allows an entire system to be configured properly once, and only values
* that change between environments - such as database names and credentials - need to be overridden in the per
* environment section.
* </p>
* <p>
* Reading down through the series of sections is done automatically. Currently there is no process to specify a
* multiple level of hierarchy, nor is there a way to prevent a value from being read out of the default section if
* no such value is found in the environment section. Perhaps future enhancements will support that, but at present
* the author has no use for those functions in his own work.
* </p>
*/
public class Config {
private Ini ini;
Expand All @@ -25,31 +37,49 @@ public class Config {

/**
* Reads the value of {@link Enum#name()} as the name for the config section
* @param e
* @throws Exception
*
* <p>
* Passes through name to {@link #Config(String)}
* </p>
*
* @param e The name of the environment
* @see #Config(String)
*/
public Config(final Enum e) throws Exception {
public Config(final Enum e) {
this(e.name());
}

public Config(final String environment) {
/**
* String version of {@link #Config(Enum)}
*
* <p>
* Defaults the source to be read as a file at path "/config.ini" from the
* class's bundled resources
* </p>
*
* @param environment Name of the environment
* @see #Config(Enum)
*/
public Config(final String environment) {
this(environment, Config.class.getResourceAsStream("/config.ini"));
}

/**
* Reads the value of {@link Enum#name()} as the name for the config section
*
* @param e
* @param stream
* @param e Name of the environment
* @param stream Place from whence to read the INI file
* @see #Config(String, InputStream)
*/
public Config(final Enum e, InputStream stream) {
this(e.name(), stream);
}

/**
* Reads the specified environment, with fallback, from the specified INI file
*
* @param environment
* @param stream
* @param environment Name of the environment
* @param stream Place from whence to read the INI file
*/
public Config(final String environment, InputStream stream) {
try {
Expand Down Expand Up @@ -86,8 +116,9 @@ public String get(final String name) {
/**
* Reads the value of {@link Enum#name()} as the field name
*
* @param e
* @return
* @param e The name of the field to read
* @return The string value of the variable from the configured section
* @see #get(String)
*/
public String get(final Enum e) {
return this.get(e.name());
Expand Down
60 changes: 60 additions & 0 deletions src/test/java/com/thehellings/config/ConfigTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.thehellings.config;

import org.junit.Test;

import static org.junit.Assert.*;

public class ConfigTest {
enum TestEnums {
field1,
field2,
field3,
not_present
};

@Test
public void developmentEnumeratorConstructor() {
Config config = new Config(Mode.DEVELOPMENT);
assertEquals("dev1", config.get("field1"));
assertEquals("dev2", config.get("field2"));
assertEquals("default3", config.get(TestEnums.field3));
assertNull(config.get(TestEnums.not_present));
}

@Test
public void productionStringConstructor() {
Config config = new Config("PRODUCTION");
assertEquals("prod1", config.get(TestEnums.field1));
assertNull(config.get(TestEnums.field2));
assertEquals("prod3", config.get("field3"));
assertNull(config.get(TestEnums.not_present));
}

@Test
public void nonDefaultFilename() {
Config config = new Config("DEVELOPMENT", ConfigTest.class.getClassLoader().getResourceAsStream("other.ini"));
assertEquals("dev1", config.get("field1"));
assertEquals("dev2", config.get("field2"));
assertNull(config.get("field3"));
}

@Test
public void nonDefaultFilenameEnum() throws Exception {
Config config = new Config(Mode.PRODUCTION, ConfigTest.class.getClassLoader().getResourceAsStream("other.ini"));
assertEquals("prod1", config.get("field1"));
assertNull(config.get("field2"));
assertEquals("prod3", config.get("field3"));
}

@Test
public void defaultsOnly() {
Config config = new Config("derpenv", ConfigTest.class.getClassLoader().getResourceAsStream("defaultsonly.ini"));
assertEquals("somevalue", config.get("field1"));
}

@Test
public void failOpen() {
Config config = new Config("anyenv", null);
assertNull(config.get("anything"));
}
}
11 changes: 11 additions & 0 deletions src/test/resources/config.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[DEVELOPMENT]
field1 = dev1
field2 = dev2

[PRODUCTION]
field1 = prod1
field3 = prod3

[default]
field1 = default1
field3 = default3
2 changes: 2 additions & 0 deletions src/test/resources/defaultsonly.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[default]
field1 = somevalue
7 changes: 7 additions & 0 deletions src/test/resources/other.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[DEVELOPMENT]
field1 = dev1
field2 = dev2

[PRODUCTION]
field1 = prod1
field3 = prod3

0 comments on commit e0e0bb9

Please sign in to comment.