I... I can't help it, man. I inject dependencies, OK?

There, I said it.
This commit is contained in:
AppleDash 2016-09-18 04:37:44 -04:00
parent eab9e0774b
commit 4b088afec9
5 changed files with 79 additions and 10 deletions

View File

@ -0,0 +1,28 @@
package org.appledash.saneeconomy;
import org.appledash.saneeconomy.economy.EconomyManager;
import org.appledash.saneeconomy.economy.logger.TransactionLogger;
/**
* Created by appledash on 9/18/16.
* Blackjack is best pony.
*/
public interface ISaneEconomy {
/**
* Get the active EconomyManager.
* @return EconomyManager
*/
EconomyManager getEconomyManager();
/**
* Check whether transactions should be logged.
* @return True if transactions should be logged, false otherwise.
*/
boolean shouldLogTransactions();
/**
* Get the active TransactionLogger.
* @return TransactionLogger, if there is one.
*/
TransactionLogger getTransactionLogger();
}

View File

@ -25,7 +25,7 @@ import java.util.logging.Logger;
* Created by AppleDash on 6/13/2016.
* Blackjack is still best pony.
*/
public class SaneEconomy extends JavaPlugin {
public class SaneEconomy extends JavaPlugin implements ISaneEconomy {
private static SaneEconomy instance;
private EconomyManager economyManager;
private VaultHook vaultHook;
@ -131,7 +131,7 @@ public class SaneEconomy extends JavaPlugin {
saveConfig();
}
economyManager = new EconomyManager(currency, backend);
economyManager = new EconomyManager(this, currency, backend);
return true;
}
@ -199,6 +199,7 @@ public class SaneEconomy extends JavaPlugin {
* Get the active EconomyManager.
* @return EconomyManager
*/
@Override
public EconomyManager getEconomyManager() {
return economyManager;
}
@ -207,6 +208,7 @@ public class SaneEconomy extends JavaPlugin {
* Check whether transactions should be logged.
* @return True if transactions should be logged, false otherwise.
*/
@Override
public boolean shouldLogTransactions() {
return transactionLogger != null;
}
@ -215,6 +217,7 @@ public class SaneEconomy extends JavaPlugin {
* Get the active TransactionLogger.
* @return TransactionLogger, if there is one.
*/
@Override
public TransactionLogger getTransactionLogger() {
return transactionLogger;
}

View File

@ -1,5 +1,6 @@
package org.appledash.saneeconomy.economy;
import org.appledash.saneeconomy.ISaneEconomy;
import org.appledash.saneeconomy.SaneEconomy;
import org.appledash.saneeconomy.economy.backend.EconomyStorageBackend;
import org.appledash.saneeconomy.economy.economable.Economable;
@ -18,10 +19,12 @@ import java.util.UUID;
* Represents our EconomyManager, which manages players' balances.
*/
public class EconomyManager {
private ISaneEconomy saneEconomy;
private final Currency currency;
private final EconomyStorageBackend backend;
public EconomyManager(Currency currency, EconomyStorageBackend backend) {
public EconomyManager(ISaneEconomy saneEconomy, Currency currency, EconomyStorageBackend backend) {
this.saneEconomy = saneEconomy;
this.currency = currency;
this.backend = backend;
}
@ -138,11 +141,11 @@ public class EconomyManager {
backend.setBalance(targetPlayer, amount);
if (SaneEconomy.getInstance().shouldLogTransactions() && reason != TransactionReason.PLAYER_PAY) { // Player pay is handled in the transfer() method.
if (saneEconomy.shouldLogTransactions() && reason != TransactionReason.PLAYER_PAY) { // Player pay is handled in the transfer() method.
if (oldAmount > amount) { // Lower amount now
SaneEconomy.getInstance().getTransactionLogger().logSubtraction(targetPlayer, amount, reason);
saneEconomy.getTransactionLogger().logSubtraction(targetPlayer, amount, reason);
} else if (oldAmount < amount) { // Higher amount now
SaneEconomy.getInstance().getTransactionLogger().logAddition(targetPlayer, amount, reason);
saneEconomy.getTransactionLogger().logAddition(targetPlayer, amount, reason);
}
}
}
@ -170,8 +173,8 @@ public class EconomyManager {
subtractBalance(from, amount, TransactionReason.PLAYER_PAY);
addBalance(to, amount, TransactionReason.PLAYER_PAY);
if (SaneEconomy.getInstance().shouldLogTransactions()) {
SaneEconomy.getInstance().getTransactionLogger().logTransfer(from, to, amount, TransactionReason.PLAYER_PAY);
if (saneEconomy.shouldLogTransactions()) {
saneEconomy.getTransactionLogger().logTransfer(from, to, amount, TransactionReason.PLAYER_PAY);
}
return true;

View File

@ -6,7 +6,9 @@ import org.appledash.saneeconomy.economy.TransactionReason;
import org.appledash.saneeconomy.economy.economable.Economable;
import org.appledash.saneeconomy.test.mock.MockEconomyStorageBackend;
import org.appledash.saneeconomy.test.mock.MockOfflinePlayer;
import org.appledash.saneeconomy.test.mock.MockSaneEconomy;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.text.DecimalFormat;
@ -16,9 +18,17 @@ import java.text.DecimalFormat;
* Blackjack is still best pony.
*/
public class EconomyManagerTest {
private EconomyManager economyManager;
@Before
public void setupEconomyManager() {
economyManager = new EconomyManager(new MockSaneEconomy(),
new Currency("test dollar", "test dollars", new DecimalFormat("0.00")),
new MockEconomyStorageBackend());
}
@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"));
@ -59,7 +69,6 @@ public class EconomyManagerTest {
@Test(expected = IllegalArgumentException.class)
public void testNegativeBalance() {
EconomyManager economyManager = new EconomyManager(new Currency("test dollar", "test dollars", new DecimalFormat("0.00")), new MockEconomyStorageBackend());
Economable economable = Economable.wrap(new MockOfflinePlayer("Bob"));
economyManager.setBalance(economable, -1.0, TransactionReason.PLUGIN);
}

View File

@ -0,0 +1,26 @@
package org.appledash.saneeconomy.test.mock;
import org.appledash.saneeconomy.ISaneEconomy;
import org.appledash.saneeconomy.economy.EconomyManager;
import org.appledash.saneeconomy.economy.logger.TransactionLogger;
/**
* Created by appledash on 9/18/16.
* Blackjack is best pony.
*/
public class MockSaneEconomy implements ISaneEconomy {
@Override
public EconomyManager getEconomyManager() {
return null;
}
@Override
public boolean shouldLogTransactions() {
return false;
}
@Override
public TransactionLogger getTransactionLogger() {
return null;
}
}