2016-07-29 13:16:11 +02:00
|
|
|
package org.appledash.saneeconomy.test;
|
|
|
|
|
|
|
|
import org.appledash.saneeconomy.economy.Currency;
|
|
|
|
import org.appledash.saneeconomy.economy.EconomyManager;
|
|
|
|
import org.appledash.saneeconomy.economy.economable.Economable;
|
2016-09-21 10:16:36 +02:00
|
|
|
import org.appledash.saneeconomy.economy.transaction.Transaction;
|
|
|
|
import org.appledash.saneeconomy.economy.transaction.TransactionReason;
|
|
|
|
import org.appledash.saneeconomy.economy.transaction.TransactionResult;
|
2016-07-29 13:16:11 +02:00
|
|
|
import org.appledash.saneeconomy.test.mock.MockEconomyStorageBackend;
|
|
|
|
import org.appledash.saneeconomy.test.mock.MockOfflinePlayer;
|
2016-09-18 10:37:44 +02:00
|
|
|
import org.appledash.saneeconomy.test.mock.MockSaneEconomy;
|
2016-07-29 13:16:11 +02:00
|
|
|
import org.junit.Assert;
|
2016-09-18 10:37:44 +02:00
|
|
|
import org.junit.Before;
|
2016-07-29 13:16:11 +02:00
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
import java.text.DecimalFormat;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by AppleDash on 7/29/2016.
|
|
|
|
* Blackjack is still best pony.
|
|
|
|
*/
|
|
|
|
public class EconomyManagerTest {
|
2016-09-18 10:37:44 +02:00
|
|
|
private EconomyManager economyManager;
|
|
|
|
|
|
|
|
@Before
|
|
|
|
public void setupEconomyManager() {
|
|
|
|
economyManager = new EconomyManager(new MockSaneEconomy(),
|
|
|
|
new Currency("test dollar", "test dollars", new DecimalFormat("0.00")),
|
2017-02-20 22:17:25 +01:00
|
|
|
new MockEconomyStorageBackend(), null);
|
2016-09-18 10:37:44 +02:00
|
|
|
}
|
|
|
|
|
2016-07-29 13:16:11 +02:00
|
|
|
@Test
|
|
|
|
public void testEconomyManager() {
|
|
|
|
Economable playerOne = Economable.wrap(new MockOfflinePlayer("One"));
|
|
|
|
Economable playerTwo = Economable.wrap(new MockOfflinePlayer("Two"));
|
|
|
|
|
|
|
|
// Accounts should not exist
|
|
|
|
Assert.assertFalse(economyManager.accountExists(playerOne));
|
|
|
|
Assert.assertFalse(economyManager.accountExists(playerTwo));
|
2016-10-02 17:33:30 +02:00
|
|
|
Assert.assertEquals(0.0D, economyManager.getBalance(playerOne), 0.0);
|
|
|
|
Assert.assertEquals(0.0D, economyManager.getBalance(playerTwo), 0.0);
|
2016-07-29 13:16:11 +02:00
|
|
|
|
2016-09-21 10:16:36 +02:00
|
|
|
economyManager.setBalance(playerOne, 100.0D);
|
2016-07-29 13:16:11 +02:00
|
|
|
|
|
|
|
// Now one should have an account, but two should not
|
|
|
|
Assert.assertTrue(economyManager.accountExists(playerOne));
|
|
|
|
Assert.assertFalse(economyManager.accountExists(playerTwo));
|
|
|
|
|
|
|
|
// One should have balance, two should not
|
2016-10-02 17:33:30 +02:00
|
|
|
Assert.assertEquals(100.0, economyManager.getBalance(playerOne), 0.0);
|
|
|
|
Assert.assertEquals(0.0, economyManager.getBalance(playerTwo), 0.0);
|
2016-07-29 13:16:11 +02:00
|
|
|
|
|
|
|
// One should be able to transfer to two
|
2016-09-21 10:16:36 +02:00
|
|
|
Assert.assertTrue(economyManager.transact(new Transaction(playerOne, playerTwo, 50.0, TransactionReason.PLAYER_PAY)).getStatus() == TransactionResult.Status.SUCCESS);
|
2016-07-29 13:16:11 +02:00
|
|
|
|
|
|
|
// One should now have only 50 left, two should have 50 now
|
2016-10-02 17:33:30 +02:00
|
|
|
Assert.assertEquals(50.0, economyManager.getBalance(playerOne), 0.0);
|
|
|
|
Assert.assertEquals(50.0, economyManager.getBalance(playerTwo), 0.0);
|
2016-07-29 13:16:11 +02:00
|
|
|
|
|
|
|
// Ensure that balance addition and subtraction works...
|
2016-10-02 17:33:30 +02:00
|
|
|
Assert.assertEquals(25.0, economyManager.transact(
|
|
|
|
new Transaction(playerOne, Economable.CONSOLE, 25.0, TransactionReason.TEST_TAKE)
|
|
|
|
).getFromBalance(), 0.0);
|
2016-09-21 10:16:36 +02:00
|
|
|
|
2016-10-02 17:33:30 +02:00
|
|
|
Assert.assertEquals(50.0, economyManager.transact(
|
|
|
|
new Transaction(Economable.CONSOLE, playerOne, 25.0, TransactionReason.TEST_GIVE)
|
|
|
|
).getToBalance(), 0.0);
|
2016-09-21 10:16:36 +02:00
|
|
|
|
2016-10-02 17:33:30 +02:00
|
|
|
Assert.assertEquals(TransactionResult.Status.ERR_NOT_ENOUGH_FUNDS, economyManager.transact(
|
|
|
|
new Transaction(playerTwo, Economable.CONSOLE, Double.MAX_VALUE, TransactionReason.TEST_TAKE)
|
|
|
|
).getStatus());
|
2016-07-29 13:16:11 +02:00
|
|
|
|
|
|
|
// Ensure that hasBalance works
|
|
|
|
Assert.assertTrue(economyManager.hasBalance(playerOne, 50.0));
|
|
|
|
Assert.assertFalse(economyManager.hasBalance(playerOne, 51.0));
|
2016-07-29 13:25:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = IllegalArgumentException.class)
|
|
|
|
public void testNegativeBalance() {
|
|
|
|
Economable economable = Economable.wrap(new MockOfflinePlayer("Bob"));
|
2016-09-21 10:16:36 +02:00
|
|
|
economyManager.setBalance(economable, -1.0);
|
2016-07-29 13:16:11 +02:00
|
|
|
}
|
|
|
|
}
|