From 058fb6e70ea92726ee4f3b9a1739e848bc76bddb Mon Sep 17 00:00:00 2001 From: tastybento Date: Thu, 7 Nov 2024 21:29:25 -0800 Subject: [PATCH] Use custom server to handle new enums --- .../json/adapters/BiomeTypeAdapter.java | 5 +- .../database/yaml/YamlDatabaseHandler.java | 30 ++++- .../world/bentobox/bentobox/util/Util.java | 2 - .../bentobox/AbstractCommonSetup.java | 8 ++ .../bentobox/RanksManagerBeforeClassTest.java | 6 +- .../admin/AdminResetFlagsCommandTest.java | 8 +- .../AdminBlueprintLoadCommandTest.java | 13 +- .../AdminBlueprintSaveCommandTest.java | 12 +- .../admin/range/AdminRangeCommandTest.java | 5 +- .../range/AdminRangeResetCommandTest.java | 5 +- .../admin/range/AdminRangeSetCommandTest.java | 5 +- .../team/AdminTeamDisbandCommandTest.java | 6 +- .../island/IslandExpelCommandTest.java | 18 ++- .../commands/island/IslandGoCommandTest.java | 5 +- .../island/IslandResetCommandTest.java | 10 +- .../island/IslandSpawnCommandTest.java | 7 +- .../flags/clicklisteners/CycleClickTest.java | 16 +-- .../clicklisteners/IslandToggleClickTest.java | 8 +- .../clicklisteners/WorldToggleClickTest.java | 8 +- .../bentobox/bentobox/api/user/UserTest.java | 6 +- .../commands/BentoBoxPermsCommandTest.java | 5 +- .../listeners/JoinLeaveListenerTest.java | 6 +- .../StandardSpawnProtectionListenerTest.java | 7 +- .../CommandRankClickListenerTest.java | 9 +- .../protection/LockAndBanListenerTest.java | 10 +- .../ChestDamageListenerTest.java | 5 +- .../worldsettings/CreeperListenerTest.java | 8 +- .../InvincibleVisitorsListenerTest.java | 7 +- .../ObsidianScoopingListenerTest.java | 11 +- .../VisitorKeepInventoryListenerTest.java | 5 +- .../BlueprintClipboardManagerTest.java | 17 +-- .../managers/BlueprintsManagerTest.java | 11 +- .../bentobox/managers/PlayersManagerTest.java | 6 +- .../bentobox/bentobox/mocks/ServerMocks.java | 118 ++++++++++++++++++ 34 files changed, 248 insertions(+), 160 deletions(-) create mode 100644 src/test/java/world/bentobox/bentobox/mocks/ServerMocks.java diff --git a/src/main/java/world/bentobox/bentobox/database/json/adapters/BiomeTypeAdapter.java b/src/main/java/world/bentobox/bentobox/database/json/adapters/BiomeTypeAdapter.java index b30b52b95..c7a239387 100644 --- a/src/main/java/world/bentobox/bentobox/database/json/adapters/BiomeTypeAdapter.java +++ b/src/main/java/world/bentobox/bentobox/database/json/adapters/BiomeTypeAdapter.java @@ -8,10 +8,10 @@ package world.bentobox.bentobox.database.json.adapters; import java.io.IOException; -import java.util.Arrays; import java.util.HashMap; import java.util.Map; +import org.bukkit.Registry; import org.bukkit.block.Biome; import com.google.gson.TypeAdapter; @@ -32,11 +32,12 @@ public final class BiomeTypeAdapter extends TypeAdapter */ final Map biomeMap; + @SuppressWarnings("deprecation") public BiomeTypeAdapter() { this.biomeMap = new HashMap<>(); // Put in current values. - Arrays.stream(Biome.values()).forEach(biome -> this.biomeMap.put(biome.name(), biome)); + Registry.BIOME.forEach(biome -> this.biomeMap.put(biome.name(), biome)); // Put in renamed biomes values. this.biomeMap.put("TALL_BIRCH_FOREST", Biome.OLD_GROWTH_BIRCH_FOREST); diff --git a/src/main/java/world/bentobox/bentobox/database/yaml/YamlDatabaseHandler.java b/src/main/java/world/bentobox/bentobox/database/yaml/YamlDatabaseHandler.java index 5cfe753f5..2a8c28149 100644 --- a/src/main/java/world/bentobox/bentobox/database/yaml/YamlDatabaseHandler.java +++ b/src/main/java/world/bentobox/bentobox/database/yaml/YamlDatabaseHandler.java @@ -26,8 +26,13 @@ import java.util.UUID; import java.util.concurrent.CompletableFuture; import org.bukkit.Bukkit; +import org.bukkit.Keyed; import org.bukkit.Location; +import org.bukkit.NamespacedKey; +import org.bukkit.Registry; +import org.bukkit.Sound; import org.bukkit.World; +import org.bukkit.block.Biome; import org.bukkit.configuration.MemorySection; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.EntityType; @@ -216,10 +221,24 @@ public class YamlDatabaseHandler extends AbstractDatabaseHandler { try { // Floats need special handling because the database returns them as doubles Type setType = propertyDescriptor.getWriteMethod().getGenericParameterTypes()[0]; + BentoBox.getInstance().logDebug("name = " + setType.getTypeName()); + plugin.logError("Default setting value will be used: " + + propertyDescriptor.getReadMethod().invoke(instance)); + plugin.logError(method.getName()); + plugin.logError(propertyDescriptor.getReadMethod().getName()); + plugin.logError(instance.toString()); if (setType.getTypeName().equals("float")) { double d = (double) setTo; float f = (float)d; method.invoke(instance, f); + } else if (setType.getTypeName().equals("org.bukkit.Sound")) { + Sound s = Registry.SOUNDS + .get(NamespacedKey.fromString(((String) setTo).toLowerCase(Locale.ENGLISH))); + method.invoke(instance, s); + } else if (setType.getTypeName().equals("org.bukkit.block.Biome")) { + Biome b = Registry.BIOME + .get(NamespacedKey.fromString(((String) setTo).toLowerCase(Locale.ENGLISH))); + method.invoke(instance, b); } else { method.invoke(instance, setTo); } @@ -368,8 +387,9 @@ public class YamlDatabaseHandler extends AbstractDatabaseHandler { // Get the read method Method method = propertyDescriptor.getReadMethod(); // Invoke the read method to get the value. We have no idea what type of value it is. + BentoBox.getInstance().logDebug("Method is " + method.getName()); Object value = method.invoke(instance); - + BentoBox.getInstance().logDebug("Value is " + value); String storageLocation = field.getName(); // Check if there is an annotation on the field @@ -395,6 +415,7 @@ public class YamlDatabaseHandler extends AbstractDatabaseHandler { } if (!checkAdapter(field, config, storageLocation, value)) { + BentoBox.getInstance().logDebug("No adapter"); // Set the filename if it has not be set already if (filename.isEmpty() && method.getName().equals("getUniqueId")) { // Save the name for when the file is saved @@ -407,6 +428,7 @@ public class YamlDatabaseHandler extends AbstractDatabaseHandler { serializeSet((Set)value, config, storageLocation); } else { // For all other data that doesn't need special serialization + BentoBox.getInstance().logDebug("For all other data that doesn't need special serializationr"); config.set(storageLocation, serialize(value)); } } @@ -561,6 +583,7 @@ public class YamlDatabaseHandler extends AbstractDatabaseHandler { private Object serialize(@Nullable Object object) { // Null is a value object and is serialized as the string "null" if (object == null) { + BentoBox.getInstance().logDebug("Object is null"); return "null"; } // UUID has it's own serialization, that is not picked up automatically @@ -575,6 +598,11 @@ public class YamlDatabaseHandler extends AbstractDatabaseHandler { if (object instanceof Location l) { return Util.getStringLocation(l); } + // Keyed interfaces that are replacing enums + if (object instanceof Keyed k) { + BentoBox.getInstance().logDebug("Object is keyed"); + return k.getKey().getKey(); + } // Enums if (object instanceof Enum e) { //Custom enums are a child of the Enum class. Just get the names of each one. diff --git a/src/main/java/world/bentobox/bentobox/util/Util.java b/src/main/java/world/bentobox/bentobox/util/Util.java index fcfdcaf71..00434d260 100644 --- a/src/main/java/world/bentobox/bentobox/util/Util.java +++ b/src/main/java/world/bentobox/bentobox/util/Util.java @@ -743,7 +743,6 @@ public class Util { throw new IllegalStateException("Class " + clazz.getName() + " does not implement WorldRegenerator"); } } catch (Exception e) { - e.printStackTrace(); plugin.logWarning("No Regenerator found for " + bukkitVersion + ", falling back to Bukkit API."); handler = new world.bentobox.bentobox.nms.fallback.WorldRegeneratorImpl(); } @@ -773,7 +772,6 @@ public class Util { throw new IllegalStateException("Class " + clazz.getName() + " does not implement PasteHandler"); } } catch (Exception e) { - e.printStackTrace(); plugin.logWarning("No PasteHandler found for " + bukkitVersion + ", falling back to Bukkit API."); handler = new world.bentobox.bentobox.nms.fallback.PasteHandlerImpl(); } diff --git a/src/test/java/world/bentobox/bentobox/AbstractCommonSetup.java b/src/test/java/world/bentobox/bentobox/AbstractCommonSetup.java index 3d4128bcb..73596f4d4 100644 --- a/src/test/java/world/bentobox/bentobox/AbstractCommonSetup.java +++ b/src/test/java/world/bentobox/bentobox/AbstractCommonSetup.java @@ -17,6 +17,7 @@ import java.util.UUID; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; +import org.bukkit.Server; import org.bukkit.Tag; import org.bukkit.World; import org.bukkit.block.Block; @@ -34,6 +35,7 @@ import org.bukkit.plugin.PluginManager; import org.bukkit.util.Vector; import org.eclipse.jdt.annotation.Nullable; import org.junit.After; +import org.junit.Before; import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; import org.mockito.Mock; @@ -58,6 +60,7 @@ import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.LocalesManager; import world.bentobox.bentobox.managers.PlaceholdersManager; import world.bentobox.bentobox.managers.PlayersManager; +import world.bentobox.bentobox.mocks.ServerMocks; import world.bentobox.bentobox.util.Util; /** @@ -103,9 +106,13 @@ public abstract class AbstractCommonSetup { protected FlagsManager fm; @Mock protected Spigot spigot; + protected Server server; + @Before public void setUp() throws Exception { + + server = ServerMocks.newServer(); // Bukkit PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); // Set up plugin @@ -221,6 +228,7 @@ public abstract class AbstractCommonSetup { */ @After public void tearDown() throws Exception { + ServerMocks.unsetBukkitServer(); User.clearUsers(); Mockito.framework().clearInlineMocks(); } diff --git a/src/test/java/world/bentobox/bentobox/RanksManagerBeforeClassTest.java b/src/test/java/world/bentobox/bentobox/RanksManagerBeforeClassTest.java index 4f6f589b8..193bc531b 100644 --- a/src/test/java/world/bentobox/bentobox/RanksManagerBeforeClassTest.java +++ b/src/test/java/world/bentobox/bentobox/RanksManagerBeforeClassTest.java @@ -29,7 +29,6 @@ import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.reflect.Whitebox; -import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.database.AbstractDatabaseHandler; import world.bentobox.bentobox.database.DatabaseSetup; import world.bentobox.bentobox.managers.RanksManager; @@ -126,9 +125,8 @@ public abstract class RanksManagerBeforeClassTest extends AbstractCommonSetup { } @After - public void tearDown() throws IOException { - User.clearUsers(); - Mockito.framework().clearInlineMocks(); + public void tearDown() throws Exception { + super.tearDown(); deleteAll(new File("database")); deleteAll(new File("database_backup")); } diff --git a/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminResetFlagsCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminResetFlagsCommandTest.java index 3e2be1e67..858bb9a33 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminResetFlagsCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminResetFlagsCommandTest.java @@ -26,7 +26,6 @@ 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.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @@ -128,12 +127,9 @@ public class AdminResetFlagsCommandTest extends AbstractCommonSetup { } - /** - */ @After - public void tearDown() { - User.clearUsers(); - Mockito.framework().clearInlineMocks(); + public void tearDown() throws Exception { + super.tearDown(); } /** diff --git a/src/test/java/world/bentobox/bentobox/api/commands/admin/blueprints/AdminBlueprintLoadCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/admin/blueprints/AdminBlueprintLoadCommandTest.java index 81c683cb8..284028a36 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/admin/blueprints/AdminBlueprintLoadCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/admin/blueprints/AdminBlueprintLoadCommandTest.java @@ -24,7 +24,7 @@ import java.util.UUID; import org.bukkit.Bukkit; import org.junit.After; import org.junit.Before; -import org.junit.Ignore; +import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; @@ -43,6 +43,7 @@ import world.bentobox.bentobox.managers.BlueprintsManager; import world.bentobox.bentobox.managers.CommandsManager; import world.bentobox.bentobox.managers.HooksManager; import world.bentobox.bentobox.managers.LocalesManager; +import world.bentobox.bentobox.mocks.ServerMocks; /** * @author tastybento @@ -68,8 +69,11 @@ public class AdminBlueprintLoadCommandTest { private Map map; private File blueprintsFolder; - /** - */ + @BeforeClass + public static void beforeClass() { + ServerMocks.newServer(); + } + @Before public void setUp() throws Exception { // Set up plugin @@ -125,8 +129,6 @@ public class AdminBlueprintLoadCommandTest { abcc = new AdminBlueprintLoadCommand(ac); } - /** - */ @After public void tearDown() throws Exception { User.clearUsers(); @@ -181,7 +183,6 @@ public class AdminBlueprintLoadCommandTest { * Test method for {@link world.bentobox.bentobox.api.commands.admin.blueprints.AdminBlueprintLoadCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. */ @Test - @Ignore("Enums") public void testExecuteUserStringListOfStringSuccessCaps() { assertTrue(abcc.execute(user, "", List.of("island"))); verify(user).sendMessage("general.success"); diff --git a/src/test/java/world/bentobox/bentobox/api/commands/admin/blueprints/AdminBlueprintSaveCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/admin/blueprints/AdminBlueprintSaveCommandTest.java index 36601ad0e..ac5386a6c 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/admin/blueprints/AdminBlueprintSaveCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/admin/blueprints/AdminBlueprintSaveCommandTest.java @@ -25,7 +25,7 @@ import org.bukkit.Bukkit; import org.bukkit.util.Vector; import org.junit.After; import org.junit.Before; -import org.junit.Ignore; +import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; @@ -45,6 +45,7 @@ import world.bentobox.bentobox.managers.BlueprintsManager; import world.bentobox.bentobox.managers.CommandsManager; import world.bentobox.bentobox.managers.HooksManager; import world.bentobox.bentobox.managers.LocalesManager; +import world.bentobox.bentobox.mocks.ServerMocks; /** * @author tastybento @@ -68,8 +69,11 @@ public class AdminBlueprintSaveCommandTest { private BlueprintsManager bm; private Blueprint bp = new Blueprint(); - /** - */ + @BeforeClass + public static void beforeClass() { + ServerMocks.newServer(); + } + @Before public void setUp() throws Exception { // Set up plugin @@ -200,7 +204,6 @@ public class AdminBlueprintSaveCommandTest { * Test method for {@link world.bentobox.bentobox.api.commands.admin.blueprints.AdminBlueprintSaveCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. */ @Test - @Ignore("Enums") public void testExecuteUserStringListOfString() { testCanExecute(); assertTrue(absc.execute(user, "", List.of("island"))); @@ -212,7 +215,6 @@ public class AdminBlueprintSaveCommandTest { * Test method for {@link world.bentobox.bentobox.api.commands.admin.blueprints.AdminBlueprintSaveCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. */ @Test - @Ignore("Enums") public void testExecuteUserStringListOfStringFileExists() { testCanExecute(); assertTrue(absc.execute(user, "", List.of("island"))); diff --git a/src/test/java/world/bentobox/bentobox/api/commands/admin/range/AdminRangeCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/admin/range/AdminRangeCommandTest.java index 0d87b0d1e..c6384b23e 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/admin/range/AdminRangeCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/admin/range/AdminRangeCommandTest.java @@ -94,9 +94,8 @@ public class AdminRangeCommandTest extends AbstractCommonSetup { } @After - public void tearDown() { - User.clearUsers(); - Mockito.framework().clearInlineMocks(); + public void tearDown() throws Exception { + super.tearDown(); } @Test diff --git a/src/test/java/world/bentobox/bentobox/api/commands/admin/range/AdminRangeResetCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/admin/range/AdminRangeResetCommandTest.java index 49fd2c3ac..a9c0952e0 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/admin/range/AdminRangeResetCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/admin/range/AdminRangeResetCommandTest.java @@ -117,9 +117,8 @@ public class AdminRangeResetCommandTest extends AbstractCommonSetup { } @After - public void tearDown() { - User.clearUsers(); - Mockito.framework().clearInlineMocks(); + public void tearDown() throws Exception { + super.tearDown(); } /** diff --git a/src/test/java/world/bentobox/bentobox/api/commands/admin/range/AdminRangeSetCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/admin/range/AdminRangeSetCommandTest.java index 01ed546c7..819aecce5 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/admin/range/AdminRangeSetCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/admin/range/AdminRangeSetCommandTest.java @@ -119,9 +119,8 @@ public class AdminRangeSetCommandTest extends AbstractCommonSetup { } @After - public void tearDown() { - User.clearUsers(); - Mockito.framework().clearInlineMocks(); + public void tearDown() throws Exception { + super.tearDown(); } /** diff --git a/src/test/java/world/bentobox/bentobox/api/commands/admin/team/AdminTeamDisbandCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/admin/team/AdminTeamDisbandCommandTest.java index 70aa816f9..e9a245be7 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/admin/team/AdminTeamDisbandCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/admin/team/AdminTeamDisbandCommandTest.java @@ -5,7 +5,6 @@ import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.framework; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.times; @@ -152,9 +151,8 @@ public class AdminTeamDisbandCommandTest extends AbstractCommonSetup { } @After - public void tearDown() { - User.clearUsers(); - framework().clearInlineMocks(); + public void tearDown() throws Exception { + super.tearDown(); } /** diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/IslandExpelCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/IslandExpelCommandTest.java index 07edaef52..f99ae73de 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/island/IslandExpelCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/IslandExpelCommandTest.java @@ -21,13 +21,12 @@ import java.util.UUID; import org.bukkit.Bukkit; import org.bukkit.Location; -import org.bukkit.Server; import org.bukkit.World; import org.bukkit.entity.Player; import org.bukkit.plugin.PluginManager; import org.bukkit.scheduler.BukkitScheduler; +import org.junit.After; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; @@ -75,12 +74,11 @@ public class IslandExpelCommandTest extends RanksManagerBeforeClassTest { private Addon addon; private IslandExpelCommand iec; - @Mock - private Server server; - + @Before public void setUp() throws Exception { super.setUp(); + User.setPlugin(plugin); // Command manager @@ -97,7 +95,6 @@ public class IslandExpelCommandTest extends RanksManagerBeforeClassTest { when(user.isOp()).thenReturn(false); uuid = UUID.randomUUID(); when(user.getUniqueId()).thenReturn(uuid); - when(server.getOnlinePlayers()).thenReturn(Collections.emptySet()); when(mockPlayer.getServer()).thenReturn(server); when(user.getPlayer()).thenReturn(mockPlayer); when(user.getName()).thenReturn("tastybento"); @@ -155,6 +152,11 @@ public class IslandExpelCommandTest extends RanksManagerBeforeClassTest { iec = new IslandExpelCommand(ic); } + @After + public void tearDown() throws Exception { + super.tearDown(); + } + /** * Test method for * {@link world.bentobox.bentobox.api.commands.island.IslandExpelCommand#IslandExpelCommand(world.bentobox.bentobox.api.commands.CompositeCommand)}. @@ -369,7 +371,6 @@ public class IslandExpelCommandTest extends RanksManagerBeforeClassTest { * {@link world.bentobox.bentobox.api.commands.island.IslandExpelCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. */ @Test - @Ignore("Enums") public void testExecuteUserStringListOfStringHasIsland() { testCanExecute(); assertTrue(iec.execute(user, "", Collections.singletonList("tasty"))); @@ -383,7 +384,6 @@ public class IslandExpelCommandTest extends RanksManagerBeforeClassTest { * {@link world.bentobox.bentobox.api.commands.island.IslandExpelCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. */ @Test - @Ignore("Enums") public void testExecuteUserStringListOfStringNoIslandSendToSpawn() { Optional optionalIsland = Optional.of(island); when(im.getSpawn(any())).thenReturn(optionalIsland); @@ -400,7 +400,6 @@ public class IslandExpelCommandTest extends RanksManagerBeforeClassTest { * {@link world.bentobox.bentobox.api.commands.island.IslandExpelCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. */ @Test - @Ignore("Enums") public void testExecuteUserStringListOfStringCreateIsland() { GameModeAddon gma = mock(GameModeAddon.class); CompositeCommand pc = mock(CompositeCommand.class); @@ -423,7 +422,6 @@ public class IslandExpelCommandTest extends RanksManagerBeforeClassTest { * {@link world.bentobox.bentobox.api.commands.island.IslandExpelCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. */ @Test - @Ignore("Enums") public void testExecuteUserStringListOfStringCreateIslandFailCommand() { GameModeAddon gma = mock(GameModeAddon.class); CompositeCommand pc = mock(CompositeCommand.class); diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/IslandGoCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/IslandGoCommandTest.java index 5d3af68ef..c278f45b8 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/island/IslandGoCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/IslandGoCommandTest.java @@ -176,9 +176,8 @@ public class IslandGoCommandTest extends AbstractCommonSetup { } @After - public void tearDown() { - User.clearUsers(); - Mockito.framework().clearInlineMocks(); + public void tearDown() throws Exception { + super.tearDown(); } /** diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/IslandResetCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/IslandResetCommandTest.java index ca5c6a65f..7f2cc43ab 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/island/IslandResetCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/IslandResetCommandTest.java @@ -187,6 +187,10 @@ public class IslandResetCommandTest extends AbstractCommonSetup { irc = new IslandResetCommand(ic); } + @After + public void tearDown() throws Exception { + super.tearDown(); + } /** * Test method for * {@link IslandResetCommand#canExecute(User, String, java.util.List)} @@ -254,12 +258,6 @@ public class IslandResetCommandTest extends AbstractCommonSetup { checkSpigotMessage("commands.island.reset.kicked-from-island", 11); } - @After - public void tearDown() { - User.clearUsers(); - Mockito.framework().clearInlineMocks(); - } - /** * Test method for {@link IslandResetCommand#canExecute(User, String, java.util.List)} */ diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/IslandSpawnCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/IslandSpawnCommandTest.java index 47c87a3a1..931adb747 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/island/IslandSpawnCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/IslandSpawnCommandTest.java @@ -130,12 +130,9 @@ public class IslandSpawnCommandTest extends AbstractCommonSetup { isc = new IslandSpawnCommand(ic); } - /** - */ @After - public void tearDown() { - User.clearUsers(); - Mockito.framework().clearInlineMocks(); + public void tearDown() throws Exception { + super.tearDown(); } /** diff --git a/src/test/java/world/bentobox/bentobox/api/flags/clicklisteners/CycleClickTest.java b/src/test/java/world/bentobox/bentobox/api/flags/clicklisteners/CycleClickTest.java index 45302c2fb..40d8dac64 100644 --- a/src/test/java/world/bentobox/bentobox/api/flags/clicklisteners/CycleClickTest.java +++ b/src/test/java/world/bentobox/bentobox/api/flags/clicklisteners/CycleClickTest.java @@ -32,7 +32,6 @@ import org.bukkit.scheduler.BukkitScheduler; import org.eclipse.jdt.annotation.NonNull; import org.junit.After; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; @@ -56,6 +55,7 @@ import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.LocalesManager; import world.bentobox.bentobox.managers.PlayersManager; import world.bentobox.bentobox.managers.RanksManager; +import world.bentobox.bentobox.mocks.ServerMocks; import world.bentobox.bentobox.panels.settings.SettingsTab; import world.bentobox.bentobox.util.Util; @@ -96,12 +96,13 @@ public class CycleClickTest { @Mock private @NonNull Player p; + /** * @throws java.lang.Exception - exception */ @Before public void setUp() throws Exception { - + ServerMocks.newServer(); // Set up plugin Whitebox.setInternalState(BentoBox.class, "instance", plugin); @@ -234,11 +235,11 @@ public class CycleClickTest { @After public void tearDown() { + ServerMocks.unsetBukkitServer(); Mockito.framework().clearInlineMocks(); } @Test - @Ignore("Enums") public void testNoPremission() { when(user.hasPermission(anyString())).thenReturn(false); CycleClick udc = new CycleClick(LOCK); @@ -256,7 +257,6 @@ public class CycleClickTest { * Test for {@link CycleClick#onClick(world.bentobox.bentobox.api.panels.Panel, User, ClickType, int)} */ @Test - @Ignore("Enums") public void testOnLeftClick() { final int SLOT = 5; CycleClick udc = new CycleClick(LOCK); @@ -276,7 +276,6 @@ public class CycleClickTest { * Test for {@link CycleClick#onClick(world.bentobox.bentobox.api.panels.Panel, User, ClickType, int)} */ @Test - @Ignore("Enums") public void testOnLeftClickSetMinMax() { // Provide a current rank value - coop when(island.getFlag(any())).thenReturn(RanksManager.COOP_RANK); @@ -298,7 +297,6 @@ public class CycleClickTest { * Test for {@link CycleClick#onClick(world.bentobox.bentobox.api.panels.Panel, User, ClickType, int)} */ @Test - @Ignore("Enums") public void testOnRightClick() { final int SLOT = 5; CycleClick udc = new CycleClick(LOCK); @@ -318,7 +316,6 @@ public class CycleClickTest { * Test for {@link CycleClick#onClick(world.bentobox.bentobox.api.panels.Panel, User, ClickType, int)} */ @Test - @Ignore("Enums") public void testOnRightClickMinMaxSet() { // Provide a current rank value - coop when(island.getFlag(any())).thenReturn(RanksManager.TRUSTED_RANK); @@ -340,7 +337,6 @@ public class CycleClickTest { * Test for {@link CycleClick#onClick(world.bentobox.bentobox.api.panels.Panel, User, ClickType, int)} */ @Test - @Ignore("Enums") public void testAllClicks() { // Test all possible click types CycleClick udc = new CycleClick(LOCK); @@ -371,7 +367,6 @@ public class CycleClickTest { * Test for {@link CycleClick#onClick(world.bentobox.bentobox.api.panels.Panel, User, ClickType, int)} */ @Test - @Ignore("Enums") public void testOnShiftLeftClickIsOp() { when(user.isOp()).thenReturn(true); CycleClick udc = new CycleClick(LOCK); @@ -384,7 +379,6 @@ public class CycleClickTest { assertTrue(udc.onClick(panel, user, ClickType.SHIFT_LEFT, SLOT)); assertTrue(hiddenFlags.isEmpty()); // Verify sounds - verify(p).playSound(user.getLocation(), Sound.BLOCK_GLASS_BREAK, 1F, 1F); - verify(p).playSound(user.getLocation(), Sound.BLOCK_NOTE_BLOCK_CHIME, 1F, 1F); + verify(p, times(2)).playSound((Location) null, (Sound) null, 1F, 1F); } } diff --git a/src/test/java/world/bentobox/bentobox/api/flags/clicklisteners/IslandToggleClickTest.java b/src/test/java/world/bentobox/bentobox/api/flags/clicklisteners/IslandToggleClickTest.java index 30b24799c..bd697a8ca 100644 --- a/src/test/java/world/bentobox/bentobox/api/flags/clicklisteners/IslandToggleClickTest.java +++ b/src/test/java/world/bentobox/bentobox/api/flags/clicklisteners/IslandToggleClickTest.java @@ -19,7 +19,6 @@ import org.bukkit.inventory.Inventory; import org.bukkit.plugin.PluginManager; import org.junit.After; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; @@ -38,6 +37,7 @@ import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.managers.FlagsManager; import world.bentobox.bentobox.managers.IslandWorldManager; import world.bentobox.bentobox.managers.IslandsManager; +import world.bentobox.bentobox.mocks.ServerMocks; import world.bentobox.bentobox.panels.settings.SettingsTab; import world.bentobox.bentobox.util.Util; @@ -68,6 +68,7 @@ public class IslandToggleClickTest { */ @Before public void setUp() throws Exception { + ServerMocks.newServer(); PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); // Set up plugin @@ -120,11 +121,11 @@ public class IslandToggleClickTest { @After public void tearDown() { + ServerMocks.unsetBukkitServer(); Mockito.framework().clearInlineMocks(); } @Test - @Ignore("Enums") public void testOnClickNoPermission() { when(user.hasPermission(Mockito.anyString())).thenReturn(false); listener.onClick(panel, user, ClickType.LEFT, 0); @@ -132,7 +133,6 @@ public class IslandToggleClickTest { } @Test - @Ignore("Enums") public void testOnClick() { listener.onClick(panel, user, ClickType.LEFT, 0); verify(island).toggleFlag(flag); @@ -140,7 +140,6 @@ public class IslandToggleClickTest { } @Test - @Ignore("Enums") public void testOnClickNoIsland() { when(settingsTab.getIsland()).thenReturn(null); listener.onClick(panel, user, ClickType.LEFT, 0); @@ -148,7 +147,6 @@ public class IslandToggleClickTest { } @Test - @Ignore("Enums") public void testOnClickNotOwner() { // No permission when(user.hasPermission(anyString())).thenReturn(false); diff --git a/src/test/java/world/bentobox/bentobox/api/flags/clicklisteners/WorldToggleClickTest.java b/src/test/java/world/bentobox/bentobox/api/flags/clicklisteners/WorldToggleClickTest.java index d8d2bcb28..688876a78 100644 --- a/src/test/java/world/bentobox/bentobox/api/flags/clicklisteners/WorldToggleClickTest.java +++ b/src/test/java/world/bentobox/bentobox/api/flags/clicklisteners/WorldToggleClickTest.java @@ -19,7 +19,6 @@ import org.bukkit.inventory.Inventory; import org.bukkit.plugin.PluginManager; import org.junit.After; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; @@ -37,6 +36,7 @@ import world.bentobox.bentobox.api.panels.Panel; import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.managers.FlagsManager; import world.bentobox.bentobox.managers.IslandWorldManager; +import world.bentobox.bentobox.mocks.ServerMocks; import world.bentobox.bentobox.util.Util; @RunWith(PowerMockRunner.class) @@ -58,10 +58,9 @@ public class WorldToggleClickTest { @Mock private World world; - /** - */ @Before public void setUp() throws Exception { + ServerMocks.newServer(); PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); // Set up plugin BentoBox plugin = mock(BentoBox.class); @@ -105,6 +104,7 @@ public class WorldToggleClickTest { @After public void tearDown() { + ServerMocks.unsetBukkitServer(); Mockito.framework().clearInlineMocks(); } @@ -112,7 +112,6 @@ public class WorldToggleClickTest { * Test for {@link WorldToggleClick#onClick(Panel, User, ClickType, int)} */ @Test - @Ignore("Enums") public void testOnClickNoPermission() { when(user.hasPermission(anyString())).thenReturn(false); listener.onClick(panel, user, ClickType.LEFT, 0); @@ -124,7 +123,6 @@ public class WorldToggleClickTest { * Test for {@link WorldToggleClick#onClick(Panel, User, ClickType, int)} */ @Test - @Ignore("Enums") public void testOnClick() { when(user.hasPermission(anyString())).thenReturn(true); listener.onClick(panel, user, ClickType.LEFT, 0); diff --git a/src/test/java/world/bentobox/bentobox/api/user/UserTest.java b/src/test/java/world/bentobox/bentobox/api/user/UserTest.java index 2cc6fe8c3..852e03fca 100644 --- a/src/test/java/world/bentobox/bentobox/api/user/UserTest.java +++ b/src/test/java/world/bentobox/bentobox/api/user/UserTest.java @@ -45,7 +45,6 @@ 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.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @@ -136,9 +135,8 @@ public class UserTest extends AbstractCommonSetup { } @After - public void tearDown() { - User.clearUsers(); - Mockito.framework().clearInlineMocks(); + public void tearDown() throws Exception { + super.tearDown(); } diff --git a/src/test/java/world/bentobox/bentobox/commands/BentoBoxPermsCommandTest.java b/src/test/java/world/bentobox/bentobox/commands/BentoBoxPermsCommandTest.java index 338a6ea5d..a0943f280 100644 --- a/src/test/java/world/bentobox/bentobox/commands/BentoBoxPermsCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/commands/BentoBoxPermsCommandTest.java @@ -106,12 +106,9 @@ public class BentoBoxPermsCommandTest extends AbstractCommonSetup { cmd = new BentoBoxPermsCommand(ac); } - /** - * @throws java.lang.Exception - */ @After public void tearDown() throws Exception { - Mockito.framework().clearInlineMocks(); + super.tearDown(); } /** diff --git a/src/test/java/world/bentobox/bentobox/listeners/JoinLeaveListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/JoinLeaveListenerTest.java index 6ac0e9044..12f9d211d 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/JoinLeaveListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/JoinLeaveListenerTest.java @@ -35,7 +35,6 @@ 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.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @@ -206,9 +205,8 @@ public class JoinLeaveListenerTest extends RanksManagerBeforeClassTest { } @After - public void tearDown() { - User.clearUsers(); - Mockito.framework().clearInlineMocks(); + public void tearDown() throws Exception { + super.tearDown(); } /** diff --git a/src/test/java/world/bentobox/bentobox/listeners/StandardSpawnProtectionListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/StandardSpawnProtectionListenerTest.java index 2aad266a2..a654b311a 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/StandardSpawnProtectionListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/StandardSpawnProtectionListenerTest.java @@ -119,12 +119,9 @@ public class StandardSpawnProtectionListenerTest extends AbstractCommonSetup { ssp = new StandardSpawnProtectionListener(plugin); } - /** - */ @After - public void tearDown() { - User.clearUsers(); - Mockito.framework().clearInlineMocks(); + public void tearDown() throws Exception { + super.tearDown(); } diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/clicklisteners/CommandRankClickListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/clicklisteners/CommandRankClickListenerTest.java index bfdd1bf50..2e59a1051 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/clicklisteners/CommandRankClickListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/clicklisteners/CommandRankClickListenerTest.java @@ -25,8 +25,8 @@ import org.bukkit.event.inventory.ClickType; import org.bukkit.inventory.Inventory; import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.Nullable; +import org.junit.After; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; @@ -149,6 +149,11 @@ public class CommandRankClickListenerTest extends RanksManagerBeforeClassTest { crcl = new CommandRankClickListener(); } + @After + public void tearDown() throws Exception { + super.tearDown(); + } + /** * Test method for {@link world.bentobox.bentobox.listeners.flags.clicklisteners.CommandRankClickListener#onClick(world.bentobox.bentobox.api.panels.Panel, world.bentobox.bentobox.api.user.User, org.bukkit.event.inventory.ClickType, int)}. */ @@ -163,7 +168,6 @@ public class CommandRankClickListenerTest extends RanksManagerBeforeClassTest { * Test method for {@link world.bentobox.bentobox.listeners.flags.clicklisteners.CommandRankClickListener#onClick(world.bentobox.bentobox.api.panels.Panel, world.bentobox.bentobox.api.user.User, org.bukkit.event.inventory.ClickType, int)}. */ @Test - @Ignore("Enums") public void testOnClickNoPermission() { when(user.isOp()).thenReturn(false); when(user.hasPermission(anyString())).thenReturn(false); @@ -176,7 +180,6 @@ public class CommandRankClickListenerTest extends RanksManagerBeforeClassTest { * Test method for {@link world.bentobox.bentobox.listeners.flags.clicklisteners.CommandRankClickListener#onClick(world.bentobox.bentobox.api.panels.Panel, world.bentobox.bentobox.api.user.User, org.bukkit.event.inventory.ClickType, int)}. */ @Test - @Ignore("Enums") public void testOnClickNoFlag() { when(island.isAllowed(user, Flags.CHANGE_SETTINGS)).thenReturn(false); assertTrue(crcl.onClick(panel, user, ClickType.LEFT, 0)); diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/LockAndBanListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/LockAndBanListenerTest.java index 91271a55f..303697ec4 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/LockAndBanListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/LockAndBanListenerTest.java @@ -30,7 +30,6 @@ import org.bukkit.event.vehicle.VehicleMoveEvent; import org.bukkit.scheduler.BukkitScheduler; import org.junit.After; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; @@ -52,6 +51,7 @@ import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.LocalesManager; import world.bentobox.bentobox.managers.PlaceholdersManager; import world.bentobox.bentobox.managers.PlayersManager; +import world.bentobox.bentobox.mocks.ServerMocks; import world.bentobox.bentobox.util.Util; @RunWith(PowerMockRunner.class) @@ -88,6 +88,7 @@ public class LockAndBanListenerTest { @Before public void setUp() throws Exception { + ServerMocks.newServer(); // Server & Scheduler PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); when(Bukkit.getScheduler()).thenReturn(sch); @@ -190,6 +191,7 @@ public class LockAndBanListenerTest { @After public void tearDown() { + ServerMocks.unsetBukkitServer(); User.clearUsers(); framework().clearInlineMocks(); } @@ -299,7 +301,6 @@ public class LockAndBanListenerTest { } @Test - @Ignore("Enums") public void testPlayerMoveIntoBannedIsland() { // Make player when(player.getUniqueId()).thenReturn(uuid); @@ -322,7 +323,6 @@ public class LockAndBanListenerTest { } @Test - @Ignore("Enums") public void testPlayerMoveInsideBannedIsland() { // Make player when(player.getUniqueId()).thenReturn(uuid); @@ -350,7 +350,6 @@ public class LockAndBanListenerTest { } @Test - @Ignore("Enums") public void testVehicleMoveIntoBannedIsland() { // Make player when(player.getUniqueId()).thenReturn(uuid); @@ -513,7 +512,6 @@ public class LockAndBanListenerTest { } @Test - @Ignore("Enums") public void testPlayerMoveIntoLockedIsland() { // Make player when(player.getUniqueId()).thenReturn(uuid); @@ -618,7 +616,6 @@ public class LockAndBanListenerTest { } @Test - @Ignore("Enums") public void testPlayerMoveInsideLockedIsland() { // Make player when(player.getUniqueId()).thenReturn(uuid); @@ -707,7 +704,6 @@ public class LockAndBanListenerTest { } @Test - @Ignore("Enums") public void testVehicleMoveIntoLockedIsland() { // Make player Player player = mock(Player.class); diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/ChestDamageListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/ChestDamageListenerTest.java index d44ec3ab0..3c2042d23 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/ChestDamageListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/ChestDamageListenerTest.java @@ -173,9 +173,8 @@ public class ChestDamageListenerTest extends AbstractCommonSetup } @After - public void tearDown() { - User.clearUsers(); - Mockito.framework().clearInlineMocks(); + public void tearDown() throws Exception { + super.tearDown(); } /** diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/CreeperListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/CreeperListenerTest.java index a135bfa6a..0067bf715 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/CreeperListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/CreeperListenerTest.java @@ -18,13 +18,11 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.Mockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import world.bentobox.bentobox.AbstractCommonSetup; import world.bentobox.bentobox.BentoBox; -import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.util.Util; @@ -47,13 +45,9 @@ public class CreeperListenerTest extends AbstractCommonSetup { cl = new CreeperListener(); } - /** - * @throws java.lang.Exception - */ @After public void tearDown() throws Exception { - User.clearUsers(); - Mockito.framework().clearInlineMocks(); + super.tearDown(); } /** diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/InvincibleVisitorsListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/InvincibleVisitorsListenerTest.java index 615250e56..fc7181d3f 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/InvincibleVisitorsListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/InvincibleVisitorsListenerTest.java @@ -41,7 +41,6 @@ import org.bukkit.util.Vector; import org.eclipse.jdt.annotation.Nullable; import org.junit.After; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; @@ -63,6 +62,7 @@ import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.managers.FlagsManager; import world.bentobox.bentobox.managers.IslandWorldManager; import world.bentobox.bentobox.managers.IslandsManager; +import world.bentobox.bentobox.mocks.ServerMocks; import world.bentobox.bentobox.util.Util; @RunWith(PowerMockRunner.class) @@ -91,10 +91,9 @@ public class InvincibleVisitorsListenerTest { @Mock private PluginManager pim; - /** - */ @Before public void setUp() throws Exception { + ServerMocks.newServer(); PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); // Set up plugin @@ -187,6 +186,7 @@ public class InvincibleVisitorsListenerTest { @After public void tearDown() { + ServerMocks.unsetBukkitServer(); User.clearUsers(); framework().clearInlineMocks(); } @@ -199,7 +199,6 @@ public class InvincibleVisitorsListenerTest { } @Test - @Ignore("Enums") public void testOnClickNoPermission() { when(user.hasPermission(anyString())).thenReturn(false); listener.onClick(panel, user, ClickType.LEFT, 0); diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/ObsidianScoopingListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/ObsidianScoopingListenerTest.java index a4675fdb4..09b76e75a 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/ObsidianScoopingListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/ObsidianScoopingListenerTest.java @@ -29,7 +29,6 @@ import org.bukkit.inventory.PlayerInventory; import org.bukkit.util.RayTraceResult; import org.junit.After; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; @@ -141,13 +140,11 @@ public class ObsidianScoopingListenerTest extends AbstractCommonSetup { } @After - public void tearDown() { - User.clearUsers(); - Mockito.framework().clearInlineMocks(); + public void tearDown() throws Exception { + super.tearDown(); } @Test - @Ignore("Enums") public void testOnPlayerInteract() { // Test incorrect items inHand = Material.ACACIA_DOOR; @@ -157,7 +154,6 @@ public class ObsidianScoopingListenerTest extends AbstractCommonSetup { } @Test - @Ignore("Enums") public void testOnPlayerInteractBucketInHand() { // Test incorrect items inHand = Material.BUCKET; @@ -167,7 +163,6 @@ public class ObsidianScoopingListenerTest extends AbstractCommonSetup { } @Test - @Ignore("Enums") public void testOnPlayerInteractObsidianAnvilInHand() { // Test with obsidian in hand inHand = Material.ANVIL; @@ -177,7 +172,6 @@ public class ObsidianScoopingListenerTest extends AbstractCommonSetup { } @Test - @Ignore("Enums") public void testOnPlayerInteractObsidianBucketInHand() { // Positive test with 1 bucket in the stack inHand = Material.BUCKET; @@ -187,7 +181,6 @@ public class ObsidianScoopingListenerTest extends AbstractCommonSetup { } @Test - @Ignore("Enums") public void testOnPlayerInteractObsidianManyBucketsInHand() { // Positive test with 1 bucket in the stack inHand = Material.BUCKET; diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/VisitorKeepInventoryListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/VisitorKeepInventoryListenerTest.java index c58c77417..c21847e76 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/VisitorKeepInventoryListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/VisitorKeepInventoryListenerTest.java @@ -111,12 +111,9 @@ public class VisitorKeepInventoryListenerTest extends AbstractCommonSetup { l = new VisitorKeepInventoryListener(); } - /** - */ @After public void tearDown() throws Exception { - User.clearUsers(); - Mockito.framework().clearInlineMocks(); + super.tearDown(); } /** diff --git a/src/test/java/world/bentobox/bentobox/managers/BlueprintClipboardManagerTest.java b/src/test/java/world/bentobox/bentobox/managers/BlueprintClipboardManagerTest.java index efd9265a5..d6e4fc354 100644 --- a/src/test/java/world/bentobox/bentobox/managers/BlueprintClipboardManagerTest.java +++ b/src/test/java/world/bentobox/bentobox/managers/BlueprintClipboardManagerTest.java @@ -28,7 +28,6 @@ import org.bukkit.block.data.BlockData; import org.bukkit.configuration.file.YamlConfiguration; import org.junit.After; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; @@ -42,6 +41,7 @@ import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.blueprints.Blueprint; import world.bentobox.bentobox.blueprints.BlueprintClipboard; +import world.bentobox.bentobox.mocks.ServerMocks; import world.bentobox.bentobox.util.Util; @@ -104,7 +104,7 @@ public class BlueprintClipboardManagerTest { " \"ySize\": 10,\n" + " \"zSize\": 10\n" + "}"; - @Mock + private Server server; private void zip(File targetFile) throws IOException { @@ -129,6 +129,7 @@ public class BlueprintClipboardManagerTest { */ @Before public void setUp() throws Exception { + server = ServerMocks.newServer(); PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); blueprintFolder = new File("blueprints"); @@ -153,7 +154,7 @@ public class BlueprintClipboardManagerTest { */ @After public void tearDown() throws Exception { - + ServerMocks.unsetBukkitServer(); if (blueprintFolder.exists()) { // Clean up file system Files.walk(blueprintFolder.toPath()) @@ -232,7 +233,6 @@ public class BlueprintClipboardManagerTest { * Test method for {@link world.bentobox.bentobox.managers.BlueprintClipboardManager#loadBlueprint(java.lang.String)}. */ @Test - @Ignore("Enums") public void testLoadBlueprintFileInZipJSONError() throws IOException { blueprintFolder.mkdirs(); // Make a blueprint file @@ -256,7 +256,6 @@ public class BlueprintClipboardManagerTest { * Test method for {@link world.bentobox.bentobox.managers.BlueprintClipboardManager#loadBlueprint(java.lang.String)}. */ @Test - @Ignore("Enums") public void testLoadBlueprintFileInZipNoBedrock() throws IOException { blueprintFolder.mkdirs(); // Make a blueprint file @@ -277,7 +276,6 @@ public class BlueprintClipboardManagerTest { * Test method for {@link world.bentobox.bentobox.managers.BlueprintClipboardManager#loadBlueprint(java.lang.String)}. */ @Test - @Ignore("Enums") public void testLoadBlueprintFileInZip() throws IOException { blueprintFolder.mkdirs(); // Make a blueprint file @@ -299,7 +297,6 @@ public class BlueprintClipboardManagerTest { * Test method for {@link world.bentobox.bentobox.managers.BlueprintClipboardManager#load(java.lang.String)}. */ @Test - @Ignore("Enums") public void testLoadString() throws IOException { blueprintFolder.mkdirs(); // Make a blueprint file @@ -322,7 +319,6 @@ public class BlueprintClipboardManagerTest { * Test method for {@link world.bentobox.bentobox.managers.BlueprintClipboardManager#load(world.bentobox.bentobox.api.user.User, java.lang.String)}. */ @Test - @Ignore("Enums") public void testLoadUserString() throws IOException { blueprintFolder.mkdirs(); // Make a blueprint file @@ -352,7 +348,6 @@ public class BlueprintClipboardManagerTest { * Test method for {@link world.bentobox.bentobox.managers.BlueprintClipboardManager#save(world.bentobox.bentobox.api.user.User, java.lang.String, java.lang.String)}. */ @Test - @Ignore("Enums") public void testSave() throws IOException { // Load a blueprint, then save it blueprintFolder.mkdirs(); @@ -374,7 +369,6 @@ public class BlueprintClipboardManagerTest { * Test method for {@link world.bentobox.bentobox.managers.BlueprintClipboardManager#save(world.bentobox.bentobox.api.user.User, java.lang.String, java.lang.String)}. */ @Test - @Ignore("Enums") public void testSaveBadChars() throws IOException { // Load a blueprint, then save it blueprintFolder.mkdirs(); @@ -396,7 +390,6 @@ public class BlueprintClipboardManagerTest { * Test method for {@link world.bentobox.bentobox.managers.BlueprintClipboardManager#save(world.bentobox.bentobox.api.user.User, java.lang.String, java.lang.String)}. */ @Test - @Ignore("Enums") public void testSaveForeignChars() throws IOException { // Load a blueprint, then save it blueprintFolder.mkdirs(); @@ -418,7 +411,6 @@ public class BlueprintClipboardManagerTest { * Test method for {@link world.bentobox.bentobox.managers.BlueprintClipboardManager#save(world.bentobox.bentobox.api.user.User, java.lang.String, java.lang.String)}. */ @Test - @Ignore("Enums") public void testSaveForeignBadChars() throws IOException { // Load a blueprint, then save it blueprintFolder.mkdirs(); @@ -453,7 +445,6 @@ public class BlueprintClipboardManagerTest { * Test method for {@link world.bentobox.bentobox.managers.BlueprintClipboardManager#saveBlueprint(world.bentobox.bentobox.blueprints.Blueprint)}. */ @Test - @Ignore("Enums") public void testSaveBlueprintSuccess() { BlueprintClipboardManager bcm = new BlueprintClipboardManager(plugin, blueprintFolder); Blueprint blueprint = new Blueprint(); diff --git a/src/test/java/world/bentobox/bentobox/managers/BlueprintsManagerTest.java b/src/test/java/world/bentobox/bentobox/managers/BlueprintsManagerTest.java index 76cef0aa0..9cd41f9f9 100644 --- a/src/test/java/world/bentobox/bentobox/managers/BlueprintsManagerTest.java +++ b/src/test/java/world/bentobox/bentobox/managers/BlueprintsManagerTest.java @@ -37,7 +37,6 @@ import org.bukkit.scheduler.BukkitTask; import org.bukkit.util.Vector; import org.junit.After; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; @@ -61,6 +60,7 @@ import world.bentobox.bentobox.blueprints.BlueprintPaster; import world.bentobox.bentobox.blueprints.dataobjects.BlueprintBlock; import world.bentobox.bentobox.blueprints.dataobjects.BlueprintBundle; import world.bentobox.bentobox.database.objects.Island; +import world.bentobox.bentobox.mocks.ServerMocks; import world.bentobox.bentobox.util.Util; /** @@ -99,11 +99,12 @@ public class BlueprintsManagerTest { private BukkitTask task; private int times; - @Mock + private Server server; @Before public void setUp() throws Exception { + server = ServerMocks.newServer(); // Set up plugin BentoBox plugin = mock(BentoBox.class); Whitebox.setInternalState(BentoBox.class, "instance", plugin); @@ -188,6 +189,7 @@ public class BlueprintsManagerTest { */ @After public void tearDown() throws Exception { + ServerMocks.unsetBukkitServer(); // Clean up file system deleteDir(dataFolder.toPath()); // Delete addon.jar @@ -259,7 +261,6 @@ public class BlueprintsManagerTest { * Test method for {@link world.bentobox.bentobox.managers.BlueprintsManager#loadBlueprintBundles(world.bentobox.bentobox.api.addons.GameModeAddon)}. */ @Test - @Ignore("Enums") public void testLoadBlueprintBundlesNoBlueprintFolder() { // Set up running and verification when(scheduler.runTaskAsynchronously(eq(plugin), any(Runnable.class))).thenAnswer((Answer) invocation -> { @@ -280,7 +281,6 @@ public class BlueprintsManagerTest { * Test method for {@link world.bentobox.bentobox.managers.BlueprintsManager#loadBlueprintBundles(world.bentobox.bentobox.api.addons.GameModeAddon)}. */ @Test - @Ignore("Enums") public void testLoadBlueprintBundles() { // Set up running and verification when(scheduler.runTaskAsynchronously(eq(plugin), any(Runnable.class))).thenAnswer((Answer) invocation -> { @@ -319,7 +319,6 @@ public class BlueprintsManagerTest { * Test method for {@link world.bentobox.bentobox.managers.BlueprintsManager#loadBlueprints(world.bentobox.bentobox.api.addons.GameModeAddon)}. */ @Test - @Ignore("Enums") public void testLoadBlueprints() { // Set up running and verification when(scheduler.runTaskAsynchronously(eq(plugin), any(Runnable.class))).thenAnswer((Answer) invocation -> { @@ -352,7 +351,6 @@ public class BlueprintsManagerTest { * Test method for {@link world.bentobox.bentobox.managers.BlueprintsManager#saveBlueprint(world.bentobox.bentobox.api.addons.GameModeAddon, world.bentobox.bentobox.blueprints.Blueprint)}. */ @Test - @Ignore("Enums") public void testSaveBlueprint() { // Save it BlueprintsManager bpm = new BlueprintsManager(plugin); @@ -650,7 +648,6 @@ public class BlueprintsManagerTest { * Test method for {@link world.bentobox.bentobox.managers.BlueprintsManager#renameBlueprint(world.bentobox.bentobox.api.addons.GameModeAddon, world.bentobox.bentobox.blueprints.Blueprint, java.lang.String, java.lang.String)}. */ @Test - @Ignore("Enums") public void testRenameBlueprint() { // Save it BlueprintsManager bpm = new BlueprintsManager(plugin); diff --git a/src/test/java/world/bentobox/bentobox/managers/PlayersManagerTest.java b/src/test/java/world/bentobox/bentobox/managers/PlayersManagerTest.java index d761bad06..d550ca413 100644 --- a/src/test/java/world/bentobox/bentobox/managers/PlayersManagerTest.java +++ b/src/test/java/world/bentobox/bentobox/managers/PlayersManagerTest.java @@ -45,7 +45,6 @@ import org.bukkit.util.Vector; import org.junit.After; import org.junit.Before; import org.junit.BeforeClass; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; @@ -66,6 +65,7 @@ import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.database.objects.Names; import world.bentobox.bentobox.database.objects.Players; import world.bentobox.bentobox.hooks.VaultHook; +import world.bentobox.bentobox.mocks.ServerMocks; import world.bentobox.bentobox.util.Util; /** @@ -74,7 +74,6 @@ import world.bentobox.bentobox.util.Util; */ @RunWith(PowerMockRunner.class) @PrepareForTest({ Bukkit.class, BentoBox.class, User.class, Util.class, Logger.class, DatabaseSetup.class }) -@Ignore("Enums") public class PlayersManagerTest { private static AbstractDatabaseHandler handler; @@ -136,6 +135,8 @@ public class PlayersManagerTest { public void setUp() throws Exception { // Clear any lingering database tearDown(); + + ServerMocks.newServer(); // Set up plugin Whitebox.setInternalState(BentoBox.class, "instance", plugin); when(plugin.getVault()).thenReturn(Optional.of(vault)); @@ -273,6 +274,7 @@ public class PlayersManagerTest { @After public void tearDown() throws Exception { + ServerMocks.unsetBukkitServer(); User.clearUsers(); Mockito.framework().clearInlineMocks(); deleteAll(new File("database")); diff --git a/src/test/java/world/bentobox/bentobox/mocks/ServerMocks.java b/src/test/java/world/bentobox/bentobox/mocks/ServerMocks.java new file mode 100644 index 000000000..8fd039318 --- /dev/null +++ b/src/test/java/world/bentobox/bentobox/mocks/ServerMocks.java @@ -0,0 +1,118 @@ +package world.bentobox.bentobox.mocks; + +import static org.mockito.ArgumentMatchers.notNull; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.lang.reflect.Field; +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; +import java.util.Set; +import java.util.logging.Logger; + +import org.bukkit.Bukkit; +import org.bukkit.Keyed; +import org.bukkit.NamespacedKey; +import org.bukkit.Registry; +import org.bukkit.Server; +import org.bukkit.Tag; +import org.bukkit.UnsafeValues; +import org.eclipse.jdt.annotation.NonNull; + +public final class ServerMocks { + + public static @NonNull Server newServer() { + Server mock = mock(Server.class); + + Logger noOp = mock(Logger.class); + when(mock.getLogger()).thenReturn(noOp); + when(mock.isPrimaryThread()).thenReturn(true); + + // Unsafe + UnsafeValues unsafe = mock(UnsafeValues.class); + when(mock.getUnsafe()).thenReturn(unsafe); + + // Server must be available before tags can be mocked. + Bukkit.setServer(mock); + + // Bukkit has a lot of static constants referencing registry values. To initialize those, the + // registries must be able to be fetched before the classes are touched. + Map, Object> registers = new HashMap<>(); + + doAnswer(invocationGetRegistry -> registers.computeIfAbsent(invocationGetRegistry.getArgument(0), clazz -> { + Registry registry = mock(Registry.class); + Map cache = new HashMap<>(); + doAnswer(invocationGetEntry -> { + NamespacedKey key = invocationGetEntry.getArgument(0); + // Some classes (like BlockType and ItemType) have extra generics that will be + // erased during runtime calls. To ensure accurate typing, grab the constant's field. + // This approach also allows us to return null for unsupported keys. + Class constantClazz; + try { + //noinspection unchecked + constantClazz = (Class) clazz + .getField(key.getKey().toUpperCase(Locale.ROOT).replace('.', '_')).getType(); + } catch (ClassCastException e) { + throw new RuntimeException(e); + } catch (NoSuchFieldException e) { + return null; + } + + return cache.computeIfAbsent(key, key1 -> { + Keyed keyed = mock(constantClazz); + doReturn(key).when(keyed).getKey(); + return keyed; + }); + }).when(registry).get(notNull()); + return registry; + })).when(mock).getRegistry(notNull()); + + // Tags are dependent on registries, but use a different method. + // This will set up blank tags for each constant; all that needs to be done to render them + // functional is to re-mock Tag#getValues. + doAnswer(invocationGetTag -> { + Tag tag = mock(Tag.class); + doReturn(invocationGetTag.getArgument(1)).when(tag).getKey(); + doReturn(Set.of()).when(tag).getValues(); + doAnswer(invocationIsTagged -> { + Keyed keyed = invocationIsTagged.getArgument(0); + Class type = invocationGetTag.getArgument(2); + if (!type.isAssignableFrom(keyed.getClass())) { + return null; + } + // Since these are mocks, the exact instance might not be equal. Consider equal keys equal. + return tag.getValues().contains(keyed) + || tag.getValues().stream().anyMatch(value -> value.getKey().equals(keyed.getKey())); + }).when(tag).isTagged(notNull()); + return tag; + }).when(mock).getTag(notNull(), notNull(), notNull()); + + // Once the server is all set up, touch BlockType and ItemType to initialize. + // This prevents issues when trying to access dependent methods from a Material constant. + try { + Class.forName("org.bukkit.inventory.ItemType"); + Class.forName("org.bukkit.block.BlockType"); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + + return mock; + } + + public static void unsetBukkitServer() { + try { + Field server = Bukkit.class.getDeclaredField("server"); + server.setAccessible(true); + server.set(null, null); + } catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException e) { + throw new RuntimeException(e); + } + } + + private ServerMocks() { + } + +} \ No newline at end of file