2011-05-13 19:18:22 +00:00
|
|
|
package com.earth2me.essentials;
|
|
|
|
|
2013-07-25 23:07:56 +00:00
|
|
|
import com.earth2me.essentials.api.NoLoanPermittedException;
|
|
|
|
import com.earth2me.essentials.api.UserDoesNotExistException;
|
2016-11-21 18:05:17 +00:00
|
|
|
import com.earth2me.essentials.commands.IEssentialsCommand;
|
|
|
|
import com.earth2me.essentials.commands.NoChargeException;
|
|
|
|
|
2011-05-13 19:18:22 +00:00
|
|
|
import junit.framework.TestCase;
|
2013-10-11 02:44:41 +00:00
|
|
|
import net.ess3.api.Economy;
|
2011-07-16 00:45:12 +00:00
|
|
|
import org.bukkit.World.Environment;
|
2016-11-21 18:05:17 +00:00
|
|
|
import org.bukkit.command.CommandSender;
|
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
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
|
2011-05-13 19:18:22 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
public class EconomyTest extends TestCase {
|
|
|
|
private final transient Essentials ess;
|
|
|
|
private static final String NPCNAME = "npc1";
|
|
|
|
private static final String PLAYERNAME = "testPlayer1";
|
2016-11-21 18:05:17 +00:00
|
|
|
private static final String PLAYERNAME2 = "testPlayer2";
|
|
|
|
private final FakeServer server;
|
2011-05-13 19:18:22 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
public EconomyTest(final String testName) {
|
|
|
|
super(testName);
|
2016-11-21 18:05:17 +00:00
|
|
|
this.server = new FakeServer();
|
2015-04-15 04:06:16 +00:00
|
|
|
server.createWorld("testWorld", Environment.NORMAL);
|
|
|
|
ess = new Essentials(server);
|
|
|
|
try {
|
|
|
|
ess.setupForTesting(server);
|
|
|
|
} catch (InvalidDescriptionException ex) {
|
|
|
|
fail("InvalidDescriptionException");
|
|
|
|
} catch (IOException ex) {
|
|
|
|
fail("IOException");
|
|
|
|
}
|
|
|
|
server.addPlayer(new OfflinePlayer(PLAYERNAME, ess.getServer()));
|
2016-11-21 18:05:17 +00:00
|
|
|
server.addPlayer(new OfflinePlayer(PLAYERNAME2, ess.getServer()));
|
2015-04-15 04:06:16 +00:00
|
|
|
}
|
2011-06-06 12:06:41 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
// only one big test, since we use static instances
|
|
|
|
@Test
|
|
|
|
public void testEconomy() {
|
|
|
|
// test NPC
|
|
|
|
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));
|
|
|
|
try {
|
|
|
|
Economy.removeNPC(NPCNAME);
|
|
|
|
} catch (UserDoesNotExistException ex) {
|
|
|
|
fail(ex.getMessage());
|
|
|
|
}
|
|
|
|
assertFalse("NPC can be removed", Economy.playerExists(NPCNAME));
|
2011-06-06 12:06:41 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
//test Math
|
|
|
|
try {
|
2011-06-06 12:06:41 +00:00
|
|
|
|
2015-04-15 04:06:16 +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));
|
|
|
|
} catch (NoLoanPermittedException ex) {
|
|
|
|
fail(ex.getMessage());
|
|
|
|
} catch (UserDoesNotExistException ex) {
|
|
|
|
fail(ex.getMessage());
|
|
|
|
}
|
2011-06-06 12:06:41 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
//test Format
|
2016-01-13 22:54:00 +00:00
|
|
|
assertEquals("Format $1,000", "$1,000", Economy.format(1000.0));
|
2015-04-15 04:06:16 +00:00
|
|
|
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.1000001));
|
|
|
|
assertEquals("Format $10.10", "$10.10", Economy.format(10.1099999));
|
2011-06-06 12:06:41 +00:00
|
|
|
|
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
//test Exceptions
|
|
|
|
try {
|
|
|
|
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");
|
|
|
|
} catch (NoLoanPermittedException ex) {
|
|
|
|
} catch (UserDoesNotExistException ex) {
|
|
|
|
fail(ex.getMessage());
|
|
|
|
}
|
2011-06-06 12:06:41 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
try {
|
|
|
|
Economy.resetBalance("UnknownPlayer");
|
|
|
|
fail("Did not throw exception");
|
|
|
|
} catch (NoLoanPermittedException ex) {
|
|
|
|
fail(ex.getMessage());
|
|
|
|
} catch (UserDoesNotExistException ex) {
|
|
|
|
}
|
|
|
|
}
|
2016-11-21 18:05:17 +00:00
|
|
|
|
|
|
|
private void runCommand(String command, User user, String args) throws Exception {
|
|
|
|
runCommand(command, user, args.split("\\s+"));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void runCommand(String command, User user, String[] args) throws Exception {
|
|
|
|
IEssentialsCommand cmd;
|
|
|
|
|
|
|
|
try {
|
|
|
|
cmd = (IEssentialsCommand) Essentials.class.getClassLoader()
|
|
|
|
.loadClass("com.earth2me.essentials.commands.Command" + command).newInstance();
|
|
|
|
cmd.setEssentials(ess);
|
|
|
|
cmd.run(server, user, command, null, args);
|
|
|
|
} catch (NoChargeException ex) {
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void runConsoleCommand(String command, String args) throws Exception {
|
|
|
|
runConsoleCommand(command, args.split("\\s+"));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void runConsoleCommand(String command, String[] args) throws Exception {
|
|
|
|
IEssentialsCommand cmd;
|
|
|
|
|
|
|
|
CommandSender sender = server.getConsoleSender();
|
|
|
|
|
|
|
|
try {
|
|
|
|
cmd = (IEssentialsCommand) Essentials.class.getClassLoader()
|
|
|
|
.loadClass("com.earth2me.essentials.commands.Command" + command).newInstance();
|
|
|
|
cmd.setEssentials(ess);
|
|
|
|
cmd.run(server, new CommandSource(sender), command, null, args);
|
|
|
|
} catch (NoChargeException ex) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testNegativePayCommand() throws Exception {
|
|
|
|
User user1 = ess.getUser(PLAYERNAME);
|
|
|
|
try {
|
|
|
|
runCommand("pay", user1, PLAYERNAME2 + " -123");
|
|
|
|
} catch (Exception e) {
|
|
|
|
assertEquals(I18n.tl("payMustBePositive"), e.getMessage());
|
|
|
|
}
|
|
|
|
}
|
2011-05-13 19:18:22 +00:00
|
|
|
}
|