Reorged the tests, LOTS OF CLEANUP

This commit is contained in:
Eric Stokes 2011-10-20 18:36:33 -06:00
parent 2b2a406cce
commit ab311dd8d9
8 changed files with 393 additions and 208 deletions

View File

@ -0,0 +1,41 @@
/******************************************************************************
* Multiverse 2 Copyright (c) the Multiverse Team 2011. *
* Multiverse 2 is licensed under the BSD License. *
* For more information please check the README.md file included *
* with this project. *
******************************************************************************/
package com.onarandombox.MultiverseCore.test;
import com.onarandombox.MultiverseCore.MultiverseCore;
import org.bukkit.plugin.PluginDescriptionFile;
import org.powermock.api.mockito.PowerMockito;
import java.io.File;
import static org.mockito.Mockito.doReturn;
/**
* Multiverse 2
*
* @author fernferret
*/
public class MVCoreFactory {
public static final File pluginDirectory = new File("bin/test/server/plugins/coretest");
public static final File serverDirectory = new File("bin/test/server");
public MultiverseCore getNewCore() {
MultiverseCore core = PowerMockito.spy(new MultiverseCore());
// Let's let all MV files go to bin/test
doReturn(pluginDirectory).when(core).getDataFolder();
// Return a fake PDF file.
PluginDescriptionFile pdf = new PluginDescriptionFile("Multiverse-Core", "2.1-Test", "com.onarandombox.MultiverseCore.MultiverseCore");
doReturn(pdf).when(core).getDescription();
doReturn(true).when(core).isEnabled();
return core;
}
}

View File

@ -0,0 +1,16 @@
/******************************************************************************
* Multiverse 2 Copyright (c) the Multiverse Team 2011. *
* Multiverse 2 is licensed under the BSD License. *
* For more information please check the README.md file included *
* with this project. *
******************************************************************************/
package com.onarandombox.MultiverseCore.test;
/**
* Multiverse 2
*
* @author fernferret
*/
public class MockBukkitServer {
}

View File

@ -0,0 +1,34 @@
/******************************************************************************
* Multiverse 2 Copyright (c) the Multiverse Team 2011. *
* Multiverse 2 is licensed under the BSD License. *
* For more information please check the README.md file included *
* with this project. *
******************************************************************************/
package com.onarandombox.MultiverseCore.test;
import org.bukkit.Server;
import org.bukkit.WorldCreator;
import org.mockito.ArgumentMatcher;
import org.mockito.Matchers;
import java.util.logging.Logger;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* Multiverse 2
*
* @author fernferret
*/
public class MockServerFactory {
public Server getMockServer() {
Server server = mock(Server.class);
when(server.getName()).thenReturn("FernCraft");
Logger logger = Logger.getLogger("Multiverse-Core-Test");
when(server.getLogger()).thenReturn(logger);
return server;
}
}

View File

@ -0,0 +1,89 @@
/******************************************************************************
* Multiverse 2 Copyright (c) the Multiverse Team 2011. *
* Multiverse 2 is licensed under the BSD License. *
* For more information please check the README.md file included *
* with this project. *
******************************************************************************/
package com.onarandombox.MultiverseCore.test;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.generator.ChunkGenerator;
import org.mockito.ArgumentMatcher;
import org.mockito.Matchers;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* Multiverse 2
*
* @author fernferret
*/
public class MockWorldFactory {
class LocationMatcher extends ArgumentMatcher<Location> {
private Location l;
public LocationMatcher(Location location) {
this.l = location;
}
public boolean matches(Object creator) {
return creator.equals(l);
}
}
class LocationMatcherAbove extends LocationMatcher {
public LocationMatcherAbove(Location location) {
super(location);
}
public boolean matches(Object creator) {
System.out.println("Checking above...");
if (super.l == null || creator == null) {
return false;
}
boolean equal = ((Location) creator).getBlockY() >= super.l.getBlockY();
System.out.println("Checking equals/\\..." + equal);
return equal;
}
}
class LocationMatcherBelow extends LocationMatcher {
public LocationMatcherBelow(Location location) {
super(location);
}
public boolean matches(Object creator) {
if (super.l == null || creator == null) {
return false;
}
boolean equal = ((Location) creator).getBlockY() < super.l.getBlockY();
System.out.println("Checking equals\\/..." + equal);
return equal;
}
}
public World makeNewMockWorld(String world, World.Environment env) {
World mockWorld = mock(World.class);
when(mockWorld.getName()).thenReturn(world);
when(mockWorld.getEnvironment()).thenReturn(env);
when(mockWorld.getSpawnLocation()).thenReturn(new Location(mockWorld, 0, 0, 0));
LocationMatcherAbove matchWorldAbove = new LocationMatcherAbove(new Location(mockWorld, 0, 0, 0));
LocationMatcherBelow matchWorldBelow = new LocationMatcherBelow(new Location(mockWorld, 0, 0, 0));
when(mockWorld.getBlockAt(Matchers.argThat(matchWorldAbove))).thenReturn(new MockBlock(new Location(mockWorld, 0, 0, 0), Material.AIR));
when(mockWorld.getBlockAt(Matchers.argThat(matchWorldBelow))).thenReturn(new MockBlock(new Location(mockWorld, 0, 0, 0), Material.STONE));
return mockWorld;
}
public void makeNewMockWorld(String world, World.Environment env, long seed, ChunkGenerator generator) {
World w = this.makeNewMockWorld(world, env);
when(w.getGenerator()).thenReturn(generator);
when(w.getSeed()).thenReturn(seed);
}
}

View File

@ -0,0 +1,52 @@
/******************************************************************************
* Multiverse 2 Copyright (c) the Multiverse Team 2011. *
* Multiverse 2 is licensed under the BSD License. *
* For more information please check the README.md file included *
* with this project. *
******************************************************************************/
package com.onarandombox.MultiverseCore.test;
import com.onarandombox.MultiverseCore.MultiverseCore;
import junit.framework.Assert;
import org.bukkit.command.Command;
import org.bukkit.plugin.Plugin;
import org.junit.Test;
import java.io.File;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class TestDebugMode {
@Test
public void testEnableDebugMode() {
// // Start actual testing.
// // Pull a core instance from the server.
// Plugin plugin = this.mockServer.getPluginManager().getPlugin("Multiverse-Core");
//
// // Make sure Core is not null
// Assert.assertNotNull(plugin);
//
// // Make sure Core is enabled
// Assert.assertTrue(plugin.isEnabled());
//
// // Make a fake server folder to fool MV into thinking a world folder exists.
// File serverDirectory = new File(this.core.getServerFolder(), "world");
// serverDirectory.mkdirs();
//
// // Initialize a fake command
// Command mockCommand = mock(Command.class);
// when(mockCommand.getName()).thenReturn("mv");
//
// // Assert debug mode is off
// Assert.assertEquals(0, MultiverseCore.GlobalDebug);
//
// // Send the debug command.
// String[] debugArgs = new String[]{"debug", "3"};
// plugin.onCommand(mockCommandSender, mockCommand, "", debugArgs);
//
// Assert.assertEquals(3, MultiverseCore.GlobalDebug);
}
}

View File

@ -0,0 +1,89 @@
/******************************************************************************
* Multiverse 2 Copyright (c) the Multiverse Team 2011. *
* Multiverse 2 is licensed under the BSD License. *
* For more information please check the README.md file included *
* with this project. *
******************************************************************************/
package com.onarandombox.MultiverseCore.test;
import com.onarandombox.MultiverseCore.MultiverseCore;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.WorldCreator;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.mockito.Matchers;
import org.powermock.api.mockito.PowerMockito;
import java.util.ArrayList;
import java.util.List;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
/**
* Multiverse 2
*
* @author fernferret
*/
public class TestInstanceCreator {
MultiverseCore core;
private CommandSender commandSender;
public Server setupServerInstance() {
MockWorldFactory worldFactory = new MockWorldFactory();
MVCoreFactory coreFactory = new MVCoreFactory();
MockServerFactory serverFactory = new MockServerFactory();
core = coreFactory.getNewCore();
// Add Core to the list of loaded plugins
JavaPlugin[] plugins = new JavaPlugin[]{core};
// Mock the Plugin Manager
PluginManager mockPluginManager = PowerMockito.mock(PluginManager.class);
when(mockPluginManager.getPlugins()).thenReturn(plugins);
when(mockPluginManager.getPlugin("Multiverse-Core")).thenReturn(core);
// Initialize the Mock Worlds
World mockWorld = worldFactory.makeNewMockWorld("world", World.Environment.NORMAL);
World mockNetherWorld = worldFactory.makeNewMockWorld("world_nether", World.Environment.NETHER);
List<World> worldList = new ArrayList<World>();
worldList.add(mockWorld);
worldList.add(mockNetherWorld);
// Initialize the Mock server.
Server mockServer = serverFactory.getMockServer();
when(mockServer.getWorld("world")).thenReturn(mockWorld);
when(mockServer.getWorld("world_nether")).thenReturn(mockNetherWorld);
when(mockServer.getWorlds()).thenReturn(worldList);
when(mockServer.getPluginManager()).thenReturn(mockPluginManager);
// TODO: This needs to get moved somewhere specific.
WorldCreatorMatcher matchWorld = new WorldCreatorMatcher(new WorldCreator("world"));
when(mockServer.createWorld(Matchers.argThat(matchWorld))).thenReturn(mockWorld);
// Override some methods that bukkit normally provides us with for Core
doReturn(mockServer).when(core).getServer();
// Init our command sender
commandSender = spy(new TestCommandSender(mockServer));
Bukkit.setServer(mockServer);
// Load Multiverse Core
core.onLoad();
// Enable it.
core.onEnable();
return mockServer;
}
public MultiverseCore getCore() {
return this.core;
}
public CommandSender getCommandSender() {
return commandSender;
}
}

View File

@ -8,15 +8,12 @@ import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.permissions.Permission;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginLoader;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatcher;
import org.mockito.Matchers;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
@ -26,6 +23,7 @@ import java.io.File;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import static org.mockito.Mockito.*;
@ -33,157 +31,9 @@ import static org.mockito.Mockito.*;
@RunWith(PowerMockRunner.class)
@PrepareForTest({PluginManager.class, MultiverseCore.class, Permission.class, Bukkit.class})
public class TestWorldImport {
class WorldCreatorMatcher extends ArgumentMatcher<WorldCreator> {
private WorldCreator worldCreator;
public WorldCreatorMatcher(WorldCreator creator) {
this.worldCreator = creator;
}
public boolean matches(Object creator) {
System.out.println("Checking world creators.");
if (!((WorldCreator) creator).name().equals(this.worldCreator.name())) {
System.out.println("Checking Names...");
return false;
} else if (!((WorldCreator) creator).environment().equals(this.worldCreator.environment())) {
System.out.println("Checking Environments...");
return false;
}
// Don't check the seed by default, as it's randomized.
// else if (((WorldCreator) creator).seed() != this.worldCreator.seed()) {
// System.out.print("Checking Seeds...");
// return false;
// }
// else if (!((WorldCreator) creator).generator().equals(this.worldCreator.generator())) {
// System.out.print("Checking Gens...");
// return false;
// }
System.out.println("Creators matched!!!");
return true;
}
}
class LocationMatcher extends ArgumentMatcher<Location> {
private Location l;
public LocationMatcher(Location location) {
this.l = location;
}
public boolean matches(Object creator) {
return creator.equals(l);
}
}
class LocationMatcherAbove extends LocationMatcher {
public LocationMatcherAbove(Location location) {
super(location);
}
public boolean matches(Object creator) {
System.out.println("Checking above...");
if (super.l == null || creator == null) {
return false;
}
boolean equal = ((Location) creator).getBlockY() >= super.l.getBlockY();
System.out.println("Checking equals/\\..." + equal);
return equal;
}
}
class LocationMatcherBelow extends LocationMatcher {
public LocationMatcherBelow(Location location) {
super(location);
}
public boolean matches(Object creator) {
if (super.l == null || creator == null) {
return false;
}
boolean equal = ((Location) creator).getBlockY() < super.l.getBlockY();
System.out.println("Checking equals\\/..." + equal);
return equal;
}
}
CommandSender mockCommandSender;
private World mockWorld;
private World mockNetherWorld;
private List<World> worldList;
private Server mockServer;
private PluginManager mockPluginManager;
private PluginLoader pluginLoader;
private Bukkit mockBukkit;
MultiverseCore core;
File pluginDirectory;
File serverDirectory;
public TestWorldImport() {
// Initialize a fake world and world_nether.
this.mockWorld = mock(World.class);
when(this.mockWorld.getName()).thenReturn("world");
when(this.mockWorld.getEnvironment()).thenReturn(World.Environment.NORMAL);
when(this.mockWorld.getSpawnLocation()).thenReturn(new Location(this.mockWorld, 0, 0, 0));
// Build a small platform that a player can stand on:
LocationMatcherAbove matchWorldAbove = new LocationMatcherAbove(new Location(this.mockWorld, 0, 0, 0));
LocationMatcherBelow matchWorldBelow = new LocationMatcherBelow(new Location(this.mockWorld, 0, 0, 0));
when(this.mockWorld.getBlockAt(Matchers.argThat(matchWorldAbove))).thenReturn(new MockBlock(new Location(this.mockWorld, 0, 0, 0), Material.AIR));
when(this.mockWorld.getBlockAt(Matchers.argThat(matchWorldBelow))).thenReturn(new MockBlock(new Location(this.mockWorld, 0, 0, 0), Material.STONE));
this.mockNetherWorld = mock(World.class);
when(this.mockNetherWorld.getName()).thenReturn("world_nether");
when(this.mockNetherWorld.getEnvironment()).thenReturn(World.Environment.NETHER);
// Initialize our fake worldlist.
this.worldList = new ArrayList<World>();
this.worldList.add(mockWorld);
this.worldList.add(mockNetherWorld);
// Initialize Core. We must do it up here, so that the PluginManager won't return a null ref.
this.core = PowerMockito.spy(new MultiverseCore());
// Add Core to the list of loaded plugins
JavaPlugin[] plugins = new JavaPlugin[]{this.core};
// Mock the Plugin Manager
this.mockPluginManager = PowerMockito.mock(PluginManager.class);
when(this.mockPluginManager.getPlugins()).thenReturn(plugins);
when(this.mockPluginManager.getPlugin("Multiverse-Core")).thenReturn(this.core);
// Initialize the Mock server.
this.mockServer = mock(Server.class);
when(this.mockServer.getWorld("world")).thenReturn(mockWorld);
when(this.mockServer.getWorld("worldNether")).thenReturn(mockNetherWorld);
when(this.mockServer.getWorlds()).thenReturn(worldList);
when(this.mockServer.getPluginManager()).thenReturn(this.mockPluginManager);
when(this.mockServer.getName()).thenReturn("FernCraft");
WorldCreatorMatcher matchWorld = new WorldCreatorMatcher(new WorldCreator("world"));
when(this.mockServer.createWorld(Matchers.argThat(matchWorld))).thenReturn(this.mockWorld);
Logger logger = Logger.getLogger("Multiverse-Core-Test");
when(this.mockServer.getLogger()).thenReturn(logger);
// Initialize a fake console.
this.mockCommandSender = PowerMockito.spy(new TestCommandSender(this.mockServer));
// Override some methods that bukkit normally provides us with for Core
doReturn(this.mockServer).when(this.core).getServer();
// Let's let all MV files go to bin/test
pluginDirectory = new File("bin/test/server/plugins/coretest");
serverDirectory = new File("bin/test/server");
doReturn(pluginDirectory).when(this.core).getDataFolder();
PluginDescriptionFile pdf = new PluginDescriptionFile("Multiverse-Core", "2.1-Test", "com.onarandombox.MultiverseCore.MultiverseCore");
doReturn(pdf).when(this.core).getDescription();
// Wish there was a good/easy way to teardown Bukkit
}
@After
@ -191,37 +41,32 @@ public class TestWorldImport {
Field serverField = Bukkit.class.getDeclaredField("server");
serverField.setAccessible(true);
serverField.set(Class.forName("org.bukkit.Bukkit"), null);
if (serverDirectory.exists()) {
serverDirectory.delete();
FileUtils.deleteFolder(serverDirectory);
if (MVCoreFactory.serverDirectory.exists()) {
MVCoreFactory.serverDirectory.delete();
FileUtils.deleteFolder(MVCoreFactory.serverDirectory);
}
}
@Before
public void setUp() throws Exception {
if (!serverDirectory.exists()) {
serverDirectory.mkdirs();
if (!MVCoreFactory.serverDirectory.exists()) {
MVCoreFactory.serverDirectory.mkdirs();
}
if (!pluginDirectory.exists()) {
pluginDirectory.mkdirs();
if (!MVCoreFactory.pluginDirectory.exists()) {
MVCoreFactory.pluginDirectory.mkdirs();
}
Bukkit.setServer(this.mockServer);
// Load Multiverse Core
this.core.onLoad();
// Enable it.
this.core.onEnable();
// Make sure it knows it's enabled.
doReturn(true).when(this.core).isEnabled();
}
@Test
public void testWorldImportWithNoFolder() {
TestInstanceCreator creator = new TestInstanceCreator();
Server mockServer = creator.setupServerInstance();
CommandSender mockCommandSender = creator.getCommandSender();
// Start actual testing.
// Pull a core instance from the server.
Plugin plugin = this.mockServer.getPluginManager().getPlugin("Multiverse-Core");
Plugin plugin = mockServer.getPluginManager().getPlugin("Multiverse-Core");
// Make sure Core is not null
Assert.assertNotNull(plugin);
@ -234,25 +79,28 @@ public class TestWorldImport {
String[] normalArgs = new String[]{"import", "world", "normal"};
// Ensure we have a fresh copy of MV, 0 worlds.
Assert.assertEquals(0, this.core.getMVWorldManager().getMVWorlds().size());
Assert.assertEquals(0, creator.getCore().getMVWorldManager().getMVWorlds().size());
// Import the first world. The world folder does not exist.
plugin.onCommand(mockCommandSender, mockCommand, "", normalArgs);
verify(this.mockCommandSender).sendMessage(ChatColor.RED + "FAILED.");
verify(this.mockCommandSender).sendMessage("That world folder does not exist...");
verify(mockCommandSender).sendMessage(ChatColor.RED + "FAILED.");
verify(mockCommandSender).sendMessage("That world folder does not exist...");
// We should still have no worlds.
Assert.assertEquals(0, this.core.getMVWorldManager().getMVWorlds().size());
Assert.assertEquals(0, creator.getCore().getMVWorldManager().getMVWorlds().size());
}
@Test
public void testWorldImport() {
TestInstanceCreator creator = new TestInstanceCreator();
Server mockServer = creator.setupServerInstance();
CommandSender mockCommandSender = creator.getCommandSender();
// Start actual testing.
// Pull a core instance from the server.
Plugin plugin = this.mockServer.getPluginManager().getPlugin("Multiverse-Core");
Plugin plugin = mockServer.getPluginManager().getPlugin("Multiverse-Core");
// Make a fake server folder to fool MV into thinking a world folder exists.
File serverDirectory = new File(this.core.getServerFolder(), "world");
File serverDirectory = new File(creator.getCore().getServerFolder(), "world");
serverDirectory.mkdirs();
@ -272,44 +120,14 @@ public class TestWorldImport {
String[] debugArgs = new String[]{"debug", "3"};
plugin.onCommand(mockCommandSender, mockCommand, "", debugArgs);
Assert.assertEquals(0, this.core.getMVWorldManager().getMVWorlds().size());
Assert.assertEquals(0, creator.getCore().getMVWorldManager().getMVWorlds().size());
// Import the first world.
plugin.onCommand(mockCommandSender, mockCommand, "", normalArgs);
verify(this.mockCommandSender).sendMessage(ChatColor.AQUA + "Starting world import...");
verify(this.mockCommandSender).sendMessage(ChatColor.GREEN + "Complete!");
verify(mockCommandSender).sendMessage(ChatColor.AQUA + "Starting world import...");
verify(mockCommandSender).sendMessage(ChatColor.GREEN + "Complete!");
// We should now have one world imported!
Assert.assertEquals(1, this.core.getMVWorldManager().getMVWorlds().size());
}
@Test
public void testEnableDebugMode() {
// Start actual testing.
// Pull a core instance from the server.
Plugin plugin = this.mockServer.getPluginManager().getPlugin("Multiverse-Core");
// Make sure Core is not null
Assert.assertNotNull(plugin);
// Make sure Core is enabled
Assert.assertTrue(plugin.isEnabled());
// Make a fake server folder to fool MV into thinking a world folder exists.
File serverDirectory = new File(this.core.getServerFolder(), "world");
serverDirectory.mkdirs();
// Initialize a fake command
Command mockCommand = mock(Command.class);
when(mockCommand.getName()).thenReturn("mv");
// Assert debug mode is off
Assert.assertEquals(0, MultiverseCore.GlobalDebug);
// Send the debug command.
String[] debugArgs = new String[]{"debug", "3"};
plugin.onCommand(mockCommandSender, mockCommand, "", debugArgs);
Assert.assertEquals(3, MultiverseCore.GlobalDebug);
Assert.assertEquals(1, creator.getCore().getMVWorldManager().getMVWorlds().size());
}
}

View File

@ -0,0 +1,46 @@
/******************************************************************************
* Multiverse 2 Copyright (c) the Multiverse Team 2011. *
* Multiverse 2 is licensed under the BSD License. *
* For more information please check the README.md file included *
* with this project. *
******************************************************************************/
package com.onarandombox.MultiverseCore.test;
import org.bukkit.WorldCreator;
import org.mockito.ArgumentMatcher;
/**
* Multiverse 2
*
* @author fernferret
*/
public class WorldCreatorMatcher extends ArgumentMatcher<WorldCreator> {
private WorldCreator worldCreator;
public WorldCreatorMatcher(WorldCreator creator) {
this.worldCreator = creator;
}
public boolean matches(Object creator) {
System.out.println("Checking world creators.");
if (!((WorldCreator) creator).name().equals(this.worldCreator.name())) {
System.out.println("Checking Names...");
return false;
} else if (!((WorldCreator) creator).environment().equals(this.worldCreator.environment())) {
System.out.println("Checking Environments...");
return false;
}
// Don't check the seed by default, as it's randomized.
// else if (((WorldCreator) creator).seed() != this.worldCreator.seed()) {
// System.out.print("Checking Seeds...");
// return false;
// }
// else if (!((WorldCreator) creator).generator().equals(this.worldCreator.generator())) {
// System.out.print("Checking Gens...");
// return false;
// }
System.out.println("Creators matched!!!");
return true;
}
}