From 9b5009eb8c60a97657b4d985af09b03cfe983d26 Mon Sep 17 00:00:00 2001 From: ljacqu Date: Thu, 2 Jun 2016 15:49:21 +0200 Subject: [PATCH] #742 Create test that plugin.yml corresponds to command definitions - Create test - Fix definitions to correspond --- .../authme/command/CommandInitializer.java | 8 +- src/main/resources/plugin.yml | 2 + .../command/CommandConsistencyTest.java | 98 +++++++++++++++++++ 3 files changed, 104 insertions(+), 4 deletions(-) create mode 100644 src/test/java/fr/xephi/authme/command/CommandConsistencyTest.java diff --git a/src/main/java/fr/xephi/authme/command/CommandInitializer.java b/src/main/java/fr/xephi/authme/command/CommandInitializer.java index 59ab9c516..7f6b2e26d 100644 --- a/src/main/java/fr/xephi/authme/command/CommandInitializer.java +++ b/src/main/java/fr/xephi/authme/command/CommandInitializer.java @@ -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) diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 63a9d81cb..ef48d2e26 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -28,12 +28,14 @@ commands: changepassword: description: Change password of an account usage: /changepassword + aliases: [cp,changepass] logout: description: Logout usage: /logout unregister: description: Unregister your account usage: /unregister + 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' diff --git a/src/test/java/fr/xephi/authme/command/CommandConsistencyTest.java b/src/test/java/fr/xephi/authme/command/CommandConsistencyTest.java new file mode 100644 index 000000000..94a723b17 --- /dev/null +++ b/src/test/java/fr/xephi/authme/command/CommandConsistencyTest.java @@ -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> initializedCommands = initializeCommands(); + Map> pluginFileLabels = getLabelsFromPluginFile(); + + // when / then + assertThat("number of base commands are equal in plugin.yml and CommandInitializer", + initializedCommands.size(), equalTo(pluginFileLabels.size())); + for (List commandLabels : initializedCommands) { + List 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> initializeCommands() { + AuthMeServiceInitializer injector = mock(AuthMeServiceInitializer.class); + given(injector.newInstance(any(Class.class))).willAnswer(new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + return mock((Class) invocation.getArguments()[0]); + } + }); + CommandInitializer initializer = new CommandInitializer(injector); + Collection> 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> getLabelsFromPluginFile() { + FileConfiguration pluginFile = YamlConfiguration.loadConfiguration(getJarFile("/plugin.yml")); + MemorySection commandList = (MemorySection) pluginFile.get("commands"); + Map commandDefinitions = commandList.getValues(false); + + Map> commandLabels = new HashMap<>(); + for (Map.Entry commandDefinition : commandDefinitions.entrySet()) { + MemorySection definition = (MemorySection) commandDefinition.getValue(); + List alternativeLabels = definition.get("aliases") == null + ? Collections.EMPTY_LIST + : (List) definition.get("aliases"); + commandLabels.put(commandDefinition.getKey(), alternativeLabels); + } + return commandLabels; + } + +}