JoinCommand and ArenaListCommand somewhat done.

This commit is contained in:
Garbage Mule
2011-12-10 23:49:39 +01:00
parent 289b1792f5
commit c420568cb9
28 changed files with 2723 additions and 343 deletions
@@ -0,0 +1,212 @@
package garbagemule.mobarena.standard;
import static org.junit.Assert.*;
import java.io.*;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;
import org.apache.commons.io.FileUtils;
import org.bukkit.util.config.Configuration;
import org.junit.*;
import com.garbagemule.MobArena.util.Config;
@SuppressWarnings("deprecation")
public class TestNewConfig
{
private Configuration oldConfig;
private Config newConfig;
private File testFileOld, testFileNew;
@Before
public void setup() {
testFileOld = makeTestFile("test", "config.yml");
testFileNew = makeTestFile("test", "config.yml");
oldConfig = new Configuration(testFileOld);
newConfig = new Config(testFileNew);
oldConfig.load();
newConfig.load();
}
@After
public void cleanup() {
testFileOld.delete();
testFileNew.delete();
}
@Test
public void getProperty() {
String path = "arenas.lol.rewards.waves.after.7";
Object oldObj = oldConfig.getProperty(path);
Object newObj = newConfig.getProperty(path);
assertEquals(oldObj, newObj);
}
@Test
public void getInt() {
String path = "arenas.lol.settings.first-wave-delay";
int oldInt = oldConfig.getInt(path, -1);
int newInt = newConfig.getInt(path, -2);
assertEquals(oldInt, newInt);
}
@Test
public void getString() {
String path = "classes.Knight.items";
String oldString = oldConfig.getString(path, "old");
String newString = newConfig.getString(path, "new");
assertEquals(oldString, newString);
path = "classes.Knight.armor";
oldString = oldConfig.getString(path, "old");
newString = newConfig.getString(path, "new");
assertEquals(oldString, newString);
}
@Test
public void getBoolean() {
String path = "global-settings.enabled";
boolean oldBoolean = oldConfig.getBoolean(path, false);
boolean newBoolean = newConfig.getBoolean(path, true);
assertEquals(oldBoolean, newBoolean);
// Test with both permutations of the default value.
oldBoolean = oldConfig.getBoolean(path, true);
newBoolean = newConfig.getBoolean(path, false);
assertEquals(oldBoolean, newBoolean);
}
@Test
public void getKeys() {
String path = "classes";
List<String> oldList = new LinkedList<String>();
List<String> newList = new LinkedList<String>();
oldList.addAll(oldConfig.getKeys(path));
newList.addAll(newConfig.getKeys(path));
// Sort, just in case.
Collections.sort(oldList);
Collections.sort(newList);
assertEquals(oldList, newList);
}
@Test
public void getStringList() {
String path = "classes.Knight.permissions";
List<String> oldList = oldConfig.getStringList(path, null);
List<String> newList = newConfig.getStringList(path, null);
assertNotNull(oldList);
assertNotNull(newList);
assertFalse(oldList.isEmpty());
assertFalse(newList.isEmpty());
assertEquals(oldList, newList);
// Empty lists.
path = "classes.Tank.permissions";
oldList = oldConfig.getStringList(path, new LinkedList<String>());
newList = newConfig.getStringList(path, new LinkedList<String>());
assertNotNull(oldList);
assertNotNull(newList);
assertTrue(oldList.isEmpty());
assertTrue(newList.isEmpty());
assertEquals(oldList, newList);
// Null values behave the same as empty lists.
oldList = oldConfig.getStringList(path, null);
newList = newConfig.getStringList(path, null);
assertNotNull(oldList);
assertNotNull(newList);
assertTrue(oldList.isEmpty());
assertTrue(newList.isEmpty());
assertEquals(oldList, newList);
// Non-existing lists.
path = "classes.Archer.permissions";
oldList = oldConfig.getStringList(path, new LinkedList<String>());
newList = newConfig.getStringList(path, new LinkedList<String>());
assertNotNull(oldList);
assertNotNull(newList);
assertTrue(oldList.isEmpty());
assertTrue(newList.isEmpty());
assertEquals(oldList, newList);
}
@Test
public void removeProperty() {
String path = "global-settings.enabled";
// Make sure something exists first.
Object oldObj = oldConfig.getProperty(path);
Object newObj = newConfig.getProperty(path);
assertNotNull(oldObj);
assertNotNull(newObj);
assertEquals(oldObj, newObj);
// Remove and try again.
oldConfig.removeProperty(path);
newConfig.removeProperty(path);
// They should now both be null.
oldObj = oldConfig.getProperty(path);
newObj = newConfig.getProperty(path);
assertNull(oldObj);
assertNull(newObj);
assertEquals(oldObj, newObj);
// Should be idempotent.
oldConfig.removeProperty(path);
newConfig.removeProperty(path);
oldObj = oldConfig.getProperty(path);
newObj = newConfig.getProperty(path);
assertNull(oldObj);
assertNull(newObj);
assertEquals(oldObj, newObj);
}
private File makeTestFile(String path, String filename) {
// Grab the time
Timestamp timestamp = new Timestamp(new Date().getTime());
// Convert to a string
String time = new SimpleDateFormat("MM-dd-yyyy-H_m_s_S").format(timestamp);
// Make a new file
File testFile = new File(path + File.separator + time + "_" + filename);
// Start copying the bytes
try {
FileUtils.copyFile(new File(path + File.separator + filename), testFile);
}
catch (Exception e) {
e.printStackTrace();
}
return testFile;
}
}
@@ -0,0 +1,208 @@
package garbagemule.mobarena.standard;
import static org.junit.Assert.*;
import mock.bukkit.*;
import mock.mobarena.*;
import mock.util.MockLogger;
import org.junit.*;
import com.garbagemule.MobArena.*;
import com.garbagemule.MobArena.commands.*;
public class TestUserCommands
{
private static final String LABEL = "ma";
private MockLogger log;
private String expected;
private MobArenaPlugin plugin;
private ArenaMaster arenaMaster;
private CommandHandler commandHandler;
private MockPlayer player;
@Before
public void setup() {
// Set up the log.;
log = new MockLogger();
log.log("Start");
// Create a plugin.
plugin = new MockMobArena();
// Get the ArenaMaster.
arenaMaster = plugin.getArenaMaster();
// Create a player and a command handler.
player = new MockPlayer("garbagemule", log);
commandHandler = new CommandHandler(plugin);
}
@After
public void cleanup() {
log = null;
plugin = null;
arenaMaster = null;
player = null;
commandHandler = null;
}
/* ********************************************************************* */
/*
/* JOIN COMMAND
/*
/* ********************************************************************* */
// TODO: Expand all tests to check that the player is indeed added and warped.
@Test
public void expectArguments() {
commandHandler.onCommand(player, null, LABEL, compileArgs());
expected = MockLogger.compileMsgToPlayer(player, Msg.MISC_NONE);
assertEquals(expected, log.getLastEntry().getValue());
}
@Test
public void playerCanJoinExistingArena() {
// Create some arenas.
createArenas("test","test2","test21","test12");
// Try to join an existing arena.
commandHandler.onCommand(player, null, LABEL, compileArgs("join","test"));
// Success!
expected = MockLogger.compileMsgToPlayer(player, Msg.JOIN_PLAYER_JOINED);
assertEquals(expected, log.getLastEntry().getValue());
// Verify that the player is now in the lobby.
Arena a = arenaMaster.getArenaWithName("test");
assertTrue(a.inLobby(player));
}
@Test
public void playerCantJoinNonExistingArena() {
// Create some arenas.
createArenas("test","test2","test21","test12");
// Try to join one that doesn't exist.
commandHandler.onCommand(player, null, LABEL, compileArgs("join","test1"));
// Failure!
expected = MockLogger.compileMsgToPlayer(player, Msg.ARENA_DOES_NOT_EXIST);
assertEquals(expected, log.getLastEntry().getValue());
}
@Test
public void argsRequiredForMultipleArenas() {
// Create some arenas.
createArenas("test","test2","test21","test12");
// Try to join with no arguments.
commandHandler.onCommand(player, null, LABEL, compileArgs("join"));
// Should join the only existing arena.
expected = MockLogger.compileMsgToPlayer(player, Msg.JOIN_ARG_NEEDED);
assertEquals(expected, log.getLastEntry().getValue());
}
@Test
public void noArgsNeededForOneArenaOnly() {
// Create one arena.
createArenas("gjklqwejklewjglkejg");
// Try to join with no arguments.
commandHandler.onCommand(player, null, LABEL, compileArgs("join"));
// Should join the only existing arena.
expected = MockLogger.compileMsgToPlayer(player, Msg.JOIN_PLAYER_JOINED);
assertEquals(expected, log.getLastEntry().getValue());
// Verify that the player is now in the lobby.
Arena a = arenaMaster.getArenaWithName("gjklqwejklewjglkejg");
assertTrue(a.inLobby(player));
}
@Test
public void playerCanOnlyJoinPermittedArenas() {
// Create a couple of arenas.
createArenas("yay", "nay");
// Give the player no permission for one.
player.setPermission("mobarena.arenas.nay", false);
// Try to join.
commandHandler.onCommand(player, null, LABEL, compileArgs("join","nay"));
// No permission.
expected = MockLogger.compileMsgToPlayer(player, Msg.JOIN_ARENA_PERMISSION);
assertEquals(expected, log.getLastEntry().getValue());
// Verify that the player didn't join and isn't in the lobby.
Arena a = arenaMaster.getArenaWithName("nay");
assertFalse(a.inLobby(player));
}
/* ********************************************************************* */
/*
/* LIST ARENAS COMMAND
/*
/* ********************************************************************* */
@Test
public void shouldListAllArenas() {
// Create some arenas.
createArenas("one", "two", "three", "four");
// Try to list them.
commandHandler.onCommand(player, null, LABEL, compileArgs("arenas"));
// Should show all of them, because player has permission to all per default.
expected = MockLogger.compileMsgToPlayer(player, Msg.MISC_LIST_ARENAS.toString("one, two, three, four"));
assertEquals(expected, log.getLastEntry().getValue());
}
@Test
public void shouldListMiscNoneIfNoArenas() {
// Create an arena and remove the permission.
createArenas("one");
player.setPermission("mobarena.arenas.one", false);
// Try to list arenas.
commandHandler.onCommand(player, null, LABEL, compileArgs("arenas"));
// Should display MISC_NONE
String none = Msg.MISC_NONE.toString();
expected = MockLogger.compileMsgToPlayer(player, Msg.MISC_LIST_ARENAS.toString(none));
assertEquals(expected, log.getLastEntry().getValue());
}
@Test
public void shouldDisplayOnlyPermittedArenas() {
// Create three arenas, remove permissions for one.
createArenas("one", "two", "three");
player.setPermission("mobarena.arenas.two", false);
// List arenas.
commandHandler.onCommand(player, null, LABEL, compileArgs("arenas"));
// Should display one and three.
expected = MockLogger.compileMsgToPlayer(player, Msg.MISC_LIST_ARENAS.toString("one, three"));
assertEquals(expected, log.getLastEntry().getValue());
}
private void createArenas(String... args) {
for (String s : args) {
arenaMaster.createArenaNode(s, null);
}
}
private String[] compileArgs(String... args) {
return args;
}
}