mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-12-24 01:27:35 +01:00
Refactor util for setting BukkitService mock behavior
- Move helper methods for setting BukkitService mock behavior into their own class - Change methods to use Mockito's answer instead of verification + argument capture -> calling the methods now belongs to the test setup (given clause) and allows the behavior to take effect more than once
This commit is contained in:
parent
4717dc148c
commit
1053440b15
@ -1,10 +1,8 @@
|
||||
package fr.xephi.authme;
|
||||
|
||||
import ch.jalu.configme.properties.Property;
|
||||
import fr.xephi.authme.service.BukkitService;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.io.File;
|
||||
@ -21,11 +19,8 @@ import java.nio.file.Paths;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* AuthMe test utilities.
|
||||
@ -78,76 +73,6 @@ public final class TestHelper {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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(BukkitService service) {
|
||||
ArgumentCaptor<Runnable> captor = ArgumentCaptor.forClass(Runnable.class);
|
||||
verify(service).runTaskAsynchronously(captor.capture());
|
||||
Runnable runnable = captor.getValue();
|
||||
runnable.run();
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a {@link Runnable} passed to a mock's {@link BukkitService#runTaskOptionallyAsync} 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 runOptionallyAsyncTask(BukkitService service) {
|
||||
ArgumentCaptor<Runnable> captor = ArgumentCaptor.forClass(Runnable.class);
|
||||
verify(service).runTaskOptionallyAsync(captor.capture());
|
||||
Runnable runnable = captor.getValue();
|
||||
runnable.run();
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a {@link Runnable} passed to a mock's {@link BukkitService#scheduleSyncDelayedTask(Runnable)}
|
||||
* 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 runSyncDelayedTask(BukkitService service) {
|
||||
ArgumentCaptor<Runnable> captor = ArgumentCaptor.forClass(Runnable.class);
|
||||
verify(service).scheduleSyncDelayedTask(captor.capture());
|
||||
Runnable runnable = captor.getValue();
|
||||
runnable.run();
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a {@link Runnable} passed to a mock's {@link BukkitService#scheduleSyncDelayedTask(Runnable, long)}
|
||||
* 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 runSyncDelayedTaskWithDelay(BukkitService service) {
|
||||
ArgumentCaptor<Runnable> captor = ArgumentCaptor.forClass(Runnable.class);
|
||||
verify(service).scheduleSyncDelayedTask(captor.capture(), anyLong());
|
||||
Runnable runnable = captor.getValue();
|
||||
runnable.run();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a BukkitService mock to run any Runnable it is passed to its method
|
||||
* {@link BukkitService#scheduleSyncTaskFromOptionallyAsyncTask}.
|
||||
*
|
||||
* @param bukkitService the mock to set behavior on
|
||||
*/
|
||||
public static void setBukkitServiceToRunOptionallyAsyncTasks(BukkitService bukkitService) {
|
||||
doAnswer(invocation -> {
|
||||
Runnable runnable = invocation.getArgument(0);
|
||||
runnable.run();
|
||||
return null;
|
||||
}).when(bukkitService).scheduleSyncTaskFromOptionallyAsyncTask(any(Runnable.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* Assign the necessary fields on ConsoleLogger with mocks.
|
||||
*
|
||||
|
@ -17,7 +17,7 @@ import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static fr.xephi.authme.TestHelper.runInnerRunnable;
|
||||
import static fr.xephi.authme.service.BukkitServiceTestHelper.setBukkitServiceToRunTaskAsynchronously;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
@ -50,10 +50,10 @@ public class AccountsCommandTest {
|
||||
List<String> arguments = Collections.emptyList();
|
||||
given(dataSource.getAuth("tester")).willReturn(authWithIp("123.45.67.89"));
|
||||
given(dataSource.getAllAuthsByIp("123.45.67.89")).willReturn(Arrays.asList("Toaster", "Pester"));
|
||||
setBukkitServiceToRunTaskAsynchronously(bukkitService);
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, arguments);
|
||||
runInnerRunnable(bukkitService);
|
||||
|
||||
// then
|
||||
String[] messages = getMessagesSentToSender(sender, 2);
|
||||
@ -67,10 +67,10 @@ public class AccountsCommandTest {
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
List<String> arguments = Collections.singletonList("SomeUser");
|
||||
given(dataSource.getAuth("someuser")).willReturn(null);
|
||||
setBukkitServiceToRunTaskAsynchronously(bukkitService);
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, arguments);
|
||||
runInnerRunnable(bukkitService);
|
||||
|
||||
// then
|
||||
verify(service).send(sender, MessageKey.UNKNOWN_USER);
|
||||
@ -84,10 +84,10 @@ public class AccountsCommandTest {
|
||||
List<String> arguments = Collections.singletonList("SomeUser");
|
||||
PlayerAuth auth = authWithIp("144.56.77.88");
|
||||
given(dataSource.getAuth("someuser")).willReturn(auth);
|
||||
setBukkitServiceToRunTaskAsynchronously(bukkitService);
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, arguments);
|
||||
runInnerRunnable(bukkitService);
|
||||
|
||||
// then
|
||||
verify(service).send(sender, MessageKey.UNKNOWN_USER);
|
||||
@ -101,10 +101,10 @@ public class AccountsCommandTest {
|
||||
List<String> arguments = Collections.singletonList("SomeUser");
|
||||
given(dataSource.getAuth("someuser")).willReturn(authWithIp("56.78.90.123"));
|
||||
given(dataSource.getAllAuthsByIp("56.78.90.123")).willReturn(Collections.singletonList("SomeUser"));
|
||||
setBukkitServiceToRunTaskAsynchronously(bukkitService);
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, arguments);
|
||||
runInnerRunnable(bukkitService);
|
||||
|
||||
// then
|
||||
String[] messages = getMessagesSentToSender(sender, 1);
|
||||
@ -120,10 +120,10 @@ public class AccountsCommandTest {
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
List<String> arguments = Collections.singletonList("123.45.67.89");
|
||||
given(dataSource.getAllAuthsByIp("123.45.67.89")).willReturn(Collections.emptyList());
|
||||
setBukkitServiceToRunTaskAsynchronously(bukkitService);
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, arguments);
|
||||
runInnerRunnable(bukkitService);
|
||||
|
||||
// then
|
||||
String[] messages = getMessagesSentToSender(sender, 1);
|
||||
@ -136,10 +136,10 @@ public class AccountsCommandTest {
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
List<String> arguments = Collections.singletonList("24.24.48.48");
|
||||
given(dataSource.getAllAuthsByIp("24.24.48.48")).willReturn(Collections.singletonList("SomeUser"));
|
||||
setBukkitServiceToRunTaskAsynchronously(bukkitService);
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, arguments);
|
||||
runInnerRunnable(bukkitService);
|
||||
|
||||
// then
|
||||
String[] messages = getMessagesSentToSender(sender, 1);
|
||||
@ -152,10 +152,10 @@ public class AccountsCommandTest {
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
List<String> arguments = Collections.singletonList("98.76.41.122");
|
||||
given(dataSource.getAllAuthsByIp("98.76.41.122")).willReturn(Arrays.asList("Tester", "Lester", "Taster"));
|
||||
setBukkitServiceToRunTaskAsynchronously(bukkitService);
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, arguments);
|
||||
runInnerRunnable(bukkitService);
|
||||
|
||||
// then
|
||||
String[] messages = getMessagesSentToSender(sender, 2);
|
||||
|
@ -20,6 +20,7 @@ import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import static fr.xephi.authme.service.BukkitServiceTestHelper.setBukkitServiceToRunTaskAsynchronously;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
@ -105,10 +106,10 @@ public class ConverterCommandTest {
|
||||
Class<? extends Converter> converterClass = ConverterCommand.CONVERTERS.get(converterName);
|
||||
Converter converter = createMockReturnedByInjector(converterClass);
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
setBukkitServiceToRunTaskAsynchronously(bukkitService);
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Collections.singletonList(converterName));
|
||||
TestHelper.runInnerRunnable(bukkitService);
|
||||
|
||||
// then
|
||||
verify(converter).execute(sender);
|
||||
@ -125,10 +126,10 @@ public class ConverterCommandTest {
|
||||
Converter converter = createMockReturnedByInjector(converterClass);
|
||||
doThrow(IllegalStateException.class).when(converter).execute(any(CommandSender.class));
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
setBukkitServiceToRunTaskAsynchronously(bukkitService);
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Collections.singletonList(converterName.toUpperCase()));
|
||||
TestHelper.runInnerRunnable(bukkitService);
|
||||
|
||||
// then
|
||||
verify(converter).execute(sender);
|
||||
|
@ -11,7 +11,7 @@ import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import static fr.xephi.authme.TestHelper.runOptionallyAsyncTask;
|
||||
import static fr.xephi.authme.service.BukkitServiceTestHelper.setBukkitServiceToRunTaskOptionallyAsync;
|
||||
import static java.util.Arrays.asList;
|
||||
import static java.util.Collections.singletonList;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
@ -45,10 +45,10 @@ public class PurgePlayerCommandTest {
|
||||
String name = "Bobby";
|
||||
given(dataSource.isAuthAvailable(name)).willReturn(true);
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
setBukkitServiceToRunTaskOptionallyAsync(bukkitService);
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, singletonList(name));
|
||||
runOptionallyAsyncTask(bukkitService);
|
||||
|
||||
// then
|
||||
verify(sender).sendMessage(argThat(containsString("This player is still registered")));
|
||||
@ -63,10 +63,10 @@ public class PurgePlayerCommandTest {
|
||||
OfflinePlayer player = mock(OfflinePlayer.class);
|
||||
given(bukkitService.getOfflinePlayer(name)).willReturn(player);
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
setBukkitServiceToRunTaskOptionallyAsync(bukkitService);
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, singletonList(name));
|
||||
runOptionallyAsyncTask(bukkitService);
|
||||
|
||||
// then
|
||||
verify(dataSource).isAuthAvailable(name);
|
||||
@ -80,10 +80,10 @@ public class PurgePlayerCommandTest {
|
||||
OfflinePlayer player = mock(OfflinePlayer.class);
|
||||
given(bukkitService.getOfflinePlayer(name)).willReturn(player);
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
setBukkitServiceToRunTaskOptionallyAsync(bukkitService);
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, asList(name, "force"));
|
||||
runOptionallyAsyncTask(bukkitService);
|
||||
|
||||
// then
|
||||
verify(purgeExecutor).executePurge(singletonList(player), singletonList(name.toLowerCase()));
|
||||
|
@ -22,7 +22,8 @@ import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static fr.xephi.authme.TestHelper.setBukkitServiceToRunOptionallyAsyncTasks;
|
||||
import static fr.xephi.authme.service.BukkitServiceTestHelper.setBukkitServiceToRunTaskOptionallyAsync;
|
||||
import static fr.xephi.authme.service.BukkitServiceTestHelper.setBukkitServiceToScheduleSyncTaskFromOptionallyAsyncTask;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
@ -86,10 +87,10 @@ public class RegisterAdminCommandTest {
|
||||
given(validationService.validatePassword(password, user)).willReturn(new ValidationResult());
|
||||
given(dataSource.isAuthAvailable(user)).willReturn(true);
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
setBukkitServiceToRunTaskOptionallyAsync(bukkitService);
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Arrays.asList(user, password));
|
||||
TestHelper.runOptionallyAsyncTask(bukkitService);
|
||||
|
||||
// then
|
||||
verify(validationService).validatePassword(password, user);
|
||||
@ -108,10 +109,10 @@ public class RegisterAdminCommandTest {
|
||||
HashedPassword hashedPassword = new HashedPassword("235sdf4w5udsgf");
|
||||
given(passwordSecurity.computeHash(password, user)).willReturn(hashedPassword);
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
setBukkitServiceToRunTaskOptionallyAsync(bukkitService);
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Arrays.asList(user, password));
|
||||
TestHelper.runOptionallyAsyncTask(bukkitService);
|
||||
|
||||
// then
|
||||
verify(validationService).validatePassword(password, user);
|
||||
@ -133,10 +134,10 @@ public class RegisterAdminCommandTest {
|
||||
given(passwordSecurity.computeHash(password, user)).willReturn(hashedPassword);
|
||||
given(bukkitService.getPlayerExact(user)).willReturn(null);
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
setBukkitServiceToRunTaskOptionallyAsync(bukkitService);
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Arrays.asList(user, password));
|
||||
TestHelper.runOptionallyAsyncTask(bukkitService);
|
||||
|
||||
// then
|
||||
verify(validationService).validatePassword(password, user);
|
||||
@ -161,11 +162,11 @@ public class RegisterAdminCommandTest {
|
||||
String kickForAdminRegister = "Admin registered you -- log in again";
|
||||
given(commandService.retrieveSingleMessage(MessageKey.KICK_FOR_ADMIN_REGISTER)).willReturn(kickForAdminRegister);
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
setBukkitServiceToRunOptionallyAsyncTasks(bukkitService);
|
||||
setBukkitServiceToScheduleSyncTaskFromOptionallyAsyncTask(bukkitService);
|
||||
setBukkitServiceToRunTaskOptionallyAsync(bukkitService);
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Arrays.asList(user, password));
|
||||
TestHelper.runOptionallyAsyncTask(bukkitService);
|
||||
|
||||
// then
|
||||
verify(validationService).validatePassword(password, user);
|
||||
|
@ -16,7 +16,7 @@ import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static fr.xephi.authme.TestHelper.runOptionallyAsyncTask;
|
||||
import static fr.xephi.authme.service.BukkitServiceTestHelper.setBukkitServiceToRunTaskOptionallyAsync;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@ -74,10 +74,10 @@ public class SetEmailCommandTest {
|
||||
given(validationService.validateEmail(email)).willReturn(true);
|
||||
given(dataSource.getAuth(user)).willReturn(null);
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
setBukkitServiceToRunTaskOptionallyAsync(bukkitService);
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Arrays.asList(user, email));
|
||||
runOptionallyAsyncTask(bukkitService);
|
||||
|
||||
// then
|
||||
verify(validationService).validateEmail(email);
|
||||
@ -96,10 +96,10 @@ public class SetEmailCommandTest {
|
||||
given(dataSource.getAuth(user)).willReturn(auth);
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
given(validationService.isEmailFreeForRegistration(email, sender)).willReturn(false);
|
||||
setBukkitServiceToRunTaskOptionallyAsync(bukkitService);
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Arrays.asList(user, email));
|
||||
runOptionallyAsyncTask(bukkitService);
|
||||
|
||||
// then
|
||||
verify(validationService).validateEmail(email);
|
||||
@ -121,10 +121,10 @@ public class SetEmailCommandTest {
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
given(validationService.isEmailFreeForRegistration(email, sender)).willReturn(true);
|
||||
given(dataSource.updateEmail(auth)).willReturn(false);
|
||||
setBukkitServiceToRunTaskOptionallyAsync(bukkitService);
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Arrays.asList(user, email));
|
||||
runOptionallyAsyncTask(bukkitService);
|
||||
|
||||
// then
|
||||
verify(validationService).validateEmail(email);
|
||||
@ -147,10 +147,10 @@ public class SetEmailCommandTest {
|
||||
given(validationService.isEmailFreeForRegistration(email, sender)).willReturn(true);
|
||||
given(dataSource.updateEmail(auth)).willReturn(true);
|
||||
given(playerCache.getAuth(user)).willReturn(null);
|
||||
setBukkitServiceToRunTaskOptionallyAsync(bukkitService);
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Arrays.asList(user, email));
|
||||
runOptionallyAsyncTask(bukkitService);
|
||||
|
||||
// then
|
||||
verify(validationService).validateEmail(email);
|
||||
@ -174,10 +174,10 @@ public class SetEmailCommandTest {
|
||||
given(validationService.isEmailFreeForRegistration(email, sender)).willReturn(true);
|
||||
given(dataSource.updateEmail(auth)).willReturn(true);
|
||||
given(playerCache.getAuth(user)).willReturn(mock(PlayerAuth.class));
|
||||
setBukkitServiceToRunTaskOptionallyAsync(bukkitService);
|
||||
|
||||
// when
|
||||
command.executeCommand(sender, Arrays.asList(user, email));
|
||||
runOptionallyAsyncTask(bukkitService);
|
||||
|
||||
// then
|
||||
verify(validationService).validateEmail(email);
|
||||
|
@ -20,6 +20,7 @@ import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static fr.xephi.authme.service.BukkitServiceTestHelper.setBukkitServiceToScheduleSyncDelayedTask;
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.lessThan;
|
||||
@ -151,10 +152,10 @@ public class TempbanManagerTest {
|
||||
given(messages.retrieveSingle(MessageKey.TEMPBAN_MAX_LOGINS)).willReturn(banReason);
|
||||
Settings settings = mockSettings(2, 100, "");
|
||||
TempbanManager manager = new TempbanManager(bukkitService, messages, settings);
|
||||
setBukkitServiceToScheduleSyncDelayedTask(bukkitService);
|
||||
|
||||
// when
|
||||
manager.tempbanPlayer(player);
|
||||
TestHelper.runSyncDelayedTask(bukkitService);
|
||||
|
||||
// then
|
||||
verify(player).kickPlayer(banReason);
|
||||
@ -178,10 +179,10 @@ public class TempbanManagerTest {
|
||||
String banCommand = "banip %ip% 15d IP ban too many logins";
|
||||
Settings settings = mockSettings(2, 100, banCommand);
|
||||
TempbanManager manager = new TempbanManager(bukkitService, messages, settings);
|
||||
setBukkitServiceToScheduleSyncDelayedTask(bukkitService);
|
||||
|
||||
// when
|
||||
manager.tempbanPlayer(player);
|
||||
TestHelper.runSyncDelayedTask(bukkitService);
|
||||
|
||||
// then
|
||||
verify(bukkitService).dispatchConsoleCommand(banCommand.replace("%ip%", ip));
|
||||
@ -200,10 +201,10 @@ public class TempbanManagerTest {
|
||||
manager.increaseCount(ip, "user");
|
||||
manager.increaseCount(ip, "name2");
|
||||
manager.increaseCount(ip, "user");
|
||||
setBukkitServiceToScheduleSyncDelayedTask(bukkitService);
|
||||
|
||||
// when
|
||||
manager.tempbanPlayer(player);
|
||||
TestHelper.runSyncDelayedTask(bukkitService);
|
||||
|
||||
// then
|
||||
verify(player).kickPlayer(banReason);
|
||||
|
@ -55,6 +55,7 @@ import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
||||
import static fr.xephi.authme.listener.EventCancelVerifier.withServiceMock;
|
||||
import static fr.xephi.authme.service.BukkitServiceTestHelper.setBukkitServiceToScheduleSyncDelayedTaskWithDelay;
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.hamcrest.Matchers.empty;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
@ -755,13 +756,13 @@ public class PlayerListenerTest {
|
||||
InventoryOpenEvent event = new InventoryOpenEvent(transaction);
|
||||
given(event.getPlayer()).willReturn(player);
|
||||
given(listenerService.shouldCancelEvent(player)).willReturn(true);
|
||||
setBukkitServiceToScheduleSyncDelayedTaskWithDelay(bukkitService);
|
||||
|
||||
// when
|
||||
listener.onPlayerInventoryOpen(event);
|
||||
|
||||
// then
|
||||
assertThat(event.isCancelled(), equalTo(true));
|
||||
TestHelper.runSyncDelayedTaskWithDelay(bukkitService);
|
||||
verify(player).closeInventory();
|
||||
}
|
||||
|
||||
|
@ -22,6 +22,7 @@ import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import static fr.xephi.authme.AuthMeMatchers.equalToHash;
|
||||
import static fr.xephi.authme.AuthMeMatchers.hasAuthBasicData;
|
||||
import static fr.xephi.authme.service.BukkitServiceTestHelper.setBukkitServiceToScheduleSyncDelayedTaskWithDelay;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.greaterThan;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
@ -116,12 +117,12 @@ public class PasswordRegisterExecutorTest {
|
||||
given(commonService.getProperty(PluginSettings.USE_ASYNC_TASKS)).willReturn(false);
|
||||
Player player = mock(Player.class);
|
||||
PasswordRegisterParams params = PasswordRegisterParams.of(player, "pass", "mail@example.org");
|
||||
setBukkitServiceToScheduleSyncDelayedTaskWithDelay(bukkitService);
|
||||
|
||||
// when
|
||||
executor.executePostPersistAction(params);
|
||||
|
||||
// then
|
||||
TestHelper.runSyncDelayedTaskWithDelay(bukkitService);
|
||||
verify(asynchronousLogin).forceLogin(player);
|
||||
verify(syncProcessManager).processSyncPasswordRegister(player);
|
||||
}
|
||||
|
@ -31,7 +31,7 @@ import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
import static fr.xephi.authme.TestHelper.setBukkitServiceToRunOptionallyAsyncTasks;
|
||||
import static fr.xephi.authme.service.BukkitServiceTestHelper.setBukkitServiceToScheduleSyncTaskFromOptionallyAsyncTask;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
@ -115,7 +115,7 @@ public class AsynchronousUnregisterTest {
|
||||
given(service.getProperty(RegistrationSettings.FORCE)).willReturn(true);
|
||||
given(service.getProperty(RegistrationSettings.APPLY_BLIND_EFFECT)).willReturn(true);
|
||||
given(service.getProperty(RestrictionSettings.TIMEOUT)).willReturn(21);
|
||||
setBukkitServiceToRunOptionallyAsyncTasks(bukkitService);
|
||||
setBukkitServiceToScheduleSyncTaskFromOptionallyAsyncTask(bukkitService);
|
||||
|
||||
// when
|
||||
asynchronousUnregister.unregister(player, userPassword);
|
||||
@ -148,7 +148,7 @@ public class AsynchronousUnregisterTest {
|
||||
given(dataSource.removeAuth(name)).willReturn(true);
|
||||
given(service.getProperty(RegistrationSettings.FORCE)).willReturn(true);
|
||||
given(service.getProperty(RegistrationSettings.APPLY_BLIND_EFFECT)).willReturn(false);
|
||||
setBukkitServiceToRunOptionallyAsyncTasks(bukkitService);
|
||||
setBukkitServiceToScheduleSyncTaskFromOptionallyAsyncTask(bukkitService);
|
||||
|
||||
// when
|
||||
asynchronousUnregister.unregister(player, userPassword);
|
||||
@ -179,7 +179,7 @@ public class AsynchronousUnregisterTest {
|
||||
given(passwordSecurity.comparePassword(userPassword, password, name)).willReturn(true);
|
||||
given(dataSource.removeAuth(name)).willReturn(true);
|
||||
given(service.getProperty(RegistrationSettings.FORCE)).willReturn(false);
|
||||
setBukkitServiceToRunOptionallyAsyncTasks(bukkitService);
|
||||
setBukkitServiceToScheduleSyncTaskFromOptionallyAsyncTask(bukkitService);
|
||||
|
||||
// when
|
||||
asynchronousUnregister.unregister(player, userPassword);
|
||||
@ -257,7 +257,7 @@ public class AsynchronousUnregisterTest {
|
||||
given(service.getProperty(RegistrationSettings.FORCE)).willReturn(true);
|
||||
given(service.getProperty(RegistrationSettings.APPLY_BLIND_EFFECT)).willReturn(false);
|
||||
CommandSender initiator = mock(CommandSender.class);
|
||||
setBukkitServiceToRunOptionallyAsyncTasks(bukkitService);
|
||||
setBukkitServiceToScheduleSyncTaskFromOptionallyAsyncTask(bukkitService);
|
||||
|
||||
// when
|
||||
asynchronousUnregister.adminUnregister(initiator, name, player);
|
||||
|
@ -11,14 +11,16 @@ import fr.xephi.authme.settings.Settings;
|
||||
import fr.xephi.authme.settings.properties.ProtectionSettings;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.scheduler.BukkitTask;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mock;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static fr.xephi.authme.TestHelper.runSyncDelayedTaskWithDelay;
|
||||
import static fr.xephi.authme.service.BukkitServiceTestHelper.setBukkitServiceToScheduleSyncDelayedTaskWithDelay;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
@ -55,14 +57,12 @@ public class AntiBotServiceTest {
|
||||
given(settings.getProperty(ProtectionSettings.ANTIBOT_SENSIBILITY)).willReturn(5);
|
||||
given(settings.getProperty(ProtectionSettings.ENABLE_ANTIBOT)).willReturn(true);
|
||||
given(settings.getProperty(ProtectionSettings.ANTIBOT_DELAY)).willReturn(8);
|
||||
setBukkitServiceToScheduleSyncDelayedTaskWithDelay(bukkitService);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldStartListenerOnStartup() {
|
||||
// given / when
|
||||
runSyncDelayedTaskWithDelay(bukkitService);
|
||||
|
||||
// then
|
||||
// given / when / then
|
||||
assertThat(antiBotService.getAntiBotStatus(), equalTo(AntiBotService.AntiBotStatus.LISTENING));
|
||||
}
|
||||
|
||||
@ -81,9 +81,10 @@ public class AntiBotServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore // TODO ljacqu fix test
|
||||
public void shouldActivateAntibot() {
|
||||
// given - listening antibot
|
||||
runSyncDelayedTaskWithDelay(bukkitService);
|
||||
reset(bukkitService);
|
||||
|
||||
// when
|
||||
antiBotService.overrideAntiBotStatus(true);
|
||||
@ -91,23 +92,23 @@ public class AntiBotServiceTest {
|
||||
// then
|
||||
assertThat(antiBotService.getAntiBotStatus(), equalTo(AntiBotService.AntiBotStatus.ACTIVE));
|
||||
// Check that a task is scheduled to disable again
|
||||
runSyncDelayedTaskWithDelay(bukkitService);
|
||||
ArgumentCaptor<Runnable> runnableCaptor = ArgumentCaptor.forClass(Runnable.class);
|
||||
verify(bukkitService).runTaskLater(runnableCaptor.capture(), anyLong());
|
||||
runnableCaptor.getValue().run();
|
||||
assertThat(antiBotService.getAntiBotStatus(), equalTo(AntiBotService.AntiBotStatus.LISTENING));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldNotActivateAntibotForDisabledSetting() {
|
||||
// given - disabled antibot
|
||||
reset(bukkitService);
|
||||
assertThat(antiBotService.getAntiBotStatus(), equalTo(AntiBotService.AntiBotStatus.DISABLED));
|
||||
given(settings.getProperty(ProtectionSettings.ENABLE_ANTIBOT)).willReturn(false);
|
||||
AntiBotService antiBotService = new AntiBotService(settings, messages, permissionsManager, bukkitService);
|
||||
|
||||
// when
|
||||
antiBotService.overrideAntiBotStatus(true);
|
||||
|
||||
// then
|
||||
assertThat(antiBotService.getAntiBotStatus(), equalTo(AntiBotService.AntiBotStatus.DISABLED));
|
||||
verifyZeroInteractions(bukkitService);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -127,10 +128,7 @@ public class AntiBotServiceTest {
|
||||
|
||||
@Test
|
||||
public void shouldAcceptPlayerToJoin() {
|
||||
// given - listening antibot
|
||||
runSyncDelayedTaskWithDelay(bukkitService);
|
||||
|
||||
// when
|
||||
// given / when
|
||||
boolean result = antiBotService.shouldKick();
|
||||
|
||||
// then
|
||||
@ -142,9 +140,7 @@ public class AntiBotServiceTest {
|
||||
// given
|
||||
int sensitivity = 10;
|
||||
given(settings.getProperty(ProtectionSettings.ANTIBOT_SENSIBILITY)).willReturn(sensitivity);
|
||||
reset(bukkitService);
|
||||
AntiBotService antiBotService = new AntiBotService(settings, messages, permissionsManager, bukkitService);
|
||||
runSyncDelayedTaskWithDelay(bukkitService);
|
||||
|
||||
for (int i = 0; i < sensitivity; ++i) {
|
||||
antiBotService.shouldKick();
|
||||
@ -162,7 +158,6 @@ public class AntiBotServiceTest {
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
public void shouldInformPlayersOnActivation() {
|
||||
// given - listening antibot
|
||||
runSyncDelayedTaskWithDelay(bukkitService);
|
||||
List<Player> players = Arrays.asList(mock(Player.class), mock(Player.class));
|
||||
given(bukkitService.getOnlinePlayers()).willReturn((List) players);
|
||||
given(permissionsManager.hasPermission(players.get(0), AdminPermission.ANTIBOT_MESSAGES)).willReturn(false);
|
||||
@ -180,7 +175,6 @@ public class AntiBotServiceTest {
|
||||
@Test
|
||||
public void shouldImmediatelyStartAfterFirstStartup() {
|
||||
// given - listening antibot
|
||||
runSyncDelayedTaskWithDelay(bukkitService);
|
||||
given(bukkitService.runTaskLater(any(Runnable.class), anyLong())).willReturn(mock(BukkitTask.class));
|
||||
antiBotService.overrideAntiBotStatus(true);
|
||||
|
||||
|
@ -0,0 +1,84 @@
|
||||
package fr.xephi.authme.service;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
|
||||
/**
|
||||
* Offers utility methods for testing involving a {@link BukkitService} mock.
|
||||
*/
|
||||
public final class BukkitServiceTestHelper {
|
||||
|
||||
private BukkitServiceTestHelper() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a BukkitService mock to run any Runnable it is passed to its method
|
||||
* {@link BukkitService#scheduleSyncTaskFromOptionallyAsyncTask}.
|
||||
*
|
||||
* @param bukkitService the mock to set behavior on
|
||||
*/
|
||||
public static void setBukkitServiceToScheduleSyncTaskFromOptionallyAsyncTask(BukkitService bukkitService) {
|
||||
doAnswer(invocation -> {
|
||||
Runnable runnable = invocation.getArgument(0);
|
||||
runnable.run();
|
||||
return null;
|
||||
}).when(bukkitService).scheduleSyncTaskFromOptionallyAsyncTask(any(Runnable.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a BukkitService mock to run any Runnable it is passed to its method
|
||||
* {@link BukkitService#runTaskAsynchronously}.
|
||||
*
|
||||
* @param bukkitService the mock to set behavior on
|
||||
*/
|
||||
public static void setBukkitServiceToRunTaskAsynchronously(BukkitService bukkitService) {
|
||||
doAnswer(invocation -> {
|
||||
Runnable runnable = invocation.getArgument(0);
|
||||
runnable.run();
|
||||
return null;
|
||||
}).when(bukkitService).runTaskAsynchronously(any(Runnable.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a BukkitService mock to run any Runnable it is passed to its method
|
||||
* {@link BukkitService#runTaskOptionallyAsync}.
|
||||
*
|
||||
* @param bukkitService the mock to set behavior on
|
||||
*/
|
||||
public static void setBukkitServiceToRunTaskOptionallyAsync(BukkitService bukkitService) {
|
||||
doAnswer(invocation -> {
|
||||
Runnable runnable = invocation.getArgument(0);
|
||||
runnable.run();
|
||||
return null;
|
||||
}).when(bukkitService).runTaskOptionallyAsync(any(Runnable.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a BukkitService mock to run any Runnable it is passed to its method
|
||||
* {@link BukkitService#scheduleSyncDelayedTask(Runnable)}.
|
||||
*
|
||||
* @param bukkitService the mock to set behavior on
|
||||
*/
|
||||
public static void setBukkitServiceToScheduleSyncDelayedTask(BukkitService bukkitService) {
|
||||
doAnswer(invocation -> {
|
||||
Runnable runnable = invocation.getArgument(0);
|
||||
runnable.run();
|
||||
return null;
|
||||
}).when(bukkitService).scheduleSyncDelayedTask(any(Runnable.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a BukkitService mock to run any Runnable it is passed to its method
|
||||
* {@link BukkitService#scheduleSyncDelayedTask(Runnable, long)}.
|
||||
*
|
||||
* @param bukkitService the mock to set behavior on
|
||||
*/
|
||||
public static void setBukkitServiceToScheduleSyncDelayedTaskWithDelay(BukkitService bukkitService) {
|
||||
doAnswer(invocation -> {
|
||||
Runnable runnable = invocation.getArgument(0);
|
||||
runnable.run();
|
||||
return null;
|
||||
}).when(bukkitService).scheduleSyncDelayedTask(any(Runnable.class), anyLong());
|
||||
}
|
||||
}
|
@ -21,7 +21,7 @@ import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static fr.xephi.authme.TestHelper.setBukkitServiceToRunOptionallyAsyncTasks;
|
||||
import static fr.xephi.authme.service.BukkitServiceTestHelper.setBukkitServiceToScheduleSyncTaskFromOptionallyAsyncTask;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
@ -89,7 +89,7 @@ public class TeleportationServiceTest {
|
||||
given(player.isOnline()).willReturn(true);
|
||||
Location firstSpawn = mockLocation();
|
||||
given(spawnLoader.getFirstSpawn()).willReturn(firstSpawn);
|
||||
setBukkitServiceToRunOptionallyAsyncTasks(bukkitService);
|
||||
setBukkitServiceToScheduleSyncTaskFromOptionallyAsyncTask(bukkitService);
|
||||
|
||||
// when
|
||||
teleportationService.teleportNewPlayerToFirstSpawn(player);
|
||||
@ -109,7 +109,7 @@ public class TeleportationServiceTest {
|
||||
given(player.isOnline()).willReturn(true);
|
||||
Location spawn = mockLocation();
|
||||
given(spawnLoader.getSpawnLocation(player)).willReturn(spawn);
|
||||
setBukkitServiceToRunOptionallyAsyncTasks(bukkitService);
|
||||
setBukkitServiceToScheduleSyncTaskFromOptionallyAsyncTask(bukkitService);
|
||||
|
||||
// when
|
||||
teleportationService.teleportOnJoin(player);
|
||||
@ -178,7 +178,7 @@ public class TeleportationServiceTest {
|
||||
event.setTo(null);
|
||||
return null;
|
||||
}).when(bukkitService).callEvent(any(SpawnTeleportEvent.class));
|
||||
setBukkitServiceToRunOptionallyAsyncTasks(bukkitService);
|
||||
setBukkitServiceToScheduleSyncTaskFromOptionallyAsyncTask(bukkitService);
|
||||
|
||||
// when
|
||||
teleportationService.teleportOnJoin(player);
|
||||
@ -201,7 +201,7 @@ public class TeleportationServiceTest {
|
||||
event.setCancelled(true);
|
||||
return null;
|
||||
}).when(bukkitService).callEvent(any(SpawnTeleportEvent.class));
|
||||
setBukkitServiceToRunOptionallyAsyncTasks(bukkitService);
|
||||
setBukkitServiceToScheduleSyncTaskFromOptionallyAsyncTask(bukkitService);
|
||||
|
||||
// when
|
||||
teleportationService.teleportOnJoin(player);
|
||||
@ -242,7 +242,7 @@ public class TeleportationServiceTest {
|
||||
Location limboLocation = mockLocation();
|
||||
given(limboLocation.getWorld().getName()).willReturn("forced1");
|
||||
given(limbo.getLocation()).willReturn(limboLocation);
|
||||
setBukkitServiceToRunOptionallyAsyncTasks(bukkitService);
|
||||
setBukkitServiceToScheduleSyncTaskFromOptionallyAsyncTask(bukkitService);
|
||||
|
||||
// when
|
||||
teleportationService.teleportOnLogin(player, auth, limbo);
|
||||
@ -289,7 +289,7 @@ public class TeleportationServiceTest {
|
||||
LimboPlayer limbo = mock(LimboPlayer.class);
|
||||
Location limboLocation = mockLocation();
|
||||
given(limbo.getLocation()).willReturn(limboLocation);
|
||||
setBukkitServiceToRunOptionallyAsyncTasks(bukkitService);
|
||||
setBukkitServiceToScheduleSyncTaskFromOptionallyAsyncTask(bukkitService);
|
||||
|
||||
// when
|
||||
teleportationService.teleportOnLogin(player, auth, limbo);
|
||||
@ -317,7 +317,7 @@ public class TeleportationServiceTest {
|
||||
LimboPlayer limbo = mock(LimboPlayer.class);
|
||||
Location limboLocation = mockLocation();
|
||||
given(limbo.getLocation()).willReturn(limboLocation);
|
||||
setBukkitServiceToRunOptionallyAsyncTasks(bukkitService);
|
||||
setBukkitServiceToScheduleSyncTaskFromOptionallyAsyncTask(bukkitService);
|
||||
|
||||
// when
|
||||
teleportationService.teleportOnLogin(player, auth, limbo);
|
||||
@ -342,7 +342,7 @@ public class TeleportationServiceTest {
|
||||
LimboPlayer limbo = mock(LimboPlayer.class);
|
||||
Location location = mockLocation();
|
||||
given(limbo.getLocation()).willReturn(location);
|
||||
setBukkitServiceToRunOptionallyAsyncTasks(bukkitService);
|
||||
setBukkitServiceToScheduleSyncTaskFromOptionallyAsyncTask(bukkitService);
|
||||
|
||||
// when
|
||||
teleportationService.teleportOnLogin(player, auth, limbo);
|
||||
@ -364,7 +364,7 @@ public class TeleportationServiceTest {
|
||||
LimboPlayer limbo = mock(LimboPlayer.class);
|
||||
Location location = mockLocation();
|
||||
given(limbo.getLocation()).willReturn(location);
|
||||
setBukkitServiceToRunOptionallyAsyncTasks(bukkitService);
|
||||
setBukkitServiceToScheduleSyncTaskFromOptionallyAsyncTask(bukkitService);
|
||||
|
||||
// when
|
||||
teleportationService.teleportOnLogin(player, auth, limbo);
|
||||
|
Loading…
Reference in New Issue
Block a user