From e80526c3fe083a43b763c45e1040c151b7ee154b Mon Sep 17 00:00:00 2001 From: tastybento Date: Sat, 9 Nov 2024 19:00:59 -0800 Subject: [PATCH] MC 1.21.3 update and codemc update --- pom.xml | 18 +++---- .../listeners/FlyFlagListenerTest.java | 50 +++++++++++++++++-- .../islandfly/listeners/FlyListenerTest.java | 43 +++++++++++++++- 3 files changed, 95 insertions(+), 16 deletions(-) diff --git a/pom.xml b/pom.xml index 511039b..373a921 100644 --- a/pom.xml +++ b/pom.xml @@ -31,13 +31,9 @@ - - codemc-snapshots - https://repo.codemc.org/repository/maven-snapshots - - codemc-releases - https://repo.codemc.org/repository/maven-releases + bentoboxworld + https://repo.codemc.org/repository/bentoboxworld/ @@ -47,14 +43,14 @@ 17 2.0.9 - 1.21.1-R0.1-SNAPSHOT - 2.5.0-SNAPSHOT + 1.21.3-R0.1-SNAPSHOT + 2.7.1-SNAPSHOT 2.9.0 ${build.version}-SNAPSHOT - 1.13.0 + 1.14.0 -LOCAL BentoBoxWorld_Boxed bentobox-world @@ -106,6 +102,10 @@ spigot-repo https://hub.spigotmc.org/nexus/content/repositories/snapshots + + bentoboxworld + https://repo.codemc.org/repository/bentoboxworld/ + codemc-repo https://repo.codemc.org/repository/maven-public/ diff --git a/src/test/java/world/bentobox/islandfly/listeners/FlyFlagListenerTest.java b/src/test/java/world/bentobox/islandfly/listeners/FlyFlagListenerTest.java index 903c822..4426484 100644 --- a/src/test/java/world/bentobox/islandfly/listeners/FlyFlagListenerTest.java +++ b/src/test/java/world/bentobox/islandfly/listeners/FlyFlagListenerTest.java @@ -1,8 +1,10 @@ package world.bentobox.islandfly.listeners; +import static org.junit.Assert.assertEquals; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.atLeast; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; @@ -15,18 +17,21 @@ import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.entity.Player; +import org.bukkit.entity.Player.Spigot; import org.bukkit.scheduler.BukkitScheduler; import org.eclipse.jdt.annotation.NonNull; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.mockito.stubbing.Answer; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; +import net.md_5.bungee.api.chat.TextComponent; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.events.flags.FlagProtectionChangeEvent; import world.bentobox.bentobox.api.flags.Flag; @@ -66,6 +71,8 @@ public class FlyFlagListenerTest { private Player op; @Mock private Island island; + @Mock + private Spigot spigot; /** * @throws java.lang.Exception @@ -95,17 +102,21 @@ public void setUp() throws Exception { @NonNull List list = new ArrayList<>(); when(p1.getUniqueId()).thenReturn(UUID.randomUUID()); + when(p1.spigot()).thenReturn(spigot); User.getInstance(p1); when(p1.isFlying()).thenReturn(true); when(p2.getUniqueId()).thenReturn(UUID.randomUUID()); + when(p2.spigot()).thenReturn(spigot); User.getInstance(p2); when(p2.isFlying()).thenReturn(true); when(p2.isOnline()).thenReturn(true); when(p2.getLocation()).thenReturn(mock(Location.class)); when(p3.getUniqueId()).thenReturn(UUID.randomUUID()); + when(p3.spigot()).thenReturn(spigot); User.getInstance(p3); when(p3.isFlying()).thenReturn(false); when(op.getUniqueId()).thenReturn(UUID.randomUUID()); + when(op.spigot()).thenReturn(spigot); User.getInstance(op); when(op.isFlying()).thenReturn(true); when(op.isOp()).thenReturn(true); @@ -148,7 +159,7 @@ public void testOnFlagChangeOtherFlag() { public void testOnFlagChange() { ffl.onFlagChange(e); verify(p1, never()).sendMessage(anyString()); - verify(p2).sendMessage(eq("islandfly.fly-turning-off-alert")); + this.checkSpigotMessage("islandfly.fly-turning-off-alert"); verify(p3, never()).sendMessage(anyString()); verify(op, never()).sendMessage(anyString()); verify(scheduler).runTaskLater(eq(plugin), any(Runnable.class), eq(100L)); @@ -162,13 +173,13 @@ public void testOnFlagChangeZeroTime() { when(settings.getFlyTimeout()).thenReturn(0); ffl.onFlagChange(e); verify(p1, never()).sendMessage(anyString()); - verify(p2).sendMessage(eq("islandfly.fly-turning-off-alert")); + this.checkSpigotMessage("islandfly.fly-turning-off-alert"); verify(p3, never()).sendMessage(anyString()); verify(op, never()).sendMessage(anyString()); verify(p2).setFlying(false); verify(p2).setAllowFlight(false); - verify(p2).sendMessage("islandfly.disable-fly"); + checkSpigotMessage("islandfly.disable-fly"); } @@ -179,7 +190,7 @@ public void testOnFlagChangeZeroTime() { public void testDisableAllowedAgain() { when(island.isAllowed(any(), any())).thenReturn(true); ffl.disable(p2, User.getInstance(p2), island); - verify(p2).sendMessage(eq("islandfly.reallowed-fly")); + checkSpigotMessage("islandfly.reallowed-fly"); } /** @@ -189,6 +200,35 @@ public void testDisableAllowedAgain() { public void testDisable() { when(island.isAllowed(any(), any())).thenReturn(false); ffl.disable(p2, User.getInstance(p2), island); - verify(p2).sendMessage(eq("islandfly.disable-fly")); + this.checkSpigotMessage("islandfly.disable-fly"); + } + + /** + * Check that spigot sent the message + * @param message - message to check + */ + public void checkSpigotMessage(String expectedMessage) { + checkSpigotMessage(expectedMessage, 1); + } + + public void checkSpigotMessage(String expectedMessage, int expectedOccurrences) { + // Capture the argument passed to spigot().sendMessage(...) if messages are sent + ArgumentCaptor captor = ArgumentCaptor.forClass(TextComponent.class); + + // Verify that sendMessage() was called at least 0 times (capture any sent messages) + verify(spigot, atLeast(0)).sendMessage(captor.capture()); + + // Get all captured TextComponents + List capturedMessages = captor.getAllValues(); + + // Count the number of occurrences of the expectedMessage in the captured messages + long actualOccurrences = capturedMessages.stream().map(component -> component.toLegacyText()) // Convert each TextComponent to plain text + .filter(messageText -> messageText.contains(expectedMessage)) // Check if the message contains the expected text + .count(); // Count how many times the expected message appears + + // Assert that the number of occurrences matches the expectedOccurrences + assertEquals("Expected message occurrence mismatch: " + expectedMessage, expectedOccurrences, + actualOccurrences); } + } diff --git a/src/test/java/world/bentobox/islandfly/listeners/FlyListenerTest.java b/src/test/java/world/bentobox/islandfly/listeners/FlyListenerTest.java index 849d910..884c7cd 100644 --- a/src/test/java/world/bentobox/islandfly/listeners/FlyListenerTest.java +++ b/src/test/java/world/bentobox/islandfly/listeners/FlyListenerTest.java @@ -1,15 +1,18 @@ package world.bentobox.islandfly.listeners; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; 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.atLeast; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import java.util.List; import java.util.Optional; import java.util.UUID; @@ -18,17 +21,20 @@ import org.bukkit.Location; import org.bukkit.World; import org.bukkit.entity.Player; +import org.bukkit.entity.Player.Spigot; import org.bukkit.scheduler.BukkitScheduler; import org.eclipse.jdt.annotation.Nullable; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; +import net.md_5.bungee.api.chat.TextComponent; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.addons.GameModeAddon; import world.bentobox.bentobox.api.events.island.IslandExitEvent; @@ -79,6 +85,8 @@ public class FlyListenerTest { private BukkitScheduler sch; @Mock private Settings settings; + @Mock + private Spigot spigot; /** */ @@ -102,6 +110,7 @@ public void setUp() { when(p.isOp()).thenReturn(false); when(p.isFlying()).thenReturn(true); when(p.getGameMode()).thenReturn(GameMode.SURVIVAL); + when(p.spigot()).thenReturn(spigot); User.setPlugin(plugin); User.getInstance(p); when(user.getUniqueId()).thenReturn(uuid); @@ -117,6 +126,7 @@ public void setUp() { when(gameMode.getPermissionPrefix()).thenReturn("bskyblock."); when(iwm.getAddon(any())).thenReturn(opGm); when(iwm.getPermissionPrefix(any())).thenReturn("bskyblock."); + when(iwm.getFriendlyName(any())).thenReturn("BSkyBlock"); // Bukkit PowerMockito.mockStatic(Bukkit.class); when(Bukkit.getScheduler()).thenReturn(sch); @@ -153,9 +163,38 @@ public void testOnExitIslandGraceTime() { when(event.getPlayerUUID()).thenReturn(uuid); fl.onExitIsland(event); verify(sch).runTaskLater(eq(plugin), any(Runnable.class), eq(100L)); - verify(p).sendMessage("islandfly.fly-outside-alert"); + checkSpigotMessage("islandfly.fly-outside-alert"); + } + + /** + * Check that spigot sent the message + * @param message - message to check + */ + public void checkSpigotMessage(String expectedMessage) { + checkSpigotMessage(expectedMessage, 1); + } + + public void checkSpigotMessage(String expectedMessage, int expectedOccurrences) { + // Capture the argument passed to spigot().sendMessage(...) if messages are sent + ArgumentCaptor captor = ArgumentCaptor.forClass(TextComponent.class); + + // Verify that sendMessage() was called at least 0 times (capture any sent messages) + verify(spigot, atLeast(0)).sendMessage(captor.capture()); + + // Get all captured TextComponents + List capturedMessages = captor.getAllValues(); + + // Count the number of occurrences of the expectedMessage in the captured messages + long actualOccurrences = capturedMessages.stream().map(component -> component.toLegacyText()) // Convert each TextComponent to plain text + .filter(messageText -> messageText.contains(expectedMessage)) // Check if the message contains the expected text + .count(); // Count how many times the expected message appears + + // Assert that the number of occurrences matches the expectedOccurrences + assertEquals("Expected message occurrence mismatch: " + expectedMessage, expectedOccurrences, + actualOccurrences); } + /** * Test method for {@link world.bentobox.islandfly.listeners.FlyListener#onExitIsland(world.bentobox.bentobox.api.events.island.IslandExitEvent)}. */ @@ -210,7 +249,7 @@ public void testOnExitIslandNoGraceTime() { when(event.getPlayerUUID()).thenReturn(uuid); fl.onExitIsland(event); verify(sch, never()).runTaskLater(eq(plugin), any(Runnable.class), any(Long.class)); - verify(p).sendMessage("islandfly.disable-fly"); + checkSpigotMessage("islandfly.disable-fly"); } /**