Extracted island info from the Island object.

This is in preparation to have a different info for players than admins.

https://github.com/BentoBoxWorld/BentoBox/issues/1501
This commit is contained in:
tastybento 2021-09-04 09:46:28 -07:00
parent 5bb12d53bd
commit 547c266975
11 changed files with 408 additions and 261 deletions

View File

@ -9,6 +9,7 @@ import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.localization.TextVariables; import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.util.IslandInfo;
import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.Util;
public class AdminInfoCommand extends CompositeCommand { public class AdminInfoCommand extends CompositeCommand {
@ -34,7 +35,7 @@ public class AdminInfoCommand extends CompositeCommand {
} }
// If there are no args, then the player wants info on the island at this location // If there are no args, then the player wants info on the island at this location
if (args.isEmpty()) { if (args.isEmpty()) {
if (!getIslands().getIslandAt(user.getLocation()).map(i -> i.showInfo(user)).orElse(false)) { if (!getIslands().getIslandAt(user.getLocation()).map(i -> new IslandInfo(i).showInfo(user)).orElse(false)) {
user.sendMessage("commands.admin.info.no-island"); user.sendMessage("commands.admin.info.no-island");
return false; return false;
} }
@ -49,7 +50,7 @@ public class AdminInfoCommand extends CompositeCommand {
// Show info for this player // Show info for this player
Island island = getIslands().getIsland(getWorld(), targetUUID); Island island = getIslands().getIsland(getWorld(), targetUUID);
if (island != null) { if (island != null) {
island.showInfo(user); new IslandInfo(island).showInfo(user);
if (!getIslands().getQuarantinedIslandByUser(getWorld(), targetUUID).isEmpty()) { if (!getIslands().getQuarantinedIslandByUser(getWorld(), targetUUID).isEmpty()) {
user.sendMessage("commands.admin.info.islands-in-trash"); user.sendMessage("commands.admin.info.islands-in-trash");
} }

View File

@ -7,6 +7,7 @@ import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.localization.TextVariables; import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.util.IslandInfo;
public class AdminTrashCommand extends CompositeCommand { public class AdminTrashCommand extends CompositeCommand {
@ -63,7 +64,7 @@ public class AdminTrashCommand extends CompositeCommand {
user.sendMessage("commands.admin.trash.title"); user.sendMessage("commands.admin.trash.title");
for (int i = 0; i < islands.size(); i++) { for (int i = 0; i < islands.size(); i++) {
user.sendMessage("commands.admin.trash.count", TextVariables.NUMBER, String.valueOf(i+1)); user.sendMessage("commands.admin.trash.count", TextVariables.NUMBER, String.valueOf(i+1));
islands.get(i).showInfo(user); new IslandInfo(islands.get(i)).showInfo(user);
} }
user.sendMessage("commands.admin.trash.use-switch", TextVariables.LABEL, getTopLabel()); user.sendMessage("commands.admin.trash.use-switch", TextVariables.LABEL, getTopLabel());
user.sendMessage("commands.admin.trash.use-emptytrash", TextVariables.LABEL, getTopLabel()); user.sendMessage("commands.admin.trash.use-emptytrash", TextVariables.LABEL, getTopLabel());

View File

@ -10,6 +10,7 @@ import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.RanksManager; import world.bentobox.bentobox.managers.RanksManager;
import world.bentobox.bentobox.util.IslandInfo;
import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.Util;
public class AdminTeamAddCommand extends CompositeCommand { public class AdminTeamAddCommand extends CompositeCommand {
@ -49,7 +50,10 @@ public class AdminTeamAddCommand extends CompositeCommand {
} }
if (getIslands().inTeam(getWorld(), ownerUUID) && !getIslands().getOwner(getWorld(), ownerUUID).equals(ownerUUID)) { if (getIslands().inTeam(getWorld(), ownerUUID) && !getIslands().getOwner(getWorld(), ownerUUID).equals(ownerUUID)) {
user.sendMessage("commands.admin.team.add.name-not-owner", TextVariables.NAME, args.get(0)); user.sendMessage("commands.admin.team.add.name-not-owner", TextVariables.NAME, args.get(0));
getIslands().getIsland(getWorld(), ownerUUID).showMembers(user); Island island = getIslands().getIsland(getWorld(), ownerUUID);
if (island != null) {
new IslandInfo(island).showMembers(user);
}
return false; return false;
} }
if (getIslands().inTeam(getWorld(), targetUUID)) { if (getIslands().inTeam(getWorld(), targetUUID)) {

View File

@ -13,6 +13,7 @@ import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.RanksManager; import world.bentobox.bentobox.managers.RanksManager;
import world.bentobox.bentobox.util.IslandInfo;
import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.Util;
/** /**
@ -59,10 +60,12 @@ public class AdminTeamKickCommand extends CompositeCommand {
@Override @Override
public boolean execute(User user, String label, @NonNull List<String> args) { public boolean execute(User user, String label, @NonNull List<String> args) {
Island island = getIslands().getIsland(getWorld(), targetUUID); Island island = getIslands().getIsland(getWorld(), targetUUID);
if (island == null) {
return false;
}
if (targetUUID.equals(island.getOwner())) { if (targetUUID.equals(island.getOwner())) {
user.sendMessage("commands.admin.team.kick.cannot-kick-owner"); user.sendMessage("commands.admin.team.kick.cannot-kick-owner");
island.showMembers(user); new IslandInfo(island).showMembers(user);
return false; return false;
} }
User target = User.getInstance(targetUUID); User target = User.getInstance(targetUUID);

View File

@ -9,6 +9,7 @@ import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.localization.TextVariables; import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.util.IslandInfo;
import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.Util;
/** /**
@ -37,7 +38,7 @@ public class IslandInfoCommand extends CompositeCommand {
} }
// If there are no args, then the player wants info on the island at this location // If there are no args, then the player wants info on the island at this location
if (args.isEmpty()) { if (args.isEmpty()) {
if (!getIslands().getIslandAt(user.getLocation()).map(i -> i.showInfo(user)).orElse(false)) { if (!getIslands().getIslandAt(user.getLocation()).map(i -> new IslandInfo(i).showInfo(user)).orElse(false)) {
user.sendMessage("commands.admin.info.no-island"); user.sendMessage("commands.admin.info.no-island");
return false; return false;
} }
@ -56,10 +57,10 @@ public class IslandInfoCommand extends CompositeCommand {
return false; return false;
} }
// Show info for this player // Show info for this player
island.showInfo(user); new IslandInfo(island).showInfo(user);
return true; return true;
} }
@Override @Override
public Optional<List<String>> tabComplete(User user, String alias, List<String> args) { public Optional<List<String>> tabComplete(User user, String alias, List<String> args) {
String lastArg = !args.isEmpty() ? args.get(args.size()-1) : ""; String lastArg = !args.isEmpty() ? args.get(args.size()-1) : "";

View File

@ -1,8 +1,6 @@
package world.bentobox.bentobox.database.objects; package world.bentobox.bentobox.database.objects;
import java.io.IOException; import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.EnumMap; import java.util.EnumMap;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
@ -23,7 +21,6 @@ import org.bukkit.World;
import org.bukkit.World.Environment; import org.bukkit.World.Environment;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.util.BoundingBox; import org.bukkit.util.BoundingBox;
import org.bukkit.util.Vector;
import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -37,7 +34,6 @@ import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.configuration.WorldSettings; import world.bentobox.bentobox.api.configuration.WorldSettings;
import world.bentobox.bentobox.api.events.island.IslandEvent; import world.bentobox.bentobox.api.events.island.IslandEvent;
import world.bentobox.bentobox.api.flags.Flag; import world.bentobox.bentobox.api.flags.Flag;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.logs.LogEntry; import world.bentobox.bentobox.api.logs.LogEntry;
import world.bentobox.bentobox.api.metadata.MetaDataAble; import world.bentobox.bentobox.api.metadata.MetaDataAble;
import world.bentobox.bentobox.api.metadata.MetaDataValue; import world.bentobox.bentobox.api.metadata.MetaDataValue;
@ -49,6 +45,7 @@ import world.bentobox.bentobox.database.objects.adapters.LogEntryListAdapter;
import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.managers.IslandWorldManager; import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.RanksManager; import world.bentobox.bentobox.managers.RanksManager;
import world.bentobox.bentobox.util.IslandInfo;
import world.bentobox.bentobox.util.Pair; import world.bentobox.bentobox.util.Pair;
import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.Util;
@ -1080,71 +1077,21 @@ public class Island implements DataObject, MetaDataAble {
* Shows info of this island to this user. * Shows info of this island to this user.
* @param user the User who is requesting it * @param user the User who is requesting it
* @return always true * @return always true
* @deprecated Use {@link IslandInfo#showInfo(User) instead}
*/ */
@Deprecated
public boolean showInfo(User user) { public boolean showInfo(User user) {
BentoBox plugin = BentoBox.getInstance(); return new IslandInfo(this).showInfo(user);
user.sendMessage("commands.admin.info.title");
user.sendMessage("commands.admin.info.island-uuid", "[uuid]", this.getUniqueId());
if (getOwner() == null) {
user.sendMessage("commands.admin.info.unowned");
} else {
user.sendMessage("commands.admin.info.owner", "[owner]", plugin.getPlayers().getName(getOwner()), "[uuid]", getOwner().toString());
// Fixes #getLastPlayed() returning 0 when it is the owner's first connection.
long lastPlayed = (Bukkit.getServer().getOfflinePlayer(getOwner()).getLastPlayed() != 0) ?
Bukkit.getServer().getOfflinePlayer(getOwner()).getLastPlayed() : Bukkit.getServer().getOfflinePlayer(getOwner()).getFirstPlayed();
String formattedDate;
try {
String dateTimeFormat = plugin.getLocalesManager().get("commands.admin.info.last-login-date-time-format");
formattedDate = new SimpleDateFormat(dateTimeFormat).format(new Date(lastPlayed));
} catch (NullPointerException | IllegalArgumentException ignored) {
formattedDate = new Date(lastPlayed).toString();
}
user.sendMessage("commands.admin.info.last-login","[date]", formattedDate);
user.sendMessage("commands.admin.info.deaths", "[number]", String.valueOf(plugin.getPlayers().getDeaths(getWorld(), getOwner())));
String resets = String.valueOf(plugin.getPlayers().getResets(getWorld(), getOwner()));
String total = plugin.getIWM().getResetLimit(getWorld()) < 0 ? "Unlimited" : String.valueOf(plugin.getIWM().getResetLimit(getWorld()));
user.sendMessage("commands.admin.info.resets-left", "[number]", resets, "[total]", total);
// Show team members
showMembers(user);
}
Vector location = getProtectionCenter().toVector();
user.sendMessage("commands.admin.info.island-protection-center", TextVariables.XYZ, Util.xyz(location));
user.sendMessage("commands.admin.info.island-center", TextVariables.XYZ, Util.xyz(getCenter().toVector()));
user.sendMessage("commands.admin.info.island-coords", "[xz1]", Util.xyz(new Vector(this.getMinX(), 0, getMinZ())), "[xz2]", Util.xyz(new Vector(this.getMaxX(), 0, getMaxZ())));
user.sendMessage("commands.admin.info.protection-range", "[range]", String.valueOf(getProtectionRange()));
user.sendMessage("commands.admin.info.max-protection-range", "[range]", String.valueOf(getMaxEverProtectionRange()));
user.sendMessage("commands.admin.info.protection-coords", "[xz1]", Util.xyz(new Vector(this.getMinProtectedX(), 0, getMinProtectedZ())), "[xz2]", Util.xyz(new Vector(this.getMaxProtectedX(), 0, getMaxProtectedZ())));
if (spawn) {
user.sendMessage("commands.admin.info.is-spawn");
}
if (!getBanned().isEmpty()) {
user.sendMessage("commands.admin.info.banned-players");
getBanned().forEach(u -> user.sendMessage("commands.admin.info.banned-format", TextVariables.NAME, plugin.getPlayers().getName(u)));
}
if (getPurgeProtected()) {
user.sendMessage("commands.admin.info.purge-protected");
}
return true;
} }
/** /**
* Shows the members of this island to this user. * Shows the members of this island to this user.
* @param user the User who is requesting it * @param user the User who is requesting it
* @deprecated Use {@link IslandInfo#showMembers(User) instead}
*/ */
@Deprecated
public void showMembers(User user) { public void showMembers(User user) {
BentoBox plugin = BentoBox.getInstance(); new IslandInfo(this).showMembers(user);
user.sendMessage("commands.admin.info.team-members-title");
members.forEach((u, i) -> {
if (owner.equals(u)) {
user.sendMessage("commands.admin.info.team-owner-format", TextVariables.NAME, plugin.getPlayers().getName(u)
, "[rank]", user.getTranslation(plugin.getRanksManager().getRank(i)));
} else if (i > RanksManager.VISITOR_RANK){
user.sendMessage("commands.admin.info.team-member-format", TextVariables.NAME, plugin.getPlayers().getName(u)
, "[rank]", user.getTranslation(plugin.getRanksManager().getRank(i)));
}
});
} }
/** /**

View File

@ -0,0 +1,109 @@
package world.bentobox.bentobox.util;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.util.Vector;
import org.eclipse.jdt.annotation.Nullable;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.RanksManager;
/**
* @author tastybento
* @since 1.17.3
*/
public class IslandInfo {
private final BentoBox plugin;
private final Island island;
private final @Nullable UUID owner;
private final World world;
/**
* @param plugin
* @param island
*/
public IslandInfo(Island island) {
this.plugin = BentoBox.getInstance();
this.island = island;
this.owner = island.getOwner();
this.world = island.getWorld();
}
/**
* Shows info of this island to this user.
* @param user the User who is requesting it
* @return always true
*/
public boolean showInfo(User user) {
user.sendMessage("commands.admin.info.title");
user.sendMessage("commands.admin.info.island-uuid", "[uuid]", island.getUniqueId());
if (owner == null) {
user.sendMessage("commands.admin.info.unowned");
} else {
user.sendMessage("commands.admin.info.owner", "[owner]", plugin.getPlayers().getName(owner), "[uuid]", owner.toString());
// Fixes #getLastPlayed() returning 0 when it is the owner's first connection.
long lastPlayed = (Bukkit.getOfflinePlayer(owner).getLastPlayed() != 0) ?
Bukkit.getOfflinePlayer(owner).getLastPlayed() : Bukkit.getOfflinePlayer(owner).getFirstPlayed();
String formattedDate;
try {
String dateTimeFormat = plugin.getLocalesManager().get("commands.admin.info.last-login-date-time-format");
formattedDate = new SimpleDateFormat(dateTimeFormat).format(new Date(lastPlayed));
} catch (NullPointerException | IllegalArgumentException ignored) {
formattedDate = new Date(lastPlayed).toString();
}
user.sendMessage("commands.admin.info.last-login","[date]", formattedDate);
user.sendMessage("commands.admin.info.deaths", "[number]", String.valueOf(plugin.getPlayers().getDeaths(world, owner)));
String resets = String.valueOf(plugin.getPlayers().getResets(world, owner));
String total = plugin.getIWM().getResetLimit(world) < 0 ? "Unlimited" : String.valueOf(plugin.getIWM().getResetLimit(world));
user.sendMessage("commands.admin.info.resets-left", "[number]", resets, "[total]", total);
// Show team members
showMembers(user);
}
Vector location = island.getProtectionCenter().toVector();
user.sendMessage("commands.admin.info.island-protection-center", TextVariables.XYZ, Util.xyz(location));
user.sendMessage("commands.admin.info.island-center", TextVariables.XYZ, Util.xyz(island.getCenter().toVector()));
user.sendMessage("commands.admin.info.island-coords", "[xz1]", Util.xyz(new Vector(island.getMinX(), 0, island.getMinZ())), "[xz2]", Util.xyz(new Vector(island.getMaxX(), 0, island.getMaxZ())));
user.sendMessage("commands.admin.info.protection-range", "[range]", String.valueOf(island.getProtectionRange()));
user.sendMessage("commands.admin.info.max-protection-range", "[range]", String.valueOf(island.getMaxEverProtectionRange()));
user.sendMessage("commands.admin.info.protection-coords", "[xz1]", Util.xyz(new Vector(island.getMinProtectedX(), 0, island.getMinProtectedZ())), "[xz2]", Util.xyz(new Vector(island.getMaxProtectedX(), 0, island.getMaxProtectedZ())));
if (island.isSpawn()) {
user.sendMessage("commands.admin.info.is-spawn");
}
if (!island.getBanned().isEmpty()) {
user.sendMessage("commands.admin.info.banned-players");
island.getBanned().forEach(u -> user.sendMessage("commands.admin.info.banned-format", TextVariables.NAME, plugin.getPlayers().getName(u)));
}
if (island.getPurgeProtected()) {
user.sendMessage("commands.admin.info.purge-protected");
}
return true;
}
/**
* Shows the members of this island to this user.
* @param user the User who is requesting it
*/
public void showMembers(User user) {
user.sendMessage("commands.admin.info.team-members-title");
island.getMembers().forEach((u, i) -> {
if (owner.equals(u)) {
user.sendMessage("commands.admin.info.team-owner-format", TextVariables.NAME, plugin.getPlayers().getName(u)
, "[rank]", user.getTranslation(plugin.getRanksManager().getRank(i)));
} else if (i > RanksManager.VISITOR_RANK){
user.sendMessage("commands.admin.info.team-member-format", TextVariables.NAME, plugin.getPlayers().getName(u)
, "[rank]", user.getTranslation(plugin.getRanksManager().getRank(i)));
}
});
}
}

View File

@ -1,27 +1,35 @@
package world.bentobox.bentobox.api.commands.admin; package world.bentobox.bentobox.api.commands.admin;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; 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.mock; 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 static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.Collections;
import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.UUID; import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.util.Vector;
import org.eclipse.jdt.annotation.NonNull;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.stubbing.Answer;
import org.powermock.api.mockito.PowerMockito; import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.modules.junit4.PowerMockRunner;
@ -35,7 +43,9 @@ import world.bentobox.bentobox.managers.CommandsManager;
import world.bentobox.bentobox.managers.IslandWorldManager; import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.managers.LocalesManager; import world.bentobox.bentobox.managers.LocalesManager;
import world.bentobox.bentobox.managers.PlaceholdersManager;
import world.bentobox.bentobox.managers.PlayersManager; import world.bentobox.bentobox.managers.PlayersManager;
import world.bentobox.bentobox.managers.RanksManager;
import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.Util;
/** /**
@ -43,16 +53,32 @@ import world.bentobox.bentobox.util.Util;
* *
*/ */
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest({Bukkit.class, BentoBox.class, User.class }) @PrepareForTest({Bukkit.class, BentoBox.class, Util.class})
public class AdminInfoCommandTest { public class AdminInfoCommandTest {
private BentoBox plugin; @Mock
private CompositeCommand ac; private CompositeCommand ic;
private UUID uuid; @Mock
private User user; private User user;
@Mock
private IslandsManager im; private IslandsManager im;
@Mock
private PlayersManager pm; private PlayersManager pm;
private UUID notUUID;
private Island island;
private AdminInfoCommand iic;
@Mock
private Player player;
@Mock
private World world;
@Mock
private PlaceholdersManager phm;
@Mock
private @NonNull Location location;
@Mock
private IslandWorldManager iwm;
/** /**
* @throws java.lang.Exception * @throws java.lang.Exception
@ -60,69 +86,62 @@ public class AdminInfoCommandTest {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
// Set up plugin // Set up plugin
plugin = mock(BentoBox.class); BentoBox plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin); Whitebox.setInternalState(BentoBox.class, "instance", plugin);
Util.setPlugin(plugin); // IWM
when(plugin.getIWM()).thenReturn(iwm);
when(plugin.getRanksManager()).thenReturn(new RanksManager());
// Bukkit
PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS);
// Command manager // Command manager
CommandsManager cm = mock(CommandsManager.class); CommandsManager cm = mock(CommandsManager.class);
when(plugin.getCommandsManager()).thenReturn(cm); when(plugin.getCommandsManager()).thenReturn(cm);
// Player // Player
Player p = mock(Player.class); when(player.isOp()).thenReturn(false);
// Sometimes use Mockito.withSettings().verboseLogging() UUID uuid = UUID.randomUUID();
user = mock(User.class);
when(user.isOp()).thenReturn(false);
uuid = UUID.randomUUID();
notUUID = UUID.randomUUID();
while(notUUID.equals(uuid)) {
notUUID = UUID.randomUUID();
}
when(user.getUniqueId()).thenReturn(uuid); when(user.getUniqueId()).thenReturn(uuid);
when(user.getPlayer()).thenReturn(p);
when(user.getName()).thenReturn("tastybento"); when(user.getName()).thenReturn("tastybento");
when(user.getWorld()).thenReturn(world);
when(user.getPlayer()).thenReturn(player);
when(user.isPlayer()).thenReturn(true);
//user = User.getInstance(player);
// Set the User class plugin as this one
User.setPlugin(plugin); User.setPlugin(plugin);
// Parent command has no aliases
ac = mock(CompositeCommand.class);
when(ac.getSubCommandAliases()).thenReturn(new HashMap<>());
// Island World Manager
IslandWorldManager iwm = mock(IslandWorldManager.class);
when(iwm.getFriendlyName(Mockito.any())).thenReturn("BSkyBlock");
when(plugin.getIWM()).thenReturn(iwm);
// Player has island to begin with
im = mock(IslandsManager.class);
when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true);
when(im.hasIsland(Mockito.any(), Mockito.any(User.class))).thenReturn(true);
when(im.isOwner(Mockito.any(),Mockito.any())).thenReturn(true);
when(im.getOwner(Mockito.any(),Mockito.any())).thenReturn(uuid);
when(plugin.getIslands()).thenReturn(im);
// Has team
pm = mock(PlayersManager.class);
when(im.inTeam(Mockito.any(), Mockito.eq(uuid))).thenReturn(true);
when(plugin.getPlayers()).thenReturn(pm);
// Server & Scheduler
BukkitScheduler sch = mock(BukkitScheduler.class);
PowerMockito.mockStatic(Bukkit.class);
when(Bukkit.getScheduler()).thenReturn(sch);
// Locales // Locales
LocalesManager lm = mock(LocalesManager.class); LocalesManager lm = mock(LocalesManager.class);
when(lm.get(Mockito.any(), Mockito.any())).thenReturn("mock translation"); when(lm.get(any(), any())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(1, String.class));
when(plugin.getLocalesManager()).thenReturn(lm); when(plugin.getLocalesManager()).thenReturn(lm);
// Return the same string
when(phm.replacePlaceholders(any(), anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(1, String.class));
when(plugin.getPlaceholdersManager()).thenReturn(phm);
// Translate
when(user.getTranslation(anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(0, String.class));
when(user.getTranslation(anyString(), anyString(), anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(0, String.class));
// Addon // Island manager
when(iwm.getAddon(Mockito.any())).thenReturn(Optional.empty()); island = new Island(location, uuid, 100);
when(location.toVector()).thenReturn(new Vector(1,2,3));
when(plugin.getIslands()).thenReturn(im);
Optional<Island> optionalIsland = Optional.of(island);
when(im.getIslandAt(any())).thenReturn(optionalIsland);
when(im.getIsland(any(), any(UUID.class))).thenReturn(island);
// Players manager
when(plugin.getPlayers()).thenReturn(pm);
when(pm.getUUID(any())).thenReturn(uuid);
// Command
iic = new AdminInfoCommand(ic);
} }
/**
* @throws java.lang.Exception
*/
@After @After
public void tearDown() { public void tearDown() {
User.clearUsers(); User.clearUsers();
@ -130,93 +149,111 @@ public class AdminInfoCommandTest {
} }
/** /**
* Test method for {@link AdminInfoCommand#execute(User, String, List)}. * Test method for {@link world.bentobox.bentobox.api.commands.island.AdminInfoCommand#setup()}.
*/ */
@Test @Test
public void testExecuteNoTargetConsole() { public void testSetup() {
AdminInfoCommand itl = new AdminInfoCommand(ac); assertEquals("mod.info", iic.getPermission());
CommandSender sender = mock(CommandSender.class); assertFalse(iic.isOnlyPlayer());
User console = User.getInstance(sender); assertEquals("commands.admin.info.parameters", iic.getParameters());
assertFalse(itl.execute(console, itl.getLabel(), new ArrayList<>())); assertEquals("commands.admin.info.description", iic.getDescription());
// Show help
} }
/** /**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.AdminInfoCommand#execute(User, String, List)} . * Test method for {@link world.bentobox.bentobox.api.commands.island.AdminInfoCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/ */
@Test @Test
public void testExecuteUnknownPlayer() { public void testExecuteUserStringListOfStringTooManyArgs() {
AdminInfoCommand itl = new AdminInfoCommand(ac); assertFalse(iic.execute(user, "", Arrays.asList("hdhh", "hdhdhd")));
String[] name = {"tastybento"}; verify(user).sendMessage("commands.help.header", "[label]", "commands.help.console");
when(pm.getUUID(Mockito.any())).thenReturn(null);
assertFalse(itl.execute(user, itl.getLabel(), Arrays.asList(name)));
Mockito.verify(user).sendMessage("general.errors.unknown-player", "[name]", name[0]);
} }
/** /**
* Test method for . * Test method for {@link world.bentobox.bentobox.api.commands.island.AdminInfoCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/ */
@Test @Test
public void testExecutePlayerHasNoIsland() { public void testExecuteUserStringListOfStringNoArgsConsole() {
AdminInfoCommand itl = new AdminInfoCommand(ac); CommandSender console = mock(CommandSender.class);
String[] name = {"tastybento"}; User sender = User.getInstance(console);
when(pm.getUUID(Mockito.any())).thenReturn(notUUID); assertFalse(iic.execute(sender, "", Collections.emptyList()));
when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(false); verify(user, never()).sendMessage("commands.help.header", "[label]", "commands.help.console");
when(im.inTeam(Mockito.any(), Mockito.any())).thenReturn(false);
when(im.getIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(null);
assertFalse(itl.execute(user, itl.getLabel(), Arrays.asList(name)));
Mockito.verify(user).sendMessage(Mockito.eq("general.errors.player-has-no-island"));
} }
/** /**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.AdminInfoCommand#execute(User, String, List)}. * Test method for {@link world.bentobox.bentobox.api.commands.island.AdminInfoCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/ */
@Test @Test
public void testExecuteSuccess() { public void testExecuteUserStringListOfStringNoArgsNoIsland() {
AdminInfoCommand itl = new AdminInfoCommand(ac); when(im.getIslandAt(any())).thenReturn(Optional.empty());
String[] name = {"tastybento"}; assertFalse(iic.execute(user, "", Collections.emptyList()));
when(pm.getUUID(Mockito.any())).thenReturn(notUUID); verify(user).sendMessage("commands.admin.info.no-island");
when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true);
Island is = mock(Island.class);
when(im.getIsland(Mockito.any(), Mockito.eq(notUUID))).thenReturn(is);
assertTrue(itl.execute(user, itl.getLabel(), Arrays.asList(name)));
Mockito.verify(is).showInfo(Mockito.eq(user));
} }
/** /**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.AdminInfoCommand#execute(User, String, List)}. * Test method for {@link world.bentobox.bentobox.api.commands.island.AdminInfoCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/ */
@Test @Test
public void testExecuteUserNotOnIsland() { public void testExecuteUserStringListOfStringNoArgsSuccess() {
when(user.isPlayer()).thenReturn(true); assertTrue(iic.execute(user, "", Collections.emptyList()));
AdminInfoCommand itl = new AdminInfoCommand(ac); verify(user).sendMessage("commands.admin.info.title");
// No island here verify(user).sendMessage(eq("commands.admin.info.island-uuid"), eq("[uuid]"), any());
when(im.getIslandAt(Mockito.any())).thenReturn(Optional.empty()); verify(user).sendMessage(eq("commands.admin.info.owner"), eq("[owner]"), eq(null), eq("[uuid]"), any());
assertFalse(itl.execute(user, itl.getLabel(), new ArrayList<>())); verify(user).sendMessage(eq("commands.admin.info.last-login"), eq("[date]"), any());
// Confirm other verifications verify(user).sendMessage("commands.admin.info.deaths", "[number]", "0");
Mockito.verify(user).sendMessage("commands.admin.info.no-island"); verify(user).sendMessage("commands.admin.info.resets-left", "[number]", "0", "[total]", "0");
verify(user).sendMessage("commands.admin.info.team-members-title");
verify(user).sendMessage("commands.admin.info.team-owner-format", "[name]", null, "[rank]", "ranks.owner");
verify(user).sendMessage("commands.admin.info.island-protection-center", "[xyz]", "0,0,0");
verify(user).sendMessage("commands.admin.info.island-center", "[xyz]", "0,0,0");
verify(user).sendMessage("commands.admin.info.island-coords", "[xz1]", "0,0,0", "[xz2]", "0,0,0");
verify(user).sendMessage("commands.admin.info.protection-range", "[range]", "100");
verify(user).sendMessage("commands.admin.info.max-protection-range", "[range]", "100");
verify(user).sendMessage("commands.admin.info.protection-coords", "[xz1]", "0,0,0", "[xz2]", "0,0,0");
} }
/** /**
* Test method for {@link world.bentobox.bentobox.api.commands.admin.AdminInfoCommand#execute(User, String, List)}. * Test method for {@link world.bentobox.bentobox.api.commands.island.AdminInfoCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/ */
@Test @Test
public void testExecuteSuccessUserOnIsland() { public void testExecuteUserStringListOfStringArgsSuccess() {
when(user.isPlayer()).thenReturn(true); assertTrue(iic.execute(user, "", Collections.singletonList("tastybento")));
AdminInfoCommand itl = new AdminInfoCommand(ac); verify(user).sendMessage("commands.admin.info.title");
Location loc = mock(Location.class); verify(user).sendMessage(eq("commands.admin.info.island-uuid"), eq("[uuid]"), any());
verify(user).sendMessage(eq("commands.admin.info.owner"), eq("[owner]"), eq(null), eq("[uuid]"), any());
verify(user).sendMessage(eq("commands.admin.info.last-login"), eq("[date]"), any());
verify(user).sendMessage("commands.admin.info.deaths", "[number]", "0");
verify(user).sendMessage("commands.admin.info.resets-left", "[number]", "0", "[total]", "0");
verify(user).sendMessage("commands.admin.info.team-members-title");
verify(user).sendMessage("commands.admin.info.team-owner-format", "[name]", null, "[rank]", "ranks.owner");
verify(user).sendMessage("commands.admin.info.island-protection-center", "[xyz]", "0,0,0");
verify(user).sendMessage("commands.admin.info.island-center", "[xyz]", "0,0,0");
verify(user).sendMessage("commands.admin.info.island-coords", "[xz1]", "0,0,0", "[xz2]", "0,0,0");
verify(user).sendMessage("commands.admin.info.protection-range", "[range]", "100");
verify(user).sendMessage("commands.admin.info.max-protection-range", "[range]", "100");
verify(user).sendMessage("commands.admin.info.protection-coords", "[xz1]", "0,0,0", "[xz2]", "0,0,0");
// Island has owner
Island is = mock(Island.class);
when(is.getOwner()).thenReturn(uuid);
when(is.showInfo(Mockito.any())).thenReturn(true);
Optional<Island> opi = Optional.of(is);
when(im.getIslandAt(Mockito.any())).thenReturn(opi);
when(user.getLocation()).thenReturn(loc);
assertTrue(itl.execute(user, itl.getLabel(), new ArrayList<>()));
// Confirm other verifications
Mockito.verify(is).showInfo(Mockito.eq(user));
} }
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.island.AdminInfoCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringArgsNoIsland() {
when(im.getIsland(any(), any(UUID.class))).thenReturn(null);
assertFalse(iic.execute(user, "", Collections.singletonList("tastybento")));
verify(user).sendMessage("general.errors.player-has-no-island");
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.island.AdminInfoCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringArgsUnknownPlayer() {
PowerMockito.mockStatic(Util.class);
when(Util.getUUID(any())).thenReturn(null);
assertFalse(iic.execute(user, "", Collections.singletonList("tastybento")));
verify(user).sendMessage("general.errors.unknown-player", "[name]", "tastybento");
}
}

View File

@ -2,7 +2,10 @@ package world.bentobox.bentobox.api.commands.admin.team;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import java.util.ArrayList; import java.util.ArrayList;
@ -90,15 +93,15 @@ public class AdminTeamAddCommandTest {
// Player has island to begin with // Player has island to begin with
im = mock(IslandsManager.class); im = mock(IslandsManager.class);
when(im.hasIsland(Mockito.any(), Mockito.any(User.class))).thenReturn(true); when(im.hasIsland(any(), any(User.class))).thenReturn(true);
when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true); when(im.hasIsland(any(), any(UUID.class))).thenReturn(true);
when(im.isOwner(Mockito.any(), Mockito.any())).thenReturn(true); when(im.isOwner(any(), any())).thenReturn(true);
when(im.getOwner(Mockito.any(), Mockito.any())).thenReturn(uuid); when(im.getOwner(any(), any())).thenReturn(uuid);
when(plugin.getIslands()).thenReturn(im); when(plugin.getIslands()).thenReturn(im);
// Has team // Has team
pm = mock(PlayersManager.class); pm = mock(PlayersManager.class);
when(im.inTeam(Mockito.any(), Mockito.eq(uuid))).thenReturn(true); when(im.inTeam(any(), eq(uuid))).thenReturn(true);
when(plugin.getPlayers()).thenReturn(pm); when(plugin.getPlayers()).thenReturn(pm);
@ -112,16 +115,16 @@ public class AdminTeamAddCommandTest {
// Locales // Locales
LocalesManager lm = mock(LocalesManager.class); LocalesManager lm = mock(LocalesManager.class);
when(lm.get(Mockito.any(), Mockito.any())).thenReturn("mock translation"); when(lm.get(any(), any())).thenReturn("mock translation");
when(plugin.getLocalesManager()).thenReturn(lm); when(plugin.getLocalesManager()).thenReturn(lm);
// Island World Manager // Island World Manager
IslandWorldManager iwm = mock(IslandWorldManager.class); IslandWorldManager iwm = mock(IslandWorldManager.class);
when(iwm.getFriendlyName(Mockito.any())).thenReturn("BSkyBlock"); when(iwm.getFriendlyName(any())).thenReturn("BSkyBlock");
when(plugin.getIWM()).thenReturn(iwm); when(plugin.getIWM()).thenReturn(iwm);
// Addon // Addon
when(iwm.getAddon(Mockito.any())).thenReturn(Optional.empty()); when(iwm.getAddon(any())).thenReturn(Optional.empty());
} }
@ -158,16 +161,16 @@ public class AdminTeamAddCommandTest {
String[] name = {"tastybento", "poslovich"}; String[] name = {"tastybento", "poslovich"};
// Unknown owner // Unknown owner
when(pm.getUUID(Mockito.eq("tastybento"))).thenReturn(null); when(pm.getUUID(eq("tastybento"))).thenReturn(null);
when(pm.getUUID(Mockito.eq("poslovich"))).thenReturn(notUUID); when(pm.getUUID(eq("poslovich"))).thenReturn(notUUID);
assertFalse(itl.execute(user, ac.getLabel(), Arrays.asList(name))); assertFalse(itl.execute(user, ac.getLabel(), Arrays.asList(name)));
Mockito.verify(user).sendMessage("general.errors.unknown-player", "[name]", "tastybento"); verify(user).sendMessage("general.errors.unknown-player", "[name]", "tastybento");
// Unknown target // Unknown target
when(pm.getUUID(Mockito.eq("tastybento"))).thenReturn(uuid); when(pm.getUUID(eq("tastybento"))).thenReturn(uuid);
when(pm.getUUID(Mockito.eq("poslovich"))).thenReturn(null); when(pm.getUUID(eq("poslovich"))).thenReturn(null);
assertFalse(itl.execute(user, ac.getLabel(), Arrays.asList(name))); assertFalse(itl.execute(user, ac.getLabel(), Arrays.asList(name)));
Mockito.verify(user).sendMessage("general.errors.unknown-player", "[name]", "poslovich"); verify(user).sendMessage("general.errors.unknown-player", "[name]", "poslovich");
} }
/** /**
@ -178,13 +181,13 @@ public class AdminTeamAddCommandTest {
AdminTeamAddCommand itl = new AdminTeamAddCommand(ac); AdminTeamAddCommand itl = new AdminTeamAddCommand(ac);
String[] name = {"tastybento", "poslovich"}; String[] name = {"tastybento", "poslovich"};
when(pm.getUUID(Mockito.eq("tastybento"))).thenReturn(uuid); when(pm.getUUID(eq("tastybento"))).thenReturn(uuid);
when(pm.getUUID(Mockito.eq("poslovich"))).thenReturn(notUUID); when(pm.getUUID(eq("poslovich"))).thenReturn(notUUID);
when(im.inTeam(Mockito.any(), Mockito.eq(notUUID))).thenReturn(true); when(im.inTeam(any(), eq(notUUID))).thenReturn(true);
assertFalse(itl.execute(user, itl.getLabel(), Arrays.asList(name))); assertFalse(itl.execute(user, itl.getLabel(), Arrays.asList(name)));
Mockito.verify(user).sendMessage(Mockito.eq("commands.island.team.invite.errors.already-on-team")); verify(user).sendMessage(eq("commands.island.team.invite.errors.already-on-team"));
} }
@ -196,14 +199,14 @@ public class AdminTeamAddCommandTest {
AdminTeamAddCommand itl = new AdminTeamAddCommand(ac); AdminTeamAddCommand itl = new AdminTeamAddCommand(ac);
String[] name = {"tastybento", "poslovich"}; String[] name = {"tastybento", "poslovich"};
when(pm.getUUID(Mockito.eq("tastybento"))).thenReturn(uuid); when(pm.getUUID(eq("tastybento"))).thenReturn(uuid);
when(pm.getUUID(Mockito.eq("poslovich"))).thenReturn(notUUID); when(pm.getUUID(eq("poslovich"))).thenReturn(notUUID);
// No island, // No island,
when(im.hasIsland(Mockito.any(), Mockito.eq(uuid))).thenReturn(false); when(im.hasIsland(any(), eq(uuid))).thenReturn(false);
assertFalse(itl.execute(user, itl.getLabel(), Arrays.asList(name))); assertFalse(itl.execute(user, itl.getLabel(), Arrays.asList(name)));
Mockito.verify(user).sendMessage("general.errors.player-has-no-island"); verify(user).sendMessage("general.errors.player-has-no-island");
} }
@ -215,21 +218,21 @@ public class AdminTeamAddCommandTest {
AdminTeamAddCommand itl = new AdminTeamAddCommand(ac); AdminTeamAddCommand itl = new AdminTeamAddCommand(ac);
String[] name = {"tastybento", "poslovich"}; String[] name = {"tastybento", "poslovich"};
when(pm.getUUID(Mockito.eq("tastybento"))).thenReturn(uuid); when(pm.getUUID(eq("tastybento"))).thenReturn(uuid);
when(pm.getUUID(Mockito.eq("poslovich"))).thenReturn(notUUID); when(pm.getUUID(eq("poslovich"))).thenReturn(notUUID);
// Has island, has team, but not an owner // Has island, has team, but not an owner
when(im.hasIsland(Mockito.any(),Mockito.eq(uuid))).thenReturn(true); when(im.hasIsland(any(),eq(uuid))).thenReturn(true);
when(im.inTeam(Mockito.any(),Mockito.eq(uuid))).thenReturn(true); when(im.inTeam(any(),eq(uuid))).thenReturn(true);
when(im.getOwner(Mockito.any(),Mockito.eq(uuid))).thenReturn(notUUID); when(im.getOwner(any(),eq(uuid))).thenReturn(notUUID);
// Island // Island
Island island = mock(Island.class); Island island = mock(Island.class);
when(im.getIsland(Mockito.any(), Mockito.eq(uuid))).thenReturn(island); when(im.getIsland(any(), eq(uuid))).thenReturn(island);
assertFalse(itl.execute(user, itl.getLabel(), Arrays.asList(name))); assertFalse(itl.execute(user, itl.getLabel(), Arrays.asList(name)));
Mockito.verify(user).sendMessage("commands.admin.team.add.name-not-owner", "[name]", "tastybento"); verify(user).sendMessage("commands.admin.team.add.name-not-owner", "[name]", "tastybento");
Mockito.verify(island).showMembers(Mockito.any()); verify(user).sendMessage("commands.admin.info.team-members-title");
} }
/** /**
@ -240,19 +243,19 @@ public class AdminTeamAddCommandTest {
AdminTeamAddCommand itl = new AdminTeamAddCommand(ac); AdminTeamAddCommand itl = new AdminTeamAddCommand(ac);
String[] name = {"tastybento", "poslovich"}; String[] name = {"tastybento", "poslovich"};
when(pm.getUUID(Mockito.eq("tastybento"))).thenReturn(uuid); when(pm.getUUID(eq("tastybento"))).thenReturn(uuid);
when(pm.getUUID(Mockito.eq("poslovich"))).thenReturn(notUUID); when(pm.getUUID(eq("poslovich"))).thenReturn(notUUID);
// Has island, has team, is owner // Has island, has team, is owner
when(im.hasIsland(Mockito.any(),Mockito.eq(uuid))).thenReturn(true); when(im.hasIsland(any(),eq(uuid))).thenReturn(true);
when(im.inTeam(Mockito.any(),Mockito.eq(uuid))).thenReturn(true); when(im.inTeam(any(),eq(uuid))).thenReturn(true);
when(im.getOwner(Mockito.any(), Mockito.eq(uuid))).thenReturn(uuid); when(im.getOwner(any(), eq(uuid))).thenReturn(uuid);
// Target has island // Target has island
when(im.hasIsland(Mockito.any(), Mockito.eq(notUUID))).thenReturn(true); when(im.hasIsland(any(), eq(notUUID))).thenReturn(true);
assertFalse(itl.execute(user, itl.getLabel(), Arrays.asList(name))); assertFalse(itl.execute(user, itl.getLabel(), Arrays.asList(name)));
Mockito.verify(user).sendMessage("commands.admin.team.add.name-has-island", "[name]", "poslovich"); verify(user).sendMessage("commands.admin.team.add.name-has-island", "[name]", "poslovich");
} }
@ -264,18 +267,18 @@ public class AdminTeamAddCommandTest {
AdminTeamAddCommand itl = new AdminTeamAddCommand(ac); AdminTeamAddCommand itl = new AdminTeamAddCommand(ac);
String[] name = {"tastybento", "poslovich"}; String[] name = {"tastybento", "poslovich"};
when(pm.getUUID(Mockito.eq("tastybento"))).thenReturn(uuid); when(pm.getUUID(eq("tastybento"))).thenReturn(uuid);
when(pm.getUUID(Mockito.eq("poslovich"))).thenReturn(notUUID); when(pm.getUUID(eq("poslovich"))).thenReturn(notUUID);
// Has island, no team // Has island, no team
when(im.hasIsland(Mockito.any(), Mockito.eq(uuid))).thenReturn(true); when(im.hasIsland(any(), eq(uuid))).thenReturn(true);
when(im.inTeam(Mockito.any(), Mockito.eq(uuid))).thenReturn(false); when(im.inTeam(any(), eq(uuid))).thenReturn(false);
// Target has island // Target has island
when(im.hasIsland(Mockito.any(), Mockito.eq(notUUID))).thenReturn(true); when(im.hasIsland(any(), eq(notUUID))).thenReturn(true);
assertFalse(itl.execute(user, itl.getLabel(), Arrays.asList(name))); assertFalse(itl.execute(user, itl.getLabel(), Arrays.asList(name)));
Mockito.verify(user).sendMessage("commands.admin.team.add.name-has-island", "[name]", "poslovich"); verify(user).sendMessage("commands.admin.team.add.name-has-island", "[name]", "poslovich");
} }
@ -287,29 +290,29 @@ public class AdminTeamAddCommandTest {
AdminTeamAddCommand itl = new AdminTeamAddCommand(ac); AdminTeamAddCommand itl = new AdminTeamAddCommand(ac);
String[] name = {"tastybento", "poslovich"}; String[] name = {"tastybento", "poslovich"};
when(pm.getUUID(Mockito.eq("tastybento"))).thenReturn(uuid); when(pm.getUUID(eq("tastybento"))).thenReturn(uuid);
when(pm.getUUID(Mockito.eq("poslovich"))).thenReturn(notUUID); when(pm.getUUID(eq("poslovich"))).thenReturn(notUUID);
// Has island, no team // Has island, no team
when(im.hasIsland(Mockito.any(), Mockito.eq(uuid))).thenReturn(true); when(im.hasIsland(any(), eq(uuid))).thenReturn(true);
when(im.inTeam(Mockito.any(), Mockito.eq(uuid))).thenReturn(false); when(im.inTeam(any(), eq(uuid))).thenReturn(false);
// Target has no island // Target has no island
when(im.hasIsland(Mockito.any(), Mockito.eq(notUUID))).thenReturn(false); when(im.hasIsland(any(), eq(notUUID))).thenReturn(false);
// Island // Island
Island island = mock(Island.class); Island island = mock(Island.class);
when(im.getIsland(Mockito.any(), Mockito.eq(uuid))).thenReturn(island); when(im.getIsland(any(), eq(uuid))).thenReturn(island);
// Player name // Player name
when(pm.getName(Mockito.eq(uuid))).thenReturn("tastybento"); when(pm.getName(eq(uuid))).thenReturn("tastybento");
when(pm.getName(Mockito.eq(notUUID))).thenReturn("poslovich"); when(pm.getName(eq(notUUID))).thenReturn("poslovich");
when(plugin.getPlayers()).thenReturn(pm); when(plugin.getPlayers()).thenReturn(pm);
// Success // Success
assertTrue(itl.execute(user, itl.getLabel(), Arrays.asList(name))); assertTrue(itl.execute(user, itl.getLabel(), Arrays.asList(name)));
Mockito.verify(im).setJoinTeam(Mockito.eq(island), Mockito.eq(notUUID)); verify(im).setJoinTeam(eq(island), eq(notUUID));
Mockito.verify(user).sendMessage("commands.admin.team.add.success", TextVariables.NAME, name[1], "[owner]", name[0]); verify(user).sendMessage("commands.admin.team.add.success", TextVariables.NAME, name[1], "[owner]", name[0]);
} }
} }

View File

@ -190,7 +190,7 @@ public class AdminTeamKickCommandTest {
assertTrue(itl.canExecute(user, itl.getLabel(), Collections.singletonList("tastybento"))); assertTrue(itl.canExecute(user, itl.getLabel(), Collections.singletonList("tastybento")));
assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("tastybento"))); assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("tastybento")));
verify(user).sendMessage(eq("commands.admin.team.kick.cannot-kick-owner")); verify(user).sendMessage(eq("commands.admin.team.kick.cannot-kick-owner"));
verify(is).showMembers(any()); verify(user).sendMessage("commands.admin.info.team-members-title");
verify(im, never()).removePlayer(eq(world), eq(notUUID)); verify(im, never()).removePlayer(eq(world), eq(notUUID));
verify(pm, never()).clearHomeLocations(eq(world), eq(notUUID)); verify(pm, never()).clearHomeLocations(eq(world), eq(notUUID));
verify(user, never()).sendMessage(eq("commands.admin.team.kick.success"), anyString(), anyString(), anyString(), anyString()); verify(user, never()).sendMessage(eq("commands.admin.team.kick.success"), anyString(), anyString(), anyString(), anyString());

View File

@ -5,7 +5,9 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@ -15,9 +17,12 @@ import java.util.Optional;
import java.util.UUID; import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
import org.eclipse.jdt.annotation.NonNull;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -25,6 +30,7 @@ import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.stubbing.Answer; import org.mockito.stubbing.Answer;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox; import org.powermock.reflect.Whitebox;
@ -34,10 +40,12 @@ import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.CommandsManager; import world.bentobox.bentobox.managers.CommandsManager;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.managers.LocalesManager; import world.bentobox.bentobox.managers.LocalesManager;
import world.bentobox.bentobox.managers.PlaceholdersManager; import world.bentobox.bentobox.managers.PlaceholdersManager;
import world.bentobox.bentobox.managers.PlayersManager; import world.bentobox.bentobox.managers.PlayersManager;
import world.bentobox.bentobox.managers.RanksManager;
import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.Util;
/** /**
@ -50,13 +58,13 @@ public class IslandInfoCommandTest {
@Mock @Mock
private CompositeCommand ic; private CompositeCommand ic;
@Mock
private User user; private User user;
@Mock @Mock
private IslandsManager im; private IslandsManager im;
@Mock @Mock
private PlayersManager pm; private PlayersManager pm;
@Mock
private Island island; private Island island;
private IslandInfoCommand iic; private IslandInfoCommand iic;
@ -67,6 +75,10 @@ public class IslandInfoCommandTest {
private World world; private World world;
@Mock @Mock
private PlaceholdersManager phm; private PlaceholdersManager phm;
@Mock
private @NonNull Location location;
@Mock
private IslandWorldManager iwm;
/** /**
* @throws java.lang.Exception * @throws java.lang.Exception
@ -77,6 +89,12 @@ public class IslandInfoCommandTest {
BentoBox plugin = mock(BentoBox.class); BentoBox plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin); Whitebox.setInternalState(BentoBox.class, "instance", plugin);
// IWM
when(plugin.getIWM()).thenReturn(iwm);
when(plugin.getRanksManager()).thenReturn(new RanksManager());
// Bukkit
PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS);
// Command manager // Command manager
CommandsManager cm = mock(CommandsManager.class); CommandsManager cm = mock(CommandsManager.class);
when(plugin.getCommandsManager()).thenReturn(cm); when(plugin.getCommandsManager()).thenReturn(cm);
@ -84,10 +102,12 @@ public class IslandInfoCommandTest {
// Player // Player
when(player.isOp()).thenReturn(false); when(player.isOp()).thenReturn(false);
UUID uuid = UUID.randomUUID(); UUID uuid = UUID.randomUUID();
when(player.getUniqueId()).thenReturn(uuid); when(user.getUniqueId()).thenReturn(uuid);
when(player.getName()).thenReturn("tastybento"); when(user.getName()).thenReturn("tastybento");
when(player.getWorld()).thenReturn(world); when(user.getWorld()).thenReturn(world);
user = User.getInstance(player); when(user.getPlayer()).thenReturn(player);
when(user.isPlayer()).thenReturn(true);
//user = User.getInstance(player);
// Set the User class plugin as this one // Set the User class plugin as this one
User.setPlugin(plugin); User.setPlugin(plugin);
@ -98,12 +118,16 @@ public class IslandInfoCommandTest {
// Return the same string // Return the same string
when(phm.replacePlaceholders(any(), anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(1, String.class)); when(phm.replacePlaceholders(any(), anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(1, String.class));
when(plugin.getPlaceholdersManager()).thenReturn(phm); when(plugin.getPlaceholdersManager()).thenReturn(phm);
// Translate
when(user.getTranslation(anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(0, String.class));
when(user.getTranslation(anyString(), anyString(), anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(0, String.class));
// Island manager // Island manager
island = new Island(location, uuid, 100);
when(location.toVector()).thenReturn(new Vector(1,2,3));
when(plugin.getIslands()).thenReturn(im); when(plugin.getIslands()).thenReturn(im);
Optional<Island> optionalIsland = Optional.of(island); Optional<Island> optionalIsland = Optional.of(island);
when(im.getIslandAt(any())).thenReturn(optionalIsland); when(im.getIslandAt(any())).thenReturn(optionalIsland);
when(island.showInfo(any())).thenReturn(true);
when(im.getIsland(any(), any(UUID.class))).thenReturn(island); when(im.getIsland(any(), any(UUID.class))).thenReturn(island);
// Players manager // Players manager
@ -141,7 +165,7 @@ public class IslandInfoCommandTest {
@Test @Test
public void testExecuteUserStringListOfStringTooManyArgs() { public void testExecuteUserStringListOfStringTooManyArgs() {
assertFalse(iic.execute(user, "", Arrays.asList("hdhh", "hdhdhd"))); assertFalse(iic.execute(user, "", Arrays.asList("hdhh", "hdhdhd")));
verify(player).sendMessage("commands.help.header"); verify(user).sendMessage("commands.help.header", "[label]", "commands.help.console");
} }
/** /**
@ -152,17 +176,7 @@ public class IslandInfoCommandTest {
CommandSender console = mock(CommandSender.class); CommandSender console = mock(CommandSender.class);
User sender = User.getInstance(console); User sender = User.getInstance(console);
assertFalse(iic.execute(sender, "", Collections.emptyList())); assertFalse(iic.execute(sender, "", Collections.emptyList()));
verify(console).sendMessage("commands.help.header"); verify(user, never()).sendMessage("commands.help.header", "[label]", "commands.help.console");
}
/**
* Test method for {@link world.bentobox.bentobox.api.commands.island.IslandInfoCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringNoArgsNoIslandFalseInfo() {
when(island.showInfo(any())).thenReturn(false);
assertFalse(iic.execute(user, "", Collections.emptyList()));
verify(player).sendMessage("commands.admin.info.no-island");
} }
/** /**
@ -172,7 +186,7 @@ public class IslandInfoCommandTest {
public void testExecuteUserStringListOfStringNoArgsNoIsland() { public void testExecuteUserStringListOfStringNoArgsNoIsland() {
when(im.getIslandAt(any())).thenReturn(Optional.empty()); when(im.getIslandAt(any())).thenReturn(Optional.empty());
assertFalse(iic.execute(user, "", Collections.emptyList())); assertFalse(iic.execute(user, "", Collections.emptyList()));
verify(player).sendMessage("commands.admin.info.no-island"); verify(user).sendMessage("commands.admin.info.no-island");
} }
/** /**
@ -181,7 +195,20 @@ public class IslandInfoCommandTest {
@Test @Test
public void testExecuteUserStringListOfStringNoArgsSuccess() { public void testExecuteUserStringListOfStringNoArgsSuccess() {
assertTrue(iic.execute(user, "", Collections.emptyList())); assertTrue(iic.execute(user, "", Collections.emptyList()));
verify(island).showInfo(any()); verify(user).sendMessage("commands.admin.info.title");
verify(user).sendMessage(eq("commands.admin.info.island-uuid"), eq("[uuid]"), any());
verify(user).sendMessage(eq("commands.admin.info.owner"), eq("[owner]"), eq(null), eq("[uuid]"), any());
verify(user).sendMessage(eq("commands.admin.info.last-login"), eq("[date]"), any());
verify(user).sendMessage("commands.admin.info.deaths", "[number]", "0");
verify(user).sendMessage("commands.admin.info.resets-left", "[number]", "0", "[total]", "0");
verify(user).sendMessage("commands.admin.info.team-members-title");
verify(user).sendMessage("commands.admin.info.team-owner-format", "[name]", null, "[rank]", "ranks.owner");
verify(user).sendMessage("commands.admin.info.island-protection-center", "[xyz]", "0,0,0");
verify(user).sendMessage("commands.admin.info.island-center", "[xyz]", "0,0,0");
verify(user).sendMessage("commands.admin.info.island-coords", "[xz1]", "0,0,0", "[xz2]", "0,0,0");
verify(user).sendMessage("commands.admin.info.protection-range", "[range]", "100");
verify(user).sendMessage("commands.admin.info.max-protection-range", "[range]", "100");
verify(user).sendMessage("commands.admin.info.protection-coords", "[xz1]", "0,0,0", "[xz2]", "0,0,0");
} }
/** /**
@ -190,7 +217,21 @@ public class IslandInfoCommandTest {
@Test @Test
public void testExecuteUserStringListOfStringArgsSuccess() { public void testExecuteUserStringListOfStringArgsSuccess() {
assertTrue(iic.execute(user, "", Collections.singletonList("tastybento"))); assertTrue(iic.execute(user, "", Collections.singletonList("tastybento")));
verify(island).showInfo(any()); verify(user).sendMessage("commands.admin.info.title");
verify(user).sendMessage(eq("commands.admin.info.island-uuid"), eq("[uuid]"), any());
verify(user).sendMessage(eq("commands.admin.info.owner"), eq("[owner]"), eq(null), eq("[uuid]"), any());
verify(user).sendMessage(eq("commands.admin.info.last-login"), eq("[date]"), any());
verify(user).sendMessage("commands.admin.info.deaths", "[number]", "0");
verify(user).sendMessage("commands.admin.info.resets-left", "[number]", "0", "[total]", "0");
verify(user).sendMessage("commands.admin.info.team-members-title");
verify(user).sendMessage("commands.admin.info.team-owner-format", "[name]", null, "[rank]", "ranks.owner");
verify(user).sendMessage("commands.admin.info.island-protection-center", "[xyz]", "0,0,0");
verify(user).sendMessage("commands.admin.info.island-center", "[xyz]", "0,0,0");
verify(user).sendMessage("commands.admin.info.island-coords", "[xz1]", "0,0,0", "[xz2]", "0,0,0");
verify(user).sendMessage("commands.admin.info.protection-range", "[range]", "100");
verify(user).sendMessage("commands.admin.info.max-protection-range", "[range]", "100");
verify(user).sendMessage("commands.admin.info.protection-coords", "[xz1]", "0,0,0", "[xz2]", "0,0,0");
} }
/** /**
@ -200,7 +241,7 @@ public class IslandInfoCommandTest {
public void testExecuteUserStringListOfStringArgsNoIsland() { public void testExecuteUserStringListOfStringArgsNoIsland() {
when(im.getIsland(any(), any(UUID.class))).thenReturn(null); when(im.getIsland(any(), any(UUID.class))).thenReturn(null);
assertFalse(iic.execute(user, "", Collections.singletonList("tastybento"))); assertFalse(iic.execute(user, "", Collections.singletonList("tastybento")));
verify(player).sendMessage("general.errors.player-has-no-island"); verify(user).sendMessage("general.errors.player-has-no-island");
} }
/** /**
@ -210,8 +251,8 @@ public class IslandInfoCommandTest {
public void testExecuteUserStringListOfStringArgsUnknownPlayer() { public void testExecuteUserStringListOfStringArgsUnknownPlayer() {
when(pm.getUUID(any())).thenReturn(null); when(pm.getUUID(any())).thenReturn(null);
assertFalse(iic.execute(user, "", Collections.singletonList("tastybento"))); assertFalse(iic.execute(user, "", Collections.singletonList("tastybento")));
verify(player).sendMessage("general.errors.unknown-player"); verify(user).sendMessage("general.errors.unknown-player", "[name]", "tastybento");
verify(phm).replacePlaceholders(player, "general.errors.unknown-player");
} }
} }