git-svn-id: https://svn.java.net/svn/essentials~svn/trunk@1593 e251c2fe-e539-e718-e476-b85c1f46cddb
This commit is contained in:
snowleo 2011-06-06 12:06:41 +00:00
parent 6c862e72b5
commit b7791caa75

View File

@ -4,22 +4,22 @@ import com.earth2me.essentials.api.Economy;
import com.earth2me.essentials.api.NoLoanPermittedException; import com.earth2me.essentials.api.NoLoanPermittedException;
import com.earth2me.essentials.api.UserDoesNotExistException; import com.earth2me.essentials.api.UserDoesNotExistException;
import java.io.IOException; import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.bukkit.plugin.InvalidDescriptionException; import org.bukkit.plugin.InvalidDescriptionException;
import org.junit.Test;
public class EconomyTest extends TestCase public class EconomyTest extends TestCase
{ {
private final OfflinePlayer base1; private final transient Essentials ess;
private final Essentials ess; private final static String NPCNAME = "npc1";
private final static String PLAYERNAME = "TestPlayer1";
public EconomyTest(String testName) public EconomyTest(final String testName)
{ {
super(testName); super(testName);
ess = new Essentials(); ess = new Essentials();
FakeServer server = new FakeServer(); final FakeServer server = new FakeServer();
try try
{ {
ess.setupForTesting(server); ess.setupForTesting(server);
@ -32,46 +32,45 @@ public class EconomyTest extends TestCase
{ {
fail("IOException"); fail("IOException");
} }
base1 = new OfflinePlayer("TestPlayer1"); server.addPlayer(new OfflinePlayer(PLAYERNAME));
server.addPlayer(base1);
} }
// only one big test, since we use static instances // only one big test, since we use static instances
@Test
public void testEconomy() public void testEconomy()
{ {
// test NPC // test NPC
String npcName = "npc1"; assertFalse("NPC does not exists", Economy.playerExists(NPCNAME));
assertFalse(Economy.playerExists(npcName)); assertTrue("Create NPC", Economy.createNPC(NPCNAME));
assertTrue(Economy.createNPC(npcName)); assertTrue("NPC exists", Economy.playerExists(NPCNAME));
assertTrue(Economy.playerExists(npcName)); assertNotNull("NPC can be accessed", ess.getOfflineUser(NPCNAME));
assertNotNull(ess.getOfflineUser(npcName));
try try
{ {
Economy.removeNPC(npcName); Economy.removeNPC(NPCNAME);
} }
catch (UserDoesNotExistException ex) catch (UserDoesNotExistException ex)
{ {
fail(ex.getMessage()); fail(ex.getMessage());
} }
assertFalse(Economy.playerExists(npcName)); assertFalse("NPC can be removed", Economy.playerExists(NPCNAME));
//test Math //test Math
try try
{ {
String playerName = "TestPlayer1";
assertTrue(Economy.playerExists(playerName)); assertTrue("Player exists", Economy.playerExists(PLAYERNAME));
Economy.resetBalance(playerName); Economy.resetBalance(PLAYERNAME);
assertEquals(0.0, Economy.getMoney(playerName)); assertEquals("Player has no money", 0.0, Economy.getMoney(PLAYERNAME));
Economy.add(playerName, 10.0); Economy.add(PLAYERNAME, 10.0);
assertEquals(10.0, Economy.getMoney(playerName)); assertEquals("Add money", 10.0, Economy.getMoney(PLAYERNAME));
Economy.subtract(playerName, 5.0); Economy.subtract(PLAYERNAME, 5.0);
assertEquals(5.0, Economy.getMoney(playerName)); assertEquals("Subtract money", 5.0, Economy.getMoney(PLAYERNAME));
Economy.multiply(playerName, 2.0); Economy.multiply(PLAYERNAME, 2.0);
assertEquals(10.0, Economy.getMoney(playerName)); assertEquals("Multiply money", 10.0, Economy.getMoney(PLAYERNAME));
Economy.divide(playerName, 2.0); Economy.divide(PLAYERNAME, 2.0);
assertEquals(5.0, Economy.getMoney(playerName)); assertEquals("Divide money", 5.0, Economy.getMoney(PLAYERNAME));
Economy.setMoney(playerName, 10.0); Economy.setMoney(PLAYERNAME, 10.0);
assertEquals(10.0, Economy.getMoney(playerName)); assertEquals("Set money", 10.0, Economy.getMoney(PLAYERNAME));
} }
catch (NoLoanPermittedException ex) catch (NoLoanPermittedException ex)
{ {
@ -81,24 +80,23 @@ public class EconomyTest extends TestCase
{ {
fail(ex.getMessage()); fail(ex.getMessage());
} }
//test Format //test Format
assertEquals("$1000", Economy.format(1000.0)); assertEquals("Format $1000", "$1000", Economy.format(1000.0));
assertEquals("$10", Economy.format(10.0)); assertEquals("Format $10", "$10", Economy.format(10.0));
assertEquals("$10.10", Economy.format(10.10)); assertEquals("Format $10.10", "$10.10", Economy.format(10.10));
assertEquals("$10.10", Economy.format(10.102)); assertEquals("Format $10.10", "$10.10", Economy.format(10.102));
assertEquals("$10.11", Economy.format(10.109)); assertEquals("Format $10.11", "$10.11", Economy.format(10.109));
//test Exceptions //test Exceptions
try try
{ {
String playerName = "TestPlayer1"; assertTrue("Player exists", Economy.playerExists(PLAYERNAME));
assertTrue(Economy.playerExists(playerName)); Economy.resetBalance(PLAYERNAME);
Economy.resetBalance(playerName); assertEquals("Reset balance", 0.0, Economy.getMoney(PLAYERNAME));
assertEquals(0.0, Economy.getMoney(playerName)); Economy.subtract(PLAYERNAME, 5.0);
Economy.subtract(playerName, 5.0); fail("Did not throw exception");
fail();
} }
catch (NoLoanPermittedException ex) catch (NoLoanPermittedException ex)
{ {
@ -107,12 +105,11 @@ public class EconomyTest extends TestCase
{ {
fail(ex.getMessage()); fail(ex.getMessage());
} }
try try
{ {
String playerName = "UnknownPlayer"; Economy.resetBalance("UnknownPlayer");
Economy.resetBalance(playerName); fail("Did not throw exception");
fail();
} }
catch (NoLoanPermittedException ex) catch (NoLoanPermittedException ex)
{ {