diff --git a/src/main/java/fr/xephi/authme/command/CommandDescription.java b/src/main/java/fr/xephi/authme/command/CommandDescription.java index faeb0d74..2911055d 100644 --- a/src/main/java/fr/xephi/authme/command/CommandDescription.java +++ b/src/main/java/fr/xephi/authme/command/CommandDescription.java @@ -1,6 +1,7 @@ package fr.xephi.authme.command; import fr.xephi.authme.permission.PermissionNode; +import fr.xephi.authme.util.CollectionUtils; import fr.xephi.authme.util.StringUtils; import org.bukkit.command.CommandSender; @@ -185,10 +186,6 @@ public class CommandDescription { * @return True if the command reference is suitable to this command label, false otherwise. */ public boolean isSuitableLabel(CommandParts commandReference) { - // Make sure the command reference is valid - if (commandReference.getCount() <= 0) - return false; - // Get the parent count //getParent() = getParent().getParentCount() + 1 String element = commandReference.get(getParentCount()); @@ -252,7 +249,8 @@ public class CommandDescription { CommandParts reference = getCommandReference(other); // Compare the two references, return the result - return reference.getDifference(new CommandParts(other.getRange(0, reference.getCount())), fullCompare); + return CommandUtils.getDifference(reference.getList(), + CollectionUtils.getRange(other.getList(), 0, reference.getList().size()), fullCompare); } /** @@ -469,13 +467,13 @@ public class CommandDescription { return new FoundCommandResult( this, getCommandReference(queryReference), - new CommandParts(), + new CommandParts(new ArrayList()), queryReference); } // Get the new command reference and arguments - CommandParts newReference = new CommandParts(queryReference.getRange(0, getParentCount() + 1)); - CommandParts newArguments = new CommandParts(queryReference.getRange(getParentCount() + 1)); + CommandParts newReference = new CommandParts(CollectionUtils.getRange(queryReference.getList(), 0, getParentCount() + 1)); + CommandParts newArguments = new CommandParts(CollectionUtils.getRange(queryReference.getList(), getParentCount() + 1)); // Handle the child's, if this command has any if (getChildren().size() > 0) { diff --git a/src/main/java/fr/xephi/authme/command/CommandParts.java b/src/main/java/fr/xephi/authme/command/CommandParts.java index 35e89574..58df2601 100644 --- a/src/main/java/fr/xephi/authme/command/CommandParts.java +++ b/src/main/java/fr/xephi/authme/command/CommandParts.java @@ -14,12 +14,6 @@ public class CommandParts { */ private final List parts = new ArrayList<>(); - /** - * Constructor. - */ - public CommandParts() { - } - /** * Constructor. * @@ -47,17 +41,6 @@ public class CommandParts { this.parts.addAll(parts); } - /** - * Constructor. - * - * @param base The base part. - * @param parts The list of additional parts. - */ - public CommandParts(String base, List parts) { - this.parts.add(base); - this.parts.addAll(parts); - } - /** * Get the command parts. * @@ -67,41 +50,6 @@ public class CommandParts { return this.parts; } - /** - * Add a part. - * - * @param part The part to add. - * - * @return The result. - */ - public boolean add(String part) { - return this.parts.add(part); - } - - /** - * Add some parts. - * - * @param parts The parts to add. - * - * @return The result. - */ - public boolean add(List parts) { - return this.parts.addAll(parts); - } - - /** - * Add some parts. - * - * @param parts The parts to add. - * - * @return The result. - */ - public boolean add(String[] parts) { - for (String entry : parts) - add(entry); - return true; - } - /** * Get the number of parts. * @@ -146,6 +94,7 @@ public class CommandParts { * * @return The parts range. Parts that were out of bound are not included. */ + @Deprecated public List getRange(int start, int count) { // Create a new list to put the range into List elements = new ArrayList<>(); @@ -162,27 +111,7 @@ public class CommandParts { return elements; } - /** - * Get the difference value between two references. - * - * @param other The other reference. - * @param fullCompare True to compare the full references as far as the range reaches. - * - * @return The result from zero to above. A negative number will be returned on error. - */ - public double getDifference(CommandParts other, boolean fullCompare) { - // Make sure the other reference is correct - if (other == null) - return -1; - // Get the range to use - int range = Math.min(this.getCount(), other.getCount()); - - // Get and the difference - if (fullCompare) - return StringUtils.getDifference(this.toString(), other.toString()); - return StringUtils.getDifference(this.getRange(range - 1, 1).toString(), other.getRange(range - 1, 1).toString()); - } /** * Convert the parts to a string. diff --git a/src/main/java/fr/xephi/authme/command/CommandUtils.java b/src/main/java/fr/xephi/authme/command/CommandUtils.java index 6d5e2b55..6903ff7a 100644 --- a/src/main/java/fr/xephi/authme/command/CommandUtils.java +++ b/src/main/java/fr/xephi/authme/command/CommandUtils.java @@ -1,5 +1,11 @@ package fr.xephi.authme.command; +import fr.xephi.authme.util.CollectionUtils; +import fr.xephi.authme.util.StringUtils; + +import java.util.ArrayList; +import java.util.List; + public final class CommandUtils { public static int getMinNumberOfArguments(CommandDescription command) { @@ -16,4 +22,37 @@ public final class CommandUtils { return command.getArguments().size(); } + /** + * Provide a textual representation of a list of labels to show it as a command. For example, a list containing + * the items ["authme", "register", "player"] it will return "authme register player". + * + * @param labels The labels to format + * @return The space-separated labels + */ + public static String labelsToString(Iterable labels) { + return StringUtils.join(" ", labels); + } + + public static double getDifference(List labels1, List labels2, boolean fullCompare) { + // Make sure the other reference is correct + if (labels1 == null || labels2 == null) { + return -1; + } + + // Get the range to use + int range = Math.min(labels1.size(), labels2.size()); + + // Get and the difference + if (fullCompare) { + return StringUtils.getDifference(CommandUtils.labelsToString(labels1), CommandUtils.labelsToString(labels2)); + } + return StringUtils.getDifference( + labelsToString(CollectionUtils.getRange(labels1, range - 1, 1)), + labelsToString(CollectionUtils.getRange(labels2, range - 1, 1))); + } + + + + + } diff --git a/src/main/java/fr/xephi/authme/command/FoundCommandResult.java b/src/main/java/fr/xephi/authme/command/FoundCommandResult.java index 1ee4fbd9..91551650 100644 --- a/src/main/java/fr/xephi/authme/command/FoundCommandResult.java +++ b/src/main/java/fr/xephi/authme/command/FoundCommandResult.java @@ -139,10 +139,11 @@ public class FoundCommandResult { */ public double getDifference() { // Get the difference through the command found - if (this.commandDescription != null) + if (this.commandDescription != null) { return this.commandDescription.getCommandDifference(this.queryReference); + } // Get the difference from the query reference - return this.queryReference.getDifference(commandReference, true); + return CommandUtils.getDifference(queryReference.getList(), commandReference.getList(), true); } } diff --git a/src/main/java/fr/xephi/authme/command/help/HelpProvider.java b/src/main/java/fr/xephi/authme/command/help/HelpProvider.java index 03dab7b4..a2f3d1f2 100644 --- a/src/main/java/fr/xephi/authme/command/help/HelpProvider.java +++ b/src/main/java/fr/xephi/authme/command/help/HelpProvider.java @@ -9,6 +9,9 @@ import fr.xephi.authme.settings.Settings; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; +import java.util.ArrayList; +import java.util.List; + /** */ public class HelpProvider { @@ -40,7 +43,12 @@ public class HelpProvider { public static void showHelp(CommandSender sender, CommandParts reference, CommandParts helpQuery, boolean showCommand, boolean showDescription, boolean showArguments, boolean showPermissions, boolean showAlternatives, boolean showCommands) { // Find the command for this help query, one with and one without a prefixed base command FoundCommandResult result = AuthMe.getInstance().getCommandHandler().findCommand(new CommandParts(helpQuery.getList())); - CommandParts commandReferenceOther = new CommandParts(reference.get(0), helpQuery.getList()); + + // TODO ljacqu 20151204 Fix me to nicer code + List parts = new ArrayList<>(helpQuery.getList()); + parts.add(0, reference.get(0)); + CommandParts commandReferenceOther = new CommandParts(parts); + FoundCommandResult resultOther = AuthMe.getInstance().getCommandHandler().findCommand(commandReferenceOther); if (resultOther != null) { if (result == null) diff --git a/src/main/java/fr/xephi/authme/util/CollectionUtils.java b/src/main/java/fr/xephi/authme/util/CollectionUtils.java new file mode 100644 index 00000000..4d43757e --- /dev/null +++ b/src/main/java/fr/xephi/authme/util/CollectionUtils.java @@ -0,0 +1,47 @@ +package fr.xephi.authme.util; + +import java.util.ArrayList; +import java.util.List; + +/** + * Utils class for collections. + */ +public final class CollectionUtils { + + private CollectionUtils() { + } + + /** + * Get a range from a list based on start and count parameters in a safe way. + * + * @param start The start index + * @param count The number of elements to add + * + * @return The sublist consisting at most of {@code count} elements (less if the parameters + * exceed the size of the list) + */ + public static List getRange(List list, int start, int count) { + if (start >= list.size() || count <= 0) { + return new ArrayList<>(); + } else if (start < 0) { + start = 0; + } + int end = Math.min(list.size(), start + count); + return list.subList(start, end); + } + + /** + * Get all elements from a list starting from the given index. + * + * @param start The start index + * + * @return The sublist of all elements from index {@code start} and on; empty list + * if the start index exceeds the list's size + */ + public static List getRange(List list, int start) { + if (start >= list.size()) { + return new ArrayList<>(); + } + return getRange(list, start, list.size() - start); + } +} diff --git a/src/main/java/fr/xephi/authme/util/StringUtils.java b/src/main/java/fr/xephi/authme/util/StringUtils.java index 74fe0475..2e37bfce 100644 --- a/src/main/java/fr/xephi/authme/util/StringUtils.java +++ b/src/main/java/fr/xephi/authme/util/StringUtils.java @@ -29,8 +29,9 @@ public final class StringUtils { */ public static double getDifference(String first, String second) { // Make sure the strings are valid. - if (first == null || second == null) + if (first == null || second == null) { return 1.0; + } // Create a string similarity service instance, to allow comparison StringSimilarityService service = new StringSimilarityServiceImpl(new LevenshteinDistanceStrategy()); diff --git a/src/test/java/fr/xephi/authme/command/CommandInitializerTest.java b/src/test/java/fr/xephi/authme/command/CommandInitializerTest.java index dcbec20e..51a8e3cd 100644 --- a/src/test/java/fr/xephi/authme/command/CommandInitializerTest.java +++ b/src/test/java/fr/xephi/authme/command/CommandInitializerTest.java @@ -254,12 +254,12 @@ public class CommandInitializerTest { } /** - * Get the absolute label that a command defines. Note: Assumes that only the passed command might have + * Get the absolute binding that a command defines. Note: Assumes that only the passed command can have * multiple labels; only considering the first label for all of the command's parents. * - * @param command The command to verify + * @param command The command to process * - * @return The full command binding + * @return List of all bindings that lead to the command */ private static List getAbsoluteLabels(CommandDescription command) { String parentPath = ""; diff --git a/src/test/java/fr/xephi/authme/command/CommandPartsTest.java b/src/test/java/fr/xephi/authme/command/CommandPartsTest.java index 62d22b5f..163f52d4 100644 --- a/src/test/java/fr/xephi/authme/command/CommandPartsTest.java +++ b/src/test/java/fr/xephi/authme/command/CommandPartsTest.java @@ -3,6 +3,7 @@ package fr.xephi.authme.command; import org.junit.Test; import java.util.Arrays; +import java.util.Collections; import static org.hamcrest.Matchers.equalTo; import static org.junit.Assert.assertThat; @@ -27,7 +28,7 @@ public class CommandPartsTest { @Test public void shouldPrintEmptyStringForNoArguments() { // given - CommandParts parts = new CommandParts(); + CommandParts parts = new CommandParts(Collections.EMPTY_LIST); // when String str = parts.toString(); diff --git a/src/test/java/fr/xephi/authme/command/executable/captcha/CaptchaCommandTest.java b/src/test/java/fr/xephi/authme/command/executable/captcha/CaptchaCommandTest.java index 36169db0..6b183e35 100644 --- a/src/test/java/fr/xephi/authme/command/executable/captcha/CaptchaCommandTest.java +++ b/src/test/java/fr/xephi/authme/command/executable/captcha/CaptchaCommandTest.java @@ -15,6 +15,8 @@ import org.junit.Ignore; import org.junit.Test; import org.mockito.Mockito; +import java.util.Collections; + import static org.hamcrest.Matchers.equalTo; import static org.junit.Assert.assertThat; import static org.mockito.Mockito.verify; @@ -40,7 +42,7 @@ public class CaptchaCommandTest { ExecutableCommand command = new CaptchaCommand(); // when - boolean result = command.executeCommand(sender, new CommandParts(), new CommandParts()); + boolean result = command.executeCommand(sender, new CommandParts(Collections.EMPTY_LIST), new CommandParts(Collections.EMPTY_LIST)); // then assertThat(result, equalTo(true)); @@ -56,7 +58,7 @@ public class CaptchaCommandTest { ExecutableCommand command = new CaptchaCommand(); // when - boolean result = command.executeCommand(player, new CommandParts(), new CommandParts()); + boolean result = command.executeCommand(player, new CommandParts(Collections.EMPTY_LIST), new CommandParts(Collections.EMPTY_LIST)); // then assertThat(result, equalTo(true)); diff --git a/src/test/java/fr/xephi/authme/command/executable/changepassword/ChangePasswordCommandTest.java b/src/test/java/fr/xephi/authme/command/executable/changepassword/ChangePasswordCommandTest.java index a81611a5..9092205a 100644 --- a/src/test/java/fr/xephi/authme/command/executable/changepassword/ChangePasswordCommandTest.java +++ b/src/test/java/fr/xephi/authme/command/executable/changepassword/ChangePasswordCommandTest.java @@ -18,6 +18,7 @@ import org.junit.Before; import org.junit.Test; import org.mockito.ArgumentCaptor; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -58,7 +59,7 @@ public class ChangePasswordCommandTest { CommandParts arguments = mock(CommandParts.class); // when - command.executeCommand(sender, new CommandParts(), arguments); + command.executeCommand(sender, newParts(), arguments); // then verify(arguments, never()).get(anyInt()); @@ -72,7 +73,7 @@ public class ChangePasswordCommandTest { ChangePasswordCommand command = new ChangePasswordCommand(); // when - command.executeCommand(sender, new CommandParts(), new CommandParts("pass")); + command.executeCommand(sender, newParts(), new CommandParts("pass")); // then verify(messagesMock).send(sender, MessageKey.NOT_LOGGED_IN); @@ -86,7 +87,7 @@ public class ChangePasswordCommandTest { ChangePasswordCommand command = new ChangePasswordCommand(); // when - command.executeCommand(sender, new CommandParts(), newParts("old123", "!pass")); + command.executeCommand(sender, newParts(), newParts("old123", "!pass")); // then verify(messagesMock).send(sender, MessageKey.PASSWORD_MATCH_ERROR); @@ -101,7 +102,7 @@ public class ChangePasswordCommandTest { ChangePasswordCommand command = new ChangePasswordCommand(); // when - command.executeCommand(sender, new CommandParts(), newParts("old_", "Tester")); + command.executeCommand(sender, newParts(), newParts("old_", "Tester")); // then verify(messagesMock).send(sender, MessageKey.PASSWORD_IS_USERNAME_ERROR); @@ -116,7 +117,7 @@ public class ChangePasswordCommandTest { Settings.passwordMaxLength = 3; // when - command.executeCommand(sender, new CommandParts(), newParts("12", "test")); + command.executeCommand(sender, newParts(), newParts("12", "test")); // then verify(messagesMock).send(sender, MessageKey.INVALID_PASSWORD_LENGTH); @@ -131,7 +132,7 @@ public class ChangePasswordCommandTest { Settings.getPasswordMinLen = 7; // when - command.executeCommand(sender, new CommandParts(), newParts("oldverylongpassword", "tester")); + command.executeCommand(sender, newParts(), newParts("oldverylongpassword", "tester")); // then verify(messagesMock).send(sender, MessageKey.INVALID_PASSWORD_LENGTH); @@ -146,7 +147,7 @@ public class ChangePasswordCommandTest { Settings.unsafePasswords = asList("test", "abc123"); // when - command.executeCommand(sender, new CommandParts(), newParts("oldpw", "abc123")); + command.executeCommand(sender, newParts(), newParts("oldpw", "abc123")); // then verify(messagesMock).send(sender, MessageKey.PASSWORD_UNSAFE_ERROR); @@ -160,7 +161,7 @@ public class ChangePasswordCommandTest { ChangePasswordCommand command = new ChangePasswordCommand(); // when - command.executeCommand(sender, new CommandParts(), newParts("abc123", "abc123")); + command.executeCommand(sender, newParts(), newParts("abc123", "abc123")); // then verify(messagesMock, never()).send(eq(sender), any(MessageKey.class)); diff --git a/src/test/java/fr/xephi/authme/command/executable/email/AddEmailCommandTest.java b/src/test/java/fr/xephi/authme/command/executable/email/AddEmailCommandTest.java index a06cff22..d377fc0f 100644 --- a/src/test/java/fr/xephi/authme/command/executable/email/AddEmailCommandTest.java +++ b/src/test/java/fr/xephi/authme/command/executable/email/AddEmailCommandTest.java @@ -11,6 +11,7 @@ import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; +import java.util.ArrayList; import java.util.Arrays; import static org.mockito.Mockito.never; @@ -40,7 +41,7 @@ public class AddEmailCommandTest { AddEmailCommand command = new AddEmailCommand(); // when - command.executeCommand(sender, new CommandParts(), new CommandParts()); + command.executeCommand(sender, newParts(), newParts()); // then verify(authMeMock, never()).getManagement(); @@ -53,11 +54,15 @@ public class AddEmailCommandTest { AddEmailCommand command = new AddEmailCommand(); // when - command.executeCommand(sender, new CommandParts(), + command.executeCommand(sender, newParts(), new CommandParts(Arrays.asList("mail@example", "other_example"))); // then verify(authMeMock).getManagement(); verify(managementMock).performAddEmail(sender, "mail@example", "other_example"); } + + private static CommandParts newParts() { + return new CommandParts(new ArrayList()); + } } diff --git a/src/test/java/fr/xephi/authme/command/executable/email/ChangeEmailCommandTest.java b/src/test/java/fr/xephi/authme/command/executable/email/ChangeEmailCommandTest.java index 0f2c05e6..2dc25c96 100644 --- a/src/test/java/fr/xephi/authme/command/executable/email/ChangeEmailCommandTest.java +++ b/src/test/java/fr/xephi/authme/command/executable/email/ChangeEmailCommandTest.java @@ -11,6 +11,7 @@ import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; +import java.util.ArrayList; import java.util.Arrays; import static org.mockito.Mockito.never; @@ -40,7 +41,7 @@ public class ChangeEmailCommandTest { ChangeEmailCommand command = new ChangeEmailCommand(); // when - command.executeCommand(sender, new CommandParts(), new CommandParts()); + command.executeCommand(sender, newParts(), newParts()); // then verify(authMeMock, never()).getManagement(); @@ -53,11 +54,15 @@ public class ChangeEmailCommandTest { ChangeEmailCommand command = new ChangeEmailCommand(); // when - command.executeCommand(sender, new CommandParts(), + command.executeCommand(sender, newParts(), new CommandParts(Arrays.asList("new.mail@example.org", "old_mail@example.org"))); // then verify(authMeMock).getManagement(); verify(managementMock).performChangeEmail(sender, "new.mail@example.org", "old_mail@example.org"); } + + private static CommandParts newParts() { + return new CommandParts(new ArrayList()); + } } diff --git a/src/test/java/fr/xephi/authme/command/executable/email/RecoverEmailCommandTest.java b/src/test/java/fr/xephi/authme/command/executable/email/RecoverEmailCommandTest.java index 9857a2d4..a1403c5a 100644 --- a/src/test/java/fr/xephi/authme/command/executable/email/RecoverEmailCommandTest.java +++ b/src/test/java/fr/xephi/authme/command/executable/email/RecoverEmailCommandTest.java @@ -9,6 +9,8 @@ import org.junit.Ignore; import org.junit.Test; import org.mockito.Mockito; +import java.util.Collections; + /** * Test for {@link RecoverEmailCommand}. */ @@ -27,7 +29,7 @@ public class RecoverEmailCommandTest { RecoverEmailCommand command = new RecoverEmailCommand(); // when - command.executeCommand(sender, new CommandParts(), new CommandParts()); + command.executeCommand(sender, new CommandParts(Collections.EMPTY_LIST), new CommandParts(Collections.EMPTY_LIST)); // then } diff --git a/src/test/java/fr/xephi/authme/command/executable/login/LoginCommandTest.java b/src/test/java/fr/xephi/authme/command/executable/login/LoginCommandTest.java index 6f06a5ff..52a71891 100644 --- a/src/test/java/fr/xephi/authme/command/executable/login/LoginCommandTest.java +++ b/src/test/java/fr/xephi/authme/command/executable/login/LoginCommandTest.java @@ -11,6 +11,8 @@ import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; +import java.util.ArrayList; + import static org.mockito.Matchers.*; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; @@ -38,7 +40,7 @@ public class LoginCommandTest { LoginCommand command = new LoginCommand(); // when - command.executeCommand(sender, new CommandParts(), new CommandParts()); + command.executeCommand(sender, newParts(), newParts()); // then Mockito.verify(managementMock, never()).performLogin(any(Player.class), anyString(), anyBoolean()); @@ -51,7 +53,7 @@ public class LoginCommandTest { LoginCommand command = new LoginCommand(); // when - command.executeCommand(sender, new CommandParts(), new CommandParts("password")); + command.executeCommand(sender, newParts(), new CommandParts("password")); // then Mockito.verify(managementMock).performLogin(eq(sender), eq("password"), eq(false)); @@ -64,11 +66,15 @@ public class LoginCommandTest { LoginCommand command = new LoginCommand(); // when - command.executeCommand(sender, new CommandParts(), new CommandParts()); + command.executeCommand(sender, newParts(), newParts()); // then // TODO ljacqu 20151121: May make sense to handle null password in LoginCommand instead of forwarding the call String password = null; Mockito.verify(managementMock).performLogin(eq(sender), eq(password), eq(false)); } + + private static CommandParts newParts() { + return new CommandParts(new ArrayList()); + } } diff --git a/src/test/java/fr/xephi/authme/command/executable/logout/LogoutCommandTest.java b/src/test/java/fr/xephi/authme/command/executable/logout/LogoutCommandTest.java index c8774978..65f24639 100644 --- a/src/test/java/fr/xephi/authme/command/executable/logout/LogoutCommandTest.java +++ b/src/test/java/fr/xephi/authme/command/executable/logout/LogoutCommandTest.java @@ -12,6 +12,8 @@ import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; +import java.util.ArrayList; + import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; @@ -40,7 +42,7 @@ public class LogoutCommandTest { LogoutCommand command = new LogoutCommand(); // when - command.executeCommand(sender, new CommandParts(), new CommandParts()); + command.executeCommand(sender, new CommandParts(new ArrayList()), new CommandParts(new ArrayList())); // then Mockito.verify(managementMock, never()).performLogout(any(Player.class)); @@ -53,7 +55,7 @@ public class LogoutCommandTest { LogoutCommand command = new LogoutCommand(); // when - command.executeCommand(sender, new CommandParts(), new CommandParts("password")); + command.executeCommand(sender, new CommandParts(new ArrayList()), new CommandParts("password")); // then Mockito.verify(managementMock).performLogout(sender); diff --git a/src/test/java/fr/xephi/authme/command/executable/register/RegisterCommandTest.java b/src/test/java/fr/xephi/authme/command/executable/register/RegisterCommandTest.java index 02bb65a7..6dd19eb2 100644 --- a/src/test/java/fr/xephi/authme/command/executable/register/RegisterCommandTest.java +++ b/src/test/java/fr/xephi/authme/command/executable/register/RegisterCommandTest.java @@ -14,6 +14,8 @@ import org.junit.Test; import org.mockito.ArgumentCaptor; import org.mockito.Mockito; +import java.util.ArrayList; + import static org.hamcrest.Matchers.equalTo; import static org.junit.Assert.assertThat; import static org.mockito.Matchers.any; @@ -48,7 +50,7 @@ public class RegisterCommandTest { ArgumentCaptor messageCaptor = ArgumentCaptor.forClass(String.class); // when - command.executeCommand(sender, new CommandParts(), new CommandParts()); + command.executeCommand(sender, newParts(), newParts()); // then verify(sender).sendMessage(messageCaptor.capture()); @@ -63,7 +65,7 @@ public class RegisterCommandTest { RegisterCommand command = new RegisterCommand(); // when - command.executeCommand(sender, new CommandParts(), new CommandParts()); + command.executeCommand(sender, newParts(), newParts()); // then verify(messagesMock).send(sender, MessageKey.USAGE_REGISTER); @@ -77,9 +79,13 @@ public class RegisterCommandTest { RegisterCommand command = new RegisterCommand(); // when - command.executeCommand(sender, new CommandParts(), new CommandParts("password")); + command.executeCommand(sender, newParts(), new CommandParts("password")); // then verify(managementMock).performRegister(sender, "password", ""); } + + private static CommandParts newParts() { + return new CommandParts(new ArrayList()); + } } diff --git a/src/test/java/fr/xephi/authme/command/help/HelpSyntaxHelperTest.java b/src/test/java/fr/xephi/authme/command/help/HelpSyntaxHelperTest.java index f9fa9a6a..acfe9028 100644 --- a/src/test/java/fr/xephi/authme/command/help/HelpSyntaxHelperTest.java +++ b/src/test/java/fr/xephi/authme/command/help/HelpSyntaxHelperTest.java @@ -7,6 +7,8 @@ import fr.xephi.authme.command.executable.authme.RegisterCommand; import org.junit.Test; import org.mockito.Mockito; +import java.util.ArrayList; + import static org.bukkit.ChatColor.BOLD; import static org.bukkit.ChatColor.ITALIC; import static org.bukkit.ChatColor.WHITE; @@ -27,7 +29,7 @@ public class HelpSyntaxHelperTest { .build(); // when - String result = HelpSyntaxHelper.getCommandSyntax(description, new CommandParts(), "", false); + String result = HelpSyntaxHelper.getCommandSyntax(description, newParts(), "", false); // then assertThat(result, equalTo(WHITE + "/authme register" + ITALIC + " [name]")); @@ -41,7 +43,7 @@ public class HelpSyntaxHelperTest { .build(); // when - String result = HelpSyntaxHelper.getCommandSyntax(description, new CommandParts(), null, false); + String result = HelpSyntaxHelper.getCommandSyntax(description, newParts(), null, false); // then assertThat(result, equalTo(WHITE + "/authme register" + ITALIC + " ")); @@ -56,7 +58,7 @@ public class HelpSyntaxHelperTest { .build(); // when - String result = HelpSyntaxHelper.getCommandSyntax(description, new CommandParts(), "", false); + String result = HelpSyntaxHelper.getCommandSyntax(description, newParts(), "", false); // then assertThat(result, equalTo(WHITE + "/authme register" + ITALIC + " [name]" + ITALIC + " ")); @@ -71,7 +73,7 @@ public class HelpSyntaxHelperTest { .build(); // when - String result = HelpSyntaxHelper.getCommandSyntax(description, new CommandParts(), "", true); + String result = HelpSyntaxHelper.getCommandSyntax(description, newParts(), "", true); // then assertThat(result, equalTo(WHITE + "/authme " @@ -85,7 +87,7 @@ public class HelpSyntaxHelperTest { CommandDescription description = getDescriptionBuilder().build(); // when - String result = HelpSyntaxHelper.getCommandSyntax(description, new CommandParts(), null, true); + String result = HelpSyntaxHelper.getCommandSyntax(description, newParts(), null, true); // then assertThat(result, equalTo(WHITE + "/authme " + YELLOW + BOLD + "register" + YELLOW)); @@ -99,12 +101,16 @@ public class HelpSyntaxHelperTest { .build(); // when - String result = HelpSyntaxHelper.getCommandSyntax(description, new CommandParts(), "alt", false); + String result = HelpSyntaxHelper.getCommandSyntax(description, newParts(), "alt", false); // then assertThat(result, equalTo(WHITE + "/authme alt" + ITALIC + " [name]")); } + private static CommandParts newParts() { + // TODO ljacqu 20151204: Remove this method once CommandParts has been removed + return new CommandParts(new ArrayList()); + } private static CommandDescription.CommandBuilder getDescriptionBuilder() { CommandDescription base = CommandDescription.builder() diff --git a/src/test/java/fr/xephi/authme/util/CollectionUtilsTest.java b/src/test/java/fr/xephi/authme/util/CollectionUtilsTest.java new file mode 100644 index 00000000..f359f088 --- /dev/null +++ b/src/test/java/fr/xephi/authme/util/CollectionUtilsTest.java @@ -0,0 +1,102 @@ +package fr.xephi.authme.util; + +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertThat; +import static org.hamcrest.Matchers.empty; + +/** + * Test for {@link CollectionUtils}. + */ +public class CollectionUtilsTest { + + @Test + public void shouldGetFullList() { + // given + List list = Arrays.asList("test", "1", "2", "3", "4"); + + // when + List result = CollectionUtils.getRange(list, 0, 24); + + // then + assertThat(result, equalTo(list)); + } + + @Test + public void shouldReturnEmptyListForZeroCount() { + // given + List list = Arrays.asList("test", "1", "2", "3", "4"); + + // when + List result = CollectionUtils.getRange(list, 2, 0); + + // then + assertThat(result, empty()); + } + + + @Test + public void shouldReturnEmptyListForTooHighStart() { + // given + List list = Arrays.asList("test", "1", "2", "3", "4"); + + // when + List result = CollectionUtils.getRange(list, 12, 2); + + // then + assertThat(result, empty()); + } + + @Test + public void shouldReturnSubList() { + // given + List list = Arrays.asList("test", "1", "2", "3", "4"); + + // when + List result = CollectionUtils.getRange(list, 1, 3); + + // then + assertThat(result, contains("1", "2", "3")); + } + + @Test + public void shouldReturnTillEnd() { + // given + List list = Arrays.asList("test", "1", "2", "3", "4"); + + // when + List result = CollectionUtils.getRange(list, 2, 3); + + // then + assertThat(result, contains("2", "3", "4")); + } + + @Test + public void shouldRemoveFirstTwo() { + // given + List list = Arrays.asList("test", "1", "2", "3", "4"); + + // when + List result = CollectionUtils.getRange(list, 2); + + // then + assertThat(result, contains("2", "3", "4")); + } + + @Test + public void shouldHandleNegativeStart() { + // given + List list = Arrays.asList("test", "1", "2", "3", "4"); + + // when + List result = CollectionUtils.getRange(list, -4); + + // then + assertThat(result, equalTo(list)); + } +}