Unit Tests for Economy API

Finally fixed the Currency format

git-svn-id: https://svn.java.net/svn/essentials~svn/trunk@1436 e251c2fe-e539-e718-e476-b85c1f46cddb
This commit is contained in:
snowleo 2011-05-13 19:18:22 +00:00
parent 6c42f42005
commit fa0a0b3301
9 changed files with 703 additions and 34 deletions

View File

@ -58,14 +58,14 @@ public class Essentials extends JavaPlugin
return settings; return settings;
} }
public void setupForTesting() throws IOException, InvalidDescriptionException public void setupForTesting(Server server) throws IOException, InvalidDescriptionException
{ {
File dataFolder = File.createTempFile("essentialstest", ""); File dataFolder = File.createTempFile("essentialstest", "");
dataFolder.delete(); dataFolder.delete();
dataFolder.mkdir(); dataFolder.mkdir();
logger.log(Level.INFO, Util.i18n("usingTempFolderForTesting")); logger.log(Level.INFO, Util.i18n("usingTempFolderForTesting"));
logger.log(Level.INFO, dataFolder.toString()); logger.log(Level.INFO, dataFolder.toString());
this.initialize(null, null, new PluginDescriptionFile(new FileReader(new File("src" + File.separator + "plugin.yml"))), dataFolder, null, null); this.initialize(null, server, new PluginDescriptionFile(new FileReader(new File("src" + File.separator + "plugin.yml"))), dataFolder, null, null);
settings = new Settings(dataFolder); settings = new Settings(dataFolder);
setStatic(); setStatic();
} }

View File

@ -25,6 +25,7 @@ public class OfflinePlayer implements Player
{ {
private final String name; private final String name;
private Location location = new Location(null, 0, 0, 0, 0, 0); private Location location = new Location(null, 0, 0, 0, 0, 0);
private World world = null;
public OfflinePlayer(String name) public OfflinePlayer(String name)
{ {
@ -132,7 +133,13 @@ public class OfflinePlayer implements Player
public World getWorld() public World getWorld()
{ {
return null; return world;
}
public void setLocation(Location loc)
{
location = loc;
world = loc.getWorld();
} }
public void teleportTo(Location lctn) public void teleportTo(Location lctn)

View File

@ -8,6 +8,7 @@ import java.io.InputStream;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.Calendar; import java.util.Calendar;
import java.util.Enumeration; import java.util.Enumeration;
@ -270,13 +271,13 @@ public class Util
} }
return isBlockAboveAir(world, x, y, z); return isBlockAboveAir(world, x, y, z);
} }
private static DecimalFormat df = new DecimalFormat("#0.##"); private static DecimalFormat df = new DecimalFormat("#0.00", DecimalFormatSymbols.getInstance(Locale.US));
public static String formatCurrency(double value) public static String formatCurrency(double value)
{ {
String str = Essentials.getStatic().getSettings().getCurrencySymbol()+df.format(value); String str = Essentials.getStatic().getSettings().getCurrencySymbol()+df.format(value);
if (str.endsWith(".")) { if (str.endsWith(".00")) {
return str.substring(0, str.length()-1); str = str.substring(0, str.length()-3);
} }
return str; return str;
} }

View File

@ -18,6 +18,10 @@ public class Economy
private static void createNPCFile(String name) private static void createNPCFile(String name)
{ {
File folder = new File(Essentials.getStatic().getDataFolder(), "userdata"); File folder = new File(Essentials.getStatic().getDataFolder(), "userdata");
if (!folder.exists())
{
folder.mkdirs();
}
EssentialsConf npcConfig = new EssentialsConf(new File(folder, Util.sanitizeFileName(name) + ".yml")); EssentialsConf npcConfig = new EssentialsConf(new File(folder, Util.sanitizeFileName(name) + ".yml"));
npcConfig.load(); npcConfig.load();
npcConfig.setProperty("npc", true); npcConfig.setProperty("npc", true);
@ -28,6 +32,10 @@ public class Economy
private static void deleteNPC(String name) private static void deleteNPC(String name)
{ {
File folder = new File(Essentials.getStatic().getDataFolder(), "userdata"); File folder = new File(Essentials.getStatic().getDataFolder(), "userdata");
if (!folder.exists())
{
folder.mkdirs();
}
File config = new File(folder, Util.sanitizeFileName(name) + ".yml"); File config = new File(folder, Util.sanitizeFileName(name) + ".yml");
EssentialsConf npcConfig = new EssentialsConf(config); EssentialsConf npcConfig = new EssentialsConf(config);
npcConfig.load(); npcConfig.load();
@ -235,13 +243,16 @@ public class Economy
/** /**
* Creates dummy files for a npc, if there is no player yet with that name. * Creates dummy files for a npc, if there is no player yet with that name.
* @param name Name of the player * @param name Name of the player
* @return true, if a new npc was created
*/ */
public static void createNPC(String name) public static boolean createNPC(String name)
{ {
User user = getUserByName(name); User user = getUserByName(name);
if (user == null) { if (user == null) {
createNPCFile(name); createNPCFile(name);
return true;
} }
return false;
} }
/** /**

View File

@ -155,6 +155,7 @@ restricted-commands:
- unbanip - unbanip
- togglejail - togglejail
- setjail - setjail
- eco.loan
# Note: All items MUST be followed by a quantity! # Note: All items MUST be followed by a quantity!
# Times are measured in seconds. # Times are measured in seconds.

View File

@ -0,0 +1,124 @@
package com.earth2me.essentials;
import com.earth2me.essentials.api.Economy;
import com.earth2me.essentials.api.NoLoanPermittedException;
import com.earth2me.essentials.api.UserDoesNotExistException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import junit.framework.TestCase;
import org.bukkit.plugin.InvalidDescriptionException;
public class EconomyTest extends TestCase
{
private OfflinePlayer base1;
private Essentials ess;
public EconomyTest(String testName)
{
super(testName);
ess = new Essentials();
FakeServer server = new FakeServer();
try
{
ess.setupForTesting(server);
}
catch (InvalidDescriptionException ex)
{
fail("InvalidDescriptionException");
}
catch (IOException ex)
{
fail("IOException");
}
base1 = new OfflinePlayer("TestPlayer1");
server.addPlayer(base1);
}
// only one big test, since we use static instances
public void testEconomy()
{
// test NPC
String npcName = "npc1";
assertFalse(Economy.playerExists(npcName));
assertTrue(Economy.createNPC(npcName));
assertTrue(Economy.playerExists(npcName));
assertNotNull(ess.getOfflineUser(npcName));
try
{
Economy.removeNPC(npcName);
}
catch (UserDoesNotExistException ex)
{
fail(ex.getMessage());
}
assertFalse(Economy.playerExists(npcName));
//test Math
try
{
String playerName = "TestPlayer1";
assertTrue(Economy.playerExists(playerName));
Economy.resetBalance(playerName);
assertEquals(0.0, Economy.getMoney(playerName));
Economy.add(playerName, 10.0);
assertEquals(10.0, Economy.getMoney(playerName));
Economy.subtract(playerName, 5.0);
assertEquals(5.0, Economy.getMoney(playerName));
Economy.multiply(playerName, 2.0);
assertEquals(10.0, Economy.getMoney(playerName));
Economy.divide(playerName, 2.0);
assertEquals(5.0, Economy.getMoney(playerName));
Economy.setMoney(playerName, 10.0);
assertEquals(10.0, Economy.getMoney(playerName));
}
catch (NoLoanPermittedException ex)
{
fail(ex.getMessage());
}
catch (UserDoesNotExistException ex)
{
fail(ex.getMessage());
}
//test Format
assertEquals("$10", Economy.format(10.0));
assertEquals("$10.10", Economy.format(10.10));
assertEquals("$10.10", Economy.format(10.102));
assertEquals("$10.11", Economy.format(10.109));
//test Exceptions
try
{
String playerName = "TestPlayer1";
assertTrue(Economy.playerExists(playerName));
Economy.resetBalance(playerName);
assertEquals(0.0, Economy.getMoney(playerName));
Economy.subtract(playerName, 5.0);
fail();
}
catch (NoLoanPermittedException ex)
{
}
catch (UserDoesNotExistException ex)
{
fail(ex.getMessage());
}
try
{
String playerName = "UnknownPlayer";
Economy.resetBalance(playerName);
fail();
}
catch (NoLoanPermittedException ex)
{
fail(ex.getMessage());
}
catch (UserDoesNotExistException ex)
{
}
}
}

View File

@ -0,0 +1,200 @@
package com.earth2me.essentials;
import com.avaje.ebean.config.ServerConfig;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.World.Environment;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Recipe;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.ServicesManager;
import org.bukkit.scheduler.BukkitScheduler;
public class FakeServer implements Server
{
private List<Player> players = new ArrayList<Player>();
private List<World> worlds = new ArrayList<World>();
public String getName()
{
return "Test Server";
}
public String getVersion()
{
return "1.0";
}
public Player[] getOnlinePlayers()
{
return players.toArray(new Player[0]);
}
public void setOnlinePlayers(List<Player> players)
{
this.players = players;
}
public int getMaxPlayers()
{
return 100;
}
public int getPort()
{
return 25565;
}
public String getIp()
{
return "127.0.0.1";
}
public String getServerName()
{
return "Test Server";
}
public String getServerId()
{
return "Test Server";
}
public int broadcastMessage(String string)
{
int i = 0;
for (Player player : players)
{
player.sendMessage(string);
i++;
}
return i;
}
public String getUpdateFolder()
{
return "update";
}
public Player getPlayer(String string)
{
for (Player player : players)
{
if (player.getName().equalsIgnoreCase(string))
{
return player;
}
}
return null;
}
public List<Player> matchPlayer(String string)
{
List<Player> matches = new ArrayList<Player>();
for (Player player : players)
{
if (player.getName().substring(0, Math.min(player.getName().length(), string.length())).equalsIgnoreCase(string))
{
matches.add(player);
}
}
return matches;
}
public PluginManager getPluginManager()
{
throw new UnsupportedOperationException("Not supported yet.");
}
public BukkitScheduler getScheduler()
{
throw new UnsupportedOperationException("Not supported yet.");
}
public ServicesManager getServicesManager()
{
throw new UnsupportedOperationException("Not supported yet.");
}
public List<World> getWorlds()
{
return worlds;
}
public World createWorld(String string, Environment e)
{
World w = new FakeWorld(string, e);
worlds.add(w);
return w;
}
public World createWorld(String string, Environment e, long l)
{
World w = new FakeWorld(string, e);
worlds.add(w);
return w;
}
public World getWorld(String string)
{
for (World world : worlds)
{
if (world.getName().equalsIgnoreCase(string)) {
return world;
}
}
return null;
}
public void reload()
{
}
public Logger getLogger()
{
return Logger.getLogger("Minecraft");
}
public PluginCommand getPluginCommand(String string)
{
throw new UnsupportedOperationException("Not supported yet.");
}
public void savePlayers()
{
}
public boolean dispatchCommand(CommandSender cs, String string)
{
throw new UnsupportedOperationException("Not supported yet.");
}
public void configureDbConfig(ServerConfig sc)
{
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean addRecipe(Recipe recipe)
{
throw new UnsupportedOperationException("Not supported yet.");
}
void addPlayer(Player base1)
{
players.add(base1);
}
OfflinePlayer createPlayer(String name)
{
OfflinePlayer player = new OfflinePlayer(name);
player.setLocation(new Location(worlds.get(0), 0, 0, 0, 0, 0));
return player;
}
}

View File

@ -0,0 +1,326 @@
package com.earth2me.essentials;
import java.util.List;
import org.bukkit.BlockChangeDelegate;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.TreeType;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Arrow;
import org.bukkit.entity.Boat;
import org.bukkit.entity.CreatureType;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Item;
import org.bukkit.entity.LightningStrike;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Minecart;
import org.bukkit.entity.Player;
import org.bukkit.entity.PoweredMinecart;
import org.bukkit.entity.StorageMinecart;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.Vector;
public class FakeWorld implements World
{
private String name;
private Environment env;
FakeWorld(String string, Environment environment)
{
this.name = string;
this.env = environment;
}
public Block getBlockAt(int i, int i1, int i2)
{
throw new UnsupportedOperationException("Not supported yet.");
}
public Block getBlockAt(Location lctn)
{
throw new UnsupportedOperationException("Not supported yet.");
}
public int getBlockTypeIdAt(int i, int i1, int i2)
{
throw new UnsupportedOperationException("Not supported yet.");
}
public int getBlockTypeIdAt(Location lctn)
{
throw new UnsupportedOperationException("Not supported yet.");
}
public int getHighestBlockYAt(int i, int i1)
{
throw new UnsupportedOperationException("Not supported yet.");
}
public int getHighestBlockYAt(Location lctn)
{
throw new UnsupportedOperationException("Not supported yet.");
}
public Chunk getChunkAt(int i, int i1)
{
throw new UnsupportedOperationException("Not supported yet.");
}
public Chunk getChunkAt(Location lctn)
{
throw new UnsupportedOperationException("Not supported yet.");
}
public Chunk getChunkAt(Block block)
{
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean isChunkLoaded(Chunk chunk)
{
throw new UnsupportedOperationException("Not supported yet.");
}
public Chunk[] getLoadedChunks()
{
throw new UnsupportedOperationException("Not supported yet.");
}
public void loadChunk(Chunk chunk)
{
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean isChunkLoaded(int i, int i1)
{
throw new UnsupportedOperationException("Not supported yet.");
}
public void loadChunk(int i, int i1)
{
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean loadChunk(int i, int i1, boolean bln)
{
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean unloadChunk(int i, int i1)
{
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean unloadChunk(int i, int i1, boolean bln)
{
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean unloadChunk(int i, int i1, boolean bln, boolean bln1)
{
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean unloadChunkRequest(int i, int i1)
{
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean unloadChunkRequest(int i, int i1, boolean bln)
{
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean regenerateChunk(int i, int i1)
{
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean refreshChunk(int i, int i1)
{
throw new UnsupportedOperationException("Not supported yet.");
}
public Item dropItem(Location lctn, ItemStack is)
{
throw new UnsupportedOperationException("Not supported yet.");
}
public Item dropItemNaturally(Location lctn, ItemStack is)
{
throw new UnsupportedOperationException("Not supported yet.");
}
public Arrow spawnArrow(Location lctn, Vector vector, float f, float f1)
{
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean generateTree(Location lctn, TreeType tt)
{
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean generateTree(Location lctn, TreeType tt, BlockChangeDelegate bcd)
{
throw new UnsupportedOperationException("Not supported yet.");
}
public Minecart spawnMinecart(Location lctn)
{
throw new UnsupportedOperationException("Not supported yet.");
}
public StorageMinecart spawnStorageMinecart(Location lctn)
{
throw new UnsupportedOperationException("Not supported yet.");
}
public PoweredMinecart spawnPoweredMinecart(Location lctn)
{
throw new UnsupportedOperationException("Not supported yet.");
}
public Boat spawnBoat(Location lctn)
{
throw new UnsupportedOperationException("Not supported yet.");
}
public LivingEntity spawnCreature(Location lctn, CreatureType ct)
{
throw new UnsupportedOperationException("Not supported yet.");
}
public LightningStrike strikeLightning(Location lctn)
{
throw new UnsupportedOperationException("Not supported yet.");
}
public LightningStrike strikeLightningEffect(Location lctn)
{
throw new UnsupportedOperationException("Not supported yet.");
}
public List<Entity> getEntities()
{
throw new UnsupportedOperationException("Not supported yet.");
}
public List<LivingEntity> getLivingEntities()
{
throw new UnsupportedOperationException("Not supported yet.");
}
public List<Player> getPlayers()
{
throw new UnsupportedOperationException("Not supported yet.");
}
public String getName()
{
return name;
}
public long getId()
{
throw new UnsupportedOperationException("Not supported yet.");
}
public Location getSpawnLocation()
{
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean setSpawnLocation(int i, int i1, int i2)
{
throw new UnsupportedOperationException("Not supported yet.");
}
public long getTime()
{
throw new UnsupportedOperationException("Not supported yet.");
}
public void setTime(long l)
{
throw new UnsupportedOperationException("Not supported yet.");
}
public long getFullTime()
{
throw new UnsupportedOperationException("Not supported yet.");
}
public void setFullTime(long l)
{
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean hasStorm()
{
throw new UnsupportedOperationException("Not supported yet.");
}
public void setStorm(boolean bln)
{
throw new UnsupportedOperationException("Not supported yet.");
}
public int getWeatherDuration()
{
throw new UnsupportedOperationException("Not supported yet.");
}
public void setWeatherDuration(int i)
{
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean isThundering()
{
throw new UnsupportedOperationException("Not supported yet.");
}
public void setThundering(boolean bln)
{
throw new UnsupportedOperationException("Not supported yet.");
}
public int getThunderDuration()
{
throw new UnsupportedOperationException("Not supported yet.");
}
public void setThunderDuration(int i)
{
throw new UnsupportedOperationException("Not supported yet.");
}
public Environment getEnvironment()
{
return env;
}
public long getSeed()
{
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean getPVP()
{
throw new UnsupportedOperationException("Not supported yet.");
}
public void setPVP(boolean bln)
{
throw new UnsupportedOperationException("Not supported yet.");
}
public void save()
{
throw new UnsupportedOperationException("Not supported yet.");
}
}

View File

@ -2,6 +2,9 @@ package com.earth2me.essentials;
import java.io.IOException; import java.io.IOException;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.World.Environment;
import org.bukkit.plugin.InvalidDescriptionException; import org.bukkit.plugin.InvalidDescriptionException;
@ -9,14 +12,17 @@ public class UserTest extends TestCase
{ {
private OfflinePlayer base1; private OfflinePlayer base1;
private Essentials ess; private Essentials ess;
private FakeServer server;
public UserTest(String testName) public UserTest(String testName)
{ {
super(testName); super(testName);
ess = new Essentials(); ess = new Essentials();
server = new FakeServer();
server.createWorld("testWorld", Environment.NORMAL);
try try
{ {
ess.setupForTesting(); ess.setupForTesting(server);
} }
catch (InvalidDescriptionException ex) catch (InvalidDescriptionException ex)
{ {
@ -26,7 +32,8 @@ public class UserTest extends TestCase
{ {
fail("IOException"); fail("IOException");
} }
base1 = new OfflinePlayer("TestPlayer1"); base1 = server.createPlayer("testPlayer1");
server.addPlayer(base1);
} }
private void should(String what) private void should(String what)
@ -34,36 +41,28 @@ public class UserTest extends TestCase
System.out.println(getName() + " should " + what); System.out.println(getName() + " should " + what);
} }
@Override
protected void setUp() throws Exception
{
super.setUp();
}
@Override
protected void tearDown() throws Exception
{
super.tearDown();
}
public void testUpdate() public void testUpdate()
{ {
should("update an existing player with the same name, rather than creating a new player"); OfflinePlayer base1alt = server.createPlayer(base1.getName());
ess.getUser(base1);
//int size1 = User.size();
OfflinePlayer base1alt = new OfflinePlayer(base1.getName());
assertEquals(base1alt, ess.getUser(base1alt).getBase()); assertEquals(base1alt, ess.getUser(base1alt).getBase());
//assertTrue(size1 == User.size());
} }
/*public void testHome() throws Exception public void testHome()
{ {
should("return the home set by setHome"); User user = ess.getUser(base1);
Location home = new Location(null, 1, 2, 3, 4, 5); Location loc = base1.getLocation();
User user = User.get(base1); user.setHome();
user.setHome(home); OfflinePlayer base2 = server.createPlayer(base1.getName());
assertEquals(user.getHome(), home); User user2 = ess.getUser(base2);
}*/ Location home = user2.getHome();
assertEquals(loc.getWorld().getName(), home.getWorld().getName());
assertEquals(loc.getX(), home.getX());
assertEquals(loc.getY(), home.getY());
assertEquals(loc.getZ(), home.getZ());
assertEquals(loc.getYaw(), home.getYaw());
assertEquals(loc.getPitch(), home.getPitch());
}
public void testMoney() public void testMoney()
{ {
should("properly set, take, give, and get money"); should("properly set, take, give, and get money");