mirror of
https://github.com/AppleDash/SaneEconomy.git
synced 2024-11-22 10:05:16 +01:00
Begin adding unit tests for SaneEconomyCore - Don't want more bugs to creep in!
This commit is contained in:
parent
c2bd61f286
commit
0acead2ea3
@ -11,6 +11,15 @@
|
||||
<artifactId>SaneEconomyCore</artifactId>
|
||||
<version>0.6.2-SNAPSHOT</version>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>${project.artifactId}-${project.version}</finalName>
|
||||
<resources>
|
||||
|
@ -61,17 +61,16 @@ public class EconomyAdminCommand extends SaneEconomyCommand {
|
||||
return;
|
||||
}
|
||||
|
||||
EconomyManager ecoMan = SaneEconomy.getInstance().getEconomyManager();
|
||||
Economable economable = Economable.wrap(targetPlayer);
|
||||
|
||||
double amount = NumberUtils.parseAndFilter(sAmount);
|
||||
double amount = NumberUtils.parseAndFilter(ecoMan.getCurrency(), sAmount);
|
||||
|
||||
if (amount <= 0) {
|
||||
MessageUtils.sendMessage(sender, "%s is not a positive number.", (amount == -1 ? sAmount : amount + ""));
|
||||
return;
|
||||
}
|
||||
|
||||
EconomyManager ecoMan = SaneEconomy.getInstance().getEconomyManager();
|
||||
|
||||
if (subCommand.equalsIgnoreCase("give")) {
|
||||
double newAmount = ecoMan.addBalance(economable, amount);
|
||||
|
||||
|
@ -42,6 +42,7 @@ public class PayCommand extends SaneEconomyCommand {
|
||||
throw new NeedPlayerException();
|
||||
}
|
||||
|
||||
EconomyManager ecoMan = SaneEconomy.getInstance().getEconomyManager();
|
||||
Player fromPlayer = (Player) sender;
|
||||
|
||||
String sToPlayer = args[0];
|
||||
@ -58,15 +59,13 @@ public class PayCommand extends SaneEconomyCommand {
|
||||
}
|
||||
|
||||
String sAmount = args[1];
|
||||
double amount = NumberUtils.parseAndFilter(sAmount);
|
||||
double amount = NumberUtils.parseAndFilter(ecoMan.getCurrency(), sAmount);
|
||||
|
||||
if (amount <= 0) {
|
||||
MessageUtils.sendMessage(sender, "%s is not a positive number.", (amount == -1 ? sAmount : amount + ""));
|
||||
return;
|
||||
}
|
||||
|
||||
EconomyManager ecoMan = SaneEconomy.getInstance().getEconomyManager();
|
||||
|
||||
/* Perform the actual transfer. False == They didn't have enough money */
|
||||
boolean result = ecoMan.transfer(Economable.wrap(fromPlayer), Economable.wrap(toPlayer), amount);
|
||||
|
||||
|
@ -15,7 +15,7 @@ public class Currency {
|
||||
private final String namePlural;
|
||||
private final DecimalFormat format;
|
||||
|
||||
private Currency(String nameSingular, String namePlural, DecimalFormat format) {
|
||||
public Currency(String nameSingular, String namePlural, DecimalFormat format) {
|
||||
this.nameSingular = nameSingular;
|
||||
this.namePlural = namePlural;
|
||||
this.format = format;
|
||||
|
@ -79,7 +79,7 @@ public class EconomyManager {
|
||||
* @throws IllegalArgumentException If amount is negative
|
||||
*/
|
||||
public double addBalance(Economable targetPlayer, double amount) {
|
||||
amount = NumberUtils.filterAmount(amount);
|
||||
amount = NumberUtils.filterAmount(currency, amount);
|
||||
|
||||
if (amount < 0) {
|
||||
throw new IllegalArgumentException("Cannot add a negative amount!");
|
||||
@ -101,7 +101,7 @@ public class EconomyManager {
|
||||
* @throws IllegalArgumentException If amount is negative
|
||||
*/
|
||||
public double subtractBalance(Economable targetPlayer, double amount) {
|
||||
amount = NumberUtils.filterAmount(amount);
|
||||
amount = NumberUtils.filterAmount(currency, amount);
|
||||
|
||||
if (amount < 0) {
|
||||
throw new IllegalArgumentException("Cannot subtract a negative amount!");
|
||||
@ -127,7 +127,7 @@ public class EconomyManager {
|
||||
* @throws IllegalArgumentException If amount is negative
|
||||
*/
|
||||
public void setBalance(Economable targetPlayer, double amount) {
|
||||
amount = NumberUtils.filterAmount(amount);
|
||||
amount = NumberUtils.filterAmount(currency, amount);
|
||||
|
||||
if (amount < 0) {
|
||||
throw new IllegalArgumentException("Cannot set balance to a negative value!");
|
||||
@ -145,7 +145,7 @@ public class EconomyManager {
|
||||
* @throws IllegalArgumentException If amount is negative
|
||||
*/
|
||||
public boolean transfer(Economable fromPlayer, Economable toPlayer, double amount) {
|
||||
amount = NumberUtils.filterAmount(amount);
|
||||
amount = NumberUtils.filterAmount(currency, amount);
|
||||
|
||||
if (amount < 0) {
|
||||
throw new IllegalArgumentException("Cannot transfer a negative amount!");
|
||||
|
@ -1,7 +1,7 @@
|
||||
package org.appledash.saneeconomy.utils;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import org.appledash.saneeconomy.SaneEconomy;
|
||||
import org.appledash.saneeconomy.economy.Currency;
|
||||
|
||||
/**
|
||||
* Created by AppleDash on 6/14/2016.
|
||||
@ -32,11 +32,11 @@ public class NumberUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public static double filterAmount(double amount) {
|
||||
return Double.valueOf(SaneEconomy.getInstance().getEconomyManager().getCurrency().getFormat().format(amount));
|
||||
public static double filterAmount(Currency currency, double amount) {
|
||||
return Double.valueOf(currency.getFormat().format(amount));
|
||||
}
|
||||
|
||||
public static double parseAndFilter(String sDouble) {
|
||||
return filterAmount(parsePositiveDouble(sDouble));
|
||||
public static double parseAndFilter(Currency currency, String sDouble) {
|
||||
return filterAmount(currency, parsePositiveDouble(sDouble));
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,20 @@
|
||||
package org.appledash.saneeconomy.test;
|
||||
|
||||
import org.appledash.saneeconomy.economy.Currency;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
|
||||
/**
|
||||
* Created by AppleDash on 7/29/2016.
|
||||
* Blackjack is still best pony.
|
||||
*/
|
||||
public class CurrencyTest {
|
||||
@Test
|
||||
public void testCurrencyFormat() {
|
||||
Currency currency = new Currency("test dollar", "test dollars", new DecimalFormat("0.00"));
|
||||
Assert.assertEquals(currency.formatAmount(1.0D), "1.00 test dollar");
|
||||
Assert.assertEquals(currency.formatAmount(1337.0D), "1337.00 test dollars");
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
package org.appledash.saneeconomy.test;
|
||||
|
||||
import org.appledash.saneeconomy.economy.economable.Economable;
|
||||
import org.appledash.saneeconomy.economy.economable.EconomableFaction;
|
||||
import org.appledash.saneeconomy.economy.economable.EconomableGeneric;
|
||||
import org.appledash.saneeconomy.economy.economable.EconomablePlayer;
|
||||
import org.appledash.saneeconomy.test.mock.MockOfflinePlayer;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Created by AppleDash on 7/29/2016.
|
||||
* Blackjack is still best pony.
|
||||
*/
|
||||
public class EconomableTest {
|
||||
@Test
|
||||
public void testWrapFaction() {
|
||||
UUID uuid = UUID.randomUUID();
|
||||
Economable economable = Economable.wrap(String.format("faction-%s", uuid.toString()));
|
||||
Assert.assertEquals(economable.getClass(), EconomableFaction.class);
|
||||
Assert.assertEquals(economable.getUniqueIdentifier(), String.format("faction:%s", uuid.toString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWrapPlayer() {
|
||||
OfflinePlayer dummy = new MockOfflinePlayer("Dummy");
|
||||
Economable economable = Economable.wrap(dummy);
|
||||
Assert.assertEquals(economable.getClass(), EconomablePlayer.class);
|
||||
Assert.assertEquals(economable.getUniqueIdentifier(), String.format("player:%s", dummy.getUniqueId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWrapGeneric() {
|
||||
Economable economable = Economable.wrap("something");
|
||||
Assert.assertEquals(economable.getClass(), EconomableGeneric.class);
|
||||
Assert.assertEquals(economable.getUniqueIdentifier(), "generic:something");
|
||||
}
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
package org.appledash.saneeconomy.test;
|
||||
|
||||
import org.appledash.saneeconomy.economy.Currency;
|
||||
import org.appledash.saneeconomy.economy.EconomyManager;
|
||||
import org.appledash.saneeconomy.economy.economable.Economable;
|
||||
import org.appledash.saneeconomy.test.mock.MockEconomyStorageBackend;
|
||||
import org.appledash.saneeconomy.test.mock.MockOfflinePlayer;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
|
||||
/**
|
||||
* Created by AppleDash on 7/29/2016.
|
||||
* Blackjack is still best pony.
|
||||
*/
|
||||
public class EconomyManagerTest {
|
||||
@Test
|
||||
public void testEconomyManager() {
|
||||
EconomyManager economyManager = new EconomyManager(new Currency("test dollar", "test dollars", new DecimalFormat("0.00")), new MockEconomyStorageBackend());
|
||||
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));
|
||||
Assert.assertEquals(economyManager.getBalance(playerOne), 0.0D, 0.0);
|
||||
Assert.assertEquals(economyManager.getBalance(playerTwo), 0.0D, 0.0);
|
||||
|
||||
economyManager.setBalance(playerOne, 100.0D);
|
||||
|
||||
// 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
|
||||
Assert.assertEquals(economyManager.getBalance(playerOne), 100.0, 0.0);
|
||||
Assert.assertEquals(economyManager.getBalance(playerTwo), 0.0, 0.0);
|
||||
|
||||
// One should be able to transfer to two
|
||||
Assert.assertTrue(economyManager.transfer(playerOne, playerTwo, 50.0));
|
||||
|
||||
// One should now have only 50 left, two should have 50 now
|
||||
Assert.assertEquals(economyManager.getBalance(playerOne), 50.0, 0.0);
|
||||
Assert.assertEquals(economyManager.getBalance(playerTwo), 50.0, 0.0);
|
||||
|
||||
// Ensure that balance addition and subtraction works...
|
||||
Assert.assertEquals(economyManager.subtractBalance(playerOne, 25.0), 25.0, 0.0);
|
||||
Assert.assertEquals(economyManager.addBalance(playerOne, 25.0), 50.0, 0.0);
|
||||
|
||||
// Ensure that hasBalance works
|
||||
Assert.assertTrue(economyManager.hasBalance(playerOne, 50.0));
|
||||
Assert.assertFalse(economyManager.hasBalance(playerOne, 51.0));
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package org.appledash.saneeconomy.test;
|
||||
|
||||
import org.appledash.saneeconomy.economy.Currency;
|
||||
import org.appledash.saneeconomy.utils.NumberUtils;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
|
||||
/**
|
||||
* Created by AppleDash on 7/29/2016.
|
||||
* Blackjack is still best pony.
|
||||
*/
|
||||
public class NumberUtilsTest {
|
||||
@Test
|
||||
public void testParsePositive() {
|
||||
// Valid input
|
||||
Assert.assertEquals(NumberUtils.parsePositiveDouble("69.0"), 69.0, 0.0);
|
||||
// Valid but not positive
|
||||
Assert.assertEquals(NumberUtils.parsePositiveDouble("-10.0"), -1.0, 0.0);
|
||||
// Invalid
|
||||
Assert.assertEquals(NumberUtils.parsePositiveDouble("nan"), -1.0, 0.0);
|
||||
Assert.assertEquals(NumberUtils.parsePositiveDouble("ponies"), -1.0, 0.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFilter() {
|
||||
Currency currency = new Currency(null, null, new DecimalFormat("0.00"));
|
||||
|
||||
Assert.assertEquals(NumberUtils.filterAmount(currency, 1337.420D), 1337.42, 0.0);
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package org.appledash.saneeconomy.test.mock;
|
||||
|
||||
import org.appledash.saneeconomy.economy.backend.type.EconomyStorageBackendCaching;
|
||||
import org.appledash.saneeconomy.economy.economable.Economable;
|
||||
|
||||
/**
|
||||
* Created by AppleDash on 7/29/2016.
|
||||
* Blackjack is still best pony.
|
||||
*/
|
||||
public class MockEconomyStorageBackend extends EconomyStorageBackendCaching {
|
||||
@Override
|
||||
public void setBalance(Economable player, double newBalance) {
|
||||
balances.put(player.getUniqueIdentifier(), newBalance);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reloadDatabase() {
|
||||
System.out.println("Reloading mock economy database (doing nothing).");
|
||||
}
|
||||
}
|
@ -0,0 +1,103 @@
|
||||
package org.appledash.saneeconomy.test.mock;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Created by AppleDash on 7/29/2016.
|
||||
* Blackjack is still best pony.
|
||||
*
|
||||
* Implemented: Name and UUID
|
||||
*/
|
||||
public class MockOfflinePlayer implements OfflinePlayer {
|
||||
private final UUID uuid;
|
||||
private final String name;
|
||||
|
||||
public MockOfflinePlayer(UUID uuid, String name) {
|
||||
this.uuid = uuid;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public MockOfflinePlayer(String name) {
|
||||
this(UUID.randomUUID(), name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isOnline() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID getUniqueId() {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isBanned() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBanned(boolean banned) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isWhitelisted() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setWhitelisted(boolean value) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Player getPlayer() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getFirstPlayed() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getLastPlayed() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPlayedBefore() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Location getBedSpawnLocation() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> serialize() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isOp() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setOp(boolean value) {
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user