#411 Improve command migration handling, write tests

This commit is contained in:
ljacqu
2016-11-24 17:39:57 +01:00
parent e83935c11e
commit f6ed39b118
14 changed files with 248 additions and 57 deletions
@@ -4,26 +4,33 @@ 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.settings.Settings;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import fr.xephi.authme.settings.SettingsMigrationService;
import org.bukkit.entity.Player;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.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.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
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.verifyNoMoreInteractions;
/**
* Test for {@link CommandManager}.
@@ -33,16 +40,18 @@ public class CommandManagerTest {
private static final String TEST_FILES_FOLDER = "/fr/xephi/authme/settings/commandconfig/";
private CommandManager manager;
@InjectMocks
private CommandsMigrater commandsMigrater;
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
private CommandManager manager;
@Mock
private BukkitService bukkitService;
@Mock
private Settings settings;
@Mock
private CommandsMigrater commandsMigrater;
private SettingsMigrationService settingsMigrationService;
private File testFolder;
@Before
@@ -75,10 +84,9 @@ public class CommandManagerTest {
}
@Test
@Ignore // TODO #411: Implement tested behavior
public void shouldLoadIncompleteFile() {
// given
File configFile = copyJarFileAsCommandsYml(TEST_FILES_FOLDER + "commands.incomplete.yml");
copyJarFileAsCommandsYml(TEST_FILES_FOLDER + "commands.incomplete.yml");
// when
initManager();
@@ -90,23 +98,74 @@ public class CommandManagerTest {
isCommand("msg %p Welcome back", Executor.CONSOLE),
isCommand("list", Executor.PLAYER)));
assertThat(commandConfig.getOnRegister(), anEmptyMap());
}
// verify that we have rewritten the file with the proper sections
FileConfiguration configuration = YamlConfiguration.loadConfiguration(configFile);
assertThat(configuration.contains("onRegister"), equalTo(true));
assertThat(configuration.contains("doesNotExist"), equalTo(false));
@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);
}
@Test
public void shouldExecuteCommandsOnLogin() {
// given
String name = "plaYer01";
// when
testCommandExecution(name, CommandManager::runCommandsOnLogin);
// then
verify(bukkitService).dispatchConsoleCommand(format("msg %s Welcome back", name));
verify(bukkitService).dispatchCommand(any(Player.class), eq("motd"));
verify(bukkitService).dispatchCommand(any(Player.class), eq("list"));
verifyNoMoreInteractions(bukkitService);
}
@Test
public void shouldHaveHiddenConstructorInSettingsHolderClass() {
// given / when / then
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, commandsMigrater, settings);
manager = new CommandManager(testFolder, bukkitService, commandsMigrater);
}
private File copyJarFileAsCommandsYml(String path) {
private void copyJarFileAsCommandsYml(String path) {
File source = TestHelper.getJarFile(path);
File destination = new File(testFolder, "commands.yml");
try {
Files.copy(source, destination);
return destination;
} catch (IOException e) {
throw new IllegalStateException(e);
}