From: David ‘Bombe’ Roden Date: Mon, 4 Nov 2013 20:47:10 +0000 (+0100) Subject: Add method to get a string with default value. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;ds=sidebyside;h=257a0abd136d250f6503b58145486beda9fc9c81;p=Sone.git Add method to get a string with default value. --- diff --git a/src/main/java/net/pterodactylus/sone/freenet/fcp/AbstractCommand.java b/src/main/java/net/pterodactylus/sone/freenet/fcp/AbstractCommand.java index fa5a372..515697d 100644 --- a/src/main/java/net/pterodactylus/sone/freenet/fcp/AbstractCommand.java +++ b/src/main/java/net/pterodactylus/sone/freenet/fcp/AbstractCommand.java @@ -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. * diff --git a/src/test/java/net/pterodactylus/sone/freenet/fcp/AbstractCommandTest.java b/src/test/java/net/pterodactylus/sone/freenet/fcp/AbstractCommandTest.java index 374c9bf..7fc930b 100644 --- a/src/test/java/net/pterodactylus/sone/freenet/fcp/AbstractCommandTest.java +++ b/src/test/java/net/pterodactylus/sone/freenet/fcp/AbstractCommandTest.java @@ -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");