mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-11-28 05:05:14 +01:00
Move most API of CommandParts to util classes
Gradual transition to removing CommandParts altogether. - Move logic from CommandParts to utils classes
This commit is contained in:
parent
bb93c7164a
commit
2faad44ffa
@ -1,6 +1,7 @@
|
|||||||
package fr.xephi.authme.command;
|
package fr.xephi.authme.command;
|
||||||
|
|
||||||
import fr.xephi.authme.permission.PermissionNode;
|
import fr.xephi.authme.permission.PermissionNode;
|
||||||
|
import fr.xephi.authme.util.CollectionUtils;
|
||||||
import fr.xephi.authme.util.StringUtils;
|
import fr.xephi.authme.util.StringUtils;
|
||||||
import org.bukkit.command.CommandSender;
|
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.
|
* @return True if the command reference is suitable to this command label, false otherwise.
|
||||||
*/
|
*/
|
||||||
public boolean isSuitableLabel(CommandParts commandReference) {
|
public boolean isSuitableLabel(CommandParts commandReference) {
|
||||||
// Make sure the command reference is valid
|
|
||||||
if (commandReference.getCount() <= 0)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
// Get the parent count
|
// Get the parent count
|
||||||
//getParent() = getParent().getParentCount() + 1
|
//getParent() = getParent().getParentCount() + 1
|
||||||
String element = commandReference.get(getParentCount());
|
String element = commandReference.get(getParentCount());
|
||||||
@ -252,7 +249,8 @@ public class CommandDescription {
|
|||||||
CommandParts reference = getCommandReference(other);
|
CommandParts reference = getCommandReference(other);
|
||||||
|
|
||||||
// Compare the two references, return the result
|
// 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(
|
return new FoundCommandResult(
|
||||||
this,
|
this,
|
||||||
getCommandReference(queryReference),
|
getCommandReference(queryReference),
|
||||||
new CommandParts(),
|
new CommandParts(new ArrayList<String>()),
|
||||||
queryReference);
|
queryReference);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the new command reference and arguments
|
// Get the new command reference and arguments
|
||||||
CommandParts newReference = new CommandParts(queryReference.getRange(0, getParentCount() + 1));
|
CommandParts newReference = new CommandParts(CollectionUtils.getRange(queryReference.getList(), 0, getParentCount() + 1));
|
||||||
CommandParts newArguments = new CommandParts(queryReference.getRange(getParentCount() + 1));
|
CommandParts newArguments = new CommandParts(CollectionUtils.getRange(queryReference.getList(), getParentCount() + 1));
|
||||||
|
|
||||||
// Handle the child's, if this command has any
|
// Handle the child's, if this command has any
|
||||||
if (getChildren().size() > 0) {
|
if (getChildren().size() > 0) {
|
||||||
|
@ -14,12 +14,6 @@ public class CommandParts {
|
|||||||
*/
|
*/
|
||||||
private final List<String> parts = new ArrayList<>();
|
private final List<String> parts = new ArrayList<>();
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor.
|
|
||||||
*/
|
|
||||||
public CommandParts() {
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
*
|
*
|
||||||
@ -47,17 +41,6 @@ public class CommandParts {
|
|||||||
this.parts.addAll(parts);
|
this.parts.addAll(parts);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor.
|
|
||||||
*
|
|
||||||
* @param base The base part.
|
|
||||||
* @param parts The list of additional parts.
|
|
||||||
*/
|
|
||||||
public CommandParts(String base, List<String> parts) {
|
|
||||||
this.parts.add(base);
|
|
||||||
this.parts.addAll(parts);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the command parts.
|
* Get the command parts.
|
||||||
*
|
*
|
||||||
@ -67,41 +50,6 @@ public class CommandParts {
|
|||||||
return this.parts;
|
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<String> 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.
|
* 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.
|
* @return The parts range. Parts that were out of bound are not included.
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
public List<String> getRange(int start, int count) {
|
public List<String> getRange(int start, int count) {
|
||||||
// Create a new list to put the range into
|
// Create a new list to put the range into
|
||||||
List<String> elements = new ArrayList<>();
|
List<String> elements = new ArrayList<>();
|
||||||
@ -162,27 +111,7 @@ public class CommandParts {
|
|||||||
return elements;
|
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.
|
* Convert the parts to a string.
|
||||||
|
@ -1,5 +1,11 @@
|
|||||||
package fr.xephi.authme.command;
|
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 final class CommandUtils {
|
||||||
|
|
||||||
public static int getMinNumberOfArguments(CommandDescription command) {
|
public static int getMinNumberOfArguments(CommandDescription command) {
|
||||||
@ -16,4 +22,37 @@ public final class CommandUtils {
|
|||||||
return command.getArguments().size();
|
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<String> labels) {
|
||||||
|
return StringUtils.join(" ", labels);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double getDifference(List<String> labels1, List<String> 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)));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -139,10 +139,11 @@ public class FoundCommandResult {
|
|||||||
*/
|
*/
|
||||||
public double getDifference() {
|
public double getDifference() {
|
||||||
// Get the difference through the command found
|
// Get the difference through the command found
|
||||||
if (this.commandDescription != null)
|
if (this.commandDescription != null) {
|
||||||
return this.commandDescription.getCommandDifference(this.queryReference);
|
return this.commandDescription.getCommandDifference(this.queryReference);
|
||||||
|
}
|
||||||
|
|
||||||
// Get the difference from the query reference
|
// Get the difference from the query reference
|
||||||
return this.queryReference.getDifference(commandReference, true);
|
return CommandUtils.getDifference(queryReference.getList(), commandReference.getList(), true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,9 @@ import fr.xephi.authme.settings.Settings;
|
|||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*/
|
*/
|
||||||
public class HelpProvider {
|
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) {
|
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
|
// 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()));
|
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<String> parts = new ArrayList<>(helpQuery.getList());
|
||||||
|
parts.add(0, reference.get(0));
|
||||||
|
CommandParts commandReferenceOther = new CommandParts(parts);
|
||||||
|
|
||||||
FoundCommandResult resultOther = AuthMe.getInstance().getCommandHandler().findCommand(commandReferenceOther);
|
FoundCommandResult resultOther = AuthMe.getInstance().getCommandHandler().findCommand(commandReferenceOther);
|
||||||
if (resultOther != null) {
|
if (resultOther != null) {
|
||||||
if (result == null)
|
if (result == null)
|
||||||
|
47
src/main/java/fr/xephi/authme/util/CollectionUtils.java
Normal file
47
src/main/java/fr/xephi/authme/util/CollectionUtils.java
Normal file
@ -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<String> getRange(List<String> 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<String> getRange(List<String> list, int start) {
|
||||||
|
if (start >= list.size()) {
|
||||||
|
return new ArrayList<>();
|
||||||
|
}
|
||||||
|
return getRange(list, start, list.size() - start);
|
||||||
|
}
|
||||||
|
}
|
@ -29,8 +29,9 @@ public final class StringUtils {
|
|||||||
*/
|
*/
|
||||||
public static double getDifference(String first, String second) {
|
public static double getDifference(String first, String second) {
|
||||||
// Make sure the strings are valid.
|
// Make sure the strings are valid.
|
||||||
if (first == null || second == null)
|
if (first == null || second == null) {
|
||||||
return 1.0;
|
return 1.0;
|
||||||
|
}
|
||||||
|
|
||||||
// Create a string similarity service instance, to allow comparison
|
// Create a string similarity service instance, to allow comparison
|
||||||
StringSimilarityService service = new StringSimilarityServiceImpl(new LevenshteinDistanceStrategy());
|
StringSimilarityService service = new StringSimilarityServiceImpl(new LevenshteinDistanceStrategy());
|
||||||
|
@ -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.
|
* 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<String> getAbsoluteLabels(CommandDescription command) {
|
private static List<String> getAbsoluteLabels(CommandDescription command) {
|
||||||
String parentPath = "";
|
String parentPath = "";
|
||||||
|
@ -3,6 +3,7 @@ package fr.xephi.authme.command;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.equalTo;
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.assertThat;
|
||||||
@ -27,7 +28,7 @@ public class CommandPartsTest {
|
|||||||
@Test
|
@Test
|
||||||
public void shouldPrintEmptyStringForNoArguments() {
|
public void shouldPrintEmptyStringForNoArguments() {
|
||||||
// given
|
// given
|
||||||
CommandParts parts = new CommandParts();
|
CommandParts parts = new CommandParts(Collections.EMPTY_LIST);
|
||||||
|
|
||||||
// when
|
// when
|
||||||
String str = parts.toString();
|
String str = parts.toString();
|
||||||
|
@ -15,6 +15,8 @@ import org.junit.Ignore;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.equalTo;
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.assertThat;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
@ -40,7 +42,7 @@ public class CaptchaCommandTest {
|
|||||||
ExecutableCommand command = new CaptchaCommand();
|
ExecutableCommand command = new CaptchaCommand();
|
||||||
|
|
||||||
// when
|
// 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
|
// then
|
||||||
assertThat(result, equalTo(true));
|
assertThat(result, equalTo(true));
|
||||||
@ -56,7 +58,7 @@ public class CaptchaCommandTest {
|
|||||||
ExecutableCommand command = new CaptchaCommand();
|
ExecutableCommand command = new CaptchaCommand();
|
||||||
|
|
||||||
// when
|
// 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
|
// then
|
||||||
assertThat(result, equalTo(true));
|
assertThat(result, equalTo(true));
|
||||||
|
@ -18,6 +18,7 @@ import org.junit.Before;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.mockito.ArgumentCaptor;
|
import org.mockito.ArgumentCaptor;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
|
||||||
@ -58,7 +59,7 @@ public class ChangePasswordCommandTest {
|
|||||||
CommandParts arguments = mock(CommandParts.class);
|
CommandParts arguments = mock(CommandParts.class);
|
||||||
|
|
||||||
// when
|
// when
|
||||||
command.executeCommand(sender, new CommandParts(), arguments);
|
command.executeCommand(sender, newParts(), arguments);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
verify(arguments, never()).get(anyInt());
|
verify(arguments, never()).get(anyInt());
|
||||||
@ -72,7 +73,7 @@ public class ChangePasswordCommandTest {
|
|||||||
ChangePasswordCommand command = new ChangePasswordCommand();
|
ChangePasswordCommand command = new ChangePasswordCommand();
|
||||||
|
|
||||||
// when
|
// when
|
||||||
command.executeCommand(sender, new CommandParts(), new CommandParts("pass"));
|
command.executeCommand(sender, newParts(), new CommandParts("pass"));
|
||||||
|
|
||||||
// then
|
// then
|
||||||
verify(messagesMock).send(sender, MessageKey.NOT_LOGGED_IN);
|
verify(messagesMock).send(sender, MessageKey.NOT_LOGGED_IN);
|
||||||
@ -86,7 +87,7 @@ public class ChangePasswordCommandTest {
|
|||||||
ChangePasswordCommand command = new ChangePasswordCommand();
|
ChangePasswordCommand command = new ChangePasswordCommand();
|
||||||
|
|
||||||
// when
|
// when
|
||||||
command.executeCommand(sender, new CommandParts(), newParts("old123", "!pass"));
|
command.executeCommand(sender, newParts(), newParts("old123", "!pass"));
|
||||||
|
|
||||||
// then
|
// then
|
||||||
verify(messagesMock).send(sender, MessageKey.PASSWORD_MATCH_ERROR);
|
verify(messagesMock).send(sender, MessageKey.PASSWORD_MATCH_ERROR);
|
||||||
@ -101,7 +102,7 @@ public class ChangePasswordCommandTest {
|
|||||||
ChangePasswordCommand command = new ChangePasswordCommand();
|
ChangePasswordCommand command = new ChangePasswordCommand();
|
||||||
|
|
||||||
// when
|
// when
|
||||||
command.executeCommand(sender, new CommandParts(), newParts("old_", "Tester"));
|
command.executeCommand(sender, newParts(), newParts("old_", "Tester"));
|
||||||
|
|
||||||
// then
|
// then
|
||||||
verify(messagesMock).send(sender, MessageKey.PASSWORD_IS_USERNAME_ERROR);
|
verify(messagesMock).send(sender, MessageKey.PASSWORD_IS_USERNAME_ERROR);
|
||||||
@ -116,7 +117,7 @@ public class ChangePasswordCommandTest {
|
|||||||
Settings.passwordMaxLength = 3;
|
Settings.passwordMaxLength = 3;
|
||||||
|
|
||||||
// when
|
// when
|
||||||
command.executeCommand(sender, new CommandParts(), newParts("12", "test"));
|
command.executeCommand(sender, newParts(), newParts("12", "test"));
|
||||||
|
|
||||||
// then
|
// then
|
||||||
verify(messagesMock).send(sender, MessageKey.INVALID_PASSWORD_LENGTH);
|
verify(messagesMock).send(sender, MessageKey.INVALID_PASSWORD_LENGTH);
|
||||||
@ -131,7 +132,7 @@ public class ChangePasswordCommandTest {
|
|||||||
Settings.getPasswordMinLen = 7;
|
Settings.getPasswordMinLen = 7;
|
||||||
|
|
||||||
// when
|
// when
|
||||||
command.executeCommand(sender, new CommandParts(), newParts("oldverylongpassword", "tester"));
|
command.executeCommand(sender, newParts(), newParts("oldverylongpassword", "tester"));
|
||||||
|
|
||||||
// then
|
// then
|
||||||
verify(messagesMock).send(sender, MessageKey.INVALID_PASSWORD_LENGTH);
|
verify(messagesMock).send(sender, MessageKey.INVALID_PASSWORD_LENGTH);
|
||||||
@ -146,7 +147,7 @@ public class ChangePasswordCommandTest {
|
|||||||
Settings.unsafePasswords = asList("test", "abc123");
|
Settings.unsafePasswords = asList("test", "abc123");
|
||||||
|
|
||||||
// when
|
// when
|
||||||
command.executeCommand(sender, new CommandParts(), newParts("oldpw", "abc123"));
|
command.executeCommand(sender, newParts(), newParts("oldpw", "abc123"));
|
||||||
|
|
||||||
// then
|
// then
|
||||||
verify(messagesMock).send(sender, MessageKey.PASSWORD_UNSAFE_ERROR);
|
verify(messagesMock).send(sender, MessageKey.PASSWORD_UNSAFE_ERROR);
|
||||||
@ -160,7 +161,7 @@ public class ChangePasswordCommandTest {
|
|||||||
ChangePasswordCommand command = new ChangePasswordCommand();
|
ChangePasswordCommand command = new ChangePasswordCommand();
|
||||||
|
|
||||||
// when
|
// when
|
||||||
command.executeCommand(sender, new CommandParts(), newParts("abc123", "abc123"));
|
command.executeCommand(sender, newParts(), newParts("abc123", "abc123"));
|
||||||
|
|
||||||
// then
|
// then
|
||||||
verify(messagesMock, never()).send(eq(sender), any(MessageKey.class));
|
verify(messagesMock, never()).send(eq(sender), any(MessageKey.class));
|
||||||
|
@ -11,6 +11,7 @@ import org.junit.Before;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
import static org.mockito.Mockito.never;
|
import static org.mockito.Mockito.never;
|
||||||
@ -40,7 +41,7 @@ public class AddEmailCommandTest {
|
|||||||
AddEmailCommand command = new AddEmailCommand();
|
AddEmailCommand command = new AddEmailCommand();
|
||||||
|
|
||||||
// when
|
// when
|
||||||
command.executeCommand(sender, new CommandParts(), new CommandParts());
|
command.executeCommand(sender, newParts(), newParts());
|
||||||
|
|
||||||
// then
|
// then
|
||||||
verify(authMeMock, never()).getManagement();
|
verify(authMeMock, never()).getManagement();
|
||||||
@ -53,11 +54,15 @@ public class AddEmailCommandTest {
|
|||||||
AddEmailCommand command = new AddEmailCommand();
|
AddEmailCommand command = new AddEmailCommand();
|
||||||
|
|
||||||
// when
|
// when
|
||||||
command.executeCommand(sender, new CommandParts(),
|
command.executeCommand(sender, newParts(),
|
||||||
new CommandParts(Arrays.asList("mail@example", "other_example")));
|
new CommandParts(Arrays.asList("mail@example", "other_example")));
|
||||||
|
|
||||||
// then
|
// then
|
||||||
verify(authMeMock).getManagement();
|
verify(authMeMock).getManagement();
|
||||||
verify(managementMock).performAddEmail(sender, "mail@example", "other_example");
|
verify(managementMock).performAddEmail(sender, "mail@example", "other_example");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static CommandParts newParts() {
|
||||||
|
return new CommandParts(new ArrayList<String>());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,7 @@ import org.junit.Before;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
import static org.mockito.Mockito.never;
|
import static org.mockito.Mockito.never;
|
||||||
@ -40,7 +41,7 @@ public class ChangeEmailCommandTest {
|
|||||||
ChangeEmailCommand command = new ChangeEmailCommand();
|
ChangeEmailCommand command = new ChangeEmailCommand();
|
||||||
|
|
||||||
// when
|
// when
|
||||||
command.executeCommand(sender, new CommandParts(), new CommandParts());
|
command.executeCommand(sender, newParts(), newParts());
|
||||||
|
|
||||||
// then
|
// then
|
||||||
verify(authMeMock, never()).getManagement();
|
verify(authMeMock, never()).getManagement();
|
||||||
@ -53,11 +54,15 @@ public class ChangeEmailCommandTest {
|
|||||||
ChangeEmailCommand command = new ChangeEmailCommand();
|
ChangeEmailCommand command = new ChangeEmailCommand();
|
||||||
|
|
||||||
// when
|
// when
|
||||||
command.executeCommand(sender, new CommandParts(),
|
command.executeCommand(sender, newParts(),
|
||||||
new CommandParts(Arrays.asList("new.mail@example.org", "old_mail@example.org")));
|
new CommandParts(Arrays.asList("new.mail@example.org", "old_mail@example.org")));
|
||||||
|
|
||||||
// then
|
// then
|
||||||
verify(authMeMock).getManagement();
|
verify(authMeMock).getManagement();
|
||||||
verify(managementMock).performChangeEmail(sender, "new.mail@example.org", "old_mail@example.org");
|
verify(managementMock).performChangeEmail(sender, "new.mail@example.org", "old_mail@example.org");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static CommandParts newParts() {
|
||||||
|
return new CommandParts(new ArrayList<String>());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,8 @@ import org.junit.Ignore;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test for {@link RecoverEmailCommand}.
|
* Test for {@link RecoverEmailCommand}.
|
||||||
*/
|
*/
|
||||||
@ -27,7 +29,7 @@ public class RecoverEmailCommandTest {
|
|||||||
RecoverEmailCommand command = new RecoverEmailCommand();
|
RecoverEmailCommand command = new RecoverEmailCommand();
|
||||||
|
|
||||||
// when
|
// when
|
||||||
command.executeCommand(sender, new CommandParts(), new CommandParts());
|
command.executeCommand(sender, new CommandParts(Collections.EMPTY_LIST), new CommandParts(Collections.EMPTY_LIST));
|
||||||
|
|
||||||
// then
|
// then
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,8 @@ import org.junit.Before;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import static org.mockito.Matchers.*;
|
import static org.mockito.Matchers.*;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.never;
|
import static org.mockito.Mockito.never;
|
||||||
@ -38,7 +40,7 @@ public class LoginCommandTest {
|
|||||||
LoginCommand command = new LoginCommand();
|
LoginCommand command = new LoginCommand();
|
||||||
|
|
||||||
// when
|
// when
|
||||||
command.executeCommand(sender, new CommandParts(), new CommandParts());
|
command.executeCommand(sender, newParts(), newParts());
|
||||||
|
|
||||||
// then
|
// then
|
||||||
Mockito.verify(managementMock, never()).performLogin(any(Player.class), anyString(), anyBoolean());
|
Mockito.verify(managementMock, never()).performLogin(any(Player.class), anyString(), anyBoolean());
|
||||||
@ -51,7 +53,7 @@ public class LoginCommandTest {
|
|||||||
LoginCommand command = new LoginCommand();
|
LoginCommand command = new LoginCommand();
|
||||||
|
|
||||||
// when
|
// when
|
||||||
command.executeCommand(sender, new CommandParts(), new CommandParts("password"));
|
command.executeCommand(sender, newParts(), new CommandParts("password"));
|
||||||
|
|
||||||
// then
|
// then
|
||||||
Mockito.verify(managementMock).performLogin(eq(sender), eq("password"), eq(false));
|
Mockito.verify(managementMock).performLogin(eq(sender), eq("password"), eq(false));
|
||||||
@ -64,11 +66,15 @@ public class LoginCommandTest {
|
|||||||
LoginCommand command = new LoginCommand();
|
LoginCommand command = new LoginCommand();
|
||||||
|
|
||||||
// when
|
// when
|
||||||
command.executeCommand(sender, new CommandParts(), new CommandParts());
|
command.executeCommand(sender, newParts(), newParts());
|
||||||
|
|
||||||
// then
|
// then
|
||||||
// TODO ljacqu 20151121: May make sense to handle null password in LoginCommand instead of forwarding the call
|
// TODO ljacqu 20151121: May make sense to handle null password in LoginCommand instead of forwarding the call
|
||||||
String password = null;
|
String password = null;
|
||||||
Mockito.verify(managementMock).performLogin(eq(sender), eq(password), eq(false));
|
Mockito.verify(managementMock).performLogin(eq(sender), eq(password), eq(false));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static CommandParts newParts() {
|
||||||
|
return new CommandParts(new ArrayList<String>());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,8 @@ import org.junit.Before;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import static org.mockito.Matchers.any;
|
import static org.mockito.Matchers.any;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.never;
|
import static org.mockito.Mockito.never;
|
||||||
@ -40,7 +42,7 @@ public class LogoutCommandTest {
|
|||||||
LogoutCommand command = new LogoutCommand();
|
LogoutCommand command = new LogoutCommand();
|
||||||
|
|
||||||
// when
|
// when
|
||||||
command.executeCommand(sender, new CommandParts(), new CommandParts());
|
command.executeCommand(sender, new CommandParts(new ArrayList<String>()), new CommandParts(new ArrayList<String>()));
|
||||||
|
|
||||||
// then
|
// then
|
||||||
Mockito.verify(managementMock, never()).performLogout(any(Player.class));
|
Mockito.verify(managementMock, never()).performLogout(any(Player.class));
|
||||||
@ -53,7 +55,7 @@ public class LogoutCommandTest {
|
|||||||
LogoutCommand command = new LogoutCommand();
|
LogoutCommand command = new LogoutCommand();
|
||||||
|
|
||||||
// when
|
// when
|
||||||
command.executeCommand(sender, new CommandParts(), new CommandParts("password"));
|
command.executeCommand(sender, new CommandParts(new ArrayList<String>()), new CommandParts("password"));
|
||||||
|
|
||||||
// then
|
// then
|
||||||
Mockito.verify(managementMock).performLogout(sender);
|
Mockito.verify(managementMock).performLogout(sender);
|
||||||
|
@ -14,6 +14,8 @@ import org.junit.Test;
|
|||||||
import org.mockito.ArgumentCaptor;
|
import org.mockito.ArgumentCaptor;
|
||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.equalTo;
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.assertThat;
|
||||||
import static org.mockito.Matchers.any;
|
import static org.mockito.Matchers.any;
|
||||||
@ -48,7 +50,7 @@ public class RegisterCommandTest {
|
|||||||
ArgumentCaptor<String> messageCaptor = ArgumentCaptor.forClass(String.class);
|
ArgumentCaptor<String> messageCaptor = ArgumentCaptor.forClass(String.class);
|
||||||
|
|
||||||
// when
|
// when
|
||||||
command.executeCommand(sender, new CommandParts(), new CommandParts());
|
command.executeCommand(sender, newParts(), newParts());
|
||||||
|
|
||||||
// then
|
// then
|
||||||
verify(sender).sendMessage(messageCaptor.capture());
|
verify(sender).sendMessage(messageCaptor.capture());
|
||||||
@ -63,7 +65,7 @@ public class RegisterCommandTest {
|
|||||||
RegisterCommand command = new RegisterCommand();
|
RegisterCommand command = new RegisterCommand();
|
||||||
|
|
||||||
// when
|
// when
|
||||||
command.executeCommand(sender, new CommandParts(), new CommandParts());
|
command.executeCommand(sender, newParts(), newParts());
|
||||||
|
|
||||||
// then
|
// then
|
||||||
verify(messagesMock).send(sender, MessageKey.USAGE_REGISTER);
|
verify(messagesMock).send(sender, MessageKey.USAGE_REGISTER);
|
||||||
@ -77,9 +79,13 @@ public class RegisterCommandTest {
|
|||||||
RegisterCommand command = new RegisterCommand();
|
RegisterCommand command = new RegisterCommand();
|
||||||
|
|
||||||
// when
|
// when
|
||||||
command.executeCommand(sender, new CommandParts(), new CommandParts("password"));
|
command.executeCommand(sender, newParts(), new CommandParts("password"));
|
||||||
|
|
||||||
// then
|
// then
|
||||||
verify(managementMock).performRegister(sender, "password", "");
|
verify(managementMock).performRegister(sender, "password", "");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static CommandParts newParts() {
|
||||||
|
return new CommandParts(new ArrayList<String>());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,8 @@ import fr.xephi.authme.command.executable.authme.RegisterCommand;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import static org.bukkit.ChatColor.BOLD;
|
import static org.bukkit.ChatColor.BOLD;
|
||||||
import static org.bukkit.ChatColor.ITALIC;
|
import static org.bukkit.ChatColor.ITALIC;
|
||||||
import static org.bukkit.ChatColor.WHITE;
|
import static org.bukkit.ChatColor.WHITE;
|
||||||
@ -27,7 +29,7 @@ public class HelpSyntaxHelperTest {
|
|||||||
.build();
|
.build();
|
||||||
|
|
||||||
// when
|
// when
|
||||||
String result = HelpSyntaxHelper.getCommandSyntax(description, new CommandParts(), "", false);
|
String result = HelpSyntaxHelper.getCommandSyntax(description, newParts(), "", false);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
assertThat(result, equalTo(WHITE + "/authme register" + ITALIC + " [name]"));
|
assertThat(result, equalTo(WHITE + "/authme register" + ITALIC + " [name]"));
|
||||||
@ -41,7 +43,7 @@ public class HelpSyntaxHelperTest {
|
|||||||
.build();
|
.build();
|
||||||
|
|
||||||
// when
|
// when
|
||||||
String result = HelpSyntaxHelper.getCommandSyntax(description, new CommandParts(), null, false);
|
String result = HelpSyntaxHelper.getCommandSyntax(description, newParts(), null, false);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
assertThat(result, equalTo(WHITE + "/authme register" + ITALIC + " <test>"));
|
assertThat(result, equalTo(WHITE + "/authme register" + ITALIC + " <test>"));
|
||||||
@ -56,7 +58,7 @@ public class HelpSyntaxHelperTest {
|
|||||||
.build();
|
.build();
|
||||||
|
|
||||||
// when
|
// when
|
||||||
String result = HelpSyntaxHelper.getCommandSyntax(description, new CommandParts(), "", false);
|
String result = HelpSyntaxHelper.getCommandSyntax(description, newParts(), "", false);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
assertThat(result, equalTo(WHITE + "/authme register" + ITALIC + " [name]" + ITALIC + " <test>"));
|
assertThat(result, equalTo(WHITE + "/authme register" + ITALIC + " [name]" + ITALIC + " <test>"));
|
||||||
@ -71,7 +73,7 @@ public class HelpSyntaxHelperTest {
|
|||||||
.build();
|
.build();
|
||||||
|
|
||||||
// when
|
// when
|
||||||
String result = HelpSyntaxHelper.getCommandSyntax(description, new CommandParts(), "", true);
|
String result = HelpSyntaxHelper.getCommandSyntax(description, newParts(), "", true);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
assertThat(result, equalTo(WHITE + "/authme "
|
assertThat(result, equalTo(WHITE + "/authme "
|
||||||
@ -85,7 +87,7 @@ public class HelpSyntaxHelperTest {
|
|||||||
CommandDescription description = getDescriptionBuilder().build();
|
CommandDescription description = getDescriptionBuilder().build();
|
||||||
|
|
||||||
// when
|
// when
|
||||||
String result = HelpSyntaxHelper.getCommandSyntax(description, new CommandParts(), null, true);
|
String result = HelpSyntaxHelper.getCommandSyntax(description, newParts(), null, true);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
assertThat(result, equalTo(WHITE + "/authme " + YELLOW + BOLD + "register" + YELLOW));
|
assertThat(result, equalTo(WHITE + "/authme " + YELLOW + BOLD + "register" + YELLOW));
|
||||||
@ -99,12 +101,16 @@ public class HelpSyntaxHelperTest {
|
|||||||
.build();
|
.build();
|
||||||
|
|
||||||
// when
|
// when
|
||||||
String result = HelpSyntaxHelper.getCommandSyntax(description, new CommandParts(), "alt", false);
|
String result = HelpSyntaxHelper.getCommandSyntax(description, newParts(), "alt", false);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
assertThat(result, equalTo(WHITE + "/authme alt" + ITALIC + " [name]"));
|
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<String>());
|
||||||
|
}
|
||||||
|
|
||||||
private static CommandDescription.CommandBuilder getDescriptionBuilder() {
|
private static CommandDescription.CommandBuilder getDescriptionBuilder() {
|
||||||
CommandDescription base = CommandDescription.builder()
|
CommandDescription base = CommandDescription.builder()
|
||||||
|
102
src/test/java/fr/xephi/authme/util/CollectionUtilsTest.java
Normal file
102
src/test/java/fr/xephi/authme/util/CollectionUtilsTest.java
Normal file
@ -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<String> list = Arrays.asList("test", "1", "2", "3", "4");
|
||||||
|
|
||||||
|
// when
|
||||||
|
List<String> result = CollectionUtils.getRange(list, 0, 24);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(result, equalTo(list));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturnEmptyListForZeroCount() {
|
||||||
|
// given
|
||||||
|
List<String> list = Arrays.asList("test", "1", "2", "3", "4");
|
||||||
|
|
||||||
|
// when
|
||||||
|
List<String> result = CollectionUtils.getRange(list, 2, 0);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(result, empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturnEmptyListForTooHighStart() {
|
||||||
|
// given
|
||||||
|
List<String> list = Arrays.asList("test", "1", "2", "3", "4");
|
||||||
|
|
||||||
|
// when
|
||||||
|
List<String> result = CollectionUtils.getRange(list, 12, 2);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(result, empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturnSubList() {
|
||||||
|
// given
|
||||||
|
List<String> list = Arrays.asList("test", "1", "2", "3", "4");
|
||||||
|
|
||||||
|
// when
|
||||||
|
List<String> result = CollectionUtils.getRange(list, 1, 3);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(result, contains("1", "2", "3"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturnTillEnd() {
|
||||||
|
// given
|
||||||
|
List<String> list = Arrays.asList("test", "1", "2", "3", "4");
|
||||||
|
|
||||||
|
// when
|
||||||
|
List<String> result = CollectionUtils.getRange(list, 2, 3);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(result, contains("2", "3", "4"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldRemoveFirstTwo() {
|
||||||
|
// given
|
||||||
|
List<String> list = Arrays.asList("test", "1", "2", "3", "4");
|
||||||
|
|
||||||
|
// when
|
||||||
|
List<String> result = CollectionUtils.getRange(list, 2);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(result, contains("2", "3", "4"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldHandleNegativeStart() {
|
||||||
|
// given
|
||||||
|
List<String> list = Arrays.asList("test", "1", "2", "3", "4");
|
||||||
|
|
||||||
|
// when
|
||||||
|
List<String> result = CollectionUtils.getRange(list, -4);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(result, equalTo(list));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user