refactor rests even more

This commit is contained in:
Eric Stokes 2011-10-21 17:48:28 -06:00
parent ab311dd8d9
commit 65369b6a4b
5 changed files with 114 additions and 51 deletions

View File

@ -81,9 +81,10 @@ public class MockWorldFactory {
return mockWorld; return mockWorld;
} }
public void makeNewMockWorld(String world, World.Environment env, long seed, ChunkGenerator generator) { public World makeNewMockWorld(String world, World.Environment env, long seed, ChunkGenerator generator) {
World w = this.makeNewMockWorld(world, env); World w = this.makeNewMockWorld(world, env);
when(w.getGenerator()).thenReturn(generator); when(w.getGenerator()).thenReturn(generator);
when(w.getSeed()).thenReturn(seed); when(w.getSeed()).thenReturn(seed);
return w;
} }
} }

View File

@ -9,44 +9,82 @@ package com.onarandombox.MultiverseCore.test;
import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.MultiverseCore;
import com.onarandombox.MultiverseCore.utils.FileUtils;
import junit.framework.Assert; import junit.framework.Assert;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.permissions.Permission;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.io.File; import java.io.File;
import java.lang.reflect.Field;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@RunWith(PowerMockRunner.class)
@PrepareForTest({MultiverseCore.class})
public class TestDebugMode { public class TestDebugMode {
@Test @After
public void tearDown() throws NoSuchFieldException, ClassNotFoundException, IllegalAccessException {
Field serverField = Bukkit.class.getDeclaredField("server");
serverField.setAccessible(true);
serverField.set(Class.forName("org.bukkit.Bukkit"), null);
if (MVCoreFactory.serverDirectory.exists()) {
MVCoreFactory.serverDirectory.delete();
FileUtils.deleteFolder(MVCoreFactory.serverDirectory);
}
}
@Before
public void setUp() throws Exception {
if (!MVCoreFactory.serverDirectory.exists()) {
MVCoreFactory.serverDirectory.mkdirs();
}
if (!MVCoreFactory.pluginDirectory.exists()) {
MVCoreFactory.pluginDirectory.mkdirs();
}
}
@Test
public void testEnableDebugMode() { public void testEnableDebugMode() {
// // Start actual testing. TestInstanceCreator creator = new TestInstanceCreator();
// // Pull a core instance from the server. Server mockServer = creator.setupDefaultServerInstance();
// Plugin plugin = this.mockServer.getPluginManager().getPlugin("Multiverse-Core"); CommandSender mockCommandSender = creator.getCommandSender();
// // Start actual testing.
// // Make sure Core is not null // Pull a core instance from the server.
// Assert.assertNotNull(plugin); Plugin plugin = mockServer.getPluginManager().getPlugin("Multiverse-Core");
//
// // Make sure Core is enabled // Make sure Core is not null
// Assert.assertTrue(plugin.isEnabled()); Assert.assertNotNull(plugin);
//
// // Make a fake server folder to fool MV into thinking a world folder exists. // Make sure Core is enabled
// File serverDirectory = new File(this.core.getServerFolder(), "world"); Assert.assertTrue(plugin.isEnabled());
// serverDirectory.mkdirs();
// // Make a fake server folder to fool MV into thinking a world folder exists.
// // Initialize a fake command File serverDirectory = new File(creator.getCore().getServerFolder(), "world");
// Command mockCommand = mock(Command.class); serverDirectory.mkdirs();
// when(mockCommand.getName()).thenReturn("mv");
// // Initialize a fake command
// // Assert debug mode is off Command mockCommand = mock(Command.class);
// Assert.assertEquals(0, MultiverseCore.GlobalDebug); when(mockCommand.getName()).thenReturn("mv");
//
// // Send the debug command. // Assert debug mode is off
// String[] debugArgs = new String[]{"debug", "3"}; Assert.assertEquals(0, MultiverseCore.GlobalDebug);
// plugin.onCommand(mockCommandSender, mockCommand, "", debugArgs);
// // Send the debug command.
// Assert.assertEquals(3, MultiverseCore.GlobalDebug); String[] debugArgs = new String[]{"debug", "3"};
plugin.onCommand(mockCommandSender, mockCommand, "", debugArgs);
Assert.assertEquals(3, MultiverseCore.GlobalDebug);
} }
} }

View File

@ -21,9 +21,7 @@ import org.powermock.api.mockito.PowerMockito;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.*;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
/** /**
* Multiverse 2 * Multiverse 2
@ -34,7 +32,7 @@ public class TestInstanceCreator {
MultiverseCore core; MultiverseCore core;
private CommandSender commandSender; private CommandSender commandSender;
public Server setupServerInstance() { public Server setupDefaultServerInstance() {
MockWorldFactory worldFactory = new MockWorldFactory(); MockWorldFactory worldFactory = new MockWorldFactory();
MVCoreFactory coreFactory = new MVCoreFactory(); MVCoreFactory coreFactory = new MVCoreFactory();
@ -51,20 +49,37 @@ public class TestInstanceCreator {
// Initialize the Mock Worlds // Initialize the Mock Worlds
World mockWorld = worldFactory.makeNewMockWorld("world", World.Environment.NORMAL); World mockWorld = worldFactory.makeNewMockWorld("world", World.Environment.NORMAL);
World mockNetherWorld = worldFactory.makeNewMockWorld("world_nether", World.Environment.NETHER); World mockNetherWorld = worldFactory.makeNewMockWorld("world_nether", World.Environment.NETHER);
World mockSkyWorld = worldFactory.makeNewMockWorld("world_skylands", World.Environment.SKYLANDS);
List<World> worldList = new ArrayList<World>(); List<World> worldList = new ArrayList<World>();
worldList.add(mockWorld); worldList.add(mockWorld);
worldList.add(mockNetherWorld); worldList.add(mockNetherWorld);
worldList.add(mockSkyWorld);
// Initialize the Mock server. // Initialize the Mock server.
Server mockServer = serverFactory.getMockServer(); Server mockServer = serverFactory.getMockServer();
// Give the server some worlds
when(mockServer.getWorld("world")).thenReturn(mockWorld); when(mockServer.getWorld("world")).thenReturn(mockWorld);
when(mockServer.getWorld("world_nether")).thenReturn(mockNetherWorld); when(mockServer.getWorld("world_nether")).thenReturn(mockNetherWorld);
when(mockServer.getWorld("world_skylands")).thenReturn(mockNetherWorld);
when(mockServer.getWorlds()).thenReturn(worldList); when(mockServer.getWorlds()).thenReturn(worldList);
when(mockServer.getPluginManager()).thenReturn(mockPluginManager); when(mockServer.getPluginManager()).thenReturn(mockPluginManager);
// TODO: This needs to get moved somewhere specific.
// Initialize some worldCreatorMatchers (so we can see when a specific creator is called)
WorldCreatorMatcher matchWorld = new WorldCreatorMatcher(new WorldCreator("world")); WorldCreatorMatcher matchWorld = new WorldCreatorMatcher(new WorldCreator("world"));
WorldCreator netherCreator = new WorldCreator("world_nether");
netherCreator.environment(World.Environment.NETHER);
WorldCreatorMatcher matchNetherWorld = new WorldCreatorMatcher(netherCreator);
WorldCreator skyCreator = new WorldCreator("world_skylands");
skyCreator.environment(World.Environment.SKYLANDS);
WorldCreatorMatcher matchSkyWorld = new WorldCreatorMatcher(skyCreator);
// If a specific creator is called, return the appropreate world.
when(mockServer.createWorld(Matchers.argThat(matchWorld))).thenReturn(mockWorld); when(mockServer.createWorld(Matchers.argThat(matchWorld))).thenReturn(mockWorld);
when(mockServer.createWorld(Matchers.argThat(matchNetherWorld))).thenReturn(mockNetherWorld);
when(mockServer.createWorld(Matchers.argThat(matchSkyWorld))).thenReturn(mockSkyWorld);
// Override some methods that bukkit normally provides us with for Core // Override some methods that bukkit normally provides us with for Core
doReturn(mockServer).when(core).getServer(); doReturn(mockServer).when(core).getServer();
@ -79,6 +94,7 @@ public class TestInstanceCreator {
core.onEnable(); core.onEnable();
return mockServer; return mockServer;
} }
public MultiverseCore getCore() { public MultiverseCore getCore() {
return this.core; return this.core;
} }

View File

@ -3,28 +3,23 @@ package com.onarandombox.MultiverseCore.test;
import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.MultiverseCore;
import com.onarandombox.MultiverseCore.utils.FileUtils; import com.onarandombox.MultiverseCore.utils.FileUtils;
import junit.framework.Assert; import junit.framework.Assert;
import org.bukkit.*; import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Server;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.permissions.Permission; import org.bukkit.permissions.Permission;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Matchers;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.modules.junit4.PowerMockRunner;
import java.io.File; import java.io.File;
import java.lang.reflect.Field; 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.*; import static org.mockito.Mockito.*;
@ -32,10 +27,6 @@ import static org.mockito.Mockito.*;
@PrepareForTest({PluginManager.class, MultiverseCore.class, Permission.class, Bukkit.class}) @PrepareForTest({PluginManager.class, MultiverseCore.class, Permission.class, Bukkit.class})
public class TestWorldImport { public class TestWorldImport {
@After @After
public void tearDown() throws NoSuchFieldException, ClassNotFoundException, IllegalAccessException { public void tearDown() throws NoSuchFieldException, ClassNotFoundException, IllegalAccessException {
Field serverField = Bukkit.class.getDeclaredField("server"); Field serverField = Bukkit.class.getDeclaredField("server");
@ -61,7 +52,7 @@ public class TestWorldImport {
@Test @Test
public void testWorldImportWithNoFolder() { public void testWorldImportWithNoFolder() {
TestInstanceCreator creator = new TestInstanceCreator(); TestInstanceCreator creator = new TestInstanceCreator();
Server mockServer = creator.setupServerInstance(); Server mockServer = creator.setupDefaultServerInstance();
CommandSender mockCommandSender = creator.getCommandSender(); CommandSender mockCommandSender = creator.getCommandSender();
// Start actual testing. // Start actual testing.
@ -93,15 +84,17 @@ public class TestWorldImport {
@Test @Test
public void testWorldImport() { public void testWorldImport() {
TestInstanceCreator creator = new TestInstanceCreator(); TestInstanceCreator creator = new TestInstanceCreator();
Server mockServer = creator.setupServerInstance(); Server mockServer = creator.setupDefaultServerInstance();
CommandSender mockCommandSender = creator.getCommandSender(); CommandSender mockCommandSender = creator.getCommandSender();
// Start actual testing. // Start actual testing.
// Pull a core instance from the server. // Pull a core instance from the server.
Plugin plugin = 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. // Make a fake server folder to fool MV into thinking a world folder exists.
File serverDirectory = new File(creator.getCore().getServerFolder(), "world"); File worldDirectory = new File(creator.getCore().getServerFolder(), "world");
serverDirectory.mkdirs(); worldDirectory.mkdirs();
File worldNetherDirectory = new File(creator.getCore().getServerFolder(), "world_nether");
worldNetherDirectory.mkdirs();
// Make sure Core is not null // Make sure Core is not null
@ -129,5 +122,13 @@ public class TestWorldImport {
// We should now have one world imported! // We should now have one world imported!
Assert.assertEquals(1, creator.getCore().getMVWorldManager().getMVWorlds().size()); Assert.assertEquals(1, creator.getCore().getMVWorldManager().getMVWorlds().size());
// Import the second world.
plugin.onCommand(mockCommandSender, mockCommand, "", netherArgs);
// verify(mockCommandSender).sendMessage(ChatColor.AQUA + "Starting world import...");
// verify(mockCommandSender).sendMessage(ChatColor.GREEN + "Complete!");
// We should now have 2 worlds imported!
Assert.assertEquals(2, creator.getCore().getMVWorldManager().getMVWorlds().size());
} }
} }

View File

@ -19,13 +19,20 @@ public class WorldCreatorMatcher extends ArgumentMatcher<WorldCreator> {
private WorldCreator worldCreator; private WorldCreator worldCreator;
public WorldCreatorMatcher(WorldCreator creator) { public WorldCreatorMatcher(WorldCreator creator) {
System.out.println("Creating NEW world matcher.(" + creator.name() + ")");
this.worldCreator = creator; this.worldCreator = creator;
} }
public boolean matches(Object creator) { public boolean matches(Object creator) {
System.out.println("Checking world creators."); System.out.println("Checking world creators.");
if (creator == null) {
System.out.println("The given creator was null, but I was checking: " + this.worldCreator.name());
return false;
}
System.out.println("Checking Names...(" + ((WorldCreator) creator).name() + ") vs (" + this.worldCreator.name() + ")");
System.out.println("Checking Envs...(" + ((WorldCreator) creator).environment() + ") vs (" + this.worldCreator.environment() + ")");
if (!((WorldCreator) creator).name().equals(this.worldCreator.name())) { if (!((WorldCreator) creator).name().equals(this.worldCreator.name())) {
System.out.println("Checking Names...");
return false; return false;
} else if (!((WorldCreator) creator).environment().equals(this.worldCreator.environment())) { } else if (!((WorldCreator) creator).environment().equals(this.worldCreator.environment())) {
System.out.println("Checking Environments..."); System.out.println("Checking Environments...");