#411 Forced commands: initial implementation

This commit is contained in:
ljacqu
2016-11-17 21:02:01 +01:00
parent 40cf79d2c1
commit 4214c6dc80
11 changed files with 351 additions and 30 deletions
@@ -3,8 +3,6 @@ package fr.xephi.authme.datasource;
import com.google.common.io.Files;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.datasource.FlatFile;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@@ -4,7 +4,12 @@ import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ReflectionTestUtils;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.PluginSettings;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
@@ -17,6 +22,7 @@ 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.verify;
/**
* Test for {@link BukkitService}.
@@ -24,10 +30,21 @@ import static org.mockito.Mockito.mock;
@RunWith(MockitoJUnitRunner.class)
public class BukkitServiceTest {
private BukkitService bukkitService;
@Mock
private AuthMe authMe;
@Mock
private Settings settings;
@Mock
private Server server;
@Before
public void constructBukkitService() {
ReflectionTestUtils.setField(Bukkit.class, null, "server", server);
given(settings.getProperty(PluginSettings.USE_ASYNC_TASKS)).willReturn(true);
bukkitService = new BukkitService(authMe, settings);
}
/**
* Checks that {@link BukkitService#getOnlinePlayersIsCollection} is initialized to {@code true} on startup;
@@ -35,11 +52,7 @@ public class BukkitServiceTest {
*/
@Test
public void shouldHavePlayerListAsCollectionMethod() {
// given
given(settings.getProperty(PluginSettings.USE_ASYNC_TASKS)).willReturn(true);
BukkitService bukkitService = new BukkitService(authMe, settings);
// when
// given / when
boolean doesMethodReturnCollection = ReflectionTestUtils
.getFieldValue(BukkitService.class, bukkitService, "getOnlinePlayersIsCollection");
@@ -50,8 +63,6 @@ public class BukkitServiceTest {
@Test
public void shouldRetrieveListOfOnlinePlayersFromReflectedMethod() {
// given
given(settings.getProperty(PluginSettings.USE_ASYNC_TASKS)).willReturn(true);
BukkitService bukkitService = new BukkitService(authMe, settings);
ReflectionTestUtils.setField(BukkitService.class, bukkitService, "getOnlinePlayersIsCollection", false);
ReflectionTestUtils.setField(BukkitService.class, bukkitService, "getOnlinePlayers",
ReflectionTestUtils.getMethod(BukkitServiceTest.class, "onlinePlayersImpl"));
@@ -63,6 +74,33 @@ public class BukkitServiceTest {
assertThat(players, hasSize(2));
}
@Test
public void shouldDispatchCommand() {
// given
CommandSender sender = mock(CommandSender.class);
String command = "help test abc";
// when
bukkitService.dispatchCommand(sender, command);
// then
verify(server).dispatchCommand(sender, command);
}
@Test
public void shouldDispatchConsoleCommand() {
// given
ConsoleCommandSender consoleSender = mock(ConsoleCommandSender.class);
given(server.getConsoleSender()).willReturn(consoleSender);
String command = "my command";
// when
bukkitService.dispatchConsoleCommand(command);
// then
verify(server).dispatchCommand(consoleSender, command);
}
// Note: This method is used through reflections
public static Player[] onlinePlayersImpl() {
return new Player[]{
@@ -0,0 +1,57 @@
package tools.filegeneration;
import com.github.authme.configme.SettingsManager;
import com.github.authme.configme.resource.YamlFileResource;
import com.google.common.collect.ImmutableMap;
import fr.xephi.authme.settings.commandconfig.Command;
import fr.xephi.authme.settings.commandconfig.CommandConfig;
import fr.xephi.authme.settings.commandconfig.CommandSettingsHolder;
import fr.xephi.authme.settings.commandconfig.Executor;
import tools.utils.AutoToolTask;
import tools.utils.ToolsConstants;
import java.io.File;
import java.util.Scanner;
/**
* Generates the commands.yml file that corresponds to the default in the code.
*/
public class GenerateCommandsYml implements AutoToolTask {
private static final String COMMANDS_YML_FILE = ToolsConstants.MAIN_RESOURCES_ROOT + "commands.yml";
@Override
public void execute(Scanner scanner) {
executeDefault();
}
@Override
public void executeDefault() {
File file = new File(COMMANDS_YML_FILE);
// Get default and add sample entry
CommandConfig commandConfig = CommandSettingsHolder.COMMANDS.getDefaultValue();
commandConfig.setOnLogin(
ImmutableMap.of("welcome", newCommand("msg %p Welcome back!", Executor.PLAYER)));
// Export the value to the file
SettingsManager settingsManager = new SettingsManager(
new YamlFileResource(file), null, CommandSettingsHolder.class);
settingsManager.setProperty(CommandSettingsHolder.COMMANDS, commandConfig);
settingsManager.save();
System.out.println("Updated " + COMMANDS_YML_FILE);
}
@Override
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;
}
}