From 01022a250085780c513035b059199f63b6905f75 Mon Sep 17 00:00:00 2001 From: tastybento Date: Wed, 8 May 2019 12:18:33 -0700 Subject: [PATCH] Added bentobox addon reload command. --- .../commands/BentoBoxReloadCommand.java | 22 ++- .../bentobox/managers/AddonsManager.java | 1 + src/main/resources/locales/en-US.yml | 5 +- .../bentobox/api/addons/AddonTest.java | 8 + .../commands/BentoBoxReloadCommandTest.java | 187 ++++++++++++++++++ 5 files changed, 219 insertions(+), 4 deletions(-) create mode 100644 src/test/java/world/bentobox/bentobox/commands/BentoBoxReloadCommandTest.java diff --git a/src/main/java/world/bentobox/bentobox/commands/BentoBoxReloadCommand.java b/src/main/java/world/bentobox/bentobox/commands/BentoBoxReloadCommand.java index be62b5e9f..6fa6118c9 100644 --- a/src/main/java/world/bentobox/bentobox/commands/BentoBoxReloadCommand.java +++ b/src/main/java/world/bentobox/bentobox/commands/BentoBoxReloadCommand.java @@ -1,9 +1,13 @@ package world.bentobox.bentobox.commands; import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; +import world.bentobox.bentobox.api.addons.Addon; import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.commands.ConfirmableCommand; +import world.bentobox.bentobox.api.localization.TextVariables; import world.bentobox.bentobox.api.user.User; /** @@ -24,9 +28,11 @@ public class BentoBoxReloadCommand extends ConfirmableCommand { @Override public void setup() { setPermission("admin.reload"); + setParametersHelp("commands.bentobox.reload.parameters"); setDescription("commands.bentobox.reload.description"); } + @Override public boolean execute(User user, String label, List args) { if (args.isEmpty()) { @@ -44,14 +50,24 @@ public class BentoBoxReloadCommand extends ConfirmableCommand { user.sendMessage("commands.bentobox.reload.locales-reloaded"); }); } else if (args.size() == 1) { - if (!getPlugin().getAddonsManager().getAddonByName(args.get(0)).isPresent()) { - user.sendRawMessage("Unknown addon"); + Optional addon = getPlugin().getAddonsManager().getAddonByName(args.get(0)); + if (!addon.isPresent()) { + user.sendMessage("commands.bentobox.reload.unknown-addon"); return false; } - this.askConfirmation(user, () -> getPlugin().getAddonsManager().getAddonByName(args.get(0)).ifPresent(getPlugin().getAddonsManager()::reloadAddon)); + + this.askConfirmation(user, () -> { + user.sendMessage("commands.bentobox.reload.addon", TextVariables.DESCRIPTION, args.get(0)); + addon.ifPresent(getPlugin().getAddonsManager()::reloadAddon); + }); } else { showHelp(this, user); } return true; } + + @Override + public Optional> tabComplete(User user, String alias, List args) { + return Optional.of(getPlugin().getAddonsManager().getAddons().stream().map(a -> a.getDescription().getName()).collect(Collectors.toList())); + } } diff --git a/src/main/java/world/bentobox/bentobox/managers/AddonsManager.java b/src/main/java/world/bentobox/bentobox/managers/AddonsManager.java index 8a202d707..235ba1c47 100644 --- a/src/main/java/world/bentobox/bentobox/managers/AddonsManager.java +++ b/src/main/java/world/bentobox/bentobox/managers/AddonsManager.java @@ -227,6 +227,7 @@ public class AddonsManager { Path p = addon.getFile().toPath(); disable(addon); loadAddon(p.toFile()); + enableAddon(addon); } /** diff --git a/src/main/resources/locales/en-US.yml b/src/main/resources/locales/en-US.yml index ae219b1c4..b4e543d47 100644 --- a/src/main/resources/locales/en-US.yml +++ b/src/main/resources/locales/en-US.yml @@ -253,10 +253,13 @@ commands: about: description: "display copyright and license info" reload: - description: "reloads settings, addons (if supported) and locales" + parameters: "[addon]" + description: "reloads all addons, settings and locales or reloads an individual addon" locales-reloaded: "&2Languages reloaded." addons-reloaded: "&2Addons reloaded." settings-reloaded: "&2Settings reloaded." + addon: "&6Reloading [description]" + unknown-addon: "&2Unknown addon!" version: plugin-version: "&2BentoBox version: &3[version]" description: "display BentoBox and addons versions" diff --git a/src/test/java/world/bentobox/bentobox/api/addons/AddonTest.java b/src/test/java/world/bentobox/bentobox/api/addons/AddonTest.java index 850445ae9..9992f7513 100644 --- a/src/test/java/world/bentobox/bentobox/api/addons/AddonTest.java +++ b/src/test/java/world/bentobox/bentobox/api/addons/AddonTest.java @@ -26,6 +26,7 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; +import org.mockito.Mockito; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @@ -44,6 +45,8 @@ public class AddonTest { static BentoBox plugin; static JavaPlugin javaPlugin; private Server server; + @Mock + private AddonsManager am; @Before public void setUp() throws Exception { @@ -64,6 +67,10 @@ public class AddonTest { plugin = mock(BentoBox.class); Whitebox.setInternalState(BentoBox.class, "instance", plugin); + // Addons manager + when(plugin.getAddonsManager()).thenReturn(am); + + // Mock item factory (for itemstacks) ItemFactory itemFactory = mock(ItemFactory.class); when(server.getItemFactory()).thenReturn(itemFactory); @@ -150,6 +157,7 @@ public class AddonTest { TestListener listener = new TestListener(); TestClass test = new TestClass(); test.registerListener(listener); + Mockito.verify(am).registerListener(Mockito.any(), Mockito.eq(listener)); } @Test diff --git a/src/test/java/world/bentobox/bentobox/commands/BentoBoxReloadCommandTest.java b/src/test/java/world/bentobox/bentobox/commands/BentoBoxReloadCommandTest.java new file mode 100644 index 000000000..10e1c10c5 --- /dev/null +++ b/src/test/java/world/bentobox/bentobox/commands/BentoBoxReloadCommandTest.java @@ -0,0 +1,187 @@ +/** + * + */ +package world.bentobox.bentobox.commands; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Optional; + +import org.bukkit.Bukkit; +import org.bukkit.scheduler.BukkitScheduler; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.reflect.Whitebox; + +import world.bentobox.bentobox.BentoBox; +import world.bentobox.bentobox.Settings; +import world.bentobox.bentobox.api.addons.Addon; +import world.bentobox.bentobox.api.addons.AddonDescription; +import world.bentobox.bentobox.api.commands.CompositeCommand; +import world.bentobox.bentobox.api.user.User; +import world.bentobox.bentobox.managers.AddonsManager; +import world.bentobox.bentobox.managers.CommandsManager; +import world.bentobox.bentobox.managers.LocalesManager; + +/** + * @author tastybento + * + */ +@RunWith(PowerMockRunner.class) +@PrepareForTest({Bukkit.class, BentoBox.class, User.class }) +public class BentoBoxReloadCommandTest { + + @Mock + private BentoBox plugin; + @Mock + private CompositeCommand ac; + @Mock + private User user; + @Mock + private AddonsManager am; + @Mock + private LocalesManager lm; + private BentoBoxReloadCommand reload; + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + // Set up plugin + Whitebox.setInternalState(BentoBox.class, "instance", plugin); + + // Command manager + CommandsManager cm = mock(CommandsManager.class); + when(plugin.getCommandsManager()).thenReturn(cm); + // Parent command has no aliases + when(ac.getSubCommandAliases()).thenReturn(new HashMap<>()); + when(ac.getPermissionPrefix()).thenReturn("bentobox."); + // Addons manager + when(plugin.getAddonsManager()).thenReturn(am); + // Addons + Addon addon1 = mock(Addon.class); + AddonDescription desc = new AddonDescription.Builder("main", "BSkyBlock", "1.0.0").build(); + when(addon1.getDescription()).thenReturn(desc); + Addon addon2 = mock(Addon.class); + AddonDescription desc2 = new AddonDescription.Builder("main", "AcidIsland", "1.0.0").build(); + when(addon2.getDescription()).thenReturn(desc2); + // Linking + Optional optionalAddon1 = Optional.of(addon1); + Optional optionalAddon2 = Optional.of(addon2); + when(am.getAddonByName(Mockito.eq("bskyblock"))).thenReturn(optionalAddon1); + when(am.getAddonByName(Mockito.eq("acidisland"))).thenReturn(optionalAddon2); + when(am.getAddonByName(Mockito.eq("warps"))).thenReturn(Optional.empty()); + when(am.getAddons()).thenReturn(Arrays.asList(addon1, addon2)); + + // Confirmable command settings + Settings settings = mock(Settings.class); + when(settings.getConfirmationTime()).thenReturn(10); + when(plugin.getSettings()).thenReturn(settings); + + // Scheduler + BukkitScheduler sch = mock(BukkitScheduler.class); + PowerMockito.mockStatic(Bukkit.class); + when(Bukkit.getScheduler()).thenReturn(sch); + + + // Command + reload = new BentoBoxReloadCommand(ac); + } + + /** + * @throws java.lang.Exception + */ + @After + public void tearDown() throws Exception { + } + + /** + * Test method for {@link world.bentobox.bentobox.commands.BentoBoxReloadCommand#BentoBoxReloadCommand(world.bentobox.bentobox.api.commands.CompositeCommand)}. + */ + @Test + public void testBentoBoxReloadCommand() { + assertNotNull(reload); + } + + /** + * Test method for {@link world.bentobox.bentobox.commands.BentoBoxReloadCommand#setup()}. + */ + @Test + public void testSetup() { + assertEquals("bentobox.admin.reload", reload.getPermission()); + assertEquals("commands.bentobox.reload.description", reload.getDescription()); + assertEquals("commands.bentobox.reload.parameters", reload.getParameters()); + } + + /** + * Test method for {@link world.bentobox.bentobox.commands.BentoBoxReloadCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecuteUserStringListOfStringReloadAll() { + reload.execute(user, "", Collections.emptyList()); + Mockito.verify(user).sendMessage("commands.confirmation.confirm", + "[seconds]", + "10"); + } + + /** + * Test method for {@link world.bentobox.bentobox.commands.BentoBoxReloadCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecuteUserStringListOfString() { + reload.execute(user, "", Collections.singletonList("bskyblock")); + Mockito.verify(user).sendMessage("commands.confirmation.confirm", + "[seconds]", + "10"); + } + + /** + * Test method for {@link world.bentobox.bentobox.commands.BentoBoxReloadCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecuteUserStringListOfStringNoAddon() { + reload.execute(user, "", Collections.singletonList("warps")); + Mockito.verify(user).sendMessage("commands.bentobox.reload.unknown-addon"); + } + + /** + * Test method for {@link world.bentobox.bentobox.commands.BentoBoxReloadCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecuteUserStringListOfStringHelp() { + reload.execute(user, "", Arrays.asList("warps", "fhfhfh")); + Mockito.verify(user).sendMessage( + "commands.help.header", + "[label]", + null + ); + } + + /** + * Test method for {@link world.bentobox.bentobox.commands.BentoBoxReloadCommand#tabComplete(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testTabCompleteUserStringListOfString() { + List tabs = reload.tabComplete(user, "", Collections.emptyList()).get(); + assertTrue(tabs.size() == 2); + assertTrue(tabs.contains("BSkyBlock")); + assertTrue(tabs.contains("AcidIsland")); + } + +}