Merge branch '5.3-DEV' of https://github.com/AuthMe-Team/AuthMeReloaded
Conflicts: pom.xml
This commit is contained in:
@@ -9,10 +9,14 @@ import org.bukkit.Server;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.ConsoleCommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
import org.bukkit.scheduler.BukkitScheduler;
|
||||
import org.bukkit.scheduler.BukkitTask;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.util.Collection;
|
||||
@@ -20,9 +24,13 @@ import java.util.Collection;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.only;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyZeroInteractions;
|
||||
|
||||
/**
|
||||
* Test for {@link BukkitService}.
|
||||
@@ -38,10 +46,13 @@ public class BukkitServiceTest {
|
||||
private Settings settings;
|
||||
@Mock
|
||||
private Server server;
|
||||
@Mock
|
||||
private BukkitScheduler scheduler;
|
||||
|
||||
@Before
|
||||
public void constructBukkitService() {
|
||||
ReflectionTestUtils.setField(Bukkit.class, null, "server", server);
|
||||
given(server.getScheduler()).willReturn(scheduler);
|
||||
given(settings.getProperty(PluginSettings.USE_ASYNC_TASKS)).willReturn(true);
|
||||
bukkitService = new BukkitService(authMe, settings);
|
||||
}
|
||||
@@ -101,6 +112,191 @@ public class BukkitServiceTest {
|
||||
verify(server).dispatchCommand(consoleSender, command);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldScheduleSyncDelayedTask() {
|
||||
// given
|
||||
Runnable task = () -> {/* noop */};
|
||||
given(scheduler.scheduleSyncDelayedTask(authMe, task)).willReturn(123);
|
||||
|
||||
// when
|
||||
int taskId = bukkitService.scheduleSyncDelayedTask(task);
|
||||
|
||||
// then
|
||||
verify(scheduler, only()).scheduleSyncDelayedTask(authMe, task);
|
||||
assertThat(taskId, equalTo(123));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldScheduleSyncDelayedTaskWithDelay() {
|
||||
// given
|
||||
Runnable task = () -> {/* noop */};
|
||||
int delay = 3;
|
||||
given(scheduler.scheduleSyncDelayedTask(authMe, task, delay)).willReturn(44);
|
||||
|
||||
// when
|
||||
int taskId = bukkitService.scheduleSyncDelayedTask(task, delay);
|
||||
|
||||
// then
|
||||
verify(scheduler, only()).scheduleSyncDelayedTask(authMe, task, delay);
|
||||
assertThat(taskId, equalTo(44));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldScheduleSyncTask() {
|
||||
// given
|
||||
BukkitService spy = Mockito.spy(bukkitService);
|
||||
doReturn(1).when(spy).scheduleSyncDelayedTask(any(Runnable.class));
|
||||
Runnable task = mock(Runnable.class);
|
||||
|
||||
// when
|
||||
spy.scheduleSyncTaskFromOptionallyAsyncTask(task);
|
||||
|
||||
// then
|
||||
verify(spy).scheduleSyncDelayedTask(task);
|
||||
verifyZeroInteractions(task);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRunTaskDirectly() {
|
||||
// given
|
||||
given(settings.getProperty(PluginSettings.USE_ASYNC_TASKS)).willReturn(false);
|
||||
bukkitService.reload(settings);
|
||||
BukkitService spy = Mockito.spy(bukkitService);
|
||||
Runnable task = mock(Runnable.class);
|
||||
|
||||
// when
|
||||
spy.scheduleSyncTaskFromOptionallyAsyncTask(task);
|
||||
|
||||
// then
|
||||
verify(task).run();
|
||||
verify(spy, only()).scheduleSyncTaskFromOptionallyAsyncTask(task);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRunTask() {
|
||||
// given
|
||||
Runnable task = () -> {/* noop */};
|
||||
BukkitTask bukkitTask = mock(BukkitTask.class);
|
||||
given(scheduler.runTask(authMe, task)).willReturn(bukkitTask);
|
||||
|
||||
// when
|
||||
BukkitTask resultingTask = bukkitService.runTask(task);
|
||||
|
||||
// then
|
||||
assertThat(resultingTask, equalTo(bukkitTask));
|
||||
verify(scheduler, only()).runTask(authMe, task);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRunTaskLater() {
|
||||
// given
|
||||
Runnable task = () -> {/* noop */};
|
||||
BukkitTask bukkitTask = mock(BukkitTask.class);
|
||||
long delay = 400;
|
||||
given(scheduler.runTaskLater(authMe, task, delay)).willReturn(bukkitTask);
|
||||
|
||||
// when
|
||||
BukkitTask resultingTask = bukkitService.runTaskLater(task, delay);
|
||||
|
||||
// then
|
||||
assertThat(resultingTask, equalTo(bukkitTask));
|
||||
verify(scheduler, only()).runTaskLater(authMe, task, delay);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRunTaskInAsync() {
|
||||
// given
|
||||
Runnable task = mock(Runnable.class);
|
||||
BukkitService spy = Mockito.spy(bukkitService);
|
||||
doReturn(null).when(spy).runTaskAsynchronously(task);
|
||||
|
||||
// when
|
||||
spy.runTaskOptionallyAsync(task);
|
||||
|
||||
// then
|
||||
verifyZeroInteractions(task);
|
||||
verify(spy).runTaskAsynchronously(task);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRunTaskDirectlyIfConfigured() {
|
||||
// given
|
||||
given(settings.getProperty(PluginSettings.USE_ASYNC_TASKS)).willReturn(false);
|
||||
bukkitService.reload(settings);
|
||||
BukkitService spy = Mockito.spy(bukkitService);
|
||||
Runnable task = mock(Runnable.class);
|
||||
|
||||
// when
|
||||
spy.runTaskOptionallyAsync(task);
|
||||
|
||||
// then
|
||||
verify(task).run();
|
||||
verify(spy, only()).runTaskOptionallyAsync(task);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRunTaskAsynchronously() {
|
||||
// given
|
||||
Runnable task = () -> {/* noop */};
|
||||
BukkitTask bukkitTask = mock(BukkitTask.class);
|
||||
given(scheduler.runTaskAsynchronously(authMe, task)).willReturn(bukkitTask);
|
||||
|
||||
// when
|
||||
BukkitTask resultingTask = bukkitService.runTaskAsynchronously(task);
|
||||
|
||||
// then
|
||||
assertThat(resultingTask, equalTo(bukkitTask));
|
||||
verify(scheduler, only()).runTaskAsynchronously(authMe, task);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRunTaskTimerAsynchronously() {
|
||||
// given
|
||||
Runnable task = () -> {/* */};
|
||||
long delay = 20L;
|
||||
long period = 4000L;
|
||||
BukkitTask bukkitTask = mock(BukkitTask.class);
|
||||
given(scheduler.runTaskTimerAsynchronously(authMe, task, delay, period)).willReturn(bukkitTask);
|
||||
|
||||
// when
|
||||
BukkitTask resultingTask = bukkitService.runTaskTimerAsynchronously(task, delay, period);
|
||||
|
||||
// then
|
||||
assertThat(resultingTask, equalTo(bukkitTask));
|
||||
verify(scheduler).runTaskTimerAsynchronously(authMe, task, delay, period);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRunTaskTimer() {
|
||||
// given
|
||||
BukkitRunnable bukkitRunnable = mock(BukkitRunnable.class);
|
||||
long delay = 20;
|
||||
long period = 80;
|
||||
BukkitTask bukkitTask = mock(BukkitTask.class);
|
||||
given(bukkitRunnable.runTaskTimer(authMe, delay, period)).willReturn(bukkitTask);
|
||||
|
||||
// when
|
||||
BukkitTask result = bukkitService.runTaskTimer(bukkitRunnable, delay, period);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(bukkitTask));
|
||||
verify(bukkitRunnable).runTaskTimer(authMe, delay, period);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldBroadcastMessage() {
|
||||
// given
|
||||
String message = "Important message to all";
|
||||
given(server.broadcastMessage(message)).willReturn(24);
|
||||
|
||||
// when
|
||||
int result = bukkitService.broadcastMessage(message);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(24));
|
||||
verify(server).broadcastMessage(message);
|
||||
}
|
||||
|
||||
// Note: This method is used through reflections
|
||||
public static Player[] onlinePlayersImpl() {
|
||||
return new Player[]{
|
||||
|
||||
@@ -4,7 +4,6 @@ import ch.jalu.configme.configurationdata.ConfigurationData;
|
||||
import ch.jalu.configme.configurationdata.ConfigurationDataBuilder;
|
||||
import ch.jalu.configme.resource.PropertyResource;
|
||||
import fr.xephi.authme.TestHelper;
|
||||
import fr.xephi.authme.settings.properties.RegistrationSettings;
|
||||
import fr.xephi.authme.settings.properties.TestConfiguration;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
@@ -16,11 +15,8 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
|
||||
import static org.hamcrest.Matchers.arrayContaining;
|
||||
import static org.hamcrest.Matchers.arrayWithSize;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
@@ -45,26 +41,6 @@ public class SettingsTest {
|
||||
testPluginFolder = temporaryFolder.newFolder();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldLoadWelcomeMessage() throws IOException {
|
||||
// given
|
||||
String welcomeMessage = "This is my welcome message for testing\nBye!";
|
||||
File welcomeFile = new File(testPluginFolder, "welcome.txt");
|
||||
createFile(welcomeFile);
|
||||
Files.write(welcomeFile.toPath(), welcomeMessage.getBytes());
|
||||
|
||||
PropertyResource resource = mock(PropertyResource.class);
|
||||
given(resource.getBoolean(RegistrationSettings.USE_WELCOME_MESSAGE.getPath())).willReturn(true);
|
||||
Settings settings = new Settings(testPluginFolder, resource, null, CONFIG_DATA);
|
||||
|
||||
// when
|
||||
String[] result = settings.getWelcomeMessage();
|
||||
|
||||
// then
|
||||
assertThat(result, arrayWithSize(2));
|
||||
assertThat(result, arrayContaining(welcomeMessage.split("\\n")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldLoadEmailMessage() throws IOException {
|
||||
// given
|
||||
|
||||
@@ -0,0 +1,141 @@
|
||||
package fr.xephi.authme.settings;
|
||||
|
||||
import ch.jalu.injector.testing.BeforeInjecting;
|
||||
import ch.jalu.injector.testing.DelayedInjectionRunner;
|
||||
import ch.jalu.injector.testing.InjectDelayed;
|
||||
import fr.xephi.authme.TestHelper;
|
||||
import fr.xephi.authme.data.auth.PlayerCache;
|
||||
import fr.xephi.authme.initialization.DataFolder;
|
||||
import fr.xephi.authme.service.BukkitService;
|
||||
import fr.xephi.authme.service.GeoIpService;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.only;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyZeroInteractions;
|
||||
|
||||
/**
|
||||
* Test for {@link WelcomeMessageConfiguration}.
|
||||
*/
|
||||
@RunWith(DelayedInjectionRunner.class)
|
||||
public class WelcomeMessageConfigurationTest {
|
||||
|
||||
@InjectDelayed
|
||||
private WelcomeMessageConfiguration welcomeMessageConfiguration;
|
||||
@Mock
|
||||
private Server server;
|
||||
@Mock
|
||||
private BukkitService bukkitService;
|
||||
@Mock
|
||||
private GeoIpService geoIpService;
|
||||
@Mock
|
||||
private PlayerCache playerCache;
|
||||
@DataFolder
|
||||
private File testPluginFolder;
|
||||
|
||||
private File welcomeFile;
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||
|
||||
@BeforeInjecting
|
||||
public void createPluginFolder() throws IOException {
|
||||
testPluginFolder = temporaryFolder.newFolder();
|
||||
welcomeFile = new File(testPluginFolder, "welcome.txt");
|
||||
welcomeFile.createNewFile();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldLoadWelcomeMessage() throws IOException {
|
||||
// given
|
||||
String welcomeMessage = "This is my welcome message for testing\nBye!";
|
||||
setWelcomeMessageAndReload(welcomeMessage);
|
||||
Player player = mock(Player.class);
|
||||
|
||||
// when
|
||||
List<String> result = welcomeMessageConfiguration.getWelcomeMessage(player);
|
||||
|
||||
// then
|
||||
assertThat(result, hasSize(2));
|
||||
assertThat(result, contains(welcomeMessage.split("\\n")));
|
||||
verifyZeroInteractions(player, playerCache, geoIpService, bukkitService, server);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReplaceNameAndIpAndCountry() throws IOException {
|
||||
// given
|
||||
String welcomeMessage = "Hello {PLAYER}, your IP is {IP}\nYour country is {COUNTRY}.\nWelcome to {SERVER}!";
|
||||
setWelcomeMessageAndReload(welcomeMessage);
|
||||
|
||||
Player player = mock(Player.class);
|
||||
given(player.getName()).willReturn("Bobby");
|
||||
TestHelper.mockPlayerIp(player, "123.45.66.77");
|
||||
given(geoIpService.getCountryName("123.45.66.77")).willReturn("Syldavia");
|
||||
given(server.getServerName()).willReturn("CrazyServer");
|
||||
|
||||
// when
|
||||
List<String> result = welcomeMessageConfiguration.getWelcomeMessage(player);
|
||||
|
||||
// then
|
||||
assertThat(result, hasSize(3));
|
||||
assertThat(result.get(0), equalTo("Hello Bobby, your IP is 123.45.66.77"));
|
||||
assertThat(result.get(1), equalTo("Your country is Syldavia."));
|
||||
assertThat(result.get(2), equalTo("Welcome to CrazyServer!"));
|
||||
verify(server, only()).getServerName();
|
||||
verifyZeroInteractions(playerCache);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldApplyOtherReplacements() throws IOException {
|
||||
// given
|
||||
String welcomeMessage = "{ONLINE}/{MAXPLAYERS} online\n{LOGINS} logged in\nYour world is {WORLD}\nServer: {VERSION}";
|
||||
setWelcomeMessageAndReload(welcomeMessage);
|
||||
given(bukkitService.getOnlinePlayers()).willReturn((List) Arrays.asList(mock(Player.class), mock(Player.class)));
|
||||
given(server.getMaxPlayers()).willReturn(20);
|
||||
given(playerCache.getLogged()).willReturn(1);
|
||||
given(server.getBukkitVersion()).willReturn("Bukkit-456.77.8");
|
||||
|
||||
World world = mock(World.class);
|
||||
given(world.getName()).willReturn("Hub");
|
||||
Player player = mock(Player.class);
|
||||
given(player.getWorld()).willReturn(world);
|
||||
|
||||
// when
|
||||
List<String> result = welcomeMessageConfiguration.getWelcomeMessage(player);
|
||||
|
||||
// then
|
||||
assertThat(result, hasSize(4));
|
||||
assertThat(result.get(0), equalTo("2/20 online"));
|
||||
assertThat(result.get(1), equalTo("1 logged in"));
|
||||
assertThat(result.get(2), equalTo("Your world is Hub"));
|
||||
assertThat(result.get(3), equalTo("Server: Bukkit-456.77.8"));
|
||||
}
|
||||
|
||||
private void setWelcomeMessageAndReload(String welcomeMessage) {
|
||||
try {
|
||||
Files.write(welcomeFile.toPath(), welcomeMessage.getBytes());
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException("Could not write to '" + welcomeFile + "'", e);
|
||||
}
|
||||
welcomeMessageConfiguration.reload();
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
package fr.xephi.authme.settings.commandconfig;
|
||||
|
||||
import com.google.common.io.Files;
|
||||
import fr.xephi.authme.ReflectionTestUtils;
|
||||
import fr.xephi.authme.TestHelper;
|
||||
import fr.xephi.authme.service.BukkitService;
|
||||
import fr.xephi.authme.service.GeoIpService;
|
||||
import fr.xephi.authme.settings.SettingsMigrationService;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.junit.Before;
|
||||
@@ -17,13 +17,7 @@ import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
import static fr.xephi.authme.settings.commandconfig.CommandConfigTestHelper.isCommand;
|
||||
import static java.lang.String.format;
|
||||
import static org.hamcrest.Matchers.anEmptyMap;
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
@@ -31,6 +25,7 @@ import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.only;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
import static org.mockito.Mockito.verifyZeroInteractions;
|
||||
|
||||
/**
|
||||
* Test for {@link CommandManager}.
|
||||
@@ -41,104 +36,116 @@ public class CommandManagerTest {
|
||||
private static final String TEST_FILES_FOLDER = "/fr/xephi/authme/settings/commandconfig/";
|
||||
|
||||
private CommandManager manager;
|
||||
private Player player;
|
||||
|
||||
@InjectMocks
|
||||
private CommandMigrationService commandMigrationService;
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||
|
||||
@Mock
|
||||
private BukkitService bukkitService;
|
||||
@Mock
|
||||
private GeoIpService geoIpService;
|
||||
@Mock
|
||||
private SettingsMigrationService settingsMigrationService;
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||
|
||||
private File testFolder;
|
||||
|
||||
@Before
|
||||
public void setup() throws IOException {
|
||||
testFolder = temporaryFolder.newFolder();
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void shouldLoadCompleteFile() {
|
||||
// given
|
||||
copyJarFileAsCommandsYml(TEST_FILES_FOLDER + "commands.complete.yml");
|
||||
|
||||
// when
|
||||
initManager();
|
||||
|
||||
// then
|
||||
CommandConfig commandConfig = ReflectionTestUtils.getFieldValue(CommandManager.class, manager, "commandConfig");
|
||||
assertThat(commandConfig.getOnJoin().keySet(), contains("broadcast"));
|
||||
assertThat(commandConfig.getOnJoin().values(), contains(isCommand("broadcast %p has joined", Executor.CONSOLE)));
|
||||
assertThat(commandConfig.getOnRegister().keySet(), contains("announce", "notify"));
|
||||
assertThat(commandConfig.getOnRegister().values(), contains(
|
||||
isCommand("me I just registered", Executor.PLAYER),
|
||||
isCommand("log %p registered", Executor.CONSOLE)));
|
||||
assertThat(commandConfig.getOnLogin().keySet(), contains("welcome", "show_motd", "display_list"));
|
||||
assertThat(commandConfig.getOnLogin().values(), contains(
|
||||
isCommand("msg %p Welcome back", Executor.CONSOLE),
|
||||
isCommand("motd", Executor.PLAYER),
|
||||
isCommand("list", Executor.PLAYER)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldLoadIncompleteFile() {
|
||||
// given
|
||||
copyJarFileAsCommandsYml(TEST_FILES_FOLDER + "commands.incomplete.yml");
|
||||
|
||||
// when
|
||||
initManager();
|
||||
|
||||
// then
|
||||
CommandConfig commandConfig = ReflectionTestUtils.getFieldValue(CommandManager.class, manager, "commandConfig");
|
||||
assertThat(commandConfig.getOnJoin().values(), contains(isCommand("broadcast %p has joined", Executor.CONSOLE)));
|
||||
assertThat(commandConfig.getOnLogin().values(), contains(
|
||||
isCommand("msg %p Welcome back", Executor.CONSOLE),
|
||||
isCommand("list", Executor.PLAYER)));
|
||||
assertThat(commandConfig.getOnRegister(), anEmptyMap());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldExecuteCommandsOnJoin() {
|
||||
// given
|
||||
String name = "Bobby1";
|
||||
|
||||
// when
|
||||
testCommandExecution(name, CommandManager::runCommandsOnJoin);
|
||||
|
||||
// then
|
||||
verify(bukkitService, only()).dispatchConsoleCommand(format("broadcast %s has joined", name));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldExecuteCommandsOnRegister() {
|
||||
// given
|
||||
String name = "luis";
|
||||
|
||||
// when
|
||||
testCommandExecution(name, CommandManager::runCommandsOnRegister);
|
||||
|
||||
// then
|
||||
verify(bukkitService).dispatchCommand(any(Player.class), eq("me I just registered"));
|
||||
verify(bukkitService).dispatchConsoleCommand(format("log %s registered", name));
|
||||
verifyNoMoreInteractions(bukkitService);
|
||||
player = mockPlayer();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldExecuteCommandsOnLogin() {
|
||||
// given
|
||||
String name = "plaYer01";
|
||||
copyJarFileAsCommandsYml(TEST_FILES_FOLDER + "commands.complete.yml");
|
||||
initManager();
|
||||
|
||||
// when
|
||||
testCommandExecution(name, CommandManager::runCommandsOnLogin);
|
||||
manager.runCommandsOnLogin(player);
|
||||
|
||||
// then
|
||||
verify(bukkitService).dispatchConsoleCommand(format("msg %s Welcome back", name));
|
||||
verify(bukkitService).dispatchConsoleCommand("msg Bobby Welcome back");
|
||||
verify(bukkitService).dispatchCommand(any(Player.class), eq("motd"));
|
||||
verify(bukkitService).dispatchCommand(any(Player.class), eq("list"));
|
||||
verifyNoMoreInteractions(bukkitService);
|
||||
verifyZeroInteractions(geoIpService);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldExecuteCommandsOnLoginWithIncompleteConfig() {
|
||||
// given
|
||||
copyJarFileAsCommandsYml(TEST_FILES_FOLDER + "commands.incomplete.yml");
|
||||
initManager();
|
||||
|
||||
// when
|
||||
manager.runCommandsOnLogin(player);
|
||||
|
||||
// then
|
||||
verify(bukkitService).dispatchConsoleCommand("msg Bobby Welcome back, bob");
|
||||
verify(bukkitService).dispatchCommand(any(Player.class), eq("list"));
|
||||
verifyNoMoreInteractions(bukkitService);
|
||||
verifyZeroInteractions(geoIpService);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldExecuteCommandsOnJoin() {
|
||||
// given
|
||||
copyJarFileAsCommandsYml(TEST_FILES_FOLDER + "commands.complete.yml");
|
||||
initManager();
|
||||
|
||||
// when
|
||||
manager.runCommandsOnJoin(player);
|
||||
|
||||
// then
|
||||
verify(bukkitService, only()).dispatchConsoleCommand("broadcast bob has joined");
|
||||
verifyZeroInteractions(geoIpService);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldExecuteCommandsOnJoinWithIncompleteConfig() {
|
||||
// given
|
||||
copyJarFileAsCommandsYml(TEST_FILES_FOLDER + "commands.incomplete.yml");
|
||||
initManager();
|
||||
|
||||
// when
|
||||
manager.runCommandsOnJoin(player);
|
||||
|
||||
// then
|
||||
verify(bukkitService, only()).dispatchConsoleCommand("broadcast Bobby has joined");
|
||||
verifyZeroInteractions(geoIpService);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldExecuteCommandsOnRegister() {
|
||||
// given
|
||||
copyJarFileAsCommandsYml(TEST_FILES_FOLDER + "commands.complete.yml");
|
||||
initManager();
|
||||
|
||||
// when
|
||||
manager.runCommandsOnRegister(player);
|
||||
|
||||
// then
|
||||
verify(bukkitService).dispatchCommand(any(Player.class), eq("me I just registered"));
|
||||
verify(bukkitService).dispatchConsoleCommand("log Bobby (127.0.0.3, Syldavia) registered");
|
||||
verifyNoMoreInteractions(bukkitService);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldExecuteCommandsOnRegisterWithIncompleteConfig() {
|
||||
// given
|
||||
copyJarFileAsCommandsYml(TEST_FILES_FOLDER + "commands.incomplete.yml");
|
||||
initManager();
|
||||
|
||||
// when
|
||||
manager.runCommandsOnRegister(player);
|
||||
|
||||
// then
|
||||
verifyZeroInteractions(bukkitService, geoIpService);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -147,18 +154,8 @@ public class CommandManagerTest {
|
||||
TestHelper.validateHasOnlyPrivateEmptyConstructor(CommandSettingsHolder.class);
|
||||
}
|
||||
|
||||
|
||||
private void testCommandExecution(String playerName, BiConsumer<CommandManager, Player> testMethod) {
|
||||
copyJarFileAsCommandsYml(TEST_FILES_FOLDER + "commands.complete.yml");
|
||||
initManager();
|
||||
Player player = mock(Player.class);
|
||||
given(player.getName()).willReturn(playerName);
|
||||
|
||||
testMethod.accept(manager, player);
|
||||
}
|
||||
|
||||
private void initManager() {
|
||||
manager = new CommandManager(testFolder, bukkitService, commandMigrationService);
|
||||
manager = new CommandManager(testFolder, bukkitService, geoIpService, commandMigrationService);
|
||||
}
|
||||
|
||||
private void copyJarFileAsCommandsYml(String path) {
|
||||
@@ -171,4 +168,13 @@ public class CommandManagerTest {
|
||||
}
|
||||
}
|
||||
|
||||
private Player mockPlayer() {
|
||||
Player player = mock(Player.class);
|
||||
given(player.getName()).willReturn("Bobby");
|
||||
given(player.getDisplayName()).willReturn("bob");
|
||||
String ip = "127.0.0.3";
|
||||
TestHelper.mockPlayerIp(player, ip);
|
||||
given(geoIpService.getCountryName(ip)).willReturn("Syldavia");
|
||||
return player;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,102 +0,0 @@
|
||||
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));
|
||||
}
|
||||
}
|
||||
@@ -137,6 +137,11 @@ public class FileUtilsTest {
|
||||
assertThat(result, equalTo("path" + File.separator + "to" + File.separator + "test-file.txt"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHaveHiddenConstructor() {
|
||||
TestHelper.validateHasOnlyPrivateEmptyConstructor(FileUtils.class);
|
||||
}
|
||||
|
||||
private static void createFiles(File... files) throws IOException {
|
||||
for (File file : files) {
|
||||
boolean result = file.getParentFile().mkdirs() & file.createNewFile();
|
||||
|
||||
@@ -26,7 +26,7 @@ public class GenerateCommandsYml implements AutoToolTask {
|
||||
// Get default and add sample entry
|
||||
CommandConfig commandConfig = CommandSettingsHolder.COMMANDS.getDefaultValue();
|
||||
commandConfig.setOnLogin(
|
||||
ImmutableMap.of("welcome", newCommand("msg %p Welcome back!", Executor.PLAYER)));
|
||||
ImmutableMap.of("welcome", new Command("msg %p Welcome back!", Executor.PLAYER)));
|
||||
|
||||
// Export the value to the file
|
||||
SettingsManager settingsManager = new SettingsManager(
|
||||
@@ -41,11 +41,4 @@ public class GenerateCommandsYml implements AutoToolTask {
|
||||
public String getTaskName() {
|
||||
return "generateCommandsYml";
|
||||
}
|
||||
|
||||
private static Command newCommand(String commandLine, Executor executor) {
|
||||
Command command = new Command();
|
||||
command.setCommand(commandLine);
|
||||
command.setExecutor(executor);
|
||||
return command;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,14 +2,14 @@
|
||||
|
||||
onJoin:
|
||||
broadcast:
|
||||
command: 'broadcast %p has joined'
|
||||
command: 'broadcast %nick has joined'
|
||||
executor: CONSOLE
|
||||
onRegister:
|
||||
announce:
|
||||
command: 'me I just registered'
|
||||
executor: PLAYER
|
||||
notify:
|
||||
command: 'log %p registered'
|
||||
command: 'log %p (%ip, %country) registered'
|
||||
executor: CONSOLE
|
||||
onLogin:
|
||||
welcome:
|
||||
|
||||
@@ -6,7 +6,7 @@ onJoin:
|
||||
executor: CONSOLE
|
||||
onLogin:
|
||||
welcome:
|
||||
command: 'msg %p Welcome back'
|
||||
command: 'msg %p Welcome back, %nick'
|
||||
executor: CONSOLE
|
||||
show_motd:
|
||||
# command: 'motd' <-- mandatory property, so entry should be ignored
|
||||
|
||||
Reference in New Issue
Block a user