Change to new HelpProvider, adjust classes to new methods

This commit is contained in:
ljacqu
2015-12-13 10:12:54 +01:00
parent 01cba2a508
commit c48c56f08c
18 changed files with 117 additions and 512 deletions
@@ -76,7 +76,7 @@ public class CommandInitializerTest {
BiConsumer connectionTester = new BiConsumer() {
@Override
public void accept(CommandDescription command, int depth) {
if (command.hasChildren()) {
if (!command.getChildren().isEmpty()) {
for (CommandDescription child : command.getChildren()) {
assertThat(command.equals(child.getParent()), equalTo(true));
}
@@ -222,7 +222,7 @@ public class CommandInitializerTest {
public void accept(CommandDescription command, int depth) {
// Fail if the command has children and has arguments at the same time
// Exception: If the parent only has one child defining the help label, it is acceptable
if (command.hasChildren() && command.hasArguments()
if (!command.getChildren().isEmpty() && !command.getArguments().isEmpty()
&& (command.getChildren().size() != 1 || !command.getChildren().get(0).hasLabel("help"))) {
fail("Parent command (labels='" + command.getLabels() + "') should not have any arguments");
}
@@ -280,7 +280,7 @@ public class CommandInitializerTest {
private static void walkThroughCommands(Collection<CommandDescription> commands, BiConsumer consumer, int depth) {
for (CommandDescription command : commands) {
consumer.accept(command, depth);
if (command.hasChildren()) {
if (!command.getChildren().isEmpty()) {
walkThroughCommands(command.getChildren(), consumer, depth + 1);
}
}
@@ -14,7 +14,7 @@ import org.junit.Ignore;
import org.junit.Test;
import org.mockito.Mockito;
import java.util.Collections;
import java.util.ArrayList;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
@@ -41,10 +41,9 @@ public class CaptchaCommandTest {
ExecutableCommand command = new CaptchaCommand();
// when
boolean result = command.executeCommand(sender, new CommandParts(Collections.EMPTY_LIST), new CommandParts(Collections.EMPTY_LIST));
command.executeCommand(sender, new ArrayList<String>());
// then
assertThat(result, equalTo(true));
assertThat(wrapperMock.wasMockCalled(AuthMe.class), equalTo(false));
assertThat(wrapperMock.wasMockCalled(Messages.class), equalTo(false));
}
@@ -57,10 +56,9 @@ public class CaptchaCommandTest {
ExecutableCommand command = new CaptchaCommand();
// when
boolean result = command.executeCommand(player, new CommandParts(Collections.EMPTY_LIST), new CommandParts(Collections.EMPTY_LIST));
command.executeCommand(player, new ArrayList<String>());
// then
assertThat(result, equalTo(true));
verify(wrapperMock.getMessages()).send(player, MessageKey.USAGE_LOGIN);
}
@@ -16,14 +16,19 @@ import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import static java.util.Arrays.asList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* Test for {@link ChangePasswordCommand}.
@@ -52,13 +57,11 @@ public class ChangePasswordCommandTest {
// given
CommandSender sender = mock(BlockCommandSender.class);
ChangePasswordCommand command = new ChangePasswordCommand();
CommandParts arguments = mock(CommandParts.class);
// when
command.executeCommand(sender, newParts(), arguments);
command.executeCommand(sender, new ArrayList<String>());
// then
verify(arguments, never()).get(anyInt());
assertThat(wrapperMock.wasMockCalled(Server.class), equalTo(false));
}
@@ -69,7 +72,7 @@ public class ChangePasswordCommandTest {
ChangePasswordCommand command = new ChangePasswordCommand();
// when
command.executeCommand(sender, newParts(), new CommandParts("pass"));
command.executeCommand(sender, Collections.singletonList("pass"));
// then
verify(messagesMock).send(sender, MessageKey.NOT_LOGGED_IN);
@@ -83,7 +86,7 @@ public class ChangePasswordCommandTest {
ChangePasswordCommand command = new ChangePasswordCommand();
// when
command.executeCommand(sender, newParts(), newParts("old123", "!pass"));
command.executeCommand(sender, Arrays.asList("old123", "!pass"));
// then
verify(messagesMock).send(sender, MessageKey.PASSWORD_MATCH_ERROR);
@@ -98,7 +101,7 @@ public class ChangePasswordCommandTest {
ChangePasswordCommand command = new ChangePasswordCommand();
// when
command.executeCommand(sender, newParts(), newParts("old_", "Tester"));
command.executeCommand(sender, Arrays.asList("old_", "Tester"));
// then
verify(messagesMock).send(sender, MessageKey.PASSWORD_IS_USERNAME_ERROR);
@@ -113,7 +116,7 @@ public class ChangePasswordCommandTest {
Settings.passwordMaxLength = 3;
// when
command.executeCommand(sender, newParts(), newParts("12", "test"));
command.executeCommand(sender, Arrays.asList("12", "test"));
// then
verify(messagesMock).send(sender, MessageKey.INVALID_PASSWORD_LENGTH);
@@ -128,7 +131,7 @@ public class ChangePasswordCommandTest {
Settings.getPasswordMinLen = 7;
// when
command.executeCommand(sender, newParts(), newParts("oldverylongpassword", "tester"));
command.executeCommand(sender, Arrays.asList("oldverylongpassword", "tester"));
// then
verify(messagesMock).send(sender, MessageKey.INVALID_PASSWORD_LENGTH);
@@ -143,7 +146,7 @@ public class ChangePasswordCommandTest {
Settings.unsafePasswords = asList("test", "abc123");
// when
command.executeCommand(sender, newParts(), newParts("oldpw", "abc123"));
command.executeCommand(sender, Arrays.asList("oldpw", "abc123"));
// then
verify(messagesMock).send(sender, MessageKey.PASSWORD_UNSAFE_ERROR);
@@ -157,7 +160,7 @@ public class ChangePasswordCommandTest {
ChangePasswordCommand command = new ChangePasswordCommand();
// when
command.executeCommand(sender, newParts(), newParts("abc123", "abc123"));
command.executeCommand(sender, Arrays.asList("abc123", "abc123"));
// then
verify(messagesMock, never()).send(eq(sender), any(MessageKey.class));
@@ -175,8 +178,4 @@ public class ChangePasswordCommandTest {
return player;
}
private static CommandParts newParts(String... parts) {
return new CommandParts(Arrays.asList(parts));
}
}
@@ -11,7 +11,6 @@ import org.junit.Test;
import org.mockito.Mockito;
import java.util.ArrayList;
import java.util.Arrays;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
@@ -40,7 +39,7 @@ public class AddEmailCommandTest {
AddEmailCommand command = new AddEmailCommand();
// when
command.executeCommand(sender, newParts(), newParts());
command.executeCommand(sender, new ArrayList<String>());
// then
verify(authMeMock, never()).getManagement();
@@ -53,15 +52,11 @@ public class AddEmailCommandTest {
AddEmailCommand command = new AddEmailCommand();
// when
command.executeCommand(sender, newParts(),
new CommandParts(Arrays.asList("mail@example", "other_example")));
command.executeCommand(sender, new ArrayList<String>());
// then
verify(authMeMock).getManagement();
verify(managementMock).performAddEmail(sender, "mail@example", "other_example");
}
private static CommandParts newParts() {
return new CommandParts(new ArrayList<String>());
}
}
@@ -40,7 +40,7 @@ public class ChangeEmailCommandTest {
ChangeEmailCommand command = new ChangeEmailCommand();
// when
command.executeCommand(sender, newParts(), newParts());
command.executeCommand(sender, new ArrayList<String>());
// then
verify(authMeMock, never()).getManagement();
@@ -53,15 +53,11 @@ public class ChangeEmailCommandTest {
ChangeEmailCommand command = new ChangeEmailCommand();
// when
command.executeCommand(sender, newParts(),
new CommandParts(Arrays.asList("new.mail@example.org", "old_mail@example.org")));
command.executeCommand(sender, 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<String>());
}
}
@@ -8,7 +8,7 @@ import org.junit.Ignore;
import org.junit.Test;
import org.mockito.Mockito;
import java.util.Collections;
import java.util.ArrayList;
/**
* Test for {@link RecoverEmailCommand}.
@@ -28,7 +28,7 @@ public class RecoverEmailCommandTest {
RecoverEmailCommand command = new RecoverEmailCommand();
// when
command.executeCommand(sender, new CommandParts(Collections.EMPTY_LIST), new CommandParts(Collections.EMPTY_LIST));
command.executeCommand(sender, new ArrayList<String>());
// then
}
@@ -11,6 +11,7 @@ import org.junit.Test;
import org.mockito.Mockito;
import java.util.ArrayList;
import java.util.Collections;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.mock;
@@ -39,7 +40,7 @@ public class LoginCommandTest {
LoginCommand command = new LoginCommand();
// when
command.executeCommand(sender, newParts(), newParts());
command.executeCommand(sender, new ArrayList<String>());
// then
Mockito.verify(managementMock, never()).performLogin(any(Player.class), anyString(), anyBoolean());
@@ -52,7 +53,7 @@ public class LoginCommandTest {
LoginCommand command = new LoginCommand();
// when
command.executeCommand(sender, newParts(), new CommandParts("password"));
command.executeCommand(sender, Collections.singletonList("password"));
// then
Mockito.verify(managementMock).performLogin(eq(sender), eq("password"), eq(false));
@@ -65,15 +66,12 @@ public class LoginCommandTest {
LoginCommand command = new LoginCommand();
// when
command.executeCommand(sender, newParts(), newParts());
command.executeCommand(sender, new ArrayList<String>());
// 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<String>());
}
}
@@ -12,6 +12,7 @@ import org.junit.Test;
import org.mockito.Mockito;
import java.util.ArrayList;
import java.util.Collections;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
@@ -41,7 +42,7 @@ public class LogoutCommandTest {
LogoutCommand command = new LogoutCommand();
// when
command.executeCommand(sender, new CommandParts(new ArrayList<String>()), new CommandParts(new ArrayList<String>()));
command.executeCommand(sender, new ArrayList<String>());
// then
Mockito.verify(managementMock, never()).performLogout(any(Player.class));
@@ -54,7 +55,7 @@ public class LogoutCommandTest {
LogoutCommand command = new LogoutCommand();
// when
command.executeCommand(sender, new CommandParts(new ArrayList<String>()), new CommandParts("password"));
command.executeCommand(sender, Collections.singletonList("password"));
// then
Mockito.verify(managementMock).performLogout(sender);
@@ -14,6 +14,7 @@ import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import java.util.ArrayList;
import java.util.Collections;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
@@ -49,7 +50,7 @@ public class RegisterCommandTest {
ArgumentCaptor<String> messageCaptor = ArgumentCaptor.forClass(String.class);
// when
command.executeCommand(sender, newParts(), newParts());
command.executeCommand(sender, new ArrayList<String>());
// then
verify(sender).sendMessage(messageCaptor.capture());
@@ -64,7 +65,7 @@ public class RegisterCommandTest {
RegisterCommand command = new RegisterCommand();
// when
command.executeCommand(sender, newParts(), newParts());
command.executeCommand(sender, new ArrayList<String>());
// then
verify(messagesMock).send(sender, MessageKey.USAGE_REGISTER);
@@ -78,13 +79,10 @@ public class RegisterCommandTest {
RegisterCommand command = new RegisterCommand();
// when
command.executeCommand(sender, newParts(), new CommandParts("password"));
command.executeCommand(sender, Collections.singletonList("password"));
// then
verify(managementMock).performRegister(sender, "password", "");
}
private static CommandParts newParts() {
return new CommandParts(new ArrayList<String>());
}
}
@@ -1,131 +0,0 @@
package fr.xephi.authme.command.help;
import fr.xephi.authme.command.CommandDescription;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.command.executable.authme.RegisterAdminCommand;
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;
import static org.bukkit.ChatColor.YELLOW;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
/**
* Test for {@link HelpSyntaxHelper}.
*/
public class HelpSyntaxHelperTest {
@Test
public void shouldFormatSimpleCommand() {
// given
CommandDescription description = getDescriptionBuilder()
.withArgument("name", "The name", true)
.build();
// when
String result = HelpSyntaxHelper.getCommandSyntax(description, newParts(), "", false);
// then
assertThat(result, equalTo(WHITE + "/authme register" + ITALIC + " [name]"));
}
@Test
public void shouldFormatSimpleCommandWithOptionalParam() {
// given
CommandDescription description = getDescriptionBuilder()
.withArgument("test", "", false)
.build();
// when
String result = HelpSyntaxHelper.getCommandSyntax(description, newParts(), null, false);
// then
assertThat(result, equalTo(WHITE + "/authme register" + ITALIC + " <test>"));
}
@Test
public void shouldFormatCommandWithMultipleParams() {
// given
CommandDescription description = getDescriptionBuilder()
.withArgument("name", "", true)
.withArgument("test", "", false)
.build();
// when
String result = HelpSyntaxHelper.getCommandSyntax(description, newParts(), "", false);
// then
assertThat(result, equalTo(WHITE + "/authme register" + ITALIC + " [name]" + ITALIC + " <test>"));
}
@Test
public void shouldHighlightCommandWithMultipleParams() {
// given
CommandDescription description = getDescriptionBuilder()
.withArgument("name", "", true)
.withArgument("test", "", false)
.build();
// when
String result = HelpSyntaxHelper.getCommandSyntax(description, newParts(), "", true);
// then
assertThat(result, equalTo(WHITE + "/authme "
+ YELLOW + BOLD + "register"
+ YELLOW + ITALIC + " [name]" + ITALIC + " <test>"));
}
@Test
public void shouldHighlightCommandWithNoParams() {
// given
CommandDescription description = getDescriptionBuilder().build();
// when
String result = HelpSyntaxHelper.getCommandSyntax(description, newParts(), null, true);
// then
assertThat(result, equalTo(WHITE + "/authme " + YELLOW + BOLD + "register" + YELLOW));
}
@Test
public void shouldFormatSimpleCommandWithAlternativeLabel() {
// given
CommandDescription description = getDescriptionBuilder()
.withArgument("name", "The name", true)
.build();
// when
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<String>());
}
private static CommandDescription.CommandBuilder getDescriptionBuilder() {
CommandDescription base = CommandDescription.builder()
.labels("authme")
.description("Base command")
.detailedDescription("AuthMe base command")
.parent(null)
.executableCommand(Mockito.mock(ExecutableCommand.class))
.build();
return CommandDescription.builder()
.executableCommand(Mockito.mock(RegisterAdminCommand.class))
.labels("register", "r")
.description("Register a player")
.detailedDescription("Register the specified player with the specified password.")
.parent(base)
.executableCommand(Mockito.mock(ExecutableCommand.class));
}
}