Fix tests, optimize tests, reord imports.

This commit is contained in:
tastybento 2024-10-04 22:25:43 -07:00
parent 97aff7a564
commit 29c50574c3
68 changed files with 592 additions and 983 deletions

View File

@ -138,9 +138,7 @@ public class AdminResetHomeCommand extends CompositeCommand
Map<String, Island> getNameIslandMap(User user) { Map<String, Island> getNameIslandMap(User user) {
Map<String, Island> islandMap = new HashMap<>(); Map<String, Island> islandMap = new HashMap<>();
int index = 0; int index = 0;
System.out.println("Getting for " + user.getName());
for (Island island : getIslands().getIslands(getWorld(), user.getUniqueId())) { for (Island island : getIslands().getIslands(getWorld(), user.getUniqueId())) {
System.out.println("Island - " + island);
index++; index++;
if (island.getName() != null && !island.getName().isBlank()) { if (island.getName() != null && !island.getName().isBlank()) {
// Name has been set // Name has been set

View File

@ -25,6 +25,7 @@ import world.bentobox.bentobox.listeners.flags.protection.EggListener;
import world.bentobox.bentobox.listeners.flags.protection.ElytraListener; import world.bentobox.bentobox.listeners.flags.protection.ElytraListener;
import world.bentobox.bentobox.listeners.flags.protection.EntityInteractListener; import world.bentobox.bentobox.listeners.flags.protection.EntityInteractListener;
import world.bentobox.bentobox.listeners.flags.protection.ExperiencePickupListener; import world.bentobox.bentobox.listeners.flags.protection.ExperiencePickupListener;
import world.bentobox.bentobox.listeners.flags.protection.ExplosionListener;
import world.bentobox.bentobox.listeners.flags.protection.FireListener; import world.bentobox.bentobox.listeners.flags.protection.FireListener;
import world.bentobox.bentobox.listeners.flags.protection.HurtingListener; import world.bentobox.bentobox.listeners.flags.protection.HurtingListener;
import world.bentobox.bentobox.listeners.flags.protection.InventoryListener; import world.bentobox.bentobox.listeners.flags.protection.InventoryListener;
@ -39,7 +40,6 @@ import world.bentobox.bentobox.listeners.flags.protection.PortalListener;
import world.bentobox.bentobox.listeners.flags.protection.SculkSensorListener; import world.bentobox.bentobox.listeners.flags.protection.SculkSensorListener;
import world.bentobox.bentobox.listeners.flags.protection.SculkShriekerListener; import world.bentobox.bentobox.listeners.flags.protection.SculkShriekerListener;
import world.bentobox.bentobox.listeners.flags.protection.ShearingListener; import world.bentobox.bentobox.listeners.flags.protection.ShearingListener;
import world.bentobox.bentobox.listeners.flags.protection.ExplosionListener;
import world.bentobox.bentobox.listeners.flags.protection.TeleportationListener; import world.bentobox.bentobox.listeners.flags.protection.TeleportationListener;
import world.bentobox.bentobox.listeners.flags.protection.ThrowingListener; import world.bentobox.bentobox.listeners.flags.protection.ThrowingListener;
import world.bentobox.bentobox.listeners.flags.settings.DecayListener; import world.bentobox.bentobox.listeners.flags.settings.DecayListener;

View File

@ -1,7 +1,6 @@
package world.bentobox.bentobox.managers.island; package world.bentobox.bentobox.managers.island;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.TreeMap; import java.util.TreeMap;
import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.database.objects.Island;

View File

@ -28,7 +28,21 @@ import org.bukkit.World.Environment;
import org.bukkit.attribute.Attribute; import org.bukkit.attribute.Attribute;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
import org.bukkit.entity.*; import org.bukkit.entity.Allay;
import org.bukkit.entity.Animals;
import org.bukkit.entity.Bat;
import org.bukkit.entity.EnderDragon;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Flying;
import org.bukkit.entity.IronGolem;
import org.bukkit.entity.Monster;
import org.bukkit.entity.Player;
import org.bukkit.entity.PufferFish;
import org.bukkit.entity.Shulker;
import org.bukkit.entity.Slime;
import org.bukkit.entity.Snowman;
import org.bukkit.entity.Tameable;
import org.bukkit.entity.WaterMob;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNull;

View File

@ -1,8 +1,11 @@
package world.bentobox.bentobox.listeners.flags; package world.bentobox.bentobox;
import static org.junit.Assert.assertEquals;
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.Mockito.atLeast;
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.Collections; import java.util.Collections;
@ -19,6 +22,7 @@ import org.bukkit.World;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.entity.Player.Spigot;
import org.bukkit.event.entity.EntityExplodeEvent; import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.inventory.ItemFactory; import org.bukkit.inventory.ItemFactory;
@ -31,6 +35,7 @@ import org.bukkit.util.Vector;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import org.junit.After; import org.junit.After;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
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;
@ -40,8 +45,7 @@ import org.powermock.reflect.Whitebox;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import world.bentobox.bentobox.BentoBox; import net.md_5.bungee.api.chat.TextComponent;
import world.bentobox.bentobox.Settings;
import world.bentobox.bentobox.api.configuration.WorldSettings; import world.bentobox.bentobox.api.configuration.WorldSettings;
import world.bentobox.bentobox.api.user.Notifier; import world.bentobox.bentobox.api.user.Notifier;
import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.api.user.User;
@ -97,6 +101,8 @@ public abstract class AbstractCommonSetup {
protected Notifier notifier; protected Notifier notifier;
@Mock @Mock
protected FlagsManager fm; protected FlagsManager fm;
@Mock
protected Spigot spigot;
public void setUp() throws Exception { public void setUp() throws Exception {
@ -129,6 +135,7 @@ public abstract class AbstractCommonSetup {
when(mockPlayer.getWorld()).thenReturn(world); when(mockPlayer.getWorld()).thenReturn(world);
when(mockPlayer.getName()).thenReturn("tastybento"); when(mockPlayer.getName()).thenReturn("tastybento");
when(mockPlayer.getInventory()).thenReturn(inv); when(mockPlayer.getInventory()).thenReturn(inv);
when(mockPlayer.spigot()).thenReturn(spigot);
User.setPlugin(plugin); User.setPlugin(plugin);
User.clearUsers(); User.clearUsers();
@ -176,12 +183,13 @@ public abstract class AbstractCommonSetup {
when(plugin.getSettings()).thenReturn(settings); when(plugin.getSettings()).thenReturn(settings);
when(settings.getFakePlayers()).thenReturn(new HashSet<>()); when(settings.getFakePlayers()).thenReturn(new HashSet<>());
PowerMockito.mockStatic(Util.class); PowerMockito.mockStatic(Util.class, Mockito.CALLS_REAL_METHODS);
when(Util.getWorld(any())).thenReturn(mock(World.class)); when(Util.getWorld(any())).thenReturn(mock(World.class));
// Util // Util
when(Util.findFirstMatchingEnum(any(), any())).thenCallRealMethod(); when(Util.findFirstMatchingEnum(any(), any())).thenCallRealMethod();
// Util translate color codes (used in user translate methods) // Util translate color codes (used in user translate methods)
when(Util.translateColorCodes(anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(0, String.class)); //when(Util.translateColorCodes(anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(0, String.class));
// Tags // Tags
for (Material m : Material.values()) { for (Material m : Material.values()) {
@ -217,6 +225,70 @@ public abstract class AbstractCommonSetup {
Mockito.framework().clearInlineMocks(); Mockito.framework().clearInlineMocks();
} }
/**
* Check that spigot sent the message
* @param message - message to check
*/
public void checkSpigotMessage(String expectedMessage) {
checkSpigotMessage(expectedMessage, 1);
}
/*
public void checkSpigotMessage(String expectedMessage, boolean shouldBePresent) {
// Capture the argument passed to spigot().sendMessage(...) if messages are sent
ArgumentCaptor<TextComponent> captor = ArgumentCaptor.forClass(TextComponent.class);
if (shouldBePresent) {
// If we expect a message to be present, verify that sendMessage() was called at least once
verify(spigot, atLeastOnce()).sendMessage(captor.capture());
// Get all captured TextComponents
List<TextComponent> capturedMessages = captor.getAllValues();
// Check if any captured message contains the expected text
boolean messageFound = capturedMessages.stream()
.map(component -> component.toPlainText()) // Convert each TextComponent to plain text
.anyMatch(messageText -> messageText.contains(expectedMessage)); // Check if the expected message is present
// Assert that the message was found
assertTrue("Expected message not found: " + expectedMessage, messageFound);
} else {
// If we expect no messages with this text, capture any sent messages to ensure none match the given message
verify(spigot, atLeast(0)).sendMessage(captor.capture());
// Get all captured TextComponents
List<TextComponent> capturedMessages = captor.getAllValues();
// Check that none of the captured messages contain the forbidden text
boolean messageFound = capturedMessages.stream().map(component -> component.toPlainText()) // Convert each TextComponent to plain text
.anyMatch(messageText -> messageText.contains(expectedMessage)); // Check if the message is present
// Assert that the message was NOT found
assertFalse("Unexpected message found: " + expectedMessage, messageFound);
}
}*/
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.toPlainText()) // 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);
}
/** /**
* Get the explode event * Get the explode event
* @param entity * @param entity

View File

@ -1,4 +1,4 @@
package world.bentobox.bentobox.managers; package world.bentobox.bentobox;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyInt;
@ -29,10 +29,10 @@ 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;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.AbstractDatabaseHandler; import world.bentobox.bentobox.database.AbstractDatabaseHandler;
import world.bentobox.bentobox.database.DatabaseSetup; import world.bentobox.bentobox.database.DatabaseSetup;
import world.bentobox.bentobox.managers.RanksManager;
/** /**
* @author tastybento * @author tastybento
@ -40,7 +40,7 @@ import world.bentobox.bentobox.database.DatabaseSetup;
*/ */
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest({ BentoBox.class, DatabaseSetup.class, RanksManager.class, Bukkit.class }) @PrepareForTest({ BentoBox.class, DatabaseSetup.class, RanksManager.class, Bukkit.class })
public abstract class RanksManagerBeforeClassTest { public abstract class RanksManagerBeforeClassTest extends AbstractCommonSetup {
// Constants that define the hard coded rank values // Constants that define the hard coded rank values
public static final String ADMIN_RANK_REF = "ranks.admin"; public static final String ADMIN_RANK_REF = "ranks.admin";
@ -68,8 +68,6 @@ public abstract class RanksManagerBeforeClassTest {
TRUSTED_RANK_REF, TRUSTED_RANK, COOP_RANK_REF, COOP_RANK, VISITOR_RANK_REF, VISITOR_RANK, BANNED_RANK_REF, TRUSTED_RANK_REF, TRUSTED_RANK, COOP_RANK_REF, COOP_RANK, VISITOR_RANK_REF, VISITOR_RANK, BANNED_RANK_REF,
BANNED_RANK); BANNED_RANK);
@Mock
public BentoBox plugin;
@Mock @Mock
public RanksManager rm; public RanksManager rm;
@ -113,6 +111,7 @@ public abstract class RanksManagerBeforeClassTest {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp();
PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS);
when(Bukkit.getBukkitVersion()).thenReturn(""); when(Bukkit.getBukkitVersion()).thenReturn("");
// Set up plugin // Set up plugin

View File

@ -48,7 +48,6 @@ import world.bentobox.bentobox.api.flags.Flag;
import world.bentobox.bentobox.api.flags.FlagListener; import world.bentobox.bentobox.api.flags.FlagListener;
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.listeners.flags.AbstractCommonSetup;
import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.managers.CommandsManager; import world.bentobox.bentobox.managers.CommandsManager;
import world.bentobox.bentobox.managers.FlagsManager; import world.bentobox.bentobox.managers.FlagsManager;

View File

@ -17,12 +17,8 @@ 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.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import org.eclipse.jdt.annotation.NonNull;
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;
@ -34,16 +30,15 @@ import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.modules.junit4.PowerMockRunner;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.RanksManagerBeforeClassTest;
import world.bentobox.bentobox.api.commands.CompositeCommand; 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.RanksManagerBeforeClassTest;
import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.Util;
/** /**
@ -59,27 +54,15 @@ public class AdminInfoCommandTest extends RanksManagerBeforeClassTest {
@Mock @Mock
private User user; private User user;
@Mock @Mock
private IslandsManager im;
@Mock
private PlayersManager pm; private PlayersManager pm;
private Island island; private Island island;
private AdminInfoCommand iic; private AdminInfoCommand iic;
@Mock
private Player player;
@Mock
private World world;
@Mock @Mock
private PlaceholdersManager phm; private PlaceholdersManager phm;
@Mock
private @NonNull Location location;
@Mock
private IslandWorldManager iwm;
/**
*/
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); super.setUp();
@ -96,12 +79,12 @@ public class AdminInfoCommandTest extends RanksManagerBeforeClassTest {
when(plugin.getCommandsManager()).thenReturn(cm); when(plugin.getCommandsManager()).thenReturn(cm);
// Player // Player
when(player.isOp()).thenReturn(false); when(mockPlayer.isOp()).thenReturn(false);
UUID uuid = UUID.randomUUID(); UUID uuid = UUID.randomUUID();
when(user.getUniqueId()).thenReturn(uuid); when(user.getUniqueId()).thenReturn(uuid);
when(user.getName()).thenReturn("tastybento"); when(user.getName()).thenReturn("tastybento");
when(user.getWorld()).thenReturn(world); when(user.getWorld()).thenReturn(world);
when(user.getPlayer()).thenReturn(player); when(user.getPlayer()).thenReturn(mockPlayer);
when(user.isPlayer()).thenReturn(true); when(user.isPlayer()).thenReturn(true);
//user = User.getInstance(player); //user = User.getInstance(player);
// Set the User class plugin as this one // Set the User class plugin as this one
@ -164,6 +147,7 @@ public class AdminInfoCommandTest extends RanksManagerBeforeClassTest {
@Test @Test
public void testExecuteUserStringListOfStringNoArgsConsole() { public void testExecuteUserStringListOfStringNoArgsConsole() {
CommandSender console = mock(CommandSender.class); CommandSender console = mock(CommandSender.class);
when(console.spigot()).thenReturn(spigot);
User sender = User.getInstance(console); User sender = User.getInstance(console);
assertFalse(iic.execute(sender, "", Collections.emptyList())); assertFalse(iic.execute(sender, "", Collections.emptyList()));
verify(user, never()).sendMessage("commands.help.header", "[label]", "commands.help.console"); verify(user, never()).sendMessage("commands.help.header", "[label]", "commands.help.console");

View File

@ -4,7 +4,6 @@ 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.any;
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.never;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
@ -19,7 +18,6 @@ import java.util.Optional;
import java.util.UUID; import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.scheduler.BukkitScheduler;
import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
@ -30,11 +28,10 @@ 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 world.bentobox.bentobox.AbstractCommonSetup;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.Settings;
import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.commands.CompositeCommand;
@ -43,25 +40,23 @@ import world.bentobox.bentobox.api.flags.Flag.Type;
import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.managers.CommandsManager; import world.bentobox.bentobox.managers.CommandsManager;
import world.bentobox.bentobox.managers.FlagsManager; import world.bentobox.bentobox.managers.FlagsManager;
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.util.Util;
/** /**
* @author tastybento * @author tastybento
* *
*/ */
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest({Bukkit.class, BentoBox.class, User.class }) @PrepareForTest({ Bukkit.class, BentoBox.class, User.class, Util.class })
public class AdminResetFlagsCommandTest { public class AdminResetFlagsCommandTest extends AbstractCommonSetup {
@Mock @Mock
private CompositeCommand ac; private CompositeCommand ac;
private final UUID uuid = UUID.randomUUID(); private final UUID uuid = UUID.randomUUID();
@Mock @Mock
private IslandsManager im;
@Mock
private PlayersManager pm; private PlayersManager pm;
@Mock @Mock
private FlagsManager fm; private FlagsManager fm;
@ -71,8 +66,6 @@ public class AdminResetFlagsCommandTest {
private Flag flag2; private Flag flag2;
@Mock @Mock
private Flag flag3; private Flag flag3;
@Mock
private Player player;
private AdminResetFlagsCommand arf; private AdminResetFlagsCommand arf;
private @Nullable User user; private @Nullable User user;
@ -81,10 +74,7 @@ public class AdminResetFlagsCommandTest {
*/ */
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); super.setUp();
// Set up plugin
BentoBox plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
// Command manager // Command manager
CommandsManager cm = mock(CommandsManager.class); CommandsManager cm = mock(CommandsManager.class);
@ -96,8 +86,8 @@ public class AdminResetFlagsCommandTest {
when(ac.getPermissionPrefix()).thenReturn("bskyblock."); when(ac.getPermissionPrefix()).thenReturn("bskyblock.");
// Player // Player
when(player.getUniqueId()).thenReturn(uuid); when(mockPlayer.getUniqueId()).thenReturn(uuid);
user = User.getInstance(player); user = User.getInstance(mockPlayer);
// Flags manager // Flags manager
when(plugin.getFlagsManager()).thenReturn(fm); when(plugin.getFlagsManager()).thenReturn(fm);
@ -175,7 +165,7 @@ public class AdminResetFlagsCommandTest {
public void testExecuteUserStringListOfStringTwoArgs() { public void testExecuteUserStringListOfStringTwoArgs() {
List<String> args = Arrays.asList("sdfsd", "werwerw"); List<String> args = Arrays.asList("sdfsd", "werwerw");
assertFalse(arf.execute(user, "", args)); assertFalse(arf.execute(user, "", args));
verify(player).sendMessage(eq("commands.help.header")); checkSpigotMessage("commands.help.header");
} }
/** /**
@ -184,7 +174,7 @@ public class AdminResetFlagsCommandTest {
@Test @Test
public void testExecuteUserStringListOfStringOneArgNotFlag() { public void testExecuteUserStringListOfStringOneArgNotFlag() {
assertFalse(arf.execute(user, "", Collections.singletonList("FLAG3"))); assertFalse(arf.execute(user, "", Collections.singletonList("FLAG3")));
verify(player).sendMessage(eq("commands.help.header")); checkSpigotMessage("commands.help.header");
} }
/** /**

View File

@ -30,13 +30,13 @@ import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.modules.junit4.PowerMockRunner;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.RanksManagerBeforeClassTest;
import world.bentobox.bentobox.api.commands.CompositeCommand; 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.IslandsManager; import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.managers.PlayersManager; import world.bentobox.bentobox.managers.PlayersManager;
import world.bentobox.bentobox.managers.RanksManager; import world.bentobox.bentobox.managers.RanksManager;
import world.bentobox.bentobox.managers.RanksManagerBeforeClassTest;
import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.Util;
/** /**

View File

@ -40,6 +40,7 @@ import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.modules.junit4.PowerMockRunner;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.RanksManagerBeforeClassTest;
import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.Settings;
import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.localization.TextVariables; import world.bentobox.bentobox.api.localization.TextVariables;
@ -51,7 +52,6 @@ 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.PlayersManager; import world.bentobox.bentobox.managers.PlayersManager;
import world.bentobox.bentobox.managers.RanksManagerBeforeClassTest;
import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.Util;
/** /**

View File

@ -11,78 +11,64 @@ import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
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.scheduler.BukkitScheduler; import org.bukkit.scheduler.BukkitScheduler;
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.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 world.bentobox.bentobox.AbstractCommonSetup;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.commands.CompositeCommand; 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.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.LocalesManager; import world.bentobox.bentobox.managers.LocalesManager;
import world.bentobox.bentobox.managers.PlayersManager; import world.bentobox.bentobox.managers.PlayersManager;
import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.Util;
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest({ Bukkit.class, BentoBox.class, User.class }) @PrepareForTest({ Bukkit.class, BentoBox.class, User.class, Util.class })
public class AdminRangeCommandTest { public class AdminRangeCommandTest extends AbstractCommonSetup {
@Mock
private CompositeCommand ac; private CompositeCommand ac;
@Mock
private User user; private User user;
/** /**
*/ */
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); super.setUp();
// Set up plugin
BentoBox plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
Util.setPlugin(plugin); Util.setPlugin(plugin);
// 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 // User
Player p = mock(Player.class);
// Sometimes use Mockito.withSettings().verboseLogging()
user = mock(User.class);
when(user.isOp()).thenReturn(false); when(user.isOp()).thenReturn(false);
UUID uuid = UUID.randomUUID(); UUID uuid = UUID.randomUUID();
UUID 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.getPlayer()).thenReturn(mockPlayer);
when(user.getName()).thenReturn("tastybento"); when(user.getName()).thenReturn("tastybento");
when(user.getTranslation("commands.help.console")).thenReturn("Console"); when(user.getTranslation("commands.help.console")).thenReturn("Console");
User.setPlugin(plugin); User.setPlugin(plugin);
// Parent command has no aliases // Parent command has no aliases
ac = mock(CompositeCommand.class);
when(ac.getSubCommandAliases()).thenReturn(new HashMap<>()); when(ac.getSubCommandAliases()).thenReturn(new HashMap<>());
when(ac.getWorld()).thenReturn(mock(World.class)); when(ac.getWorld()).thenReturn(mock(World.class));
// Island World Manager // Island World Manager
IslandWorldManager iwm = mock(IslandWorldManager.class);
when(iwm.getFriendlyName(Mockito.any())).thenReturn("BSkyBlock"); when(iwm.getFriendlyName(Mockito.any())).thenReturn("BSkyBlock");
when(plugin.getIWM()).thenReturn(iwm); when(plugin.getIWM()).thenReturn(iwm);
// Player has island to begin with // Player has island to begin with
IslandsManager im = mock(IslandsManager.class);
when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true); when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true);
when(im.hasIsland(Mockito.any(), Mockito.any(User.class))).thenReturn(true); when(im.hasIsland(Mockito.any(), Mockito.any(User.class))).thenReturn(true);
when(plugin.getIslands()).thenReturn(im); when(plugin.getIslands()).thenReturn(im);
@ -117,10 +103,11 @@ public class AdminRangeCommandTest {
public void testExecuteConsoleNoArgs() { public void testExecuteConsoleNoArgs() {
AdminRangeCommand arc = new AdminRangeCommand(ac); AdminRangeCommand arc = new AdminRangeCommand(ac);
CommandSender sender = mock(CommandSender.class); CommandSender sender = mock(CommandSender.class);
when(sender.spigot()).thenReturn(spigot);
User console = User.getInstance(sender); User console = User.getInstance(sender);
arc.execute(console, "", new ArrayList<>()); arc.execute(console, "", new ArrayList<>());
// Show help // Show help
Mockito.verify(sender).sendMessage("commands.help.header"); checkSpigotMessage("commands.help.header");
} }
@Test @Test

View File

@ -2,6 +2,7 @@ package world.bentobox.bentobox.api.commands.admin.range;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; 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.ArrayList;
@ -14,7 +15,6 @@ import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
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.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.scheduler.BukkitScheduler;
import org.junit.After; import org.junit.After;
@ -24,19 +24,16 @@ 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 world.bentobox.bentobox.AbstractCommonSetup;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.commands.CompositeCommand; 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.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.LocalesManager; import world.bentobox.bentobox.managers.LocalesManager;
import world.bentobox.bentobox.managers.PlayersManager; import world.bentobox.bentobox.managers.PlayersManager;
import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.Util;
@ -46,13 +43,14 @@ import world.bentobox.bentobox.util.Util;
* *
*/ */
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest({ Bukkit.class, BentoBox.class, User.class }) @PrepareForTest({ Bukkit.class, BentoBox.class, User.class, Util.class })
public class AdminRangeResetCommandTest { public class AdminRangeResetCommandTest extends AbstractCommonSetup {
@Mock
private CompositeCommand ac; private CompositeCommand ac;
private UUID uuid; private UUID uuid;
@Mock
private User user; private User user;
private IslandsManager im;
private PlayersManager pm; private PlayersManager pm;
@Mock @Mock
private PluginManager pim; private PluginManager pim;
@ -61,21 +59,14 @@ public class AdminRangeResetCommandTest {
*/ */
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); super.setUp();
// Set up plugin
BentoBox plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
Util.setPlugin(plugin); Util.setPlugin(plugin);
// 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 p = mock(Player.class);
// Sometimes use Mockito.withSettings().verboseLogging()
user = mock(User.class);
when(user.isOp()).thenReturn(false); when(user.isOp()).thenReturn(false);
uuid = UUID.randomUUID(); uuid = UUID.randomUUID();
UUID notUUID = UUID.randomUUID(); UUID notUUID = UUID.randomUUID();
@ -83,23 +74,20 @@ public class AdminRangeResetCommandTest {
notUUID = UUID.randomUUID(); notUUID = UUID.randomUUID();
} }
when(user.getUniqueId()).thenReturn(uuid); when(user.getUniqueId()).thenReturn(uuid);
when(user.getPlayer()).thenReturn(p); when(user.getPlayer()).thenReturn(mockPlayer);
when(user.getName()).thenReturn("tastybento"); when(user.getName()).thenReturn("tastybento");
User.setPlugin(plugin); User.setPlugin(plugin);
// Parent command has no aliases // Parent command has no aliases
ac = mock(CompositeCommand.class);
when(ac.getSubCommandAliases()).thenReturn(new HashMap<>()); when(ac.getSubCommandAliases()).thenReturn(new HashMap<>());
when(ac.getWorld()).thenReturn(mock(World.class)); when(ac.getWorld()).thenReturn(mock(World.class));
// Island World Manager // Island World Manager
IslandWorldManager iwm = mock(IslandWorldManager.class);
when(iwm.getFriendlyName(Mockito.any())).thenReturn("BSkyBlock"); when(iwm.getFriendlyName(Mockito.any())).thenReturn("BSkyBlock");
when(iwm.getIslandProtectionRange(Mockito.any())).thenReturn(200); when(iwm.getIslandProtectionRange(Mockito.any())).thenReturn(200);
when(plugin.getIWM()).thenReturn(iwm); when(plugin.getIWM()).thenReturn(iwm);
// Player has island to begin with // 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(UUID.class))).thenReturn(true);
when(im.hasIsland(Mockito.any(), Mockito.any(User.class))).thenReturn(true); when(im.hasIsland(Mockito.any(), Mockito.any(User.class))).thenReturn(true);
Island island = mock(Island.class); Island island = mock(Island.class);
@ -142,10 +130,11 @@ public class AdminRangeResetCommandTest {
public void testExecuteConsoleNoArgs() { public void testExecuteConsoleNoArgs() {
AdminRangeResetCommand arc = new AdminRangeResetCommand(ac); AdminRangeResetCommand arc = new AdminRangeResetCommand(ac);
CommandSender sender = mock(CommandSender.class); CommandSender sender = mock(CommandSender.class);
when(sender.spigot()).thenReturn(spigot);
User console = User.getInstance(sender); User console = User.getInstance(sender);
arc.execute(console, "", new ArrayList<>()); arc.execute(console, "", new ArrayList<>());
// Show help // Show help
Mockito.verify(sender).sendMessage("commands.help.header"); checkSpigotMessage("commands.help.header");
} }
/** /**
@ -157,7 +146,7 @@ public class AdminRangeResetCommandTest {
AdminRangeResetCommand arc = new AdminRangeResetCommand(ac); AdminRangeResetCommand arc = new AdminRangeResetCommand(ac);
arc.execute(user, "", new ArrayList<>()); arc.execute(user, "", new ArrayList<>());
// Show help // Show help
Mockito.verify(user).sendMessage("commands.help.header", "[label]", "BSkyBlock"); verify(user).sendMessage("commands.help.header", "[label]", "BSkyBlock");
} }
/** /**
@ -169,7 +158,7 @@ public class AdminRangeResetCommandTest {
AdminRangeResetCommand arc = new AdminRangeResetCommand(ac); AdminRangeResetCommand arc = new AdminRangeResetCommand(ac);
String[] name = { "tastybento" }; String[] name = { "tastybento" };
arc.execute(user, "", Arrays.asList(name)); arc.execute(user, "", Arrays.asList(name));
Mockito.verify(user).sendMessage("general.errors.unknown-player", "[name]", name[0]); verify(user).sendMessage("general.errors.unknown-player", "[name]", name[0]);
} }
/** /**
@ -184,7 +173,7 @@ public class AdminRangeResetCommandTest {
List<String> args = new ArrayList<>(); List<String> args = new ArrayList<>();
args.add("tastybento"); args.add("tastybento");
arc.execute(user, "", args); arc.execute(user, "", args);
Mockito.verify(user).sendMessage("general.errors.player-has-no-island"); verify(user).sendMessage("general.errors.player-has-no-island");
} }
@Test @Test
@ -196,7 +185,7 @@ public class AdminRangeResetCommandTest {
List<String> args = new ArrayList<>(); List<String> args = new ArrayList<>();
args.add("tastybento"); args.add("tastybento");
arc.execute(user, "", args); arc.execute(user, "", args);
Mockito.verify(user, never()).sendMessage("general.errors.player-has-no-island"); verify(user, never()).sendMessage("general.errors.player-has-no-island");
} }
/** /**
@ -209,7 +198,7 @@ public class AdminRangeResetCommandTest {
List<String> args = new ArrayList<>(); List<String> args = new ArrayList<>();
args.add("tastybento"); args.add("tastybento");
arc.execute(user, "", args); arc.execute(user, "", args);
Mockito.verify(user).sendMessage("commands.admin.range.reset.success", TextVariables.NUMBER, "200"); verify(user).sendMessage("commands.admin.range.reset.success", TextVariables.NUMBER, "200");
} }
} }

View File

@ -17,14 +17,10 @@ import java.util.Set;
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.plugin.PluginManager;
import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.scheduler.BukkitScheduler;
import org.bukkit.util.Vector; 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;
@ -32,19 +28,15 @@ 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 world.bentobox.bentobox.AbstractCommonSetup;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.commands.CompositeCommand; 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.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.LocalesManager; import world.bentobox.bentobox.managers.LocalesManager;
import world.bentobox.bentobox.managers.PlayersManager; import world.bentobox.bentobox.managers.PlayersManager;
import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.Util;
@ -54,64 +46,49 @@ import world.bentobox.bentobox.util.Util;
* *
*/ */
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest({ Bukkit.class, BentoBox.class, User.class }) @PrepareForTest({ Bukkit.class, BentoBox.class, User.class, Util.class })
public class AdminRangeSetCommandTest { public class AdminRangeSetCommandTest extends AbstractCommonSetup {
@Mock
private CommandsManager cm;
@Mock
private CompositeCommand ac; private CompositeCommand ac;
private UUID uuid; private UUID uuid;
@Mock
private User user; private User user;
private IslandsManager im;
private PlayersManager pm; private PlayersManager pm;
@Mock
private PluginManager pim;
@Mock
private @NonNull Location location;
/** /**
*/ */
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); super.setUp();
// Set up plugin
BentoBox plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
Util.setPlugin(plugin); Util.setPlugin(plugin);
// Command manager // Command manager
CommandsManager cm = mock(CommandsManager.class);
when(plugin.getCommandsManager()).thenReturn(cm); when(plugin.getCommandsManager()).thenReturn(cm);
// Player
Player p = mock(Player.class);
// Sometimes use Mockito.withSettings().verboseLogging()
user = mock(User.class);
when(user.isOp()).thenReturn(false); when(user.isOp()).thenReturn(false);
uuid = UUID.randomUUID(); uuid = UUID.randomUUID();
UUID 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.getPlayer()).thenReturn(mockPlayer);
when(user.getName()).thenReturn("tastybento"); when(user.getName()).thenReturn("tastybento");
User.setPlugin(plugin); User.setPlugin(plugin);
// Parent command has no aliases // Parent command has no aliases
ac = mock(CompositeCommand.class);
when(ac.getSubCommandAliases()).thenReturn(new HashMap<>()); when(ac.getSubCommandAliases()).thenReturn(new HashMap<>());
when(ac.getWorld()).thenReturn(mock(World.class)); when(ac.getWorld()).thenReturn(mock(World.class));
// Island World Manager // Island World Manager
IslandWorldManager iwm = mock(IslandWorldManager.class);
when(iwm.getFriendlyName(any())).thenReturn("BSkyBlock"); when(iwm.getFriendlyName(any())).thenReturn("BSkyBlock");
when(iwm.getIslandProtectionRange(any())).thenReturn(200); when(iwm.getIslandProtectionRange(any())).thenReturn(200);
when(plugin.getIWM()).thenReturn(iwm); when(plugin.getIWM()).thenReturn(iwm);
// Player has island to begin with // Player has island to begin with
im = mock(IslandsManager.class);
when(im.hasIsland(any(), any(UUID.class))).thenReturn(true); when(im.hasIsland(any(), any(UUID.class))).thenReturn(true);
when(im.hasIsland(any(), any(User.class))).thenReturn(true); when(im.hasIsland(any(), any(User.class))).thenReturn(true);
Island island = mock(Island.class);
when(island.getRange()).thenReturn(50); when(island.getRange()).thenReturn(50);
when(island.getProtectionRange()).thenReturn(50); when(island.getProtectionRange()).thenReturn(50);
when(location.toVector()).thenReturn(new Vector(2, 3, 4)); when(location.toVector()).thenReturn(new Vector(2, 3, 4));
@ -155,10 +132,11 @@ public class AdminRangeSetCommandTest {
public void testExecuteConsoleNoArgs() { public void testExecuteConsoleNoArgs() {
AdminRangeSetCommand arc = new AdminRangeSetCommand(ac); AdminRangeSetCommand arc = new AdminRangeSetCommand(ac);
CommandSender sender = mock(CommandSender.class); CommandSender sender = mock(CommandSender.class);
when(sender.spigot()).thenReturn(spigot);
User console = User.getInstance(sender); User console = User.getInstance(sender);
assertFalse(arc.canExecute(console, "", new ArrayList<>())); assertFalse(arc.canExecute(console, "", new ArrayList<>()));
// Show help // Show help
verify(sender).sendMessage("commands.help.header"); checkSpigotMessage("commands.help.header");
} }
/** /**

View File

@ -21,13 +21,10 @@ import java.util.Set;
import java.util.UUID; import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.scheduler.BukkitScheduler;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -38,18 +35,15 @@ 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;
import org.powermock.reflect.Whitebox;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import world.bentobox.bentobox.AbstractCommonSetup;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.commands.CompositeCommand; 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.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.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;
@ -61,7 +55,7 @@ import world.bentobox.bentobox.util.Util;
*/ */
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest({ Bukkit.class, BentoBox.class, User.class, Util.class }) @PrepareForTest({ Bukkit.class, BentoBox.class, User.class, Util.class })
public class AdminTeamDisbandCommandTest { public class AdminTeamDisbandCommandTest extends AbstractCommonSetup {
@Mock @Mock
private CompositeCommand ac; private CompositeCommand ac;
@ -69,31 +63,17 @@ public class AdminTeamDisbandCommandTest {
@Mock @Mock
private User user; private User user;
@Mock @Mock
private Player p;
@Mock
private Player p2; private Player p2;
@Mock @Mock
private IslandsManager im;
@Mock
private PlayersManager pm; private PlayersManager pm;
@Mock
private PluginManager pim;
private UUID notUUID; private UUID notUUID;
@Mock
private @Nullable Island island;
@Mock
private @NonNull Location location;
private AdminTeamDisbandCommand itl; private AdminTeamDisbandCommand itl;
/**
*/
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); super.setUp();
// Set up plugin
BentoBox plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
Util.setPlugin(plugin); Util.setPlugin(plugin);
// Command manager // Command manager
@ -109,25 +89,21 @@ public class AdminTeamDisbandCommandTest {
notUUID = UUID.randomUUID(); notUUID = UUID.randomUUID();
} }
when(user.getUniqueId()).thenReturn(uuid); when(user.getUniqueId()).thenReturn(uuid);
when(user.getPlayer()).thenReturn(p); when(user.getPlayer()).thenReturn(mockPlayer);
when(user.getName()).thenReturn("tastybento"); when(user.getName()).thenReturn("tastybento");
User.setPlugin(plugin); User.setPlugin(plugin);
// Set up users // Set up users
when(p.getUniqueId()).thenReturn(uuid); when(mockPlayer.getUniqueId()).thenReturn(uuid);
when(p2.getUniqueId()).thenReturn(notUUID); when(p2.getUniqueId()).thenReturn(notUUID);
User.getInstance(p); User.getInstance(mockPlayer);
when(p2.spigot()).thenReturn(spigot);
User.getInstance(p2); User.getInstance(p2);
// Parent command has no aliases // Parent command has no aliases
ac = mock(CompositeCommand.class); ac = mock(CompositeCommand.class);
when(ac.getSubCommandAliases()).thenReturn(new HashMap<>()); when(ac.getSubCommandAliases()).thenReturn(new HashMap<>());
// Island World Manager
IslandWorldManager iwm = mock(IslandWorldManager.class);
when(plugin.getIWM()).thenReturn(iwm);
// Player has island to begin with // Player has island to begin with
im = mock(IslandsManager.class);
when(im.hasIsland(any(), any(UUID.class))).thenReturn(true); when(im.hasIsland(any(), any(UUID.class))).thenReturn(true);
when(im.hasIsland(any(), any(User.class))).thenReturn(true); when(im.hasIsland(any(), any(User.class))).thenReturn(true);
when(island.getOwner()).thenReturn(uuid); when(island.getOwner()).thenReturn(uuid);
@ -166,6 +142,7 @@ public class AdminTeamDisbandCommandTest {
// Online players // Online players
PowerMockito.mockStatic(Util.class, Mockito.RETURNS_MOCKS); PowerMockito.mockStatic(Util.class, Mockito.RETURNS_MOCKS);
when(Util.getOnlinePlayerList(user)).thenReturn(List.of("tastybento", "BONNe")); when(Util.getOnlinePlayerList(user)).thenReturn(List.of("tastybento", "BONNe"));
when(Util.translateColorCodes(anyString())) when(Util.translateColorCodes(anyString()))
.thenAnswer((Answer<String>) invocation -> invocation.getArgument(0, String.class)); .thenAnswer((Answer<String>) invocation -> invocation.getArgument(0, String.class));
@ -246,8 +223,7 @@ public class AdminTeamDisbandCommandTest {
verify(im, never()).removePlayer(island, uuid); verify(im, never()).removePlayer(island, uuid);
verify(im).removePlayer(island, notUUID); verify(im).removePlayer(island, notUUID);
verify(user).sendMessage("commands.admin.team.disband.success", TextVariables.NAME, "tastybento"); verify(user).sendMessage("commands.admin.team.disband.success", TextVariables.NAME, "tastybento");
verify(p).sendMessage("commands.admin.team.disband.disbanded"); checkSpigotMessage("commands.admin.team.disband.disbanded", 2);
verify(p2).sendMessage("commands.admin.team.disband.disbanded");
// 2 + 1 // 2 + 1
verify(pim, times(3)).callEvent(any()); verify(pim, times(3)).callEvent(any());
} }

View File

@ -14,32 +14,29 @@ import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.World; import org.bukkit.World;
import org.eclipse.jdt.annotation.Nullable;
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.Mock;
import org.mockito.Mockito;
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 world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.RanksManagerBeforeClassTest;
import world.bentobox.bentobox.api.addons.GameModeAddon; import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.api.configuration.WorldSettings; import world.bentobox.bentobox.api.configuration.WorldSettings;
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.managers.CommandsManager; import world.bentobox.bentobox.managers.CommandsManager;
import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.managers.RanksManagerBeforeClassTest; import world.bentobox.bentobox.util.Util;
/** /**
* @author tastybento * @author tastybento
* *
*/ */
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest({ Bukkit.class, BentoBox.class, IslandsManager.class }) @PrepareForTest({ Bukkit.class, BentoBox.class, IslandsManager.class, Util.class })
public class DefaultPlayerCommandTest extends RanksManagerBeforeClassTest { public class DefaultPlayerCommandTest extends RanksManagerBeforeClassTest {
@Mock @Mock
@ -49,10 +46,6 @@ public class DefaultPlayerCommandTest extends RanksManagerBeforeClassTest {
private WorldSettings ws; private WorldSettings ws;
@Mock @Mock
private User user; private User user;
@Mock
private IslandsManager im;
@Mock
private @Nullable Island island;
class PlayerCommand extends DefaultPlayerCommand { class PlayerCommand extends DefaultPlayerCommand {
@ -68,7 +61,6 @@ public class DefaultPlayerCommandTest extends RanksManagerBeforeClassTest {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); super.setUp();
PowerMockito.mockStatic(IslandsManager.class, Mockito.RETURNS_MOCKS);
// User // User
when(user.getUniqueId()).thenReturn(UUID.randomUUID()); when(user.getUniqueId()).thenReturn(UUID.randomUUID());
@ -85,8 +77,6 @@ public class DefaultPlayerCommandTest extends RanksManagerBeforeClassTest {
when(ws.getDefaultPlayerAction()).thenReturn("go"); when(ws.getDefaultPlayerAction()).thenReturn("go");
when(ws.getDefaultNewPlayerAction()).thenReturn("create"); when(ws.getDefaultNewPlayerAction()).thenReturn("create");
PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS);
when(ws.getPlayerCommandAliases()).thenReturn("island is"); when(ws.getPlayerCommandAliases()).thenReturn("island is");
when(addon.getWorldSettings()).thenReturn(ws); when(addon.getWorldSettings()).thenReturn(ws);
dpc = new PlayerCommand(addon); dpc = new PlayerCommand(addon);

View File

@ -39,27 +39,26 @@ import org.powermock.modules.junit4.PowerMockRunner;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.RanksManagerBeforeClassTest;
import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.Settings;
import world.bentobox.bentobox.api.addons.Addon; import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.api.commands.CompositeCommand; 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.managers.CommandsManager; 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.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.managers.RanksManager;
import world.bentobox.bentobox.managers.RanksManagerBeforeClassTest; import world.bentobox.bentobox.util.Util;
/** /**
* @author tastybento * @author tastybento
* *
*/ */
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest({ Bukkit.class, BentoBox.class }) @PrepareForTest({ Bukkit.class, BentoBox.class, Util.class })
public class IslandBanCommandTest extends RanksManagerBeforeClassTest { public class IslandBanCommandTest extends RanksManagerBeforeClassTest {
@Mock @Mock
@ -68,16 +67,10 @@ public class IslandBanCommandTest extends RanksManagerBeforeClassTest {
@Mock @Mock
private User user; private User user;
@Mock @Mock
private IslandsManager im;
@Mock
private PlayersManager pm; private PlayersManager pm;
@Mock @Mock
private Island island;
@Mock
private Addon addon; private Addon addon;
private IslandBanCommand ibc; private IslandBanCommand ibc;
@Mock
private Player targetPlayer;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
@ -153,13 +146,13 @@ public class IslandBanCommandTest extends RanksManagerBeforeClassTest {
// Target bill - default target. Non Op, online, no ban prevention permission // Target bill - default target. Non Op, online, no ban prevention permission
UUID uuid = UUID.randomUUID(); UUID uuid = UUID.randomUUID();
when(pm.getUUID(anyString())).thenReturn(uuid); when(pm.getUUID(anyString())).thenReturn(uuid);
when(targetPlayer.getName()).thenReturn("bill"); when(mockPlayer.getName()).thenReturn("bill");
when(targetPlayer.getDisplayName()).thenReturn("&Cbill"); when(mockPlayer.getDisplayName()).thenReturn("&Cbill");
when(targetPlayer.getUniqueId()).thenReturn(uuid); when(mockPlayer.getUniqueId()).thenReturn(uuid);
when(targetPlayer.isOp()).thenReturn(false); when(mockPlayer.isOp()).thenReturn(false);
when(targetPlayer.isOnline()).thenReturn(true); when(mockPlayer.isOnline()).thenReturn(true);
when(targetPlayer.hasPermission(anyString())).thenReturn(false); when(mockPlayer.hasPermission(anyString())).thenReturn(false);
User.getInstance(targetPlayer); User.getInstance(mockPlayer);
// Island Ban Command // Island Ban Command
ibc = new IslandBanCommand(ic); ibc = new IslandBanCommand(ic);
@ -242,15 +235,15 @@ public class IslandBanCommandTest extends RanksManagerBeforeClassTest {
@Test @Test
public void testBanOp() { public void testBanOp() {
when(targetPlayer.isOp()).thenReturn(true); when(mockPlayer.isOp()).thenReturn(true);
assertFalse(ibc.canExecute(user, ibc.getLabel(), Collections.singletonList("bill"))); assertFalse(ibc.canExecute(user, ibc.getLabel(), Collections.singletonList("bill")));
verify(user).sendMessage("commands.island.ban.cannot-ban"); verify(user).sendMessage("commands.island.ban.cannot-ban");
} }
@Test @Test
public void testBanOnlineNoBanPermission() { public void testBanOnlineNoBanPermission() {
when(targetPlayer.hasPermission(anyString())).thenReturn(true); when(mockPlayer.hasPermission(anyString())).thenReturn(true);
User.getInstance(targetPlayer); User.getInstance(mockPlayer);
assertFalse(ibc.canExecute(user, ibc.getLabel(), Collections.singletonList("billy"))); assertFalse(ibc.canExecute(user, ibc.getLabel(), Collections.singletonList("billy")));
verify(user).sendMessage("commands.island.ban.cannot-ban"); verify(user).sendMessage("commands.island.ban.cannot-ban");
@ -258,7 +251,7 @@ public class IslandBanCommandTest extends RanksManagerBeforeClassTest {
@Test @Test
public void testBanOfflineUserSuccess() { public void testBanOfflineUserSuccess() {
when(targetPlayer.isOnline()).thenReturn(false); when(mockPlayer.isOnline()).thenReturn(false);
assertTrue(ibc.canExecute(user, ibc.getLabel(), Collections.singletonList("bill"))); assertTrue(ibc.canExecute(user, ibc.getLabel(), Collections.singletonList("bill")));
// Allow adding to ban list // Allow adding to ban list
@ -266,7 +259,7 @@ public class IslandBanCommandTest extends RanksManagerBeforeClassTest {
// Run execute // Run execute
assertTrue(ibc.execute(user, ibc.getLabel(), Collections.singletonList("bill"))); assertTrue(ibc.execute(user, ibc.getLabel(), Collections.singletonList("bill")));
verify(user).sendMessage("commands.island.ban.player-banned", TextVariables.NAME, "bill", TextVariables.DISPLAY_NAME, "&Cbill"); verify(user).sendMessage("commands.island.ban.player-banned", TextVariables.NAME, "bill", TextVariables.DISPLAY_NAME, "&Cbill");
verify(targetPlayer).sendMessage("commands.island.ban.owner-banned-you"); checkSpigotMessage("commands.island.ban.owner-banned-you");
} }
@Test @Test
@ -279,7 +272,7 @@ public class IslandBanCommandTest extends RanksManagerBeforeClassTest {
assertTrue(ibc.execute(user, ibc.getLabel(), Collections.singletonList("bill"))); assertTrue(ibc.execute(user, ibc.getLabel(), Collections.singletonList("bill")));
verify(user).sendMessage("commands.island.ban.player-banned", TextVariables.NAME, "bill", verify(user).sendMessage("commands.island.ban.player-banned", TextVariables.NAME, "bill",
TextVariables.DISPLAY_NAME, "&Cbill"); TextVariables.DISPLAY_NAME, "&Cbill");
verify(targetPlayer).sendMessage("commands.island.ban.owner-banned-you"); checkSpigotMessage("commands.island.ban.owner-banned-you");
} }
@Test @Test
@ -291,8 +284,8 @@ public class IslandBanCommandTest extends RanksManagerBeforeClassTest {
assertFalse(ibc.execute(user, ibc.getLabel(), Collections.singletonList("bill"))); assertFalse(ibc.execute(user, ibc.getLabel(), Collections.singletonList("bill")));
verify(user, never()).sendMessage("commands.island.ban.player-banned", TextVariables.NAME, verify(user, never()).sendMessage("commands.island.ban.player-banned", TextVariables.NAME,
targetPlayer.getName(), TextVariables.DISPLAY_NAME, targetPlayer.getDisplayName()); mockPlayer.getName(), TextVariables.DISPLAY_NAME, mockPlayer.getDisplayName());
verify(targetPlayer, never()).sendMessage("commands.island.ban.owner-banned-you"); verify(mockPlayer, never()).sendMessage("commands.island.ban.owner-banned-you");
} }
@Test @Test

View File

@ -19,7 +19,6 @@ import java.util.Set;
import java.util.UUID; import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.scheduler.BukkitScheduler;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -31,40 +30,31 @@ import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.modules.junit4.PowerMockRunner;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.RanksManagerBeforeClassTest;
import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.Settings;
import world.bentobox.bentobox.api.commands.CompositeCommand; 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.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.PlayersManager; import world.bentobox.bentobox.managers.PlayersManager;
import world.bentobox.bentobox.managers.RanksManager; import world.bentobox.bentobox.managers.RanksManager;
import world.bentobox.bentobox.managers.RanksManagerBeforeClassTest; import world.bentobox.bentobox.util.Util;
/** /**
* @author tastybento * @author tastybento
* *
*/ */
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest({ Bukkit.class, BentoBox.class, User.class }) @PrepareForTest({ Bukkit.class, BentoBox.class, User.class, Util.class })
public class IslandBanlistCommandTest extends RanksManagerBeforeClassTest { public class IslandBanlistCommandTest extends RanksManagerBeforeClassTest {
@Mock @Mock
private CompositeCommand ic; private CompositeCommand ic;
private UUID uuid;
@Mock @Mock
private User user; private User user;
@Mock @Mock
private IslandsManager im;
@Mock
private PlayersManager pm; private PlayersManager pm;
@Mock
private Island island;
/**
*/
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); super.setUp();
@ -78,12 +68,10 @@ public class IslandBanlistCommandTest extends RanksManagerBeforeClassTest {
when(plugin.getSettings()).thenReturn(s); when(plugin.getSettings()).thenReturn(s);
// Player // Player
Player p = mock(Player.class);
// Sometimes use Mockito.withSettings().verboseLogging()
when(user.isOp()).thenReturn(false); when(user.isOp()).thenReturn(false);
uuid = UUID.randomUUID(); uuid = UUID.randomUUID();
when(user.getUniqueId()).thenReturn(uuid); when(user.getUniqueId()).thenReturn(uuid);
when(user.getPlayer()).thenReturn(p); when(user.getPlayer()).thenReturn(mockPlayer);
when(user.getName()).thenReturn("tastybento"); when(user.getName()).thenReturn("tastybento");
when(user.getPermissionValue(anyString(), anyInt())).thenReturn(-1); // Unlimited bans when(user.getPermissionValue(anyString(), anyInt())).thenReturn(-1); // Unlimited bans
when(user.getTranslation(any())).thenAnswer(invocation -> invocation.getArgument(0, String.class)); when(user.getTranslation(any())).thenAnswer(invocation -> invocation.getArgument(0, String.class));
@ -112,9 +100,7 @@ public class IslandBanlistCommandTest extends RanksManagerBeforeClassTest {
when(im.getIsland(any(), any(UUID.class))).thenReturn(island); when(im.getIsland(any(), any(UUID.class))).thenReturn(island);
// IWM friendly name // IWM friendly name
IslandWorldManager iwm = mock(IslandWorldManager.class);
when(iwm.getFriendlyName(any())).thenReturn("BSkyBlock"); when(iwm.getFriendlyName(any())).thenReturn("BSkyBlock");
when(plugin.getIWM()).thenReturn(iwm);
} }

View File

@ -18,59 +18,43 @@ 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.entity.Player;
import org.eclipse.jdt.annotation.NonNull;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
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.Mock;
import org.mockito.Mockito; import org.mockito.Mockito;
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 world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.RanksManagerBeforeClassTest;
import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.Settings;
import world.bentobox.bentobox.api.addons.GameModeAddon; import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.api.commands.CompositeCommand; 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.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.PlayersManager; import world.bentobox.bentobox.managers.PlayersManager;
import world.bentobox.bentobox.managers.RanksManager; import world.bentobox.bentobox.managers.RanksManager;
import world.bentobox.bentobox.managers.RanksManagerBeforeClassTest; import world.bentobox.bentobox.util.Util;
/** /**
* @author tastybento * @author tastybento
* *
*/ */
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest({Bukkit.class, BentoBox.class, User.class }) @PrepareForTest({ Bukkit.class, BentoBox.class, User.class, Util.class })
public class IslandDeletehomeCommandTest extends RanksManagerBeforeClassTest { public class IslandDeletehomeCommandTest extends RanksManagerBeforeClassTest {
@Mock @Mock
private CompositeCommand ic; private CompositeCommand ic;
private UUID uuid;
@Mock @Mock
private User user; private User user;
@Mock @Mock
private IslandsManager im;
@Mock
private PlayersManager pm; private PlayersManager pm;
@Mock
private Island island;
private IslandDeletehomeCommand idh; private IslandDeletehomeCommand idh;
@Mock
private IslandWorldManager iwm;
@Mock
private @NonNull World world;
@Mock
private Location location;
/** /**
* @throws java.lang.Exception * @throws java.lang.Exception
@ -99,13 +83,11 @@ public class IslandDeletehomeCommandTest extends RanksManagerBeforeClassTest {
when(ic.getSubCommand(Mockito.anyString())).thenReturn(Optional.empty()); when(ic.getSubCommand(Mockito.anyString())).thenReturn(Optional.empty());
when(ic.getAddon()).thenReturn(addon); when(ic.getAddon()).thenReturn(addon);
when(ic.getWorld()).thenReturn(world); when(ic.getWorld()).thenReturn(world);
when(plugin.getIslands()).thenReturn(im);
// Player // Player
Player player = mock(Player.class);
when(user.isOp()).thenReturn(false); when(user.isOp()).thenReturn(false);
uuid = UUID.randomUUID(); uuid = UUID.randomUUID();
when(user.getUniqueId()).thenReturn(uuid); when(user.getUniqueId()).thenReturn(uuid);
when(user.getPlayer()).thenReturn(player); when(user.getPlayer()).thenReturn(mockPlayer);
when(user.getName()).thenReturn("tastybento"); when(user.getName()).thenReturn("tastybento");
when(user.getWorld()).thenReturn(world); when(user.getWorld()).thenReturn(world);
when(user.getTranslation(anyString())).thenAnswer(i -> i.getArgument(0, String.class)); when(user.getTranslation(anyString())).thenAnswer(i -> i.getArgument(0, String.class));
@ -131,10 +113,6 @@ public class IslandDeletehomeCommandTest extends RanksManagerBeforeClassTest {
when(iwm.isEnd(any())).thenReturn(false); when(iwm.isEnd(any())).thenReturn(false);
// Number of homes default // Number of homes default
when(iwm.getMaxHomes(any())).thenReturn(3); when(iwm.getMaxHomes(any())).thenReturn(3);
when(plugin.getIWM()).thenReturn(iwm);
// Bukkit
PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS);
idh = new IslandDeletehomeCommand(ic); idh = new IslandDeletehomeCommand(ic);
} }

View File

@ -30,15 +30,14 @@ 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.Mock;
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 com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.RanksManagerBeforeClassTest;
import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.Settings;
import world.bentobox.bentobox.TestWorldSettings; import world.bentobox.bentobox.TestWorldSettings;
import world.bentobox.bentobox.api.addons.Addon; import world.bentobox.bentobox.api.addons.Addon;
@ -48,20 +47,18 @@ 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.CommandsManager; import world.bentobox.bentobox.managers.CommandsManager;
import world.bentobox.bentobox.managers.IslandWorldManager;
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.managers.RanksManager;
import world.bentobox.bentobox.managers.RanksManagerBeforeClassTest; import world.bentobox.bentobox.util.Util;
/** /**
* @author tastybento * @author tastybento
* *
*/ */
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest({ Bukkit.class, BentoBox.class, User.class }) @PrepareForTest({ Bukkit.class, BentoBox.class, User.class, Util.class })
public class IslandExpelCommandTest extends RanksManagerBeforeClassTest { public class IslandExpelCommandTest extends RanksManagerBeforeClassTest {
@Mock @Mock
@ -70,23 +67,15 @@ public class IslandExpelCommandTest extends RanksManagerBeforeClassTest {
@Mock @Mock
private User user; private User user;
@Mock @Mock
private IslandsManager im;
@Mock
private PlayersManager pm; private PlayersManager pm;
@Mock @Mock
private Island island;
@Mock
private LocalesManager lm; private LocalesManager lm;
@Mock @Mock
private IslandWorldManager iwm;
@Mock
private Addon addon; private Addon addon;
private IslandExpelCommand iec; private IslandExpelCommand iec;
@Mock @Mock
private Server server; private Server server;
@Mock
private Player p;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
@ -108,10 +97,11 @@ public class IslandExpelCommandTest extends RanksManagerBeforeClassTest {
uuid = UUID.randomUUID(); uuid = UUID.randomUUID();
when(user.getUniqueId()).thenReturn(uuid); when(user.getUniqueId()).thenReturn(uuid);
when(server.getOnlinePlayers()).thenReturn(Collections.emptySet()); when(server.getOnlinePlayers()).thenReturn(Collections.emptySet());
when(p.getServer()).thenReturn(server); when(mockPlayer.getServer()).thenReturn(server);
when(user.getPlayer()).thenReturn(p); when(user.getPlayer()).thenReturn(mockPlayer);
when(user.getName()).thenReturn("tastybento"); when(user.getName()).thenReturn("tastybento");
when(user.getDisplayName()).thenReturn("&Ctastybento"); when(user.getDisplayName()).thenReturn("&Ctastybento");
when(user.isOnline()).thenReturn(true);
when(user.getTranslation(any())).thenAnswer(invocation -> invocation.getArgument(0, String.class)); when(user.getTranslation(any())).thenAnswer(invocation -> invocation.getArgument(0, String.class));
// Parent command has no aliases // Parent command has no aliases
@ -130,7 +120,6 @@ public class IslandExpelCommandTest extends RanksManagerBeforeClassTest {
// Server & Scheduler // Server & Scheduler
BukkitScheduler sch = mock(BukkitScheduler.class); BukkitScheduler sch = mock(BukkitScheduler.class);
PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS);
when(Bukkit.getScheduler()).thenReturn(sch); when(Bukkit.getScheduler()).thenReturn(sch);
// Island Banned list initialization // Island Banned list initialization
@ -296,7 +285,7 @@ public class IslandExpelCommandTest extends RanksManagerBeforeClassTest {
public void testCanExecuteInvisiblePlayer() { public void testCanExecuteInvisiblePlayer() {
when(im.hasIsland(any(), any(User.class))).thenReturn(true); when(im.hasIsland(any(), any(User.class))).thenReturn(true);
Player t = setUpTarget(); Player t = setUpTarget();
when(p.canSee(t)).thenReturn(false); when(mockPlayer.canSee(t)).thenReturn(false);
//when(im.getMembers(any(), any())).thenReturn(Collections.emptySet()); //when(im.getMembers(any(), any())).thenReturn(Collections.emptySet());
assertFalse(iec.canExecute(user, "", Collections.singletonList("tasty"))); assertFalse(iec.canExecute(user, "", Collections.singletonList("tasty")));
verify(user).sendMessage("general.errors.offline-player"); verify(user).sendMessage("general.errors.offline-player");
@ -365,10 +354,11 @@ public class IslandExpelCommandTest extends RanksManagerBeforeClassTest {
when(t.getName()).thenReturn("target"); when(t.getName()).thenReturn("target");
when(t.getDisplayName()).thenReturn("&Ctarget"); when(t.getDisplayName()).thenReturn("&Ctarget");
when(t.getServer()).thenReturn(server); when(t.getServer()).thenReturn(server);
when(t.spigot()).thenReturn(spigot);
when(server.getOnlinePlayers()).thenReturn(Collections.emptySet()); when(server.getOnlinePlayers()).thenReturn(Collections.emptySet());
User.getInstance(t); User.getInstance(t);
when(pm.getUUID(anyString())).thenReturn(target); when(pm.getUUID(anyString())).thenReturn(target);
when(p.canSee(t)).thenReturn(true); when(mockPlayer.canSee(t)).thenReturn(true);
when(Bukkit.getPlayer(target)).thenReturn(t); when(Bukkit.getPlayer(target)).thenReturn(t);
return t; return t;
} }
@ -472,20 +462,25 @@ public class IslandExpelCommandTest extends RanksManagerBeforeClassTest {
List<Player> list = new ArrayList<>(); List<Player> list = new ArrayList<>();
Player p1 = mock(Player.class); Player p1 = mock(Player.class);
when(p1.getName()).thenReturn("normal"); when(p1.getName()).thenReturn("normal");
when(p.canSee(p1)).thenReturn(true); when(p1.spigot()).thenReturn(spigot);
when(mockPlayer.canSee(p1)).thenReturn(true);
Player p2 = mock(Player.class); Player p2 = mock(Player.class);
when(p2.getName()).thenReturn("op"); when(p2.getName()).thenReturn("op");
when(p.canSee(p2)).thenReturn(true); when(p2.spigot()).thenReturn(spigot);
when(mockPlayer.canSee(p2)).thenReturn(true);
when(p2.isOp()).thenReturn(true); when(p2.isOp()).thenReturn(true);
Player p3 = mock(Player.class); Player p3 = mock(Player.class);
when(p3.getName()).thenReturn("invisible"); when(p3.getName()).thenReturn("invisible");
when(p3.spigot()).thenReturn(spigot);
Player p4 = mock(Player.class); Player p4 = mock(Player.class);
when(p4.getName()).thenReturn("adminPerm"); when(p4.getName()).thenReturn("adminPerm");
when(p.canSee(p4)).thenReturn(true); when(p4.spigot()).thenReturn(spigot);
when(mockPlayer.canSee(p4)).thenReturn(true);
when(p4.hasPermission(eq("bskyblock.admin.noexpel"))).thenReturn(true); when(p4.hasPermission(eq("bskyblock.admin.noexpel"))).thenReturn(true);
Player p5 = mock(Player.class); Player p5 = mock(Player.class);
when(p5.getName()).thenReturn("modPerm"); when(p5.getName()).thenReturn("modPerm");
when(p.canSee(p5)).thenReturn(true); when(p5.spigot()).thenReturn(spigot);
when(mockPlayer.canSee(p5)).thenReturn(true);
when(p5.hasPermission(eq("bskyblock.mod.bypassexpel"))).thenReturn(true); when(p5.hasPermission(eq("bskyblock.mod.bypassexpel"))).thenReturn(true);
list.add(p1); list.add(p1);
list.add(p2); list.add(p2);

View File

@ -24,7 +24,6 @@ import org.bukkit.GameMode;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.scheduler.BukkitScheduler;
@ -42,8 +41,8 @@ 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;
import org.powermock.reflect.Whitebox;
import world.bentobox.bentobox.AbstractCommonSetup;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.Settings;
import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.commands.CompositeCommand;
@ -51,7 +50,6 @@ import world.bentobox.bentobox.api.configuration.WorldSettings;
import world.bentobox.bentobox.api.flags.Flag; import world.bentobox.bentobox.api.flags.Flag;
import world.bentobox.bentobox.api.user.Notifier; import world.bentobox.bentobox.api.user.Notifier;
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.lists.Flags; import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.managers.CommandsManager; import world.bentobox.bentobox.managers.CommandsManager;
import world.bentobox.bentobox.managers.IslandWorldManager; import world.bentobox.bentobox.managers.IslandWorldManager;
@ -69,22 +67,18 @@ import world.bentobox.bentobox.util.Util;
*/ */
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest({ Bukkit.class, BentoBox.class, Util.class }) @PrepareForTest({ Bukkit.class, BentoBox.class, Util.class })
public class IslandGoCommandTest { public class IslandGoCommandTest extends AbstractCommonSetup {
@Mock @Mock
private CompositeCommand ic; private CompositeCommand ic;
private User user; private User user;
@Mock @Mock
private IslandsManager im; private IslandsManager im;
@Mock @Mock
private Island island;
@Mock
private PluginManager pim; private PluginManager pim;
@Mock @Mock
private Settings s; private Settings s;
@Mock @Mock
private BukkitTask task; private BukkitTask task;
@Mock
private Player player;
private IslandGoCommand igc; private IslandGoCommand igc;
@Mock @Mock
private Notifier notifier; private Notifier notifier;
@ -97,10 +91,7 @@ public class IslandGoCommandTest {
*/ */
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); super.setUp();
// Set up plugin
BentoBox plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
// Command manager // Command manager
CommandsManager cm = mock(CommandsManager.class); CommandsManager cm = mock(CommandsManager.class);
@ -110,11 +101,11 @@ public class IslandGoCommandTest {
when(plugin.getSettings()).thenReturn(s); when(plugin.getSettings()).thenReturn(s);
// Player // Player
when(player.isOp()).thenReturn(false); when(mockPlayer.isOp()).thenReturn(false);
when(player.getUniqueId()).thenReturn(uuid); when(mockPlayer.getUniqueId()).thenReturn(uuid);
when(player.getName()).thenReturn("tastybento"); when(mockPlayer.getName()).thenReturn("tastybento");
when(player.getWorld()).thenReturn(world); when(mockPlayer.getWorld()).thenReturn(world);
user = User.getInstance(player); user = User.getInstance(mockPlayer);
// Set the User class plugin as this one // Set the User class plugin as this one
User.setPlugin(plugin); User.setPlugin(plugin);
@ -197,7 +188,7 @@ public class IslandGoCommandTest {
public void testExecuteMidTeleport() { public void testExecuteMidTeleport() {
when(im.isGoingHome(user)).thenReturn(true); when(im.isGoingHome(user)).thenReturn(true);
assertFalse(igc.canExecute(user, igc.getLabel(), Collections.emptyList())); assertFalse(igc.canExecute(user, igc.getLabel(), Collections.emptyList()));
verify(player).sendMessage("commands.island.go.in-progress"); checkSpigotMessage("commands.island.go.in-progress");
} }
/** /**
@ -207,7 +198,7 @@ public class IslandGoCommandTest {
public void testExecuteNoArgsNoIsland() { public void testExecuteNoArgsNoIsland() {
when(im.getIslands(world, uuid)).thenReturn(List.of()); when(im.getIslands(world, uuid)).thenReturn(List.of());
assertFalse(igc.canExecute(user, igc.getLabel(), Collections.emptyList())); assertFalse(igc.canExecute(user, igc.getLabel(), Collections.emptyList()));
verify(player).sendMessage("general.errors.no-island"); checkSpigotMessage("general.errors.no-island");
} }
/** /**
@ -248,9 +239,9 @@ public class IslandGoCommandTest {
@Test @Test
public void testExecuteNoArgsNoTeleportWhenFalling() { public void testExecuteNoArgsNoTeleportWhenFalling() {
Flags.PREVENT_TELEPORT_WHEN_FALLING.setSetting(world, true); Flags.PREVENT_TELEPORT_WHEN_FALLING.setSetting(world, true);
when(player.getFallDistance()).thenReturn(10F); when(mockPlayer.getFallDistance()).thenReturn(10F);
assertFalse(igc.canExecute(user, igc.getLabel(), Collections.emptyList())); assertFalse(igc.canExecute(user, igc.getLabel(), Collections.emptyList()));
verify(player).sendMessage(eq("protection.flags.PREVENT_TELEPORT_WHEN_FALLING.hint")); checkSpigotMessage("protection.flags.PREVENT_TELEPORT_WHEN_FALLING.hint");
} }
/** /**
@ -259,7 +250,7 @@ public class IslandGoCommandTest {
@Test @Test
public void testExecuteNoArgsNoTeleportWhenFallingNotFalling() { public void testExecuteNoArgsNoTeleportWhenFallingNotFalling() {
Flags.PREVENT_TELEPORT_WHEN_FALLING.setSetting(world, true); Flags.PREVENT_TELEPORT_WHEN_FALLING.setSetting(world, true);
when(player.getFallDistance()).thenReturn(0F); when(mockPlayer.getFallDistance()).thenReturn(0F);
assertTrue(igc.canExecute(user, igc.getLabel(), Collections.emptyList())); assertTrue(igc.canExecute(user, igc.getLabel(), Collections.emptyList()));
} }
@ -279,9 +270,9 @@ public class IslandGoCommandTest {
@Test @Test
public void testExecuteArgs1MultipleHomes() { public void testExecuteArgs1MultipleHomes() {
assertFalse(igc.execute(user, igc.getLabel(), Collections.singletonList("1"))); assertFalse(igc.execute(user, igc.getLabel(), Collections.singletonList("1")));
verify(player).sendMessage("commands.island.go.unknown-home"); checkSpigotMessage("commands.island.go.unknown-home");
verify(player).sendMessage("commands.island.sethome.homes-are"); checkSpigotMessage("commands.island.sethome.homes-are");
verify(player).sendMessage("commands.island.sethome.home-list-syntax"); checkSpigotMessage("commands.island.sethome.home-list-syntax");
} }
/** /**
@ -292,7 +283,7 @@ public class IslandGoCommandTest {
when(s.getDelayTime()).thenReturn(10); when(s.getDelayTime()).thenReturn(10);
assertTrue(igc.execute(user, igc.getLabel(), Collections.emptyList())); assertTrue(igc.execute(user, igc.getLabel(), Collections.emptyList()));
verify(player).sendMessage(eq("commands.delay.stand-still")); checkSpigotMessage("commands.delay.stand-still");
} }
/** /**
@ -306,8 +297,8 @@ public class IslandGoCommandTest {
// Twice // Twice
assertTrue(igc.execute(user, igc.getLabel(), Collections.emptyList())); assertTrue(igc.execute(user, igc.getLabel(), Collections.emptyList()));
verify(task).cancel(); verify(task).cancel();
verify(player).sendMessage(eq("commands.delay.previous-command-cancelled")); checkSpigotMessage("commands.delay.previous-command-cancelled");
verify(player, Mockito.times(2)).sendMessage(eq("commands.delay.stand-still")); checkSpigotMessage("commands.delay.stand-still", 2);
} }
/** /**
@ -318,10 +309,10 @@ public class IslandGoCommandTest {
Location l = mock(Location.class); Location l = mock(Location.class);
Vector vector = mock(Vector.class); Vector vector = mock(Vector.class);
when(l.toVector()).thenReturn(vector); when(l.toVector()).thenReturn(vector);
when(player.getLocation()).thenReturn(l); when(mockPlayer.getLocation()).thenReturn(l);
PlayerMoveEvent e = new PlayerMoveEvent(player, l, l); PlayerMoveEvent e = new PlayerMoveEvent(mockPlayer, l, l);
igc.onPlayerMove(e); igc.onPlayerMove(e);
verify(player, Mockito.never()).sendMessage(eq("commands.delay.moved-so-command-cancelled")); verify(mockPlayer, Mockito.never()).sendMessage(eq("commands.delay.moved-so-command-cancelled"));
} }
/** /**
@ -332,11 +323,11 @@ public class IslandGoCommandTest {
Location l = mock(Location.class); Location l = mock(Location.class);
Vector vector = mock(Vector.class); Vector vector = mock(Vector.class);
when(l.toVector()).thenReturn(vector); when(l.toVector()).thenReturn(vector);
when(player.getLocation()).thenReturn(l); when(mockPlayer.getLocation()).thenReturn(l);
testExecuteNoArgsDelay(); testExecuteNoArgsDelay();
PlayerMoveEvent e = new PlayerMoveEvent(player, l, l); PlayerMoveEvent e = new PlayerMoveEvent(mockPlayer, l, l);
igc.onPlayerMove(e); igc.onPlayerMove(e);
verify(player, Mockito.never()).sendMessage(eq("commands.delay.moved-so-command-cancelled")); checkSpigotMessage("commands.delay.moved-so-command-cancelled", 0);
} }
/** /**
@ -347,12 +338,12 @@ public class IslandGoCommandTest {
Location l = mock(Location.class); Location l = mock(Location.class);
Vector vector = mock(Vector.class); Vector vector = mock(Vector.class);
when(l.toVector()).thenReturn(vector); when(l.toVector()).thenReturn(vector);
when(player.getLocation()).thenReturn(l); when(mockPlayer.getLocation()).thenReturn(l);
testExecuteNoArgsDelay(); testExecuteNoArgsDelay();
Location l2 = mock(Location.class); Location l2 = mock(Location.class);
Vector vector2 = mock(Vector.class); Vector vector2 = mock(Vector.class);
when(l2.toVector()).thenReturn(vector2); when(l2.toVector()).thenReturn(vector2);
PlayerMoveEvent e = new PlayerMoveEvent(player, l, l2); PlayerMoveEvent e = new PlayerMoveEvent(mockPlayer, l, l2);
igc.onPlayerMove(e); igc.onPlayerMove(e);
verify(notifier).notify(any(), eq("commands.delay.moved-so-command-cancelled")); verify(notifier).notify(any(), eq("commands.delay.moved-so-command-cancelled"));
} }

View File

@ -17,33 +17,27 @@ 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.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import org.eclipse.jdt.annotation.NonNull;
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.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 world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.RanksManagerBeforeClassTest;
import world.bentobox.bentobox.api.commands.CompositeCommand; 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.RanksManagerBeforeClassTest;
import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.Util;
/** /**
@ -59,49 +53,29 @@ public class IslandInfoCommandTest extends RanksManagerBeforeClassTest {
@Mock @Mock
private User user; private User user;
@Mock @Mock
private IslandsManager im;
@Mock
private PlayersManager pm; private PlayersManager pm;
@Mock
private PlaceholdersManager phm;
private Island island; private Island island;
private IslandInfoCommand iic; private IslandInfoCommand iic;
@Mock
private Player player;
@Mock
private World world;
@Mock
private PlaceholdersManager phm;
@Mock
private @NonNull Location location;
@Mock
private IslandWorldManager iwm;
/**
*/
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); super.setUp();
PowerMockito.mockStatic(IslandsManager.class, Mockito.RETURNS_MOCKS);
// IWM
when(plugin.getIWM()).thenReturn(iwm);
// 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
when(player.isOp()).thenReturn(false); when(mockPlayer.isOp()).thenReturn(false);
UUID uuid = UUID.randomUUID();
when(user.getUniqueId()).thenReturn(uuid); when(user.getUniqueId()).thenReturn(uuid);
when(user.getName()).thenReturn("tastybento"); when(user.getName()).thenReturn("tastybento");
when(user.getWorld()).thenReturn(world); when(user.getWorld()).thenReturn(world);
when(user.getPlayer()).thenReturn(player); when(user.getPlayer()).thenReturn(mockPlayer);
when(user.isPlayer()).thenReturn(true); when(user.isPlayer()).thenReturn(true);
//user = User.getInstance(player); //user = User.getInstance(player);
// Set the User class plugin as this one // Set the User class plugin as this one
@ -163,6 +137,7 @@ public class IslandInfoCommandTest extends RanksManagerBeforeClassTest {
public void testExecuteUserStringListOfStringNoArgsConsole() { public void testExecuteUserStringListOfStringNoArgsConsole() {
CommandSender console = mock(CommandSender.class); CommandSender console = mock(CommandSender.class);
User sender = User.getInstance(console); User sender = User.getInstance(console);
when(console.spigot()).thenReturn(spigot);
assertFalse(iic.execute(sender, "", Collections.emptyList())); assertFalse(iic.execute(sender, "", Collections.emptyList()));
verify(user, never()).sendMessage("commands.help.header", "[label]", "commands.help.console"); verify(user, never()).sendMessage("commands.help.header", "[label]", "commands.help.console");
} }

View File

@ -18,12 +18,10 @@ 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.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.scheduler.BukkitScheduler;
import org.bukkit.scheduler.BukkitTask; import org.bukkit.scheduler.BukkitTask;
import org.eclipse.jdt.annotation.Nullable;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -34,11 +32,11 @@ 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;
import org.powermock.reflect.Whitebox;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSet.Builder; import com.google.common.collect.ImmutableSet.Builder;
import world.bentobox.bentobox.AbstractCommonSetup;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.Settings;
import world.bentobox.bentobox.api.addons.AddonDescription; import world.bentobox.bentobox.api.addons.AddonDescription;
@ -50,20 +48,20 @@ 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.BlueprintsManager; import world.bentobox.bentobox.managers.BlueprintsManager;
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.island.NewIsland; import world.bentobox.bentobox.managers.island.NewIsland;
import world.bentobox.bentobox.util.Util;
/** /**
* @author tastybento * @author tastybento
* *
*/ */
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest({ Bukkit.class, BentoBox.class, NewIsland.class, IslandsManager.class }) @PrepareForTest({ Bukkit.class, BentoBox.class, NewIsland.class, IslandsManager.class, Util.class })
public class IslandResetCommandTest { public class IslandResetCommandTest extends AbstractCommonSetup {
@Mock @Mock
private CompositeCommand ic; private CompositeCommand ic;
@ -72,37 +70,22 @@ public class IslandResetCommandTest {
@Mock @Mock
private Settings s; private Settings s;
@Mock @Mock
private IslandsManager im;
@Mock
private PlayersManager pm; private PlayersManager pm;
@Mock @Mock
private World world;
@Mock
private IslandWorldManager iwm;
@Mock
private BlueprintsManager bpm; private BlueprintsManager bpm;
@Mock @Mock
private @Nullable Island island;
@Mock
private PluginManager pim; private PluginManager pim;
private IslandResetCommand irc; private IslandResetCommand irc;
@Mock
private Player pp;
private UUID uuid; private UUID uuid;
/** /**
*/ */
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); super.setUp();
PowerMockito.mockStatic(IslandsManager.class, Mockito.RETURNS_MOCKS); PowerMockito.mockStatic(IslandsManager.class, Mockito.RETURNS_MOCKS);
// Set up plugin
BentoBox plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
// Command manager // Command manager
CommandsManager cm = mock(CommandsManager.class); CommandsManager cm = mock(CommandsManager.class);
@ -115,8 +98,10 @@ public class IslandResetCommandTest {
// Player // Player
Player p = mock(Player.class); Player p = mock(Player.class);
when(p.getUniqueId()).thenReturn(uuid); when(p.getUniqueId()).thenReturn(uuid);
User.getInstance(p); when(p.spigot()).thenReturn(spigot);
when(p.isOnline()).thenReturn(true); when(p.isOnline()).thenReturn(true);
User.getInstance(p);
// User // User
User.setPlugin(plugin); User.setPlugin(plugin);
@ -170,8 +155,8 @@ public class IslandResetCommandTest {
// Put a team on the island // Put a team on the island
for (int j = 0; j < 11; j++) { for (int j = 0; j < 11; j++) {
UUID temp = UUID.randomUUID(); UUID temp = UUID.randomUUID();
when(pp.getUniqueId()).thenReturn(temp); when(mockPlayer.getUniqueId()).thenReturn(temp);
User.getInstance(pp); User.getInstance(mockPlayer);
members.add(temp); members.add(temp);
} }
when(island.getMemberSet()).thenReturn(members.build()); when(island.getMemberSet()).thenReturn(members.build());
@ -266,7 +251,7 @@ public class IslandResetCommandTest {
verify(user).sendMessage("commands.island.create.creating-island"); verify(user).sendMessage("commands.island.create.creating-island");
verify(user, never()).sendMessage(eq("commands.island.reset.kicked-from-island"), eq(TextVariables.GAMEMODE), anyString()); verify(user, never()).sendMessage(eq("commands.island.reset.kicked-from-island"), eq(TextVariables.GAMEMODE), anyString());
// Only 11 because the leader should not see this // Only 11 because the leader should not see this
verify(pp, times(11)).sendMessage("commands.island.reset.kicked-from-island"); checkSpigotMessage("commands.island.reset.kicked-from-island", 11);
} }
@After @After

View File

@ -5,10 +5,7 @@ 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.when; import static org.mockito.Mockito.when;
import java.util.Collections; import java.util.Collections;
@ -19,7 +16,6 @@ import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.scheduler.BukkitScheduler;
import org.bukkit.scheduler.BukkitTask; import org.bukkit.scheduler.BukkitTask;
@ -31,11 +27,10 @@ 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 world.bentobox.bentobox.AbstractCommonSetup;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.Settings;
import world.bentobox.bentobox.api.addons.GameModeAddon; import world.bentobox.bentobox.api.addons.GameModeAddon;
@ -43,37 +38,26 @@ 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.user.User; import world.bentobox.bentobox.api.user.User;
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.LocalesManager; import world.bentobox.bentobox.managers.LocalesManager;
import world.bentobox.bentobox.managers.PlaceholdersManager; import world.bentobox.bentobox.managers.PlaceholdersManager;
import world.bentobox.bentobox.util.Util;
/** /**
* @author tastybento * @author tastybento
* *
*/ */
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest({Bukkit.class, BentoBox.class}) @PrepareForTest({ Bukkit.class, BentoBox.class, Util.class })
public class IslandSpawnCommandTest { public class IslandSpawnCommandTest extends AbstractCommonSetup {
@Mock
private BentoBox plugin;
@Mock
private IslandsManager im;
@Mock @Mock
private CompositeCommand ic; private CompositeCommand ic;
private IslandSpawnCommand isc; private IslandSpawnCommand isc;
@Mock
private IslandWorldManager iwm;
private @Nullable User user; private @Nullable User user;
@Mock @Mock
private World world;
@Mock
private @Nullable WorldSettings ws; private @Nullable WorldSettings ws;
private Map<String, Boolean> map; private Map<String, Boolean> map;
@Mock @Mock
private Player player;
@Mock
private BukkitTask task; private BukkitTask task;
@Mock @Mock
private PluginManager pim; private PluginManager pim;
@ -84,22 +68,21 @@ public class IslandSpawnCommandTest {
*/ */
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
// Set up plugin super.setUp();
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
// 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
when(player.isOp()).thenReturn(false); when(mockPlayer.isOp()).thenReturn(false);
UUID uuid = UUID.randomUUID(); UUID uuid = UUID.randomUUID();
when(player.getUniqueId()).thenReturn(uuid); when(mockPlayer.getUniqueId()).thenReturn(uuid);
when(player.hasPermission(anyString())).thenReturn(true); when(mockPlayer.hasPermission(anyString())).thenReturn(true);
when(player.getWorld()).thenReturn(world); when(mockPlayer.getWorld()).thenReturn(world);
User.setPlugin(plugin); User.setPlugin(plugin);
// Set up user already // Set up user already
user = User.getInstance(player); user = User.getInstance(mockPlayer);
// Addon // Addon
GameModeAddon addon = mock(GameModeAddon.class); GameModeAddon addon = mock(GameModeAddon.class);
@ -116,7 +99,6 @@ public class IslandSpawnCommandTest {
// Server & Scheduler // Server & Scheduler
BukkitScheduler sch = mock(BukkitScheduler.class); BukkitScheduler sch = mock(BukkitScheduler.class);
PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS);
when(Bukkit.getScheduler()).thenReturn(sch); when(Bukkit.getScheduler()).thenReturn(sch);
when(sch.runTaskLater(any(), any(Runnable.class), any(Long.class))).thenReturn(task); when(sch.runTaskLater(any(), any(Runnable.class), any(Long.class))).thenReturn(task);
@ -187,11 +169,11 @@ public class IslandSpawnCommandTest {
*/ */
@Test @Test
public void testExecuteUserStringListOfStringInWorldNoTeleportFalling() { public void testExecuteUserStringListOfStringInWorldNoTeleportFalling() {
when(player.getFallDistance()).thenReturn(10F); when(mockPlayer.getFallDistance()).thenReturn(10F);
map.put("PREVENT_TELEPORT_WHEN_FALLING", true); map.put("PREVENT_TELEPORT_WHEN_FALLING", true);
when(iwm.inWorld(any(World.class))).thenReturn(true); when(iwm.inWorld(any(World.class))).thenReturn(true);
assertFalse(isc.execute(user, "spawn", Collections.emptyList())); assertFalse(isc.execute(user, "spawn", Collections.emptyList()));
verify(player).sendMessage(eq("protection.flags.PREVENT_TELEPORT_WHEN_FALLING.hint")); checkSpigotMessage("protection.flags.PREVENT_TELEPORT_WHEN_FALLING.hint");
} }
/** /**
@ -199,11 +181,11 @@ public class IslandSpawnCommandTest {
*/ */
@Test @Test
public void testExecuteUserStringListOfStringInWorldTeleportOkFalling() { public void testExecuteUserStringListOfStringInWorldTeleportOkFalling() {
when(player.getFallDistance()).thenReturn(10F); when(mockPlayer.getFallDistance()).thenReturn(10F);
map.put("PREVENT_TELEPORT_WHEN_FALLING", false); map.put("PREVENT_TELEPORT_WHEN_FALLING", false);
when(iwm.inWorld(any(World.class))).thenReturn(true); when(iwm.inWorld(any(World.class))).thenReturn(true);
assertTrue(isc.execute(user, "spawn", Collections.emptyList())); assertTrue(isc.execute(user, "spawn", Collections.emptyList()));
verify(player, never()).sendMessage(eq("protection.flags.PREVENT_TELEPORT_WHEN_FALLING.hint")); checkSpigotMessage("protection.flags.PREVENT_TELEPORT_WHEN_FALLING.hint", 0);
} }
/** /**
@ -211,11 +193,11 @@ public class IslandSpawnCommandTest {
*/ */
@Test @Test
public void testExecuteUserStringListOfStringWrongWorldTeleportOkFalling() { public void testExecuteUserStringListOfStringWrongWorldTeleportOkFalling() {
when(player.getFallDistance()).thenReturn(10F); when(mockPlayer.getFallDistance()).thenReturn(10F);
map.put("PREVENT_TELEPORT_WHEN_FALLING", true); map.put("PREVENT_TELEPORT_WHEN_FALLING", true);
when(iwm.inWorld(any(World.class))).thenReturn(false); when(iwm.inWorld(any(World.class))).thenReturn(false);
assertTrue(isc.execute(user, "spawn", Collections.emptyList())); assertTrue(isc.execute(user, "spawn", Collections.emptyList()));
verify(player, never()).sendMessage(eq("protection.flags.PREVENT_TELEPORT_WHEN_FALLING.hint")); checkSpigotMessage("protection.flags.PREVENT_TELEPORT_WHEN_FALLING.hint", 0);
} }
/** /**
@ -223,11 +205,11 @@ public class IslandSpawnCommandTest {
*/ */
@Test @Test
public void testExecuteUserStringListOfStringInWorldTeleportNotFalling() { public void testExecuteUserStringListOfStringInWorldTeleportNotFalling() {
when(player.getFallDistance()).thenReturn(0F); when(mockPlayer.getFallDistance()).thenReturn(0F);
map.put("PREVENT_TELEPORT_WHEN_FALLING", true); map.put("PREVENT_TELEPORT_WHEN_FALLING", true);
when(iwm.inWorld(any(World.class))).thenReturn(true); when(iwm.inWorld(any(World.class))).thenReturn(true);
assertTrue(isc.execute(user, "spawn", Collections.emptyList())); assertTrue(isc.execute(user, "spawn", Collections.emptyList()));
verify(player, never()).sendMessage(eq("protection.flags.PREVENT_TELEPORT_WHEN_FALLING.hint")); checkSpigotMessage("protection.flags.PREVENT_TELEPORT_WHEN_FALLING.hint", 0);
} }
} }

View File

@ -21,8 +21,6 @@ import java.util.Set;
import java.util.UUID; import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager;
import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.scheduler.BukkitScheduler;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -34,40 +32,31 @@ import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.modules.junit4.PowerMockRunner;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.RanksManagerBeforeClassTest;
import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.Settings;
import world.bentobox.bentobox.api.commands.CompositeCommand; 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.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.PlayersManager; import world.bentobox.bentobox.managers.PlayersManager;
import world.bentobox.bentobox.managers.RanksManager; import world.bentobox.bentobox.managers.RanksManager;
import world.bentobox.bentobox.managers.RanksManagerBeforeClassTest; import world.bentobox.bentobox.util.Util;
/** /**
* @author tastybento * @author tastybento
* *
*/ */
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest({ Bukkit.class, BentoBox.class, User.class }) @PrepareForTest({ Bukkit.class, BentoBox.class, User.class, Util.class })
public class IslandUnbanCommandTest extends RanksManagerBeforeClassTest { public class IslandUnbanCommandTest extends RanksManagerBeforeClassTest {
@Mock @Mock
private CompositeCommand ic; private CompositeCommand ic;
private UUID uuid;
@Mock @Mock
private User user; private User user;
@Mock @Mock
private IslandsManager im;
@Mock
private PlayersManager pm; private PlayersManager pm;
@Mock
private Island island;
/**
*/
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); super.setUp();
@ -81,13 +70,11 @@ public class IslandUnbanCommandTest extends RanksManagerBeforeClassTest {
Settings s = mock(Settings.class); Settings s = mock(Settings.class);
when(plugin.getSettings()).thenReturn(s); when(plugin.getSettings()).thenReturn(s);
// Player
Player p = mock(Player.class);
// User // User
when(user.isOp()).thenReturn(false); when(user.isOp()).thenReturn(false);
uuid = UUID.randomUUID(); uuid = UUID.randomUUID();
when(user.getUniqueId()).thenReturn(uuid); when(user.getUniqueId()).thenReturn(uuid);
when(user.getPlayer()).thenReturn(p); when(user.getPlayer()).thenReturn(mockPlayer);
when(user.getName()).thenReturn("tastybento"); when(user.getName()).thenReturn("tastybento");
when(user.getDisplayName()).thenReturn("&Ctastybento"); when(user.getDisplayName()).thenReturn("&Ctastybento");
when(user.getTranslation(any())).thenAnswer(invocation -> invocation.getArgument(0, String.class)); when(user.getTranslation(any())).thenAnswer(invocation -> invocation.getArgument(0, String.class));
@ -97,8 +84,6 @@ public class IslandUnbanCommandTest extends RanksManagerBeforeClassTest {
// No island for player to begin with (set it later in the tests) // No island for player to begin with (set it later in the tests)
when(im.hasIsland(any(), eq(uuid))).thenReturn(false); when(im.hasIsland(any(), eq(uuid))).thenReturn(false);
// when(im.isOwner(any(), eq(uuid))).thenReturn(false);
when(plugin.getIslands()).thenReturn(im);
// Has team // Has team
when(im.inTeam(any(), eq(uuid))).thenReturn(true); when(im.inTeam(any(), eq(uuid))).thenReturn(true);
@ -116,12 +101,9 @@ public class IslandUnbanCommandTest extends RanksManagerBeforeClassTest {
when(im.getIsland(any(), any(UUID.class))).thenReturn(island); when(im.getIsland(any(), any(UUID.class))).thenReturn(island);
// IWM friendly name // IWM friendly name
IslandWorldManager iwm = mock(IslandWorldManager.class);
when(iwm.getFriendlyName(any())).thenReturn("BSkyBlock"); when(iwm.getFriendlyName(any())).thenReturn("BSkyBlock");
when(plugin.getIWM()).thenReturn(iwm);
// Server and Plugin Manager for events // Server and Plugin Manager for events
PluginManager pim = mock(PluginManager.class);
when(Bukkit.getPluginManager()).thenReturn(pim); when(Bukkit.getPluginManager()).thenReturn(pim);
} }

View File

@ -18,37 +18,31 @@ import java.util.Collections;
import java.util.UUID; import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.plugin.PluginManager;
import org.eclipse.jdt.annotation.Nullable;
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.Mock;
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 com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.RanksManagerBeforeClassTest;
import world.bentobox.bentobox.api.addons.GameModeAddon; import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.api.commands.CompositeCommand; 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.TeamInvite.Type; import world.bentobox.bentobox.database.objects.TeamInvite.Type;
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.RanksManager; import world.bentobox.bentobox.managers.RanksManager;
import world.bentobox.bentobox.managers.RanksManagerBeforeClassTest; import world.bentobox.bentobox.util.Util;
/** /**
* @author tastybento * @author tastybento
* *
*/ */
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest({ Bukkit.class, BentoBox.class, User.class }) @PrepareForTest({ Bukkit.class, BentoBox.class, User.class, Util.class })
public class IslandTeamCommandTest extends RanksManagerBeforeClassTest { public class IslandTeamCommandTest extends RanksManagerBeforeClassTest {
@Mock @Mock
@ -56,33 +50,14 @@ public class IslandTeamCommandTest extends RanksManagerBeforeClassTest {
private IslandTeamCommand tc; private IslandTeamCommand tc;
private UUID uuid;
private UUID invitee; private UUID invitee;
@Mock
private IslandsManager im;
@Mock @Mock
private User user; private User user;
@Mock
private World world;
@Mock
private PluginManager pim;
@Mock
private IslandWorldManager iwm;
@Mock
private @Nullable Island island;
@Mock @Mock
private GameModeAddon addon; private GameModeAddon addon;
/**
*/
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); super.setUp();
@ -97,13 +72,11 @@ public class IslandTeamCommandTest extends RanksManagerBeforeClassTest {
when(ic.getAddon()).thenReturn(addon); when(ic.getAddon()).thenReturn(addon);
// user // user
uuid = UUID.randomUUID();
invitee = UUID.randomUUID(); invitee = UUID.randomUUID();
when(user.getUniqueId()).thenReturn(uuid); when(user.getUniqueId()).thenReturn(uuid);
when(user.getPermissionValue(eq("bskyblock.team.maxsize"), anyInt())).thenReturn(3); when(user.getPermissionValue(eq("bskyblock.team.maxsize"), anyInt())).thenReturn(3);
// island Manager // island Manager
when(plugin.getIslands()).thenReturn(im);
// is owner of island // is owner of island
when(im.getPrimaryIsland(world, uuid)).thenReturn(island); when(im.getPrimaryIsland(world, uuid)).thenReturn(island);
when(im.getIsland(world, user)).thenReturn(island); when(im.getIsland(world, user)).thenReturn(island);
@ -122,12 +95,7 @@ public class IslandTeamCommandTest extends RanksManagerBeforeClassTest {
// island // island
when(im.getIsland(any(), eq(uuid))).thenReturn(island); when(im.getIsland(any(), eq(uuid))).thenReturn(island);
// Bukkit
PowerMockito.mockStatic(Bukkit.class);
when(Bukkit.getPluginManager()).thenReturn(pim);
// IWM // IWM
when(plugin.getIWM()).thenReturn(iwm);
when(iwm.getPermissionPrefix(any())).thenReturn("bskyblock."); when(iwm.getPermissionPrefix(any())).thenReturn("bskyblock.");
// Command under test // Command under test

View File

@ -29,10 +29,10 @@ import org.powermock.modules.junit4.PowerMockRunner;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.RanksManagerBeforeClassTest;
import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.Settings;
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.managers.CommandsManager; 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;
@ -40,14 +40,14 @@ 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.managers.RanksManager;
import world.bentobox.bentobox.managers.RanksManagerBeforeClassTest; import world.bentobox.bentobox.util.Util;
/** /**
* @author tastybento * @author tastybento
* *
*/ */
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest({ Bukkit.class, BentoBox.class, User.class }) @PrepareForTest({ Bukkit.class, BentoBox.class, User.class, Util.class })
public class IslandTeamCoopCommandTest extends RanksManagerBeforeClassTest { public class IslandTeamCoopCommandTest extends RanksManagerBeforeClassTest {
@Mock @Mock
@ -62,8 +62,6 @@ public class IslandTeamCoopCommandTest extends RanksManagerBeforeClassTest {
private UUID notUUID; private UUID notUUID;
@Mock @Mock
private Settings s; private Settings s;
@Mock
private Island island;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
@ -76,7 +74,6 @@ public class IslandTeamCoopCommandTest extends RanksManagerBeforeClassTest {
when(plugin.getSettings()).thenReturn(s); when(plugin.getSettings()).thenReturn(s);
// Player // Player
Player p = mock(Player.class);
// Sometimes use Mockito.withSettings().verboseLogging() // Sometimes use Mockito.withSettings().verboseLogging()
when(user.isOp()).thenReturn(false); when(user.isOp()).thenReturn(false);
when(user.getPermissionValue(anyString(), anyInt())).thenReturn(4); when(user.getPermissionValue(anyString(), anyInt())).thenReturn(4);
@ -87,7 +84,7 @@ public class IslandTeamCoopCommandTest extends RanksManagerBeforeClassTest {
notUUID = UUID.randomUUID(); notUUID = UUID.randomUUID();
} }
when(user.getUniqueId()).thenReturn(uuid); when(user.getUniqueId()).thenReturn(uuid);
when(user.getPlayer()).thenReturn(p); when(user.getPlayer()).thenReturn(mockPlayer);
when(user.getName()).thenReturn("tastybento"); when(user.getName()).thenReturn("tastybento");
when(user.getDisplayName()).thenReturn("&Ctastybento"); when(user.getDisplayName()).thenReturn("&Ctastybento");
when(user.getTranslation(any())).thenAnswer(invocation -> invocation.getArgument(0, String.class)); when(user.getTranslation(any())).thenAnswer(invocation -> invocation.getArgument(0, String.class));
@ -300,6 +297,7 @@ public class IslandTeamCoopCommandTest extends RanksManagerBeforeClassTest {
when(p.getUniqueId()).thenReturn(notUUID); when(p.getUniqueId()).thenReturn(notUUID);
when(p.getName()).thenReturn("target"); when(p.getName()).thenReturn("target");
when(p.getDisplayName()).thenReturn("&Ctarget"); when(p.getDisplayName()).thenReturn("&Ctarget");
when(p.spigot()).thenReturn(spigot);
User target = User.getInstance(p); User target = User.getInstance(p);
// Can execute // Can execute
when(pm.getUUID(any())).thenReturn(notUUID); when(pm.getUUID(any())).thenReturn(notUUID);

View File

@ -18,13 +18,10 @@ import java.util.List;
import java.util.UUID; import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryType; import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemFactory; import org.bukkit.inventory.ItemFactory;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.PluginManager;
import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.scheduler.BukkitScheduler;
import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNull;
import org.junit.Before; import org.junit.Before;
@ -38,40 +35,32 @@ import org.powermock.modules.junit4.PowerMockRunner;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.RanksManagerBeforeClassTest;
import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.Settings;
import world.bentobox.bentobox.TestWorldSettings; import world.bentobox.bentobox.TestWorldSettings;
import world.bentobox.bentobox.api.configuration.WorldSettings; import world.bentobox.bentobox.api.configuration.WorldSettings;
import world.bentobox.bentobox.api.events.IslandBaseEvent; import world.bentobox.bentobox.api.events.IslandBaseEvent;
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.TeamInvite; import world.bentobox.bentobox.database.objects.TeamInvite;
import world.bentobox.bentobox.database.objects.TeamInvite.Type; import world.bentobox.bentobox.database.objects.TeamInvite.Type;
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.LocalesManager; import world.bentobox.bentobox.managers.LocalesManager;
import world.bentobox.bentobox.managers.PlayersManager; import world.bentobox.bentobox.managers.PlayersManager;
import world.bentobox.bentobox.managers.RanksManager; import world.bentobox.bentobox.managers.RanksManager;
import world.bentobox.bentobox.managers.RanksManagerBeforeClassTest; import world.bentobox.bentobox.util.Util;
/** /**
* @author tastybento * @author tastybento
* *
*/ */
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest({ Bukkit.class, BentoBox.class, User.class }) @PrepareForTest({ Bukkit.class, BentoBox.class, User.class, Util.class })
public class IslandTeamInviteCommandTest extends RanksManagerBeforeClassTest { public class IslandTeamInviteCommandTest extends RanksManagerBeforeClassTest {
@Mock @Mock
private IslandTeamCommand ic; private IslandTeamCommand ic;
@Mock @Mock
private IslandsManager im;
@Mock
private Island island;
@Mock
private PluginManager pim;
@Mock
private PlayersManager pm; private PlayersManager pm;
@Mock @Mock
private Settings s; private Settings s;
@ -80,14 +69,9 @@ public class IslandTeamInviteCommandTest extends RanksManagerBeforeClassTest {
@Mock @Mock
private User user; private User user;
private UUID uuid;
private UUID islandUUID; private UUID islandUUID;
private IslandTeamInviteCommand itl; private IslandTeamInviteCommand itl;
private UUID notUUID; private UUID notUUID;
@Mock
private Player p;
@Mock
private @NonNull World world;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
@ -111,7 +95,7 @@ public class IslandTeamInviteCommandTest extends RanksManagerBeforeClassTest {
when(user.isOp()).thenReturn(false); when(user.isOp()).thenReturn(false);
uuid = UUID.randomUUID(); uuid = UUID.randomUUID();
when(user.getUniqueId()).thenReturn(uuid); when(user.getUniqueId()).thenReturn(uuid);
when(user.getPlayer()).thenReturn(p); when(user.getPlayer()).thenReturn(mockPlayer);
when(user.getName()).thenReturn("tastybento"); when(user.getName()).thenReturn("tastybento");
when(user.getDisplayName()).thenReturn("&Ctastbento"); when(user.getDisplayName()).thenReturn("&Ctastbento");
when(user.isOnline()).thenReturn(true); when(user.isOnline()).thenReturn(true);
@ -120,13 +104,13 @@ public class IslandTeamInviteCommandTest extends RanksManagerBeforeClassTest {
when(User.getInstance(uuid)).thenReturn(user); when(User.getInstance(uuid)).thenReturn(user);
when(user.getTranslation(any())).thenAnswer(invocation -> invocation.getArgument(0, String.class)); when(user.getTranslation(any())).thenAnswer(invocation -> invocation.getArgument(0, String.class));
// Vanished players // Vanished players
when(p.canSee(any())).thenReturn(true); when(mockPlayer.canSee(any())).thenReturn(true);
User.setPlugin(plugin); User.setPlugin(plugin);
// Target // Target
notUUID = UUID.randomUUID(); notUUID = UUID.randomUUID();
when(target.getUniqueId()).thenReturn(notUUID); when(target.getUniqueId()).thenReturn(notUUID);
when(target.getPlayer()).thenReturn(p); when(target.getPlayer()).thenReturn(mockPlayer);
when(target.isOnline()).thenReturn(true); when(target.isOnline()).thenReturn(true);
when(target.getName()).thenReturn("target"); when(target.getName()).thenReturn("target");
when(target.getDisplayName()).thenReturn("&Ctarget"); when(target.getDisplayName()).thenReturn("&Ctarget");
@ -170,7 +154,6 @@ public class IslandTeamInviteCommandTest extends RanksManagerBeforeClassTest {
when(plugin.getLocalesManager()).thenReturn(lm); when(plugin.getLocalesManager()).thenReturn(lm);
// IWM friendly name // IWM friendly name
IslandWorldManager iwm = mock(IslandWorldManager.class);
when(iwm.getFriendlyName(any())).thenReturn("BSkyBlock"); when(iwm.getFriendlyName(any())).thenReturn("BSkyBlock");
@NonNull @NonNull
WorldSettings ws = new TestWorldSettings(); WorldSettings ws = new TestWorldSettings();
@ -246,7 +229,7 @@ public class IslandTeamInviteCommandTest extends RanksManagerBeforeClassTest {
public void testCanExecuteNoTarget() { public void testCanExecuteNoTarget() {
assertFalse(itl.canExecute(user, itl.getLabel(), Collections.emptyList())); assertFalse(itl.canExecute(user, itl.getLabel(), Collections.emptyList()));
// Show panel // Show panel
verify(p).openInventory(any(Inventory.class)); verify(mockPlayer).openInventory(any(Inventory.class));
} }
/** /**
@ -264,7 +247,7 @@ public class IslandTeamInviteCommandTest extends RanksManagerBeforeClassTest {
*/ */
@Test @Test
public void testCanExecuteVanishedPlayer() { public void testCanExecuteVanishedPlayer() {
when(p.canSee(any())).thenReturn(false); when(mockPlayer.canSee(any())).thenReturn(false);
assertFalse(itl.canExecute(user, itl.getLabel(), List.of("target"))); assertFalse(itl.canExecute(user, itl.getLabel(), List.of("target")));
verify(user).sendMessage(eq("general.errors.offline-player")); verify(user).sendMessage(eq("general.errors.offline-player"));
} }

View File

@ -20,7 +20,6 @@ import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.scheduler.BukkitScheduler;
@ -36,6 +35,7 @@ import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSet.Builder; import com.google.common.collect.ImmutableSet.Builder;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.RanksManagerBeforeClassTest;
import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.Settings;
import world.bentobox.bentobox.api.addons.Addon; import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.api.addons.AddonDescription; import world.bentobox.bentobox.api.addons.AddonDescription;
@ -51,13 +51,13 @@ 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.managers.RanksManager;
import world.bentobox.bentobox.managers.RanksManagerBeforeClassTest; import world.bentobox.bentobox.util.Util;
/** /**
* @author tastybento * @author tastybento
*/ */
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest({ Bukkit.class, BentoBox.class, User.class }) @PrepareForTest({ Bukkit.class, BentoBox.class, User.class, Util.class })
public class IslandTeamKickCommandTest extends RanksManagerBeforeClassTest { public class IslandTeamKickCommandTest extends RanksManagerBeforeClassTest {
@Mock @Mock
@ -68,27 +68,15 @@ public class IslandTeamKickCommandTest extends RanksManagerBeforeClassTest {
@Mock @Mock
private Settings s; private Settings s;
@Mock @Mock
private IslandsManager im;
@Mock
private PlayersManager pm; private PlayersManager pm;
private UUID notUUID; private UUID notUUID;
@Mock @Mock
private IslandWorldManager iwm;
@Mock
private Player player;
@Mock
private Player target; private Player target;
@Mock @Mock
private CompositeCommand subCommand; private CompositeCommand subCommand;
@Mock @Mock
private Island island;
@Mock
private Addon addon; private Addon addon;
@Mock
private World world;
/**
*/
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); super.setUp();
@ -112,10 +100,11 @@ public class IslandTeamKickCommandTest extends RanksManagerBeforeClassTest {
when(target.isOnline()).thenReturn(true); when(target.isOnline()).thenReturn(true);
when(target.getName()).thenReturn("poslovitch"); when(target.getName()).thenReturn("poslovitch");
when(target.getDisplayName()).thenReturn("&Cposlovich"); when(target.getDisplayName()).thenReturn("&Cposlovich");
when(target.spigot()).thenReturn(spigot);
// Set the target user // Set the target user
User.getInstance(target); User.getInstance(target);
when(user.getUniqueId()).thenReturn(uuid); when(user.getUniqueId()).thenReturn(uuid);
when(user.getPlayer()).thenReturn(player); when(user.getPlayer()).thenReturn(mockPlayer);
when(user.getName()).thenReturn("tastybento"); when(user.getName()).thenReturn("tastybento");
when(user.getDisplayName()).thenReturn("&Ctastybento"); when(user.getDisplayName()).thenReturn("&Ctastybento");
when(user.getTranslation(any())).thenAnswer(invocation -> invocation.getArgument(0, String.class)); when(user.getTranslation(any())).thenAnswer(invocation -> invocation.getArgument(0, String.class));

View File

@ -17,9 +17,6 @@ import java.util.Optional;
import java.util.UUID; import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.eclipse.jdt.annotation.Nullable;
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;
@ -32,35 +29,29 @@ import org.powermock.modules.junit4.PowerMockRunner;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.RanksManagerBeforeClassTest;
import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.Settings;
import world.bentobox.bentobox.TestWorldSettings; import world.bentobox.bentobox.TestWorldSettings;
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.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.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.managers.RanksManager;
import world.bentobox.bentobox.managers.RanksManagerBeforeClassTest; import world.bentobox.bentobox.util.Util;
/** /**
* @author tastybento * @author tastybento
* *
*/ */
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest({Bukkit.class, BentoBox.class, User.class }) @PrepareForTest({ Bukkit.class, BentoBox.class, User.class, Util.class })
public class IslandTeamPromoteCommandTest extends RanksManagerBeforeClassTest { public class IslandTeamPromoteCommandTest extends RanksManagerBeforeClassTest {
@Mock
Player player;
@Mock @Mock
private IslandTeamCommand ic; private IslandTeamCommand ic;
@Mock @Mock
User user; User user;
@Mock
IslandsManager im;
// DUT // DUT
private IslandTeamPromoteCommand ipc; private IslandTeamPromoteCommand ipc;
@ -68,12 +59,6 @@ public class IslandTeamPromoteCommandTest extends RanksManagerBeforeClassTest {
@Mock @Mock
private PlayersManager pm; private PlayersManager pm;
@Mock @Mock
private World world;
@Mock
private IslandWorldManager iwm;
@Mock
private @Nullable Island island;
@Mock
private User target; private User target;
@ -99,7 +84,7 @@ public class IslandTeamPromoteCommandTest extends RanksManagerBeforeClassTest {
UUID uuid = UUID.randomUUID(); UUID uuid = UUID.randomUUID();
when(user.getUniqueId()).thenReturn(uuid); when(user.getUniqueId()).thenReturn(uuid);
when(user.getName()).thenReturn("tastybento"); when(user.getName()).thenReturn("tastybento");
when(user.getPlayer()).thenReturn(player); when(user.getPlayer()).thenReturn(mockPlayer);
when(pm.getUser("target")).thenReturn(target); when(pm.getUser("target")).thenReturn(target);
when(target.getName()).thenReturn("target"); when(target.getName()).thenReturn("target");
when(target.getDisplayName()).thenReturn("Target"); when(target.getDisplayName()).thenReturn("Target");
@ -138,8 +123,8 @@ public class IslandTeamPromoteCommandTest extends RanksManagerBeforeClassTest {
// Bukkit // Bukkit
PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS);
when(Bukkit.getOfflinePlayer(uuid)).thenReturn(player); when(Bukkit.getOfflinePlayer(uuid)).thenReturn(mockPlayer);
when(player.getName()).thenReturn("tastybento"); when(mockPlayer.getName()).thenReturn("tastybento");
ipc = new IslandTeamPromoteCommand(ic, "promote"); ipc = new IslandTeamPromoteCommand(ic, "promote");
@ -179,7 +164,7 @@ public class IslandTeamPromoteCommandTest extends RanksManagerBeforeClassTest {
@Test @Test
public void testCanExecuteUserStringListOfStringShowHelp() { public void testCanExecuteUserStringListOfStringShowHelp() {
assertFalse(ipc.canExecute(user, "promote", List.of())); // Nothing assertFalse(ipc.canExecute(user, "promote", List.of())); // Nothing
verify(user).sendMessage("commands.help.header", TextVariables.LABEL, null); verify(user).sendMessage("commands.help.header", TextVariables.LABEL, "BSkyBlock");
} }
/** /**

View File

@ -31,10 +31,10 @@ import org.powermock.modules.junit4.PowerMockRunner;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.RanksManagerBeforeClassTest;
import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.Settings;
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.managers.CommandsManager; 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;
@ -42,14 +42,14 @@ 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.managers.RanksManager;
import world.bentobox.bentobox.managers.RanksManagerBeforeClassTest; import world.bentobox.bentobox.util.Util;
/** /**
* @author tastybento * @author tastybento
* *
*/ */
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest({ Bukkit.class, BentoBox.class, User.class }) @PrepareForTest({ Bukkit.class, BentoBox.class, User.class, Util.class })
public class IslandTeamTrustCommandTest extends RanksManagerBeforeClassTest { public class IslandTeamTrustCommandTest extends RanksManagerBeforeClassTest {
@Mock @Mock
@ -64,10 +64,6 @@ public class IslandTeamTrustCommandTest extends RanksManagerBeforeClassTest {
private UUID notUUID; private UUID notUUID;
@Mock @Mock
private Settings s; private Settings s;
@Mock
private Island island;
@Mock
private Player targetPlayer;
/** /**
*/ */
@ -101,24 +97,24 @@ public class IslandTeamTrustCommandTest extends RanksManagerBeforeClassTest {
when(user.getTranslation(any())).thenAnswer(invocation -> invocation.getArgument(0, String.class)); when(user.getTranslation(any())).thenAnswer(invocation -> invocation.getArgument(0, String.class));
User.setPlugin(plugin); User.setPlugin(plugin);
// Target player // Target player
when(targetPlayer.getUniqueId()).thenReturn(notUUID); when(mockPlayer.getUniqueId()).thenReturn(notUUID);
when(targetPlayer.getName()).thenReturn("target"); when(mockPlayer.getName()).thenReturn("target");
when(targetPlayer.getDisplayName()).thenReturn("&Cposlovich"); when(mockPlayer.getDisplayName()).thenReturn("&Cposlovich");
User.getInstance(targetPlayer); User.getInstance(mockPlayer);
// Parent command has no aliases // Parent command has no aliases
when(ic.getSubCommandAliases()).thenReturn(new HashMap<>()); when(ic.getSubCommandAliases()).thenReturn(new HashMap<>());
// Player has island to begin with // Player has island to begin with
when(im.hasIsland(any(), Mockito.any(UUID.class))).thenReturn(true); when(im.hasIsland(any(), any(UUID.class))).thenReturn(true);
when(im.inTeam(any(), Mockito.any(UUID.class))).thenReturn(true); when(im.inTeam(any(), any(UUID.class))).thenReturn(true);
// when(im.isOwner(any(), any())).thenReturn(true); // when(im.isOwner(any(), any())).thenReturn(true);
// when(im.getOwner(any(), any())).thenReturn(uuid); // when(im.getOwner(any(), any())).thenReturn(uuid);
// Island // Island
when(island.getRank(any(User.class))).thenReturn(RanksManager.OWNER_RANK); when(island.getRank(any(User.class))).thenReturn(RanksManager.OWNER_RANK);
when(island.getMemberSet(anyInt(), any(Boolean.class))).thenReturn(ImmutableSet.of(uuid)); when(island.getMemberSet(anyInt(), any(Boolean.class))).thenReturn(ImmutableSet.of(uuid));
when(im.getIsland(any(), Mockito.any(User.class))).thenReturn(island); when(im.getIsland(any(), any(User.class))).thenReturn(island);
when(im.getIsland(any(), Mockito.any(UUID.class))).thenReturn(island); when(im.getIsland(any(), any(UUID.class))).thenReturn(island);
when(plugin.getIslands()).thenReturn(im); when(plugin.getIslands()).thenReturn(im);
// Has team // Has team
@ -157,7 +153,7 @@ public class IslandTeamTrustCommandTest extends RanksManagerBeforeClassTest {
when(im.inTeam(any(), Mockito.any(UUID.class))).thenReturn(false); when(im.inTeam(any(), Mockito.any(UUID.class))).thenReturn(false);
IslandTeamTrustCommand itl = new IslandTeamTrustCommand(ic); IslandTeamTrustCommand itl = new IslandTeamTrustCommand(ic);
assertFalse(itl.canExecute(user, itl.getLabel(), Collections.singletonList("bill"))); assertFalse(itl.canExecute(user, itl.getLabel(), Collections.singletonList("bill")));
verify(user).sendMessage(eq("general.errors.no-island")); verify(user).sendMessage("general.errors.no-island");
} }
/** /**
@ -207,7 +203,7 @@ public class IslandTeamTrustCommandTest extends RanksManagerBeforeClassTest {
IslandTeamTrustCommand itl = new IslandTeamTrustCommand(ic); IslandTeamTrustCommand itl = new IslandTeamTrustCommand(ic);
when(pm.getUUID(any())).thenReturn(uuid); when(pm.getUUID(any())).thenReturn(uuid);
assertFalse(itl.canExecute(user, itl.getLabel(), Collections.singletonList("tastybento"))); assertFalse(itl.canExecute(user, itl.getLabel(), Collections.singletonList("tastybento")));
verify(user).sendMessage(eq("commands.island.team.trust.trust-in-yourself")); verify(user).sendMessage("commands.island.team.trust.trust-in-yourself");
} }
/** /**
@ -224,7 +220,7 @@ public class IslandTeamTrustCommandTest extends RanksManagerBeforeClassTest {
when(im.inTeam(any(), any())).thenReturn(true); when(im.inTeam(any(), any())).thenReturn(true);
// when(im.getMembers(any(), any())).thenReturn(Collections.singleton(notUUID)); // when(im.getMembers(any(), any())).thenReturn(Collections.singleton(notUUID));
assertFalse(itl.canExecute(user, itl.getLabel(), Collections.singletonList("bento"))); assertFalse(itl.canExecute(user, itl.getLabel(), Collections.singletonList("bento")));
verify(user).sendMessage(eq("commands.island.team.trust.player-already-trusted")); verify(user).sendMessage("commands.island.team.trust.player-already-trusted");
} }
/** /**
@ -235,7 +231,7 @@ public class IslandTeamTrustCommandTest extends RanksManagerBeforeClassTest {
when(pm.getUUID(any())).thenReturn(uuid); when(pm.getUUID(any())).thenReturn(uuid);
IslandTeamTrustCommand itl = new IslandTeamTrustCommand(ic); IslandTeamTrustCommand itl = new IslandTeamTrustCommand(ic);
assertFalse(itl.canExecute(user, itl.getLabel(), Collections.singletonList("tastybento"))); assertFalse(itl.canExecute(user, itl.getLabel(), Collections.singletonList("tastybento")));
verify(user).sendMessage(eq("commands.island.team.trust.trust-in-yourself")); verify(user).sendMessage("commands.island.team.trust.trust-in-yourself");
} }
/** /**
@ -249,7 +245,7 @@ public class IslandTeamTrustCommandTest extends RanksManagerBeforeClassTest {
// when(im.getMembers(any(), any())).thenReturn(Collections.singleton(other)); // when(im.getMembers(any(), any())).thenReturn(Collections.singleton(other));
IslandTeamTrustCommand itl = new IslandTeamTrustCommand(ic); IslandTeamTrustCommand itl = new IslandTeamTrustCommand(ic);
assertFalse(itl.canExecute(user, itl.getLabel(), Collections.singletonList("tastybento"))); assertFalse(itl.canExecute(user, itl.getLabel(), Collections.singletonList("tastybento")));
verify(user).sendMessage(eq("commands.island.team.trust.player-already-trusted")); verify(user).sendMessage("commands.island.team.trust.player-already-trusted");
} }
/** /**
@ -266,7 +262,7 @@ public class IslandTeamTrustCommandTest extends RanksManagerBeforeClassTest {
// Execute // Execute
when(im.getIsland(any(), Mockito.any(UUID.class))).thenReturn(null); when(im.getIsland(any(), Mockito.any(UUID.class))).thenReturn(null);
assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("tastybento"))); assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("tastybento")));
verify(user).sendMessage(eq("general.errors.general")); verify(user).sendMessage("general.errors.general");
} }
/** /**
@ -284,7 +280,7 @@ public class IslandTeamTrustCommandTest extends RanksManagerBeforeClassTest {
// Execute // Execute
when(im.getIsland(any(), Mockito.any(UUID.class))).thenReturn(island); when(im.getIsland(any(), Mockito.any(UUID.class))).thenReturn(island);
assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("target"))); assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("target")));
verify(user).sendMessage(eq("commands.island.team.trust.is-full")); verify(user).sendMessage("commands.island.team.trust.is-full");
} }
/** /**
@ -293,7 +289,7 @@ public class IslandTeamTrustCommandTest extends RanksManagerBeforeClassTest {
*/ */
@Test @Test
public void testExecuteSuccessNoConfirmation() { public void testExecuteSuccessNoConfirmation() {
User target = User.getInstance(targetPlayer); User target = User.getInstance(mockPlayer);
// Can execute // Can execute
when(pm.getUUID(any())).thenReturn(notUUID); when(pm.getUUID(any())).thenReturn(notUUID);
// when(im.getMembers(any(), any())).thenReturn(Collections.emptySet()); // when(im.getMembers(any(), any())).thenReturn(Collections.emptySet());
@ -308,7 +304,7 @@ public class IslandTeamTrustCommandTest extends RanksManagerBeforeClassTest {
verify(user).sendMessage("commands.island.team.trust.success", TextVariables.NAME, "target", verify(user).sendMessage("commands.island.team.trust.success", TextVariables.NAME, "target",
TextVariables.DISPLAY_NAME, "&Cposlovich"); TextVariables.DISPLAY_NAME, "&Cposlovich");
verify(island).setRank(target, RanksManager.TRUSTED_RANK); verify(island).setRank(target, RanksManager.TRUSTED_RANK);
verify(targetPlayer).sendMessage("commands.island.team.trust.you-are-trusted"); checkSpigotMessage("commands.island.team.trust.you-are-trusted");
} }
/** /**
@ -317,7 +313,7 @@ public class IslandTeamTrustCommandTest extends RanksManagerBeforeClassTest {
@Test @Test
public void testExecuteSuccessConfirmation() { public void testExecuteSuccessConfirmation() {
when(s.isInviteConfirmation()).thenReturn(true); when(s.isInviteConfirmation()).thenReturn(true);
User target = User.getInstance(targetPlayer); User target = User.getInstance(mockPlayer);
// Can execute // Can execute
when(pm.getUUID(any())).thenReturn(notUUID); when(pm.getUUID(any())).thenReturn(notUUID);
//when(im.getMembers(any(), any())).thenReturn(Collections.emptySet()); //when(im.getMembers(any(), any())).thenReturn(Collections.emptySet());
@ -330,8 +326,8 @@ public class IslandTeamTrustCommandTest extends RanksManagerBeforeClassTest {
assertTrue(itl.execute(user, itl.getLabel(), Collections.singletonList("target"))); assertTrue(itl.execute(user, itl.getLabel(), Collections.singletonList("target")));
verify(user).sendMessage("commands.island.team.invite.invitation-sent", TextVariables.NAME, "target", TextVariables.DISPLAY_NAME, "&Cposlovich"); verify(user).sendMessage("commands.island.team.invite.invitation-sent", TextVariables.NAME, "target", TextVariables.DISPLAY_NAME, "&Cposlovich");
// Send message to online player // Send message to online player
verify(targetPlayer).sendMessage(eq("commands.island.team.trust.name-has-invited-you")); checkSpigotMessage("commands.island.team.trust.name-has-invited-you");
verify(targetPlayer).sendMessage(eq("commands.island.team.invite.to-accept-or-reject")); checkSpigotMessage("commands.island.team.invite.to-accept-or-reject");
verify(island, never()).setRank(target, RanksManager.TRUSTED_RANK); verify(island, never()).setRank(target, RanksManager.TRUSTED_RANK);
} }

View File

@ -21,7 +21,6 @@ import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.scheduler.BukkitScheduler;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -34,39 +33,34 @@ import org.powermock.modules.junit4.PowerMockRunner;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.RanksManagerBeforeClassTest;
import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.Settings;
import world.bentobox.bentobox.api.commands.CompositeCommand; 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.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.LocalesManager; import world.bentobox.bentobox.managers.LocalesManager;
import world.bentobox.bentobox.managers.PlayersManager; import world.bentobox.bentobox.managers.PlayersManager;
import world.bentobox.bentobox.managers.RanksManager; import world.bentobox.bentobox.managers.RanksManager;
import world.bentobox.bentobox.managers.RanksManagerBeforeClassTest; import world.bentobox.bentobox.util.Util;
/** /**
* @author tastybento * @author tastybento
* *
*/ */
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest({ Bukkit.class, BentoBox.class, User.class }) @PrepareForTest({ Bukkit.class, BentoBox.class, User.class, Util.class })
public class IslandTeamUncoopCommandTest extends RanksManagerBeforeClassTest { public class IslandTeamUncoopCommandTest extends RanksManagerBeforeClassTest {
@Mock
private CompositeCommand ic; private CompositeCommand ic;
private UUID uuid; @Mock
private User user; private User user;
private IslandsManager im;
private PlayersManager pm; private PlayersManager pm;
private UUID notUUID; private UUID notUUID;
@Mock @Mock
private Settings s; private Settings s;
private Island island;
/**
*/
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); super.setUp();
@ -79,9 +73,6 @@ public class IslandTeamUncoopCommandTest extends RanksManagerBeforeClassTest {
when(plugin.getSettings()).thenReturn(s); when(plugin.getSettings()).thenReturn(s);
// Player // Player
Player p = mock(Player.class);
// Sometimes use withSettings().verboseLogging()
user = mock(User.class);
when(user.isOp()).thenReturn(false); when(user.isOp()).thenReturn(false);
uuid = UUID.randomUUID(); uuid = UUID.randomUUID();
notUUID = UUID.randomUUID(); notUUID = UUID.randomUUID();
@ -89,27 +80,21 @@ public class IslandTeamUncoopCommandTest extends RanksManagerBeforeClassTest {
notUUID = UUID.randomUUID(); notUUID = UUID.randomUUID();
} }
when(user.getUniqueId()).thenReturn(uuid); when(user.getUniqueId()).thenReturn(uuid);
when(user.getPlayer()).thenReturn(p); when(user.getPlayer()).thenReturn(mockPlayer);
when(user.getName()).thenReturn("tastybento"); when(user.getName()).thenReturn("tastybento");
when(user.getTranslation(any())).thenAnswer(invocation -> invocation.getArgument(0, String.class)); when(user.getTranslation(any())).thenAnswer(invocation -> invocation.getArgument(0, String.class));
User.setPlugin(plugin); User.setPlugin(plugin);
// Parent command has no aliases // Parent command has no aliases
ic = mock(CompositeCommand.class);
when(ic.getSubCommandAliases()).thenReturn(new HashMap<>()); when(ic.getSubCommandAliases()).thenReturn(new HashMap<>());
// Player has island to begin with // Player has island to begin with
im = mock(IslandsManager.class);
when(im.hasIsland(any(), any(UUID.class))).thenReturn(true); when(im.hasIsland(any(), any(UUID.class))).thenReturn(true);
when(im.inTeam(any(), any(UUID.class))).thenReturn(true); when(im.inTeam(any(), any(UUID.class))).thenReturn(true);
// when(im.isOwner(any(), any())).thenReturn(true);
// when(im.getOwner(any(), any())).thenReturn(uuid);
island = mock(Island.class);
when(island.getRank(any(User.class))).thenReturn(RanksManager.OWNER_RANK); when(island.getRank(any(User.class))).thenReturn(RanksManager.OWNER_RANK);
when(im.getIsland(any(), any(User.class))).thenReturn(island); when(im.getIsland(any(), any(User.class))).thenReturn(island);
when(im.getIsland(any(), any(UUID.class))).thenReturn(island); when(im.getIsland(any(), any(UUID.class))).thenReturn(island);
when(im.getPrimaryIsland(any(), any())).thenReturn(island); when(im.getPrimaryIsland(any(), any())).thenReturn(island);
when(plugin.getIslands()).thenReturn(im);
// Has team // Has team
when(im.inTeam(any(), eq(uuid))).thenReturn(true); when(im.inTeam(any(), eq(uuid))).thenReturn(true);
@ -129,7 +114,6 @@ public class IslandTeamUncoopCommandTest extends RanksManagerBeforeClassTest {
when(plugin.getLocalesManager()).thenReturn(lm); when(plugin.getLocalesManager()).thenReturn(lm);
// IWM friendly name // IWM friendly name
IslandWorldManager iwm = mock(IslandWorldManager.class);
when(iwm.getFriendlyName(any())).thenReturn("BSkyBlock"); when(iwm.getFriendlyName(any())).thenReturn("BSkyBlock");
when(plugin.getIWM()).thenReturn(iwm); when(plugin.getIWM()).thenReturn(iwm);

View File

@ -20,7 +20,6 @@ import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.scheduler.BukkitScheduler;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -33,39 +32,35 @@ import org.powermock.modules.junit4.PowerMockRunner;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.RanksManagerBeforeClassTest;
import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.Settings;
import world.bentobox.bentobox.api.commands.CompositeCommand; 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.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.LocalesManager; import world.bentobox.bentobox.managers.LocalesManager;
import world.bentobox.bentobox.managers.PlayersManager; import world.bentobox.bentobox.managers.PlayersManager;
import world.bentobox.bentobox.managers.RanksManager; import world.bentobox.bentobox.managers.RanksManager;
import world.bentobox.bentobox.managers.RanksManagerBeforeClassTest; import world.bentobox.bentobox.util.Util;
/** /**
* @author tastybento * @author tastybento
* *
*/ */
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest({ Bukkit.class, BentoBox.class, User.class }) @PrepareForTest({ Bukkit.class, BentoBox.class, User.class, Util.class })
public class IslandTeamUntrustCommandTest extends RanksManagerBeforeClassTest { public class IslandTeamUntrustCommandTest extends RanksManagerBeforeClassTest {
@Mock
private CompositeCommand ic; private CompositeCommand ic;
private UUID uuid; @Mock
private User user; private User user;
private IslandsManager im; @Mock
private PlayersManager pm; private PlayersManager pm;
private UUID notUUID; private UUID notUUID;
@Mock @Mock
private Settings s; private Settings s;
private Island island;
/**
*/
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); super.setUp();
@ -78,17 +73,11 @@ public class IslandTeamUntrustCommandTest extends RanksManagerBeforeClassTest {
when(plugin.getSettings()).thenReturn(s); when(plugin.getSettings()).thenReturn(s);
// Player // Player
Player p = mock(Player.class);
// Sometimes use Mockito.withSettings().verboseLogging()
user = mock(User.class);
when(user.isOp()).thenReturn(false); when(user.isOp()).thenReturn(false);
uuid = UUID.randomUUID(); uuid = UUID.randomUUID();
notUUID = 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.getPlayer()).thenReturn(mockPlayer);
when(user.getName()).thenReturn("tastybento"); when(user.getName()).thenReturn("tastybento");
when(user.getDisplayName()).thenReturn("&Ctastybento"); when(user.getDisplayName()).thenReturn("&Ctastybento");
when(user.getTranslation(any())).thenAnswer(invocation -> invocation.getArgument(0, String.class)); when(user.getTranslation(any())).thenAnswer(invocation -> invocation.getArgument(0, String.class));
@ -99,17 +88,12 @@ public class IslandTeamUntrustCommandTest extends RanksManagerBeforeClassTest {
when(ic.getSubCommandAliases()).thenReturn(new HashMap<>()); when(ic.getSubCommandAliases()).thenReturn(new HashMap<>());
// Player has island to begin with // Player has island to begin with
im = mock(IslandsManager.class);
when(im.hasIsland(any(), any(UUID.class))).thenReturn(true); when(im.hasIsland(any(), any(UUID.class))).thenReturn(true);
when(im.inTeam(any(), any(UUID.class))).thenReturn(true); when(im.inTeam(any(), any(UUID.class))).thenReturn(true);
// when(im.isOwner(any(), any())).thenReturn(true);
// when(im.getOwner(any(), any())).thenReturn(uuid);
island = mock(Island.class);
when(island.getRank(any(User.class))).thenReturn(RanksManager.OWNER_RANK); when(island.getRank(any(User.class))).thenReturn(RanksManager.OWNER_RANK);
when(im.getIsland(any(), any(User.class))).thenReturn(island); when(im.getIsland(any(), any(User.class))).thenReturn(island);
when(im.getIsland(any(), any(UUID.class))).thenReturn(island); when(im.getIsland(any(), any(UUID.class))).thenReturn(island);
when(im.getPrimaryIsland(any(), any())).thenReturn(island); when(im.getPrimaryIsland(any(), any())).thenReturn(island);
when(plugin.getIslands()).thenReturn(im);
// Has team // Has team
when(im.inTeam(any(), eq(uuid))).thenReturn(true); when(im.inTeam(any(), eq(uuid))).thenReturn(true);
@ -129,7 +113,6 @@ public class IslandTeamUntrustCommandTest extends RanksManagerBeforeClassTest {
when(plugin.getLocalesManager()).thenReturn(lm); when(plugin.getLocalesManager()).thenReturn(lm);
// IWM friendly name // IWM friendly name
IslandWorldManager iwm = mock(IslandWorldManager.class);
when(iwm.getFriendlyName(any())).thenReturn("BSkyBlock"); when(iwm.getFriendlyName(any())).thenReturn("BSkyBlock");
when(plugin.getIWM()).thenReturn(iwm); when(plugin.getIWM()).thenReturn(iwm);
@ -144,7 +127,7 @@ public class IslandTeamUntrustCommandTest extends RanksManagerBeforeClassTest {
when(im.inTeam(any(), any(UUID.class))).thenReturn(false); when(im.inTeam(any(), any(UUID.class))).thenReturn(false);
IslandTeamUntrustCommand itl = new IslandTeamUntrustCommand(ic); IslandTeamUntrustCommand itl = new IslandTeamUntrustCommand(ic);
assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("bill"))); assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("bill")));
verify(user).sendMessage(eq("general.errors.no-island")); verify(user).sendMessage("general.errors.no-island");
} }
/** /**
@ -194,7 +177,7 @@ public class IslandTeamUntrustCommandTest extends RanksManagerBeforeClassTest {
IslandTeamUntrustCommand itl = new IslandTeamUntrustCommand(ic); IslandTeamUntrustCommand itl = new IslandTeamUntrustCommand(ic);
when(pm.getUUID(any())).thenReturn(uuid); when(pm.getUUID(any())).thenReturn(uuid);
assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("tastybento"))); assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("tastybento")));
verify(user).sendMessage(eq("commands.island.team.untrust.cannot-untrust-yourself")); verify(user).sendMessage("commands.island.team.untrust.cannot-untrust-yourself");
} }
/** /**
@ -212,7 +195,7 @@ public class IslandTeamUntrustCommandTest extends RanksManagerBeforeClassTest {
when(island.getMemberSet()).thenReturn(ImmutableSet.of(notUUID)); when(island.getMemberSet()).thenReturn(ImmutableSet.of(notUUID));
when(island.inTeam(notUUID)).thenReturn(true); when(island.inTeam(notUUID)).thenReturn(true);
assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("bento"))); assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("bento")));
verify(user).sendMessage(eq("commands.island.team.untrust.cannot-untrust-member")); verify(user).sendMessage("commands.island.team.untrust.cannot-untrust-member");
} }
/** /**

View File

@ -33,12 +33,10 @@ import org.bukkit.Particle.DustOptions;
import org.bukkit.Server; import org.bukkit.Server;
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.inventory.ItemFactory; import org.bukkit.inventory.ItemFactory;
import org.bukkit.inventory.PlayerInventory; import org.bukkit.inventory.PlayerInventory;
import org.bukkit.permissions.PermissionAttachment; import org.bukkit.permissions.PermissionAttachment;
import org.bukkit.permissions.PermissionAttachmentInfo; import org.bukkit.permissions.PermissionAttachmentInfo;
import org.bukkit.plugin.PluginManager;
import org.bukkit.util.Vector; 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;
@ -49,11 +47,10 @@ 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 world.bentobox.bentobox.AbstractCommonSetup;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.Settings;
import world.bentobox.bentobox.api.addons.Addon; import world.bentobox.bentobox.api.addons.Addon;
@ -62,36 +59,28 @@ import world.bentobox.bentobox.api.addons.AddonDescription.Builder;
import world.bentobox.bentobox.api.addons.GameModeAddon; import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.api.metadata.MetaDataValue; import world.bentobox.bentobox.api.metadata.MetaDataValue;
import world.bentobox.bentobox.database.objects.Players; import world.bentobox.bentobox.database.objects.Players;
import world.bentobox.bentobox.managers.IslandWorldManager;
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.util.Util;
/** /**
* @author tastybento * @author tastybento
* *
*/ */
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest({ BentoBox.class, Bukkit.class }) @PrepareForTest({ BentoBox.class, Bukkit.class, Util.class })
public class UserTest { public class UserTest extends AbstractCommonSetup {
private static final String TEST_TRANSLATION = "mock &a translation &b [test]"; private static final String TEST_TRANSLATION = "mock &a translation &b [test]";
private static final String TEST_TRANSLATION_WITH_COLOR = "mock §atranslation §b[test]"; private static final String TEST_TRANSLATION_WITH_COLOR = "mock §atranslation §b[test]";
@Mock @Mock
private Player player;
@Mock
private BentoBox plugin;
@Mock
private LocalesManager lm; private LocalesManager lm;
private User user; private User user;
@Mock
private IslandWorldManager iwm;
private UUID uuid; private UUID uuid;
@Mock @Mock
private PluginManager pim;
@Mock
private CommandSender sender; private CommandSender sender;
@Mock @Mock
private Server server; private Server server;
@ -101,28 +90,26 @@ public class UserTest {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); super.setUp();
// Set up plugin
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
User.setPlugin(plugin);
uuid = UUID.randomUUID(); uuid = UUID.randomUUID();
when(player.getUniqueId()).thenReturn(uuid); when(mockPlayer.getUniqueId()).thenReturn(uuid);
ItemFactory itemFactory = mock(ItemFactory.class); ItemFactory itemFactory = mock(ItemFactory.class);
when(Bukkit.getPlayer(any(UUID.class))).thenReturn(player); when(Bukkit.getPlayer(any(UUID.class))).thenReturn(mockPlayer);
when(Bukkit.getPluginManager()).thenReturn(pim); when(Bukkit.getPluginManager()).thenReturn(pim);
when(Bukkit.getItemFactory()).thenReturn(itemFactory); when(Bukkit.getItemFactory()).thenReturn(itemFactory);
when(Bukkit.getServer()).thenReturn(server); when(Bukkit.getServer()).thenReturn(server);
// Player // Player
when(player.getServer()).thenReturn(server); when(mockPlayer.getServer()).thenReturn(server);
when(server.getOnlinePlayers()).thenReturn(Collections.emptySet()); when(server.getOnlinePlayers()).thenReturn(Collections.emptySet());
when(sender.spigot()).thenReturn(spigot);
@NonNull @NonNull
World world = mock(World.class); World world = mock(World.class);
when(world.getName()).thenReturn("BSkyBlock"); when(world.getName()).thenReturn("BSkyBlock");
when(player.getWorld()).thenReturn(world); when(mockPlayer.getWorld()).thenReturn(world);
// IWM // IWM
when(plugin.getIWM()).thenReturn(iwm); when(plugin.getIWM()).thenReturn(iwm);
@ -130,7 +117,7 @@ public class UserTest {
when(iwm.getAddon(any())).thenReturn(Optional.empty()); when(iwm.getAddon(any())).thenReturn(Optional.empty());
when(iwm.getFriendlyName(world)).thenReturn("BSkyBlock-Fiendly"); when(iwm.getFriendlyName(world)).thenReturn("BSkyBlock-Fiendly");
user = User.getInstance(player); user = User.getInstance(mockPlayer);
// Locales // Locales
when(plugin.getLocalesManager()).thenReturn(lm); when(plugin.getLocalesManager()).thenReturn(lm);
@ -164,7 +151,7 @@ public class UserTest {
@Test @Test
public void testGetInstancePlayer() { public void testGetInstancePlayer() {
assertEquals(player,user.getPlayer()); assertEquals(mockPlayer, user.getPlayer());
} }
@Test @Test
@ -179,7 +166,7 @@ public class UserTest {
public void testRemovePlayer() { public void testRemovePlayer() {
assertNotNull(User.getInstance(uuid)); assertNotNull(User.getInstance(uuid));
assertEquals(user, User.getInstance(uuid)); assertEquals(user, User.getInstance(uuid));
User.removePlayer(player); User.removePlayer(mockPlayer);
// If the player has been removed from the cache, then code will ask server for player // If the player has been removed from the cache, then code will ask server for player
// Return null and check if instance is null will show that the player is not in the cache // Return null and check if instance is null will show that the player is not in the cache
when(Bukkit.getPlayer(any(UUID.class))).thenReturn(null); when(Bukkit.getPlayer(any(UUID.class))).thenReturn(null);
@ -190,7 +177,7 @@ public class UserTest {
BentoBox plugin = mock(BentoBox.class); BentoBox plugin = mock(BentoBox.class);
User.setPlugin(plugin); User.setPlugin(plugin);
user.addPerm("testing123"); user.addPerm("testing123");
verify(player).addAttachment(eq(plugin), eq("testing123"), eq(true)); verify(mockPlayer).addAttachment(eq(plugin), eq("testing123"), eq(true));
} }
@Test @Test
@ -206,9 +193,9 @@ public class UserTest {
@Test @Test
public void testGetInventory() { public void testGetInventory() {
PlayerInventory value = mock(PlayerInventory.class); PlayerInventory value = mock(PlayerInventory.class);
when(player.getInventory()).thenReturn(value); when(mockPlayer.getInventory()).thenReturn(value);
assertEquals(value, player.getInventory()); assertEquals(value, mockPlayer.getInventory());
User user = User.getInstance(player); User user = User.getInstance(mockPlayer);
assertNotNull(user.getInventory()); assertNotNull(user.getInventory());
assertEquals(value, user.getInventory()); assertEquals(value, user.getInventory());
} }
@ -216,8 +203,8 @@ public class UserTest {
@Test @Test
public void testGetLocation() { public void testGetLocation() {
Location loc = mock(Location.class); Location loc = mock(Location.class);
when(player.getLocation()).thenReturn(loc); when(mockPlayer.getLocation()).thenReturn(loc);
User user = User.getInstance(player); User user = User.getInstance(mockPlayer);
assertNotNull(user.getLocation()); assertNotNull(user.getLocation());
assertEquals(loc, user.getLocation()); assertEquals(loc, user.getLocation());
} }
@ -225,8 +212,8 @@ public class UserTest {
@Test @Test
public void testGetName() { public void testGetName() {
String name = "tastybento"; String name = "tastybento";
when(player.getName()).thenReturn(name); when(mockPlayer.getName()).thenReturn(name);
User user = User.getInstance(player); User user = User.getInstance(mockPlayer);
assertNotNull(user.getName()); assertNotNull(user.getName());
assertEquals(name, user.getName()); assertEquals(name, user.getName());
@ -234,15 +221,15 @@ public class UserTest {
@Test @Test
public void testGetPlayer() { public void testGetPlayer() {
User user = User.getInstance(player); User user = User.getInstance(mockPlayer);
assertEquals(player, user.getPlayer()); assertEquals(mockPlayer, user.getPlayer());
} }
@Test @Test
public void testIsPlayer() { public void testIsPlayer() {
User user = User.getInstance(sender); User user = User.getInstance(sender);
assertFalse(user.isPlayer()); assertFalse(user.isPlayer());
user = User.getInstance(player); user = User.getInstance(mockPlayer);
assertTrue(user.isPlayer()); assertTrue(user.isPlayer());
} }
@ -255,8 +242,8 @@ public class UserTest {
@Test @Test
public void testGetUniqueId() { public void testGetUniqueId() {
UUID uuid = UUID.randomUUID(); UUID uuid = UUID.randomUUID();
when(player.getUniqueId()).thenReturn(uuid); when(mockPlayer.getUniqueId()).thenReturn(uuid);
user = User.getInstance(player); user = User.getInstance(mockPlayer);
assertEquals(uuid, user.getUniqueId()); assertEquals(uuid, user.getUniqueId());
} }
@ -267,7 +254,7 @@ public class UserTest {
assertTrue(user.hasPermission(null)); assertTrue(user.hasPermission(null));
// test if player has the permission // test if player has the permission
when(player.hasPermission(anyString())).thenReturn(true); when(mockPlayer.hasPermission(anyString())).thenReturn(true);
assertTrue(user.hasPermission("perm")); assertTrue(user.hasPermission("perm"));
} }
@ -283,13 +270,13 @@ public class UserTest {
@Test @Test
public void testIsOnline() { public void testIsOnline() {
when(player.isOnline()).thenReturn(true); when(mockPlayer.isOnline()).thenReturn(true);
assertTrue(user.isOnline()); assertTrue(user.isOnline());
} }
@Test @Test
public void testIsOp() { public void testIsOp() {
when(player.isOp()).thenReturn(true); when(mockPlayer.isOp()).thenReturn(true);
assertTrue(user.isOp()); assertTrue(user.isOp());
} }
@ -325,7 +312,7 @@ public class UserTest {
when(lm.get(any(), any())).thenReturn("fake.reference"); when(lm.get(any(), any())).thenReturn("fake.reference");
when(lm.get(any())).thenReturn("fake.reference"); when(lm.get(any())).thenReturn("fake.reference");
User user = User.getInstance(player); User user = User.getInstance(mockPlayer);
assertEquals("", user.getTranslationOrNothing("fake.reference")); assertEquals("", user.getTranslationOrNothing("fake.reference"));
assertEquals("", user.getTranslationOrNothing("fake.reference", "[test]", "variable")); assertEquals("", user.getTranslationOrNothing("fake.reference", "[test]", "variable"));
} }
@ -333,7 +320,7 @@ public class UserTest {
@Test @Test
public void testSendMessage() { public void testSendMessage() {
user.sendMessage("a.reference"); user.sendMessage("a.reference");
verify(player).sendMessage(TEST_TRANSLATION_WITH_COLOR); checkSpigotMessage(TEST_TRANSLATION_WITH_COLOR);
} }
@Test @Test
@ -347,8 +334,8 @@ public class UserTest {
when(iwm .getAddon(any())).thenReturn(optionalAddon); when(iwm .getAddon(any())).thenReturn(optionalAddon);
when(lm.get(any(), eq("name.a.reference"))).thenReturn("mockmockmock"); when(lm.get(any(), eq("name.a.reference"))).thenReturn("mockmockmock");
user.sendMessage("a.reference"); user.sendMessage("a.reference");
verify(player, never()).sendMessage(eq(TEST_TRANSLATION)); verify(mockPlayer, never()).sendMessage(eq(TEST_TRANSLATION));
verify(player).sendMessage(eq("mockmockmock")); checkSpigotMessage("mockmockmock");
} }
@Test @Test
@ -356,7 +343,7 @@ public class UserTest {
// Nothing - blank translation // Nothing - blank translation
when(lm.get(any(), any())).thenReturn(""); when(lm.get(any(), any())).thenReturn("");
user.sendMessage("a.reference"); user.sendMessage("a.reference");
verify(player, never()).sendMessage(anyString()); checkSpigotMessage("a.reference", 0);
} }
@Test @Test
@ -368,21 +355,21 @@ public class UserTest {
} }
when(lm.get(any(), any())).thenReturn(allColors.toString()); when(lm.get(any(), any())).thenReturn(allColors.toString());
user.sendMessage("a.reference"); user.sendMessage("a.reference");
verify(player, never()).sendMessage(anyString()); verify(mockPlayer, never()).sendMessage(anyString());
} }
@Test @Test
public void testSendMessageColorsAndSpaces() { public void testSendMessageColorsAndSpaces() {
when(lm.get(any(), any())).thenReturn(ChatColor.COLOR_CHAR + "6 Hello there"); when(lm.get(any(), any())).thenReturn(ChatColor.COLOR_CHAR + "6 Hello there");
user.sendMessage("a.reference"); user.sendMessage("a.reference");
verify(player).sendMessage(eq(ChatColor.COLOR_CHAR + "6Hello there")); checkSpigotMessage(ChatColor.COLOR_CHAR + "6Hello there");
} }
@Test @Test
public void testSendRawMessage() { public void testSendRawMessage() {
String raw = ChatColor.RED + "" + ChatColor.BOLD + "test message"; String raw = ChatColor.RED + "" + ChatColor.BOLD + "test message";
user.sendRawMessage(raw); user.sendRawMessage(raw);
verify(player).sendMessage(raw); checkSpigotMessage(raw);
} }
@Test @Test
@ -390,7 +377,7 @@ public class UserTest {
String raw = ChatColor.RED + "" + ChatColor.BOLD + "test message"; String raw = ChatColor.RED + "" + ChatColor.BOLD + "test message";
user = User.getInstance((CommandSender)null); user = User.getInstance((CommandSender)null);
user.sendRawMessage(raw); user.sendRawMessage(raw);
verify(player, never()).sendMessage(anyString()); checkSpigotMessage(raw, 0);
} }
@Test @Test
@ -414,29 +401,29 @@ public class UserTest {
for (GameMode gm: GameMode.values()) { for (GameMode gm: GameMode.values()) {
user.setGameMode(gm); user.setGameMode(gm);
} }
verify(player, times(GameMode.values().length)).setGameMode(any()); verify(mockPlayer, times(GameMode.values().length)).setGameMode(any());
} }
@Test @Test
public void testTeleport() { public void testTeleport() {
when(player.teleport(any(Location.class))).thenReturn(true); when(mockPlayer.teleport(any(Location.class))).thenReturn(true);
Location loc = mock(Location.class); Location loc = mock(Location.class);
user.teleport(loc); user.teleport(loc);
verify(player).teleport(loc); verify(mockPlayer).teleport(loc);
} }
@Test @Test
public void testGetWorld() { public void testGetWorld() {
World world = mock(World.class); World world = mock(World.class);
when(player.getWorld()).thenReturn(world); when(mockPlayer.getWorld()).thenReturn(world);
User user = User.getInstance(player); User user = User.getInstance(mockPlayer);
assertEquals(world, user.getWorld()); assertEquals(world, user.getWorld());
} }
@Test @Test
public void testCloseInventory() { public void testCloseInventory() {
user.closeInventory(); user.closeInventory();
verify(player).closeInventory(); verify(mockPlayer).closeInventory();
} }
@Test @Test
@ -468,13 +455,13 @@ public class UserTest {
@Test @Test
public void testUpdateInventory() { public void testUpdateInventory() {
user.updateInventory(); user.updateInventory();
verify(player).updateInventory(); verify(mockPlayer).updateInventory();
} }
@Test @Test
public void testPerformCommand() { public void testPerformCommand() {
user.performCommand("test"); user.performCommand("test");
verify(player).performCommand("test"); verify(mockPlayer).performCommand("test");
} }
@Test @Test
@ -520,8 +507,8 @@ public class UserTest {
permSet.add(pai); permSet.add(pai);
permSet.add(pai2); permSet.add(pai2);
permSet.add(pai3); permSet.add(pai3);
when(player.getEffectivePermissions()).thenReturn(permSet); when(mockPlayer.getEffectivePermissions()).thenReturn(permSet);
User u = User.getInstance(player); User u = User.getInstance(mockPlayer);
assertEquals(33, u.getPermissionValue("bskyblock.max", 2)); assertEquals(33, u.getPermissionValue("bskyblock.max", 2));
} }
@ -544,8 +531,8 @@ public class UserTest {
permSet.add(pai); permSet.add(pai);
permSet.add(pai2); permSet.add(pai2);
permSet.add(pai3); permSet.add(pai3);
when(player.getEffectivePermissions()).thenReturn(permSet); when(mockPlayer.getEffectivePermissions()).thenReturn(permSet);
User u = User.getInstance(player); User u = User.getInstance(mockPlayer);
assertEquals(7, u.getPermissionValue("bskyblock.max", 2)); assertEquals(7, u.getPermissionValue("bskyblock.max", 2));
} }
@ -579,8 +566,8 @@ public class UserTest {
permSet.add(pai); permSet.add(pai);
permSet.add(pai2); permSet.add(pai2);
permSet.add(pai3); permSet.add(pai3);
when(player.getEffectivePermissions()).thenReturn(permSet); when(mockPlayer.getEffectivePermissions()).thenReturn(permSet);
User u = User.getInstance(player); User u = User.getInstance(mockPlayer);
assertEquals(-1, u.getPermissionValue("bskyblock.max", 2)); assertEquals(-1, u.getPermissionValue("bskyblock.max", 2));
} }
@ -600,8 +587,8 @@ public class UserTest {
permSet.add(pai); permSet.add(pai);
permSet.add(pai2); permSet.add(pai2);
permSet.add(pai3); permSet.add(pai3);
when(player.getEffectivePermissions()).thenReturn(permSet); when(mockPlayer.getEffectivePermissions()).thenReturn(permSet);
User u = User.getInstance(player); User u = User.getInstance(mockPlayer);
assertEquals(22, u.getPermissionValue("bskyblock.max", 22)); assertEquals(22, u.getPermissionValue("bskyblock.max", 22));
} }
@ -614,14 +601,14 @@ public class UserTest {
PermissionAttachmentInfo pai = mock(PermissionAttachmentInfo.class); PermissionAttachmentInfo pai = mock(PermissionAttachmentInfo.class);
when(pai.getPermission()).thenReturn("bskyblock.max.3"); when(pai.getPermission()).thenReturn("bskyblock.max.3");
when(pai.getValue()).thenReturn(true); when(pai.getValue()).thenReturn(true);
when(player.getEffectivePermissions()).thenReturn(Collections.singleton(pai)); when(mockPlayer.getEffectivePermissions()).thenReturn(Collections.singleton(pai));
User u = User.getInstance(player); User u = User.getInstance(mockPlayer);
assertEquals(3, u.getPermissionValue("bskyblock.max", 22)); assertEquals(3, u.getPermissionValue("bskyblock.max", 22));
} }
@Test @Test
public void testMetaData() { public void testMetaData() {
User u = User.getInstance(player); User u = User.getInstance(mockPlayer);
assertTrue(u.getMetaData().get().isEmpty()); assertTrue(u.getMetaData().get().isEmpty());
// Store a string in a new key // Store a string in a new key
assertFalse(u.putMetaData("string", new MetaDataValue("a string")).isPresent()); assertFalse(u.putMetaData("string", new MetaDataValue("a string")).isPresent());
@ -686,7 +673,7 @@ public class UserTest {
User s = User.getInstance(sender); User s = User.getInstance(sender);
assertFalse(s.isOfflinePlayer()); assertFalse(s.isOfflinePlayer());
User.clearUsers(); User.clearUsers();
User p = User.getInstance(player); User p = User.getInstance(mockPlayer);
assertTrue(p.isOfflinePlayer()); assertTrue(p.isOfflinePlayer());
} }
@ -696,9 +683,9 @@ public class UserTest {
@Test @Test
public void testAddPerm() { public void testAddPerm() {
User.clearUsers(); User.clearUsers();
User p = User.getInstance(player); User p = User.getInstance(mockPlayer);
p.addPerm("test.perm"); p.addPerm("test.perm");
verify(player).addAttachment(plugin, "test.perm", true); verify(mockPlayer).addAttachment(plugin, "test.perm", true);
} }
/** /**
@ -708,19 +695,19 @@ public class UserTest {
public void testRemovePerm() { public void testRemovePerm() {
User.clearUsers(); User.clearUsers();
// No perms to start // No perms to start
when(player.getEffectivePermissions()).thenReturn(Collections.emptySet()); when(mockPlayer.getEffectivePermissions()).thenReturn(Collections.emptySet());
when(player.hasPermission(anyString())).thenReturn(false); when(mockPlayer.hasPermission(anyString())).thenReturn(false);
User p = User.getInstance(player); User p = User.getInstance(mockPlayer);
assertTrue(p.removePerm("test.perm")); assertTrue(p.removePerm("test.perm"));
verify(player).recalculatePermissions(); verify(mockPlayer).recalculatePermissions();
// Has the perm // Has the perm
PermissionAttachmentInfo pi = mock(PermissionAttachmentInfo.class); PermissionAttachmentInfo pi = mock(PermissionAttachmentInfo.class);
when(pi.getPermission()).thenReturn("test.perm"); when(pi.getPermission()).thenReturn("test.perm");
PermissionAttachment attachment = mock(PermissionAttachment.class); PermissionAttachment attachment = mock(PermissionAttachment.class);
when(pi.getAttachment()).thenReturn(attachment); when(pi.getAttachment()).thenReturn(attachment);
when(player.getEffectivePermissions()).thenReturn(Set.of(pi)); when(mockPlayer.getEffectivePermissions()).thenReturn(Set.of(pi));
assertTrue(p.removePerm("test.perm")); assertTrue(p.removePerm("test.perm"));
verify(player).removeAttachment(attachment); verify(mockPlayer).removeAttachment(attachment);
} }
@ -731,7 +718,7 @@ public class UserTest {
@Test @Test
public void testGetTranslationWorldStringStringArray() { public void testGetTranslationWorldStringStringArray() {
User.clearUsers(); User.clearUsers();
User p = User.getInstance(player); User p = User.getInstance(mockPlayer);
// No addon // No addon
World world = mock(World.class); World world = mock(World.class);
assertEquals("mock §atranslation §btastybento", p.getTranslation(world, "test.ref", "[test]", "tastybento")); assertEquals("mock §atranslation §btastybento", p.getTranslation(world, "test.ref", "[test]", "tastybento"));
@ -743,7 +730,7 @@ public class UserTest {
@Test @Test
public void testGetTranslationWorldStringStringArrayWwithAddon() { public void testGetTranslationWorldStringStringArrayWwithAddon() {
User.clearUsers(); User.clearUsers();
User p = User.getInstance(player); User p = User.getInstance(mockPlayer);
World world = mock(World.class); World world = mock(World.class);
GameModeAddon gameAddon = mock(GameModeAddon.class); GameModeAddon gameAddon = mock(GameModeAddon.class);
@ -759,7 +746,7 @@ public class UserTest {
@Test @Test
public void testGetTranslationStringStringArray() { public void testGetTranslationStringStringArray() {
User.clearUsers(); User.clearUsers();
User p = User.getInstance(player); User p = User.getInstance(mockPlayer);
assertEquals("mock §atranslation §btastybento", p.getTranslation("test.ref", "[test]", "tastybento")); assertEquals("mock §atranslation §btastybento", p.getTranslation("test.ref", "[test]", "tastybento"));
} }
@ -771,7 +758,7 @@ public class UserTest {
Notifier notifier = mock(Notifier.class); Notifier notifier = mock(Notifier.class);
when(plugin.getNotifier()).thenReturn(notifier); when(plugin.getNotifier()).thenReturn(notifier);
User.clearUsers(); User.clearUsers();
User p = User.getInstance(player); User p = User.getInstance(mockPlayer);
p.notify(TEST_TRANSLATION, "[test]", "tastybento"); p.notify(TEST_TRANSLATION, "[test]", "tastybento");
verify(notifier).notify(any(User.class), eq("mock §atranslation §btastybento")); verify(notifier).notify(any(User.class), eq("mock §atranslation §btastybento"));
@ -785,7 +772,7 @@ public class UserTest {
Notifier notifier = mock(Notifier.class); Notifier notifier = mock(Notifier.class);
when(plugin.getNotifier()).thenReturn(notifier); when(plugin.getNotifier()).thenReturn(notifier);
User.clearUsers(); User.clearUsers();
User p = User.getInstance(player); User p = User.getInstance(mockPlayer);
World world = mock(World.class); World world = mock(World.class);
GameModeAddon gameAddon = mock(GameModeAddon.class); GameModeAddon gameAddon = mock(GameModeAddon.class);
@ -820,7 +807,7 @@ public class UserTest {
when(plugin.getSettings()).thenReturn(settings); when(plugin.getSettings()).thenReturn(settings);
when(pm.getLocale(uuid)).thenReturn("fr-FR"); when(pm.getLocale(uuid)).thenReturn("fr-FR");
User.clearUsers(); User.clearUsers();
User p = User.getInstance(player); User p = User.getInstance(mockPlayer);
assertEquals(Locale.FRANCE, p.getLocale()); assertEquals(Locale.FRANCE, p.getLocale());
} }
@ -830,8 +817,8 @@ public class UserTest {
@Test @Test
public void testInWorld() { public void testInWorld() {
User.clearUsers(); User.clearUsers();
User p = User.getInstance(player); User p = User.getInstance(mockPlayer);
when(player.getLocation()).thenReturn(mock(Location.class)); when(mockPlayer.getLocation()).thenReturn(mock(Location.class));
when(iwm.inWorld(any(Location.class))).thenReturn(false); when(iwm.inWorld(any(Location.class))).thenReturn(false);
assertFalse(p.inWorld()); assertFalse(p.inWorld());
when(iwm.inWorld(any(Location.class))).thenReturn(true); when(iwm.inWorld(any(Location.class))).thenReturn(true);
@ -844,7 +831,7 @@ public class UserTest {
@Test @Test
public void testSpawnParticleParticleObjectDoubleDoubleDoubleError() { public void testSpawnParticleParticleObjectDoubleDoubleDoubleError() {
User.clearUsers(); User.clearUsers();
User p = User.getInstance(player); User p = User.getInstance(mockPlayer);
try { try {
p.spawnParticle(Particle.DUST, 4, 0.0d, 0.0d, 0.0d); p.spawnParticle(Particle.DUST, 4, 0.0d, 0.0d, 0.0d);
} catch (Exception e) { } catch (Exception e) {
@ -860,13 +847,13 @@ public class UserTest {
public void testSpawnParticleParticleObjectDoubleDoubleDouble() { public void testSpawnParticleParticleObjectDoubleDoubleDouble() {
User.clearUsers(); User.clearUsers();
Location loc = mock(Location.class); Location loc = mock(Location.class);
when(player.getLocation()).thenReturn(loc); when(mockPlayer.getLocation()).thenReturn(loc);
when(loc.toVector()).thenReturn(new Vector(1,1,1)); when(loc.toVector()).thenReturn(new Vector(1,1,1));
when(server.getViewDistance()).thenReturn(16); when(server.getViewDistance()).thenReturn(16);
User p = User.getInstance(player); User p = User.getInstance(mockPlayer);
p.spawnParticle(Particle.SHRIEK, 4, 0.0d, 0.0d, 0.0d); p.spawnParticle(Particle.SHRIEK, 4, 0.0d, 0.0d, 0.0d);
verify(player).spawnParticle(Particle.SHRIEK, 0.0d, 0.0d, 0.0d, 1, 4); verify(mockPlayer).spawnParticle(Particle.SHRIEK, 0.0d, 0.0d, 0.0d, 1, 4);
} }
@ -877,14 +864,14 @@ public class UserTest {
public void testSpawnParticleParticleObjectDoubleDoubleDoubleRedstone() { public void testSpawnParticleParticleObjectDoubleDoubleDoubleRedstone() {
User.clearUsers(); User.clearUsers();
Location loc = mock(Location.class); Location loc = mock(Location.class);
when(player.getLocation()).thenReturn(loc); when(mockPlayer.getLocation()).thenReturn(loc);
when(loc.toVector()).thenReturn(new Vector(1,1,1)); when(loc.toVector()).thenReturn(new Vector(1,1,1));
when(server.getViewDistance()).thenReturn(16); when(server.getViewDistance()).thenReturn(16);
User p = User.getInstance(player); User p = User.getInstance(mockPlayer);
DustOptions dust = mock(DustOptions.class); DustOptions dust = mock(DustOptions.class);
p.spawnParticle(Particle.DUST, dust, 0.0d, 0.0d, 0.0d); p.spawnParticle(Particle.DUST, dust, 0.0d, 0.0d, 0.0d);
verify(player).spawnParticle(Particle.DUST, 0.0d, 0.0d, 0.0d, 1, 0, 0, 0, 1, dust); verify(mockPlayer).spawnParticle(Particle.DUST, 0.0d, 0.0d, 0.0d, 1, 0, 0, 0, 1, dust);
} }
@ -895,14 +882,14 @@ public class UserTest {
public void testSpawnParticleParticleDustOptionsDoubleDoubleDouble() { public void testSpawnParticleParticleDustOptionsDoubleDoubleDouble() {
User.clearUsers(); User.clearUsers();
Location loc = mock(Location.class); Location loc = mock(Location.class);
when(player.getLocation()).thenReturn(loc); when(mockPlayer.getLocation()).thenReturn(loc);
when(loc.toVector()).thenReturn(new Vector(1,1,1)); when(loc.toVector()).thenReturn(new Vector(1,1,1));
when(server.getViewDistance()).thenReturn(16); when(server.getViewDistance()).thenReturn(16);
User p = User.getInstance(player); User p = User.getInstance(mockPlayer);
DustOptions dust = mock(DustOptions.class); DustOptions dust = mock(DustOptions.class);
p.spawnParticle(Particle.DUST, dust, 0.0d, 0.0d, 0.0d); p.spawnParticle(Particle.DUST, dust, 0.0d, 0.0d, 0.0d);
verify(player).spawnParticle(Particle.DUST, 0.0d, 0.0d, 0.0d, 1, 0, 0, 0, 1, dust); verify(mockPlayer).spawnParticle(Particle.DUST, 0.0d, 0.0d, 0.0d, 1, 0, 0, 0, 1, dust);
} }
@ -913,14 +900,14 @@ public class UserTest {
public void testSpawnParticleParticleDustOptionsIntIntInt() { public void testSpawnParticleParticleDustOptionsIntIntInt() {
User.clearUsers(); User.clearUsers();
Location loc = mock(Location.class); Location loc = mock(Location.class);
when(player.getLocation()).thenReturn(loc); when(mockPlayer.getLocation()).thenReturn(loc);
when(loc.toVector()).thenReturn(new Vector(1,1,1)); when(loc.toVector()).thenReturn(new Vector(1,1,1));
when(server.getViewDistance()).thenReturn(16); when(server.getViewDistance()).thenReturn(16);
User p = User.getInstance(player); User p = User.getInstance(mockPlayer);
DustOptions dust = mock(DustOptions.class); DustOptions dust = mock(DustOptions.class);
p.spawnParticle(Particle.DUST, dust, 0, 0, 0); p.spawnParticle(Particle.DUST, dust, 0, 0, 0);
verify(player).spawnParticle(Particle.DUST, 0.0d, 0.0d, 0.0d, 1, 0, 0, 0, 1, dust); verify(mockPlayer).spawnParticle(Particle.DUST, 0.0d, 0.0d, 0.0d, 1, 0, 0, 0, 1, dust);
} }
@ -930,7 +917,7 @@ public class UserTest {
@Test @Test
public void testSetAddon() { public void testSetAddon() {
User.clearUsers(); User.clearUsers();
User p = User.getInstance(player); User p = User.getInstance(mockPlayer);
Addon addon = mock(Addon.class); Addon addon = mock(Addon.class);
when(addon.getDescription()).thenReturn(new Builder("main", "gameAddon", "1.0").build()); when(addon.getDescription()).thenReturn(new Builder("main", "gameAddon", "1.0").build());
p.setAddon(addon); p.setAddon(addon);
@ -944,7 +931,7 @@ public class UserTest {
@Test @Test
public void testGetMetaData() { public void testGetMetaData() {
User.clearUsers(); User.clearUsers();
User p = User.getInstance(player); User p = User.getInstance(mockPlayer);
when(pm.getPlayer(uuid)).thenReturn(players); when(pm.getPlayer(uuid)).thenReturn(players);
assertEquals(Optional.of(new HashMap<>()), p.getMetaData()); assertEquals(Optional.of(new HashMap<>()), p.getMetaData());
} }
@ -955,7 +942,7 @@ public class UserTest {
@Test @Test
public void testSetMetaData() { public void testSetMetaData() {
User.clearUsers(); User.clearUsers();
User p = User.getInstance(player); User p = User.getInstance(mockPlayer);
when(pm.getPlayer(uuid)).thenReturn(players); when(pm.getPlayer(uuid)).thenReturn(players);
Map<String, MetaDataValue> metaData = new HashMap<>(); Map<String, MetaDataValue> metaData = new HashMap<>();
p.setMetaData(metaData); p.setMetaData(metaData);

View File

@ -7,7 +7,6 @@ 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.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@ -21,7 +20,6 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.permissions.Permission; import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionDefault; import org.bukkit.permissions.PermissionDefault;
import org.bukkit.plugin.PluginManager;
import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNull;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
@ -30,29 +28,26 @@ 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 world.bentobox.bentobox.AbstractCommonSetup;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.commands.CompositeCommand; 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.managers.CommandsManager; import world.bentobox.bentobox.managers.CommandsManager;
import world.bentobox.bentobox.managers.IslandWorldManager;
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.util.Util;
/** /**
* @author tastybento * @author tastybento
* *
*/ */
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest({Bukkit.class, BentoBox.class, User.class }) @PrepareForTest({ Bukkit.class, BentoBox.class, User.class, Util.class })
public class BentoBoxPermsCommandTest { public class BentoBoxPermsCommandTest extends AbstractCommonSetup {
@Mock
private BentoBox plugin;
@Mock @Mock
private CompositeCommand ac; private CompositeCommand ac;
@Mock @Mock
@ -64,8 +59,6 @@ public class BentoBoxPermsCommandTest {
@Mock @Mock
private PlaceholdersManager phm; private PlaceholdersManager phm;
@Mock @Mock
private PluginManager pim;
@Mock
private Permission perm; private Permission perm;
private PermissionDefault defaultPerm = PermissionDefault.OP; private PermissionDefault defaultPerm = PermissionDefault.OP;
@ -74,8 +67,7 @@ public class BentoBoxPermsCommandTest {
*/ */
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
// Set up plugin super.setUp();
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
// Command manager // Command manager
CommandsManager cm = mock(CommandsManager.class); CommandsManager cm = mock(CommandsManager.class);
@ -101,10 +93,8 @@ public class BentoBoxPermsCommandTest {
User.setPlugin(plugin); User.setPlugin(plugin);
// Bukkit // Bukkit
PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS);
when(perm.getDefault()).thenReturn(defaultPerm); when(perm.getDefault()).thenReturn(defaultPerm);
when(pim.getPermission(anyString())).thenReturn(perm); when(pim.getPermission(anyString())).thenReturn(perm);
when(Bukkit.getPluginManager()).thenReturn(pim);
// Placeholders // Placeholders
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));
@ -112,11 +102,6 @@ public class BentoBoxPermsCommandTest {
// BentoBox // BentoBox
when(plugin.getLocalesManager()).thenReturn(lm); when(plugin.getLocalesManager()).thenReturn(lm);
when(plugin.getPlaceholdersManager()).thenReturn(phm); when(plugin.getPlaceholdersManager()).thenReturn(phm);
IslandWorldManager iwm = mock(IslandWorldManager.class);
when(plugin.getIWM()).thenReturn(iwm);
// Commands for perms
cmd = new BentoBoxPermsCommand(ac); cmd = new BentoBoxPermsCommand(ac);
} }
@ -172,9 +157,10 @@ public class BentoBoxPermsCommandTest {
public void testExecuteUserStringListOfStringConsole() { public void testExecuteUserStringListOfStringConsole() {
String[] args = new String[1]; String[] args = new String[1];
args[0] = ""; args[0] = "";
CommandSender p = mock(CommandSender.class); CommandSender console = mock(CommandSender.class);
assertTrue(cmd.execute(p, "perms", args)); when(console.spigot()).thenReturn(spigot);
verify(p, never()).sendMessage("general.errors.use-in-console"); assertTrue(cmd.execute(console, "perms", args));
checkSpigotMessage("general.errors.use-in-console", 0);
} }
/** /**
@ -185,8 +171,7 @@ public class BentoBoxPermsCommandTest {
when(user.isPlayer()).thenReturn(true); when(user.isPlayer()).thenReturn(true);
String[] args = new String[1]; String[] args = new String[1];
args[0] = ""; args[0] = "";
Player p = mock(Player.class); assertFalse(cmd.execute(mockPlayer, "perms", args));
assertFalse(cmd.execute(p, "perms", args)); checkSpigotMessage("general.errors.use-in-console");
verify(p).sendMessage("general.errors.use-in-console");
} }
} }

View File

@ -41,13 +41,13 @@ import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox; import org.powermock.reflect.Whitebox;
import dev.lone.itemsadder.api.CustomBlock; import dev.lone.itemsadder.api.CustomBlock;
import world.bentobox.bentobox.AbstractCommonSetup;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.user.Notifier; import world.bentobox.bentobox.api.user.Notifier;
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.database.objects.Players; import world.bentobox.bentobox.database.objects.Players;
import world.bentobox.bentobox.hooks.ItemsAdderHook.BlockInteractListener; import world.bentobox.bentobox.hooks.ItemsAdderHook.BlockInteractListener;
import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup;
import world.bentobox.bentobox.managers.FlagsManager; import world.bentobox.bentobox.managers.FlagsManager;
import world.bentobox.bentobox.managers.IslandWorldManager; import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.IslandsManager;

View File

@ -22,10 +22,10 @@ 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;
import world.bentobox.bentobox.AbstractCommonSetup;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.configuration.WorldSettings; import world.bentobox.bentobox.api.configuration.WorldSettings;
import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup;
import world.bentobox.bentobox.managers.IslandWorldManager; import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.PlayersManager; import world.bentobox.bentobox.managers.PlayersManager;
import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.Util;

View File

@ -21,8 +21,6 @@ import java.util.Set;
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.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerChangedWorldEvent; import org.bukkit.event.player.PlayerChangedWorldEvent;
import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerJoinEvent;
@ -30,9 +28,7 @@ import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.PlayerInventory; import org.bukkit.inventory.PlayerInventory;
import org.bukkit.permissions.PermissionAttachmentInfo; import org.bukkit.permissions.PermissionAttachmentInfo;
import org.bukkit.plugin.PluginManager;
import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.scheduler.BukkitScheduler;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
@ -41,19 +37,17 @@ 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 world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.RanksManagerBeforeClassTest;
import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.Settings;
import world.bentobox.bentobox.api.addons.GameModeAddon; import world.bentobox.bentobox.api.addons.GameModeAddon;
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.database.objects.Players; import world.bentobox.bentobox.database.objects.Players;
import world.bentobox.bentobox.managers.AddonsManager; import world.bentobox.bentobox.managers.AddonsManager;
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;
@ -67,34 +61,24 @@ import world.bentobox.bentobox.util.Util;
*/ */
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest({ BentoBox.class, Util.class, Bukkit.class, IslandsManager.class }) @PrepareForTest({ BentoBox.class, Util.class, Bukkit.class, IslandsManager.class })
public class JoinLeaveListenerTest { public class JoinLeaveListenerTest extends RanksManagerBeforeClassTest {
private static final String[] NAMES = { "adam", "ben", "cara", "dave", "ed", "frank", "freddy", "george", "harry", private static final String[] NAMES = { "adam", "ben", "cara", "dave", "ed", "frank", "freddy", "george", "harry",
"ian", "joe" }; "ian", "joe" };
@Mock
private BentoBox plugin;
@Mock @Mock
private PlayersManager pm; private PlayersManager pm;
@Mock @Mock
private Player player;
@Mock
private Player coopPlayer; private Player coopPlayer;
@Mock
private World world;
private JoinLeaveListener jll; private JoinLeaveListener jll;
@Mock @Mock
private Players pls; private Players pls;
@Mock @Mock
private IslandWorldManager iwm;
@Mock
private Inventory chest; private Inventory chest;
@Mock @Mock
private Settings settings; private Settings settings;
@Mock @Mock
private IslandsManager im;
@Mock
private BukkitScheduler scheduler; private BukkitScheduler scheduler;
@Mock @Mock
private PlayerInventory inv; private PlayerInventory inv;
@ -103,10 +87,6 @@ public class JoinLeaveListenerTest {
private @Nullable Island island; private @Nullable Island island;
@Mock @Mock
private GameModeAddon gameMode; private GameModeAddon gameMode;
@Mock
private PluginManager pim;
@Mock
private @NonNull Location location;
@Mock @Mock
private AddonsManager am; private AddonsManager am;
@ -115,10 +95,7 @@ public class JoinLeaveListenerTest {
*/ */
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
PowerMockito.mockStatic(IslandsManager.class, Mockito.RETURNS_MOCKS); super.setUp();
// Set up plugin
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
// World // World
when(world.getName()).thenReturn("worldname"); when(world.getName()).thenReturn("worldname");
@ -138,12 +115,12 @@ public class JoinLeaveListenerTest {
UUID uuid = UUID.randomUUID(); UUID uuid = UUID.randomUUID();
// Player // Player
when(player.getUniqueId()).thenReturn(uuid); when(mockPlayer.getUniqueId()).thenReturn(uuid);
when(player.getWorld()).thenReturn(world); when(mockPlayer.getWorld()).thenReturn(world);
when(player.getEnderChest()).thenReturn(chest); when(mockPlayer.getEnderChest()).thenReturn(chest);
when(player.getName()).thenReturn("tastybento"); when(mockPlayer.getName()).thenReturn("tastybento");
when(player.getInventory()).thenReturn(inv); when(mockPlayer.getInventory()).thenReturn(inv);
when(player.getEffectivePermissions()).thenReturn(Collections.emptySet()); when(mockPlayer.getEffectivePermissions()).thenReturn(Collections.emptySet());
// Player is pending kicks // Player is pending kicks
set = new HashSet<>(); set = new HashSet<>();
@ -178,12 +155,12 @@ public class JoinLeaveListenerTest {
// Add a coop member // Add a coop member
UUID uuid2 = UUID.randomUUID(); UUID uuid2 = UUID.randomUUID();
when(coopPlayer.getUniqueId()).thenReturn(uuid2); when(coopPlayer.getUniqueId()).thenReturn(uuid2);
when(coopPlayer.spigot()).thenReturn(spigot);
User.getInstance(coopPlayer); User.getInstance(coopPlayer);
memberMap.put(uuid2, RanksManager.COOP_RANK); memberMap.put(uuid2, RanksManager.COOP_RANK);
island.setMembers(memberMap); island.setMembers(memberMap);
// Bukkit // Bukkit
PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS);
when(Bukkit.getScheduler()).thenReturn(scheduler); when(Bukkit.getScheduler()).thenReturn(scheduler);
when(Bukkit.getPluginManager()).thenReturn(pim); when(Bukkit.getPluginManager()).thenReturn(pim);
@ -200,14 +177,13 @@ public class JoinLeaveListenerTest {
online.put(u, name); online.put(u, name);
onlinePlayers.add(p1); onlinePlayers.add(p1);
} }
onlinePlayers.add(player); onlinePlayers.add(mockPlayer);
when(Bukkit.getOnlinePlayers()).then((Answer<Set<Player>>) invocation -> onlinePlayers); when(Bukkit.getOnlinePlayers()).then((Answer<Set<Player>>) invocation -> onlinePlayers);
User.setPlugin(plugin); User.setPlugin(plugin);
User.getInstance(player); User.getInstance(mockPlayer);
// Util // Util
PowerMockito.mockStatic(Util.class);
when(Util.getWorld(any())).thenReturn(world); when(Util.getWorld(any())).thenReturn(world);
// Util translate color codes (used in user translate methods) // Util translate color codes (used in user translate methods)
when(Util.translateColorCodes(anyString())) when(Util.translateColorCodes(anyString()))
@ -241,11 +217,11 @@ public class JoinLeaveListenerTest {
*/ */
@Test @Test
public void testOnPlayerJoinNotKnownNoAutoCreate() { public void testOnPlayerJoinNotKnownNoAutoCreate() {
PlayerJoinEvent event = new PlayerJoinEvent(player, ""); PlayerJoinEvent event = new PlayerJoinEvent(mockPlayer, "");
jll.onPlayerJoin(event); jll.onPlayerJoin(event);
// Verify // Verify
verify(pm, times(3)).getPlayer(any()); verify(pm, times(3)).getPlayer(any());
verify(player, never()).sendMessage(anyString()); verify(mockPlayer, never()).sendMessage(anyString());
// Verify resets // Verify resets
verify(pm).setResets(eq(world), any(), eq(0)); verify(pm).setResets(eq(world), any(), eq(0));
// Verify inventory clear because of kick // Verify inventory clear because of kick
@ -260,9 +236,9 @@ public class JoinLeaveListenerTest {
*/ */
@Test @Test
public void testOnPlayerJoinNullWorld() { public void testOnPlayerJoinNullWorld() {
when(player.getWorld()).thenReturn(null); // Null when(mockPlayer.getWorld()).thenReturn(null); // Null
when(Util.getWorld(any())).thenReturn(null); // Make null when(Util.getWorld(any())).thenReturn(null); // Make null
PlayerJoinEvent event = new PlayerJoinEvent(player, ""); PlayerJoinEvent event = new PlayerJoinEvent(mockPlayer, "");
jll.onPlayerJoin(event); jll.onPlayerJoin(event);
// Verify inventory clear because of kick // Verify inventory clear because of kick
// Check inventory cleared // Check inventory cleared
@ -280,11 +256,11 @@ public class JoinLeaveListenerTest {
PermissionAttachmentInfo pa = mock(PermissionAttachmentInfo.class); PermissionAttachmentInfo pa = mock(PermissionAttachmentInfo.class);
when(pa.getPermission()).thenReturn("acidisland.island.range.1000"); when(pa.getPermission()).thenReturn("acidisland.island.range.1000");
when(pa.getValue()).thenReturn(true); when(pa.getValue()).thenReturn(true);
when(player.getEffectivePermissions()).thenReturn(Collections.singleton(pa)); when(mockPlayer.getEffectivePermissions()).thenReturn(Collections.singleton(pa));
PlayerJoinEvent event = new PlayerJoinEvent(player, ""); PlayerJoinEvent event = new PlayerJoinEvent(mockPlayer, "");
jll.onPlayerJoin(event); jll.onPlayerJoin(event);
// Verify // Verify
verify(player).sendMessage(eq("commands.admin.setrange.range-updated")); checkSpigotMessage("commands.admin.setrange.range-updated");
// Verify island setting // Verify island setting
assertEquals(100, island.getProtectionRange()); assertEquals(100, island.getProtectionRange());
// Verify log // Verify log
@ -300,11 +276,11 @@ public class JoinLeaveListenerTest {
PermissionAttachmentInfo pa = mock(PermissionAttachmentInfo.class); PermissionAttachmentInfo pa = mock(PermissionAttachmentInfo.class);
when(pa.getPermission()).thenReturn("acidisland.island.range.10"); when(pa.getPermission()).thenReturn("acidisland.island.range.10");
when(pa.getValue()).thenReturn(true); when(pa.getValue()).thenReturn(true);
when(player.getEffectivePermissions()).thenReturn(Collections.singleton(pa)); when(mockPlayer.getEffectivePermissions()).thenReturn(Collections.singleton(pa));
PlayerJoinEvent event = new PlayerJoinEvent(player, ""); PlayerJoinEvent event = new PlayerJoinEvent(mockPlayer, "");
jll.onPlayerJoin(event); jll.onPlayerJoin(event);
// Verify // Verify
verify(player).sendMessage(eq("commands.admin.setrange.range-updated")); checkSpigotMessage("commands.admin.setrange.range-updated");
// Verify island setting // Verify island setting
assertEquals(10, island.getProtectionRange()); assertEquals(10, island.getProtectionRange());
// Verify log // Verify log
@ -320,11 +296,11 @@ public class JoinLeaveListenerTest {
PermissionAttachmentInfo pa = mock(PermissionAttachmentInfo.class); PermissionAttachmentInfo pa = mock(PermissionAttachmentInfo.class);
when(pa.getPermission()).thenReturn("acidisland.island.range.55"); when(pa.getPermission()).thenReturn("acidisland.island.range.55");
when(pa.getValue()).thenReturn(true); when(pa.getValue()).thenReturn(true);
when(player.getEffectivePermissions()).thenReturn(Collections.singleton(pa)); when(mockPlayer.getEffectivePermissions()).thenReturn(Collections.singleton(pa));
PlayerJoinEvent event = new PlayerJoinEvent(player, ""); PlayerJoinEvent event = new PlayerJoinEvent(mockPlayer, "");
jll.onPlayerJoin(event); jll.onPlayerJoin(event);
// Verify // Verify
verify(player).sendMessage(eq("commands.admin.setrange.range-updated")); checkSpigotMessage("commands.admin.setrange.range-updated");
// Verify island setting // Verify island setting
assertEquals(55, island.getProtectionRange()); assertEquals(55, island.getProtectionRange());
// Verify log // Verify log
@ -340,11 +316,11 @@ public class JoinLeaveListenerTest {
PermissionAttachmentInfo pa = mock(PermissionAttachmentInfo.class); PermissionAttachmentInfo pa = mock(PermissionAttachmentInfo.class);
when(pa.getPermission()).thenReturn("acidisland.island.range.50"); when(pa.getPermission()).thenReturn("acidisland.island.range.50");
when(pa.getValue()).thenReturn(true); when(pa.getValue()).thenReturn(true);
when(player.getEffectivePermissions()).thenReturn(Collections.singleton(pa)); when(mockPlayer.getEffectivePermissions()).thenReturn(Collections.singleton(pa));
PlayerJoinEvent event = new PlayerJoinEvent(player, ""); PlayerJoinEvent event = new PlayerJoinEvent(mockPlayer, "");
jll.onPlayerJoin(event); jll.onPlayerJoin(event);
// Verify // Verify
verify(player, never()).sendMessage(eq("commands.admin.setrange.range-updated")); verify(mockPlayer, never()).sendMessage(eq("commands.admin.setrange.range-updated"));
// Verify that the island protection range is not changed if it is already at // Verify that the island protection range is not changed if it is already at
// that value // that value
assertEquals(50, island.getProtectionRange()); assertEquals(50, island.getProtectionRange());
@ -358,11 +334,11 @@ public class JoinLeaveListenerTest {
@Test @Test
public void testOnPlayerJoinNotKnownAutoCreate() { public void testOnPlayerJoinNotKnownAutoCreate() {
when(iwm.isCreateIslandOnFirstLoginEnabled(eq(world))).thenReturn(true); when(iwm.isCreateIslandOnFirstLoginEnabled(eq(world))).thenReturn(true);
PlayerJoinEvent event = new PlayerJoinEvent(player, ""); PlayerJoinEvent event = new PlayerJoinEvent(mockPlayer, "");
jll.onPlayerJoin(event); jll.onPlayerJoin(event);
// Verify // Verify
verify(pm, times(3)).getPlayer(any()); verify(pm, times(3)).getPlayer(any());
verify(player).sendMessage(eq("commands.island.create.on-first-login")); checkSpigotMessage("commands.island.create.on-first-login");
} }
/** /**
@ -371,7 +347,7 @@ public class JoinLeaveListenerTest {
*/ */
@Test @Test
public void testOnPlayerSwitchWorld() { public void testOnPlayerSwitchWorld() {
PlayerChangedWorldEvent event = new PlayerChangedWorldEvent(player, world); PlayerChangedWorldEvent event = new PlayerChangedWorldEvent(mockPlayer, world);
jll.onPlayerSwitchWorld(event); jll.onPlayerSwitchWorld(event);
// Check inventory cleared // Check inventory cleared
verify(chest).clear(); verify(chest).clear();
@ -385,7 +361,7 @@ public class JoinLeaveListenerTest {
@Test @Test
public void testOnPlayerSwitchWorldNullWorld() { public void testOnPlayerSwitchWorldNullWorld() {
when(Util.getWorld(any())).thenReturn(null); when(Util.getWorld(any())).thenReturn(null);
PlayerChangedWorldEvent event = new PlayerChangedWorldEvent(player, world); PlayerChangedWorldEvent event = new PlayerChangedWorldEvent(mockPlayer, world);
jll.onPlayerSwitchWorld(event); jll.onPlayerSwitchWorld(event);
// These should not happen // These should not happen
verify(chest, never()).clear(); verify(chest, never()).clear();
@ -399,9 +375,9 @@ public class JoinLeaveListenerTest {
*/ */
@Test @Test
public void testOnPlayerQuit() { public void testOnPlayerQuit() {
PlayerQuitEvent event = new PlayerQuitEvent(player, ""); PlayerQuitEvent event = new PlayerQuitEvent(mockPlayer, "");
jll.onPlayerQuit(event); jll.onPlayerQuit(event);
verify(coopPlayer).sendMessage(eq("commands.island.team.uncoop.all-members-logged-off")); checkSpigotMessage("commands.island.team.uncoop.all-members-logged-off");
// Team is now only 1 big // Team is now only 1 big
assertEquals(1, island.getMembers().size()); assertEquals(1, island.getMembers().size());
} }

View File

@ -6,13 +6,10 @@ 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.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.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
@ -20,7 +17,6 @@ import org.bukkit.World;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState; import org.bukkit.block.BlockState;
import org.bukkit.entity.Player;
import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.EntityExplodeEvent; import org.bukkit.event.entity.EntityExplodeEvent;
@ -34,17 +30,13 @@ 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 world.bentobox.bentobox.AbstractCommonSetup;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.configuration.WorldSettings; import world.bentobox.bentobox.api.configuration.WorldSettings;
import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup;
import world.bentobox.bentobox.managers.IslandWorldManager;
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;
@ -58,25 +50,13 @@ import world.bentobox.bentobox.util.Util;
@PrepareForTest({Bukkit.class, BentoBox.class, User.class, Util.class }) @PrepareForTest({Bukkit.class, BentoBox.class, User.class, Util.class })
public class StandardSpawnProtectionListenerTest extends AbstractCommonSetup { public class StandardSpawnProtectionListenerTest extends AbstractCommonSetup {
@Mock
private BentoBox plugin;
@Mock
private IslandsManager im;
@Mock @Mock
private PlayersManager pm; private PlayersManager pm;
@Mock @Mock
private IslandWorldManager iwm;
@Mock
private World world;
@Mock
private World nether; private World nether;
@Mock @Mock
private World end; private World end;
@Mock @Mock
private Player player;
@Mock
private Location location;
@Mock
private Block block; private Block block;
private StandardSpawnProtectionListener ssp; private StandardSpawnProtectionListener ssp;
@ -91,10 +71,7 @@ public class StandardSpawnProtectionListenerTest extends AbstractCommonSetup {
*/ */
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); super.setUp();
// Setup plugin
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
when(plugin.getIWM()).thenReturn(iwm);
// Worlds // Worlds
when(world.getEnvironment()).thenReturn(World.Environment.NORMAL); when(world.getEnvironment()).thenReturn(World.Environment.NORMAL);
when(nether.getEnvironment()).thenReturn(World.Environment.NETHER); when(nether.getEnvironment()).thenReturn(World.Environment.NETHER);
@ -112,7 +89,6 @@ public class StandardSpawnProtectionListenerTest extends AbstractCommonSetup {
when(iwm.getWorldSettings(any())).thenReturn(ws); when(iwm.getWorldSettings(any())).thenReturn(ws);
when(iwm.getFriendlyName(any())).thenReturn("BSkyBlock"); when(iwm.getFriendlyName(any())).thenReturn("BSkyBlock");
// Util // Util
PowerMockito.mockStatic(Util.class);
when(Util.getWorld(any())).thenReturn(world); when(Util.getWorld(any())).thenReturn(world);
// Location // Location
when(location.toVector()).thenReturn(new Vector(5,5,5)); when(location.toVector()).thenReturn(new Vector(5,5,5));
@ -120,9 +96,9 @@ public class StandardSpawnProtectionListenerTest extends AbstractCommonSetup {
when(spawnLocation.toVector()).thenReturn(new Vector(0,0,0)); when(spawnLocation.toVector()).thenReturn(new Vector(0,0,0));
when(spawnLocation.getWorld()).thenReturn(nether); when(spawnLocation.getWorld()).thenReturn(nether);
// Player // Player
when(player.getWorld()).thenReturn(nether); when(mockPlayer.getWorld()).thenReturn(nether);
when(player.getUniqueId()).thenReturn(UUID.randomUUID()); when(mockPlayer.getUniqueId()).thenReturn(uuid);
User.getInstance(player); User.getInstance(mockPlayer);
// Locales // Locales
LocalesManager lm = mock(LocalesManager.class); LocalesManager lm = mock(LocalesManager.class);
when(lm.get(any(), any())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(1, String.class)); when(lm.get(any(), any())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(1, String.class));
@ -157,10 +133,10 @@ public class StandardSpawnProtectionListenerTest extends AbstractCommonSetup {
*/ */
@Test @Test
public void testOnBlockPlaceDisallowed() { public void testOnBlockPlaceDisallowed() {
BlockPlaceEvent e = new BlockPlaceEvent(block, blockState, null, null, player, true, EquipmentSlot.HAND); BlockPlaceEvent e = new BlockPlaceEvent(block, blockState, null, null, mockPlayer, true, EquipmentSlot.HAND);
ssp.onBlockPlace(e); ssp.onBlockPlace(e);
assertTrue(e.isCancelled()); assertTrue(e.isCancelled());
verify(player).sendMessage("protection.spawn-protected"); checkSpigotMessage("protection.spawn-protected");
} }
/** /**
@ -169,10 +145,10 @@ public class StandardSpawnProtectionListenerTest extends AbstractCommonSetup {
@Test @Test
public void testOnBlockPlaceDisallowedNoProtection() { public void testOnBlockPlaceDisallowedNoProtection() {
when(iwm.isNetherIslands(any())).thenReturn(true); when(iwm.isNetherIslands(any())).thenReturn(true);
BlockPlaceEvent e = new BlockPlaceEvent(block, blockState, null, null, player, true, EquipmentSlot.HAND); BlockPlaceEvent e = new BlockPlaceEvent(block, blockState, null, null, mockPlayer, true, EquipmentSlot.HAND);
ssp.onBlockPlace(e); ssp.onBlockPlace(e);
assertFalse(e.isCancelled()); assertFalse(e.isCancelled());
verify(player, never()).sendMessage("protection.spawn-protected"); checkSpigotMessage("protection.spawn-protected", 0);
} }
/** /**
@ -180,11 +156,11 @@ public class StandardSpawnProtectionListenerTest extends AbstractCommonSetup {
*/ */
@Test @Test
public void testOnBlockPlaceAllowed() { public void testOnBlockPlaceAllowed() {
when(player.isOp()).thenReturn(true); when(mockPlayer.isOp()).thenReturn(true);
BlockPlaceEvent e = new BlockPlaceEvent(block, blockState, null, null, player, true, EquipmentSlot.HAND); BlockPlaceEvent e = new BlockPlaceEvent(block, blockState, null, null, mockPlayer, true, EquipmentSlot.HAND);
ssp.onBlockPlace(e); ssp.onBlockPlace(e);
assertFalse(e.isCancelled()); assertFalse(e.isCancelled());
verify(player, never()).sendMessage("protection.spawn-protected"); checkSpigotMessage("protection.spawn-protected", 0);
} }
/** /**
@ -193,10 +169,10 @@ public class StandardSpawnProtectionListenerTest extends AbstractCommonSetup {
@Test @Test
public void testOnBlockPlaceAllowedOutsideSpawn() { public void testOnBlockPlaceAllowedOutsideSpawn() {
when(iwm.getNetherSpawnRadius(any())).thenReturn(1); when(iwm.getNetherSpawnRadius(any())).thenReturn(1);
BlockPlaceEvent e = new BlockPlaceEvent(block, blockState, null, null, player, true, EquipmentSlot.HAND); BlockPlaceEvent e = new BlockPlaceEvent(block, blockState, null, null, mockPlayer, true, EquipmentSlot.HAND);
ssp.onBlockPlace(e); ssp.onBlockPlace(e);
assertFalse(e.isCancelled()); assertFalse(e.isCancelled());
verify(player, never()).sendMessage("protection.spawn-protected"); checkSpigotMessage("protection.spawn-protected", 0);
} }
/** /**
@ -205,11 +181,11 @@ public class StandardSpawnProtectionListenerTest extends AbstractCommonSetup {
@Test @Test
public void testOnBlockPlaceAllowedWrongWorld() { public void testOnBlockPlaceAllowedWrongWorld() {
when(location.getWorld()).thenReturn(world); when(location.getWorld()).thenReturn(world);
when(player.getWorld()).thenReturn(world); when(mockPlayer.getWorld()).thenReturn(world);
BlockPlaceEvent e = new BlockPlaceEvent(block, blockState, null, null, player, true, EquipmentSlot.HAND); BlockPlaceEvent e = new BlockPlaceEvent(block, blockState, null, null, mockPlayer, true, EquipmentSlot.HAND);
ssp.onBlockPlace(e); ssp.onBlockPlace(e);
assertFalse(e.isCancelled()); assertFalse(e.isCancelled());
verify(player, never()).sendMessage("protection.spawn-protected"); checkSpigotMessage("protection.spawn-protected", 0);
} }
/** /**
@ -218,10 +194,10 @@ public class StandardSpawnProtectionListenerTest extends AbstractCommonSetup {
@Test @Test
public void testOnBlockPlaceAllowedNetherIslandWorlds() { public void testOnBlockPlaceAllowedNetherIslandWorlds() {
when(iwm.isNetherIslands(any())).thenReturn(true); when(iwm.isNetherIslands(any())).thenReturn(true);
BlockPlaceEvent e = new BlockPlaceEvent(block, blockState, null, null, player, true, EquipmentSlot.HAND); BlockPlaceEvent e = new BlockPlaceEvent(block, blockState, null, null, mockPlayer, true, EquipmentSlot.HAND);
ssp.onBlockPlace(e); ssp.onBlockPlace(e);
assertFalse(e.isCancelled()); assertFalse(e.isCancelled());
verify(player, never()).sendMessage("protection.spawn-protected"); checkSpigotMessage("protection.spawn-protected", 0);
} }
/** /**
@ -230,13 +206,13 @@ public class StandardSpawnProtectionListenerTest extends AbstractCommonSetup {
@Test @Test
public void testOnBlockPlaceAllowedEndIslandWorlds() { public void testOnBlockPlaceAllowedEndIslandWorlds() {
when(location.getWorld()).thenReturn(end); when(location.getWorld()).thenReturn(end);
when(player.getWorld()).thenReturn(end); when(mockPlayer.getWorld()).thenReturn(end);
when(spawnLocation.getWorld()).thenReturn(end); when(spawnLocation.getWorld()).thenReturn(end);
when(iwm.isEndIslands(any())).thenReturn(true); when(iwm.isEndIslands(any())).thenReturn(true);
BlockPlaceEvent e = new BlockPlaceEvent(block, blockState, null, null, player, true, EquipmentSlot.HAND); BlockPlaceEvent e = new BlockPlaceEvent(block, blockState, null, null, mockPlayer, true, EquipmentSlot.HAND);
ssp.onBlockPlace(e); ssp.onBlockPlace(e);
assertFalse(e.isCancelled()); assertFalse(e.isCancelled());
verify(player, never()).sendMessage("protection.spawn-protected"); checkSpigotMessage("protection.spawn-protected", 0);
} }
/** /**
@ -244,10 +220,10 @@ public class StandardSpawnProtectionListenerTest extends AbstractCommonSetup {
*/ */
@Test @Test
public void testOnBlockBreakDisallowed() { public void testOnBlockBreakDisallowed() {
BlockBreakEvent e = new BlockBreakEvent(block, player); BlockBreakEvent e = new BlockBreakEvent(block, mockPlayer);
ssp.onBlockBreak(e); ssp.onBlockBreak(e);
assertTrue(e.isCancelled()); assertTrue(e.isCancelled());
verify(player).sendMessage("protection.spawn-protected"); checkSpigotMessage("protection.spawn-protected");
} }
/** /**
@ -256,10 +232,10 @@ public class StandardSpawnProtectionListenerTest extends AbstractCommonSetup {
@Test @Test
public void testOnBlockBreakDisallowedNoProtection() { public void testOnBlockBreakDisallowedNoProtection() {
when(ws.isMakeNetherPortals()).thenReturn(true); when(ws.isMakeNetherPortals()).thenReturn(true);
BlockBreakEvent e = new BlockBreakEvent(block, player); BlockBreakEvent e = new BlockBreakEvent(block, mockPlayer);
ssp.onBlockBreak(e); ssp.onBlockBreak(e);
assertFalse(e.isCancelled()); assertFalse(e.isCancelled());
verify(player, never()).sendMessage("protection.spawn-protected"); checkSpigotMessage("protection.spawn-protected", 0);
} }
/** /**
@ -267,11 +243,11 @@ public class StandardSpawnProtectionListenerTest extends AbstractCommonSetup {
*/ */
@Test @Test
public void testOnBlockBreakAllowed() { public void testOnBlockBreakAllowed() {
when(player.isOp()).thenReturn(true); when(mockPlayer.isOp()).thenReturn(true);
BlockBreakEvent e = new BlockBreakEvent(block, player); BlockBreakEvent e = new BlockBreakEvent(block, mockPlayer);
ssp.onBlockBreak(e); ssp.onBlockBreak(e);
assertFalse(e.isCancelled()); assertFalse(e.isCancelled());
verify(player, never()).sendMessage("protection.spawn-protected"); checkSpigotMessage("protection.spawn-protected", 0);
} }
/** /**
@ -291,7 +267,7 @@ public class StandardSpawnProtectionListenerTest extends AbstractCommonSetup {
new Vector(0,0,0), new Vector(0,0,0),
new Vector(0,0,0), new Vector(0,0,0),
new Vector(10000,0,0)); new Vector(10000,0,0));
EntityExplodeEvent e = getExplodeEvent(player, location, blockList); EntityExplodeEvent e = getExplodeEvent(mockPlayer, location, blockList);
ssp.onExplosion(e); ssp.onExplosion(e);
// 4 blocks inside the spawn should be removed, leaving one // 4 blocks inside the spawn should be removed, leaving one
assertEquals(1, blockList.size()); assertEquals(1, blockList.size());
@ -316,7 +292,7 @@ public class StandardSpawnProtectionListenerTest extends AbstractCommonSetup {
new Vector(0,0,0), new Vector(0,0,0),
new Vector(0,0,0), new Vector(0,0,0),
new Vector(10000,0,0)); new Vector(10000,0,0));
EntityExplodeEvent e = getExplodeEvent(player, location, blockList); EntityExplodeEvent e = getExplodeEvent(mockPlayer, location, blockList);
ssp.onExplosion(e); ssp.onExplosion(e);
// No blocks should be removed // No blocks should be removed
assertEquals(5, blockList.size()); assertEquals(5, blockList.size());
@ -328,10 +304,11 @@ public class StandardSpawnProtectionListenerTest extends AbstractCommonSetup {
*/ */
@Test @Test
public void testOnBucketEmptyDisallowed() { public void testOnBucketEmptyDisallowed() {
PlayerBucketEmptyEvent e = new PlayerBucketEmptyEvent(player, block, block, BlockFace.DOWN, null, null, null); PlayerBucketEmptyEvent e = new PlayerBucketEmptyEvent(mockPlayer, block, block, BlockFace.DOWN, null, null,
null);
ssp.onBucketEmpty(e); ssp.onBucketEmpty(e);
assertTrue(e.isCancelled()); assertTrue(e.isCancelled());
verify(player).sendMessage("protection.spawn-protected"); checkSpigotMessage("protection.spawn-protected");
} }
/** /**
@ -340,10 +317,11 @@ public class StandardSpawnProtectionListenerTest extends AbstractCommonSetup {
@Test @Test
public void testOnBucketEmptyDisallowedNoProtection() { public void testOnBucketEmptyDisallowedNoProtection() {
when(ws.isMakeNetherPortals()).thenReturn(true); when(ws.isMakeNetherPortals()).thenReturn(true);
PlayerBucketEmptyEvent e = new PlayerBucketEmptyEvent(player, block, block, BlockFace.DOWN, null, null, null); PlayerBucketEmptyEvent e = new PlayerBucketEmptyEvent(mockPlayer, block, block, BlockFace.DOWN, null, null,
null);
ssp.onBucketEmpty(e); ssp.onBucketEmpty(e);
assertFalse(e.isCancelled()); assertFalse(e.isCancelled());
verify(player, never()).sendMessage("protection.spawn-protected"); checkSpigotMessage("protection.spawn-protected", 0);
} }
/** /**
@ -351,11 +329,12 @@ public class StandardSpawnProtectionListenerTest extends AbstractCommonSetup {
*/ */
@Test @Test
public void testOnBucketEmptyAllowed() { public void testOnBucketEmptyAllowed() {
when(player.isOp()).thenReturn(true); when(mockPlayer.isOp()).thenReturn(true);
PlayerBucketEmptyEvent e = new PlayerBucketEmptyEvent(player, block, block, BlockFace.DOWN, null, null, null); PlayerBucketEmptyEvent e = new PlayerBucketEmptyEvent(mockPlayer, block, block, BlockFace.DOWN, null, null,
null);
ssp.onBucketEmpty(e); ssp.onBucketEmpty(e);
assertFalse(e.isCancelled()); assertFalse(e.isCancelled());
verify(player, never()).sendMessage("protection.spawn-protected"); checkSpigotMessage("protection.spawn-protected", 0);
} }
} }

View File

@ -36,6 +36,7 @@ import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.modules.junit4.PowerMockRunner;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.RanksManagerBeforeClassTest;
import world.bentobox.bentobox.api.addons.GameModeAddon; import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.localization.TextVariables; import world.bentobox.bentobox.api.localization.TextVariables;
@ -48,7 +49,6 @@ 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.RanksManager; import world.bentobox.bentobox.managers.RanksManager;
import world.bentobox.bentobox.managers.RanksManagerBeforeClassTest;
import world.bentobox.bentobox.panels.settings.SettingsTab; import world.bentobox.bentobox.panels.settings.SettingsTab;
import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.Util;

View File

@ -34,10 +34,10 @@ import org.mockito.Mock;
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 world.bentobox.bentobox.AbstractCommonSetup;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.flags.Flag; import world.bentobox.bentobox.api.flags.Flag;
import world.bentobox.bentobox.api.flags.Flag.Type; import world.bentobox.bentobox.api.flags.Flag.Type;
import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup;
import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.Util;

View File

@ -45,8 +45,8 @@ import org.mockito.Mock;
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 world.bentobox.bentobox.AbstractCommonSetup;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup;
import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.Util;

View File

@ -31,8 +31,8 @@ import org.junit.runner.RunWith;
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 world.bentobox.bentobox.AbstractCommonSetup;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup;
import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.Util;

View File

@ -26,8 +26,8 @@ import org.junit.runner.RunWith;
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 world.bentobox.bentobox.AbstractCommonSetup;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup;
import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.Util;

View File

@ -21,8 +21,8 @@ import org.mockito.Mock;
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 world.bentobox.bentobox.AbstractCommonSetup;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup;
import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.Util;

View File

@ -19,8 +19,8 @@ import org.junit.runner.RunWith;
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 world.bentobox.bentobox.AbstractCommonSetup;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup;
import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.Util;

View File

@ -18,8 +18,8 @@ import org.junit.runner.RunWith;
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 world.bentobox.bentobox.AbstractCommonSetup;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup;
import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.Util;
/** /**

View File

@ -33,9 +33,9 @@ import org.mockito.Mock;
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 world.bentobox.bentobox.AbstractCommonSetup;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup;
import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.Util;

View File

@ -24,8 +24,8 @@ import org.junit.runner.RunWith;
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 world.bentobox.bentobox.AbstractCommonSetup;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup;
import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.Util;

View File

@ -29,9 +29,9 @@ import org.mockito.Mock;
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 world.bentobox.bentobox.AbstractCommonSetup;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup;
import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.Util;

View File

@ -42,8 +42,8 @@ import org.junit.runner.RunWith;
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 world.bentobox.bentobox.AbstractCommonSetup;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup;
import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.Util;

View File

@ -39,8 +39,8 @@ import org.junit.runner.RunWith;
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 world.bentobox.bentobox.AbstractCommonSetup;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup;
import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.Util;

View File

@ -34,8 +34,8 @@ import org.junit.runner.RunWith;
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 world.bentobox.bentobox.AbstractCommonSetup;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup;
import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.Util;

View File

@ -19,9 +19,9 @@ import org.mockito.Mock;
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 world.bentobox.bentobox.AbstractCommonSetup;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup;
import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.Util;

View File

@ -43,8 +43,8 @@ import org.mockito.Mockito;
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 world.bentobox.bentobox.AbstractCommonSetup;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup;
import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.Util;

View File

@ -18,8 +18,8 @@ import org.mockito.Mockito;
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 world.bentobox.bentobox.AbstractCommonSetup;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup;
import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.Util;

View File

@ -46,12 +46,12 @@ 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;
import world.bentobox.bentobox.AbstractCommonSetup;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.Settings;
import world.bentobox.bentobox.api.configuration.WorldSettings; import world.bentobox.bentobox.api.configuration.WorldSettings;
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.listeners.flags.AbstractCommonSetup;
import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.managers.FlagsManager; import world.bentobox.bentobox.managers.FlagsManager;
import world.bentobox.bentobox.managers.IslandWorldManager; import world.bentobox.bentobox.managers.IslandWorldManager;

View File

@ -22,9 +22,9 @@ import org.mockito.Mockito;
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 world.bentobox.bentobox.AbstractCommonSetup;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup;
import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.Util;

View File

@ -35,9 +35,9 @@ import org.mockito.Mockito;
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 world.bentobox.bentobox.AbstractCommonSetup;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup;
import world.bentobox.bentobox.listeners.flags.protection.BlockInteractionListener; import world.bentobox.bentobox.listeners.flags.protection.BlockInteractionListener;
import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.Util;

View File

@ -38,13 +38,13 @@ 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;
import world.bentobox.bentobox.AbstractCommonSetup;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.Settings;
import world.bentobox.bentobox.api.addons.GameModeAddon; import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.api.configuration.WorldSettings; import world.bentobox.bentobox.api.configuration.WorldSettings;
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.listeners.flags.AbstractCommonSetup;
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.IslandsManager; import world.bentobox.bentobox.managers.IslandsManager;

View File

@ -20,14 +20,12 @@ import org.bukkit.Server;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player;
import org.bukkit.event.block.Action; import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerEvent; import org.bukkit.event.player.PlayerEvent;
import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemFactory; import org.bukkit.inventory.ItemFactory;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory; import org.bukkit.inventory.PlayerInventory;
import org.bukkit.plugin.PluginManager;
import org.bukkit.util.RayTraceResult; import org.bukkit.util.RayTraceResult;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
@ -35,48 +33,34 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.Mockito; import org.mockito.Mockito;
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 world.bentobox.bentobox.AbstractCommonSetup;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.configuration.WorldSettings; import world.bentobox.bentobox.api.configuration.WorldSettings;
import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.managers.IslandWorldManager;
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.util.Util;
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest({BentoBox.class, PlayerEvent.class, PlayerInteractEvent.class, Bukkit.class}) @PrepareForTest({ BentoBox.class, PlayerEvent.class, PlayerInteractEvent.class, Bukkit.class, Util.class })
public class ObsidianScoopingListenerTest { public class ObsidianScoopingListenerTest extends AbstractCommonSetup {
@Mock
private World world;
private ObsidianScoopingListener listener; private ObsidianScoopingListener listener;
@Mock @Mock
private ItemStack item; private ItemStack item;
@Mock @Mock
private Block clickedBlock; private Block clickedBlock;
@Mock @Mock
private BentoBox plugin;
@Mock
private Player p;
@Mock
private IslandWorldManager iwm;
@Mock
private IslandsManager im;
@Mock
private LocalesManager lm; private LocalesManager lm;
private Material inHand; private Material inHand;
private Material block; private Material block;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); super.setUp();
// Set up plugin
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
// Mock server // Mock server
Server server = mock(Server.class); Server server = mock(Server.class);
@ -84,9 +68,6 @@ public class ObsidianScoopingListenerTest {
when(server.getWorld("world")).thenReturn(world); when(server.getWorld("world")).thenReturn(world);
when(server.getVersion()).thenReturn("BSB_Mocking"); when(server.getVersion()).thenReturn("BSB_Mocking");
PluginManager pluginManager = mock(PluginManager.class);
when(Bukkit.getPluginManager()).thenReturn(pluginManager);
// Mock item factory (for itemstacks) // Mock item factory (for itemstacks)
ItemFactory itemFactory = mock(ItemFactory.class); ItemFactory itemFactory = mock(ItemFactory.class);
when(server.getItemFactory()).thenReturn(itemFactory); when(server.getItemFactory()).thenReturn(itemFactory);
@ -95,30 +76,25 @@ public class ObsidianScoopingListenerTest {
listener = new ObsidianScoopingListener(); listener = new ObsidianScoopingListener();
// Mock player // Mock player
when(p.getWorld()).thenReturn(world); when(mockPlayer.getWorld()).thenReturn(world);
RayTraceResult rtr = mock(RayTraceResult.class); RayTraceResult rtr = mock(RayTraceResult.class);
when(p.rayTraceBlocks(5, FluidCollisionMode.ALWAYS)).thenReturn(rtr); when(mockPlayer.rayTraceBlocks(5, FluidCollisionMode.ALWAYS)).thenReturn(rtr);
when(rtr.getHitBlock()).thenReturn(clickedBlock); when(rtr.getHitBlock()).thenReturn(clickedBlock);
Location location = mock(Location.class);
when(location.getWorld()).thenReturn(world); when(location.getWorld()).thenReturn(world);
when(location.getBlockX()).thenReturn(0); when(location.getBlockX()).thenReturn(0);
when(location.getBlockY()).thenReturn(0); when(location.getBlockY()).thenReturn(0);
when(location.getBlockZ()).thenReturn(0); when(location.getBlockZ()).thenReturn(0);
when(p.getLocation()).thenReturn(location); when(mockPlayer.getLocation()).thenReturn(location);
when(p.getInventory()).thenReturn(mock(PlayerInventory.class)); when(mockPlayer.getInventory()).thenReturn(mock(PlayerInventory.class));
// Worlds // Worlds
when(plugin.getIWM()).thenReturn(iwm);
when(iwm.getIslandWorld(Mockito.any())).thenReturn(world); when(iwm.getIslandWorld(Mockito.any())).thenReturn(world);
when(iwm.getNetherWorld(Mockito.any())).thenReturn(world); when(iwm.getNetherWorld(Mockito.any())).thenReturn(world);
when(iwm.getEndWorld(Mockito.any())).thenReturn(world); when(iwm.getEndWorld(Mockito.any())).thenReturn(world);
when(iwm.getFriendlyName(any())).thenReturn("BSkyBlock"); when(iwm.getFriendlyName(any())).thenReturn("BSkyBlock");
// Mock up IslandsManager
when(plugin.getIslands()).thenReturn(im);
// Mock up items and blocks // Mock up items and blocks
when(clickedBlock.getX()).thenReturn(0); when(clickedBlock.getX()).thenReturn(0);
when(clickedBlock.getY()).thenReturn(0); when(clickedBlock.getY()).thenReturn(0);
@ -136,7 +112,7 @@ public class ObsidianScoopingListenerTest {
// Put player on island // Put player on island
when(im.userIsOnIsland(Mockito.any(), Mockito.any())).thenReturn(true); when(im.userIsOnIsland(Mockito.any(), Mockito.any())).thenReturn(true);
// Set as survival // Set as survival
when(p.getGameMode()).thenReturn(GameMode.SURVIVAL); when(mockPlayer.getGameMode()).thenReturn(GameMode.SURVIVAL);
// Locales // Locales
when(plugin.getLocalesManager()).thenReturn(lm); when(plugin.getLocalesManager()).thenReturn(lm);
@ -157,7 +133,7 @@ public class ObsidianScoopingListenerTest {
PlayerInventory playerInventory = mock(PlayerInventory.class); PlayerInventory playerInventory = mock(PlayerInventory.class);
when(playerInventory.getItemInMainHand()).thenReturn(item); when(playerInventory.getItemInMainHand()).thenReturn(item);
when(playerInventory.getItemInOffHand()).thenReturn(new ItemStack(Material.AIR)); when(playerInventory.getItemInOffHand()).thenReturn(new ItemStack(Material.AIR));
when(p.getInventory()).thenReturn(playerInventory); when(mockPlayer.getInventory()).thenReturn(playerInventory);
// Addon // Addon
when(iwm.getAddon(Mockito.any())).thenReturn(Optional.empty()); when(iwm.getAddon(Mockito.any())).thenReturn(Optional.empty());
@ -219,7 +195,8 @@ public class ObsidianScoopingListenerTest {
@Test @Test
public void testOnPlayerInteractNotInWorld() { public void testOnPlayerInteractNotInWorld() {
PlayerInteractEvent event = new PlayerInteractEvent(p, Action.RIGHT_CLICK_BLOCK, item, clickedBlock, BlockFace.EAST); PlayerInteractEvent event = new PlayerInteractEvent(mockPlayer, Action.RIGHT_CLICK_BLOCK, item, clickedBlock,
BlockFace.EAST);
// Test not in world // Test not in world
when(iwm.inWorld(any(World.class))).thenReturn(false); when(iwm.inWorld(any(World.class))).thenReturn(false);
when(iwm.inWorld(any(Location.class))).thenReturn(false); when(iwm.inWorld(any(Location.class))).thenReturn(false);
@ -235,11 +212,12 @@ public class ObsidianScoopingListenerTest {
@Test @Test
public void testOnPlayerInteractGameModes() { public void testOnPlayerInteractGameModes() {
PlayerInteractEvent event = new PlayerInteractEvent(p, Action.RIGHT_CLICK_BLOCK, item, clickedBlock, BlockFace.EAST); PlayerInteractEvent event = new PlayerInteractEvent(mockPlayer, Action.RIGHT_CLICK_BLOCK, item, clickedBlock,
BlockFace.EAST);
// Test different game modes // Test different game modes
for (GameMode gm : GameMode.values()) { for (GameMode gm : GameMode.values()) {
when(p.getGameMode()).thenReturn(gm); when(mockPlayer.getGameMode()).thenReturn(gm);
if (!gm.equals(GameMode.SURVIVAL)) { if (!gm.equals(GameMode.SURVIVAL)) {
assertFalse(listener.onPlayerInteract(event)); assertFalse(listener.onPlayerInteract(event));
} }
@ -248,10 +226,11 @@ public class ObsidianScoopingListenerTest {
@Test @Test
public void testOnPlayerInteractSurvivalNotOnIsland() { public void testOnPlayerInteractSurvivalNotOnIsland() {
PlayerInteractEvent event = new PlayerInteractEvent(p, Action.RIGHT_CLICK_BLOCK, item, clickedBlock, BlockFace.EAST); PlayerInteractEvent event = new PlayerInteractEvent(mockPlayer, Action.RIGHT_CLICK_BLOCK, item, clickedBlock,
BlockFace.EAST);
// Set as survival // Set as survival
when(p.getGameMode()).thenReturn(GameMode.SURVIVAL); when(mockPlayer.getGameMode()).thenReturn(GameMode.SURVIVAL);
// Positive test with 1 bucket in the stack // Positive test with 1 bucket in the stack
inHand = Material.BUCKET; inHand = Material.BUCKET;
@ -273,7 +252,8 @@ public class ObsidianScoopingListenerTest {
when(airBlock.getType()).thenReturn(Material.AIR); when(airBlock.getType()).thenReturn(Material.AIR);
ObsidianScoopingListener listener = new ObsidianScoopingListener(); ObsidianScoopingListener listener = new ObsidianScoopingListener();
PlayerInteractEvent event = new PlayerInteractEvent(p, Action.RIGHT_CLICK_BLOCK, item, clickedBlock, BlockFace.EAST); PlayerInteractEvent event = new PlayerInteractEvent(mockPlayer, Action.RIGHT_CLICK_BLOCK, item, clickedBlock,
BlockFace.EAST);
if (!item.getType().equals(Material.BUCKET) if (!item.getType().equals(Material.BUCKET)
|| !clickedBlock.getType().equals(Material.OBSIDIAN)) { || !clickedBlock.getType().equals(Material.OBSIDIAN)) {
assertFalse(listener.onPlayerInteract(event)); assertFalse(listener.onPlayerInteract(event));

View File

@ -21,9 +21,9 @@ import org.powermock.modules.junit4.PowerMockRunner;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import world.bentobox.bentobox.AbstractCommonSetup;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup;
import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.Util;

View File

@ -24,7 +24,6 @@ import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.metadata.FixedMetadataValue; import org.bukkit.metadata.FixedMetadataValue;
@ -33,23 +32,18 @@ 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.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;
import org.powermock.reflect.Whitebox;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import world.bentobox.bentobox.AbstractCommonSetup;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.configuration.WorldSettings; import world.bentobox.bentobox.api.configuration.WorldSettings;
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.listeners.flags.AbstractCommonSetup;
import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.Util;
/** /**
@ -62,49 +56,24 @@ public class VisitorKeepInventoryListenerTest extends AbstractCommonSetup {
// Class under test // Class under test
private VisitorKeepInventoryListener l; private VisitorKeepInventoryListener l;
@Mock
private Player player;
/* IslandWorldManager */
@Mock
private IslandWorldManager iwm;
/* World */
@Mock
private World world;
/* Islands */
@Mock
private IslandsManager islandsManager;
@Mock
private Island island;
private PlayerDeathEvent e; private PlayerDeathEvent e;
@Mock
private Location location;
/**
*/
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); super.setUp();
// Set up plugin
BentoBox plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
/* Island World Manager */
when(plugin.getIWM()).thenReturn(iwm);
// User // User
User.setPlugin(plugin); User.setPlugin(plugin);
UUID uuid = UUID.randomUUID(); UUID uuid = UUID.randomUUID();
when(player.getUniqueId()).thenReturn(uuid); when(mockPlayer.getUniqueId()).thenReturn(uuid);
when(player.getName()).thenReturn("tastybento"); when(mockPlayer.getName()).thenReturn("tastybento");
when(player.getLocation()).thenReturn(location); when(mockPlayer.getLocation()).thenReturn(location);
when(location.getWorld()).thenReturn(world); when(location.getWorld()).thenReturn(world);
when(location.toVector()).thenReturn(new Vector(1,2,3)); when(location.toVector()).thenReturn(new Vector(1,2,3));
// Turn on why for player // Turn on why for player
when(player.getMetadata(eq("bskyblock_world_why_debug"))).thenReturn(Collections.singletonList(new FixedMetadataValue(plugin, true))); when(mockPlayer.getMetadata(eq("bskyblock_world_why_debug"))).thenReturn(Collections.singletonList(new FixedMetadataValue(plugin, true)));
when(player.getMetadata(eq("bskyblock_world_why_debug_issuer"))).thenReturn(Collections.singletonList(new FixedMetadataValue(plugin, uuid.toString()))); when(mockPlayer.getMetadata(eq("bskyblock_world_why_debug_issuer"))).thenReturn(Collections.singletonList(new FixedMetadataValue(plugin, uuid.toString())));
User.getInstance(player); User.getInstance(mockPlayer);
// WorldSettings and World Flags // WorldSettings and World Flags
WorldSettings ws = mock(WorldSettings.class); WorldSettings ws = mock(WorldSettings.class);
@ -124,11 +93,11 @@ public class VisitorKeepInventoryListenerTest extends AbstractCommonSetup {
Flags.VISITOR_KEEP_INVENTORY.setSetting(world, false); Flags.VISITOR_KEEP_INVENTORY.setSetting(world, false);
/* Islands */ /* Islands */
when(plugin.getIslands()).thenReturn(islandsManager); when(plugin.getIslands()).thenReturn(im);
// Visitor // Visitor
when(island.getMemberSet(anyInt())).thenReturn(ImmutableSet.of()); when(island.getMemberSet(anyInt())).thenReturn(ImmutableSet.of());
// By default, there should be an island. // By default, there should be an island.
when(islandsManager.getProtectedIslandAt(any())).thenReturn(Optional.of(island)); when(im.getProtectedIslandAt(any())).thenReturn(Optional.of(island));
// Util // Util
PowerMockito.mockStatic(Util.class, Mockito.CALLS_REAL_METHODS); PowerMockito.mockStatic(Util.class, Mockito.CALLS_REAL_METHODS);
@ -137,7 +106,7 @@ public class VisitorKeepInventoryListenerTest extends AbstractCommonSetup {
// Default death event // Default death event
List<ItemStack> drops = new ArrayList<>(); List<ItemStack> drops = new ArrayList<>();
drops.add(new ItemStack(Material.ACACIA_BOAT)); drops.add(new ItemStack(Material.ACACIA_BOAT));
e = getPlayerDeathEvent(player, drops, 100, 0, 0, 0, "Death message"); e = getPlayerDeathEvent(mockPlayer, drops, 100, 0, 0, 0, "Death message");
// Make new // Make new
l = new VisitorKeepInventoryListener(); l = new VisitorKeepInventoryListener();
} }
@ -169,8 +138,8 @@ public class VisitorKeepInventoryListenerTest extends AbstractCommonSetup {
assertFalse(e.getDrops().isEmpty()); assertFalse(e.getDrops().isEmpty());
assertEquals(100, e.getDroppedExp()); assertEquals(100, e.getDroppedExp());
// Why // Why
verify(player).sendMessage("Why: PlayerDeathEvent in world bskyblock_world at 1,2,3"); checkSpigotMessage("Why: PlayerDeathEvent in world bskyblock_world at 1,2,3");
verify(player).sendMessage("Why: tastybento VISITOR_KEEP_INVENTORY - SETTING_NOT_ALLOWED_IN_WORLD"); checkSpigotMessage("Why: tastybento VISITOR_KEEP_INVENTORY - SETTING_NOT_ALLOWED_IN_WORLD");
} }
/** /**
@ -185,8 +154,8 @@ public class VisitorKeepInventoryListenerTest extends AbstractCommonSetup {
assertTrue(e.getDrops().isEmpty()); assertTrue(e.getDrops().isEmpty());
assertEquals(0, e.getDroppedExp()); assertEquals(0, e.getDroppedExp());
// Why // Why
verify(player).sendMessage("Why: PlayerDeathEvent in world bskyblock_world at 1,2,3"); checkSpigotMessage("Why: PlayerDeathEvent in world bskyblock_world at 1,2,3");
verify(player).sendMessage("Why: tastybento VISITOR_KEEP_INVENTORY - SETTING_ALLOWED_IN_WORLD"); checkSpigotMessage("Why: tastybento VISITOR_KEEP_INVENTORY - SETTING_ALLOWED_IN_WORLD");
} }
/** /**
@ -202,8 +171,8 @@ public class VisitorKeepInventoryListenerTest extends AbstractCommonSetup {
assertFalse(e.getDrops().isEmpty()); assertFalse(e.getDrops().isEmpty());
assertEquals(100, e.getDroppedExp()); assertEquals(100, e.getDroppedExp());
// Why // Why
verify(player).sendMessage("Why: PlayerDeathEvent in world bskyblock_world at 1,2,3"); checkSpigotMessage("Why: PlayerDeathEvent in world bskyblock_world at 1,2,3");
verify(player).sendMessage("Why: tastybento VISITOR_KEEP_INVENTORY - SETTING_NOT_ALLOWED_IN_WORLD"); checkSpigotMessage("Why: tastybento VISITOR_KEEP_INVENTORY - SETTING_NOT_ALLOWED_IN_WORLD");
} }
/** /**
@ -211,7 +180,7 @@ public class VisitorKeepInventoryListenerTest extends AbstractCommonSetup {
*/ */
@Test @Test
public void testOnVisitorDeathTrueFlagNoIsland() { public void testOnVisitorDeathTrueFlagNoIsland() {
when(islandsManager.getProtectedIslandAt(any())).thenReturn(Optional.empty()); when(im.getProtectedIslandAt(any())).thenReturn(Optional.empty());
Flags.VISITOR_KEEP_INVENTORY.setSetting(world, true); Flags.VISITOR_KEEP_INVENTORY.setSetting(world, true);
l.onVisitorDeath(e); l.onVisitorDeath(e);
assertFalse(e.getKeepInventory()); assertFalse(e.getKeepInventory());
@ -219,7 +188,7 @@ public class VisitorKeepInventoryListenerTest extends AbstractCommonSetup {
assertFalse(e.getDrops().isEmpty()); assertFalse(e.getDrops().isEmpty());
assertEquals(100, e.getDroppedExp()); assertEquals(100, e.getDroppedExp());
// Why // Why
verify(player, never()).sendMessage(anyString()); verify(mockPlayer, never()).sendMessage(anyString());
} }
} }

View File

@ -33,10 +33,10 @@ 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;
import world.bentobox.bentobox.AbstractCommonSetup;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.configuration.WorldSettings; import world.bentobox.bentobox.api.configuration.WorldSettings;
import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup;
import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.managers.IslandWorldManager; import world.bentobox.bentobox.managers.IslandWorldManager;

View File

@ -23,8 +23,8 @@ 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 world.bentobox.bentobox.AbstractCommonSetup;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup;
import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.Util;

View File

@ -76,6 +76,7 @@ import com.google.common.collect.ImmutableSet.Builder;
import io.papermc.lib.PaperLib; import io.papermc.lib.PaperLib;
import io.papermc.lib.environments.CraftBukkitEnvironment; import io.papermc.lib.environments.CraftBukkitEnvironment;
import io.papermc.lib.environments.Environment; import io.papermc.lib.environments.Environment;
import world.bentobox.bentobox.AbstractCommonSetup;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.Settings;
import world.bentobox.bentobox.api.configuration.WorldSettings; import world.bentobox.bentobox.api.configuration.WorldSettings;
@ -85,7 +86,6 @@ import world.bentobox.bentobox.database.AbstractDatabaseHandler;
import world.bentobox.bentobox.database.Database; import world.bentobox.bentobox.database.Database;
import world.bentobox.bentobox.database.DatabaseSetup; import world.bentobox.bentobox.database.DatabaseSetup;
import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup;
import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.managers.island.IslandCache; import world.bentobox.bentobox.managers.island.IslandCache;
import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.Util;

View File

@ -46,13 +46,13 @@ import org.powermock.reflect.Whitebox;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSet.Builder; import com.google.common.collect.ImmutableSet.Builder;
import world.bentobox.bentobox.AbstractCommonSetup;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.flags.Flag; import world.bentobox.bentobox.api.flags.Flag;
import world.bentobox.bentobox.database.AbstractDatabaseHandler; import world.bentobox.bentobox.database.AbstractDatabaseHandler;
import world.bentobox.bentobox.database.Database; import world.bentobox.bentobox.database.Database;
import world.bentobox.bentobox.database.DatabaseSetup; import world.bentobox.bentobox.database.DatabaseSetup;
import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup;
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.util.Util; import world.bentobox.bentobox.util.Util;

View File

@ -29,6 +29,7 @@ import org.bukkit.World.Environment;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
import org.bukkit.command.ConsoleCommandSender; import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.entity.Player.Spigot;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
@ -72,6 +73,8 @@ public class UtilTest {
private User user; private User user;
@Mock @Mock
private ConsoleCommandSender sender; private ConsoleCommandSender sender;
@Mock
private Spigot spigot;
/** /**
*/ */
@ -100,6 +103,7 @@ public class UtilTest {
Server server = mock(Server.class); Server server = mock(Server.class);
when(Bukkit.getServer()).thenReturn(server); when(Bukkit.getServer()).thenReturn(server);
when(Bukkit.getWorld(anyString())).thenReturn(world); when(Bukkit.getWorld(anyString())).thenReturn(world);
when(sender.spigot()).thenReturn(spigot);
when(Bukkit.getConsoleSender()).thenReturn(sender); when(Bukkit.getConsoleSender()).thenReturn(sender);
// Bukkit - online players // Bukkit - online players
@ -113,6 +117,7 @@ public class UtilTest {
when(p1.getUniqueId()).thenReturn(uuid); when(p1.getUniqueId()).thenReturn(uuid);
when(p1.getName()).thenReturn(name); when(p1.getName()).thenReturn(name);
when(p1.hasPermission(anyString())).thenReturn(true); when(p1.hasPermission(anyString())).thenReturn(true);
when(p1.spigot()).thenReturn(spigot);
online.put(uuid, name); online.put(uuid, name);
onlinePlayers.add(p1); onlinePlayers.add(p1);
// Add to User cache // Add to User cache