Set default values differently
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 6 May 2015 04:26:30 +0000 (06:26 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 6 May 2015 04:26:30 +0000 (06:26 +0200)
src/main/java/net/pterodactylus/rhynodge/engine/Options.java
src/main/java/net/pterodactylus/util/envopt/Option.java
src/main/java/net/pterodactylus/util/envopt/Parser.java
src/test/java/net/pterodactylus/util/envopt/ParserTest.java

index a9c6e8f..2914aa0 100644 (file)
@@ -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;
 
 }
index 640d4fa..b6ae380 100644 (file)
@@ -17,5 +17,6 @@ public @interface Option {
 
        String name();
        boolean required() default false;
+       String defaultValue() default "";
 
 }
index bc2d986..dc619ea 100644 (file)
@@ -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. */
                                }
index 5a729dc..5014b9d 100644 (file)
@@ -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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+        */
+       private static class DefaultTestOptions {
+
+               @Option(name = "foo", defaultValue = "something")
+               public final String optionOne = null;
+
+       }
+
 }