Started work on tons of new tests

This commit is contained in:
main() 2011-12-31 18:31:38 +01:00
parent 76629ebd25
commit 9173a15eb9
4 changed files with 284 additions and 2 deletions

View File

@ -129,7 +129,6 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core {
// Setup the block/player/entity listener.
private MVPlayerListener playerListener = new MVPlayerListener(this);
private MVEntityListener entityListener = new MVEntityListener(this);
private MVPluginListener pluginListener = new MVPluginListener(this);
private MVWeatherListener weatherListener = new MVWeatherListener(this);
@ -708,6 +707,24 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core {
return this.playerListener;
}
/**
* Gets the {@link MVEntityListener}.
*
* @return The {@link MVEntityListener}.
*/
public MVEntityListener getEntityListener() {
return this.entityListener;
}
/**
* Gets the {@link MVWeatherListener}.
*
* @return The {@link MVWeatherListener}.
*/
public MVWeatherListener getWeatherListener() {
return this.weatherListener;
}
/**
* Saves the Multiverse-Config.
*

View File

@ -0,0 +1,208 @@
package com.onarandombox.MultiverseCore.test;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Difficulty;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerChatEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.weather.ThunderChangeEvent;
import org.bukkit.event.weather.WeatherChangeEvent;
import org.bukkit.permissions.Permission;
import org.bukkit.plugin.PluginManager;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Matchers;
import org.mockito.internal.verification.VerificationModeFactory;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import com.onarandombox.MultiverseCore.MultiverseCore;
import com.onarandombox.MultiverseCore.api.MVWorldManager;
import com.onarandombox.MultiverseCore.api.MultiverseWorld;
import com.onarandombox.MultiverseCore.test.utils.TestInstanceCreator;
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 })
public class TestWorldProperties {
private TestInstanceCreator creator;
private Server mockServer;
private MultiverseCore core;
private CommandSender mockCommandSender;
@Before
public void setUp() throws Exception {
creator = new TestInstanceCreator();
assertTrue(creator.setUp());
mockServer = creator.getServer();
core = creator.getCore();
mockCommandSender = creator.getCommandSender();
}
@After
public void tearDown() throws Exception {
creator.tearDown();
}
@Test
public void test() {
// Initialize a fake command
Command mockCommand = mock(Command.class);
when(mockCommand.getName()).thenReturn("mv");
// Import the first world
String[] normalArgs = new String[] { "import", "world", "normal" };
core.onCommand(mockCommandSender, mockCommand, "", normalArgs);
verify(mockCommandSender).sendMessage("Starting import of world 'world'...");
verify(mockCommandSender).sendMessage("Complete!");
// Import a second world
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("Complete!");
// ////////////////////////////////////////////////
// let's set some world-properties
// we can test the API with this, too :D
MVWorldManager worldManager = core.getMVWorldManager();
assertNotNull(worldManager);
MultiverseWorld mvWorld = worldManager.getMVWorld("world");
assertNotNull(mvWorld);
assertSame(mvWorld, worldManager.getFirstSpawnWorld());
assertSame(mvWorld, worldManager.getSpawnWorld());
/* ****************************************** *
* Check defaults
* ****************************************** */
assertFalse(mvWorld.isHidden());
assertEquals(mvWorld.getName(), mvWorld.getAlias());
assertEquals(ChatColor.WHITE, mvWorld.getColor());
assertTrue(mvWorld.isPVPEnabled());
assertEquals((Object) 1D, (Object) mvWorld.getScaling()); // we're casting this to objects to use
// assertEquals(Object,Object) instead of assertEquals(double,double)
assertNull(mvWorld.getRespawnToWorld());
assertTrue(mvWorld.isWeatherEnabled());
// assertEquals(Difficulty.EASY, mvWorld.getDifficulty());
assertTrue(mvWorld.canAnimalsSpawn());
assertTrue(mvWorld.canMonstersSpawn());
assertEquals(-1, mvWorld.getCurrency());
assertEquals((Object) 0D, (Object) mvWorld.getPrice());
assertTrue(mvWorld.getHunger());
assertTrue(mvWorld.getAutoHeal());
assertTrue(mvWorld.getAdjustSpawn());
assertEquals(GameMode.SURVIVAL, mvWorld.getGameMode());
assertTrue(mvWorld.isKeepingSpawnInMemory());
assertTrue(mvWorld.getBedRespawn());
assertTrue(mvWorld.getAutoLoad());
assertEquals(new Location(mvWorld.getCBWorld(), 0, 0, 0), mvWorld.getSpawnLocation());
/* ****************************************** *
* 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);
// call both weather change events
core.getWeatherListener().onWeatherChange(weatherChangeOffEvent);
assertFalse(weatherChangeOffEvent.isCancelled());
core.getWeatherListener().onWeatherChange(weatherChangeOnEvent);
assertFalse(weatherChangeOnEvent.isCancelled());
// call both thunder change events
core.getWeatherListener().onThunderChange(thunderChangeOffEvent);
assertFalse(thunderChangeOffEvent.isCancelled());
core.getWeatherListener().onThunderChange(thunderChangeOnEvent);
assertFalse(thunderChangeOnEvent.isCancelled());
// call player chat event
core.getPlayerListener().onPlayerChat(playerChatEvent);
verify(playerChatEvent).setFormat("[" + mvWorld.getColoredWorldString() + "]" + "format");
// call player join events
core.getPlayerListener().onPlayerJoin(playerJoinEvent);
verify(mockPlayer, never()).teleport(any(Location.class));
core.getPlayerListener().onPlayerJoin(playerNewJoinEvent);
verify(mockNewPlayer).teleport(worldManager.getFirstSpawnWorld().getSpawnLocation());
/* ****************************************** *
* Modify & Verify
* ****************************************** */
mvWorld.setHidden(true);
mvWorld.setAlias("alias");
assertFalse(mvWorld.setColor("INVALID COLOR"));
assertTrue(mvWorld.setColor("BLACK"));
mvWorld.setPVPMode(false);
mvWorld.setScaling(2D);
assertFalse(mvWorld.setRespawnToWorld("INVALID WORLD"));
assertTrue(mvWorld.setRespawnToWorld("world_nether"));
mvWorld.setEnableWeather(false);
assertFalse(mvWorld.setDifficulty("INVALID DIFFICULTY"));
assertTrue(mvWorld.setDifficulty("PEACEFUL"));
mvWorld.setAllowAnimalSpawn(false);
mvWorld.setAllowMonsterSpawn(false);
mvWorld.setCurrency(1);
mvWorld.setPrice(1D);
mvWorld.setHunger(false);
mvWorld.setAutoHeal(false);
mvWorld.setAdjustSpawn(false);
assertFalse(mvWorld.setGameMode("INVALID GAMEMODE"));
assertTrue(mvWorld.setGameMode("CREATIVE"));
mvWorld.setKeepSpawnInMemory(false);
mvWorld.setBedRespawn(false);
mvWorld.setAutoLoad(false);
mvWorld.setSpawnLocation(new Location(mvWorld.getCBWorld(), 1, 1, 1));
/* ****************************************** *
* Call some events and verify behavior
* ****************************************** */
// call both weather change events
core.getWeatherListener().onWeatherChange(weatherChangeOffEvent);
assertFalse(weatherChangeOffEvent.isCancelled());
core.getWeatherListener().onWeatherChange(weatherChangeOnEvent);
assertTrue(weatherChangeOnEvent.isCancelled());
// call both thunder change events
core.getWeatherListener().onThunderChange(thunderChangeOffEvent);
assertFalse(thunderChangeOffEvent.isCancelled());
core.getWeatherListener().onThunderChange(thunderChangeOnEvent);
assertTrue(thunderChangeOnEvent.isCancelled());
}
}

View File

@ -121,7 +121,13 @@ public class MockWorldFactory {
}
public static List<World> getWorlds() {
return new ArrayList<World>(createdWorlds.values());
// we have to invert the order!
ArrayList<World> myList = new ArrayList<World>(createdWorlds.values());
List<World> retList = new ArrayList<World>();
for (int i = (myList.size() - 1); i > 0; i--) {
retList.add(myList.get(i));
}
return retList;
}
public static void clearWorlds() {

View File

@ -26,9 +26,11 @@ import org.bukkit.World;
import org.bukkit.WorldCreator;
import org.bukkit.command.CommandSender;
import org.bukkit.permissions.Permission;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitScheduler;
import org.mockito.Matchers;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
@ -36,6 +38,9 @@ import org.powermock.api.mockito.PowerMockito;
import com.onarandombox.MultiverseCore.MultiverseCore;
import com.onarandombox.MultiverseCore.api.MultiverseWorld;
import com.onarandombox.MultiverseCore.listeners.MVEntityListener;
import com.onarandombox.MultiverseCore.listeners.MVPlayerListener;
import com.onarandombox.MultiverseCore.listeners.MVWeatherListener;
import com.onarandombox.MultiverseCore.utils.FileUtils;
import com.onarandombox.MultiverseCore.utils.WorldManager;
@ -128,6 +133,34 @@ public class TestInstanceCreator {
when(mockServer.unloadWorld(anyString(), anyBoolean())).thenReturn(true);
// add mock scheduler
BukkitScheduler mockScheduler = mock(BukkitScheduler.class);
when(mockScheduler.scheduleSyncDelayedTask(any(Plugin.class), any(Runnable.class), anyLong())).
thenAnswer(new Answer<Integer>() {
public Integer answer(InvocationOnMock invocation) throws Throwable {
Runnable arg;
try {
arg = (Runnable) invocation.getArguments()[1];
} catch (Exception e) {
return null;
}
arg.run();
return null;
}});
when(mockScheduler.scheduleSyncDelayedTask(any(Plugin.class), any(Runnable.class))).
thenAnswer(new Answer<Integer>() {
public Integer answer(InvocationOnMock invocation) throws Throwable {
Runnable arg;
try {
arg = (Runnable) invocation.getArguments()[1];
} catch (Exception e) {
return null;
}
arg.run();
return null;
}});
when(mockServer.getScheduler()).thenReturn(mockScheduler);
// Set server
Field serverfield = JavaPlugin.class.getDeclaredField("server");
serverfield.setAccessible(true);
@ -139,6 +172,24 @@ public class TestInstanceCreator {
worldmanagerfield.setAccessible(true);
worldmanagerfield.set(core, wm);
// Set playerListener
MVPlayerListener pl = PowerMockito.spy(new MVPlayerListener(core));
Field playerlistenerfield = MultiverseCore.class.getDeclaredField("playerListener");
playerlistenerfield.setAccessible(true);
playerlistenerfield.set(core, pl);
// Set entityListener
MVEntityListener el = PowerMockito.spy(new MVEntityListener(core));
Field entitylistenerfield = MultiverseCore.class.getDeclaredField("entityListener");
entitylistenerfield.setAccessible(true);
entitylistenerfield.set(core, el);
// Set weatherListener
MVWeatherListener wl = PowerMockito.spy(new MVWeatherListener(core));
Field weatherlistenerfield = MultiverseCore.class.getDeclaredField("weatherListener");
weatherlistenerfield.setAccessible(true);
weatherlistenerfield.set(core, wl);
// Init our command sender
final Logger commandSenderLogger = Logger.getLogger("CommandSender");
commandSenderLogger.setParent(Util.logger);