From 224be307a0f70bad62ebe961d921e83603971481 Mon Sep 17 00:00:00 2001 From: tastybento Date: Wed, 8 Feb 2023 18:40:03 -0800 Subject: [PATCH 1/5] Fix JavaDoc errors --- .../bentobox/database/json/adapters/EnumTypeAdapter.java | 2 +- .../flags/worldsettings/CleanSuperFlatListener.java | 1 - .../java/world/bentobox/bentobox/util/ItemParser.java | 8 ++++++++ src/main/java/world/bentobox/bentobox/util/Util.java | 2 +- 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/database/json/adapters/EnumTypeAdapter.java b/src/main/java/world/bentobox/bentobox/database/json/adapters/EnumTypeAdapter.java index edccf73b8..1daff230b 100644 --- a/src/main/java/world/bentobox/bentobox/database/json/adapters/EnumTypeAdapter.java +++ b/src/main/java/world/bentobox/bentobox/database/json/adapters/EnumTypeAdapter.java @@ -21,7 +21,7 @@ public final class EnumTypeAdapter> extends TypeAdapter { /** - * Bimap to store name <-> enum references + * Bimap to store name,enum pair references */ private final BiMap enumMap = HashBiMap.create(); diff --git a/src/main/java/world/bentobox/bentobox/listeners/flags/worldsettings/CleanSuperFlatListener.java b/src/main/java/world/bentobox/bentobox/listeners/flags/worldsettings/CleanSuperFlatListener.java index 0c5471707..3d91e2586 100644 --- a/src/main/java/world/bentobox/bentobox/listeners/flags/worldsettings/CleanSuperFlatListener.java +++ b/src/main/java/world/bentobox/bentobox/listeners/flags/worldsettings/CleanSuperFlatListener.java @@ -103,7 +103,6 @@ public class CleanSuperFlatListener extends FlagListener { /** * This method clears the chunk from queue in the given world * @param world The world that must be cleared. - * @param cg Chunk generator. */ private void cleanChunk(World world) { diff --git a/src/main/java/world/bentobox/bentobox/util/ItemParser.java b/src/main/java/world/bentobox/bentobox/util/ItemParser.java index 31cc9e197..49335b69e 100644 --- a/src/main/java/world/bentobox/bentobox/util/ItemParser.java +++ b/src/main/java/world/bentobox/bentobox/util/ItemParser.java @@ -182,9 +182,13 @@ public class ItemParser { /** * This method parses array of 6 items into an item stack. * Format: + *
{@code 
      *      POTION:NAME::::QTY
+     * }
* Example: + *
{@code 
      *      POTION:STRENGTH:1:EXTENDED:SPLASH:1
+     * }
* @param part String array that contains 6 elements. * @return Potion with given properties. */ @@ -257,13 +261,17 @@ public class ItemParser { /** * This method parses array of 2 to 3 elements that represents player head. * Format: + *
{@code 
      *    PLAYER_HEAD::QTY
      *    PLAYER_HEAD:
      *    PLAYER_HEAD:QTY
+     * }
* Example: + *
{@code 
      *    PLAYER_HEAD:1
      *    PLAYER_HEAD:BONNe1704
      *    PLAYER_HEAD:eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvYWY1ZjE1OTg4NmNjNTMxZmZlYTBkOGFhNWY5MmVkNGU1ZGE2NWY3MjRjMDU3MGFmODZhOTBiZjAwYzY3YzQyZSJ9fX0:1
+     * }
* @param part String array that contains at least 2 elements. * @return Player head with given properties. */ diff --git a/src/main/java/world/bentobox/bentobox/util/Util.java b/src/main/java/world/bentobox/bentobox/util/Util.java index c24d839c6..d76f9c12f 100644 --- a/src/main/java/world/bentobox/bentobox/util/Util.java +++ b/src/main/java/world/bentobox/bentobox/util/Util.java @@ -59,7 +59,7 @@ import world.bentobox.bentobox.nms.WorldRegenerator; */ public class Util { /** - * Use standard color code definition: &. + * Use standard color code definition: {@code &}. */ private static final Pattern HEX_PATTERN = Pattern.compile("&#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})"); private static final String NETHER = "_nether"; From a7034998171b950794e7421298b60e9d63b3d2cc Mon Sep 17 00:00:00 2001 From: tastybento Date: Wed, 8 Feb 2023 19:16:32 -0800 Subject: [PATCH 2/5] Added test class. --- .../events/addon/AddonEnableEventTest.java | 105 ++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 src/test/java/world/bentobox/bentobox/api/events/addon/AddonEnableEventTest.java diff --git a/src/test/java/world/bentobox/bentobox/api/events/addon/AddonEnableEventTest.java b/src/test/java/world/bentobox/bentobox/api/events/addon/AddonEnableEventTest.java new file mode 100644 index 000000000..2d27787bc --- /dev/null +++ b/src/test/java/world/bentobox/bentobox/api/events/addon/AddonEnableEventTest.java @@ -0,0 +1,105 @@ +package world.bentobox.bentobox.api.events.addon; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.powermock.modules.junit4.PowerMockRunner; + +import world.bentobox.bentobox.api.addons.Addon; + +/** + * @author tastybento + * + */ +@RunWith(PowerMockRunner.class) +public class AddonEnableEventTest { + + private AddonEnableEvent aee; + @Mock + private Addon addon; + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + Map map = new HashMap<>(); + aee = new AddonEnableEvent(addon, map); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.events.addon.AddonEnableEvent#getHandlers()}. + */ + @Test + public void testGetHandlers() { + assertNotNull(aee.getHandlers()); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.events.addon.AddonEnableEvent#getHandlerList()}. + */ + @Test + public void testGetHandlerList() { + assertNotNull(AddonEnableEvent.getHandlerList()); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.events.addon.AddonEnableEvent#AddonEnableEvent(world.bentobox.bentobox.api.addons.Addon, java.util.Map)}. + */ + @Test + public void testAddonEnableEvent() { + assertNotNull(aee); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.events.addon.AddonBaseEvent#getKeyValues()}. + */ + @Test + public void testGetKeyValues() { + assertTrue(aee.getKeyValues().isEmpty()); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.events.addon.AddonBaseEvent#getAddon()}. + */ + @Test + public void testGetAddon() { + assertEquals(addon, aee.getAddon()); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.events.addon.AddonBaseEvent#getNewEvent()}. + */ + @Test + public void testGetNewEvent() { + assertTrue(aee.getNewEvent().isEmpty()); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.events.addon.AddonBaseEvent#setNewEvent(world.bentobox.bentobox.api.events.addon.AddonBaseEvent)}. + */ + @Test + public void testSetNewEvent() { + aee.setNewEvent(aee); + assertEquals(aee, aee.getNewEvent().get()); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.events.BentoBoxEvent#setKeyValues(java.util.Map)}. + */ + @Test + @Ignore + public void testSetKeyValues() { + // No fields to set values for in the class + } + +} From 1783bb7058fc1980099fefc801742070c61092b7 Mon Sep 17 00:00:00 2001 From: tastybento Date: Wed, 8 Feb 2023 19:21:50 -0800 Subject: [PATCH 3/5] Fixes bug where pasting was happening twice --- .../bentobox/bentobox/blueprints/BlueprintPaster.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/blueprints/BlueprintPaster.java b/src/main/java/world/bentobox/bentobox/blueprints/BlueprintPaster.java index 0a1f6a854..8536ff294 100644 --- a/src/main/java/world/bentobox/bentobox/blueprints/BlueprintPaster.java +++ b/src/main/java/world/bentobox/bentobox/blueprints/BlueprintPaster.java @@ -84,10 +84,10 @@ public class BlueprintPaster { private final Island island; /** - * Paste a clipboard to a location and run task + * Paste a clipboard to a location. Run {@link #paste()} to paste * @param plugin - BentoBox * @param clipboard - clipboard to paste - * @param location - location to paste to + * @param location - location to which to paste */ public BlueprintPaster(@NonNull BentoBox plugin, @NonNull BlueprintClipboard clipboard, @NonNull Location location) { this.plugin = plugin; @@ -97,9 +97,6 @@ public class BlueprintPaster { this.location = location; this.world = location.getWorld(); this.island = null; - - // Paste - paste(); } /** From a793a3375839282cc5c74de5e8d90cc8d289657d Mon Sep 17 00:00:00 2001 From: tastybento Date: Wed, 8 Feb 2023 19:53:16 -0800 Subject: [PATCH 4/5] Minimal test class for BlueprintPaster --- .../blueprints/BlueprintPasterTest.java | 130 ++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 src/test/java/world/bentobox/bentobox/blueprints/BlueprintPasterTest.java diff --git a/src/test/java/world/bentobox/bentobox/blueprints/BlueprintPasterTest.java b/src/test/java/world/bentobox/bentobox/blueprints/BlueprintPasterTest.java new file mode 100644 index 000000000..f589723f0 --- /dev/null +++ b/src/test/java/world/bentobox/bentobox/blueprints/BlueprintPasterTest.java @@ -0,0 +1,130 @@ +package world.bentobox.bentobox.blueprints; + +import static org.junit.Assert.assertNotNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.when; + +import java.util.UUID; +import java.util.concurrent.CompletableFuture; + +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.util.Vector; +import org.eclipse.jdt.annotation.NonNull; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.reflect.Whitebox; + +import world.bentobox.bentobox.BentoBox; +import world.bentobox.bentobox.Settings; +import world.bentobox.bentobox.api.user.User; +import world.bentobox.bentobox.database.objects.Island; + +/** + * @author tastybento + * + */ +@RunWith(PowerMockRunner.class) +@PrepareForTest({BentoBox.class, User.class, Bukkit.class}) +public class BlueprintPasterTest { + + private BlueprintPaster bp; + private BlueprintPaster bp2; + + @Mock + private BentoBox plugin; + @Mock + private @NonNull Blueprint blueprint; + @Mock + private World world; + @Mock + private @NonNull Island island; + @Mock + private Location location; + @Mock + private @NonNull BlueprintClipboard clipboard; + @Mock + private @NonNull User user; + + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + // Set up plugin + Whitebox.setInternalState(BentoBox.class, "instance", plugin); + + // Scheduler + PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); + + Settings settings = new Settings(); + // Settings + when(plugin.getSettings()).thenReturn(settings); + + // Location + when(location.toVector()).thenReturn(new Vector(1D,2D,3D)); + + // Island + when(island.getProtectionCenter()).thenReturn(location); + when(island.getOwner()).thenReturn(UUID.randomUUID()); + + // Clipboard + when(clipboard.getBlueprint()).thenReturn(blueprint); + + // User + PowerMockito.mockStatic(User.class, Mockito.RETURNS_MOCKS); + when(User.getInstance(any(UUID.class))).thenReturn(user); + + bp = new BlueprintPaster(plugin, blueprint, world, island); + bp2 = new BlueprintPaster(plugin, clipboard, location); + } + + /** + * Test method for {@link world.bentobox.bentobox.blueprints.BlueprintPaster#BlueprintPaster(world.bentobox.bentobox.BentoBox, world.bentobox.bentobox.blueprints.BlueprintClipboard, org.bukkit.Location)}. + */ + @Test + public void testBlueprintPasterBentoBoxBlueprintClipboardLocation() { + assertNotNull(bp2); + } + + /** + * Test method for {@link world.bentobox.bentobox.blueprints.BlueprintPaster#BlueprintPaster(world.bentobox.bentobox.BentoBox, world.bentobox.bentobox.blueprints.Blueprint, org.bukkit.World, world.bentobox.bentobox.database.objects.Island)}. + */ + @Test + public void testBlueprintPasterBentoBoxBlueprintWorldIsland() { + assertNotNull(bp); + } + + /** + * Test method for {@link world.bentobox.bentobox.blueprints.BlueprintPaster#paste()}. + */ + @Test + public void testPaste() { + CompletableFuture result = bp.paste(); + assertNotNull(result); + PowerMockito.verifyStatic(Bukkit.class, times(1)); + Bukkit.getScheduler(); + } + + /** + * Test method for {@link world.bentobox.bentobox.blueprints.BlueprintPaster#paste()}. + */ + @Test + public void testPaste2() { + CompletableFuture result = bp2.paste(); + assertNotNull(result); + PowerMockito.verifyStatic(Bukkit.class, times(1)); + Bukkit.getScheduler(); + } + + +} From 6c1bcdec05c0e2e7666bb164a1db3c090d20589c Mon Sep 17 00:00:00 2001 From: tastybento Date: Wed, 8 Feb 2023 20:23:19 -0800 Subject: [PATCH 5/5] Added test class for IslandChunkDeletionManager --- .../managers/IslandChunkDeletionManager.java | 4 +- .../IslandChunkDeletionManagerTest.java | 100 ++++++++++++++++++ 2 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 src/test/java/world/bentobox/bentobox/managers/IslandChunkDeletionManagerTest.java diff --git a/src/main/java/world/bentobox/bentobox/managers/IslandChunkDeletionManager.java b/src/main/java/world/bentobox/bentobox/managers/IslandChunkDeletionManager.java index 7316cd48d..16fafd89b 100644 --- a/src/main/java/world/bentobox/bentobox/managers/IslandChunkDeletionManager.java +++ b/src/main/java/world/bentobox/bentobox/managers/IslandChunkDeletionManager.java @@ -4,6 +4,8 @@ import java.util.LinkedList; import java.util.Queue; import java.util.concurrent.atomic.AtomicReference; +import org.bukkit.Bukkit; + import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.database.objects.IslandDeletion; import world.bentobox.bentobox.util.DeleteIslandChunks; @@ -24,7 +26,7 @@ public class IslandChunkDeletionManager implements Runnable { this.slowDeletion = plugin.getSettings().isSlowDeletion(); if (slowDeletion) { - plugin.getServer().getScheduler().runTaskTimer(plugin, this, 0L, 20L); + Bukkit.getScheduler().runTaskTimer(plugin, this, 0L, 20L); } } diff --git a/src/test/java/world/bentobox/bentobox/managers/IslandChunkDeletionManagerTest.java b/src/test/java/world/bentobox/bentobox/managers/IslandChunkDeletionManagerTest.java new file mode 100644 index 000000000..341a2966d --- /dev/null +++ b/src/test/java/world/bentobox/bentobox/managers/IslandChunkDeletionManagerTest.java @@ -0,0 +1,100 @@ +package world.bentobox.bentobox.managers; + +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import org.bukkit.Bukkit; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.reflect.Whitebox; + +import world.bentobox.bentobox.BentoBox; +import world.bentobox.bentobox.Settings; +import world.bentobox.bentobox.database.objects.IslandDeletion; +import world.bentobox.bentobox.util.DeleteIslandChunks; + +/** + * @author tastybento + * + */ +@RunWith(PowerMockRunner.class) +@PrepareForTest({BentoBox.class, Bukkit.class, DeleteIslandChunks.class}) +public class IslandChunkDeletionManagerTest { + + @Mock + private BentoBox plugin; + private IslandChunkDeletionManager icdm; + @Mock + private DeleteIslandChunks dic; + @Mock + private IslandWorldManager iwm; + @Mock + private IslandDeletion id; + + private Settings settings; + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + // Set up plugin + Whitebox.setInternalState(BentoBox.class, "instance", plugin); + + // IWM + when(plugin.getIWM()).thenReturn(iwm); + + // Scheduler + PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); + + // DeleteIslandChunks + PowerMockito.whenNew(DeleteIslandChunks.class).withAnyArguments().thenReturn(dic); + + + settings = new Settings(); + settings.setSlowDeletion(true); + // Settings + when(plugin.getSettings()).thenReturn(settings); + + icdm = new IslandChunkDeletionManager(plugin); + + } + + /** + * Test method for {@link world.bentobox.bentobox.managers.IslandChunkDeletionManager#IslandChunkDeletionManager(world.bentobox.bentobox.BentoBox)}. + */ + @Test + public void testIslandChunkDeletionManager() { + PowerMockito.verifyStatic(Bukkit.class, times(1)); + Bukkit.getScheduler(); + } + + /** + * Test method for {@link world.bentobox.bentobox.managers.IslandChunkDeletionManager#run()}. + */ + @Test + public void testRun() { + icdm.add(id); + icdm.run(); + verify(id, times(3)).getWorld(); + } + + /** + * Test method for {@link world.bentobox.bentobox.managers.IslandChunkDeletionManager#add(world.bentobox.bentobox.database.objects.IslandDeletion)}. + */ + @Test + public void testAdd() { + settings.setSlowDeletion(false); + icdm = new IslandChunkDeletionManager(plugin); + icdm.add(id); + verify(id, times(3)).getWorld(); + } + +}