From 9173a15eb9602d69d1b2f4fc8ea58217fb700c4c Mon Sep 17 00:00:00 2001 From: "main()" Date: Sat, 31 Dec 2011 18:31:38 +0100 Subject: [PATCH] Started work on tons of new tests --- .../MultiverseCore/MultiverseCore.java | 19 +- .../test/TestWorldProperties.java | 208 ++++++++++++++++++ .../test/utils/MockWorldFactory.java | 8 +- .../test/utils/TestInstanceCreator.java | 51 +++++ 4 files changed, 284 insertions(+), 2 deletions(-) create mode 100644 src/test/java/com/onarandombox/MultiverseCore/test/TestWorldProperties.java diff --git a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java index d359224e..8ac188fd 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java +++ b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java @@ -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. * diff --git a/src/test/java/com/onarandombox/MultiverseCore/test/TestWorldProperties.java b/src/test/java/com/onarandombox/MultiverseCore/test/TestWorldProperties.java new file mode 100644 index 00000000..c530ff73 --- /dev/null +++ b/src/test/java/com/onarandombox/MultiverseCore/test/TestWorldProperties.java @@ -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()); + } + +} diff --git a/src/test/java/com/onarandombox/MultiverseCore/test/utils/MockWorldFactory.java b/src/test/java/com/onarandombox/MultiverseCore/test/utils/MockWorldFactory.java index 1df477ae..94c3aa21 100644 --- a/src/test/java/com/onarandombox/MultiverseCore/test/utils/MockWorldFactory.java +++ b/src/test/java/com/onarandombox/MultiverseCore/test/utils/MockWorldFactory.java @@ -121,7 +121,13 @@ public class MockWorldFactory { } public static List getWorlds() { - return new ArrayList(createdWorlds.values()); + // we have to invert the order! + ArrayList myList = new ArrayList(createdWorlds.values()); + List retList = new ArrayList(); + for (int i = (myList.size() - 1); i > 0; i--) { + retList.add(myList.get(i)); + } + return retList; } public static void clearWorlds() { diff --git a/src/test/java/com/onarandombox/MultiverseCore/test/utils/TestInstanceCreator.java b/src/test/java/com/onarandombox/MultiverseCore/test/utils/TestInstanceCreator.java index ce6471ef..b40b01bb 100644 --- a/src/test/java/com/onarandombox/MultiverseCore/test/utils/TestInstanceCreator.java +++ b/src/test/java/com/onarandombox/MultiverseCore/test/utils/TestInstanceCreator.java @@ -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() { + 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() { + 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);