Some fixes + added tests.

This commit is contained in:
main() 2012-03-07 19:59:20 +01:00
parent fd227960b9
commit d1280a9031
4 changed files with 97 additions and 21 deletions

View File

@ -451,6 +451,14 @@ public class MVWorld extends SerializationConfig implements MultiverseWorld {
}
}
/**
* {@inheritDoc}
*/
@Override
public void copyValues(SerializationConfig other) {
super.copyValues(other);
}
/**
* Sets the CB-World.
* <p>

View File

@ -567,12 +567,26 @@ public class WorldManager implements MVWorldManager {
// load world-objects
Stack<String> worldKeys = new Stack<String>();
worldKeys.addAll(this.configWorlds.getConfigurationSection("worlds").getKeys(false));
Map<String, MVWorld> newWorldsFromTheConfig = new HashMap<String, MVWorld>();
while (!worldKeys.isEmpty()) {
String key = worldKeys.pop();
String path = "worlds" + SEPARATOR + key;
Object obj = this.configWorlds.get(path);
if ((obj != null) && (obj instanceof MVWorld)) {
this.worldsFromTheConfig.put(key.replaceAll(String.valueOf(SEPARATOR), "."), (MVWorld) obj);
String worldName = key.replaceAll(String.valueOf(SEPARATOR), ".");
if (this.worldsFromTheConfig.containsKey(worldName)) {
// Object-Recycling :D
MVWorld oldMVWorld = (MVWorld) this.worlds.get(worldName);
oldMVWorld.copyValues((MVWorld) obj);
newWorldsFromTheConfig.put(worldName, oldMVWorld);
} else {
// we have to use a new one
World cbworld = this.plugin.getServer().getWorld(worldName);
MVWorld mvworld = (MVWorld) obj;
if (cbworld != null)
mvworld.init(cbworld, this.plugin);
newWorldsFromTheConfig.put(worldName, mvworld);
}
} else if (this.configWorlds.isConfigurationSection(path)) {
ConfigurationSection section = this.configWorlds.getConfigurationSection(path);
Set<String> subkeys = section.getKeys(false);
@ -581,6 +595,8 @@ public class WorldManager implements MVWorldManager {
}
}
}
this.worldsFromTheConfig = newWorldsFromTheConfig;
this.worlds.keySet().retainAll(this.worldsFromTheConfig.keySet());
return this.configWorlds;
}

View File

@ -12,15 +12,16 @@ import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.*;
import java.io.File;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Difficulty;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.HumanEntity;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.entity.EntityRegainHealthEvent;
@ -43,6 +44,7 @@ import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import com.onarandombox.MultiverseCore.MVWorld;
import com.onarandombox.MultiverseCore.MultiverseCore;
import com.onarandombox.MultiverseCore.api.MVWorldManager;
import com.onarandombox.MultiverseCore.api.MultiverseWorld;
@ -91,7 +93,7 @@ public class TestWorldProperties {
}
@Test
public void test() {
public void test() throws Exception {
// Initialize a fake command
Command mockCommand = mock(Command.class);
when(mockCommand.getName()).thenReturn("mv");
@ -129,12 +131,9 @@ public class TestWorldProperties {
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)
assertEquals(1D, mvWorld.getScaling(), 0);
assertNull(mvWorld.getRespawnToWorld());
assertTrue(mvWorld.isWeatherEnabled());
World cbWorld = mvWorld.getCBWorld();
when(cbWorld.getDifficulty()).thenReturn(Difficulty.NORMAL);
assertEquals(Difficulty.NORMAL, mvWorld.getDifficulty());
assertTrue(mvWorld.canAnimalsSpawn());
assertTrue(mvWorld.canMonstersSpawn());
@ -201,24 +200,21 @@ public class TestWorldProperties {
mvWorld.setAlias("alias");
assertEquals("alias", mvWorld.getAlias());
assertTrue(mvWorld.setColor("BLACK"));
ChatColor oldColor = mvWorld.getColor();
assertFalse(mvWorld.setColor("INVALID COLOR"));
assertEquals(oldColor, mvWorld.getColor());
assertEquals(oldColor.toString() + "alias" + ChatColor.WHITE.toString(), mvWorld.getColoredWorldString());
assertEquals(ChatColor.BLACK, mvWorld.getColor());
assertEquals(ChatColor.BLACK.toString() + "alias" + ChatColor.WHITE.toString(), mvWorld.getColoredWorldString());
mvWorld.setPVPMode(false);
assertEquals(false, mvWorld.isPVPEnabled());
assertTrue(mvWorld.setScaling(2D));
assertEquals((Object) 2D, (Object) mvWorld.getScaling());
assertEquals(2D, mvWorld.getScaling(), 0);
assertFalse(mvWorld.setRespawnToWorld("INVALID WORLD"));
assertTrue(mvWorld.setRespawnToWorld("world_nether"));
assertSame(worldManager.getMVWorld("world_nether").getCBWorld(),
mvWorld.getRespawnToWorld());
mvWorld.setEnableWeather(false);
assertEquals(false, mvWorld.isWeatherEnabled());
assertTrue(mvWorld.setDifficulty("PEACEFUL"));
Difficulty oldDifficulty = mvWorld.getDifficulty();
assertFalse(mvWorld.setDifficulty("INVALID DIFFICULTY"));
assertEquals(oldDifficulty, mvWorld.getDifficulty());
assertTrue(mvWorld.setDifficulty(Difficulty.PEACEFUL));
assertEquals(Difficulty.PEACEFUL, mvWorld.getDifficulty());
mvWorld.setAllowAnimalSpawn(false);
assertEquals(false, mvWorld.canAnimalsSpawn());
mvWorld.setAllowMonsterSpawn(false);
@ -226,17 +222,15 @@ public class TestWorldProperties {
mvWorld.setCurrency(1);
assertEquals(1, mvWorld.getCurrency());
mvWorld.setPrice(1D);
assertEquals((Object) 1D, (Object) mvWorld.getPrice());
assertEquals(1D, mvWorld.getPrice(), 0);
mvWorld.setHunger(false);
assertEquals(false, mvWorld.getHunger());
mvWorld.setAutoHeal(false);
assertEquals(false, mvWorld.getAutoHeal());
mvWorld.setAdjustSpawn(false);
assertEquals(false, mvWorld.getAdjustSpawn());
assertTrue(mvWorld.setGameMode("CREATIVE"));
GameMode oldGamemode = mvWorld.getGameMode();
assertFalse(mvWorld.setGameMode("INVALID GAMEMODE"));
assertEquals(oldGamemode, mvWorld.getGameMode());
assertTrue(mvWorld.setGameMode(GameMode.CREATIVE));
assertEquals(GameMode.CREATIVE, mvWorld.getGameMode());
mvWorld.setKeepSpawnInMemory(false);
assertEquals(false, mvWorld.isKeepingSpawnInMemory());
mvWorld.setBedRespawn(false);
@ -246,6 +240,7 @@ public class TestWorldProperties {
mvWorld.setSpawnLocation(new Location(mvWorld.getCBWorld(), 1, 1, 1));
assertEquals(new SpawnLocation(1, 1, 1), mvWorld.getSpawnLocation());
/* ****************************************** *
* Call some events and verify behavior
* ****************************************** */
@ -276,6 +271,7 @@ public class TestWorldProperties {
core.getMVConfig().setPrefixChat(false);
core.getPlayerListener().playerChat(playerChatEvent);
verify(playerChatEvent, times(1)).setFormat(anyString()); // only ONE TIME (not the 2nd time!)
mvWorld.setHidden(true); // reset hidden-state
// call player join events
core.getPlayerListener().playerJoin(playerJoinEvent);
@ -294,6 +290,44 @@ public class TestWorldProperties {
core.getEntityListener().entityRegainHealth(entityRegainHealthEvent);
// autoheal is off so something should happen
verify(entityRegainHealthEvent).setCancelled(true);
/* ****************************************** *
* Test saving/loading
* ****************************************** */
assertTrue(core.saveMVConfigs());
// change a value here
FileConfiguration config = YamlConfiguration.loadConfiguration(new File(core.getDataFolder(), "worlds.yml"));
MVWorld worldObj = (MVWorld) config.get("worlds.world");
assertTrue(worldObj.setColor("GREEN"));
config.set("worlds.world", worldObj);
config.save(new File(core.getDataFolder(), "worlds.yml"));
// load
core.loadConfigs();
mvWorld = worldManager.getMVWorld("world");
assertEquals(true, mvWorld.isHidden());
assertEquals("alias", mvWorld.getAlias());
assertEquals(ChatColor.GREEN, mvWorld.getColor());
assertEquals(ChatColor.GREEN.toString() + "alias" + ChatColor.WHITE.toString(), mvWorld.getColoredWorldString());
assertEquals(false, mvWorld.isPVPEnabled());
assertEquals(2D, mvWorld.getScaling(), 0);
assertSame(worldManager.getMVWorld("world_nether").getCBWorld(),
mvWorld.getRespawnToWorld());
assertEquals(false, mvWorld.isWeatherEnabled());
assertEquals(Difficulty.PEACEFUL, mvWorld.getDifficulty());
assertEquals(false, mvWorld.canAnimalsSpawn());
assertEquals(false, mvWorld.canMonstersSpawn());
assertEquals(1, mvWorld.getCurrency());
assertEquals(1D, mvWorld.getPrice(), 0);
assertEquals(false, mvWorld.getHunger());
assertEquals(false, mvWorld.getAutoHeal());
assertEquals(false, mvWorld.getAdjustSpawn());
assertEquals(GameMode.CREATIVE, mvWorld.getGameMode());
assertEquals(false, mvWorld.isKeepingSpawnInMemory());
assertEquals(false, mvWorld.getBedRespawn());
assertEquals(false, mvWorld.getAutoLoad());
assertEquals(new SpawnLocation(1, 1, 1), mvWorld.getSpawnLocation());
}
public void createEvents(MultiverseWorld mvWorld) {

View File

@ -16,6 +16,7 @@ import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
import org.bukkit.Difficulty;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
@ -31,6 +32,7 @@ public class MockWorldFactory {
private static final Map<World, Boolean> pvpStates = new WeakHashMap<World, Boolean>();
private static final Map<World, Boolean> keepSpawnInMemoryStates = new WeakHashMap<World, Boolean>();
private static final Map<World, Difficulty> difficultyStates = new WeakHashMap<World, Difficulty>();
private MockWorldFactory() {
}
@ -74,6 +76,22 @@ public class MockWorldFactory {
return null;
}
}).when(mockWorld).setKeepSpawnInMemory(anyBoolean());
when(mockWorld.getDifficulty()).thenAnswer(new Answer<Difficulty>() {
@Override
public Difficulty answer(InvocationOnMock invocation) throws Throwable {
World w = (World) invocation.getMock();
if (!difficultyStates.containsKey(w))
difficultyStates.put(w, Difficulty.NORMAL); // default value
return difficultyStates.get(w);
}
});
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
difficultyStates.put((World) invocation.getMock(), (Difficulty) invocation.getArguments()[0]);
return null;
}
}).when(mockWorld).setDifficulty(any(Difficulty.class));
when(mockWorld.getEnvironment()).thenReturn(env);
when(mockWorld.getWorldType()).thenReturn(type);
when(mockWorld.getSpawnLocation()).thenReturn(new Location(mockWorld, 0, 64, 0));