diff --git a/.travis.yml b/.travis.yml index 429d4ad..605e455 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,6 @@ language: java script: - mvn test - - mvn javadoc:javadoc - - mvn javadoc:test-javadoc cache: directories: - ~/.m2/repository \ No newline at end of file diff --git a/pom.xml b/pom.xml index bacc982..1f39e8c 100644 --- a/pom.xml +++ b/pom.xml @@ -82,11 +82,79 @@ @{project.version} - org.apache.maven.plugins maven-javadoc-plugin 2.10.4 + + + test-docs + test + + javadoc + test-javadoc + + + + + + org.jacoco + jacoco-maven-plugin + 0.7.9 + + + prepare-agent + + prepare-agent + + + + report + prepare-package + + report + + + + post-unit-test + test + + report + check + + + + target/jacoco.exec + + target/jacoco-ut + + + BUNDLE + + *Test + + + + INSTRUCTION + COVEREDRATIO + 0.80 + + + CLASS + MISSEDCOUNT + 0 + + + + + + + + + + target/jacoco.exec + + diff --git a/src/main/java/com/thehellings/config/Config.java b/src/main/java/com/thehellings/config/Config.java index 12c1e85..3c20e14 100644 --- a/src/main/java/com/thehellings/config/Config.java +++ b/src/main/java/com/thehellings/config/Config.java @@ -10,10 +10,22 @@ /** * Wraps access to the configuration ini file to allow defaults to be processed. *

- * 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. *

+ *

+ * 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. + *

+ *

+ * 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. + *

*/ public class Config { private Ini ini; @@ -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 + * + *

+ * Passes through name to {@link #Config(String)} + *

+ * + * @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)} + * + *

+ * Defaults the source to be read as a file at path "/config.ini" from the + * class's bundled resources + *

+ * + * @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 { @@ -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()); diff --git a/src/test/java/com/thehellings/config/ConfigTest.java b/src/test/java/com/thehellings/config/ConfigTest.java new file mode 100644 index 0000000..24e7cd7 --- /dev/null +++ b/src/test/java/com/thehellings/config/ConfigTest.java @@ -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")); + } +} \ No newline at end of file diff --git a/src/test/resources/config.ini b/src/test/resources/config.ini new file mode 100644 index 0000000..af489fe --- /dev/null +++ b/src/test/resources/config.ini @@ -0,0 +1,11 @@ +[DEVELOPMENT] +field1 = dev1 +field2 = dev2 + +[PRODUCTION] +field1 = prod1 +field3 = prod3 + +[default] +field1 = default1 +field3 = default3 \ No newline at end of file diff --git a/src/test/resources/defaultsonly.ini b/src/test/resources/defaultsonly.ini new file mode 100644 index 0000000..7468c33 --- /dev/null +++ b/src/test/resources/defaultsonly.ini @@ -0,0 +1,2 @@ +[default] +field1 = somevalue \ No newline at end of file diff --git a/src/test/resources/other.ini b/src/test/resources/other.ini new file mode 100644 index 0000000..95318f1 --- /dev/null +++ b/src/test/resources/other.ini @@ -0,0 +1,7 @@ +[DEVELOPMENT] +field1 = dev1 +field2 = dev2 + +[PRODUCTION] +field1 = prod1 +field3 = prod3 \ No newline at end of file