add input validators

This commit is contained in:
Manfred Karrer 2014-08-27 21:25:49 +02:00
parent 223b32e603
commit 7fee8b665e
15 changed files with 284 additions and 136 deletions

View file

@ -1,39 +0,0 @@
/*
* This file is part of Bitsquare.
*
* Bitsquare is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bitsquare is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bitsquare. If not, see <http://www.gnu.org/licenses/>.
*/
package io.bitsquare.btc;
import com.google.bitcoin.core.Coin;
import com.google.bitcoin.core.NetworkParameters;
import com.google.bitcoin.core.Transaction;
import javax.inject.Inject;
// TODO will be probably removed
public class BtcValidator {
private static NetworkParameters params;
@Inject
public BtcValidator(NetworkParameters params) {
BtcValidator.params = params;
}
public static boolean isMinSpendableAmount(Coin amount) {
return amount != null && amount.compareTo(FeePolicy.TX_FEE.add(Transaction.MIN_NONDUST_OUTPUT)) > 0;
}
}

View file

@ -18,10 +18,14 @@
package io.bitsquare.btc;
import com.google.bitcoin.core.Coin;
import com.google.bitcoin.core.Transaction;
// Lets see if we get more restriction otherwise move it to other class
public class Restrictions {
public static final Coin MIN_TRADE_AMOUNT = Coin.parseCoin("0.0001");
public static boolean isMinSpendableAmount(Coin amount) {
return amount != null && amount.compareTo(FeePolicy.TX_FEE.add(Transaction.MIN_NONDUST_OUTPUT)) > 0;
}
}

View file

@ -18,8 +18,8 @@
package io.bitsquare.gui.funds.withdrawal;
import io.bitsquare.btc.AddressEntry;
import io.bitsquare.btc.BtcValidator;
import io.bitsquare.btc.FeePolicy;
import io.bitsquare.btc.Restrictions;
import io.bitsquare.btc.WalletFacade;
import io.bitsquare.gui.CachedViewController;
import io.bitsquare.gui.components.Popups;
@ -148,8 +148,8 @@ public class WithdrawalController extends CachedViewController {
amountTextField, withdrawFromTextField, withdrawToTextField, changeAddressTextField);
BitSquareValidator.textFieldsHasDoubleValueWithReset(amountTextField);
Coin amount = BSFormatter.parseToCoin(amountTextField.getText());
if (BtcValidator.isMinSpendableAmount(amount)) {
Coin amount = BSFormatter.parseToBtc(amountTextField.getText());
if (Restrictions.isMinSpendableAmount(amount)) {
FutureCallback<Transaction> callback = new FutureCallback<Transaction>() {
@Override
public void onSuccess(@javax.annotation.Nullable Transaction transaction) {

View file

@ -24,6 +24,9 @@ import io.bitsquare.gui.components.btc.AddressTextField;
import io.bitsquare.gui.components.btc.BalanceTextField;
import io.bitsquare.gui.components.confidence.ConfidenceProgressIndicator;
import io.bitsquare.gui.trade.TradeController;
import io.bitsquare.gui.util.validation.BtcValidator;
import io.bitsquare.gui.util.validation.FiatValidator;
import io.bitsquare.gui.util.validation.ValidationHelper;
import io.bitsquare.trade.orderbook.OrderBookFilter;
import java.net.URL;
@ -34,6 +37,7 @@ import javax.inject.Inject;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -57,9 +61,8 @@ import org.slf4j.LoggerFactory;
* <p>
* Optional ViewBuilder:
* - Replacement for FXML view definitions.
*
* <p>
* Note: Don't assign the root node as it is defined in the base class!
*
*/
public class CreateOfferCodeBehind extends CachedViewController {
private static final Logger log = LoggerFactory.getLogger(CreateOfferCodeBehind.class);
@ -154,15 +157,25 @@ public class CreateOfferCodeBehind extends CachedViewController {
///////////////////////////////////////////////////////////////////////////////////////////
private void setupListeners() {
volumeTextField.focusedProperty().addListener((observableValue, oldValue,
newValue) -> presenter.validateVolumeOnFocusOut(oldValue,
newValue, volumeTextField.getText()));
amountTextField.focusedProperty().addListener((observableValue, oldValue,
newValue) -> presenter.onFocusOutAmountTextField(oldValue,
newValue));
priceTextField.focusedProperty().addListener((observableValue, oldValue,
newValue) -> presenter.onFocusOutPriceTextField(oldValue,
newValue));
volumeTextField.focusedProperty().addListener((o, oldValue, newValue) -> {
presenter.onFocusOutVolumeTextField(oldValue, newValue, volumeTextField.getText());
volumeTextField.setText(presenter.volume.get());
});
amountTextField.focusedProperty().addListener((o, oldValue, newValue) -> {
presenter.onFocusOutAmountTextField(oldValue, newValue);
amountTextField.setText(presenter.amount.get());
});
priceTextField.focusedProperty().addListener((o, oldValue, newValue) -> {
presenter.onFocusOutPriceTextField(oldValue, newValue);
priceTextField.setText(presenter.price.get());
});
minAmountTextField.focusedProperty().addListener((o, oldValue, newValue) -> {
presenter.onFocusOutMinAmountTextField(oldValue, newValue);
minAmountTextField.setText(presenter.minAmount.get());
});
presenter.needsInputValidation.addListener((o, oldValue, newValue) -> {
if (newValue) {
@ -173,17 +186,36 @@ public class CreateOfferCodeBehind extends CachedViewController {
}
});
presenter.showVolumeAdjustedWarning.addListener((o, oldValue, newValue) -> {
presenter.showWarningInvalidBtcDecimalPlaces.addListener((o, oldValue, newValue) -> {
if (newValue) {
Popups.openWarningPopup("Warning", "The amount you have entered exceeds the number of allowed decimal" +
" places.\nThe amount has been adjusted to 4 decimal places.");
presenter.showWarningInvalidBtcDecimalPlaces.set(false);
}
});
presenter.showWarningInvalidFiatDecimalPlaces.addListener((o, oldValue, newValue) -> {
if (newValue) {
Popups.openWarningPopup("Warning", "The amount you have entered exceeds the number of allowed decimal" +
" places.\nThe amount has been adjusted to 2 decimal places.");
presenter.showWarningInvalidFiatDecimalPlaces.set(false);
}
});
presenter.showWarningInvalidBtcFractions.addListener((o, oldValue, newValue) -> {
if (newValue) {
Popups.openWarningPopup("Warning", "The total volume you have entered leads to invalid fractional " +
"Bitcoin amounts.\nThe amount has been adjusted and a new total volume be calculated from it.");
presenter.showWarningInvalidBtcFractions.set(false);
volumeTextField.setText(presenter.volume.get());
}
});
presenter.requestPlaceOfferFailed.addListener((o, oldValue, newValue) -> {
if (newValue) {
Popups.openErrorPopup("Error", "An error occurred when placing the offer.\n" +
presenter.requestPlaceOfferErrorMessage);
presenter.requestPlaceOfferFailed.set(false);
}
});
}
@ -231,26 +263,27 @@ public class CreateOfferCodeBehind extends CachedViewController {
}
private void setupTextFieldValidators() {
/* BtcValidator amountValidator = new BtcValidator();
amountTextField.setNumberValidator(amountValidator);
amountTextField.setErrorPopupLayoutReference((Region) amountTextField.getParent());
Region referenceNode = (Region) amountTextField.getParent();
BtcValidator amountValidator = new BtcValidator();
amountTextField.setValidator(amountValidator);
amountTextField.setErrorPopupLayoutReference(referenceNode);
priceTextField.setNumberValidator(new FiatValidator());
priceTextField.setErrorPopupLayoutReference((Region) amountTextField.getParent());
priceTextField.setValidator(new FiatValidator());
priceTextField.setErrorPopupLayoutReference(referenceNode);
BtcValidator volumeValidator = new BtcValidator();
volumeTextField.setNumberValidator(volumeValidator);
volumeTextField.setErrorPopupLayoutReference((Region) volumeTextField.getParent());
volumeTextField.setValidator(new FiatValidator());
volumeTextField.setErrorPopupLayoutReference(referenceNode);
BtcValidator minAmountValidator = new BtcValidator();
minAmountTextField.setNumberValidator(minAmountValidator);
minAmountTextField.setValidator(minAmountValidator);
ValidationHelper.setupMinAmountInRangeOfAmountValidation(amountTextField,
minAmountTextField,
presenter.amount,
presenter.minAmount,
amountValidator,
minAmountValidator);*/
minAmountTextField,
presenter.amount,
presenter.minAmount,
amountValidator,
minAmountValidator);
}
}

View file

@ -75,7 +75,7 @@ class CreateOfferModel {
Coin minAmountAsCoin;
Coin collateralAsCoin;
Fiat priceAsFiat;
Fiat tradeVolumeAsFiat;
Fiat volumeAsFiat;
AddressEntry addressEntry;
final StringProperty requestPlaceOfferErrorMessage = new SimpleStringProperty();

View file

@ -81,7 +81,9 @@ class CreateOfferPresenter {
final BooleanProperty isPlaceOfferButtonVisible = new SimpleBooleanProperty(true);
final BooleanProperty isPlaceOfferButtonDisabled = new SimpleBooleanProperty();
final BooleanProperty needsInputValidation = new SimpleBooleanProperty();
final BooleanProperty showVolumeAdjustedWarning = new SimpleBooleanProperty();
final BooleanProperty showWarningInvalidBtcFractions = new SimpleBooleanProperty();
final BooleanProperty showWarningInvalidFiatDecimalPlaces = new SimpleBooleanProperty();
final BooleanProperty showWarningInvalidBtcDecimalPlaces = new SimpleBooleanProperty();
final BooleanProperty showTransactionPublishedScreen = new SimpleBooleanProperty();
final BooleanProperty requestPlaceOfferFailed = new SimpleBooleanProperty();
@ -161,7 +163,7 @@ class CreateOfferPresenter {
model.minAmountAsCoin = orderBookFilter.getAmount();
// TODO use Fiat in orderBookFilter
model.priceAsFiat = parseToFiat(String.valueOf(orderBookFilter.getPrice()));
model.priceAsFiat = parseToFiatWith2Decimals(String.valueOf(orderBookFilter.getPrice()));
directionLabel.set(model.getDirection() == Direction.BUY ? "Buy:" : "Sell:");
amount.set(formatBtc(model.amountAsCoin));
@ -175,10 +177,10 @@ class CreateOfferPresenter {
///////////////////////////////////////////////////////////////////////////////////////////
void placeOffer() {
model.amountAsCoin = parseToCoin(amount.get());
model.minAmountAsCoin = parseToCoin(minAmount.get());
model.priceAsFiat = parseToFiat(price.get());
model.minAmountAsCoin = parseToCoin(minAmount.get());
model.amountAsCoin = parseToBtcWith4Decimals(amount.get());
model.minAmountAsCoin = parseToBtcWith4Decimals(minAmount.get());
model.priceAsFiat = parseToFiatWith2Decimals(price.get());
model.minAmountAsCoin = parseToBtcWith4Decimals(minAmount.get());
needsInputValidation.set(true);
@ -200,53 +202,64 @@ class CreateOfferPresenter {
// bindBidirectional for amount, price, volume and minAmount
amount.addListener(ov -> {
model.amountAsCoin = parseToCoin(amount.get());
model.amountAsCoin = parseToBtcWith4Decimals(amount.get());
calculateVolume();
calculateTotalToPay();
calculateCollateral();
});
price.addListener(ov -> {
model.priceAsFiat = parseToFiat(price.get());
model.priceAsFiat = parseToFiatWith2Decimals(price.get());
calculateVolume();
calculateTotalToPay();
calculateCollateral();
});
volume.addListener(ov -> {
model.tradeVolumeAsFiat = parseToFiat(volume.get());
model.volumeAsFiat = parseToFiatWith2Decimals(volume.get());
calculateAmount();
calculateTotalToPay();
calculateCollateral();
});
}
void onFocusOutAmountTextField(Boolean oldValue, Boolean newValue) {
// We adjust the volume if fractional coins result from volume/price division on focus out
void validateVolumeOnFocusOut(Boolean oldValue, Boolean newValue, String volumeTextFieldText) {
if (oldValue && !newValue) {
showWarningInvalidBtcDecimalPlaces.set(!hasBtcValidDecimals(amount.get()));
model.amountAsCoin = parseToBtcWith4Decimals(amount.get());
amount.set(formatBtc(model.amountAsCoin));
calculateVolume();
if (!formatFiat(parseToFiat(volumeTextFieldText)).equals(volume.get()))
showVolumeAdjustedWarning.set(true);
}
//
// only on focus out and ignore focus loss from window
/* if (!newValue && volumeTextField.getScene() != null && volumeTextField.getScene().getWindow().isFocused())
amountTextField.reValidate();*/
}
void onFocusOutAmountTextField(Boolean oldValue, Boolean newValue) {
// only on focus out and ignore focus loss from window
/* if (!newValue && amountTextField.getScene() != null && amountTextField.getScene().getWindow().isFocused())
volumeTextField.reValidate();*/
void onFocusOutMinAmountTextField(Boolean oldValue, Boolean newValue) {
if (oldValue && !newValue) {
showWarningInvalidBtcDecimalPlaces.set(!hasBtcValidDecimals(minAmount.get()));
model.minAmountAsCoin = parseToBtcWith4Decimals(minAmount.get());
minAmount.set(formatBtc(model.minAmountAsCoin));
}
}
void onFocusOutVolumeTextField(Boolean oldValue, Boolean newValue, String volumeTextFieldText) {
if (oldValue && !newValue) {
showWarningInvalidFiatDecimalPlaces.set(!hasFiatValidDecimals(volume.get()));
model.volumeAsFiat = parseToFiatWith2Decimals(volume.get());
volume.set(formatFiat(model.volumeAsFiat));
calculateAmount();
showWarningInvalidBtcFractions.set(!formatFiat(parseToFiatWith2Decimals(volumeTextFieldText)).equals
(volume.get()));
}
}
void onFocusOutPriceTextField(Boolean oldValue, Boolean newValue) {
// only on focus out and ignore focus loss from window
/* if (!newValue && priceTextField.getScene() != null && priceTextField.getScene().getWindow().isFocused())
volumeTextField.reValidate();*/
if (oldValue && !newValue) {
showWarningInvalidFiatDecimalPlaces.set(!hasFiatValidDecimals(price.get()));
model.priceAsFiat = parseToFiatWith2Decimals(price.get());
price.set(formatFiat(model.priceAsFiat));
}
}
@ -269,24 +282,24 @@ class CreateOfferPresenter {
}
private void calculateVolume() {
model.amountAsCoin = parseToCoin(amount.get());
model.priceAsFiat = parseToFiat(price.get());
model.amountAsCoin = parseToBtcWith4Decimals(amount.get());
model.priceAsFiat = parseToFiatWith2Decimals(price.get());
if (model.priceAsFiat != null && model.amountAsCoin != null && !model.amountAsCoin.isZero()) {
model.tradeVolumeAsFiat = new ExchangeRate(model.priceAsFiat).coinToFiat(model.amountAsCoin);
volume.set(formatFiat(model.tradeVolumeAsFiat));
model.volumeAsFiat = new ExchangeRate(model.priceAsFiat).coinToFiat(model.amountAsCoin);
volume.set(formatFiat(model.volumeAsFiat));
}
}
private void calculateAmount() {
model.tradeVolumeAsFiat = parseToFiat(volume.get());
model.priceAsFiat = parseToFiat(price.get());
model.volumeAsFiat = parseToFiatWith2Decimals(volume.get());
model.priceAsFiat = parseToFiatWith2Decimals(price.get());
if (model.tradeVolumeAsFiat != null && model.priceAsFiat != null && !model.priceAsFiat.isZero()) {
model.amountAsCoin = new ExchangeRate(model.priceAsFiat).fiatToCoin(model.tradeVolumeAsFiat);
if (model.volumeAsFiat != null && model.priceAsFiat != null && !model.priceAsFiat.isZero()) {
model.amountAsCoin = new ExchangeRate(model.priceAsFiat).fiatToCoin(model.volumeAsFiat);
// If we got a btc value with more then 4 decimals we convert it to max 4 decimals
model.amountAsCoin = applyFormatRules(model.amountAsCoin);
model.amountAsCoin = reduceto4Dezimals(model.amountAsCoin);
amount.set(formatBtc(model.amountAsCoin));
calculateTotalToPay();
calculateCollateral();

View file

@ -48,11 +48,11 @@
<Label GridPane.rowIndex="1" fx:id="buyLabel"/>
<HBox GridPane.rowIndex="1" GridPane.columnIndex="1" spacing="5" GridPane.hgrow="NEVER" alignment="CENTER_LEFT">
<ValidatingTextField fx:id="amountTextField" prefWidth="70.0" alignment="CENTER_RIGHT"/>
<Label text="BTC for:"/>
<ValidatingTextField fx:id="priceTextField" prefWidth="70.0" alignment="CENTER_RIGHT"/>
<ValidatingTextField fx:id="amountTextField" prefWidth="100.0" alignment="CENTER_RIGHT"/>
<Label text="BTC @"/>
<ValidatingTextField fx:id="priceTextField" prefWidth="100.0" alignment="CENTER_RIGHT"/>
<Label text="EUR ="/>
<ValidatingTextField fx:id="volumeTextField" prefWidth="70.0" alignment="CENTER_RIGHT"/>
<ValidatingTextField fx:id="volumeTextField" prefWidth="100.0" alignment="CENTER_RIGHT"/>
<Label text="EUR in total"/>
</HBox>

View file

@ -187,7 +187,7 @@ public class OrderBookController extends CachedViewController {
// handlers
amount.textProperty().addListener((observable, oldValue, newValue) -> {
orderBookFilter.setAmount(BSFormatter.parseToCoin(newValue));
orderBookFilter.setAmount(BSFormatter.parseToBtc(newValue));
updateVolume();
});
@ -348,7 +348,7 @@ public class OrderBookController extends CachedViewController {
Coin requestedAmount;
if (!"".equals(amount.getText())) {
requestedAmount = BSFormatter.parseToCoin(amount.getText());
requestedAmount = BSFormatter.parseToBtc(amount.getText());
}
else {
requestedAmount = offer.getAmount();

View file

@ -153,7 +153,7 @@ public class TakeOfferController extends CachedViewController {
@FXML
public void onTakeOffer() {
AddressEntry addressEntry = walletFacade.getAddressInfoByTradeID(offer.getId());
Coin amount = BSFormatter.parseToCoin(getAmountString());
Coin amount = BSFormatter.parseToBtc(getAmountString());
// TODO more validation (fee payment, blacklist,...)
if (amountTextField.isInvalid()) {
Popups.openErrorPopup("Invalid input", "The requested amount you entered is not a valid amount.");
@ -279,7 +279,7 @@ public class TakeOfferController extends CachedViewController {
}
private Coin getAmountInSatoshis() {
return BSFormatter.parseToCoin(getAmountString());
return BSFormatter.parseToBtc(getAmountString());
}
private String getAmountString() {
@ -304,12 +304,12 @@ public class TakeOfferController extends CachedViewController {
}
private Coin getCollateralAsCoin() {
Coin amountAsCoin = BSFormatter.parseToCoin(getAmountString());
Coin amountAsCoin = BSFormatter.parseToBtc(getAmountString());
return amountAsCoin.divide((long) (1d / offer.getCollateral()));
}
private String getFormattedCollateralAsBtc() {
Coin amountAsCoin = BSFormatter.parseToCoin(getAmountString());
Coin amountAsCoin = BSFormatter.parseToBtc(getAmountString());
Coin collateralAsCoin = amountAsCoin.divide((long) (1d / getCollateral()));
return BSFormatter.formatCoin(collateralAsCoin);
}

View file

@ -17,15 +17,17 @@
package io.bitsquare.gui.util;
import io.bitsquare.arbitrator.Arbitrator;
import io.bitsquare.locale.Country;
import io.bitsquare.locale.Localisation;
import io.bitsquare.trade.Direction;
import io.bitsquare.arbitrator.Arbitrator;
import com.google.bitcoin.core.Coin;
import com.google.bitcoin.utils.CoinFormat;
import com.google.bitcoin.utils.Fiat;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.DecimalFormat;
@ -67,6 +69,7 @@ public class BSFormatter {
/**
* Note that setting the locale does not set the currency as it might be independent.
*
* @param locale
*/
public static void setLocale(Locale locale) {
@ -96,7 +99,7 @@ public class BSFormatter {
}
}
public static Coin parseToCoin(String input) {
public static Coin parseToBtc(String input) {
try {
input = input.replace(",", ".");
Double.parseDouble(input); // test if valid double
@ -107,14 +110,38 @@ public class BSFormatter {
}
}
/**
* Converts to a coin with max. 4 decimal places. Last place gets rounded.
* 0.01234 -> 0.0123
* 0.01235 -> 0.0124
*
* @param input
* @return
*/
public static Coin parseToBtcWith4Decimals(String input) {
try {
input = input.replace(",", ".");
Double.parseDouble(input); // test if valid double
return parseToBtc(new BigDecimal(input).setScale(4, BigDecimal.ROUND_HALF_UP).toString());
} catch (Throwable t) {
log.warn("Exception at parseCoinTo4Decimals: " + t.toString());
return Coin.ZERO;
}
}
public static boolean hasBtcValidDecimals(String input) {
return parseToBtc(input).equals(parseToBtcWith4Decimals(input));
}
/**
* Transform a coin with the properties defined in the format (used to reduce decimal places)
*
* @param coin The coin which should be transformed
* @return The transformed coin
*/
public static Coin applyFormatRules(Coin coin) {
return parseToCoin(formatBtc(coin));
public static Coin reduceto4Dezimals(Coin coin) {
return parseToBtc(formatBtc(coin));
}
@ -150,7 +177,29 @@ public class BSFormatter {
}
}
/**
* Converts to a fiat with max. 2 decimal places. Last place gets rounded.
* 0.234 -> 0.23
* 0.235 -> 0.24
*
* @param input
* @return
*/
public static Fiat parseToFiatWith2Decimals(String input) {
try {
input = input.replace(",", ".");
Double.parseDouble(input); // test if valid double
return parseToFiat(new BigDecimal(input).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
} catch (Throwable t) {
log.warn("Exception at parseCoinTo4Decimals: " + t.toString());
return Fiat.valueOf(currencyCode, 0);
}
}
public static boolean hasFiatValidDecimals(String input) {
return parseToFiat(input).equals(parseToFiatWith2Decimals(input));
}
///////////////////////////////////////////////////////////////////////////////////////////
// Other
///////////////////////////////////////////////////////////////////////////////////////////

View file

@ -34,6 +34,8 @@ public class ValidationHelper {
/**
* Handles validation between minAmount and amount fields
* Min amount must not be larger as amount.
* Handles focus out events to display always the error popup from the field where the focus out happened.
*/
public static void setupMinAmountInRangeOfAmountValidation(ValidatingTextField amountTextField,
ValidatingTextField minAmountTextField,

View file

@ -17,8 +17,9 @@
package io.bitsquare;
import io.bitsquare.btc.BtcValidatorTest;
import io.bitsquare.btc.RestrictionsTest;
import io.bitsquare.gui.trade.createoffer.CreateOfferPresenterTest;
import io.bitsquare.gui.util.BSFormatterTest;
import io.bitsquare.gui.util.BitSquareConverterTest;
import io.bitsquare.gui.util.BitSquareNumberValidatorTest;
import io.bitsquare.gui.util.FiatValidatorTest;
@ -29,13 +30,14 @@ import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
BtcValidatorTest.class,
RestrictionsTest.class,
BitSquareConverterTest.class,
BitSquareNumberValidatorTest.class,
P2PNodeTest.class,
FiatValidatorTest.class,
BtcValidatorTest.class,
CreateOfferPresenterTest.class
RestrictionsTest.class,
CreateOfferPresenterTest.class,
BSFormatterTest.class
})
public class BitSquareTestSuite {

View file

@ -24,25 +24,25 @@ import org.junit.Test;
import static org.junit.Assert.*;
public class BtcValidatorTest {
public class RestrictionsTest {
@Test
public void testIsMinSpendableAmount() {
Coin amount = null;
assertFalse("tx unfunded, pending", BtcValidator.isMinSpendableAmount(amount));
assertFalse("tx unfunded, pending", Restrictions.isMinSpendableAmount(amount));
amount = Coin.ZERO;
assertFalse("tx unfunded, pending", BtcValidator.isMinSpendableAmount(amount));
assertFalse("tx unfunded, pending", Restrictions.isMinSpendableAmount(amount));
amount = FeePolicy.TX_FEE;
assertFalse("tx unfunded, pending", BtcValidator.isMinSpendableAmount(amount));
assertFalse("tx unfunded, pending", Restrictions.isMinSpendableAmount(amount));
amount = Transaction.MIN_NONDUST_OUTPUT;
assertFalse("tx unfunded, pending", BtcValidator.isMinSpendableAmount(amount));
assertFalse("tx unfunded, pending", Restrictions.isMinSpendableAmount(amount));
amount = FeePolicy.TX_FEE.add(Transaction.MIN_NONDUST_OUTPUT);
assertFalse("tx unfunded, pending", BtcValidator.isMinSpendableAmount(amount));
assertFalse("tx unfunded, pending", Restrictions.isMinSpendableAmount(amount));
amount = FeePolicy.TX_FEE.add(Transaction.MIN_NONDUST_OUTPUT).add(Coin.valueOf(1));
assertTrue("tx unfunded, pending", BtcValidator.isMinSpendableAmount(amount));
assertTrue("tx unfunded, pending", Restrictions.isMinSpendableAmount(amount));
}
}

View file

@ -42,17 +42,17 @@ public class CreateOfferPresenterTest {
BSFormatter.setLocale(Locale.US);
BSFormatter.setFiatCurrencyCode("USD");
CreateOfferPresenter presenter = new CreateOfferPresenter(model);
presenter.onViewInitialized();
model.collateralAsLong.set(100);
presenter.price.set("500");
presenter.amount.set("1");
assertEquals("500.00", presenter.volume.get());
assertEquals(Coin.COIN, model.amountAsCoin);
assertEquals(Fiat.valueOf("USD", 500 * 10000), model.priceAsFiat);
assertEquals(Fiat.valueOf("USD", 500 * 10000), model.tradeVolumeAsFiat);
assertEquals(Fiat.valueOf("USD", 500 * 10000), model.volumeAsFiat);
assertEquals(Coin.parseCoin("0.1011"), model.totalToPayAsCoin.get());
presenter.price.set("500");
@ -60,28 +60,28 @@ public class CreateOfferPresenterTest {
assertEquals("1.00", presenter.amount.get());
assertEquals(Coin.COIN, model.amountAsCoin);
assertEquals(Fiat.valueOf("USD", 500 * 10000), model.priceAsFiat);
assertEquals(Fiat.valueOf("USD", 500 * 10000), model.tradeVolumeAsFiat);
assertEquals(Fiat.valueOf("USD", 500 * 10000), model.volumeAsFiat);
presenter.price.set("300");
presenter.volume.set("1000");
assertEquals("3.3333", presenter.amount.get());
assertEquals(Coin.parseCoin("3.3333"), model.amountAsCoin);
assertEquals(Fiat.valueOf("USD", 300 * 10000), model.priceAsFiat);
assertEquals(Fiat.valueOf("USD", 9999900), model.tradeVolumeAsFiat);
assertEquals(Fiat.valueOf("USD", 9999900), model.volumeAsFiat);
presenter.price.set("300");
presenter.amount.set("3.3333");
assertEquals("999.99", presenter.volume.get());
assertEquals(Coin.parseCoin("3.3333"), model.amountAsCoin);
assertEquals(Fiat.valueOf("USD", 300 * 10000), model.priceAsFiat);
assertEquals(Fiat.valueOf("USD", 9999900), model.tradeVolumeAsFiat);
assertEquals(Fiat.valueOf("USD", 9999900), model.volumeAsFiat);
presenter.price.set("300");
presenter.amount.set("3.33333333");
assertEquals("999.99", presenter.volume.get());
assertEquals(Coin.parseCoin("3.3333"), model.amountAsCoin);
assertEquals(Fiat.valueOf("USD", 300 * 10000), model.priceAsFiat);
assertEquals(Fiat.valueOf("USD", 9999900), model.tradeVolumeAsFiat);
assertEquals(Fiat.valueOf("USD", 9999900), model.volumeAsFiat);
model.collateralAsLong.set(100);
@ -117,6 +117,10 @@ public class CreateOfferPresenterTest {
model.acceptedCountries.add(new Country(null, "Spain", null));
assertEquals("Italy, Spain", presenter.acceptedCountries.get());
}

View file

@ -0,0 +1,80 @@
/*
* This file is part of Bitsquare.
*
* Bitsquare is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bitsquare is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bitsquare. If not, see <http://www.gnu.org/licenses/>.
*/
package io.bitsquare.gui.util;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static io.bitsquare.gui.util.BSFormatter.*;
import static org.junit.Assert.*;
public class BSFormatterTest {
private static final Logger log = LoggerFactory.getLogger(BSFormatterTest.class);
@Test
public void testParseToBtcWith4Decimals() {
assertEquals("0", parseToBtcWith4Decimals("0").toPlainString());
assertEquals("0", parseToBtcWith4Decimals(null).toPlainString());
assertEquals("0", parseToBtcWith4Decimals("s").toPlainString());
assertEquals("0.0012", parseToBtcWith4Decimals("0.00123").toPlainString());
assertEquals("0.0013", parseToBtcWith4Decimals("0.00125").toPlainString());
assertEquals("0.0013", parseToBtcWith4Decimals("0,00125").toPlainString());
}
@Test
public void testHasBtcValidDecimals() {
assertTrue(hasBtcValidDecimals(null));
assertTrue(hasBtcValidDecimals("0"));
assertTrue(hasBtcValidDecimals("0,0001"));
assertTrue(hasBtcValidDecimals("0.0001"));
assertTrue(hasBtcValidDecimals("0.0009"));
assertTrue(hasBtcValidDecimals("20000000.0001"));
assertFalse(hasBtcValidDecimals("20000000.000123"));
assertFalse(hasBtcValidDecimals("0.00012"));
assertFalse(hasBtcValidDecimals("0.0001222312312312313"));
}
@Test
public void testParseToFiatWith2Decimals() {
assertEquals("0", parseToFiatWith2Decimals("0").toPlainString());
assertEquals("0", parseToFiatWith2Decimals(null).toPlainString());
assertEquals("0", parseToFiatWith2Decimals("s").toPlainString());
assertEquals("0.12", parseToFiatWith2Decimals("0.123").toPlainString());
assertEquals("0.13", parseToFiatWith2Decimals("0.125").toPlainString());
assertEquals("0.13", parseToFiatWith2Decimals("0,125").toPlainString());
}
@Test
public void testHasFiatValidDecimals() {
assertTrue(hasFiatValidDecimals(null));
assertTrue(hasFiatValidDecimals("0"));
assertTrue(hasFiatValidDecimals("0,01"));
assertTrue(hasFiatValidDecimals("0.01"));
assertTrue(hasFiatValidDecimals("0.09"));
assertTrue(hasFiatValidDecimals("20000000.01"));
assertFalse(hasFiatValidDecimals("20000000.0123"));
assertFalse(hasFiatValidDecimals("0.012"));
assertFalse(hasFiatValidDecimals("0.01222312312312313"));
}
}