#1026 Add more tags for forced commands (lazily replaced) (#214)

* #1026 Add more tags for forced commands (lazily replaced)
- Extract lazy replacement of tags to its own class
- Implement wrapper to replace a String property within an object
  - Use wrapper in command manager and add new tags

* Make argument type generic in lazy tags util
This commit is contained in:
ljacqu
2017-01-29 13:54:37 +01:00
committed by Gabriele C
parent 7578247085
commit 89c70ff447
12 changed files with 329 additions and 189 deletions
@@ -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;
}
}
@@ -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