#742 Create test that plugin.yml corresponds to command definitions

- Create test
- Fix definitions to correspond
This commit is contained in:
ljacqu 2016-06-02 15:49:21 +02:00
parent e75cff5fb8
commit 9b5009eb8c
3 changed files with 104 additions and 4 deletions

View File

@ -291,7 +291,7 @@ public class CommandInitializer {
// Register the base login command
final CommandDescription LOGIN_BASE = CommandDescription.builder()
.parent(null)
.labels("login", "l")
.labels("login", "l", "log")
.description("Login command")
.detailedDescription("Command to log in using AuthMeReloaded.")
.withArgument("password", "Login password", false)
@ -324,7 +324,7 @@ public class CommandInitializer {
// Register the base unregister command
CommandDescription UNREGISTER_BASE = CommandDescription.builder()
.parent(null)
.labels("unreg", "unregister")
.labels("unregister", "unreg")
.description("Unregistration Command")
.detailedDescription("Command to unregister using AuthMeReloaded.")
.withArgument("password", "Password", false)
@ -347,7 +347,7 @@ public class CommandInitializer {
// Register the base Email command
CommandDescription EMAIL_BASE = CommandDescription.builder()
.parent(null)
.labels("email", "mail")
.labels("email")
.description("Email command")
.detailedDescription("The AuthMeReloaded Email command base.")
.executableCommand(initializer.newInstance(EmailBaseCommand.class))
@ -392,7 +392,7 @@ public class CommandInitializer {
// Register the base captcha command
CommandDescription CAPTCHA_BASE = CommandDescription.builder()
.parent(null)
.labels("captcha", "capt")
.labels("captcha")
.description("Captcha Command")
.detailedDescription("Captcha command for AuthMeReloaded.")
.withArgument("captcha", "The Captcha", false)

View File

@ -28,12 +28,14 @@ commands:
changepassword:
description: Change password of an account
usage: /changepassword <oldPassword> <newPassword>
aliases: [cp,changepass]
logout:
description: Logout
usage: /logout
unregister:
description: Unregister your account
usage: /unregister <password>
aliases: [unreg]
email:
description: Add Email or recover password
usage: '/email add your@email.com your@email.com|change oldEmail newEmail|recovery your@email.com'

View File

@ -0,0 +1,98 @@
package fr.xephi.authme.command;
import fr.xephi.authme.initialization.AuthMeServiceInitializer;
import org.bukkit.configuration.MemorySection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static fr.xephi.authme.TestHelper.getJarFile;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
/**
* Checks that the commands declared in plugin.yml correspond
* to the ones built by the {@link CommandInitializer}.
*/
public class CommandConsistencyTest {
@Test
public void shouldHaveEqualDefinitions() {
// given
Collection<List<String>> initializedCommands = initializeCommands();
Map<String, List<String>> pluginFileLabels = getLabelsFromPluginFile();
// when / then
assertThat("number of base commands are equal in plugin.yml and CommandInitializer",
initializedCommands.size(), equalTo(pluginFileLabels.size()));
for (List<String> commandLabels : initializedCommands) {
List<String> pluginYmlLabels = pluginFileLabels.get(commandLabels.get(0));
// NB: the first label in CommandDescription needs to correspond to the key in plugin.yml
assertThat("plugin.yml contains definition for command '" + commandLabels.get(0) + "'",
pluginYmlLabels, not(nullValue()));
assertThat("plugin.yml and CommandDescription have same alternative labels for /" + commandLabels.get(0),
pluginYmlLabels, containsInAnyOrder(commandLabels.subList(1, commandLabels.size()).toArray()));
}
}
/**
* Gets the command definitions from CommandInitializer and returns the
* labels of all base commands.
*
* @return collection of all base command labels
*/
private static Collection<List<String>> initializeCommands() {
AuthMeServiceInitializer injector = mock(AuthMeServiceInitializer.class);
given(injector.newInstance(any(Class.class))).willAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return mock((Class<?>) invocation.getArguments()[0]);
}
});
CommandInitializer initializer = new CommandInitializer(injector);
Collection<List<String>> commandLabels = new ArrayList<>();
for (CommandDescription baseCommand : initializer.getCommands()) {
commandLabels.add(baseCommand.getLabels());
}
return commandLabels;
}
/**
* Reads plugin.yml and returns the defined commands by main label and aliases.
*
* @return collection of all labels and their aliases
*/
@SuppressWarnings("unchecked")
private static Map<String, List<String>> getLabelsFromPluginFile() {
FileConfiguration pluginFile = YamlConfiguration.loadConfiguration(getJarFile("/plugin.yml"));
MemorySection commandList = (MemorySection) pluginFile.get("commands");
Map<String, Object> commandDefinitions = commandList.getValues(false);
Map<String, List<String>> commandLabels = new HashMap<>();
for (Map.Entry<String, Object> commandDefinition : commandDefinitions.entrySet()) {
MemorySection definition = (MemorySection) commandDefinition.getValue();
List<String> alternativeLabels = definition.get("aliases") == null
? Collections.EMPTY_LIST
: (List<String>) definition.get("aliases");
commandLabels.put(commandDefinition.getKey(), alternativeLabels);
}
return commandLabels;
}
}