From cc1dc2fbff3741417832af07b84aa1933e13bb0d Mon Sep 17 00:00:00 2001 From: xxyy Date: Sat, 20 Sep 2014 17:30:17 +0200 Subject: [PATCH] Fix CommandHelper#getRawCommand() misbehaving with sub-commands (Fixes #9), Add corresponding test to prevent this from happening again --- common/pom.xml | 4 ++++ .../cmdblocker/common/util/CommandHelper.java | 2 +- .../common/util/CommandHelperTest.java | 24 +++++++++++++++++++ pom.xml | 6 +++++ 4 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 common/src/test/java/io/github/xxyy/cmdblocker/common/util/CommandHelperTest.java diff --git a/common/pom.xml b/common/pom.xml index 6280e180..ef9d4262 100644 --- a/common/pom.xml +++ b/common/pom.xml @@ -16,5 +16,9 @@ net.cubespace Yamler-Core + + junit + junit + diff --git a/common/src/main/java/io/github/xxyy/cmdblocker/common/util/CommandHelper.java b/common/src/main/java/io/github/xxyy/cmdblocker/common/util/CommandHelper.java index c3885253..4c1072d7 100644 --- a/common/src/main/java/io/github/xxyy/cmdblocker/common/util/CommandHelper.java +++ b/common/src/main/java/io/github/xxyy/cmdblocker/common/util/CommandHelper.java @@ -24,7 +24,7 @@ public static String getRawCommand(String chatMessage) { if (spaceIndex == -1) { //If no space found chatMessage = chatMessage.substring(1); //Just remove slash } else { //If we have a space - chatMessage = chatMessage.substring(1, spaceIndex - 1); //Get the first word of the message and remove slash + chatMessage = chatMessage.substring(1, spaceIndex); //Get the first word of the message and remove slash } return removeModPrefix(chatMessage); //Return the raw command name! diff --git a/common/src/test/java/io/github/xxyy/cmdblocker/common/util/CommandHelperTest.java b/common/src/test/java/io/github/xxyy/cmdblocker/common/util/CommandHelperTest.java new file mode 100644 index 00000000..a7c95f70 --- /dev/null +++ b/common/src/test/java/io/github/xxyy/cmdblocker/common/util/CommandHelperTest.java @@ -0,0 +1,24 @@ +package io.github.xxyy.cmdblocker.common.util; + +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +/** + * Tests the CommandHelper class for compliance with the JavaDoc declarations + * + * @author xxyy + * @since 20/09/14 + */ +public class CommandHelperTest { + @Test + public void testGetRawCommand() { + assertThat(CommandHelper.getRawCommand("/help"), is("help")); + assertThat(CommandHelper.getRawCommand("/help test"), is("help")); + assertThat(CommandHelper.getRawCommand("/minecraft:help"), is("help")); + assertThat(CommandHelper.getRawCommand("/minecraft:help test"), is("help")); + assertThat(CommandHelper.getRawCommand("/bukkit:help"), is("help")); + assertThat(CommandHelper.getRawCommand("/bukkit:help something multiple arguments wow"), is("help")); + } +} diff --git a/pom.xml b/pom.xml index c07e6f96..c49bb90e 100644 --- a/pom.xml +++ b/pom.xml @@ -122,6 +122,12 @@ Yamler-Core 2.2.3-SNAPSHOT + + + junit + junit + 4.11 +