From: David ‘Bombe’ Roden Date: Wed, 6 May 2015 04:26:30 +0000 (+0200) Subject: Set default values differently X-Git-Tag: v2~188 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=9b91adb936254e3c24e4a26a1f9193e73ae13be1;p=rhynodge.git Set default values differently --- diff --git a/src/main/java/net/pterodactylus/rhynodge/engine/Options.java b/src/main/java/net/pterodactylus/rhynodge/engine/Options.java index a9c6e8f..2914aa0 100644 --- a/src/main/java/net/pterodactylus/rhynodge/engine/Options.java +++ b/src/main/java/net/pterodactylus/rhynodge/engine/Options.java @@ -9,8 +9,8 @@ import net.pterodactylus.util.envopt.Option; */ public class Options { - @Option(name = "SMTP_HOSTNAME") - public final String smtpHostname = "localhost"; + @Option(name = "SMTP_HOSTNAME", defaultValue = "localhost") + public final String smtpHostname = null; @Option(name = "ERROR_EMAIL_SENDER", required = true) public final String errorEmailSender = null; @@ -18,10 +18,10 @@ public class Options { @Option(name = "ERROR_EMAIL_RECIPIENT", required = true) public final String errorEmailRecipient = null; - @Option(name = "STATE_DIRECTORY") - public final String stateDirectory = "states"; + @Option(name = "STATE_DIRECTORY", defaultValue = "states") + public final String stateDirectory = null; - @Option(name = "CHAIN_DIRECTORY") - public final String chainDirectory = "chains"; + @Option(name = "CHAIN_DIRECTORY", defaultValue = "chains") + public final String chainDirectory = null; } diff --git a/src/main/java/net/pterodactylus/util/envopt/Option.java b/src/main/java/net/pterodactylus/util/envopt/Option.java index 640d4fa..b6ae380 100644 --- a/src/main/java/net/pterodactylus/util/envopt/Option.java +++ b/src/main/java/net/pterodactylus/util/envopt/Option.java @@ -17,5 +17,6 @@ public @interface Option { String name(); boolean required() default false; + String defaultValue() default ""; } diff --git a/src/main/java/net/pterodactylus/util/envopt/Parser.java b/src/main/java/net/pterodactylus/util/envopt/Parser.java index bc2d986..dc619ea 100644 --- a/src/main/java/net/pterodactylus/util/envopt/Parser.java +++ b/src/main/java/net/pterodactylus/util/envopt/Parser.java @@ -33,7 +33,7 @@ public class Parser { } field.setAccessible(true); try { - field.set(optionsObject, value.orElse(null)); + field.set(optionsObject, value.orElse(option.defaultValue())); } catch (IllegalAccessException iae1) { /* swallow. */ } diff --git a/src/test/java/net/pterodactylus/util/envopt/ParserTest.java b/src/test/java/net/pterodactylus/util/envopt/ParserTest.java index 5a729dc..5014b9d 100644 --- a/src/test/java/net/pterodactylus/util/envopt/ParserTest.java +++ b/src/test/java/net/pterodactylus/util/envopt/ParserTest.java @@ -46,6 +46,13 @@ public class ParserTest { requiredTestOptions.getOptionOne(); } + @Test + public void parserSetsDefaultValueIfValueIsMissing() { + Mockito.when(environment.getValue("foo")).thenReturn(Optional.empty()); + DefaultTestOptions options = parser.parseEnvironment(DefaultTestOptions::new); + MatcherAssert.assertThat(options.optionOne, Matchers.is("something")); + } + /** * Test class with options used by {@link Parser}. * @@ -109,4 +116,16 @@ public class ParserTest { } + /** + * Test class with options used by {@link Parser}. + * + * @author David ‘Bombe’ Roden + */ + private static class DefaultTestOptions { + + @Option(name = "foo", defaultValue = "something") + public final String optionOne = null; + + } + }