Remove BukkitService from CommandService, inject where needed instead

This commit is contained in:
ljacqu 2016-05-08 13:50:20 +02:00
parent 5e5836f167
commit 8e878d6a5a
20 changed files with 127 additions and 119 deletions

View File

@ -5,15 +5,10 @@ import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.output.Messages;
import fr.xephi.authme.settings.NewSetting;
import fr.xephi.authme.settings.domain.Property;
import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.util.ValidationService;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitScheduler;
import org.bukkit.scheduler.BukkitTask;
import javax.inject.Inject;
import java.util.Collection;
import java.util.List;
/**
@ -22,21 +17,16 @@ import java.util.List;
*/
public class CommandService {
@Inject
private BukkitScheduler scheduler;
@Inject
private Messages messages;
@Inject
private HelpProvider helpProvider;
@Inject
private CommandMapper commandMapper;
@SuppressWarnings("unused")
@Inject
private NewSetting settings;
@Inject
private ValidationService validationService;
@Inject
private BukkitService bukkitService;
/**
* Send a message to a player.
@ -70,18 +60,6 @@ public class CommandService {
return commandMapper.mapPartsToCommand(sender, commandParts);
}
/**
* Run the given task asynchronously with the Bukkit scheduler.
*
* @param task The task to run
* @return a BukkitTask that contains the id number
* @throws IllegalArgumentException if plugin is null
* @throws IllegalArgumentException if task is null
*/
public BukkitTask runTaskAsynchronously(Runnable task) {
return bukkitService.runTaskAsynchronously(task);
}
/**
* Output the help for a given command.
*
@ -146,16 +124,4 @@ public class CommandService {
return validationService.isEmailFreeForRegistration(email, sender);
}
public Player getPlayer(String name) {
return bukkitService.getPlayerExact(name);
}
public Collection<? extends Player> getOnlinePlayers() {
return bukkitService.getOnlinePlayers();
}
public BukkitService getBukkitService() {
return bukkitService;
}
}

View File

@ -5,7 +5,7 @@ import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.output.Messages;
import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.util.StringUtils;
import org.bukkit.command.CommandSender;
@ -19,8 +19,9 @@ public class AccountsCommand implements ExecutableCommand {
@Inject
private DataSource dataSource;
@Inject
private Messages messages;
private BukkitService bukkitService;
@Override
public void executeCommand(final CommandSender sender, List<String> arguments,
@ -29,18 +30,18 @@ public class AccountsCommand implements ExecutableCommand {
// Assumption: a player name cannot contain '.'
if (!playerName.contains(".")) {
commandService.runTaskAsynchronously(new Runnable() {
bukkitService.runTaskAsynchronously(new Runnable() {
@Override
public void run() {
PlayerAuth auth = dataSource.getAuth(playerName.toLowerCase());
if (auth == null) {
messages.send(sender, MessageKey.UNKNOWN_USER);
commandService.send(sender, MessageKey.UNKNOWN_USER);
return;
}
List<String> accountList = dataSource.getAllAuthsByIp(auth.getIp());
if (accountList.isEmpty()) {
messages.send(sender, MessageKey.USER_NOT_REGISTERED);
commandService.send(sender, MessageKey.USER_NOT_REGISTERED);
} else if (accountList.size() == 1) {
sender.sendMessage("[AuthMe] " + playerName + " is a single account player");
} else {
@ -49,7 +50,7 @@ public class AccountsCommand implements ExecutableCommand {
}
});
} else {
commandService.runTaskAsynchronously(new Runnable() {
bukkitService.runTaskAsynchronously(new Runnable() {
@Override
public void run() {
List<String> accountList = dataSource.getAllAuthsByIp(playerName);

View File

@ -9,6 +9,7 @@ import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.security.crypts.HashedPassword;
import fr.xephi.authme.util.BukkitService;
import org.bukkit.command.CommandSender;
import javax.inject.Inject;
@ -28,6 +29,9 @@ public class ChangePasswordAdminCommand implements ExecutableCommand {
@Inject
private DataSource dataSource;
@Inject
private BukkitService bukkitService;
@Override
public void executeCommand(final CommandSender sender, List<String> arguments,
final CommandService commandService) {
@ -44,7 +48,7 @@ public class ChangePasswordAdminCommand implements ExecutableCommand {
// Set the password
final String playerNameLowerCase = playerName.toLowerCase();
commandService.runTaskAsynchronously(new Runnable() {
bukkitService.runTaskAsynchronously(new Runnable() {
@Override
public void run() {

View File

@ -11,6 +11,7 @@ import fr.xephi.authme.converter.SqliteToSql;
import fr.xephi.authme.converter.vAuthConverter;
import fr.xephi.authme.converter.xAuthConverter;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.util.BukkitService;
import org.bukkit.command.CommandSender;
import javax.inject.Inject;
@ -21,6 +22,9 @@ public class ConverterCommand implements ExecutableCommand {
@Inject
private AuthMe authMe;
@Inject
private BukkitService bukkitService;
@Override
public void executeCommand(CommandSender sender, List<String> arguments, CommandService commandService) {
// Get the conversion job
@ -59,7 +63,7 @@ public class ConverterCommand implements ExecutableCommand {
}
// Run the convert job
commandService.runTaskAsynchronously(converter);
bukkitService.runTaskAsynchronously(converter);
// Show a status message
sender.sendMessage("[AuthMe] Successfully converted from " + jobType.getName());

View File

@ -4,6 +4,7 @@ import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.process.Management;
import fr.xephi.authme.util.BukkitService;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@ -23,12 +24,15 @@ public class ForceLoginCommand implements ExecutableCommand {
@Inject
private Management management;
@Inject
private BukkitService bukkitService;
@Override
public void executeCommand(CommandSender sender, List<String> arguments, CommandService commandService) {
// Get the player query
String playerName = arguments.isEmpty() ? sender.getName() : arguments.get(0);
Player player = commandService.getPlayer(playerName);
Player player = bukkitService.getPlayerExact(playerName);
if (player == null || !player.isOnline()) {
sender.sendMessage("Player needs to be online!");
} else if (!permissionsManager.hasPermission(player, CAN_LOGIN_BE_FORCED)) {

View File

@ -2,19 +2,24 @@ package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.util.BukkitService;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import javax.inject.Inject;
import java.util.List;
public class GetIpCommand implements ExecutableCommand {
@Inject
private BukkitService bukkitService;
@Override
public void executeCommand(CommandSender sender, List<String> arguments, CommandService commandService) {
// Get the player query
String playerName = arguments.get(0);
Player player = commandService.getPlayer(playerName);
Player player = bukkitService.getPlayerExact(playerName);
if (player == null) {
sender.sendMessage("The player is not online");
return;

View File

@ -6,6 +6,7 @@ import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.hooks.PluginHooks;
import fr.xephi.authme.settings.properties.PurgeSettings;
import fr.xephi.authme.util.BukkitService;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
@ -28,11 +29,14 @@ public class PurgeBannedPlayersCommand implements ExecutableCommand {
@Inject
private AuthMe plugin;
@Inject
private BukkitService bukkitService;
@Override
public void executeCommand(CommandSender sender, List<String> arguments, CommandService commandService) {
// Get the list of banned players
List<String> bannedPlayers = new ArrayList<>();
for (OfflinePlayer offlinePlayer : commandService.getBukkitService().getBannedPlayers()) {
for (OfflinePlayer offlinePlayer : bukkitService.getBannedPlayers()) {
bannedPlayers.add(offlinePlayer.getName().toLowerCase());
}

View File

@ -8,6 +8,7 @@ import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.security.crypts.HashedPassword;
import fr.xephi.authme.util.BukkitService;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@ -25,6 +26,9 @@ public class RegisterAdminCommand implements ExecutableCommand {
@Inject
private DataSource dataSource;
@Inject
private BukkitService bukkitService;
@Override
public void executeCommand(final CommandSender sender, List<String> arguments,
final CommandService commandService) {
@ -40,7 +44,7 @@ public class RegisterAdminCommand implements ExecutableCommand {
return;
}
commandService.runTaskAsynchronously(new Runnable() {
bukkitService.runTaskAsynchronously(new Runnable() {
@Override
public void run() {
@ -63,9 +67,9 @@ public class RegisterAdminCommand implements ExecutableCommand {
commandService.send(sender, MessageKey.REGISTER_SUCCESS);
ConsoleLogger.info(sender.getName() + " registered " + playerName);
final Player player = commandService.getPlayer(playerName);
final Player player = bukkitService.getPlayerExact(playerName);
if (player != null) {
commandService.getBukkitService().scheduleSyncDelayedTask(new Runnable() {
bukkitService.scheduleSyncDelayedTask(new Runnable() {
@Override
public void run() {
player.kickPlayer("An admin just registered you, please log in again");

View File

@ -6,6 +6,7 @@ import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.util.BukkitService;
import org.bukkit.command.CommandSender;
import javax.inject.Inject;
@ -22,6 +23,9 @@ public class SetEmailCommand implements ExecutableCommand {
@Inject
private PlayerCache playerCache;
@Inject
private BukkitService bukkitService;
@Override
public void executeCommand(final CommandSender sender, List<String> arguments,
final CommandService commandService) {
@ -35,7 +39,7 @@ public class SetEmailCommand implements ExecutableCommand {
return;
}
commandService.runTaskAsynchronously(new Runnable() {
bukkitService.runTaskAsynchronously(new Runnable() {
@Override
public void run() {
// Validate the user

View File

@ -39,6 +39,9 @@ public class UnregisterAdminCommand implements ExecutableCommand {
@Inject
private AuthMe authMe;
@Inject
private BukkitService bukkitService;
@Override
public void executeCommand(final CommandSender sender, List<String> arguments, CommandService commandService) {
// Get the player name
@ -58,7 +61,7 @@ public class UnregisterAdminCommand implements ExecutableCommand {
}
// Unregister the player
Player target = commandService.getPlayer(playerNameLowerCase);
Player target = bukkitService.getPlayerExact(playerNameLowerCase);
playerCache.removePlayer(playerNameLowerCase);
Utils.setGroup(target, Utils.GroupType.UNREGISTERED);
if (target != null && target.isOnline()) {
@ -82,7 +85,6 @@ public class UnregisterAdminCommand implements ExecutableCommand {
* @param service the command service
*/
private void applyUnregisteredEffectsAndTasks(Player target, CommandService service) {
final BukkitService bukkitService = service.getBukkitService();
final String playerNameLowerCase = target.getName().toLowerCase();
Utils.teleportToSpawn(target);
@ -94,7 +96,7 @@ public class UnregisterAdminCommand implements ExecutableCommand {
LimboCache.getInstance().getLimboPlayer(playerNameLowerCase).setTimeoutTask(id);
}
LimboCache.getInstance().getLimboPlayer(playerNameLowerCase).setMessageTask(
bukkitService.runTask(new MessageTask(service.getBukkitService(), authMe.getMessages(),
bukkitService.runTask(new MessageTask(bukkitService, authMe.getMessages(),
playerNameLowerCase, MessageKey.REGISTER_MESSAGE, interval)));
if (service.getProperty(RegistrationSettings.APPLY_BLIND_EFFECT)) {

View File

@ -3,10 +3,12 @@ package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.util.BukkitService;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import javax.inject.Inject;
import java.util.Collection;
import java.util.List;
@ -14,6 +16,9 @@ import static fr.xephi.authme.settings.properties.PluginSettings.HELP_HEADER;
public class VersionCommand implements ExecutableCommand {
@Inject
private BukkitService bukkitService;
@Override
public void executeCommand(CommandSender sender, List<String> arguments, CommandService commandService) {
// Show some version info
@ -22,7 +27,7 @@ public class VersionCommand implements ExecutableCommand {
sender.sendMessage(ChatColor.GOLD + "Version: " + ChatColor.WHITE + AuthMe.getPluginName()
+ " v" + AuthMe.getPluginVersion() + ChatColor.GRAY + " (build: " + AuthMe.getPluginBuildNumber() + ")");
sender.sendMessage(ChatColor.GOLD + "Developers:");
Collection<? extends Player> onlinePlayers = commandService.getOnlinePlayers();
Collection<? extends Player> onlinePlayers = bukkitService.getOnlinePlayers();
printDeveloper(sender, "Xephi", "xephi59", "Lead Developer", onlinePlayers);
printDeveloper(sender, "DNx5", "DNx5", "Developer", onlinePlayers);
printDeveloper(sender, "games647", "games647", "Developer", onlinePlayers);

View File

@ -6,6 +6,7 @@ import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.PlayerCommand;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.task.ChangePasswordTask;
import fr.xephi.authme.util.BukkitService;
import org.bukkit.entity.Player;
import javax.inject.Inject;
@ -19,6 +20,9 @@ public class ChangePasswordCommand extends PlayerCommand {
@Inject
private PlayerCache playerCache;
@Inject
private BukkitService bukkitService;
@Override
public void runCommand(Player player, List<String> arguments, CommandService commandService) {
String oldPassword = arguments.get(0);
@ -39,6 +43,6 @@ public class ChangePasswordCommand extends PlayerCommand {
AuthMe plugin = AuthMe.getInstance();
// TODO ljacqu 20160117: Call async task via Management
commandService.runTaskAsynchronously(new ChangePasswordTask(plugin, player, oldPassword, newPassword));
bukkitService.runTaskAsynchronously(new ChangePasswordTask(plugin, player, oldPassword, newPassword));
}
}

View File

@ -1,6 +1,5 @@
package fr.xephi.authme;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.util.BukkitService;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
@ -60,13 +59,13 @@ public final class TestHelper {
}
/**
* Execute a {@link Runnable} passed to a mock's {@link CommandService#runTaskAsynchronously} method.
* Execute a {@link Runnable} passed to a mock's {@link BukkitService#runTaskAsynchronously} method.
* Note that calling this method expects that there be a runnable sent to the method and will fail
* otherwise.
*
* @param service The mock service
*/
public static void runInnerRunnable(CommandService service) {
public static void runInnerRunnable(BukkitService service) {
ArgumentCaptor<Runnable> captor = ArgumentCaptor.forClass(Runnable.class);
verify(service).runTaskAsynchronously(captor.capture());
Runnable runnable = captor.getValue();

View File

@ -189,19 +189,4 @@ public class CommandServiceTest {
verify(validationService).isEmailFreeForRegistration(email, sender);
}
@Test
public void shouldGetPlayer() {
// given
String playerName = "_tester";
Player player = mock(Player.class);
given(bukkitService.getPlayerExact(playerName)).willReturn(player);
// when
Player result = commandService.getPlayer(playerName);
// then
assertThat(result, equalTo(player));
verify(bukkitService).getPlayerExact(playerName);
}
}

View File

@ -4,7 +4,7 @@ import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.output.Messages;
import fr.xephi.authme.util.BukkitService;
import org.bukkit.command.CommandSender;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -42,7 +42,7 @@ public class AccountsCommandTest {
@Mock
private DataSource dataSource;
@Mock
private Messages messages;
private BukkitService bukkitService;
@Test
public void shouldGetAccountsOfCurrentUser() {
@ -54,7 +54,7 @@ public class AccountsCommandTest {
// when
command.executeCommand(sender, arguments, service);
runInnerRunnable(service);
runInnerRunnable(bukkitService);
// then
String[] messages = getMessagesSentToSender(sender, 2);
@ -70,10 +70,10 @@ public class AccountsCommandTest {
// when
command.executeCommand(sender, arguments, service);
runInnerRunnable(service);
runInnerRunnable(bukkitService);
// then
verify(messages).send(sender, MessageKey.UNKNOWN_USER);
verify(service).send(sender, MessageKey.UNKNOWN_USER);
verify(sender, never()).sendMessage(anyString());
}
@ -86,10 +86,10 @@ public class AccountsCommandTest {
// when
command.executeCommand(sender, arguments, service);
runInnerRunnable(service);
runInnerRunnable(bukkitService);
// then
verify(messages).send(sender, MessageKey.USER_NOT_REGISTERED);
verify(service).send(sender, MessageKey.USER_NOT_REGISTERED);
verify(sender, never()).sendMessage(anyString());
}
@ -102,7 +102,7 @@ public class AccountsCommandTest {
// when
command.executeCommand(sender, arguments, service);
runInnerRunnable(service);
runInnerRunnable(bukkitService);
// then
String[] messages = getMessagesSentToSender(sender, 1);
@ -120,7 +120,7 @@ public class AccountsCommandTest {
// when
command.executeCommand(sender, arguments, service);
runInnerRunnable(service);
runInnerRunnable(bukkitService);
// then
String[] messages = getMessagesSentToSender(sender, 1);
@ -135,7 +135,7 @@ public class AccountsCommandTest {
// when
command.executeCommand(sender, arguments, service);
runInnerRunnable(service);
runInnerRunnable(bukkitService);
// then
String[] messages = getMessagesSentToSender(sender, 1);
@ -150,7 +150,7 @@ public class AccountsCommandTest {
// when
command.executeCommand(sender, arguments, service);
runInnerRunnable(service);
runInnerRunnable(bukkitService);
// then
String[] messages = getMessagesSentToSender(sender, 2);

View File

@ -8,6 +8,7 @@ import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.security.PasswordSecurity;
import fr.xephi.authme.security.crypts.HashedPassword;
import fr.xephi.authme.util.BukkitService;
import org.bukkit.command.CommandSender;
import org.junit.BeforeClass;
import org.junit.Test;
@ -47,6 +48,9 @@ public class ChangePasswordAdminCommandTest {
@Mock
private PlayerCache playerCache;
@Mock
private BukkitService bukkitService;
@BeforeClass
public static void setUpLogger() {
TestHelper.setupLogger();
@ -77,7 +81,7 @@ public class ChangePasswordAdminCommandTest {
// when
command.executeCommand(sender, Arrays.asList(player, "password"), service);
runInnerRunnable(service);
runInnerRunnable(bukkitService);
// then
verify(service).send(sender, MessageKey.UNKNOWN_USER);
@ -101,7 +105,7 @@ public class ChangePasswordAdminCommandTest {
// when
command.executeCommand(sender, Arrays.asList(player, password), service);
runInnerRunnable(service);
runInnerRunnable(bukkitService);
// then
verify(service).validatePassword(password, player);
@ -128,7 +132,7 @@ public class ChangePasswordAdminCommandTest {
// when
command.executeCommand(sender, Arrays.asList(player, password), service);
runInnerRunnable(service);
runInnerRunnable(bukkitService);
// then
verify(service).validatePassword(password, player);
@ -154,7 +158,7 @@ public class ChangePasswordAdminCommandTest {
// when
command.executeCommand(sender, Arrays.asList(player, password), service);
runInnerRunnable(service);
runInnerRunnable(bukkitService);
// then
verify(service).validatePassword(password, player);

View File

@ -1,10 +1,10 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.permission.PermissionsManager;
import fr.xephi.authme.permission.PlayerPermission;
import fr.xephi.authme.process.Management;
import fr.xephi.authme.util.BukkitService;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.junit.Test;
@ -39,6 +39,9 @@ public class ForceLoginCommandTest {
@Mock
private PermissionsManager permissionsManager;
@Mock
private BukkitService bukkitService;
@Mock
private CommandService commandService;
@ -48,14 +51,14 @@ public class ForceLoginCommandTest {
// given
String playerName = "Bobby";
Player player = mockPlayer(false, playerName);
given(commandService.getPlayer(playerName)).willReturn(player);
given(bukkitService.getPlayerExact(playerName)).willReturn(player);
CommandSender sender = mock(CommandSender.class);
// when
command.executeCommand(sender, Collections.singletonList(playerName), commandService);
// then
verify(commandService).getPlayer(playerName);
verify(bukkitService).getPlayerExact(playerName);
verify(sender).sendMessage(argThat(equalTo("Player needs to be online!")));
verifyZeroInteractions(management);
}
@ -64,15 +67,14 @@ public class ForceLoginCommandTest {
public void shouldRejectInexistentPlayer() {
// given
String playerName = "us3rname01";
given(commandService.getPlayer(playerName)).willReturn(null);
given(bukkitService.getPlayerExact(playerName)).willReturn(null);
CommandSender sender = mock(CommandSender.class);
ExecutableCommand command = new ForceLoginCommand();
// when
command.executeCommand(sender, Collections.singletonList(playerName), commandService);
// then
verify(commandService).getPlayer(playerName);
verify(bukkitService).getPlayerExact(playerName);
verify(sender).sendMessage(argThat(equalTo("Player needs to be online!")));
verifyZeroInteractions(management);
}
@ -82,7 +84,7 @@ public class ForceLoginCommandTest {
// given
String playerName = "testTest";
Player player = mockPlayer(true, playerName);
given(commandService.getPlayer(playerName)).willReturn(player);
given(bukkitService.getPlayerExact(playerName)).willReturn(player);
given(permissionsManager.hasPermission(player, PlayerPermission.CAN_LOGIN_BE_FORCED)).willReturn(false);
CommandSender sender = mock(CommandSender.class);
@ -90,7 +92,7 @@ public class ForceLoginCommandTest {
command.executeCommand(sender, Collections.singletonList(playerName), commandService);
// then
verify(commandService).getPlayer(playerName);
verify(bukkitService).getPlayerExact(playerName);
verify(sender).sendMessage(argThat(containsString("You cannot force login the player")));
verifyZeroInteractions(management);
}
@ -100,7 +102,7 @@ public class ForceLoginCommandTest {
// given
String playerName = "tester23";
Player player = mockPlayer(true, playerName);
given(commandService.getPlayer(playerName)).willReturn(player);
given(bukkitService.getPlayerExact(playerName)).willReturn(player);
given(permissionsManager.hasPermission(player, PlayerPermission.CAN_LOGIN_BE_FORCED)).willReturn(true);
CommandSender sender = mock(CommandSender.class);
@ -108,7 +110,7 @@ public class ForceLoginCommandTest {
command.executeCommand(sender, Collections.singletonList(playerName), commandService);
// then
verify(commandService).getPlayer(playerName);
verify(bukkitService).getPlayerExact(playerName);
verify(management).performLogin(eq(player), anyString(), eq(true));
}
@ -117,7 +119,7 @@ public class ForceLoginCommandTest {
// given
String senderName = "tester23";
Player player = mockPlayer(true, senderName);
given(commandService.getPlayer(senderName)).willReturn(player);
given(bukkitService.getPlayerExact(senderName)).willReturn(player);
given(permissionsManager.hasPermission(player, PlayerPermission.CAN_LOGIN_BE_FORCED)).willReturn(true);
CommandSender sender = mock(CommandSender.class);
given(sender.getName()).willReturn(senderName);
@ -126,7 +128,7 @@ public class ForceLoginCommandTest {
command.executeCommand(sender, Collections.<String>emptyList(), commandService);
// then
verify(commandService).getPlayer(senderName);
verify(bukkitService).getPlayerExact(senderName);
verify(management).performLogin(eq(player), anyString(), eq(true));
}

View File

@ -1,11 +1,12 @@
package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.util.BukkitService;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
@ -27,22 +28,28 @@ import static org.mockito.Mockito.verify;
@RunWith(MockitoJUnitRunner.class)
public class GetIpCommandTest {
@InjectMocks
private GetIpCommand command;
@Mock
private CommandService commandService;
@Mock
private BukkitService bukkitService;
@Mock
private CommandSender sender;
@Test
public void shouldGetIpOfPlayer() {
// given
given(commandService.getPlayer(anyString())).willReturn(null);
ExecutableCommand command = new GetIpCommand();
given(bukkitService.getPlayerExact(anyString())).willReturn(null);
// when
command.executeCommand(sender, Collections.singletonList("Testt"), commandService);
// then
verify(commandService).getPlayer("Testt");
verify(bukkitService).getPlayerExact("Testt");
verify(sender).sendMessage(argThat(containsString("not online")));
}
@ -52,14 +59,13 @@ public class GetIpCommandTest {
String playerName = "charlie";
String ip = "123.34.56.88";
Player player = mockPlayer(playerName, ip);
given(commandService.getPlayer(playerName)).willReturn(player);
ExecutableCommand command = new GetIpCommand();
given(bukkitService.getPlayerExact(playerName)).willReturn(player);
// when
command.executeCommand(sender, Collections.singletonList(playerName), commandService);
// then
verify(commandService).getPlayer(playerName);
verify(bukkitService).getPlayerExact(playerName);
verify(sender).sendMessage(argThat(allOf(containsString(playerName), containsString(ip))));
}

View File

@ -42,15 +42,18 @@ public class RegisterAdminCommandTest {
@Mock
private PasswordSecurity passwordSecurity;
@Mock
private DataSource dataSource;
@Mock
private BukkitService bukkitService;
@Mock
private CommandSender sender;
@Mock
private CommandService commandService;
@Mock
private DataSource dataSource;
@BeforeClass
public static void setUpLogger() {
TestHelper.setupLogger();
@ -69,7 +72,7 @@ public class RegisterAdminCommandTest {
// then
verify(commandService).validatePassword(password, user);
verify(commandService).send(sender, MessageKey.INVALID_PASSWORD_LENGTH);
verify(commandService, never()).runTaskAsynchronously(any(Runnable.class));
verify(bukkitService, never()).runTaskAsynchronously(any(Runnable.class));
}
@Test
@ -82,7 +85,7 @@ public class RegisterAdminCommandTest {
// when
command.executeCommand(sender, Arrays.asList(user, password), commandService);
TestHelper.runInnerRunnable(commandService);
TestHelper.runInnerRunnable(bukkitService);
// then
verify(commandService).validatePassword(password, user);
@ -103,7 +106,7 @@ public class RegisterAdminCommandTest {
// when
command.executeCommand(sender, Arrays.asList(user, password), commandService);
TestHelper.runInnerRunnable(commandService);
TestHelper.runInnerRunnable(bukkitService);
// then
verify(commandService).validatePassword(password, user);
@ -123,11 +126,11 @@ public class RegisterAdminCommandTest {
given(dataSource.saveAuth(any(PlayerAuth.class))).willReturn(true);
HashedPassword hashedPassword = new HashedPassword("$aea2345EW235dfsa@#R%987048");
given(passwordSecurity.computeHash(password, user)).willReturn(hashedPassword);
given(commandService.getPlayer(user)).willReturn(null);
given(bukkitService.getPlayerExact(user)).willReturn(null);
// when
command.executeCommand(sender, Arrays.asList(user, password), commandService);
TestHelper.runInnerRunnable(commandService);
TestHelper.runInnerRunnable(bukkitService);
// then
verify(commandService).validatePassword(password, user);
@ -149,13 +152,11 @@ public class RegisterAdminCommandTest {
HashedPassword hashedPassword = new HashedPassword("$aea2345EW235dfsa@#R%987048");
given(passwordSecurity.computeHash(password, user)).willReturn(hashedPassword);
Player player = mock(Player.class);
given(commandService.getPlayer(user)).willReturn(player);
BukkitService bukkitService = mock(BukkitService.class);
given(commandService.getBukkitService()).willReturn(bukkitService);
given(bukkitService.getPlayerExact(user)).willReturn(player);
// when
command.executeCommand(sender, Arrays.asList(user, password), commandService);
TestHelper.runInnerRunnable(commandService);
TestHelper.runInnerRunnable(bukkitService);
runSyncDelayedTask(bukkitService);
// then

View File

@ -7,6 +7,7 @@ import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.task.ChangePasswordTask;
import fr.xephi.authme.util.BukkitService;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@ -49,6 +50,9 @@ public class ChangePasswordCommandTest {
@Mock
private CommandService commandService;
@Mock
private BukkitService bukkitService;
@Before
public void setSettings() {
when(commandService.getProperty(SecuritySettings.MIN_PASSWORD_LENGTH)).thenReturn(2);
@ -109,7 +113,7 @@ public class ChangePasswordCommandTest {
verify(commandService).validatePassword("abc123", "parker");
verify(commandService, never()).send(eq(sender), any(MessageKey.class));
ArgumentCaptor<ChangePasswordTask> taskCaptor = ArgumentCaptor.forClass(ChangePasswordTask.class);
verify(commandService).runTaskAsynchronously(taskCaptor.capture());
verify(bukkitService).runTaskAsynchronously(taskCaptor.capture());
ChangePasswordTask task = taskCaptor.getValue();
assertThat((String) ReflectionTestUtils.getFieldValue(ChangePasswordTask.class, task, "newPassword"),
equalTo("abc123"));