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;
}
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);
when(w.getGenerator()).thenReturn(generator);
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.utils.FileUtils;
import junit.framework.Assert;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.permissions.Permission;
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.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.io.File;
import java.lang.reflect.Field;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@RunWith(PowerMockRunner.class)
@PrepareForTest({MultiverseCore.class})
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() {
// // 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);
TestInstanceCreator creator = new TestInstanceCreator();
Server mockServer = creator.setupDefaultServerInstance();
CommandSender mockCommandSender = creator.getCommandSender();
// Start actual testing.
// Pull a core instance from the server.
Plugin plugin = 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(creator.getCore().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

@ -21,9 +21,7 @@ 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;
import static org.mockito.Mockito.*;
/**
* Multiverse 2
@ -34,7 +32,7 @@ public class TestInstanceCreator {
MultiverseCore core;
private CommandSender commandSender;
public Server setupServerInstance() {
public Server setupDefaultServerInstance() {
MockWorldFactory worldFactory = new MockWorldFactory();
MVCoreFactory coreFactory = new MVCoreFactory();
@ -51,20 +49,37 @@ public class TestInstanceCreator {
// Initialize the Mock Worlds
World mockWorld = worldFactory.makeNewMockWorld("world", World.Environment.NORMAL);
World mockNetherWorld = worldFactory.makeNewMockWorld("world_nether", World.Environment.NETHER);
World mockSkyWorld = worldFactory.makeNewMockWorld("world_skylands", World.Environment.SKYLANDS);
List<World> worldList = new ArrayList<World>();
worldList.add(mockWorld);
worldList.add(mockNetherWorld);
worldList.add(mockSkyWorld);
// Initialize the Mock server.
Server mockServer = serverFactory.getMockServer();
// Give the server some worlds
when(mockServer.getWorld("world")).thenReturn(mockWorld);
when(mockServer.getWorld("world_nether")).thenReturn(mockNetherWorld);
when(mockServer.getWorld("world_skylands")).thenReturn(mockNetherWorld);
when(mockServer.getWorlds()).thenReturn(worldList);
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"));
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(matchNetherWorld))).thenReturn(mockNetherWorld);
when(mockServer.createWorld(Matchers.argThat(matchSkyWorld))).thenReturn(mockSkyWorld);
// Override some methods that bukkit normally provides us with for Core
doReturn(mockServer).when(core).getServer();
@ -79,6 +94,7 @@ public class TestInstanceCreator {
core.onEnable();
return mockServer;
}
public MultiverseCore getCore() {
return this.core;
}

View File

@ -3,28 +3,23 @@ package com.onarandombox.MultiverseCore.test;
import com.onarandombox.MultiverseCore.MultiverseCore;
import com.onarandombox.MultiverseCore.utils.FileUtils;
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.CommandSender;
import org.bukkit.permissions.Permission;
import org.bukkit.plugin.Plugin;
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.Matchers;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
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.*;
@ -32,10 +27,6 @@ import static org.mockito.Mockito.*;
@PrepareForTest({PluginManager.class, MultiverseCore.class, Permission.class, Bukkit.class})
public class TestWorldImport {
@After
public void tearDown() throws NoSuchFieldException, ClassNotFoundException, IllegalAccessException {
Field serverField = Bukkit.class.getDeclaredField("server");
@ -61,7 +52,7 @@ public class TestWorldImport {
@Test
public void testWorldImportWithNoFolder() {
TestInstanceCreator creator = new TestInstanceCreator();
Server mockServer = creator.setupServerInstance();
Server mockServer = creator.setupDefaultServerInstance();
CommandSender mockCommandSender = creator.getCommandSender();
// Start actual testing.
@ -93,15 +84,17 @@ public class TestWorldImport {
@Test
public void testWorldImport() {
TestInstanceCreator creator = new TestInstanceCreator();
Server mockServer = creator.setupServerInstance();
Server mockServer = creator.setupDefaultServerInstance();
CommandSender mockCommandSender = creator.getCommandSender();
// Start actual testing.
// Pull a core instance from the server.
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(creator.getCore().getServerFolder(), "world");
serverDirectory.mkdirs();
File worldDirectory = new File(creator.getCore().getServerFolder(), "world");
worldDirectory.mkdirs();
File worldNetherDirectory = new File(creator.getCore().getServerFolder(), "world_nether");
worldNetherDirectory.mkdirs();
// Make sure Core is not null
@ -129,5 +122,13 @@ public class TestWorldImport {
// We should now have one world imported!
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;
public WorldCreatorMatcher(WorldCreator creator) {
System.out.println("Creating NEW world matcher.(" + creator.name() + ")");
this.worldCreator = creator;
}
public boolean matches(Object creator) {
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())) {
System.out.println("Checking Names...");
return false;
} else if (!((WorldCreator) creator).environment().equals(this.worldCreator.environment())) {
System.out.println("Checking Environments...");