Improvements to tests & version bump to 0.16.1

This commit is contained in:
AppleDash 2019-11-11 16:08:04 -05:00
parent 5663077718
commit d2fea85ffe
9 changed files with 61 additions and 18 deletions

View File

@ -9,7 +9,7 @@
<version>0</version> <version>0</version>
</parent> </parent>
<artifactId>SaneEconomyCore</artifactId> <artifactId>SaneEconomyCore</artifactId>
<version>0.16.0-SNAPSHOT</version> <version>0.16.1-SNAPSHOT</version>
<dependencies> <dependencies>
<dependency> <dependency>

View File

@ -98,7 +98,7 @@ public class SaneEconomy extends SanePlugin implements ISaneEconomy {
Arrays.stream(playersToSync).filter(p -> (p != null) && !p.isOnline()).forEach(p -> { Arrays.stream(playersToSync).filter(p -> (p != null) && !p.isOnline()).forEach(p -> {
ByteArrayDataOutput bado = ByteStreams.newDataOutput(); ByteArrayDataOutput bado = ByteStreams.newDataOutput();
bado.writeUTF("Forward"); bado.writeUTF("Forward");
bado.writeUTF("ALL"); bado.writeUTF("ONLINE");
bado.writeUTF("SaneEconomy"); bado.writeUTF("SaneEconomy");
bado.writeUTF("SyncPlayer"); bado.writeUTF("SyncPlayer");
bado.writeUTF(p.getUniqueId().toString()); bado.writeUTF(p.getUniqueId().toString());

View File

@ -60,6 +60,18 @@ public final class NumberUtils {
return filterAmount(currency, parsePositiveDouble(sDouble)); return filterAmount(currency, parsePositiveDouble(sDouble));
} }
public static boolean equals(BigDecimal left, BigDecimal right) {
if (left == null) {
throw new NullPointerException("left == null");
}
if (right == null) {
throw new NullPointerException("right == null");
}
return left.compareTo(right) == 0;
}
private static DecimalFormat constructDecimalFormat() { private static DecimalFormat constructDecimalFormat() {
DecimalFormat decimalFormat = (DecimalFormat) NumberFormat.getInstance(); DecimalFormat decimalFormat = (DecimalFormat) NumberFormat.getInstance();

View File

@ -10,6 +10,7 @@ import org.appledash.saneeconomy.economy.transaction.TransactionResult;
import org.appledash.saneeconomy.test.mock.MockEconomyStorageBackend; import org.appledash.saneeconomy.test.mock.MockEconomyStorageBackend;
import org.appledash.saneeconomy.test.mock.MockOfflinePlayer; import org.appledash.saneeconomy.test.mock.MockOfflinePlayer;
import org.appledash.saneeconomy.test.mock.MockSaneEconomy; import org.appledash.saneeconomy.test.mock.MockSaneEconomy;
import org.appledash.saneeconomy.test.util.SaneEcoAssert;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -41,8 +42,8 @@ public class EconomyManagerTest {
// Accounts should not exist // Accounts should not exist
Assert.assertFalse(this.economyManager.accountExists(playerOne)); Assert.assertFalse(this.economyManager.accountExists(playerOne));
Assert.assertFalse(this.economyManager.accountExists(playerTwo)); Assert.assertFalse(this.economyManager.accountExists(playerTwo));
Assert.assertEquals(BigDecimal.ZERO, this.economyManager.getBalance(playerOne)); SaneEcoAssert.assertEquals(BigDecimal.ZERO, this.economyManager.getBalance(playerOne));
Assert.assertEquals(BigDecimal.ZERO, this.economyManager.getBalance(playerTwo)); SaneEcoAssert.assertEquals(BigDecimal.ZERO, this.economyManager.getBalance(playerTwo));
this.economyManager.setBalance(playerOne, new BigDecimal(100.0)); this.economyManager.setBalance(playerOne, new BigDecimal(100.0));
@ -51,22 +52,22 @@ public class EconomyManagerTest {
Assert.assertFalse(this.economyManager.accountExists(playerTwo)); Assert.assertFalse(this.economyManager.accountExists(playerTwo));
// One should have balance, two should not // One should have balance, two should not
Assert.assertEquals(new BigDecimal("100.00"), this.economyManager.getBalance(playerOne)); SaneEcoAssert.assertEquals(new BigDecimal("100.00"), this.economyManager.getBalance(playerOne));
Assert.assertEquals(BigDecimal.ZERO, this.economyManager.getBalance(playerTwo)); SaneEcoAssert.assertEquals(BigDecimal.ZERO, this.economyManager.getBalance(playerTwo));
// One should be able to transfer to two // One should be able to transfer to two
Assert.assertSame(this.economyManager.transact(new Transaction(this.economyManager.getCurrency(), playerOne, playerTwo, new BigDecimal(50.0), TransactionReason.PLAYER_PAY)).getStatus(), TransactionResult.Status.SUCCESS); Assert.assertSame(this.economyManager.transact(new Transaction(this.economyManager.getCurrency(), playerOne, playerTwo, new BigDecimal(50.0), TransactionReason.PLAYER_PAY)).getStatus(), TransactionResult.Status.SUCCESS);
// One should now have only 50 left, two should have 50 now // One should now have only 50 left, two should have 50 now
Assert.assertEquals("Player one should have 50 dollars", new BigDecimal("50.00"), this.economyManager.getBalance(playerOne)); SaneEcoAssert.assertEquals("Player one should have 50 dollars", new BigDecimal("50.00"), this.economyManager.getBalance(playerOne));
Assert.assertEquals("Player two should have 50 dollars", new BigDecimal("50.00"), this.economyManager.getBalance(playerTwo)); SaneEcoAssert.assertEquals("Player two should have 50 dollars", new BigDecimal("50.00"), this.economyManager.getBalance(playerTwo));
// Ensure that balance addition and subtraction works... // Ensure that balance addition and subtraction works...
Assert.assertEquals(new BigDecimal("25.00"), this.economyManager.transact( SaneEcoAssert.assertEquals(new BigDecimal("25.00"), this.economyManager.transact(
new Transaction(this.economyManager.getCurrency(), playerOne, Economable.CONSOLE, new BigDecimal("25.00"), TransactionReason.TEST_TAKE) new Transaction(this.economyManager.getCurrency(), playerOne, Economable.CONSOLE, new BigDecimal("25.00"), TransactionReason.TEST_TAKE)
).getFromBalance()); ).getFromBalance());
Assert.assertEquals(new BigDecimal("50.00"), this.economyManager.transact( SaneEcoAssert.assertEquals(new BigDecimal("50.00"), this.economyManager.transact(
new Transaction(this.economyManager.getCurrency(), Economable.CONSOLE, playerOne, new BigDecimal("25.00"), TransactionReason.TEST_GIVE) new Transaction(this.economyManager.getCurrency(), Economable.CONSOLE, playerOne, new BigDecimal("25.00"), TransactionReason.TEST_GIVE)
).getToBalance()); ).getToBalance());

View File

@ -1,6 +1,7 @@
package org.appledash.saneeconomy.test; package org.appledash.saneeconomy.test;
import org.appledash.saneeconomy.economy.Currency; import org.appledash.saneeconomy.economy.Currency;
import org.appledash.saneeconomy.test.util.SaneEcoAssert;
import org.appledash.saneeconomy.utils.NumberUtils; import org.appledash.saneeconomy.utils.NumberUtils;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
@ -17,12 +18,12 @@ public class NumberUtilsTest {
@Test @Test
public void testParsePositive() { public void testParsePositive() {
// Valid input // Valid input
Assert.assertEquals(new BigDecimal("69.0"), NumberUtils.parsePositiveDouble("69.0")); SaneEcoAssert.assertEquals(new BigDecimal("69.0"), NumberUtils.parsePositiveDouble("69.0"));
// Valid but not positive // Valid but not positive
Assert.assertEquals(BigDecimal.ONE.negate(), NumberUtils.parsePositiveDouble("-10.0")); SaneEcoAssert.assertEquals(BigDecimal.ONE.negate(), NumberUtils.parsePositiveDouble("-10.0"));
// Invalid // Invalid
Assert.assertEquals(BigDecimal.ONE.negate(), NumberUtils.parsePositiveDouble("nan")); SaneEcoAssert.assertEquals(BigDecimal.ONE.negate(), NumberUtils.parsePositiveDouble("nan"));
Assert.assertEquals(BigDecimal.ONE.negate(), NumberUtils.parsePositiveDouble("ponies")); SaneEcoAssert.assertEquals(BigDecimal.ONE.negate(), NumberUtils.parsePositiveDouble("ponies"));
// Infinite // Infinite
// TODO: Not needed with BigDecimal? Assert.assertEquals(BigDecimal.ONE.negate(), NumberUtils.parsePositiveDouble("1E1000000000")); // TODO: Not needed with BigDecimal? Assert.assertEquals(BigDecimal.ONE.negate(), NumberUtils.parsePositiveDouble("1E1000000000"));
} }
@ -31,7 +32,7 @@ public class NumberUtilsTest {
public void testFilter() { public void testFilter() {
Currency currency = new Currency(null, null, new DecimalFormat("0.00")); Currency currency = new Currency(null, null, new DecimalFormat("0.00"));
Assert.assertEquals(new BigDecimal("1337.42"), NumberUtils.filterAmount(currency, new BigDecimal("1337.420"))); SaneEcoAssert.assertEquals(new BigDecimal("1337.42"), NumberUtils.filterAmount(currency, new BigDecimal("1337.420")));
} }
@Test @Test
@ -47,4 +48,14 @@ public class NumberUtilsTest {
Locale.setDefault(old); Locale.setDefault(old);
} }
} }
@Test
public void testBigDecimalEquals() {
BigDecimal one = new BigDecimal("100.0");
BigDecimal two = new BigDecimal("100.00");
BigDecimal three = new BigDecimal("100.1");
Assert.assertTrue("100.0 should equal 100.00", NumberUtils.equals(one, two));
Assert.assertFalse("100.0 should not equal 100.1", NumberUtils.equals(one, three));
}
} }

View File

@ -0,0 +1,19 @@
package org.appledash.saneeconomy.test.util;
import org.appledash.saneeconomy.utils.NumberUtils;
import org.junit.Assert;
import java.math.BigDecimal;
public final class SaneEcoAssert {
private SaneEcoAssert() {
}
public static void assertEquals(BigDecimal left, BigDecimal right) {
Assert.assertTrue(String.format("%s != %s", left.toPlainString(), right.toPlainString()), NumberUtils.equals(left, right));
}
public static void assertEquals(String message, BigDecimal left, BigDecimal right) {
Assert.assertTrue(message, NumberUtils.equals(left, right));
}
}

View File

@ -16,7 +16,7 @@
<dependency> <dependency>
<groupId>org.appledash</groupId> <groupId>org.appledash</groupId>
<artifactId>SaneEconomyCore</artifactId> <artifactId>SaneEconomyCore</artifactId>
<version>0.14.0-SNAPSHOT</version> <version>0.16.1-SNAPSHOT</version>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@ -17,7 +17,7 @@
<dependency> <dependency>
<groupId>org.appledash</groupId> <groupId>org.appledash</groupId>
<artifactId>SaneEconomyCore</artifactId> <artifactId>SaneEconomyCore</artifactId>
<version>0.14.0-SNAPSHOT</version> <version>0.16.1-SNAPSHOT</version>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@ -16,7 +16,7 @@
<dependency> <dependency>
<groupId>org.appledash</groupId> <groupId>org.appledash</groupId>
<artifactId>SaneEconomyCore</artifactId> <artifactId>SaneEconomyCore</artifactId>
<version>0.14.0-SNAPSHOT</version> <version>0.16.1-SNAPSHOT</version>
</dependency> </dependency>
</dependencies> </dependencies>