Skip to content

Commit 7ec3db9

Browse files
authored
Merge pull request #2794 from BentoBoxWorld/copilot/sub-pr-2791
Add unit tests for homeTeleportAsync(Island, User, boolean) overload
2 parents 255fe1f + 19d2253 commit 7ec3db9

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed

src/test/java/world/bentobox/bentobox/managers/IslandsManagerTest.java

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,4 +1619,147 @@ public void testsetMaxHomes() {
16191619
im.setMaxHomes(island, 40);
16201620
verify(island).setMaxHomes(eq(40));
16211621
}
1622+
1623+
/**
1624+
* Test method for
1625+
* {@link world.bentobox.bentobox.managers.IslandsManager#homeTeleportAsync(Island, User)}.
1626+
*/
1627+
@Test
1628+
public void testHomeTeleportAsyncIslandUser() throws Exception {
1629+
// Setup
1630+
Island island = mock(Island.class);
1631+
Location homeLoc = mock(Location.class);
1632+
when(island.getHome("")).thenReturn(homeLoc);
1633+
when(island.getWorld()).thenReturn(world);
1634+
when(user.getPlayer()).thenReturn(player);
1635+
when(user.getUniqueId()).thenReturn(uuid);
1636+
1637+
// Mock player methods called by readyPlayer
1638+
when(player.isInsideVehicle()).thenReturn(false);
1639+
1640+
// Mock teleportAsync to return successful future
1641+
CompletableFuture<Boolean> future = CompletableFuture.completedFuture(true);
1642+
mockedUtil.when(() -> Util.teleportAsync(eq(player), eq(homeLoc))).thenReturn(future);
1643+
1644+
// Test
1645+
IslandsManager im = new IslandsManager(plugin);
1646+
CompletableFuture<Void> result = im.homeTeleportAsync(island, user);
1647+
1648+
// Wait for async completion
1649+
result.get();
1650+
1651+
// Verify
1652+
assertNotNull(result);
1653+
verify(user).sendMessage("commands.island.go.teleport");
1654+
verify(island).getHome("");
1655+
}
1656+
1657+
/**
1658+
* Test method for
1659+
* {@link world.bentobox.bentobox.managers.IslandsManager#homeTeleportAsync(Island, User, boolean)}.
1660+
* Test with a default home location.
1661+
*/
1662+
@Test
1663+
public void testHomeTeleportAsyncIslandUserBooleanWithDefaultHome() throws Exception {
1664+
// Setup
1665+
Island island = mock(Island.class);
1666+
Location homeLoc = mock(Location.class);
1667+
when(island.getHome("")).thenReturn(homeLoc);
1668+
when(island.getWorld()).thenReturn(world);
1669+
when(user.getPlayer()).thenReturn(player);
1670+
when(user.getUniqueId()).thenReturn(uuid);
1671+
1672+
// Mock player methods called by readyPlayer
1673+
when(player.isInsideVehicle()).thenReturn(false);
1674+
1675+
// Mock teleportAsync to return successful future
1676+
CompletableFuture<Boolean> future = CompletableFuture.completedFuture(true);
1677+
mockedUtil.when(() -> Util.teleportAsync(eq(player), eq(homeLoc))).thenReturn(future);
1678+
1679+
// Test
1680+
IslandsManager im = new IslandsManager(plugin);
1681+
CompletableFuture<Void> result = im.homeTeleportAsync(island, user, false);
1682+
1683+
// Wait for async completion
1684+
result.get();
1685+
1686+
// Verify
1687+
assertNotNull(result);
1688+
verify(user).sendMessage("commands.island.go.teleport");
1689+
verify(island).getHome("");
1690+
// User should be removed from goingHome after successful teleport
1691+
assertFalse(im.isGoingHome(user));
1692+
}
1693+
1694+
/**
1695+
* Test method for
1696+
* {@link world.bentobox.bentobox.managers.IslandsManager#homeTeleportAsync(Island, User, boolean)}.
1697+
* Test with newIsland parameter set to true.
1698+
*/
1699+
@Test
1700+
public void testHomeTeleportAsyncIslandUserBooleanNewIsland() throws Exception {
1701+
// Setup
1702+
Island island = mock(Island.class);
1703+
Location homeLoc = mock(Location.class);
1704+
when(island.getHome("")).thenReturn(homeLoc);
1705+
when(island.getWorld()).thenReturn(world);
1706+
when(user.getPlayer()).thenReturn(player);
1707+
when(user.getUniqueId()).thenReturn(uuid);
1708+
1709+
// Mock player methods called by readyPlayer
1710+
when(player.isInsideVehicle()).thenReturn(false);
1711+
1712+
// Mock teleportAsync to return successful future
1713+
CompletableFuture<Boolean> future = CompletableFuture.completedFuture(true);
1714+
mockedUtil.when(() -> Util.teleportAsync(eq(player), eq(homeLoc))).thenReturn(future);
1715+
1716+
// Test
1717+
IslandsManager im = new IslandsManager(plugin);
1718+
CompletableFuture<Void> result = im.homeTeleportAsync(island, user, true);
1719+
1720+
// Wait for async completion
1721+
result.get();
1722+
1723+
// Verify
1724+
assertNotNull(result);
1725+
verify(user).sendMessage("commands.island.go.teleport");
1726+
verify(island).getHome("");
1727+
// User should be removed from goingHome after successful teleport
1728+
assertFalse(im.isGoingHome(user));
1729+
}
1730+
1731+
/**
1732+
* Test method for
1733+
* {@link world.bentobox.bentobox.managers.IslandsManager#homeTeleportAsync(Island, User, boolean)}.
1734+
* Test with failed teleport - should not set primary island and remove from goingHome.
1735+
*/
1736+
@Test
1737+
public void testHomeTeleportAsyncIslandUserBooleanFailedTeleport() throws Exception {
1738+
// Setup
1739+
Island island = mock(Island.class);
1740+
Location homeLoc = mock(Location.class);
1741+
when(island.getHome("")).thenReturn(homeLoc);
1742+
when(island.getWorld()).thenReturn(world);
1743+
when(user.getPlayer()).thenReturn(player);
1744+
when(user.getUniqueId()).thenReturn(uuid);
1745+
1746+
// Mock player methods called by readyPlayer
1747+
when(player.isInsideVehicle()).thenReturn(false);
1748+
1749+
// Mock teleportAsync to return failed future
1750+
CompletableFuture<Boolean> future = CompletableFuture.completedFuture(false);
1751+
mockedUtil.when(() -> Util.teleportAsync(eq(player), eq(homeLoc))).thenReturn(future);
1752+
1753+
// Test
1754+
IslandsManager im = new IslandsManager(plugin);
1755+
CompletableFuture<Void> result = im.homeTeleportAsync(island, user, false);
1756+
1757+
// Wait for async completion
1758+
result.get();
1759+
1760+
// Verify user was removed from goingHome after failed teleport
1761+
assertFalse(im.isGoingHome(user));
1762+
verify(user).sendMessage("commands.island.go.teleport");
1763+
verify(island).getHome("");
1764+
}
16221765
}

0 commit comments

Comments
 (0)