From 9b91adb936254e3c24e4a26a1f9193e73ae13be1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 6 May 2015 06:26:30 +0200 Subject: [PATCH] Set default values differently --- .../net/pterodactylus/rhynodge/engine/Options.java | 12 ++++++------ .../java/net/pterodactylus/util/envopt/Option.java | 1 + .../java/net/pterodactylus/util/envopt/Parser.java | 2 +- .../net/pterodactylus/util/envopt/ParserTest.java | 19 +++++++++++++++++++ 4 files changed, 27 insertions(+), 7 deletions(-) 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; + + } + } -- 2.7.4