From d7513ecc7be42dfc7fc1fd7b0aaf2e7d580b0929 Mon Sep 17 00:00:00 2001 From: ljacqu Date: Sun, 29 Nov 2015 12:04:01 +0100 Subject: [PATCH] Fix nullpointer when command requires no permissions - Add test to verify connections among the command initialization --- .../authme/command/FoundCommandResult.java | 11 ++++++---- .../authme/command/CommandManagerTest.java | 22 +++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/main/java/fr/xephi/authme/command/FoundCommandResult.java b/src/main/java/fr/xephi/authme/command/FoundCommandResult.java index b236f09a..d369a8a7 100644 --- a/src/main/java/fr/xephi/authme/command/FoundCommandResult.java +++ b/src/main/java/fr/xephi/authme/command/FoundCommandResult.java @@ -108,12 +108,15 @@ public class FoundCommandResult { * @return True if the command sender has permission, false otherwise. */ public boolean hasPermission(CommandSender sender) { - // Make sure the command description is valid - if (this.commandDescription == null) + if (commandDescription == null) { return false; + } else if (commandDescription.getCommandPermissions() == null) { + return true; + } - // Get and return the permission - return this.commandDescription.getCommandPermissions().hasPermission(sender); + // TODO: Move permissions check to the permission package; command package should not define permission-checking + // API + return commandDescription.getCommandPermissions().hasPermission(sender); } /** diff --git a/src/test/java/fr/xephi/authme/command/CommandManagerTest.java b/src/test/java/fr/xephi/authme/command/CommandManagerTest.java index 14b725f1..c15b203d 100644 --- a/src/test/java/fr/xephi/authme/command/CommandManagerTest.java +++ b/src/test/java/fr/xephi/authme/command/CommandManagerTest.java @@ -65,6 +65,28 @@ public class CommandManagerTest { walkThroughCommands(manager.getCommandDescriptions(), descriptionTester); } + /** Ensure that all children of a command stored the parent. */ + @Test + public void shouldHaveConnectionBetweenParentAndChild() { + // given + BiConsumer connectionTester = new BiConsumer() { + @Override + public void accept(CommandDescription command, int depth) { + if (command.hasChildren()) { + for (CommandDescription child : command.getChildren()) { + assertThat(command.equals(child.getParent()), equalTo(true)); + } + } + // Checking that the parent has the current command as child is redundant as this is how we can traverse + // the "command tree" in the first place - if we're here, it's that the parent definitely has the + // command as child. + } + }; + + // when/then + walkThroughCommands(manager.getCommandDescriptions(), connectionTester); + } + @Test public void shouldNotDefineSameLabelTwice() { // given