More tests :D

This commit is contained in:
main() 2012-01-25 21:03:09 +01:00
parent 7b74384a8e
commit 51586c7dd5
2 changed files with 143 additions and 30 deletions

View File

@ -8,6 +8,8 @@
package com.onarandombox.MultiverseCore.test;
import static org.junit.Assert.*;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.*;
import org.bukkit.Bukkit;
@ -15,11 +17,17 @@ import org.bukkit.ChatColor;
import org.bukkit.Difficulty;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.entity.EntityRegainHealthEvent;
import org.bukkit.event.entity.EntityRegainHealthEvent.RegainReason;
import org.bukkit.event.entity.FoodLevelChangeEvent;
import org.bukkit.event.player.PlayerChatEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.event.weather.ThunderChangeEvent;
import org.bukkit.event.weather.WeatherChangeEvent;
import org.bukkit.permissions.Permission;
@ -42,13 +50,31 @@ import com.onarandombox.MultiverseCore.utils.WorldManager;
@RunWith(PowerMockRunner.class)
@PrepareForTest({ PluginManager.class, MultiverseCore.class, Permission.class, Bukkit.class,
WeatherChangeEvent.class, ThunderChangeEvent.class, PlayerChatEvent.class,
PlayerJoinEvent.class, WorldManager.class })
PlayerJoinEvent.class, PlayerRespawnEvent.class, EntityRegainHealthEvent.class,
FoodLevelChangeEvent.class, WorldManager.class })
public class TestWorldProperties {
private TestInstanceCreator creator;
private MultiverseCore core;
private CommandSender mockCommandSender;
// Events
private WeatherChangeEvent weatherChangeOffEvent;
private WeatherChangeEvent weatherChangeOnEvent;
private ThunderChangeEvent thunderChangeOffEvent;
private ThunderChangeEvent thunderChangeOnEvent;
private Player mockPlayer;
private PlayerChatEvent playerChatEvent;
private Player mockNewPlayer;
private PlayerJoinEvent playerNewJoinEvent;
private PlayerJoinEvent playerJoinEvent;
private PlayerRespawnEvent playerRespawnBed;
private PlayerRespawnEvent playerRespawnNormal;
private Entity mockEntity;
private EntityRegainHealthEvent entityRegainHealthEvent;
private FoodLevelChangeEvent entityFoodLevelChangeEvent;
private FoodLevelChangeEvent entityFoodLevelRiseEvent;
@Before
public void setUp() throws Exception {
creator = new TestInstanceCreator();
@ -78,7 +104,8 @@ public class TestWorldProperties {
String[] netherArgs = new String[] { "import", "world_nether", "nether" };
core.onCommand(mockCommandSender, mockCommand, "", netherArgs);
verify(mockCommandSender).sendMessage("Starting import of world 'world_nether'...");
verify(mockCommandSender, VerificationModeFactory.times(2)).sendMessage(ChatColor.GREEN + "Complete!");
verify(mockCommandSender, VerificationModeFactory.times(2)).sendMessage(
ChatColor.GREEN + "Complete!");
// ////////////////////////////////////////////////
// let's set some world-properties
@ -87,13 +114,15 @@ public class TestWorldProperties {
assertNotNull(worldManager);
MultiverseWorld mvWorld = worldManager.getMVWorld("world");
MultiverseWorld netherWorld = worldManager.getMVWorld("world_nether");
assertNotNull(mvWorld);
assertNotNull(netherWorld);
assertSame(mvWorld, worldManager.getFirstSpawnWorld());
assertSame(mvWorld, worldManager.getSpawnWorld());
/* ****************************************** *
* Check defaults
* ****************************************** */
/* ***************************** *
* Check defaults
* ***************************** */
assertFalse(mvWorld.isHidden());
assertEquals(mvWorld.getName(), mvWorld.getAlias());
assertEquals(ChatColor.WHITE, mvWorld.getColor());
@ -102,7 +131,9 @@ public class TestWorldProperties {
// assertEquals(Object,Object) instead of assertEquals(double,double)
assertNull(mvWorld.getRespawnToWorld());
assertTrue(mvWorld.isWeatherEnabled());
// assertEquals(Difficulty.EASY, mvWorld.getDifficulty());
World cbWorld = mvWorld.getCBWorld();
when(cbWorld.getDifficulty()).thenReturn(Difficulty.NORMAL);
assertEquals(Difficulty.NORMAL, mvWorld.getDifficulty());
assertTrue(mvWorld.canAnimalsSpawn());
assertTrue(mvWorld.canMonstersSpawn());
assertEquals(-1, mvWorld.getCurrency());
@ -119,26 +150,7 @@ public class TestWorldProperties {
/* ****************************************** *
* Call some events and verify behavior
* ****************************************** */
// weather change
WeatherChangeEvent weatherChangeOffEvent = new WeatherChangeEvent(mvWorld.getCBWorld(), false);
WeatherChangeEvent weatherChangeOnEvent = new WeatherChangeEvent(mvWorld.getCBWorld(), true);
// thunder change
ThunderChangeEvent thunderChangeOffEvent = new ThunderChangeEvent(mvWorld.getCBWorld(), false);
ThunderChangeEvent thunderChangeOnEvent = new ThunderChangeEvent(mvWorld.getCBWorld(), true);
// player chat
Player mockPlayer = mock(Player.class);
when(mockPlayer.getWorld()).thenReturn(mvWorld.getCBWorld());
when(mockPlayer.hasPlayedBefore()).thenReturn(true);
PlayerChatEvent playerChatEvent = PowerMockito.mock(PlayerChatEvent.class);
PowerMockito.when(playerChatEvent.getPlayer()).thenReturn(mockPlayer);
PowerMockito.when(playerChatEvent.getFormat()).thenReturn("format");
// player join
Player mockNewPlayer = mock(Player.class);
when(mockNewPlayer.hasPlayedBefore()).thenReturn(false);
PlayerJoinEvent playerJoinEvent = PowerMockito.mock(PlayerJoinEvent.class);
when(playerJoinEvent.getPlayer()).thenReturn(mockPlayer);
PlayerJoinEvent playerNewJoinEvent = PowerMockito.mock(PlayerJoinEvent.class);
when(playerNewJoinEvent.getPlayer()).thenReturn(mockNewPlayer);
createEvents(mvWorld);
// call both weather change events
core.getWeatherListener().weatherChange(weatherChangeOffEvent);
@ -153,8 +165,12 @@ public class TestWorldProperties {
assertFalse(thunderChangeOnEvent.isCancelled());
// call player chat event
MultiverseCore.PrefixChat = true;
core.getPlayerListener().playerChat(playerChatEvent);
verify(playerChatEvent).setFormat("[" + mvWorld.getColoredWorldString() + "]" + "format");
MultiverseCore.PrefixChat = false;
core.getPlayerListener().playerChat(playerChatEvent);
verify(playerChatEvent, times(1)).setFormat(anyString()); // only ONE TIME (not the 2nd time!)
// call player join events
core.getPlayerListener().playerJoin(playerJoinEvent);
@ -162,9 +178,22 @@ public class TestWorldProperties {
core.getPlayerListener().playerJoin(playerNewJoinEvent);
verify(mockNewPlayer).teleport(worldManager.getFirstSpawnWorld().getSpawnLocation());
/* ****************************************** *
* Modify & Verify
* ****************************************** */
// call player respawn events
core.getPlayerListener().playerRespawn(playerRespawnBed);
// bedrespawn is on so nothing should happen
verify(playerRespawnBed, never()).setRespawnLocation(any(Location.class));
core.getPlayerListener().playerRespawn(playerRespawnNormal);
verify(playerRespawnNormal).setRespawnLocation(mvWorld.getSpawnLocation());
// call entity regain health event
core.getEntityListener().entityRegainHealth(entityRegainHealthEvent);
// autoheal is on so nothing should happen
verify(entityRegainHealthEvent, never()).setCancelled(true);
/* ************************ *
* Modify & Verify
* ************************ */
mvWorld.setHidden(true);
assertEquals(true, mvWorld.isHidden());
mvWorld.setAlias("alias");
@ -173,13 +202,15 @@ public class TestWorldProperties {
ChatColor oldColor = mvWorld.getColor();
assertFalse(mvWorld.setColor("INVALID COLOR"));
assertEquals(oldColor, mvWorld.getColor());
assertEquals(oldColor.toString() + "alias" + ChatColor.WHITE.toString(), mvWorld.getColoredWorldString());
mvWorld.setPVPMode(false);
assertEquals(false, mvWorld.isPVPEnabled());
assertTrue(mvWorld.setScaling(2D));
assertEquals((Object) 2D, (Object) mvWorld.getScaling());
assertFalse(mvWorld.setRespawnToWorld("INVALID WORLD"));
assertTrue(mvWorld.setRespawnToWorld("world_nether"));
assertSame(worldManager.getMVWorld("world_nether").getCBWorld(), mvWorld.getRespawnToWorld());
assertSame(worldManager.getMVWorld("world_nether").getCBWorld(),
mvWorld.getRespawnToWorld());
mvWorld.setEnableWeather(false);
assertEquals(false, mvWorld.isWeatherEnabled());
assertTrue(mvWorld.setDifficulty("PEACEFUL"));
@ -216,6 +247,9 @@ public class TestWorldProperties {
/* ****************************************** *
* Call some events and verify behavior
* ****************************************** */
// We have to recreate the events and the mock-objects
createEvents(mvWorld);
// call both weather change events
core.getWeatherListener().weatherChange(weatherChangeOffEvent);
assertFalse(weatherChangeOffEvent.isCancelled());
@ -227,6 +261,84 @@ public class TestWorldProperties {
assertFalse(thunderChangeOffEvent.isCancelled());
core.getWeatherListener().thunderChange(thunderChangeOnEvent);
assertTrue(thunderChangeOnEvent.isCancelled());
// call player chat event
MultiverseCore.PrefixChat = true;
core.getPlayerListener().playerChat(playerChatEvent);
// never because it's hidden!
verify(playerChatEvent, never()).setFormat(
"[" + mvWorld.getColoredWorldString() + "]" + "format");
mvWorld.setHidden(false);
core.getPlayerListener().playerChat(playerChatEvent);
verify(playerChatEvent).setFormat("[" + mvWorld.getColoredWorldString() + "]" + "format");
MultiverseCore.PrefixChat = false;
core.getPlayerListener().playerChat(playerChatEvent);
verify(playerChatEvent, times(1)).setFormat(anyString()); // only ONE TIME (not the 2nd time!)
// call player join events
core.getPlayerListener().playerJoin(playerJoinEvent);
verify(mockPlayer, never()).teleport(any(Location.class));
core.getPlayerListener().playerJoin(playerNewJoinEvent);
verify(mockNewPlayer).teleport(new Location(mvWorld.getCBWorld(), 1, 1, 1));
// call player respawn events
core.getPlayerListener().playerRespawn(playerRespawnBed);
// bedrespawn is off so something should happen (and we've set respawn to nether...)
verify(playerRespawnBed).setRespawnLocation(netherWorld.getSpawnLocation());
core.getPlayerListener().playerRespawn(playerRespawnNormal);
verify(playerRespawnNormal).setRespawnLocation(netherWorld.getSpawnLocation());
// call entity regain health event
core.getEntityListener().entityRegainHealth(entityRegainHealthEvent);
// autoheal is off so something should happen
verify(entityRegainHealthEvent).setCancelled(true);
}
public void createEvents(MultiverseWorld mvWorld) {
//// Weather events
// weather change
weatherChangeOffEvent = new WeatherChangeEvent(mvWorld.getCBWorld(), false);
weatherChangeOnEvent = new WeatherChangeEvent(mvWorld.getCBWorld(), true);
// thunder change
thunderChangeOffEvent = new ThunderChangeEvent(mvWorld.getCBWorld(), false);
thunderChangeOnEvent = new ThunderChangeEvent(mvWorld.getCBWorld(), true);
//// Player events
// player chat
mockPlayer = mock(Player.class);
when(mockPlayer.getWorld()).thenReturn(mvWorld.getCBWorld());
when(mockPlayer.hasPlayedBefore()).thenReturn(true);
playerChatEvent = PowerMockito.mock(PlayerChatEvent.class);
when(playerChatEvent.getPlayer()).thenReturn(mockPlayer);
when(playerChatEvent.getFormat()).thenReturn("format");
// player join
mockNewPlayer = mock(Player.class);
when(mockNewPlayer.hasPlayedBefore()).thenReturn(false);
playerJoinEvent = PowerMockito.mock(PlayerJoinEvent.class);
when(playerJoinEvent.getPlayer()).thenReturn(mockPlayer);
playerNewJoinEvent = PowerMockito.mock(PlayerJoinEvent.class);
when(playerNewJoinEvent.getPlayer()).thenReturn(mockNewPlayer);
// player respawn
playerRespawnBed = PowerMockito.mock(PlayerRespawnEvent.class);
when(playerRespawnBed.getPlayer()).thenReturn(mockPlayer);
when(playerRespawnBed.isBedSpawn()).thenReturn(true);
playerRespawnNormal = PowerMockito.mock(PlayerRespawnEvent.class);
when(playerRespawnNormal.getPlayer()).thenReturn(mockPlayer);
when(playerRespawnNormal.isBedSpawn()).thenReturn(false);
//// Entity events
mockEntity = mock(Entity.class);
// entity regain health
entityRegainHealthEvent = PowerMockito.mock(EntityRegainHealthEvent.class);
when(entityRegainHealthEvent.getRegainReason()).thenReturn(RegainReason.REGEN);
when(mockEntity.getLocation()).thenReturn(new Location(mvWorld.getCBWorld(), 0, 0, 0));
when(entityRegainHealthEvent.getEntity()).thenReturn(mockEntity);
// entity food level change event
entityFoodLevelChangeEvent = PowerMockito.mock(FoodLevelChangeEvent.class);
// this won't do anything since we're not mocking a player,
// but the plugin should be able to handle this!
when(entityFoodLevelChangeEvent.getEntity()).thenReturn(mockEntity);
entityFoodLevelRiseEvent = PowerMockito.mock(FoodLevelChangeEvent.class);
when(mockPlayer.getFoodLevel()).thenReturn(2);
when(entityFoodLevelRiseEvent.getEntity()).thenReturn(mockPlayer);
when(entityFoodLevelRiseEvent.getFoodLevel()).thenReturn(3);
}
}

View File

@ -202,6 +202,7 @@ public class TestWorldStuff {
}
@Test
// TODO Migrate this to TestWorldProperties
public void testModifyGameMode() {
// Pull a core instance from the server.
Plugin plugin = mockServer.getPluginManager().getPlugin("Multiverse-Core");