|
| 1 | +package com.bencodez.simpleapi.tests; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 5 | +import static org.mockito.Mockito.mock; |
| 6 | +import static org.mockito.Mockito.never; |
| 7 | +import static org.mockito.Mockito.verify; |
| 8 | + |
| 9 | +import org.bukkit.command.CommandSender; |
| 10 | +import org.bukkit.entity.Player; |
| 11 | +import org.bukkit.plugin.java.JavaPlugin; |
| 12 | +import org.junit.jupiter.api.BeforeEach; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | + |
| 15 | +import com.bencodez.simpleapi.command.PlayerCommandHandler; |
| 16 | +import com.bencodez.simpleapi.scheduler.BukkitScheduler; |
| 17 | + |
| 18 | +class PlayerCommandHandlerTest { |
| 19 | + private JavaPlugin plugin; |
| 20 | + private BukkitScheduler scheduler; |
| 21 | + |
| 22 | + @BeforeEach |
| 23 | + void setUp() { |
| 24 | + plugin = mock(JavaPlugin.class); |
| 25 | + scheduler = mock(BukkitScheduler.class); |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + void forceConsoleRejectsPlayers() { |
| 30 | + TestPlayerCommandHandler handler = new TestPlayerCommandHandler(true, true); |
| 31 | + Player player = mock(Player.class); |
| 32 | + |
| 33 | + assertTrue(handler.runCommand(player, new String[] { "command" })); |
| 34 | + assertTrue(handler.isForceConsole()); |
| 35 | + verify(scheduler, never()).runTaskAsynchronously(org.mockito.ArgumentMatchers.eq(plugin), |
| 36 | + org.mockito.ArgumentMatchers.any(Runnable.class)); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + void forceConsoleAllowsConsoleWhenConsoleIsAllowed() { |
| 41 | + TestPlayerCommandHandler handler = new TestPlayerCommandHandler(true, true); |
| 42 | + CommandSender console = mock(CommandSender.class); |
| 43 | + |
| 44 | + assertTrue(handler.runCommand(console, new String[] { "command" })); |
| 45 | + verify(scheduler).runTaskAsynchronously(org.mockito.ArgumentMatchers.eq(plugin), |
| 46 | + org.mockito.ArgumentMatchers.any(Runnable.class)); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void allowConsoleStillControlsConsoleForOrdinaryCommands() { |
| 51 | + TestPlayerCommandHandler consoleDisallowed = new TestPlayerCommandHandler(false, false); |
| 52 | + TestPlayerCommandHandler consoleAllowed = new TestPlayerCommandHandler(true, false); |
| 53 | + CommandSender console = mock(CommandSender.class); |
| 54 | + |
| 55 | + assertTrue(consoleDisallowed.runCommand(console, new String[] { "command" })); |
| 56 | + verify(scheduler, never()).runTaskAsynchronously(org.mockito.ArgumentMatchers.eq(plugin), |
| 57 | + org.mockito.ArgumentMatchers.any(Runnable.class)); |
| 58 | + |
| 59 | + assertTrue(consoleAllowed.runCommand(console, new String[] { "command" })); |
| 60 | + verify(scheduler).runTaskAsynchronously(org.mockito.ArgumentMatchers.eq(plugin), |
| 61 | + org.mockito.ArgumentMatchers.any(Runnable.class)); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void existingConstructorsKeepTheirPreviousDefaults() { |
| 66 | + TestPlayerCommandHandler argsAndPerm = new TestPlayerCommandHandler(new String[] { "command" }, ""); |
| 67 | + TestPlayerCommandHandler help = new TestPlayerCommandHandler(new String[] { "command" }, "", "help"); |
| 68 | + TestPlayerCommandHandler allowConsole = new TestPlayerCommandHandler(new String[] { "command" }, "", "help", |
| 69 | + false); |
| 70 | + |
| 71 | + assertTrue(argsAndPerm.isAllowConsole()); |
| 72 | + assertFalse(argsAndPerm.isForceConsole()); |
| 73 | + assertTrue(help.isAllowConsole()); |
| 74 | + assertFalse(help.isForceConsole()); |
| 75 | + assertFalse(allowConsole.isAllowConsole()); |
| 76 | + assertFalse(allowConsole.isForceConsole()); |
| 77 | + } |
| 78 | + |
| 79 | + private class TestPlayerCommandHandler extends PlayerCommandHandler { |
| 80 | + TestPlayerCommandHandler(String[] args, String perm) { |
| 81 | + super(plugin, args, perm); |
| 82 | + } |
| 83 | + |
| 84 | + TestPlayerCommandHandler(String[] args, String perm, String helpMessage) { |
| 85 | + super(plugin, args, perm, helpMessage); |
| 86 | + } |
| 87 | + |
| 88 | + TestPlayerCommandHandler(String[] args, String perm, String helpMessage, boolean allowConsole) { |
| 89 | + super(plugin, args, perm, helpMessage, allowConsole); |
| 90 | + } |
| 91 | + |
| 92 | + TestPlayerCommandHandler(boolean allowConsole, boolean forceConsole) { |
| 93 | + super(plugin, new String[] { "command" }, "", "help", allowConsole, forceConsole); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public void debug(String debug) { |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public void executeAll(CommandSender sender, String[] args) { |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void executeSinglePlayer(CommandSender sender, String[] args) { |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public String formatNoPerms() { |
| 110 | + return "No permission"; |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public String formatNotNumber() { |
| 115 | + return "Not a number"; |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public BukkitScheduler getBukkitScheduler() { |
| 120 | + return scheduler; |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public String getHelpLine() { |
| 125 | + return "Help line"; |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments