From 35b715ff80c110d2c9ef73a5375a3dd57a1c0693 Mon Sep 17 00:00:00 2001 From: tastybento Date: Fri, 25 May 2018 17:58:07 -0700 Subject: [PATCH] Fixed tests. --- .../tastybento/bskyblock/api/user/User.java | 7 +----- .../admin/teams/AdminTeamAddCommand.java | 2 +- .../bskyblock/database/objects/Players.java | 5 ++++- .../admin/teams/AdminTeamAddCommandTest.java | 6 ++++- .../database/objects/PlayersTest.java | 12 ++++++++++ .../listeners/flags/FireListenerTest.java | 6 +++++ .../managers/PlayersManagerTest.java | 22 +++++++++++++++++-- 7 files changed, 49 insertions(+), 11 deletions(-) diff --git a/src/main/java/us/tastybento/bskyblock/api/user/User.java b/src/main/java/us/tastybento/bskyblock/api/user/User.java index 9d7ea7644..ecb48f8b4 100644 --- a/src/main/java/us/tastybento/bskyblock/api/user/User.java +++ b/src/main/java/us/tastybento/bskyblock/api/user/User.java @@ -133,12 +133,7 @@ public class User { } public String getName() { - player = Bukkit.getPlayer(playerUUID); - if (player != null) { - return player.getName(); - } - // Try and get name from database - return plugin.getPlayers().getName(playerUUID); + return player != null ? player.getName() : plugin.getPlayers().getName(playerUUID); } /** diff --git a/src/main/java/us/tastybento/bskyblock/commands/admin/teams/AdminTeamAddCommand.java b/src/main/java/us/tastybento/bskyblock/commands/admin/teams/AdminTeamAddCommand.java index ec34d8353..e37347df3 100644 --- a/src/main/java/us/tastybento/bskyblock/commands/admin/teams/AdminTeamAddCommand.java +++ b/src/main/java/us/tastybento/bskyblock/commands/admin/teams/AdminTeamAddCommand.java @@ -63,7 +63,7 @@ public class AdminTeamAddCommand extends CompositeCommand { // Success User target = User.getInstance(targetUUID); User leader = User.getInstance(leaderUUID); - leader.sendMessage("commands.island.team.invite.accept.name-joined-your-island", "[name]", target.getName()); + leader.sendMessage("commands.island.team.invite.accept.name-joined-your-island", "[name]", getPlugin().getPlayers().getName(targetUUID)); target.sendMessage("commands.island.team.invite.accept.you-joined-island", "[label]", Constants.ISLANDCOMMAND); getIslands().getIsland(world, leaderUUID).addMember(targetUUID); user.sendMessage("general.success"); diff --git a/src/main/java/us/tastybento/bskyblock/database/objects/Players.java b/src/main/java/us/tastybento/bskyblock/database/objects/Players.java index ee32b4c74..006880389 100755 --- a/src/main/java/us/tastybento/bskyblock/database/objects/Players.java +++ b/src/main/java/us/tastybento/bskyblock/database/objects/Players.java @@ -56,7 +56,10 @@ public class Players implements DataObject { locale = ""; kickedList = new HashMap<>(); // Try to get player's name - this.playerName = Bukkit.getOfflinePlayer(uniqueId).getName(); + this.playerName = Bukkit.getServer().getOfflinePlayer(uniqueId).getName(); + if (this.playerName == null) { + this.playerName = uniqueId.toString(); + } } /** diff --git a/src/test/java/us/tastybento/bskyblock/commands/admin/teams/AdminTeamAddCommandTest.java b/src/test/java/us/tastybento/bskyblock/commands/admin/teams/AdminTeamAddCommandTest.java index 463b9574e..53392f66d 100644 --- a/src/test/java/us/tastybento/bskyblock/commands/admin/teams/AdminTeamAddCommandTest.java +++ b/src/test/java/us/tastybento/bskyblock/commands/admin/teams/AdminTeamAddCommandTest.java @@ -276,7 +276,7 @@ public class AdminTeamAddCommandTest { @Test public void testExecuteSuccess() { AdminTeamAddCommand itl = new AdminTeamAddCommand(ac); - String[] name = {"tastybento", "poslovich"}; + String[] name = {"tastybento", "poslovich"}; when(pm.getUUID(Mockito.eq("tastybento"))).thenReturn(uuid); when(pm.getUUID(Mockito.eq("poslovich"))).thenReturn(notUUID); @@ -292,6 +292,10 @@ public class AdminTeamAddCommandTest { Island island = mock(Island.class); when(im.getIsland(Mockito.any(), Mockito.eq(uuid))).thenReturn(island); + // Player name + when(pm.getName(Mockito.eq(uuid))).thenReturn("tastybento"); + when(pm.getName(Mockito.eq(notUUID))).thenReturn("poslovich"); + when(plugin.getPlayers()).thenReturn(pm); // Success assertTrue(itl.execute(user, Arrays.asList(name))); diff --git a/src/test/java/us/tastybento/bskyblock/database/objects/PlayersTest.java b/src/test/java/us/tastybento/bskyblock/database/objects/PlayersTest.java index 58e1b5d22..2a2f7135a 100644 --- a/src/test/java/us/tastybento/bskyblock/database/objects/PlayersTest.java +++ b/src/test/java/us/tastybento/bskyblock/database/objects/PlayersTest.java @@ -11,10 +11,14 @@ import java.util.UUID; import org.bukkit.Bukkit; import org.bukkit.Location; +import org.bukkit.OfflinePlayer; +import org.bukkit.Server; import org.bukkit.World; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +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; @@ -40,6 +44,14 @@ public class PlayersTest { when(s.getResetLimit()).thenReturn(3); when(s.getDeathsMax()).thenReturn(3); when(plugin.getSettings()).thenReturn(s); + + Server server = mock(Server.class); + PowerMockito.mockStatic(Bukkit.class); + when(Bukkit.getServer()).thenReturn(server); + OfflinePlayer olp = mock(OfflinePlayer.class); + when(olp.getName()).thenReturn("tasty"); + when(server.getOfflinePlayer(Mockito.any(UUID.class))).thenReturn(olp); + } private BSkyBlock plugin; diff --git a/src/test/java/us/tastybento/bskyblock/listeners/flags/FireListenerTest.java b/src/test/java/us/tastybento/bskyblock/listeners/flags/FireListenerTest.java index 083bbfdca..4a9029f8c 100644 --- a/src/test/java/us/tastybento/bskyblock/listeners/flags/FireListenerTest.java +++ b/src/test/java/us/tastybento/bskyblock/listeners/flags/FireListenerTest.java @@ -49,6 +49,7 @@ import us.tastybento.bskyblock.managers.FlagsManager; import us.tastybento.bskyblock.managers.IslandWorldManager; import us.tastybento.bskyblock.managers.IslandsManager; import us.tastybento.bskyblock.managers.LocalesManager; +import us.tastybento.bskyblock.managers.PlayersManager; import us.tastybento.bskyblock.util.Util; @RunWith(PowerMockRunner.class) @@ -128,12 +129,17 @@ public class FireListenerTest { ///user.setPlugin(plugin); User.setPlugin(plugin); + // Locales - final LocalesManager lm = mock(LocalesManager.class); when(plugin.getLocalesManager()).thenReturn(lm); when(lm.get(any(), any())).thenReturn("mock translation"); + // Player name + PlayersManager pm = mock(PlayersManager.class); + when(pm.getName(Mockito.any())).thenReturn("tastybento"); + when(plugin.getPlayers()).thenReturn(pm); } @Test diff --git a/src/test/java/us/tastybento/bskyblock/managers/PlayersManagerTest.java b/src/test/java/us/tastybento/bskyblock/managers/PlayersManagerTest.java index 467c76bba..3ac61f54b 100644 --- a/src/test/java/us/tastybento/bskyblock/managers/PlayersManagerTest.java +++ b/src/test/java/us/tastybento/bskyblock/managers/PlayersManagerTest.java @@ -185,7 +185,7 @@ public class PlayersManagerTest { when(olp.getName()).thenReturn("tasty"); when(server.getOfflinePlayer(Mockito.any(UUID.class))).thenReturn(olp); Players player = pm.getPlayer(uuid); - assertEquals(uuid.toString(), player.getPlayerName()); + assertEquals("tasty", player.getPlayerName()); assertEquals(uuid.toString(), player.getUniqueId()); } @@ -358,9 +358,15 @@ public class PlayersManagerTest { @Test public void testSetandGetPlayerName() { PlayersManager pm = new PlayersManager(plugin); + Server server = mock(Server.class); + when(Bukkit.getServer()).thenReturn(server); + OfflinePlayer olp = mock(OfflinePlayer.class); + when(olp.getName()).thenReturn("tasty"); + when(server.getOfflinePlayer(Mockito.any(UUID.class))).thenReturn(olp); + // Add a player pm.addPlayer(uuid); - assertEquals(uuid.toString(), pm.getName(user.getUniqueId())); + assertEquals("tasty", pm.getName(user.getUniqueId())); pm.setPlayerName(user); assertEquals(user.getName(), pm.getName(user.getUniqueId())); } @@ -371,6 +377,12 @@ public class PlayersManagerTest { @Test public void testGetSetResetsLeft() { PlayersManager pm = new PlayersManager(plugin); + Server server = mock(Server.class); + when(Bukkit.getServer()).thenReturn(server); + OfflinePlayer olp = mock(OfflinePlayer.class); + when(olp.getName()).thenReturn("tasty"); + when(server.getOfflinePlayer(Mockito.any(UUID.class))).thenReturn(olp); + // Add a player pm.addPlayer(uuid); assertEquals(0, pm.getResetsLeft(uuid)); @@ -384,6 +396,12 @@ public class PlayersManagerTest { @Test public void testSaveUUID() { PlayersManager pm = new PlayersManager(plugin); + Server server = mock(Server.class); + when(Bukkit.getServer()).thenReturn(server); + OfflinePlayer olp = mock(OfflinePlayer.class); + when(olp.getName()).thenReturn("tasty"); + when(server.getOfflinePlayer(Mockito.any(UUID.class))).thenReturn(olp); + // Add a player pm.addPlayer(uuid); pm.save(uuid);