2011-05-13 19:18:22 +00:00
|
|
|
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 junit.framework.TestCase;
|
2011-07-16 00:45:12 +00:00
|
|
|
import org.bukkit.World.Environment;
|
2011-05-13 19:18:22 +00:00
|
|
|
import org.bukkit.plugin.InvalidDescriptionException;
|
2011-06-06 12:06:41 +00:00
|
|
|
import org.junit.Test;
|
2011-05-13 19:18:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
public class EconomyTest extends TestCase
|
|
|
|
{
|
2011-06-06 12:06:41 +00:00
|
|
|
private final transient Essentials ess;
|
|
|
|
private final static String NPCNAME = "npc1";
|
|
|
|
private final static String PLAYERNAME = "TestPlayer1";
|
2011-05-13 19:18:22 +00:00
|
|
|
|
2011-06-06 12:06:41 +00:00
|
|
|
public EconomyTest(final String testName)
|
2011-05-13 19:18:22 +00:00
|
|
|
{
|
|
|
|
super(testName);
|
|
|
|
ess = new Essentials();
|
2011-06-06 12:06:41 +00:00
|
|
|
final FakeServer server = new FakeServer();
|
2011-07-16 00:45:12 +00:00
|
|
|
server.createWorld("testWorld", Environment.NORMAL);
|
2011-05-13 19:18:22 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
ess.setupForTesting(server);
|
|
|
|
}
|
|
|
|
catch (InvalidDescriptionException ex)
|
|
|
|
{
|
|
|
|
fail("InvalidDescriptionException");
|
|
|
|
}
|
|
|
|
catch (IOException ex)
|
|
|
|
{
|
|
|
|
fail("IOException");
|
|
|
|
}
|
2011-07-15 23:33:22 +00:00
|
|
|
server.addPlayer(new OfflinePlayer(PLAYERNAME, ess));
|
2011-05-13 19:18:22 +00:00
|
|
|
}
|
2011-06-06 12:06:41 +00:00
|
|
|
|
2011-05-13 19:18:22 +00:00
|
|
|
// only one big test, since we use static instances
|
2011-06-06 12:06:41 +00:00
|
|
|
@Test
|
2011-05-13 19:18:22 +00:00
|
|
|
public void testEconomy()
|
|
|
|
{
|
|
|
|
// test NPC
|
2011-06-06 12:06:41 +00:00
|
|
|
assertFalse("NPC does not exists", Economy.playerExists(NPCNAME));
|
|
|
|
assertTrue("Create NPC", Economy.createNPC(NPCNAME));
|
|
|
|
assertTrue("NPC exists", Economy.playerExists(NPCNAME));
|
|
|
|
assertNotNull("NPC can be accessed", ess.getOfflineUser(NPCNAME));
|
2011-05-13 19:18:22 +00:00
|
|
|
try
|
|
|
|
{
|
2011-06-06 12:06:41 +00:00
|
|
|
Economy.removeNPC(NPCNAME);
|
2011-05-13 19:18:22 +00:00
|
|
|
}
|
|
|
|
catch (UserDoesNotExistException ex)
|
|
|
|
{
|
|
|
|
fail(ex.getMessage());
|
|
|
|
}
|
2011-06-06 12:06:41 +00:00
|
|
|
assertFalse("NPC can be removed", Economy.playerExists(NPCNAME));
|
|
|
|
|
2011-05-13 19:18:22 +00:00
|
|
|
//test Math
|
|
|
|
try
|
|
|
|
{
|
2011-06-06 12:06:41 +00:00
|
|
|
|
|
|
|
assertTrue("Player exists", Economy.playerExists(PLAYERNAME));
|
|
|
|
Economy.resetBalance(PLAYERNAME);
|
|
|
|
assertEquals("Player has no money", 0.0, Economy.getMoney(PLAYERNAME));
|
|
|
|
Economy.add(PLAYERNAME, 10.0);
|
|
|
|
assertEquals("Add money", 10.0, Economy.getMoney(PLAYERNAME));
|
|
|
|
Economy.subtract(PLAYERNAME, 5.0);
|
|
|
|
assertEquals("Subtract money", 5.0, Economy.getMoney(PLAYERNAME));
|
|
|
|
Economy.multiply(PLAYERNAME, 2.0);
|
|
|
|
assertEquals("Multiply money", 10.0, Economy.getMoney(PLAYERNAME));
|
|
|
|
Economy.divide(PLAYERNAME, 2.0);
|
|
|
|
assertEquals("Divide money", 5.0, Economy.getMoney(PLAYERNAME));
|
|
|
|
Economy.setMoney(PLAYERNAME, 10.0);
|
|
|
|
assertEquals("Set money", 10.0, Economy.getMoney(PLAYERNAME));
|
2011-05-13 19:18:22 +00:00
|
|
|
}
|
|
|
|
catch (NoLoanPermittedException ex)
|
|
|
|
{
|
|
|
|
fail(ex.getMessage());
|
|
|
|
}
|
|
|
|
catch (UserDoesNotExistException ex)
|
|
|
|
{
|
|
|
|
fail(ex.getMessage());
|
|
|
|
}
|
2011-06-06 12:06:41 +00:00
|
|
|
|
2011-05-13 19:18:22 +00:00
|
|
|
//test Format
|
2011-06-06 12:06:41 +00:00
|
|
|
assertEquals("Format $1000", "$1000", Economy.format(1000.0));
|
|
|
|
assertEquals("Format $10", "$10", Economy.format(10.0));
|
|
|
|
assertEquals("Format $10.10", "$10.10", Economy.format(10.10));
|
|
|
|
assertEquals("Format $10.10", "$10.10", Economy.format(10.102));
|
|
|
|
assertEquals("Format $10.11", "$10.11", Economy.format(10.109));
|
|
|
|
|
|
|
|
|
2011-05-13 19:18:22 +00:00
|
|
|
//test Exceptions
|
|
|
|
try
|
|
|
|
{
|
2011-06-06 12:06:41 +00:00
|
|
|
assertTrue("Player exists", Economy.playerExists(PLAYERNAME));
|
|
|
|
Economy.resetBalance(PLAYERNAME);
|
|
|
|
assertEquals("Reset balance", 0.0, Economy.getMoney(PLAYERNAME));
|
|
|
|
Economy.subtract(PLAYERNAME, 5.0);
|
|
|
|
fail("Did not throw exception");
|
2011-05-13 19:18:22 +00:00
|
|
|
}
|
|
|
|
catch (NoLoanPermittedException ex)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
catch (UserDoesNotExistException ex)
|
|
|
|
{
|
|
|
|
fail(ex.getMessage());
|
|
|
|
}
|
2011-06-06 12:06:41 +00:00
|
|
|
|
2011-05-13 19:18:22 +00:00
|
|
|
try
|
|
|
|
{
|
2011-06-06 12:06:41 +00:00
|
|
|
Economy.resetBalance("UnknownPlayer");
|
|
|
|
fail("Did not throw exception");
|
2011-05-13 19:18:22 +00:00
|
|
|
}
|
|
|
|
catch (NoLoanPermittedException ex)
|
|
|
|
{
|
|
|
|
fail(ex.getMessage());
|
|
|
|
}
|
|
|
|
catch (UserDoesNotExistException ex)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|