Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 1.14.0 #123

Merged
merged 2 commits into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,9 @@
</issueManagement>

<distributionManagement>
<snapshotRepository>
<id>codemc-snapshots</id>
<url>https://repo.codemc.org/repository/maven-snapshots</url>
</snapshotRepository>
<repository>
<id>codemc-releases</id>
<url>https://repo.codemc.org/repository/maven-releases</url>
<id>bentoboxworld</id>
<url>https://repo.codemc.org/repository/bentoboxworld/</url>
</repository>
</distributionManagement>

Expand All @@ -47,14 +43,14 @@
<java.version>17</java.version>
<powermock.version>2.0.9</powermock.version>
<!-- More visible way how to change dependency versions -->
<spigot.version>1.21.1-R0.1-SNAPSHOT</spigot.version>
<bentobox.version>2.5.0-SNAPSHOT</bentobox.version>
<spigot.version>1.21.3-R0.1-SNAPSHOT</spigot.version>
<bentobox.version>2.7.1-SNAPSHOT</bentobox.version>
<!-- Level addon version -->
<level.version>2.9.0</level.version>
<!-- Revision variable removes warning about dynamic version -->
<revision>${build.version}-SNAPSHOT</revision>
<!-- This allows to change between versions and snapshots. -->
<build.version>1.13.0</build.version>
<build.version>1.14.0</build.version>
<build.number>-LOCAL</build.number>
<sonar.projectKey>BentoBoxWorld_Boxed</sonar.projectKey>
<sonar.organization>bentobox-world</sonar.organization>
Expand Down Expand Up @@ -106,6 +102,10 @@
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots</url>
</repository>
<repository>
<id>bentoboxworld</id>
<url>https://repo.codemc.org/repository/bentoboxworld/</url>
</repository>
<repository>
<id>codemc-repo</id>
<url>https://repo.codemc.org/repository/maven-public/</url>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -66,6 +71,8 @@ public class FlyFlagListenerTest {
private Player op;
@Mock
private Island island;
@Mock
private Spigot spigot;

/**
* @throws java.lang.Exception
Expand Down Expand Up @@ -95,17 +102,21 @@ public void setUp() throws Exception {
@NonNull
List<Player> 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);
Expand Down Expand Up @@ -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));
Expand All @@ -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");

}

Expand All @@ -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");
}

/**
Expand All @@ -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<TextComponent> 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<TextComponent> 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);
}

}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -79,6 +85,8 @@ public class FlyListenerTest {
private BukkitScheduler sch;
@Mock
private Settings settings;
@Mock
private Spigot spigot;

/**
*/
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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<TextComponent> 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<TextComponent> 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)}.
*/
Expand Down Expand Up @@ -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");
}

/**
Expand Down
Loading