Fix tests

This commit is contained in:
tastybento 2024-05-26 17:27:41 -07:00
parent 8b0a5a3d0b
commit 60fa60372d
2 changed files with 51 additions and 16 deletions

View File

@ -67,28 +67,29 @@ public class PlayersManager {
*/
@Nullable
public Players getPlayer(UUID uuid){
if (!playerCache.containsKey(uuid)) {
playerCache.put(uuid, addPlayer(uuid));
}
return playerCache.get(uuid);
return playerCache.computeIfAbsent(uuid, this::addPlayer);
}
/**
* Adds a player to the database. If the UUID does not exist, a new player is made
* @param playerUUID - the player's UUID
* Adds a player to the database. If the UUID does not exist, a new player is created.
*
* @param playerUUID the player's UUID, must not be null
* @return the loaded or newly created player
* @throws NullPointerException if playerUUID is null
*/
private Players addPlayer(@NonNull UUID playerUUID) {
Objects.requireNonNull(playerUUID);
// If the player is in the database, load it, otherwise create a new player
Objects.requireNonNull(playerUUID, "Player UUID must not be null");
// If the player exists in the database, load it; otherwise, create and save a new player
if (handler.objectExists(playerUUID.toString())) {
Players player = handler.loadObject(playerUUID.toString());
if (player != null) {
return player;
}
}
Players player = new Players(plugin, playerUUID);
handler.saveObject(player);
return player;
Players newPlayer = new Players(plugin, playerUUID);
handler.saveObjectAsync(newPlayer);
return newPlayer;
}
/**
@ -158,7 +159,7 @@ public class PlayersManager {
return "";
}
getPlayer(playerUUID);
return playerCache.get(playerUUID).getPlayerName();
return Objects.requireNonNullElse(playerCache.get(playerUUID).getPlayerName(), "");
}
/**

View File

@ -195,7 +195,17 @@ public class PlayersManagerTest {
when(olp.getUniqueId()).thenReturn(uuid);
when(olp.getName()).thenReturn("tastybento");
PowerMockito.mockStatic(Bukkit.class);
when(Bukkit.getOfflinePlayer(Mockito.any(UUID.class))).thenReturn(olp);
when(Bukkit.getOfflinePlayer(any(UUID.class))).thenAnswer(invocation -> {
UUID inputUUID = invocation.getArgument(0);
if (inputUUID.equals(uuid)) {
return olp;
} else {
OfflinePlayer differentOlp = mock(OfflinePlayer.class);
when(differentOlp.getUniqueId()).thenReturn(inputUUID);
when(differentOlp.getName()).thenReturn("");
return differentOlp;
}
});
// Player has island to begin with
IslandsManager im = mock(IslandsManager.class);
@ -385,10 +395,34 @@ public class PlayersManagerTest {
* {@link world.bentobox.bentobox.managers.PlayersManager#getName(java.util.UUID)}.
*/
@Test
public void testGetName() {
public void testGetNameNull() throws InstantiationException, IllegalAccessException, InvocationTargetException,
ClassNotFoundException, NoSuchMethodException, IntrospectionException {
// Null UUID
assertTrue(pm.getName(null).isEmpty());
String name = pm.getName(uuid);
assertEquals("tastybento", name);
}
/**
* Test method for
* {@link world.bentobox.bentobox.managers.PlayersManager#getName(java.util.UUID)}.
*/
@Test
public void testGetNameKnown() throws InstantiationException, IllegalAccessException, InvocationTargetException,
ClassNotFoundException, NoSuchMethodException, IntrospectionException {
pm.setPlayerName(user);
// Known UUID
assertEquals("tastybento", pm.getName(uuid));
}
/**
* Test method for
* {@link world.bentobox.bentobox.managers.PlayersManager#getName(java.util.UUID)}.
*/
@Test
public void testGetNameUnknown() throws InstantiationException, IllegalAccessException, InvocationTargetException,
ClassNotFoundException, NoSuchMethodException, IntrospectionException {
// Unknown UUID - nothing in database
when(handler.objectExists(anyString())).thenReturn(false);
assertEquals("", pm.getName(UUID.randomUUID()));
}