Added test for SafeSpotTeleport

It's not a great test.
This commit is contained in:
Tastybento 2018-04-28 10:32:03 -07:00
parent 26eb6c275e
commit 72306035f6
3 changed files with 126 additions and 4 deletions

View File

@ -5,6 +5,7 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import org.bukkit.Bukkit;
import org.bukkit.ChunkSnapshot; import org.bukkit.ChunkSnapshot;
import org.bukkit.GameMode; import org.bukkit.GameMode;
import org.bukkit.Location; import org.bukkit.Location;
@ -74,7 +75,7 @@ public class SafeSpotTeleport {
checking = true; checking = true;
// Start a recurring task until done or cancelled // Start a recurring task until done or cancelled
task = plugin.getServer().getScheduler().runTaskTimer(plugin, () -> { task = Bukkit.getScheduler().runTaskTimer(plugin, () -> {
List<ChunkSnapshot> chunkSnapshot = new ArrayList<>(); List<ChunkSnapshot> chunkSnapshot = new ArrayList<>();
if (checking) { if (checking) {
Iterator<Pair<Integer, Integer>> it = chunksToScan.iterator(); Iterator<Pair<Integer, Integer>> it = chunksToScan.iterator();
@ -161,7 +162,7 @@ public class SafeSpotTeleport {
*/ */
private void checkChunks(final List<ChunkSnapshot> chunkSnapshot) { private void checkChunks(final List<ChunkSnapshot> chunkSnapshot) {
// Run async task to scan chunks // Run async task to scan chunks
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, () -> { Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
for (ChunkSnapshot chunk: chunkSnapshot) { for (ChunkSnapshot chunk: chunkSnapshot) {
if (scanChunk(chunk)) { if (scanChunk(chunk)) {
task.cancel(); task.cancel();
@ -201,7 +202,7 @@ public class SafeSpotTeleport {
private void teleportEntity(final Location loc) { private void teleportEntity(final Location loc) {
task.cancel(); task.cancel();
// Return to main thread and teleport the player // Return to main thread and teleport the player
plugin.getServer().getScheduler().runTask(plugin, () -> { Bukkit.getScheduler().runTask(plugin, () -> {
if (!portal && entity instanceof Player) { if (!portal && entity instanceof Player) {
// Set home // Set home
plugin.getPlayers().setHomeLocation(entity.getUniqueId(), loc, homeNumber); plugin.getPlayers().setHomeLocation(entity.getUniqueId(), loc, homeNumber);

View File

@ -33,7 +33,7 @@ import us.tastybento.bskyblock.api.user.User;
import us.tastybento.bskyblock.managers.CommandsManager; import us.tastybento.bskyblock.managers.CommandsManager;
/** /**
* @author ben * @author tastybento
* *
*/ */
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)

View File

@ -0,0 +1,121 @@
/**
*
*/
package us.tastybento.bskyblock.util.teleport;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.Optional;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager;
import org.bukkit.scheduler.BukkitScheduler;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.Settings;
import us.tastybento.bskyblock.api.user.User;
import us.tastybento.bskyblock.database.objects.Island;
import us.tastybento.bskyblock.managers.IslandsManager;
import us.tastybento.bskyblock.managers.LocalesManager;
/**
* @author tastybento
*
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest( { BSkyBlock.class })
public class SafeSpotTeleportTest {
@Mock
static BSkyBlock plugin;
@Mock
private static World world;
@Mock
private static BukkitScheduler sch;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
world = mock(World.class);
Server server = mock(Server.class);
when(server.getLogger()).thenReturn(Logger.getAnonymousLogger());
when(server.getWorld("world")).thenReturn(world);
when(server.getVersion()).thenReturn("BSB_Mocking");
PluginManager pluginManager = mock(PluginManager.class);
when(server.getPluginManager()).thenReturn(pluginManager);
Bukkit.setServer(server);
when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger());
Whitebox.setInternalState(BSkyBlock.class, "instance", plugin);
plugin = mock(BSkyBlock.class);
// Users
User.setPlugin(plugin);
// Locales - final
LocalesManager lm = mock(LocalesManager.class);
when(plugin.getLocalesManager()).thenReturn(lm);
when(lm.get(any(), any())).thenReturn("mock translation");
// Island Manager
IslandsManager im = mock(IslandsManager.class);
when(plugin.getIslands()).thenReturn(im);
Island island = mock(Island.class);
when(island.getCenter()).thenReturn(mock(Location.class));
// Default is that there is no island around here
Optional<Island> oi = Optional.empty();
when(im.getIslandAt(Mockito.any())).thenReturn(oi);
// Settings
Settings settings = mock(Settings.class);
when(settings.getIslandProtectionRange()).thenReturn(1);
when(plugin.getSettings()).thenReturn(settings);
// Server & Scheduler
sch = mock(BukkitScheduler.class);
when(server.getScheduler()).thenReturn(sch);
}
/**
* Test method for {@link us.tastybento.bskyblock.util.teleport.SafeSpotTeleport#SafeSpotTeleport(us.tastybento.bskyblock.BSkyBlock, org.bukkit.entity.Entity, org.bukkit.Location, java.lang.String, boolean, int)}.
*/
@Test
public void testSafeSpotTeleport() throws Exception {
Player player = mock(Player.class);
when(player.getGameMode()).thenReturn(GameMode.SURVIVAL);
Location loc = mock(Location.class);
when(loc.getWorld()).thenReturn(world);
when(loc.getBlockX()).thenReturn(0);
when(loc.getBlockY()).thenReturn(120);
when(loc.getBlockZ()).thenReturn(0);
Block block = mock(Block.class);
when(loc.getBlock()).thenReturn(block);
boolean portal = false;
int homeNumber = 1;
new SafeSpotTeleport(plugin, player, loc, "failure message", portal, homeNumber);
}
}