Add method to get a string with default value.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 4 Nov 2013 20:47:10 +0000 (21:47 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 28 Feb 2014 21:25:52 +0000 (22:25 +0100)
src/main/java/net/pterodactylus/sone/freenet/fcp/AbstractCommand.java
src/test/java/net/pterodactylus/sone/freenet/fcp/AbstractCommandTest.java

index fa5a372..515697d 100644 (file)
@@ -52,6 +52,14 @@ public abstract class AbstractCommand implements Command {
                }
        }
 
+       protected static String getString(SimpleFieldSet simpleFieldSet, String key, String defaultValue) {
+               try {
+                       return simpleFieldSet.getString(key);
+               } catch (FSParseException fspe1) {
+                       return defaultValue;
+               }
+       }
+
        /**
         * Returns an int value from the given simple field set.
         *
index 374c9bf..7fc930b 100644 (file)
@@ -50,6 +50,20 @@ public class AbstractCommandTest {
        }
 
        @Test
+       public void verifyThatAskingForAnExistingKeyWithDefaultReturnsTheKey() {
+               SimpleFieldSet fieldSet = new SimpleFieldSetBuilder().put("Key", "Value").get();
+               String value = getString(fieldSet, "Key", "DefaultValue");
+               assertThat(value, is("Value"));
+       }
+
+       @Test
+       public void verifyThatAskingForANonExistingKeyWithDefaultReturnsTheDefaultKey() {
+               SimpleFieldSet fieldSet = new SimpleFieldSetBuilder().get();
+               String value = getString(fieldSet, "Key", "DefaultValue");
+               assertThat(value, is("DefaultValue"));
+       }
+
+       @Test
        public void verifyThatAskingForAnExistingIntDoesNotCauseAnError() throws FcpException {
                SimpleFieldSet fieldSet = new SimpleFieldSetBuilder().put("Int", "15").get();
                int value = getInt(fieldSet, "Int");