diff --git a/pom.xml b/pom.xml index d031d34..e52dca4 100644 --- a/pom.xml +++ b/pom.xml @@ -59,7 +59,7 @@ 2.0.9 1.20.1-R0.1-SNAPSHOT - 1.24.0-SNAPSHOT + 2.0.0-SNAPSHOT 2.6.2 1.3.0 diff --git a/src/test/java/world/bentobox/aoneblock/AOneBlockTest.java b/src/test/java/world/bentobox/aoneblock/AOneBlockTest.java index 3f77cab..97f43fa 100644 --- a/src/test/java/world/bentobox/aoneblock/AOneBlockTest.java +++ b/src/test/java/world/bentobox/aoneblock/AOneBlockTest.java @@ -12,17 +12,20 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import java.beans.IntrospectionException; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; +import java.lang.reflect.InvocationTargetException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Collections; import java.util.Comparator; import java.util.UUID; +import java.util.concurrent.CompletableFuture; import java.util.jar.JarEntry; import java.util.jar.JarOutputStream; import java.util.logging.Logger; @@ -34,6 +37,7 @@ import org.eclipse.jdt.annotation.NonNull; import org.junit.After; import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; @@ -51,6 +55,8 @@ import world.bentobox.bentobox.api.addons.AddonDescription; import world.bentobox.bentobox.api.configuration.Config; import world.bentobox.bentobox.api.user.User; +import world.bentobox.bentobox.database.AbstractDatabaseHandler; +import world.bentobox.bentobox.database.DatabaseSetup; import world.bentobox.bentobox.database.DatabaseSetup.DatabaseType; import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.managers.AddonsManager; @@ -59,274 +65,294 @@ import world.bentobox.bentobox.managers.IslandWorldManager; import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.PlaceholdersManager; +import world.bentobox.bentobox.managers.RanksManager; /** * @author tastybento * */ @RunWith(PowerMockRunner.class) -@PrepareForTest({Bukkit.class, BentoBox.class, User.class, Config.class }) +@PrepareForTest({ Bukkit.class, BentoBox.class, User.class, Config.class, DatabaseSetup.class }) public class AOneBlockTest { - @Mock - private User user; - @Mock - private IslandsManager im; - @Mock - private Island island; - - private AOneBlock addon; - @Mock - private BentoBox plugin; - @Mock - private FlagsManager fm; - @Mock - private Settings settings; - @Mock - private PlaceholdersManager phm; - - /** - * @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()); - - // The database type has to be created one line before the thenReturn() to work! - DatabaseType value = DatabaseType.JSON; - when(plugin.getSettings()).thenReturn(settings); - when(settings.getDatabaseType()).thenReturn(value); - when(plugin.getPlaceholdersManager()).thenReturn(phm); - // Placeholders - when(phm.replacePlaceholders(any(), anyString())).thenAnswer(a -> (String)a.getArgument(1, String.class)); - - - // 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)); - - // Create an Addon - addon = new AOneBlock(); - File jFile = new File("addon.jar"); - try (JarOutputStream tempJarOutputStream = new JarOutputStream(new FileOutputStream(jFile))) { - - // Copy over config file from src folder - Path fromPath = Paths.get("src/main/resources/config.yml"); - Path path = Paths.get("config.yml"); - Files.copy(fromPath, path); - - //Add the new files to the jar. - add(path, tempJarOutputStream); - - // Copy over panels file from src folder - fromPath = Paths.get("src/main/resources/panels/phases_panel.yml"); - path = Paths.get("panels"); - Files.createDirectory(path); - path = Paths.get("panels/phases_panel.yml"); - Files.copy(fromPath, path); - - //Add the new files to the jar. - add(path, tempJarOutputStream); - } - - File dataFolder = new File("addons/AOneBlock"); - addon.setDataFolder(dataFolder); - addon.setFile(jFile); - AddonDescription desc = new AddonDescription.Builder("bentobox", "aoneblock", "1.3").description("test").authors("tasty").build(); - addon.setDescription(desc); - // Addons manager - AddonsManager am = mock(AddonsManager.class); - when(plugin.getAddonsManager()).thenReturn(am); - - // Flags manager - when(plugin.getFlagsManager()).thenReturn(fm); - when(fm.getFlags()).thenReturn(Collections.emptyList()); - - } - - private void add(Path path, JarOutputStream tempJarOutputStream) throws FileNotFoundException, IOException { - 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); - } - } - - } - - /** - * @throws java.lang.Exception - */ - @After - public void tearDown() throws Exception { - //new File("addon.jar").delete(); - new File("config.yml").delete(); - deleteAll(new File("addons")); - deleteAll(new File("panels")); - } - - private void deleteAll(File file) throws IOException { - if (file.exists()) { - Files.walk(file.toPath()) - .sorted(Comparator.reverseOrder()) - .map(Path::toFile) - .forEach(File::delete); - } - - } - - - /** - * Test method for {@link world.bentobox.aoneblock.AOneBlock#onEnable()}. - */ - @Test - public void testOnEnable() { - testOnLoad(); - addon.setState(State.ENABLED); - addon.onEnable(); - verify(plugin, never()).logError(anyString()); - assertNotEquals(State.DISABLED, addon.getState()); - assertNotNull(addon.getBlockListener()); - assertNotNull(addon.getOneBlockManager()); - - - - } - - /** - * Test method for {@link world.bentobox.aoneblock.AOneBlock#onLoad()}. - */ - @Test - public void testOnLoad() { - addon.onLoad(); - // Check that config.yml file has been saved - File check = new File("addons/AOneBlock","config.yml"); - assertTrue(check.exists()); - assertTrue(addon.getPlayerCommand().isPresent()); - assertTrue(addon.getAdminCommand().isPresent()); - - } - - /** - * Test method for {@link world.bentobox.aoneblock.AOneBlock#onReload()}. - */ - @Test - public void testOnReload() { - addon.onEnable(); - addon.onReload(); - // Check that config.yml file has been saved - File check = new File("addons/AOneBlock","config.yml"); - assertTrue(check.exists()); - } - - /** - * Test method for {@link world.bentobox.aoneblock.AOneBlock#createWorlds()}. - */ - @Test - public void testCreateWorlds() { - addon.onLoad(); - addon.createWorlds(); - verify(plugin).log("[aoneblock] Creating AOneBlock world ..."); - verify(plugin).log("[aoneblock] Creating AOneBlock's Nether..."); - verify(plugin).log("[aoneblock] Creating AOneBlock's End World..."); - - } - - /** - * Test method for {@link world.bentobox.aoneblock.AOneBlock#getSettings()}. - */ - @Test - public void testGetSettings() { - addon.onLoad(); - assertNotNull(addon.getSettings()); - - } - - /** - * Test method for {@link world.bentobox.aoneblock.AOneBlock#getWorldSettings()}. - */ - @Test - public void testGetWorldSettings() { - addon.onLoad(); - assertEquals(addon.getSettings(), addon.getWorldSettings()); - } - - /** - * Test method for {@link world.bentobox.aoneblock.AOneBlock#getOneBlocksIsland(world.bentobox.bentobox.database.objects.Island)}. - */ - @Test - public void testGetOneBlocksIsland() { - addon.onEnable(); - @NonNull - OneBlockIslands i = addon.getOneBlocksIsland(island); - assertEquals(island.getUniqueId(), i.getUniqueId()); - } - - /** - * Test method for {@link world.bentobox.aoneblock.AOneBlock#getOneBlockManager()}. - */ - @Test - public void testGetOneBlockManager() { - assertNull(addon.getOneBlockManager()); - } - - /** - * Test method for {@link world.bentobox.aoneblock.AOneBlock#getBlockListener()}. - */ - @Test - public void testGetBlockListener() { - assertNull(addon.getBlockListener()); - } - - /** - * Test method for {@link world.bentobox.aoneblock.AOneBlock#getPlaceholdersManager()}. - */ - @Test - public void testGetPlaceholdersManager() { - assertNull(addon.getPlaceholdersManager()); - } - - /** - * Test method for {@link world.bentobox.aoneblock.AOneBlock#getHoloListener()}. - */ - @Test - public void testGetHoloListener() { - assertNull(addon.getHoloListener()); - } + @Mock + private User user; + @Mock + private IslandsManager im; + @Mock + private Island island; + + private AOneBlock addon; + @Mock + private BentoBox plugin; + @Mock + private FlagsManager fm; + @Mock + private Settings settings; + @Mock + private PlaceholdersManager phm; + + private static AbstractDatabaseHandler h; + + @SuppressWarnings("unchecked") + @BeforeClass + public static void beforeClass() throws IllegalAccessException, InvocationTargetException, IntrospectionException { + // This has to be done beforeClass otherwise the tests will interfere with each + // other + h = mock(AbstractDatabaseHandler.class); + // Database + PowerMockito.mockStatic(DatabaseSetup.class); + DatabaseSetup dbSetup = mock(DatabaseSetup.class); + when(DatabaseSetup.getDatabase()).thenReturn(dbSetup); + when(dbSetup.getHandler(any())).thenReturn(h); + when(h.saveObject(any())).thenReturn(CompletableFuture.completedFuture(true)); + } + + @After + public void tearDown() throws IOException { + User.clearUsers(); + Mockito.framework().clearInlineMocks(); + deleteAll(new File("database")); + deleteAll(new File("database_backup")); + new File("config.yml").delete(); + deleteAll(new File("addons")); + deleteAll(new File("panels")); + } + + private void deleteAll(File file) throws IOException { + if (file.exists()) { + Files.walk(file.toPath()).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete); + } + + } + + /** + * @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()); + + // The database type has to be created one line before the thenReturn() to work! + DatabaseType value = DatabaseType.JSON; + when(plugin.getSettings()).thenReturn(settings); + when(settings.getDatabaseType()).thenReturn(value); + when(plugin.getPlaceholdersManager()).thenReturn(phm); + // Placeholders + when(phm.replacePlaceholders(any(), anyString())).thenAnswer(a -> (String) a.getArgument(1, String.class)); + + // 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)); + + // Create an Addon + addon = new AOneBlock(); + File jFile = new File("addon.jar"); + try (JarOutputStream tempJarOutputStream = new JarOutputStream(new FileOutputStream(jFile))) { + + // Copy over config file from src folder + Path fromPath = Paths.get("src/main/resources/config.yml"); + Path path = Paths.get("config.yml"); + Files.copy(fromPath, path); + + // Add the new files to the jar. + add(path, tempJarOutputStream); + + // Copy over panels file from src folder + fromPath = Paths.get("src/main/resources/panels/phases_panel.yml"); + path = Paths.get("panels"); + Files.createDirectory(path); + path = Paths.get("panels/phases_panel.yml"); + Files.copy(fromPath, path); + + // Add the new files to the jar. + add(path, tempJarOutputStream); + } + + File dataFolder = new File("addons/AOneBlock"); + addon.setDataFolder(dataFolder); + addon.setFile(jFile); + AddonDescription desc = new AddonDescription.Builder("bentobox", "aoneblock", "1.3").description("test") + .authors("tasty").build(); + addon.setDescription(desc); + // Addons manager + AddonsManager am = mock(AddonsManager.class); + when(plugin.getAddonsManager()).thenReturn(am); + + // Flags manager + when(plugin.getFlagsManager()).thenReturn(fm); + when(fm.getFlags()).thenReturn(Collections.emptyList()); + + // RanksManager + RanksManager rm = new RanksManager(); + when(plugin.getRanksManager()).thenReturn(rm); + + } + + private void add(Path path, JarOutputStream tempJarOutputStream) throws FileNotFoundException, IOException { + 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); + } + } + + } + + /** + * Test method for {@link world.bentobox.aoneblock.AOneBlock#onEnable()}. + */ + @Test + public void testOnEnable() { + testOnLoad(); + addon.setState(State.ENABLED); + addon.onEnable(); + verify(plugin, never()).logError(anyString()); + assertNotEquals(State.DISABLED, addon.getState()); + assertNotNull(addon.getBlockListener()); + assertNotNull(addon.getOneBlockManager()); + + } + + /** + * Test method for {@link world.bentobox.aoneblock.AOneBlock#onLoad()}. + */ + @Test + public void testOnLoad() { + addon.onLoad(); + // Check that config.yml file has been saved + File check = new File("addons/AOneBlock", "config.yml"); + assertTrue(check.exists()); + assertTrue(addon.getPlayerCommand().isPresent()); + assertTrue(addon.getAdminCommand().isPresent()); + + } + + /** + * Test method for {@link world.bentobox.aoneblock.AOneBlock#onReload()}. + */ + @Test + public void testOnReload() { + addon.onEnable(); + addon.onReload(); + // Check that config.yml file has been saved + File check = new File("addons/AOneBlock", "config.yml"); + assertTrue(check.exists()); + } + + /** + * Test method for {@link world.bentobox.aoneblock.AOneBlock#createWorlds()}. + */ + @Test + public void testCreateWorlds() { + addon.onLoad(); + addon.createWorlds(); + verify(plugin).log("[aoneblock] Creating AOneBlock world ..."); + verify(plugin).log("[aoneblock] Creating AOneBlock's Nether..."); + verify(plugin).log("[aoneblock] Creating AOneBlock's End World..."); + + } + + /** + * Test method for {@link world.bentobox.aoneblock.AOneBlock#getSettings()}. + */ + @Test + public void testGetSettings() { + addon.onLoad(); + assertNotNull(addon.getSettings()); + + } + + /** + * Test method for + * {@link world.bentobox.aoneblock.AOneBlock#getWorldSettings()}. + */ + @Test + public void testGetWorldSettings() { + addon.onLoad(); + assertEquals(addon.getSettings(), addon.getWorldSettings()); + } + + /** + * Test method for + * {@link world.bentobox.aoneblock.AOneBlock#getOneBlocksIsland(world.bentobox.bentobox.database.objects.Island)}. + */ + @Test + public void testGetOneBlocksIsland() { + addon.onEnable(); + @NonNull + OneBlockIslands i = addon.getOneBlocksIsland(island); + assertEquals(island.getUniqueId(), i.getUniqueId()); + } + + /** + * Test method for + * {@link world.bentobox.aoneblock.AOneBlock#getOneBlockManager()}. + */ + @Test + public void testGetOneBlockManager() { + assertNull(addon.getOneBlockManager()); + } + + /** + * Test method for + * {@link world.bentobox.aoneblock.AOneBlock#getBlockListener()}. + */ + @Test + public void testGetBlockListener() { + assertNull(addon.getBlockListener()); + } + + /** + * Test method for + * {@link world.bentobox.aoneblock.AOneBlock#getPlaceholdersManager()}. + */ + @Test + public void testGetPlaceholdersManager() { + assertNull(addon.getPlaceholdersManager()); + } + + /** + * Test method for {@link world.bentobox.aoneblock.AOneBlock#getHoloListener()}. + */ + @Test + public void testGetHoloListener() { + assertNull(addon.getHoloListener()); + } } diff --git a/src/test/java/world/bentobox/aoneblock/commands/island/IslandSetCountCommandTest.java b/src/test/java/world/bentobox/aoneblock/commands/island/IslandSetCountCommandTest.java index 4dd9bb7..eabfb3a 100644 --- a/src/test/java/world/bentobox/aoneblock/commands/island/IslandSetCountCommandTest.java +++ b/src/test/java/world/bentobox/aoneblock/commands/island/IslandSetCountCommandTest.java @@ -11,10 +11,18 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import java.beans.IntrospectionException; +import java.io.File; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.Collections; +import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.UUID; +import java.util.concurrent.CompletableFuture; import org.bukkit.Bukkit; import org.bukkit.World; @@ -22,10 +30,14 @@ import org.bukkit.entity.Player; import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.Nullable; +import org.junit.After; import org.junit.Before; +import org.junit.BeforeClass; 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; @@ -39,6 +51,8 @@ import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.localization.TextVariables; import world.bentobox.bentobox.api.user.User; +import world.bentobox.bentobox.database.AbstractDatabaseHandler; +import world.bentobox.bentobox.database.DatabaseSetup; import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.managers.CommandsManager; import world.bentobox.bentobox.managers.IslandWorldManager; @@ -51,126 +65,156 @@ * */ @RunWith(PowerMockRunner.class) -@PrepareForTest({Bukkit.class, BentoBox.class, User.class}) +@PrepareForTest({ Bukkit.class, BentoBox.class, User.class, DatabaseSetup.class }) public class IslandSetCountCommandTest { - @Mock - private BentoBox plugin; - @Mock - private CompositeCommand ac; - @Mock - private User user; - @Mock - private LocalesManager lm; - @Mock - private AOneBlock addon; - private UUID uuid; - @Mock - private World world; - @Mock - private IslandsManager im; - @Mock - private @Nullable Island island; - @Mock - private IslandWorldManager iwm; - private IslandSetCountCommand iscc; - @Mock - private BlockListener bl; - private @NonNull OneBlockIslands oneBlockIsland = new OneBlockIslands(UUID.randomUUID().toString()); - - - /** - * @throws java.lang.Exception - */ - @Before - public void setUp() throws Exception { - // Set up plugin - BentoBox plugin = mock(BentoBox.class); - Whitebox.setInternalState(BentoBox.class, "instance", plugin); - - // 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); - when(user.getPermissionValue(anyString(), anyInt())).thenReturn(4); - when(user.getWorld()).thenReturn(world); - uuid = UUID.randomUUID(); - when(user.getUniqueId()).thenReturn(uuid); - when(user.getPlayer()).thenReturn(p); - when(user.getName()).thenReturn("tastybento"); - when(user.getTranslation(any())).thenAnswer(invocation -> invocation.getArgument(0, String.class)); - User.setPlugin(plugin); - - // Parent command has no aliases - when(ac.getSubCommandAliases()).thenReturn(new HashMap<>()); - when(ac.getWorld()).thenReturn(world); - when(ac.getAddon()).thenReturn(addon); - - // Islands - when(plugin.getIslands()).thenReturn(im); - when(im.getIsland(world, user)).thenReturn(island); - when(im.hasIsland(world, user)).thenReturn(true); - when(im.inTeam(world, uuid)).thenReturn(true); - when(island.getRankCommand(anyString())).thenReturn(RanksManager.OWNER_RANK); - when(island.getRank(user)).thenReturn(RanksManager.MEMBER_RANK); - - - // IWM - when(plugin.getIWM()).thenReturn(iwm); - when(iwm.getPermissionPrefix(any())).thenReturn("bskyblock."); - - // Settings - Settings settings = new Settings(); - when(addon.getSettings()).thenReturn(settings); - - // RanksManager - RanksManager rm = new RanksManager(); - when(plugin.getRanksManager()).thenReturn(rm); - - // BlockListener - when(addon.getBlockListener()).thenReturn(bl); - when(bl.getIsland(island)).thenReturn(oneBlockIsland); - - - // DUT - iscc = new IslandSetCountCommand(this.ac, - settings.getSetCountCommand().split(" ")[0], - settings.getSetCountCommand().split(" ")); - } + @Mock + private BentoBox plugin; + @Mock + private CompositeCommand ac; + @Mock + private User user; + @Mock + private LocalesManager lm; + @Mock + private AOneBlock addon; + private UUID uuid; + @Mock + private World world; + @Mock + private IslandsManager im; + @Mock + private @Nullable Island island; + @Mock + private IslandWorldManager iwm; + private IslandSetCountCommand iscc; + @Mock + private BlockListener bl; + private @NonNull OneBlockIslands oneBlockIsland = new OneBlockIslands(UUID.randomUUID().toString()); - /** - * Test method for {@link world.bentobox.aoneblock.commands.island.IslandSetCountCommand#IslandSetCountCommand(world.bentobox.bentobox.api.commands.CompositeCommand, java.lang.String, java.lang.String[])}. - */ - @Test - public void testIslandSetCountCommand() { - assertNotNull(iscc); - } + private static AbstractDatabaseHandler h; - /** - * Test method for {@link world.bentobox.aoneblock.commands.island.IslandSetCountCommand#setup()}. - */ - @Test - public void testSetup() { - assertEquals("island.setcount", iscc.getPermission()); - assertEquals("aoneblock.commands.island.setcount.parameters", iscc.getParameters()); - assertEquals("aoneblock.commands.island.setcount.description", iscc.getDescription()); - assertTrue(iscc.isConfigurableRankCommand()); - assertTrue(iscc.isOnlyPlayer()); + @SuppressWarnings("unchecked") + @BeforeClass + public static void beforeClass() throws IllegalAccessException, InvocationTargetException, IntrospectionException { + // This has to be done beforeClass otherwise the tests will interfere with each + // other + h = mock(AbstractDatabaseHandler.class); + // Database + PowerMockito.mockStatic(DatabaseSetup.class); + DatabaseSetup dbSetup = mock(DatabaseSetup.class); + when(DatabaseSetup.getDatabase()).thenReturn(dbSetup); + when(dbSetup.getHandler(any())).thenReturn(h); + when(h.saveObject(any())).thenReturn(CompletableFuture.completedFuture(true)); + } - } - - /** - * Test method for {@link world.bentobox.aoneblock.commands.island.IslandSetCountCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. - */ - public void testExecuteUserStringListOfStringShowHelp() { - assertFalse(iscc.execute(user, "", Collections.emptyList())); - verify(user).sendMessage("commands.help.header","[label]",null); - } - - /** + @After + public void tearDown() throws IOException { + User.clearUsers(); + Mockito.framework().clearInlineMocks(); + deleteAll(new File("database")); + deleteAll(new File("database_backup")); + } + + private void deleteAll(File file) throws IOException { + if (file.exists()) { + Files.walk(file.toPath()).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete); + } + + } + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + // Set up plugin + BentoBox plugin = mock(BentoBox.class); + Whitebox.setInternalState(BentoBox.class, "instance", plugin); + + // 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); + when(user.getPermissionValue(anyString(), anyInt())).thenReturn(4); + when(user.getWorld()).thenReturn(world); + uuid = UUID.randomUUID(); + when(user.getUniqueId()).thenReturn(uuid); + when(user.getPlayer()).thenReturn(p); + when(user.getName()).thenReturn("tastybento"); + when(user.getTranslation(any())).thenAnswer(invocation -> invocation.getArgument(0, String.class)); + User.setPlugin(plugin); + + // Parent command has no aliases + when(ac.getSubCommandAliases()).thenReturn(new HashMap<>()); + when(ac.getWorld()).thenReturn(world); + when(ac.getAddon()).thenReturn(addon); + + // Islands + when(plugin.getIslands()).thenReturn(im); + when(im.getIsland(world, user)).thenReturn(island); + when(im.hasIsland(world, user)).thenReturn(true); + when(im.inTeam(world, uuid)).thenReturn(true); + when(island.getRankCommand(anyString())).thenReturn(RanksManager.OWNER_RANK); + when(island.getRank(user)).thenReturn(RanksManager.MEMBER_RANK); + + // IWM + when(plugin.getIWM()).thenReturn(iwm); + when(iwm.getPermissionPrefix(any())).thenReturn("bskyblock."); + + // Settings + Settings settings = new Settings(); + when(addon.getSettings()).thenReturn(settings); + + // RanksManager + RanksManager rm = new RanksManager(); + when(plugin.getRanksManager()).thenReturn(rm); + + // BlockListener + when(addon.getBlockListener()).thenReturn(bl); + when(bl.getIsland(island)).thenReturn(oneBlockIsland); + + // DUT + iscc = new IslandSetCountCommand(this.ac, settings.getSetCountCommand().split(" ")[0], + settings.getSetCountCommand().split(" ")); + } + + /** + * Test method for + * {@link world.bentobox.aoneblock.commands.island.IslandSetCountCommand#IslandSetCountCommand(world.bentobox.bentobox.api.commands.CompositeCommand, java.lang.String, java.lang.String[])}. + */ + @Test + public void testIslandSetCountCommand() { + assertNotNull(iscc); + } + + /** + * Test method for + * {@link world.bentobox.aoneblock.commands.island.IslandSetCountCommand#setup()}. + */ + @Test + public void testSetup() { + assertEquals("island.setcount", iscc.getPermission()); + assertEquals("aoneblock.commands.island.setcount.parameters", iscc.getParameters()); + assertEquals("aoneblock.commands.island.setcount.description", iscc.getDescription()); + assertTrue(iscc.isConfigurableRankCommand()); + assertTrue(iscc.isOnlyPlayer()); + + } + + /** + * Test method for + * {@link world.bentobox.aoneblock.commands.island.IslandSetCountCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. + */ + public void testExecuteUserStringListOfStringShowHelp() { + assertFalse(iscc.execute(user, "", Collections.emptyList())); + verify(user).sendMessage("commands.help.header", "[label]", null); + } + + /** * Test method for {@link world.bentobox.aoneblock.commands.island.IslandSetCountCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. */ @Test @@ -180,17 +224,18 @@ public void testExecuteUserStringListOfStringNoIsland() { assertFalse(iscc.execute(user, "", List.of("2000"))); verify(user).sendMessage("general.errors.no-island"); } - - /** - * Test method for {@link world.bentobox.aoneblock.commands.island.IslandSetCountCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. - */ - @Test - public void testExecuteUserStringListOfStringLowRank() { - assertFalse(iscc.execute(user, "", List.of("2000"))); - verify(user).sendMessage("general.errors.insufficient-rank", TextVariables.RANK, RanksManager.MEMBER_RANK_REF); - } - - /** + + /** + * Test method for + * {@link world.bentobox.aoneblock.commands.island.IslandSetCountCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecuteUserStringListOfStringLowRank() { + assertFalse(iscc.execute(user, "", List.of("2000"))); + verify(user).sendMessage("general.errors.insufficient-rank", TextVariables.RANK, RanksManager.MEMBER_RANK_REF); + } + + /** * Test method for {@link world.bentobox.aoneblock.commands.island.IslandSetCountCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. */ @Test @@ -199,8 +244,8 @@ public void testExecuteUserStringListOfStringRankOKNegativeCount() { assertFalse(iscc.execute(user, "", List.of("-2000"))); verify(user).sendMessage("general.errors.must-be-positive-number", TextVariables.NUMBER, "-2000"); } - - /** + + /** * Test method for {@link world.bentobox.aoneblock.commands.island.IslandSetCountCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. */ @Test @@ -211,7 +256,7 @@ public void testExecuteUserStringListOfStringRankOKTooHighCount() { verify(user).sendMessage("aoneblock.commands.island.setcount.too-high", TextVariables.NUMBER, "0"); } - /** + /** * Test method for {@link world.bentobox.aoneblock.commands.island.IslandSetCountCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. */ @Test @@ -226,5 +271,4 @@ public void testExecuteUserStringListOfString() { } - }