From 6bd393fc66f4529c52b5b7625783249badec4ce5 Mon Sep 17 00:00:00 2001 From: tastybento Date: Mon, 28 Oct 2019 15:35:24 -0700 Subject: [PATCH] Added ChallengesAddon test class. --- .../bentobox/challenges/ChallengesAddon.java | 11 +- .../challenges/ChallengesAddonTest.java | 375 ++++++++++++++++++ .../challenges/ChallengesManagerTest.java | 1 - .../challenges/tasks/TryToCompleteTest.java | 2 +- 4 files changed, 384 insertions(+), 5 deletions(-) create mode 100644 src/test/java/world/bentobox/challenges/ChallengesAddonTest.java diff --git a/src/main/java/world/bentobox/challenges/ChallengesAddon.java b/src/main/java/world/bentobox/challenges/ChallengesAddon.java index 7e4b3e9..452f265 100644 --- a/src/main/java/world/bentobox/challenges/ChallengesAddon.java +++ b/src/main/java/world/bentobox/challenges/ChallengesAddon.java @@ -5,6 +5,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Optional; +import org.bukkit.Bukkit; import org.bukkit.Material; import world.bentobox.bentobox.api.addons.Addon; @@ -155,7 +156,11 @@ public class ChallengesAddon extends Addon { List hookedGameModes = new ArrayList<>(); this.getPlugin().getAddonsManager().getGameModeAddons().forEach(gameModeAddon -> { - if (!this.settings.getDisabledGameModes().contains(gameModeAddon.getDescription().getName())) + if (!this.settings + .getDisabledGameModes() + .contains(gameModeAddon + .getDescription() + .getName())) { if (gameModeAddon.getPlayerCommand().isPresent()) { @@ -238,7 +243,7 @@ public class ChallengesAddon extends Addon { if (this.settings.getAutoSaveTimer() > 0) { - this.getPlugin().getServer().getScheduler().runTaskTimerAsynchronously( + Bukkit.getScheduler().runTaskTimerAsynchronously( this.getPlugin(), bukkitTask -> ChallengesAddon.this.challengesManager.save(), this.settings.getAutoSaveTimer() * 60 * 20, @@ -264,7 +269,7 @@ public class ChallengesAddon extends Addon { { this.loadSettings(); this.challengesManager.reload(); - this.getLogger().info("Challenges addon reloaded."); + this.log("Challenges addon reloaded."); } } diff --git a/src/test/java/world/bentobox/challenges/ChallengesAddonTest.java b/src/test/java/world/bentobox/challenges/ChallengesAddonTest.java new file mode 100644 index 0000000..0c2b611 --- /dev/null +++ b/src/test/java/world/bentobox/challenges/ChallengesAddonTest.java @@ -0,0 +1,375 @@ +package world.bentobox.challenges; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Optional; +import java.util.UUID; +import java.util.jar.JarEntry; +import java.util.jar.JarOutputStream; +import java.util.logging.Logger; + +import org.bukkit.Bukkit; +import org.bukkit.Server; +import org.bukkit.entity.Player; +import org.bukkit.plugin.PluginManager; +import org.bukkit.scheduler.BukkitScheduler; +import org.eclipse.jdt.annotation.NonNull; +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.mockito.stubbing.Answer; +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.Addon.State; +import world.bentobox.bentobox.api.addons.AddonDescription; +import world.bentobox.bentobox.api.addons.GameModeAddon; +import world.bentobox.bentobox.api.commands.CompositeCommand; +import world.bentobox.bentobox.api.configuration.Config; +import world.bentobox.bentobox.api.user.User; +import world.bentobox.bentobox.database.DatabaseSetup.DatabaseType; +import world.bentobox.bentobox.database.objects.Island; +import world.bentobox.bentobox.managers.AddonsManager; +import world.bentobox.bentobox.managers.CommandsManager; +import world.bentobox.bentobox.managers.FlagsManager; +import world.bentobox.bentobox.managers.IslandWorldManager; +import world.bentobox.bentobox.managers.IslandsManager; + +/** + * @author tastybento + * + */ +@RunWith(PowerMockRunner.class) +@PrepareForTest({Bukkit.class, BentoBox.class, User.class, Config.class }) +public class ChallengesAddonTest { + + @Mock + private User user; + @Mock + private IslandsManager im; + @Mock + private Island island; + + private ChallengesAddon addon; + @Mock + private BentoBox plugin; + @Mock + private FlagsManager fm; + @Mock + private Settings settings; + @Mock + private GameModeAddon gameMode; + @Mock + private AddonsManager am; + @Mock + private BukkitScheduler scheduler; + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + // Set up plugin + Whitebox.setInternalState(BentoBox.class, "instance", plugin); + when(plugin.getLogger()).thenReturn(Logger.getAnonymousLogger()); + //when(plugin.isEnabled()).thenReturn(true); + // Command manager + CommandsManager cm = mock(CommandsManager.class); + when(plugin.getCommandsManager()).thenReturn(cm); + + // Player + Player p = mock(Player.class); + // Sometimes use Mockito.withSettings().verboseLogging() + when(user.isOp()).thenReturn(false); + UUID uuid = UUID.randomUUID(); + when(user.getUniqueId()).thenReturn(uuid); + when(user.getPlayer()).thenReturn(p); + when(user.getName()).thenReturn("tastybento"); + User.setPlugin(plugin); + + // Island World Manager + IslandWorldManager iwm = mock(IslandWorldManager.class); + when(plugin.getIWM()).thenReturn(iwm); + + + // Player has island to begin with + island = mock(Island.class); + when(im.getIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(island); + when(plugin.getIslands()).thenReturn(im); + + // Locales + // Return the reference (USE THIS IN THE FUTURE) + when(user.getTranslation(Mockito.anyString())).thenAnswer((Answer) invocation -> invocation.getArgument(0, String.class)); + + // Server + PowerMockito.mockStatic(Bukkit.class); + Server server = mock(Server.class); + when(Bukkit.getServer()).thenReturn(server); + when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger()); + when(Bukkit.getPluginManager()).thenReturn(mock(PluginManager.class)); + + // Addon + addon = new ChallengesAddon(); + File jFile = new File("addon.jar"); + List lines = Arrays.asList("# ChallengesAddon Configuration", "uniqueId: config"); + Path path = Paths.get("config.yml"); + Files.write(path, lines, Charset.forName("UTF-8")); + try (JarOutputStream tempJarOutputStream = new JarOutputStream(new FileOutputStream(jFile))) { + //Added the new files to the jar. + try (FileInputStream fis = new FileInputStream(path.toFile())) { + + byte[] buffer = new byte[1024]; + int bytesRead = 0; + JarEntry entry = new JarEntry(path.toString()); + tempJarOutputStream.putNextEntry(entry); + while((bytesRead = fis.read(buffer)) != -1) { + tempJarOutputStream.write(buffer, 0, bytesRead); + } + } + } + File dataFolder = new File("addons/Challenges"); + addon.setDataFolder(dataFolder); + addon.setFile(jFile); + AddonDescription desc = new AddonDescription.Builder("bentobox", "challenges", "1.3").description("test").authors("BONNe").build(); + addon.setDescription(desc); + // Addons manager + when(plugin.getAddonsManager()).thenReturn(am); + // One game mode + when(am.getGameModeAddons()).thenReturn(Collections.singletonList(gameMode)); + AddonDescription desc2 = new AddonDescription.Builder("bentobox", "BSkyBlock", "1.3").description("test").authors("tasty").build(); + when(gameMode.getDescription()).thenReturn(desc2); + + // Player command + CompositeCommand cmd = mock(CompositeCommand.class); + @NonNull + Optional opCmd = Optional.of(cmd); + when(gameMode.getPlayerCommand()).thenReturn(opCmd); + // Admin command + when(gameMode.getAdminCommand()).thenReturn(opCmd); + + // Flags manager + when(plugin.getFlagsManager()).thenReturn(fm); + when(fm.getFlags()).thenReturn(Collections.emptyList()); + + // The database type has to be created one line before the thenReturn() to work! + when(plugin.getSettings()).thenReturn(settings); + DatabaseType value = DatabaseType.JSON; + when(settings.getDatabaseType()).thenReturn(value); + + // Bukkit + PowerMockito.mockStatic(Bukkit.class); + when(Bukkit.getScheduler()).thenReturn(scheduler); + + + } + + /** + * @throws java.lang.Exception + */ + @After + public void tearDown() throws Exception { + new File("addon.jar").delete(); + new File("config.yml").delete(); + new File("addons/Challenges","config.yml").delete(); + new File("addons/Challenges").delete(); + new File("addons").delete(); + } + + /** + * Test method for {@link world.bentobox.challenges.ChallengesAddon#onLoad()}. + */ + @Test + public void testOnLoad() { + addon.onLoad(); + // Check that config.yml file has been saved + File check = new File("addons/Challenges","config.yml"); + assertTrue(check.exists()); + } + + /** + * Test method for {@link world.bentobox.challenges.ChallengesAddon#onEnable()}. + */ + @Test + public void testOnEnableDisabledPlugin() { + when(plugin.isEnabled()).thenReturn(false); + addon.onEnable(); + verify(plugin).logError("[challenges] BentoBox is not available or disabled!"); + assertEquals(Addon.State.DISABLED, addon.getState()); + } + + /** + * Test method for {@link world.bentobox.challenges.ChallengesAddon#onEnable()}. + */ + @Test + public void testOnEnableDisabledAddon() { + when(plugin.isEnabled()).thenReturn(true); + addon.setState(State.DISABLED); + addon.onEnable(); + verify(plugin).logError("[challenges] Challenges Addon is not available or disabled!"); + } + + /** + * Test method for {@link world.bentobox.challenges.ChallengesAddon#onEnable()}. + */ + @Test + public void testOnEnableIncompatibleDatabase() { + // The database type has to be created one line before the thenReturn() to work! + DatabaseType value = DatabaseType.YAML; + when(settings.getDatabaseType()).thenReturn(value); + when(plugin.isEnabled()).thenReturn(true); + addon.setState(State.LOADED); + addon.onEnable(); + verify(plugin).logError("[challenges] BentoBox database is not compatible with Challenges Addon."); + verify(plugin).logError("[challenges] Please use JSON based database type."); + assertEquals(State.INCOMPATIBLE, addon.getState()); + } + + /** + * Test method for {@link world.bentobox.challenges.ChallengesAddon#onEnable()}. + */ + @Test + public void testOnEnableHooked() { + addon.onLoad(); + when(plugin.isEnabled()).thenReturn(true); + addon.setState(State.LOADED); + addon.onEnable(); + verify(plugin).logWarning("[challenges] Level add-on not found so level challenges will not work!"); + verify(plugin).logWarning("[challenges] Economy plugin not found so money options will not work!"); + verify(plugin).log("[challenges] Loading challenges..."); + verify(plugin, never()).logError("Challenges could not hook into AcidIsland or BSkyBlock so will not do anything!"); + + } + + /** + * Test method for {@link world.bentobox.challenges.ChallengesAddon#onEnable()}. + */ + @Test + public void testOnEnableNotHooked() { + addon.onLoad(); + when(am.getGameModeAddons()).thenReturn(Collections.emptyList()); + when(plugin.isEnabled()).thenReturn(true); + addon.setState(State.LOADED); + addon.onEnable(); + verify(plugin).log("[challenges] Loading challenges..."); + verify(plugin).logError("[challenges] Challenges could not hook into AcidIsland or BSkyBlock so will not do anything!"); + + } + + /** + * Test method for {@link world.bentobox.challenges.ChallengesAddon#onReload()}. + */ + @Test + public void testOnReloadNotHooked() { + addon.onReload(); + verify(plugin, never()).log(anyString()); + } + + /** + * Test method for {@link world.bentobox.challenges.ChallengesAddon#getChallengesManager()}. + */ + @Test + public void testGetChallengesManager() { + assertNull(addon.getChallengesManager()); + this.testOnEnableHooked(); + assertNotNull(addon.getChallengesManager()); + } + + /** + * Test method for {@link world.bentobox.challenges.ChallengesAddon#getPermissionPrefix()}. + */ + @Test + public void testGetPermissionPrefix() { + assertEquals("addon.", addon.getPermissionPrefix()); + } + + /** + * Test method for {@link world.bentobox.challenges.ChallengesAddon#getImportManager()}. + */ + @Test + public void testGetImportManager() { + assertNull(addon.getImportManager()); + this.testOnEnableHooked(); + assertNotNull(addon.getImportManager()); + } + + /** + * Test method for {@link world.bentobox.challenges.ChallengesAddon#getWebManager()}. + */ + @Test + public void testGetWebManager() { + assertNull(addon.getWebManager()); + this.testOnEnableHooked(); + assertNotNull(addon.getWebManager()); + } + + /** + * Test method for {@link world.bentobox.challenges.ChallengesAddon#getChallengesSettings()}. + */ + @Test + public void testGetChallengesSettings() { + assertNull(addon.getChallengesSettings()); + addon.onLoad(); + assertNotNull(addon.getChallengesSettings()); + + } + + /** + * Test method for {@link world.bentobox.challenges.ChallengesAddon#isEconomyProvided()}. + */ + @Test + public void testIsEconomyProvided() { + assertFalse(addon.isEconomyProvided()); + } + + /** + * Test method for {@link world.bentobox.challenges.ChallengesAddon#getEconomyProvider()}. + */ + @Test + public void testGetEconomyProvider() { + assertNull(addon.getEconomyProvider()); + } + + /** + * Test method for {@link world.bentobox.challenges.ChallengesAddon#isLevelProvided()}. + */ + @Test + public void testIsLevelProvided() { + assertFalse(addon.isLevelProvided()); + } + + /** + * Test method for {@link world.bentobox.challenges.ChallengesAddon#getLevelAddon()}. + */ + @Test + public void testGetLevelAddon() { + assertNull(addon.getLevelAddon()); + } + +} diff --git a/src/test/java/world/bentobox/challenges/ChallengesManagerTest.java b/src/test/java/world/bentobox/challenges/ChallengesManagerTest.java index c537f99..63503f7 100644 --- a/src/test/java/world/bentobox/challenges/ChallengesManagerTest.java +++ b/src/test/java/world/bentobox/challenges/ChallengesManagerTest.java @@ -23,7 +23,6 @@ import java.io.FileWriter; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; diff --git a/src/test/java/world/bentobox/challenges/tasks/TryToCompleteTest.java b/src/test/java/world/bentobox/challenges/tasks/TryToCompleteTest.java index b5bd660..aabe6fc 100644 --- a/src/test/java/world/bentobox/challenges/tasks/TryToCompleteTest.java +++ b/src/test/java/world/bentobox/challenges/tasks/TryToCompleteTest.java @@ -3,7 +3,7 @@ package world.bentobox.challenges.tasks; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertTrue; -import static org.mockito.Matchers.any; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when;