From: David ‘Bombe’ Roden Date: Fri, 10 Feb 2023 09:12:23 +0000 (+0100) Subject: ✅ Add test for “ConfigData” X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=ab24f03dd6d0f7a8dd3a788723df42b03cda09a6;p=jFCPlib.git ✅ Add test for “ConfigData” --- diff --git a/src/main/java/net/pterodactylus/fcp/ConfigData.java b/src/main/java/net/pterodactylus/fcp/ConfigData.java index d81b9ee..998b5aa 100644 --- a/src/main/java/net/pterodactylus/fcp/ConfigData.java +++ b/src/main/java/net/pterodactylus/fcp/ConfigData.java @@ -17,6 +17,8 @@ package net.pterodactylus.fcp; +import static java.lang.Boolean.parseBoolean; + /** * A “ConfigData” message contains various aspects of the node’s configuration. * @@ -110,7 +112,7 @@ public class ConfigData extends BaseMessage { * @return The expert flag of the option */ public boolean getExpertFlag(String option) { - return Boolean.valueOf(getField("expertFlag." + option)); + return parseBoolean(getField("expertFlag." + option)); } /** @@ -121,7 +123,7 @@ public class ConfigData extends BaseMessage { * @return The force-write flag of the given option */ public boolean getForceWriteFlag(String option) { - return Boolean.valueOf(getField("forceWriteFlag." + option)); + return parseBoolean(getField("forceWriteFlag." + option)); } } diff --git a/src/test/java/net/pterodactylus/fcp/ConfigDataTest.java b/src/test/java/net/pterodactylus/fcp/ConfigDataTest.java new file mode 100644 index 0000000..ff3596f --- /dev/null +++ b/src/test/java/net/pterodactylus/fcp/ConfigDataTest.java @@ -0,0 +1,86 @@ +package net.pterodactylus.fcp; + +import java.util.function.Function; + +import org.junit.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; + +/** + * Unit test for {@link ConfigData}. + */ +public class ConfigDataTest { + + @Test + public void configDataCanExposeCurrentOptionValue() { + testExposedMethodReturningString("current", configData::getCurrent); + } + + @Test + public void configDataCanExposeShortDescription() { + testExposedMethodReturningString("shortDescription", configData::getShortDescription); + } + + @Test + public void configDataCanExposeLongDescription() { + testExposedMethodReturningString("longDescription", configData::getLongDescription); + } + + @Test + public void configDataCanExposeDataType() { + testExposedMethodReturningString("dataType", configData::getDataType); + } + + @Test + public void configDataCanExposeDefault() { + testExposedMethodReturningString("default", configData::getDefault); + } + + @Test + public void configDataCanExposeSortOrder() { + receivedMessage.setField("sortOrder.testOption", "123"); + assertThat(configData.getSortOrder("testOption"), equalTo(123)); + } + + @Test + public void sortOrderIsReturnedAsMinus1IfGivenANonNumericValue() { + receivedMessage.setField("sortOrder.testOption", "not-a-number"); + assertThat(configData.getSortOrder("testOption"), equalTo(-1)); + } + + @Test + public void expertFlagIsReturnedCorrectly() { + receivedMessage.setField("expertFlag.testOption", "true"); + assertThat(configData.getExpertFlag("testOption"), equalTo(true)); + } + + @Test + public void expertFlagIsFalseIfGivenANonBooleanValue() { + receivedMessage.setField("expertFlag.testOption", "not-a-boolean"); + assertThat(configData.getExpertFlag("testOption"), equalTo(false)); + } + + @Test + public void forceWriteFlagIsReturnedCorrectly() { + receivedMessage.setField("forceWriteFlag.testOption", "true"); + assertThat(configData.getForceWriteFlag("testOption"), equalTo(true)); + } + + @Test + public void forceWriteFlagIsFalseIfGivenANonBooleanValue() { + receivedMessage.setField("forceWriteFlag.testOption", "not-a-boolean"); + assertThat(configData.getForceWriteFlag("testOption"), equalTo(false)); + } + + private void testExposedMethodReturningString(String prefixInMap, Function getter) { + String option = "testOption" + Math.random(); + String value = "testValue" + Math.random(); + receivedMessage.setField(prefixInMap + "." + option, value); + assertThat(getter.apply(option), equalTo(value)); + } + + private final FcpMessage receivedMessage = new FcpMessage(""); + private final ConfigData configData = new ConfigData(receivedMessage); + +}