From a33d1420ba0e0a9ac17adeebacc093c7b35de87d Mon Sep 17 00:00:00 2001 From: Eric Stokes Date: Sat, 10 Dec 2011 00:36:40 -0700 Subject: [PATCH] Add game mode test. This test should pass, but alas it fails :( --- .../MultiverseCore/test/TestWorldStuff.java | 85 ++++++++++++++----- .../test/utils/TestCommandSender.java | 17 +--- .../test/utils/TestInstanceCreator.java | 2 +- .../test/utils/WorldCreatorMatcher.java | 4 +- 4 files changed, 73 insertions(+), 35 deletions(-) diff --git a/src/test/java/com/onarandombox/MultiverseCore/test/TestWorldStuff.java b/src/test/java/com/onarandombox/MultiverseCore/test/TestWorldStuff.java index 69297887..4e389f24 100644 --- a/src/test/java/com/onarandombox/MultiverseCore/test/TestWorldStuff.java +++ b/src/test/java/com/onarandombox/MultiverseCore/test/TestWorldStuff.java @@ -7,15 +7,13 @@ package com.onarandombox.MultiverseCore.test; -import static junit.framework.Assert.*; -import static org.mockito.Mockito.*; - -import java.io.File; - -import org.bukkit.Bukkit; -import org.bukkit.ChatColor; -import org.bukkit.Server; -import org.bukkit.WorldCreator; +import com.onarandombox.MultiverseCore.MultiverseCore; +import com.onarandombox.MultiverseCore.api.MultiverseWorld; +import com.onarandombox.MultiverseCore.exceptions.PropertyDoesNotExistException; +import com.onarandombox.MultiverseCore.test.utils.TestInstanceCreator; +import com.onarandombox.MultiverseCore.test.utils.WorldCreatorMatcher; +import com.onarandombox.MultiverseCore.utils.WorldManager; +import org.bukkit.*; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.permissions.Permission; @@ -30,10 +28,10 @@ import org.mockito.internal.verification.VerificationModeFactory; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; -import com.onarandombox.MultiverseCore.MultiverseCore; -import com.onarandombox.MultiverseCore.test.utils.TestInstanceCreator; -import com.onarandombox.MultiverseCore.test.utils.WorldCreatorMatcher; -import com.onarandombox.MultiverseCore.utils.WorldManager; +import java.io.File; + +import static junit.framework.Assert.*; +import static org.mockito.Mockito.*; @RunWith(PowerMockRunner.class) @PrepareForTest({ PluginManager.class, MultiverseCore.class, Permission.class, Bukkit.class, WorldManager.class }) @@ -76,7 +74,7 @@ public class TestWorldStuff { // Initialize a fake command Command mockCommand = mock(Command.class); when(mockCommand.getName()).thenReturn("mv"); - String[] normalArgs = new String[] { "import", "world", "normal" }; + String[] normalArgs = new String[]{ "import", "world", "normal" }; // Ensure we have a fresh copy of MV, 0 worlds. assertEquals(0, creator.getCore().getMVWorldManager().getMVWorlds().size()); @@ -109,21 +107,21 @@ public class TestWorldStuff { assertEquals(0, creator.getCore().getMVWorldManager().getMVWorlds().size()); // Import the first world. - String[] normalArgs = new String[] { "import", "world", "normal" }; + String[] normalArgs = new String[]{ "import", "world", "normal" }; plugin.onCommand(mockCommandSender, mockCommand, "", normalArgs); // We should now have one world imported! assertEquals(1, creator.getCore().getMVWorldManager().getMVWorlds().size()); // Import the second world. - String[] netherArgs = new String[] { "import", "world_nether", "nether" }; + String[] netherArgs = new String[]{ "import", "world_nether", "nether" }; plugin.onCommand(mockCommandSender, mockCommand, "", netherArgs); // We should now have 2 worlds imported! assertEquals(2, creator.getCore().getMVWorldManager().getMVWorlds().size()); // Import the third world. - String[] skyArgs = new String[] { "import", "world_skylands", "end" }; + String[] skyArgs = new String[]{ "import", "world_the_end", "end" }; plugin.onCommand(mockCommandSender, mockCommand, "", skyArgs); // We should now have 2 worlds imported! @@ -132,7 +130,7 @@ public class TestWorldStuff { // Verify that the commandSender has been called 3 times. verify(mockCommandSender).sendMessage("Starting import of world 'world'..."); verify(mockCommandSender).sendMessage("Starting import of world 'world_nether'..."); - verify(mockCommandSender).sendMessage("Starting import of world 'world_skylands'..."); + verify(mockCommandSender).sendMessage("Starting import of world 'world_the_end'..."); verify(mockCommandSender, VerificationModeFactory.times(3)).sendMessage("Complete!"); } @@ -155,7 +153,7 @@ public class TestWorldStuff { assertEquals(0, creator.getCore().getMVWorldManager().getMVWorlds().size()); // Create the world - String[] normalArgs = new String[] { "create", "newworld", "normal" }; + String[] normalArgs = new String[]{ "create", "newworld", "normal" }; plugin.onCommand(mockCommandSender, mockCommand, "", normalArgs); // We should now have one world! @@ -168,4 +166,53 @@ public class TestWorldStuff { WorldCreatorMatcher matcher = new WorldCreatorMatcher(new WorldCreator("newworld")); verify(mockServer).createWorld(Matchers.argThat(matcher)); } + + @Test + public void testModifyGameMode() { + // Pull a core instance from the server. + Plugin plugin = mockServer.getPluginManager().getPlugin("Multiverse-Core"); + Command mockCommand = mock(Command.class); + when(mockCommand.getName()).thenReturn("mv"); + + // Ensure that there are no worlds imported. This is a fresh setup. + assertEquals(0, creator.getCore().getMVWorldManager().getMVWorlds().size()); + this.createInitialWorlds(plugin, mockCommand); + + // Ensure that the default worlds have been created. + assertEquals(3, creator.getCore().getMVWorldManager().getMVWorlds().size()); + MultiverseWorld mainWorld = creator.getCore().getMVWorldManager().getMVWorld("world"); + + // Ensure that the default mode was normal. + assertEquals(GameMode.SURVIVAL, mainWorld.getGameMode()); + + // Set the mode to creative in world. + plugin.onCommand(mockCommandSender, mockCommand, "", new String[]{ "modify", "set", "mode", "creative", "world" }); + verify(mockCommandSender).sendMessage(ChatColor.GREEN + "Success!" + ChatColor.WHITE + " Property " + ChatColor.AQUA + "mode" + ChatColor.WHITE + " was set to " + ChatColor.GREEN + "creative"); + // Ensure the world is now a creative world + assertEquals(GameMode.CREATIVE, mainWorld.getGameMode()); + + // More tests, with alternate syntax. + plugin.onCommand(mockCommandSender, mockCommand, "", new String[]{ "modify", "set", "gamemode", "0", "world" }); + verify(mockCommandSender).sendMessage(ChatColor.GREEN + "Success!" + ChatColor.WHITE + " Property " + ChatColor.AQUA + "gamemode" + ChatColor.WHITE + " was set to " + ChatColor.GREEN + "0"); + assertEquals(GameMode.SURVIVAL, mainWorld.getGameMode()); + + // Now fail one. + plugin.onCommand(mockCommandSender, mockCommand, "", new String[]{ "modify", "set", "mode", "fish", "world" }); + try { + verify(mockCommandSender).sendMessage(mainWorld.getProperty("mode").getHelp()); + } catch (PropertyDoesNotExistException e) { + fail("Mode property did not exist."); + } + + } + + private void createInitialWorlds(Plugin plugin, Command command) { + plugin.onCommand(mockCommandSender, command, "", new String[]{ "import", "world", "normal" }); + plugin.onCommand(mockCommandSender, command, "", new String[]{ "import", "world_nether", "nether" }); + plugin.onCommand(mockCommandSender, command, "", new String[]{ "import", "world_the_end", "end" }); + verify(mockCommandSender).sendMessage("Starting import of world 'world'..."); + verify(mockCommandSender).sendMessage("Starting import of world 'world_nether'..."); + verify(mockCommandSender).sendMessage("Starting import of world 'world_the_end'..."); + verify(mockCommandSender, times(3)).sendMessage("Complete!"); + } } diff --git a/src/test/java/com/onarandombox/MultiverseCore/test/utils/TestCommandSender.java b/src/test/java/com/onarandombox/MultiverseCore/test/utils/TestCommandSender.java index 341b7045..f7ed865b 100644 --- a/src/test/java/com/onarandombox/MultiverseCore/test/utils/TestCommandSender.java +++ b/src/test/java/com/onarandombox/MultiverseCore/test/utils/TestCommandSender.java @@ -7,9 +7,6 @@ package com.onarandombox.MultiverseCore.test.utils; -import java.util.Set; -import java.util.logging.Logger; - import org.bukkit.ChatColor; import org.bukkit.Server; import org.bukkit.command.CommandSender; @@ -18,6 +15,9 @@ import org.bukkit.permissions.PermissionAttachment; import org.bukkit.permissions.PermissionAttachmentInfo; import org.bukkit.plugin.Plugin; +import java.util.Set; +import java.util.logging.Logger; + public class TestCommandSender implements CommandSender { private Server server; @@ -40,7 +40,7 @@ public class TestCommandSender implements CommandSender { */ @Override public void sendMessage(String message) { - logger.info(ChatColor.stripColor(message)); + logger.info("." + ChatColor.stripColor(message) + "."); } /** @@ -67,7 +67,6 @@ public class TestCommandSender implements CommandSender { * Checks if this object contains an override for the specified permission, by fully qualified name * * @param name Name of the permission - * * @return true if the permission is set, otherwise false */ @Override @@ -79,7 +78,6 @@ public class TestCommandSender implements CommandSender { * Checks if this object contains an override for the specified {@link org.bukkit.permissions.Permission} * * @param perm Permission to check - * * @return true if the permission is set, otherwise false */ @Override @@ -93,7 +91,6 @@ public class TestCommandSender implements CommandSender { * If a permission override is not set on this object, the default value of the permission will be returned. * * @param name Name of the permission - * * @return Value of the permission */ @Override @@ -107,7 +104,6 @@ public class TestCommandSender implements CommandSender { * If a permission override is not set on this object, the default value of the permission will be returned * * @param perm Permission to get - * * @return Value of the permission */ @Override @@ -121,7 +117,6 @@ public class TestCommandSender implements CommandSender { * @param plugin Plugin responsible for this attachment, may not be null or disabled * @param name Name of the permission to attach * @param value Value of the permission - * * @return The PermissionAttachment that was just created */ @Override @@ -133,7 +128,6 @@ public class TestCommandSender implements CommandSender { * Adds a new empty {@link org.bukkit.permissions.PermissionAttachment} to this object * * @param plugin Plugin responsible for this attachment, may not be null or disabled - * * @return The PermissionAttachment that was just created */ @Override @@ -149,7 +143,6 @@ public class TestCommandSender implements CommandSender { * @param name Name of the permission to attach * @param value Value of the permission * @param ticks Amount of ticks to automatically remove this attachment after - * * @return The PermissionAttachment that was just created */ @Override @@ -162,7 +155,6 @@ public class TestCommandSender implements CommandSender { * * @param plugin Plugin responsible for this attachment, may not be null or disabled * @param ticks Amount of ticks to automatically remove this attachment after - * * @return The PermissionAttachment that was just created */ @Override @@ -174,7 +166,6 @@ public class TestCommandSender implements CommandSender { * Removes the given {@link org.bukkit.permissions.PermissionAttachment} from this object * * @param attachment Attachment to remove - * * @throws IllegalArgumentException Thrown when the specified attachment isn't part of this object */ @Override 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 fa3f63b1..c8c85946 100644 --- a/src/test/java/com/onarandombox/MultiverseCore/test/utils/TestInstanceCreator.java +++ b/src/test/java/com/onarandombox/MultiverseCore/test/utils/TestInstanceCreator.java @@ -79,7 +79,7 @@ public class TestInstanceCreator { File worldNetherFile = new File(core.getServerFolder(), "world_nether"); Util.log("Creating world-folder: " + worldNetherFile.getAbsolutePath()); worldNetherFile.mkdirs(); - File worldSkylandsFile = new File(core.getServerFolder(), "world_skylands"); + File worldSkylandsFile = new File(core.getServerFolder(), "world_the_end"); Util.log("Creating world-folder: " + worldSkylandsFile.getAbsolutePath()); worldSkylandsFile.mkdirs(); diff --git a/src/test/java/com/onarandombox/MultiverseCore/test/utils/WorldCreatorMatcher.java b/src/test/java/com/onarandombox/MultiverseCore/test/utils/WorldCreatorMatcher.java index 7eeb7bcc..529906fd 100644 --- a/src/test/java/com/onarandombox/MultiverseCore/test/utils/WorldCreatorMatcher.java +++ b/src/test/java/com/onarandombox/MultiverseCore/test/utils/WorldCreatorMatcher.java @@ -42,10 +42,10 @@ public class WorldCreatorMatcher extends ArgumentMatcher { Util.log("Checking Environments..."); return false; } else if (careAboutSeeds && ((WorldCreator) creator).seed() != this.worldCreator.seed()) { - System.out.print("Checking Seeds..."); + Util.log("Checking Seeds..."); return false; } else if (careAboutGenerators && !((WorldCreator) creator).generator().equals(this.worldCreator.generator())) { - System.out.print("Checking Gens..."); + Util.log("Checking Gens..."); return false; } Util.log("Creators matched!!!");